Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9b71d921ffe5d4ed564342c4f2567e9e5659192e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/****************************************************************************
 * Copyright (c) 2004 Composent, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Composent, Inc. - initial API and implementation
 *****************************************************************************/
package org.eclipse.ecf.internal.provider.xmpp;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IAdapterManager;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.Namespace;
import org.eclipse.ecf.core.sharedobject.ISharedObject;
import org.eclipse.ecf.core.sharedobject.ISharedObjectConfig;
import org.eclipse.ecf.core.sharedobject.ISharedObjectContext;
import org.eclipse.ecf.core.sharedobject.SharedObjectInitException;
import org.eclipse.ecf.core.user.User;
import org.eclipse.ecf.core.util.Event;
import org.eclipse.ecf.internal.provider.xmpp.events.ChatMembershipEvent;
import org.eclipse.ecf.internal.provider.xmpp.events.MessageEvent;
import org.eclipse.ecf.internal.provider.xmpp.events.PresenceEvent;
import org.eclipse.ecf.presence.IIMMessageListener;
import org.eclipse.ecf.presence.IPresence;
import org.eclipse.ecf.presence.chatroom.ChatRoomMessage;
import org.eclipse.ecf.presence.chatroom.ChatRoomMessageEvent;
import org.eclipse.ecf.presence.chatroom.IChatRoomParticipantListener;
import org.eclipse.ecf.provider.xmpp.identity.XMPPID;
import org.eclipse.ecf.provider.xmpp.identity.XMPPRoomID;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Mode;
import org.jivesoftware.smack.packet.Presence.Type;

public class XMPPChatRoomContainerHelper implements ISharedObject {

	ISharedObjectConfig config = null;

	private final List messageListeners = new ArrayList();
	private Namespace usernamespace = null;
	private XMPPConnection connection = null;
	private final List participantListeners = new ArrayList();
	private ID roomID = null;

	private final List chatRoomContainerParticipants = Collections.synchronizedList(new ArrayList());

	protected void trace(String message) {

	}

	protected void addChatParticipantListener(IChatRoomParticipantListener listener) {
		synchronized (participantListeners) {
			participantListeners.add(listener);
		}
	}

	protected void removeChatParticipantListener(IChatRoomParticipantListener listener) {
		synchronized (participantListeners) {
			participantListeners.remove(listener);
		}
	}

	public XMPPChatRoomContainerHelper(Namespace usernamespace, XMPPConnection conn) {
		super();
		this.usernamespace = usernamespace;
		this.connection = conn;
	}

