aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/irc/BasicPollerPresenceWatcher.java
blob: c82058e7d4d24f884e841a20eae14f92f35370a9 (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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
 * Jitsi, 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 java.util.concurrent.atomic.*;

import net.java.sip.communicator.util.*;

import com.ircclouds.irc.api.*;
import com.ircclouds.irc.api.domain.*;
import com.ircclouds.irc.api.domain.messages.*;
import com.ircclouds.irc.api.state.*;

class BasicPollerPresenceWatcher
    implements PresenceWatcher
{
    /**
     * Logger.
     */
    private static final Logger LOGGER = Logger
        .getLogger(BasicPollerPresenceWatcher.class);

    /**
     * Delay before starting the presence watcher task for the first time.
     */
    private static final long INITIAL_PRESENCE_WATCHER_DELAY = 10000L;

    /**
     * Period for the presence watcher timer.
     */
    private static final long PRESENCE_WATCHER_PERIOD = 60000L;

    /**
     * Instance of IRCAPi.
     */
    private final IRCApi irc;

    /**
     * Connection state instance.
     */
    private final IIRCState connectionState;

    /**
     * The persistent presence operation set used to issue presence updates.
     */
    private final OperationSetPersistentPresenceIrcImpl operationSet;

    /**
     * Synchronized set of nicks to watch for presence changes.
     */
    private final SortedSet<String> nickWatchList;

    /**
     * Constructor.
     *
     * @param irc the IRCApi instance
     * @param connectionState the connection state
     * @param operationSet the persistent presence operation set
     * @param nickWatchList the nick watch list
     * @param serverIdentity the server identity
     */
    BasicPollerPresenceWatcher(final IRCApi irc,
        final IIRCState connectionState,
        final OperationSetPersistentPresenceIrcImpl operationSet,
        final SortedSet<String> nickWatchList,
        final AtomicReference<String> serverIdentity)
    {
        if (irc == null)
        {
            throw new IllegalArgumentException("irc cannot be null");
        }
        this.irc = irc;
        if (connectionState == null)
        {
            throw new IllegalArgumentException("connectionState cannot be null");
        }
        this.connectionState = connectionState;
        if (operationSet == null)
        {
            throw new IllegalArgumentException("operationSet cannot be null");
        }
        this.operationSet = operationSet;
        if (nickWatchList == null)
        {
            throw new IllegalArgumentException("nickWatchList cannot be null");
        }
        this.nickWatchList = nickWatchList;
        setUpPresenceWatcher(serverIdentity);
    }

    /**
     * Set up a timer for watching the presence of nicks in the watch list.
     */
    private void setUpPresenceWatcher(
        final AtomicReference<String> serverIdentity)
    {
        // FIFO query list to be shared between presence watcher task and
        // presence reply listener.
        final List<List<String>> queryList =
            Collections.synchronizedList(new LinkedList<List<String>>());
        final Timer presenceWatcher = new Timer();
        irc.addListener(new PresenceReplyListener(presenceWatcher, queryList));
        final PresenceWatcherTask task =
            new PresenceWatcherTask(this.nickWatchList, queryList,
                serverIdentity);
        presenceWatcher.schedule(task, INITIAL_PRESENCE_WATCHER_DELAY,
            PRESENCE_WATCHER_PERIOD);
        LOGGER.trace("Basic Poller presence watcher set up.");
    }

    @Override
    public void add(String nick)
    {
        this.nickWatchList.add(nick);
    }

    @Override
    public void remove(String nick)
    {
        this.nickWatchList.remove(nick);
    }

    /**
     * Task for watching nick presence.
     *
     * @author Danny van Heumen
     */
    private final class PresenceWatcherTask extends TimerTask
    {
        /**
         * Static overhead for ISON response message.
         *
         * Additional 10 chars extra overhead as fail-safe, as I was not able to
         * find the exact number in the overhead computation.
         */
        private static final int ISON_RESPONSE_STATIC_MESSAGE_OVERHEAD = 18;

        /**
         * List containing nicks that must be watched.
         */
        private final SortedSet<String> watchList;

        /**
         * FIFO list storing each ISON query that is sent, for use when
         * responses return.
         */
        private final List<List<String>> queryList;

        /**
         * Reference to the current server identity.
         */
        private final AtomicReference<String> serverIdentity;

        /**
         * Constructor.
         *
         * @param watchList the list of nicks to watch
         * @param queryList list containing list of nicks of each ISON query
         * @param serverIdentity container with the current server identity for
         *            use in overhead calculation
         */
        public PresenceWatcherTask(final SortedSet<String> watchList,
            final List<List<String>> queryList,
            final AtomicReference<String> serverIdentity)
        {
            if (watchList == null)
            {
                throw new IllegalArgumentException("watchList cannot be null");
            }
            this.watchList = watchList;
            if (queryList == null)
            {
                throw new IllegalArgumentException("queryList cannot be null");
            }
            this.queryList = queryList;
            if (serverIdentity == null)
            {
                throw new IllegalArgumentException(
                    "serverIdentity reference cannot be null");
            }
            this.serverIdentity = serverIdentity;
        }

        /**
         * The implementation of the task.
         */
        @Override
        public void run()
        {
            if (this.watchList.isEmpty())
            {
                LOGGER.trace("Watch list is empty. Not querying for online "
                    + "presence.");
                return;
            }
            if (this.serverIdentity.get() == null)
            {
                LOGGER.trace("Server identity not available yet. Skipping "
                    + "this presence status query.");
                return;
            }
            LOGGER
                .trace("Watch list contains nicks: querying presence status.");
            final StringBuilder query = new StringBuilder();
            final LinkedList<String> list;
            synchronized (this.watchList)
            {
                list = new LinkedList<String>(this.watchList);
            }
            LinkedList<String> nicks = new LinkedList<String>();
            // The ISON reply contains the most overhead, so base the maximum
            // number of nicks limit on that.
            final int maxQueryLength =
                MessageManager.IRC_PROTOCOL_MAXIMUM_MESSAGE_SIZE - overhead();
            for (String nick : list)
            {
                if (query.length() + nick.length() >= maxQueryLength)
                {
                    this.queryList.add(nicks);
                    BasicPollerPresenceWatcher.this.irc
                        .rawMessage(createQuery(query));
                    // Initialize new data types
                    query.delete(0, query.length());
                    nicks = new LinkedList<String>();
                }
                query.append(nick);
                query.append(' ');
                nicks.add(nick);
            }
            if (query.length() > 0)
            {
                // Send remaining entries.
                this.queryList.add(nicks);
                BasicPollerPresenceWatcher.this.irc
                    .rawMessage(createQuery(query));
            }
        }

        /**
         * Create an ISON query from the StringBuilder containing the list of
         * nicks.
         *
         * @param nicklist the list of nicks as a StringBuilder instance
         * @return returns the ISON query string
         */
        private String createQuery(final StringBuilder nicklist)
        {
            return "ISON " + nicklist;
        }

        /**
         * Calculate overhead for ISON response message.
         *
         * @return returns amount of overhead in response message
         */
        private int overhead()
        {
            return ISON_RESPONSE_STATIC_MESSAGE_OVERHEAD
                + this.serverIdentity.get().length()
                + BasicPollerPresenceWatcher.this.connectionState.getNickname()
                    .length();
        }
    }

    /**
     * Presence reply listener.
     *
     * Listener that acts on various replies that give an indication of actual
     * presence or presence changes, such as RPL_ISON and ERR_NOSUCHNICKCHAN.
     *
     * @author Danny van Heumen
     */
    private final class PresenceReplyListener
        extends AbstractIrcMessageListener
    {
        /**
         * Reply for ISON query.
         */
        private static final int RPL_ISON = 303;

        /**
         * Error reply in case nick does not exist on server.
         */
        private static final int ERR_NOSUCHNICK = 401;

        /**
         * Timer for presence watcher task.
         */
        private final Timer timer;

        /**
         * FIFO list containing list of nicks for each query.
         */
        private final List<List<String>> queryList;

        /**
         * Constructor.
         *
         * @param timer Timer for presence watcher task
         * @param queryList List of executed queries with expected nicks lists.
         */
        public PresenceReplyListener(final Timer timer,
            final List<List<String>> queryList)
        {
            super(BasicPollerPresenceWatcher.this.irc,
                BasicPollerPresenceWatcher.this.connectionState);
            if (timer == null)
            {
                throw new IllegalArgumentException("timer cannot be null");
            }
            this.timer = timer;
            if (queryList == null)
            {
                throw new IllegalArgumentException("queryList cannot be null");
            }
            this.queryList = queryList;
        }

        /**
         * Update nick watch list upon receiving a nick change message for a
         * nick that is on the watch list.
         *
         * NOTE: This nick change event could be handled earlier than the
         * handler that fires the contact rename event. This will result in a
         * missed presence update. However, since the nick change was just
         * announced, it is reasonable to assume that the user is still online.
         *
         * @param msg the nick message
         */
        @Override
        public void onNickChange(final NickMessage msg)
        {
            if (msg == null || msg.getSource() == null)
            {
                return;
            }
            final String oldNick = msg.getSource().getNick();
            final String newNick = msg.getNewNick();
            if (oldNick == null || newNick == null)
            {
                LOGGER.error("Incomplete nick change message. Old nick: '"
                    + oldNick + "', new nick: '" + newNick + "'.");
                return;
            }
            synchronized (BasicPollerPresenceWatcher.this.nickWatchList)
            {
                if (BasicPollerPresenceWatcher.this.nickWatchList
                    .contains(oldNick))
                {
                    update(oldNick, IrcStatusEnum.OFFLINE);
                }
                if (BasicPollerPresenceWatcher.this.nickWatchList
                    .contains(newNick))
                {
                    update(newNick, IrcStatusEnum.ONLINE);
                }
            }
        }

        /**
         * Message handling for RPL_ISON message and other indicators.
         *
         * @param msg the message
         */
        @Override
        public void onServerNumericMessage(final ServerNumericMessage msg)
        {
            if (msg == null || msg.getNumericCode() == null)
            {
                return;
            }
            switch (msg.getNumericCode())
            {
            case RPL_ISON:
                final String[] nicks = msg.getText().substring(1).split(" ");
                final List<String> offline;
                if (this.queryList.isEmpty())
                {
                    // If no query list exists, we can only update nicks that
                    // are online, since we do not know who we have actually
                    // queried for.
                    offline = new LinkedList<String>();
                }
                else
                {
                    offline = this.queryList.remove(0);
                }
                for (String nick : nicks)
                {
                    update(nick, IrcStatusEnum.ONLINE);
                    offline.remove(nick);
                }
                for (String nick : offline)
                {
                    update(nick, IrcStatusEnum.OFFLINE);
                }
                break;
            case ERR_NOSUCHNICK:
                final String errortext = msg.getText();
                final int idx = errortext.indexOf(' ');
                if (idx == -1)
                {
                    LOGGER.info("ERR_NOSUCHNICK message does not have "
                        + "expected format.");
                    return;
                }
                final String errNick = errortext.substring(0, idx);
                update(errNick, IrcStatusEnum.OFFLINE);
                break;
            default:
                break;
            }
        }

        /**
         * Upon receiving a private message from a user, conclude that the user
         * must then be online and update its presence status.
         *
         * @param msg the message
         */
        @Override
        public void onUserPrivMessage(final UserPrivMsg msg)
        {
            if (msg == null || msg.getSource() == null)
            {
                return;
            }
            final IRCUser user = msg.getSource();
            update(user.getNick(), IrcStatusEnum.ONLINE);
        }

        /**
         * Upon receiving a notice from a user, conclude that the user
         * must then be online and update its presence status.
         *
         * @param msg the message
         */
        @Override
        public void onUserNotice(final UserNotice msg)
        {
            if (msg == null || msg.getSource() == null)
            {
                return;
            }
            final IRCUser user = msg.getSource();
            update(user.getNick(), IrcStatusEnum.ONLINE);
        }

        /**
         * Upon receiving an action from a user, conclude that the user
         * must then be online and update its presence status.
         *
         * @param msg the message
         */
        @Override
        public void onUserAction(final UserActionMsg msg)
        {
            if (msg == null || msg.getSource() == null)
            {
                return;
            }
            final IRCUser user = msg.getSource();
            update(user.getNick(), IrcStatusEnum.ONLINE);
        }

        /**
         * Handler for channel join events.
         */
        @Override
        public void onChannelJoin(final ChanJoinMessage msg)
        {
            if (msg == null || msg.getSource() == null)
            {
                return;
            }
            final String user = msg.getSource().getNick();
            update(user, IrcStatusEnum.ONLINE);
        }

        /**
         * Handler for user quit events.
         *
         * @param msg the quit message
         */
        @Override
        public void onUserQuit(final QuitMessage msg)
        {
            super.onUserQuit(msg);
            final String user = msg.getSource().getNick();
            if (user == null)
            {
                return;
            }
            if (localUser(user))
            {
                // Stop presence watcher task.
                this.timer.cancel();
                updateAll(IrcStatusEnum.OFFLINE);
            }
            else
            {
                update(user, IrcStatusEnum.OFFLINE);
            }
        }

        /**
         * In case a fatal error occurs, remove the listener.
         *
         * @param msg the error message
         */
        @Override
        public void onError(final ErrorMessage msg)
        {
            super.onError(msg);
            // Stop presence watcher task.
            this.timer.cancel();
            updateAll(IrcStatusEnum.OFFLINE);
        }

        /**
         * Update the status of a single nick.
         *
         * @param nick the nick to update
         * @param status the new status
         */
        private void update(final String nick, final IrcStatusEnum status)
        {
            // User is some other user, so check if we are watching that nick.
            if (!BasicPollerPresenceWatcher.this.nickWatchList.contains(nick))
            {
                return;
            }
            BasicPollerPresenceWatcher.this.operationSet
                .updateNickContactPresence(nick, status);
        }

        /**
         * Update the status of all contacts in the nick watch list.
         *
         * @param status the new status
         */
        private void updateAll(final IrcStatusEnum status)
        {
            final LinkedList<String> list;
            synchronized (BasicPollerPresenceWatcher.this.nickWatchList)
            {
                list =
                    new LinkedList<String>(
                        BasicPollerPresenceWatcher.this.nickWatchList);
            }
            for (String nick : list)
            {
                BasicPollerPresenceWatcher.this.operationSet
                    .updateNickContactPresence(nick, status);
            }
        }
    }
}