aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/irc/OperationSetMultiUserChatIrcImpl.java
blob: 10fad62761f41e3a2d0820d0b92da21b9d18ceec (plain)
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.protocol.irc;

import java.util.*;

import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;

/**
 * Allows creating, configuring, joining and administrating of individual
 * text-based conference rooms.
 *
 * @author Stephane Remy
 * @author Loic Kempf
 * @author Yana Stamcheva
 */
public class OperationSetMultiUserChatIrcImpl
    implements OperationSetMultiUserChat
{

    private static final Logger logger =
        Logger.getLogger(OperationSetMultiUserChatIrcImpl.class);

    /**
     * A call back to the IRC provider that created us.
     */
    private ProtocolProviderServiceIrcImpl ircProvider = null;

    /**
     * A list of listeners subscribed for invitations multi-user chat events.
     */
    private List invitationListeners = new LinkedList();

    /**
     * A list of listeners subscribed for events indicating rejection of a
     * multi user chat invitation sent by us.
     */
    private List invitationRejectionListeners = new LinkedList();

    /**
     * list of this chat room local user status listener
     */
    private ArrayList localUserPresenceListeners = new ArrayList();

    /**
     * A list of the rooms that are currently open by this account. Note that
     * we have not necessarily joined these rooms, we might have simply been
     * searching through them.
     */
    private Hashtable chatRoomCache = new Hashtable();

    /**
     * A list of all private rooms opened by user on this server. These rooms
     * are a result of exchange of private messages between the local user and
     * some of the other chat room members.
     */
    private Hashtable privateRoomCache = new Hashtable();

    /**
     * The <tt>ChatRoom</tt> corresponding to the IRC server channel. This chat
     * room is not returned by any of methods getExistingChatRooms(),
     * getCurrentlyJoinedChatRooms, etc.
     */
    private ChatRoomIrcImpl serverChatRoom;

    /**
     * Instantiates the user operation set with a currently valid instance of
     * the irc protocol provider.
     * @param provider a currently valid instance of
     * ProtocolProviderServiceIrcImpl.
     */
    public OperationSetMultiUserChatIrcImpl(
        ProtocolProviderServiceIrcImpl provider)
    {
        this.ircProvider = provider;
    }

    /**
     * Returns the <tt>List</tt> of <tt>ChatRoom</tt>s currently available on
     * the server that this protocol provider is connected to.
     *
     * @return a <tt>java.util.List</tt> of <tt>ChatRoom</tt>s that are
     * currently available on the server that this protocol provider is
     * connected to.
     *
     * @throws OperationFailedException if we faile retrieving this list from
     * the server.
     */
    public List getExistingChatRooms() throws OperationFailedException
    {
        return ircProvider.getIrcStack().getServerChatRoomList();
    }

    /**
     * Returns a list of the chat rooms that we have joined and are currently
     * active in.
     *
     * @return a <tt>List</tt> of the rooms where the user has joined using a
     * given connection.
     */
    public List getCurrentlyJoinedChatRooms()
    {
        synchronized(chatRoomCache)
        {
            List joinedRooms
                = new LinkedList(this.chatRoomCache.values());

            Iterator joinedRoomsIter = joinedRooms.iterator();

            while (joinedRoomsIter.hasNext())
            {
                if ( !( (ChatRoom) joinedRoomsIter.next()).isJoined())
                    joinedRoomsIter.remove();
            }

            return joinedRooms;
        }
    }

    /**
     * Returns a list of the chat rooms that <tt>chatRoomMember</tt> has joined
     * and is currently active in.
     *
     * @param chatRoomMember the chat room member whose current ChatRooms we
     * will be querying.
     * @return a list of the chat rooms that <tt>chatRoomMember</tt> has joined
     * and is currently active in.
     */
    public List getCurrentlyJoinedChatRooms(ChatRoomMember chatRoomMember)
    {
        //TODO: Implement "who is" for the IRC stack.
        return null;
    }

    /**
     * Creates a room with the named <tt>roomName</tt> and according to the
     * specified <tt>roomProperties</tt> on the server that this protocol
     * provider is currently connected to. When the method returns the room the
     * local user will not have joined it and thus will not receive messages on
     * it until the <tt>ChatRoom.join()</tt> method is called.
     * <p>
     * @param roomName the name of the <tt>ChatRoom</tt> to create.
     * @param roomProperties properties specifying how the room should be
     * created.
     * @throws OperationFailedException if the room couldn't be created for some
     * reason (e.g. room already exists; user already joined to an existent
     * room or user has no permissions to create a chat room).
     * @throws OperationNotSupportedException if chat room creation is not
     * supported by this server
     *
     * @return the newly created <tt>ChatRoom</tt> named <tt>roomName</tt>.
     */
    public ChatRoom createChatRoom(String roomName, Hashtable roomProperties)
        throws OperationFailedException, OperationNotSupportedException
    {
        Object newChatRoom = this.findRoom(roomName);

        return ((ChatRoom) newChatRoom);
    }

    /**
     * Returns a reference to a chatRoom named <tt>roomName</tt> or null if
     * no such room exists.
     * <p>
     * @param roomName the name of the <tt>ChatRoom</tt> that we're looking for.
     * @return the <tt>ChatRoom</tt> named <tt>roomName</tt> or null if no such
     * room exists on the server that this provider is currently connected to.
     */
    public ChatRoom findRoom(String roomName)
    {
        //first check whether we have already initialized the room.
        ChatRoom room = (ChatRoom)chatRoomCache.get(roomName);

        //if yes - we return it
        if(room != null)
        {
            return room;
        }
        //if not, we create it.
        else
        {
            return createLocalChatRoomInstance(roomName);
        }
    }

    /**
     * Returns the room corresponding to the server channel.
     * 
     * @return the room corresponding to the server channel
     */
    public ChatRoom getSystemRoom()
    {
        return serverChatRoom;
    }

    /**
     * Informs the sender of an invitation that we decline their invitation.
     *
     * @param invitation the invitation we are rejecting.
     * @param reason the reason of rejecting
     */
    public void rejectInvitation(ChatRoomInvitation invitation, String reason)
    {
        //TODO: Implement reject invitation.
    }

    /**
     * Adds a listener to invitation notifications. The listener will be fired
     * anytime an invitation is received.
     *
     * @param listener an invitation listener.
     */
    public void addInvitationListener(ChatRoomInvitationListener listener)
    {
        synchronized(invitationListeners)
        {
            if (!invitationListeners.contains(listener))
                invitationListeners.add(listener);
        }
    }

    /**
     * Removes <tt>listener</tt> from the list of invitation listeners
     * registered to receive invitation events.
     *
     * @param listener the invitation listener to remove.
     */
    public void removeInvitationListener(ChatRoomInvitationListener listener)
    {
        synchronized(invitationListeners)
        {
            invitationListeners.remove(listener);
        }
    }

    /**
     * Adds a listener to invitation notifications. The listener will be fired
     * anytime an invitation is received.
     *
     * @param listener an invitation listener.
     */
    public void addInvitationRejectionListener(
        ChatRoomInvitationRejectionListener listener)
    {
        synchronized(invitationRejectionListeners)
        {
            if (!invitationRejectionListeners.contains(listener))
                invitationRejectionListeners.add(listener);
        }
    }

    /**
     * Removes <tt>listener</tt> from the list of invitation listeners
     * registered to receive invitation rejection events.
     *
     * @param listener the invitation listener to remove.
     */
    public void removeInvitationRejectionListener(
        ChatRoomInvitationRejectionListener listener)
    {
        synchronized(invitationRejectionListeners)
        {
            invitationRejectionListeners.remove(listener);
        }
    }

    /**
     * Returns true if <tt>contact</tt> supports multi-user chat sessions.
     *
     * @param contact reference to the contact whose support for chat rooms
     * we are currently querying.
     * @return a boolean indicating whether <tt>contact</tt> supports chat
     * rooms.
     */
    public boolean isMultiChatSupportedByContact(Contact contact)
    {
        //TODO: Implement isMultiChatSupportedByContact.
        return true;
    }

    /**
     * Adds a listener that will be notified of changes in our status in a chat
     * room such as us being kicked, banned or dropped.
     *
     * @param listener the <tt>LocalUserChatRoomPresenceListener</tt>.
     */
    public void addPresenceListener(LocalUserChatRoomPresenceListener listener)
    {
        synchronized (localUserPresenceListeners)
        {
            if (!localUserPresenceListeners.contains(listener))
                localUserPresenceListeners.add(listener);
        }
    }

    /**
     * Removes a listener that was being notified of changes in our status in
     * a room such as us being kicked, banned or dropped.
     *
     * @param listener the <tt>LocalUserChatRoomPresenceListener</tt>.
     */
    public void removePresenceListener(
            LocalUserChatRoomPresenceListener listener)
    {
        synchronized (localUserPresenceListeners)
        {
            localUserPresenceListeners.remove(listener);
        }
    }

    /**
     * Delivers a <tt>LocalUserChatRoomPresenceChangeEvent</tt> to all
     * registered <tt>LocalUserChatRoomPresenceListener</tt>s.
     * 
     * @param chatRoom the <tt>ChatRoom</tt> which has been joined, left, etc.
     * @param eventType the type of this event; one of LOCAL_USER_JOINED,
     * LOCAL_USER_LEFT, etc.
     * @param reason the reason
     */
    protected void fireLocalUserPresenceEvent(  ChatRoom chatRoom,
                                                String eventType,
                                                String reason)
    {
        LocalUserChatRoomPresenceChangeEvent evt
            = new LocalUserChatRoomPresenceChangeEvent( this,
                                                        chatRoom,
                                                        eventType,
                                                        reason);
        
        Iterator listeners = null;
        synchronized (localUserPresenceListeners)
        {
            listeners = new ArrayList(localUserPresenceListeners).iterator();
        }

        while (listeners.hasNext())
        {
            LocalUserChatRoomPresenceListener listener
                = (LocalUserChatRoomPresenceListener) listeners.next();
            
            listener.localUserPresenceChanged(evt);
        }
    }

    /**
     * Returns a reference to the chat room named <tt>chatRoomName</tt> or
     * null if the room hasn't been cached yet.
     *
     * @param chatRoomName the name of the room we're looking for.
     *
     * @return the <tt>ChatRoomJabberImpl</tt> instance that has been cached
     * for <tt>chatRoomName</tt> or null if no such room has been cached so far.
     */
    protected ChatRoomIrcImpl getChatRoom(String chatRoomName)
    {
        return (ChatRoomIrcImpl)this.chatRoomCache.get(chatRoomName);
    }

    /**
     * Creates a <tt>ChatRoom</tt> from the specified chat room name.
     *
     * @param chatRoomName the name of the chat room to add
     *
     * @return ChatRoom the chat room that we've just created.
     */
    private ChatRoom createLocalChatRoomInstance(String chatRoomName)
    {
        synchronized(chatRoomCache)
        {
            ChatRoomIrcImpl chatRoom
                = new ChatRoomIrcImpl(chatRoomName, ircProvider);

            this.chatRoomCache.put(chatRoom.getName(), chatRoom);

            return chatRoom;
        }
    }

    /**
     * Returns the private room corresponding to the given nick name.
     * 
     * @param nickName the nickName of the person for which the private room is.
     * @return the private room corresponding to the given nick name
     */
    protected ChatRoomIrcImpl findPrivateChatRoom(String nickName)
    {
        synchronized(privateRoomCache)
        {
            if(privateRoomCache.contains(nickName))
                return (ChatRoomIrcImpl) privateRoomCache.get(nickName);

            ChatRoomIrcImpl chatRoom
                = new ChatRoomIrcImpl(nickName, ircProvider);

            this.chatRoomCache.put(nickName, chatRoom);

            return chatRoom;
        }
    }

    /**
     * Creates the chat room corresponding to the IRC server channel.
     */
    protected ChatRoomIrcImpl createSystemRoom()
    {
        serverChatRoom = new ChatRoomIrcImpl(
            ircProvider.getAccountID().getService(), ircProvider);

        return serverChatRoom;
    }

    /**
     * Delivers a <tt>ChatRoomInvitationReceivedEvent</tt> to all
     * registered <tt>ChatRoomInvitationListener</tt>s.
     * 
     * @param targetChatRoom the room that invitation refers to
     * @param inviter the inviter that sent the invitation
     * @param reason the reason why the inviter sent the invitation
     * @param password the password to use when joining the room 
     */
    protected void fireInvitationEvent(ChatRoom targetChatRoom,
                                    String inviter,
                                    String reason,
                                    byte[] password)
    {
        ChatRoomInvitationIrcImpl invitation
            = new ChatRoomInvitationIrcImpl(targetChatRoom,
                                            inviter,
                                            reason,
                                            password);

        ChatRoomInvitationReceivedEvent evt
            = new ChatRoomInvitationReceivedEvent(this, invitation,
                new Date(System.currentTimeMillis()));

        Iterator listeners = null;
        synchronized (invitationListeners)
        {
            listeners = new ArrayList(invitationListeners).iterator();
        }
        
        while (listeners.hasNext())
        {
            ChatRoomInvitationListener listener
                = (ChatRoomInvitationListener) listeners.next();

            listener.invitationReceived(evt);
        }
    }
}