	protected ISharedObjectContext getContext() {
		return config.getContext();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObject#init(org.eclipse.ecf.core.ISharedObjectConfig)
	 */
	public void init(ISharedObjectConfig initData) throws SharedObjectInitException {
		this.config = initData;
	}

	protected ID createUserIDFromName(String name) {
		ID result = null;
		try {
			result = new XMPPID(usernamespace, name);
			return result;
		} catch (final Exception e) {
			return null;
		}
	}

	protected Message.Type[] ALLOWED_MESSAGES = {Message.Type.GROUP_CHAT};

	protected Message filterMessageType(Message msg) {
		for (int i = 0; i < ALLOWED_MESSAGES.length; i++) {
			if (ALLOWED_MESSAGES[i].equals(msg.getType())) {
				return msg;
			}
		}
		return null;
	}

	protected String canonicalizeRoomFrom(String from) {
		if (from == null)
			return null;
		final int atIndex = from.indexOf('@');
		String hostname = null;
		String username = null;
		final int index = from.indexOf("/");
		if (atIndex > 0 && index > 0) {
			hostname = from.substring(atIndex + 1, index);
			username = from.substring(index + 1);
			return username + "@" + hostname;
		}
		return from;
	}

	protected void fireMessageListeners(ID from, String body) {
		List toNotify = null;
		synchronized (messageListeners) {
			toNotify = new ArrayList(messageListeners);
		}
		for (final Iterator i = toNotify.iterator(); i.hasNext();) {
			final IIMMessageListener l = (IIMMessageListener) i.next();
			l.handleMessageEvent(new ChatRoomMessageEvent(from, new ChatRoomMessage(from, roomID, body)));
		}
	}

	protected String canonicalizeRoomTo(String to) {
		if (to == null)
			return null;
		final int index = to.indexOf("/");
		if (index > 0) {
			return to.substring(0, index);
		} else
			return to;
	}

	protected ID createRoomIDFromName(String from) {
		try {
			return new XMPPRoomID(usernamespace, connection, from);
		} catch (final URISyntaxException e) {
			return null;
		}
	}

	protected void handleMessageEvent(MessageEvent evt) {
		final Message msg = filterMessageType(evt.getMessage());
		if (msg != null)
			fireMessageListeners(createUserIDFromName(canonicalizeRoomFrom(msg.getFrom())), msg.getBody());
	}

	protected IPresence.Type createIPresenceType(Presence xmppPresence) {
		if (xmppPresence == null)
			return IPresence.Type.AVAILABLE;
		final Type type = xmppPresence.getType();
		if (type == Presence.Type.AVAILABLE) {
			return IPresence.Type.AVAILABLE;
		} else if (type == Presence.Type.ERROR) {
			return IPresence.Type.ERROR;
		} else if (type == Presence.Type.SUBSCRIBE) {
			return IPresence.Type.SUBSCRIBE;
		} else if (type == Presence.Type.SUBSCRIBED) {
			return IPresence.Type.SUBSCRIBED;
		} else if (type == Presence.Type.UNSUBSCRIBE) {
			return IPresence.Type.UNSUBSCRIBE;
		} else if (type == Presence.Type.UNSUBSCRIBED) {
			return IPresence.Type.UNSUBSCRIBED;
		} else if (type == Presence.Type.UNAVAILABLE) {
			return IPresence.Type.UNAVAILABLE;
		}
		return IPresence.Type.AVAILABLE;
	}

	protected IPresence.Mode createIPresenceMode(Presence xmppPresence) {
		if (xmppPresence == null)
			return IPresence.Mode.AVAILABLE;
		final Mode mode = xmppPresence.getMode();
		if (mode == Presence.Mode.AVAILABLE) {
			return IPresence.Mode.AVAILABLE;
		} else if (mode == Presence.Mode.AWAY) {
			return IPresence.Mode.AWAY;
		} else if (mode == Presence.Mode.CHAT) {
			return IPresence.Mode.CHAT;
		} else if (mode == Presence.Mode.DO_NOT_DISTURB) {
			return IPresence.Mode.DND;
		} else if (mode == Presence.Mode.EXTENDED_AWAY) {
			return IPresence.Mode.EXTENDED_AWAY;
		} else if (mode == Presence.Mode.INVISIBLE) {
			return IPresence.Mode.INVISIBLE;
		}
		return IPresence.Mode.AVAILABLE;
	}

	protected IPresence createIPresence(Presence xmppPresence) {
		final String status = xmppPresence.getStatus();
		final IPresence newPresence = new org.eclipse.ecf.presence.Presence(createIPresenceType(xmppPresence), status, createIPresenceMode(xmppPresence));
		return newPresence;
	}

	protected void disconnect() {
		chatRoomContainerParticipants.clear();
		setRoomID(null);
	}

	protected void handlePresenceEvent(PresenceEvent evt) {
		final Presence xmppPresence = evt.getPresence();
		final String from = canonicalizeRoomFrom(xmppPresence.getFrom());
		final IPresence newPresence = createIPresence(xmppPresence);
		final ID fromID = createUserIDFromName(from);
		if (newPresence.getType().equals(IPresence.Type.AVAILABLE)) {
			if (!chatRoomContainerParticipants.contains(fromID))
				chatRoomContainerParticipants.add(fromID);
		} else
			chatRoomContainerParticipants.remove(fromID);
		fireParticipant(fromID, newPresence);
	}

	protected void handleChatMembershipEvent(ChatMembershipEvent evt) {
		final String from = canonicalizeRoomFrom(evt.getFrom());
		final ID fromID = createUserIDFromName(from);
		fireChatParticipant(fromID, evt.isAdd());
	}

	protected void fireParticipant(ID fromID, IPresence presence) {
		List toNotify = null;
		synchronized (participantListeners) {
			toNotify = new ArrayList(participantListeners);
		}
		for (final Iterator i = toNotify.iterator(); i.hasNext();) {
			final IChatRoomParticipantListener l = (IChatRoomParticipantListener) i.next();
			l.handlePresenceUpdated(fromID, presence);
		}
	}

	protected void fireChatParticipant(ID fromID, boolean join) {
		List toNotify = null;
		synchronized (participantListeners) {
			toNotify = new ArrayList(participantListeners);
		}
		for (final Iterator i = toNotify.iterator(); i.hasNext();) {
			final IChatRoomParticipantListener l = (IChatRoomParticipantListener) i.next();
			if (join) {
				l.handleArrived(new User(fromID));
			} else {
				l.handleDeparted(new User(fromID));
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObject#handleEvent(org.eclipse.ecf.core.util.Event)
	 */
	public void handleEvent(Event event) {
		trace("handleEvent(" + event + ")");
		if (event instanceof MessageEvent) {
			handleMessageEvent((MessageEvent) event);
		} else if (event instanceof PresenceEvent) {
			handlePresenceEvent((PresenceEvent) event);
		} else if (event instanceof ChatMembershipEvent) {
			handleChatMembershipEvent((ChatMembershipEvent) event);
		} else
			trace("unrecognized event " + event);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObject#handleEvents(org.eclipse.ecf.core.util.Event[])
	 */
	public void handleEvents(Event[] events) {
		for (int i = 0; i < events.length; i++) {
			this.handleEvent(events[i]);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObject#dispose(org.eclipse.ecf.core.identity.ID)
	 */
	public void dispose(ID containerID) {
		synchronized (messageListeners) {
			messageListeners.clear();
		}
		synchronized (participantListeners) {
			participantListeners.clear();
		}
		synchronized (chatRoomContainerParticipants) {
			chatRoomContainerParticipants.clear();
		}
		this.config = null;
		this.connection = null;
		this.usernamespace = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObject#getAdapter(java.lang.Class)
	 */
	public Object getAdapter(Class adapter) {
		if (adapter == null)
			return null;
		if (adapter.isInstance(this))
			return this;
		final IAdapterManager adapterManager = XmppPlugin.getDefault().getAdapterManager();
		return (adapterManager == null) ? null : adapterManager.loadAdapter(this, adapter.getName());
	}

	/**
	 * @param msgListener
	 */
	protected void addChatRoomMessageListener(IIMMessageListener msgListener) {
		synchronized (messageListeners) {
			messageListeners.add(msgListener);
		}
	}

	/**
	 * @param msgListener
	 */
	public void removeChatRoomMessageListener(IIMMessageListener msgListener) {
		synchronized (messageListeners) {
			messageListeners.remove(msgListener);
		}
	}

	/**
	 * @param remoteServerID
	 */
	protected void setRoomID(ID roomID) {
		this.roomID = roomID;
	}

	/**
	 * @return array of IDs of chat room participants.
	 */
	public ID[] getChatRoomParticipants() {
		return (ID[]) chatRoomContainerParticipants.toArray(new ID[] {});
	}
}

Back to the top