aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/irc/IrcStack.java
blob: 609ad51e3195c760ee72ff6f7bff21f49c887fe5 (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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
/*
 * 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.io.*;
import java.util.*;

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

import org.jibble.pircbot.*;

/**
 * An implementation of the PircBot IRC stack.
 * 
 * @author Stephane Remy
 * @author Loic Kempf
 * @author Yana Stamcheva
 */
public class IrcStack
    extends PircBot
{
    private static final Logger logger = Logger.getLogger(IrcStack.class);

    /**
     * Timeout for server response.
     */
    private static final int TIMEOUT = 10000;

    /**
     * A list of timers indicating when a chat room join fails.
     */
    private final Map<ChatRoom, Timer> joinTimeoutTimers
        = new Hashtable<ChatRoom, Timer>();

    /**
     * A list of the channels on this server
     */
    private final List<String> serverChatRoomList = new ArrayList<String>();

    /**
     * A list of users that we have info about, it is used to stock "whois"
     * responses
     */
    private final Map<String, UserInfo> userInfoTable
        = new Hashtable<String, UserInfo>();

    /**
     * The IRC multi-user chat operation set.
     */
    private final OperationSetMultiUserChatIrcImpl ircMUCOpSet;

    /**
     * The IRC protocol provider service.
     */
    private final ProtocolProviderServiceIrcImpl parentProvider;


    private final Object operationLock = new Object();

    /**
     * The operation response code indicates 
     */
    private int operationResponseCode = 0;

    /**
     * Indicates if the IRC server has been initialized.
     */
    private boolean isInitialized = false;

    /**
     * Keeps all join requests received before the server is initialized.
     */
    private final List<ChatRoom> joinCache = new ArrayList<ChatRoom>();

    /**
     * The indicator which determines whether #onConnect() has been invoked and
     * thus a manual invocation of its old functionality is pending.
     */
    private boolean onConnectInvoked = false;

    /**
     * Creates an instance of <tt>IrcStack</tt>.
     *
     * @param parentProvider the IRC protocol provider service
     * @param nickname our nickname
     * @param login our login
     * @param version the version
     * @param finger the finger
     */
    public IrcStack(    ProtocolProviderServiceIrcImpl parentProvider,
                        String nickname,
                        String login,
                        String version,
                        String finger)
    {
        this.parentProvider = parentProvider;
        this.ircMUCOpSet
            = (OperationSetMultiUserChatIrcImpl) parentProvider
                .getOperationSet(OperationSetMultiUserChat.class);
        this.setName(nickname);
        this.setLogin(login);
        this.setVersion(version);
        this.setFinger(finger);
    }

    /**
     * Connects to the server.
     * 
     * @param serverAddress the address of the server
     * @param serverPort the port to connect to
     * @param serverPassword the password to use for connect
     * @param autoNickChange indicates if the nick name should be changed in 
     * case there exist already a participant with the same nick name
     * 
     * @throws OperationFailedException
     */
    public void connect(String serverAddress,
                        int serverPort,
                        String serverPassword,
                        boolean autoNickChange)
        throws OperationFailedException
    {
        this.setVerbose(false);
        this.setAutoNickChange(autoNickChange);

        boolean onConnectInvoked;

        try
        {
            // avoids deadlock - issue#620. Call the event
            // in non synchronized code
            synchronized (this)
            {
                this.onConnectInvoked = false;

                if (serverPassword == null)
                    this.connect(serverAddress, serverPort);
                else
                    this.connect(serverAddress, serverPort, serverPassword);

                onConnectInvoked = this.onConnectInvoked;
            }
        }
        catch (IOException e)
        {
            throw new OperationFailedException(e.getMessage(),
                OperationFailedException.INTERNAL_SERVER_ERROR);
        }
        catch (NickAlreadyInUseException e)
        {
            throw new OperationFailedException(e.getMessage(),
                OperationFailedException.SUBSCRIPTION_ALREADY_EXISTS);
        }
        catch (IrcException e)
        {
            throw new OperationFailedException(e.getMessage(),
                OperationFailedException.GENERAL_ERROR);
        }

        // if onConnect method is called fire event from code that
        // is not synchronized - issue#620
        if (onConnectInvoked)
        {
            parentProvider
                .setCurrentRegistrationState(RegistrationState.REGISTERED);

            // It should be done when a getExistingChatRooms request is processed.
            // Obtain information for all channels on this server.
            // this.listChannels();
        }
    }

    /**
     * Called when we're connected to the IRC server.
     */
    protected synchronized void onConnect()
    {

        /*
         * Just mark that the onConnect method is invoked so that its old
         * functionality can be invoked manually elsewhere later on thus
         * avoiding a deadlock - issue #620.
         */
        onConnectInvoked = true;
    }

    /**
     * Called when we're disconnected from the IRC server.
     */
    protected void onDisconnect()
    {
        parentProvider
            .setCurrentRegistrationState(RegistrationState.UNREGISTERED);
    }

    /**
     * Indicates that a message has arrived from the IRC stack.
     * @param channel the channel where the message is received
     * @param sender the sender of the message
     * @param login the login
     * @param hostname the host name
     * @param messageContent the content of the message
     */
    protected void onMessage(   String channel,
                                String sender,
                                String login,
                                String hostname,
                                String messageContent)
    {
        if (logger.isTraceEnabled())
            logger.trace("MESSAGE received in chat room : " + channel
                        + ": from " + sender
                        + " " + login + "@" + hostname
                        + " the message: " + messageContent);

        MessageIrcImpl message
            = new MessageIrcImpl(   messageContent,
                                    MessageIrcImpl.DEFAULT_MIME_TYPE,
                                    MessageIrcImpl.DEFAULT_MIME_ENCODING,
                                    null);
 
        ChatRoomIrcImpl chatRoom = ircMUCOpSet.getChatRoom(channel);

        if (chatRoom == null)
            chatRoom = ircMUCOpSet.findSystemRoom();

        if(chatRoom == null || !chatRoom.isJoined())
            return;

        ChatRoomMember sourceMember = chatRoom.getChatRoomMember(sender);

        if (sourceMember == null)
            return;

        chatRoom.fireMessageReceivedEvent(
            message,
            sourceMember,
            System.currentTimeMillis(),
            ChatRoomMessageReceivedEvent.CONVERSATION_MESSAGE_RECEIVED);
    }

    /**
     * Indicates that a private message has been received.
     * Note that for now this method only logs the message.
     * @param sender the sender of the message
     * @param login the login
     * @param hostname the host name
     * @param messageContent the content of the message
     */
    protected void onPrivateMessage(String sender,
                                    String login,
                                    String hostname,
                                    String messageContent)
    {
        if (logger.isTraceEnabled())
            logger.trace("PRIVATE MESSAGE received from " + sender
                        + " " + login + "@" + hostname
                        + " the message: " + messageContent);

        MessageIrcImpl message
            = new MessageIrcImpl(   messageContent,
                                    MessageIrcImpl.DEFAULT_MIME_TYPE,
                                    MessageIrcImpl.DEFAULT_MIME_ENCODING,
                                    null);

        ChatRoomIrcImpl chatRoom
            = ircMUCOpSet.findPrivateChatRoom(sender);

        if(chatRoom == null || !chatRoom.isJoined())
            return;

        ChatRoomMember sourceMember = chatRoom.getChatRoomMember(sender);

        if (sourceMember == null)
        {
            sourceMember
                = new ChatRoomMemberIrcImpl(parentProvider,
                                            chatRoom,
                                            sender,
                                            ChatRoomMemberRole.GUEST);

            chatRoom.addChatRoomMember(sender, sourceMember);

            chatRoom.fireMemberPresenceEvent(
                sourceMember,
                null, // There's no other actors in this presence event.
                ChatRoomMemberPresenceChangeEvent.MEMBER_JOINED,
                "A message received from unknown member.");
        }

        chatRoom.fireMessageReceivedEvent(
            message,
            sourceMember,
            System.currentTimeMillis(),
            ChatRoomMessageReceivedEvent.CONVERSATION_MESSAGE_RECEIVED);
    }

    /**
     * This method is called whenever an ACTION is sent from a user.  E.g.
     * such events generated by typing "/me goes shopping" in most IRC clients.
     * 
     * @param sender The nick of the user that sent the action.
     * @param login The login of the user that sent the action.
     * @param hostname The host name of the user that sent the action.
     * @param target The target of the action, be it a channel or our nick.
     * @param action The action carried out by the user.
     */
    protected void onAction(String sender,
                            String login,
                            String hostname,
                            String target,
                            String action)
    {
        if (logger.isTraceEnabled())
            logger.trace("ACTION on " + target + " : Received from " + sender
                + " " + login + "@" + hostname + " the action: " + action);
        
        MessageIrcImpl actionMessage = new MessageIrcImpl(
                                            action,
                                            MessageIrcImpl.DEFAULT_MIME_TYPE,
                                            MessageIrcImpl.DEFAULT_MIME_ENCODING,
                                            null);

        // We presume that the target is a chat room, as we have not yet
        // implemented private messages.
        ChatRoomIrcImpl chatRoom = ircMUCOpSet.getChatRoom(target);

        if (chatRoom == null)
            chatRoom = ircMUCOpSet.findSystemRoom();

        if(chatRoom == null || !chatRoom.isJoined())
            return;

        ChatRoomMember sourceMember = chatRoom.getChatRoomMember(sender);

        if (sourceMember == null)
            return;

        chatRoom.fireMessageReceivedEvent(
            actionMessage,
            sourceMember,
            System.currentTimeMillis(),
            ChatRoomMessageReceivedEvent.ACTION_MESSAGE_RECEIVED);
    }

    /**
     * After calling the listChannels() method in PircBot, the server
     * will start to send us information about each channel on the
     * server.
     * 
     * @param channel The name of the channel.
     * @param userCount The number of users visible in this channel.
     * @param topic The topic for this channel.
     */
    protected void onChannelInfo(String channel, int userCount, String topic)
    {
        this.addServerChatRoom(channel);
    }

    /**
     * Called when a user (possibly us) gets operator status taken away.
     *  <p>
     * This is a type of mode change and is also passed to the onMode
     * method in the PircBot class.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     * @param recipient The nick of the user that got 'de-opp-ed'.
     */
    protected void onDeop(String channel,
                          String sourceNick,
                          String sourceLogin,
                          String sourceHostname,
                          String recipient)
    {
        if (logger.isTraceEnabled())
            logger.trace("DEOP on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname + "on " + recipient);

        ChatRoomIrcImpl chatRoom = ircMUCOpSet.getChatRoom(channel);

        if (chatRoom == null || !chatRoom.isJoined())
            return;

        ChatRoomMember sourceMember = chatRoom.getChatRoomMember(sourceNick);

        if (sourceMember == null)
            return;

        chatRoom.fireMemberRoleEvent(sourceMember, ChatRoomMemberRole.GUEST);
    }

    /**
     * Called when a user (possibly us) gets voice status removed.
     *  <p>
     * This is a type of mode change and is also passed to the onMode
     * method in the PircBot class.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     * @param recipient The nick of the user that got 'de-voiced'.
     */
    protected void onDeVoice(String channel, String sourceNick,
        String sourceLogin, String sourceHostname, String recipient)
    {
        if (logger.isDebugEnabled())
            logger.debug("DEVOICE on " + channel + ": Received from "
                + sourceNick + " " + sourceLogin + "@" + sourceHostname + "on "
                + recipient);

        ChatRoomIrcImpl chatRoom = ircMUCOpSet.getChatRoom(channel);

        if (chatRoom == null || !chatRoom.isJoined())
            return;

        ChatRoomMember sourceMember = chatRoom.getChatRoomMember(sourceNick);

        if (sourceMember == null)
            return;

        chatRoom.fireMemberRoleEvent(   sourceMember,
                                        ChatRoomMemberRole.SILENT_MEMBER);
    }

    /**
     * Called when we are invited to a channel by a user.
     * 
     * @param targetNick The nick of the user being invited - should be us!
     * @param sourceNick The nick of the user that sent the invitation.
     * @param sourceLogin The login of the user that sent the invitation.
     * @param sourceHostname The host name of the user that sent the invitation.
     * @param channel The channel that we're being invited to.
     */
    protected void onInvite(String targetNick,
                            String sourceNick,
                            String sourceLogin,
                            String sourceHostname,
                            String channel)
    {
        if (logger.isTraceEnabled())
            logger.trace("INVITE on " + channel + ": Received from "
                + sourceNick + " " + sourceLogin + "@" + sourceHostname);

        ChatRoom targetChatRoom = ircMUCOpSet.findRoom(channel);

        ircMUCOpSet.fireInvitationEvent(targetChatRoom, 
                                        sourceNick,
                                        "",
                                        null);
    }

    /**
     * This method is called whenever someone (possibly us) joins a channel
     * which we are on.
     * 
     * @param channel The channel which somebody joined.
     * @param sender The nick of the user who joined the channel.
     * @param login The login of the user who joined the channel.
     * @param hostname The host name of the user who joined the channel.
     */
    protected void onJoin(  String channel,
                            String sender,
                            String login,
                            String hostname)
    {
        if (logger.isTraceEnabled())
            logger.trace("JOIN on " + channel + ": Received from " + sender
                + " " + login + "@" + hostname);

        ChatRoomIrcImpl chatRoom
            = (ChatRoomIrcImpl) ircMUCOpSet.findRoom(channel);

        if(joinTimeoutTimers.containsKey(chatRoom))
        {
            Timer timer = joinTimeoutTimers.get(chatRoom);

            timer.cancel();
            joinTimeoutTimers.remove(chatRoom);
        }

        if(chatRoom.getUserNickname().equals(sender))
        {
            ircMUCOpSet.fireLocalUserPresenceEvent(
                chatRoom,
                LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_JOINED,
                "");
        }
        else
        {
            ChatRoomMemberIrcImpl member = new ChatRoomMemberIrcImpl(
                    parentProvider,
                    chatRoom,
                    sender,
                    ChatRoomMemberRole.GUEST);

            chatRoom.addChatRoomMember(sender, member);

            //we don't specify a reason
            chatRoom.fireMemberPresenceEvent(
                member,
                null,
                ChatRoomMemberPresenceChangeEvent.MEMBER_JOINED,
                null);
        }
    }

    /**
     * This method is called whenever someone (possibly us) is kicked from
     * any of the channels that we are in.
     * 
     * @param channel The channel from which the recipient was kicked.
     * @param kickerNick The nick of the user who performed the kick.
     * @param kickerLogin The login of the user who performed the kick.
     * @param kickerHostname The host name of the user who performed the kick.
     * @param recipientNick The unfortunate recipient of the kick.
     * @param reason The reason given by the user who performed the kick.
     */
    protected void onKick(  String channel,
                            String kickerNick,
                            String kickerLogin,
                            String kickerHostname,
                            String recipientNick,
                            String reason)
    {
        if (logger.isTraceEnabled())
            logger.trace("KICK on " + channel
                    + ": Received from " + kickerNick
                    + " " + kickerLogin + "@" + kickerHostname);

        ChatRoomIrcImpl chatRoom = ircMUCOpSet.getChatRoom(channel);

        if (chatRoom == null || !chatRoom.isJoined())
            return;

        if(chatRoom.getUserNickname().equals(kickerNick))
        {
            notifyChatRoomOperation(0);
        }

        if(chatRoom.getUserNickname().equals(recipientNick))
            ircMUCOpSet.fireLocalUserPresenceEvent(
                chatRoom,
                LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_KICKED,
                reason);
        else
        {
            ChatRoomMember member
                = chatRoom.getChatRoomMember(recipientNick);

            ChatRoomMember actorMember
                = chatRoom.getChatRoomMember(kickerNick);

            chatRoom.removeChatRoomMember(recipientNick);

            chatRoom.fireMemberPresenceEvent(
                member,
                actorMember,
                ChatRoomMemberPresenceChangeEvent.MEMBER_KICKED,
                reason);
        }
    }

    /**
     * This method is called whenever someone (possibly us) changes nick on any
     * of the channels that we are on.
     * 
     * @param oldNick The old nick.
     * @param login The login of the user.
     * @param hostname The host name of the user.
     * @param newNick The new nick.
     */
    protected void onNickChange(String oldNick,
                                String login,
                                String hostname,
                                String newNick)
    {
        if (logger.isTraceEnabled())
            logger.trace("NICK changed: from " + oldNick + " changed to "
                + newNick);

        this.notifyChatRoomOperation(0);

        for (ChatRoom chatRoom : ircMUCOpSet.getCurrentlyJoinedChatRooms())
        {
            ChatRoomIrcImpl chatRoomIrcImpl = (ChatRoomIrcImpl) chatRoom;

            if (chatRoom.getUserNickname().equals(oldNick))
            {
                chatRoomIrcImpl.setNickName(newNick);
                return;
            }

            ChatRoomMember member = chatRoomIrcImpl.getChatRoomMember(oldNick);

            if (member == null)
                continue;

            ChatRoomMemberPropertyChangeEvent evt
                = new ChatRoomMemberPropertyChangeEvent(
                        member,
                        chatRoom,
                        ChatRoomMemberPropertyChangeEvent.MEMBER_NICKNAME,
                        oldNick,
                        newNick);

            chatRoomIrcImpl.fireMemberPropertyChangeEvent(evt);
        }
    }

    /**
     * This method is called whenever we receive a notice.
     * 
     * @param sourceNick The nick of the user that sent the notice.
     * @param sourceLogin The login of the user that sent the notice.
     * @param sourceHostname The host name of the user that sent the notice.
     * @param target The target of the notice, be it our nick or a channel name.
     * @param notice The notice message.
     */
    protected void onNotice(String sourceNick,
                            String sourceLogin,
                            String sourceHostname,
                            String target,
                            String notice)
    {
        if (logger.isTraceEnabled())
            logger.trace("NOTICE on " + target + ": Received from "
                + sourceNick + " " + sourceLogin + "@" + sourceHostname
                + " the message: " + notice);

        MessageIrcImpl message
            = new MessageIrcImpl(   notice,
                                    MessageIrcImpl.DEFAULT_MIME_TYPE,
                                    MessageIrcImpl.DEFAULT_MIME_ENCODING,
                                    null);

        ChatRoomIrcImpl chatRoom = ircMUCOpSet.getChatRoom(target);

        ChatRoomMember sourceMember = null;

        if(chatRoom == null || !chatRoom.isJoined())
        {
            chatRoom = ircMUCOpSet.findSystemRoom();

            sourceMember = ircMUCOpSet.findSystemMember();
        }
        else
        {
            sourceMember = chatRoom.getChatRoomMember(sourceNick);
        }

        if (sourceMember == null)
            return;

        chatRoom.fireMessageReceivedEvent(
                message,
                sourceMember,
                System.currentTimeMillis(),
                ChatRoomMessageReceivedEvent.ACTION_MESSAGE_RECEIVED);
    }

    /**
     * Called when a user (possibly us) gets granted operator status for a
     * channel.
     *  <p>
     * This is a type of mode change and is also passed to the onMode
     * method in the PircBot class.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode 
     * change.
     * @param recipient The nick of the user that got 'opp-ed'.
     */
    protected void onOp(String channel,
                        String sourceNick,
                        String sourceLogin,
                        String sourceHostname,
                        String recipient)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE OP on " + channel + ": from " + sourceNick + " "
                + sourceLogin + "@" + sourceHostname + " on " + recipient);

        ChatRoomIrcImpl chatRoom = ircMUCOpSet.getChatRoom(channel);

        if (chatRoom == null || !chatRoom.isJoined())
            return;

        ChatRoomMember sourceMember = chatRoom.getChatRoomMember(sourceNick);

        if (sourceMember == null)
            return;

        chatRoom.fireMemberRoleEvent(   sourceMember,
                                        ChatRoomMemberRole.ADMINISTRATOR);
    }

    /**
     * This method is called whenever someone (possibly us) leaves a channel
     * which we are on.
     * 
     * @param channel The channel which somebody parted from.
     * @param sender The nick of the user who parted from the channel.
     * @param login The login of the user who parted from the channel.
     * @param hostname The host name of the user who parted from the channel.
     */
    protected void onPart(  String channel,
                            String sender,
                            String login,
                            String hostname)
    {
        if (logger.isDebugEnabled())
            logger.debug("LEAVE on " + channel + ": Received from " + sender
                + " " + login + "@" + hostname);

        ChatRoomIrcImpl chatRoom
            = (ChatRoomIrcImpl) ircMUCOpSet.findRoom(channel);

        if(chatRoom.getUserNickname().equals(sender))
        {
            ircMUCOpSet.fireLocalUserPresenceEvent(
                chatRoom,
                LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_LEFT,
                "");

            for (ChatRoomMember member : chatRoom.getMembers())
                chatRoom.fireMemberPresenceEvent(
                    member,
                    null,
                    ChatRoomMemberPresenceChangeEvent.MEMBER_LEFT,
                    "Local user has left the chat room.");

            // Delete the list of members
            chatRoom.clearChatRoomMemberList();
        }
        else
        {
            ChatRoomMember member = chatRoom.getChatRoomMember(sender);

            if (member == null)
                return;

            chatRoom.removeChatRoomMember(sender);

            //we don't specify a reason
            chatRoom.fireMemberPresenceEvent(
                member,
                null,
                ChatRoomMemberPresenceChangeEvent.MEMBER_LEFT,
                null);
        }
    }

    /**
     * This method is called whenever someone (possibly us) quits from the
     * server. We will only observe this if the user was in one of the
     * channels to which we are connected.
     * 
     * @param sourceNick The nick of the user that quit from the server.
     * @param sourceLogin The login of the user that quit from the server.
     * @param sourceHostname The host name of the user that quit from the server.
     * @param reason The reason given for quitting the server.
     */
    protected void onQuit(  String sourceNick,
                            String sourceLogin,
                            String sourceHostname,
                            String reason)
    {
        if (logger.isDebugEnabled())
            logger.debug("QUIT : Received from " + sourceNick + " "
                + sourceLogin + "@" + sourceHostname);

        for (ChatRoom chatRoom : ircMUCOpSet.getCurrentlyJoinedChatRooms())
        {
            ChatRoomIrcImpl chatRoomIrcImpl = (ChatRoomIrcImpl) chatRoom;

            if(chatRoom.getUserNickname().equals(sourceNick))
                ircMUCOpSet.fireLocalUserPresenceEvent(
                    chatRoom,
                    LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_DROPPED,
                    reason);
            else
            {
                ChatRoomMember member
                    = chatRoomIrcImpl.getChatRoomMember(sourceNick);

                if (member == null)
                    return;

                chatRoomIrcImpl.removeChatRoomMember(sourceNick);

                chatRoomIrcImpl.fireMemberPresenceEvent(
                    member,
                    null,
                    ChatRoomMemberPresenceChangeEvent.MEMBER_QUIT,
                    reason);
            }
        }
    }

    /**
     * Called when a host mask ban is removed from a channel.
     *  <p>
     * This is a type of mode change and is also passed to the onMode
     * method in the PircBot class.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     * @param hostmask
     */
    protected void onRemoveChannelBan(  String channel,
                                        String sourceNick,
                                        String sourceLogin,
                                        String sourceHostname,
                                        String hostmask)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        ChatRoom chatRoom = ircMUCOpSet.getChatRoom(channel);

        if (chatRoom == null)
            return;

        //TODO: Implement IrcStack.onRemoveChannelBan.
    }

    /**
     * Called when a channel key is removed.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     * @param key The key that was in use before the channel key was removed.
     */
    protected void onRemoveChannelKey(  String channel,
                                        String sourceNick,
                                        String sourceLogin,
                                        String sourceHostname,
                                        String key)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);
        
        //TODO: Implement IrcStack.onRemoveChannelKey().
    }

    /**
     * Called when the user limit is removed for a channel.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     */
    protected void onRemoveChannelLimit(String channel, String sourceNick,
        String sourceLogin, String sourceHostname)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        ChatRoom chatRoom = ircMUCOpSet.getChatRoom(channel);

        if (chatRoom == null)
            return;

      //TODO: Implement IrcStack.onRemoveChannelLimit().
    }

    /**
     * Called when a channel has 'invite only' removed.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     */
    protected void onRemoveInviteOnly(  String channel,
                                        String sourceNick,
                                        String sourceLogin,
                                        String sourceHostname)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        // TODO: Implement IrcStack.onRemoveInviteOnly().
    }

    /**
     * Called when a channel has moderated mode removed.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     */
    protected void onRemoveModerated(   String channel,
                                        String sourceNick,
                                        String sourceLogin,
                                        String sourceHostname)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        // TODO: Implement IrcStack.onRemoveModerated().
    }

    /**
     * Called when a channel is set to allow messages from any user, even
     * if they are not actually in the channel.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     */
    protected void onRemoveNoExternalMessages(  String channel,
                                                String sourceNick,
                                                String sourceLogin,
                                                String sourceHostname)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        // TODO: Implement IrcStack.onRemoveNoExternalMessages().
    }

    /**
     * Called when a channel is marked as not being in private mode.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     */
    protected void onRemovePrivate( String channel,
                                    String sourceNick,
                                    String sourceLogin,
                                    String sourceHostname)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        // TODO: Implement IrcStack.onRemovePrivate().
    }

    /**
     * Called when a channel has 'secret' mode removed.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     */
    protected void onRemoveSecret(  String channel,
                                    String sourceNick,
                                    String sourceLogin,
                                    String sourceHostname)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        // TODO: Implement IrcStack.onRemoveSecret().
    }

    /**
     * Called when topic protection is removed for a channel.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     */
    protected void onRemoveTopicProtection(String channel, String sourceNick,
        String sourceLogin, String sourceHostname)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        // TODO: Implement IrcStack.onRemoveSecret().
    }

    /**
     * 
     * @param code The three-digit numerical code for the response.
     * @param response The full response from the IRC server.
     * 
     * @see ReplyConstants
     */
    protected void onServerResponse (int code, String response)
    {
        if (code == ERR_NOSUCHCHANNEL)
        {
            logger.error("No such channel:" + code
                + ": Response :" + response);

            this.notifyChatRoomOperation(ERR_NOSUCHCHANNEL);
        }
        else if (code == ERR_BADCHANMASK)
        {
            logger.error("Bad channel mask :" + code
                + ": Response :" + response);

            this.notifyChatRoomOperation(ERR_BADCHANMASK);
        }
        else if (code == ERR_BADCHANNELKEY)
        {
            logger.error("Bad channel key :" + code
                + ": Response :" + response);

            this.notifyChatRoomOperation(ERR_BADCHANNELKEY);
        }
        else if (code == ERR_BANNEDFROMCHAN)
        {
            logger.error("Banned from channel :" + code
                + ": Response :" + response);

            this.notifyChatRoomOperation(ERR_BANNEDFROMCHAN);
        }
        else if (code == ERR_CHANNELISFULL)
        {
            logger.error("Channel is full :" + code
                + ": Response :" + response);

            this.notifyChatRoomOperation(ERR_CHANNELISFULL);
        }
        else if (code == ERR_CHANOPRIVSNEEDED)
        {
            logger.error("Channel operator privilages needed :" + code
                + ": Response :" + response);

            this.notifyChatRoomOperation(ERR_CHANOPRIVSNEEDED);
        }
        else if (code == ERR_ERRONEUSNICKNAME)
        {
            logger.error("ERR_ERRONEUSNICKNAME :" + code
                + ": Response :" + response);

            this.notifyChatRoomOperation(ERR_ERRONEUSNICKNAME);
        }
        else if (code == ERR_INVITEONLYCHAN)
        {
            logger.error("Invite only channel :" + code
                + ": Response :" + response);

            this.notifyChatRoomOperation(ERR_INVITEONLYCHAN);
        }
        else if (code == ERR_NEEDMOREPARAMS)
        {
            logger.error("Need more params :" + code
                + ": Response :" + response);

            this.notifyChatRoomOperation(ERR_NEEDMOREPARAMS);
        }
        else if (code == ERR_NICKCOLLISION)
        {
            logger.error("Nick collision :" + code
                + ": Response :" + response);

            this.notifyChatRoomOperation(ERR_NICKCOLLISION);
        }
        else if (code == ERR_NICKNAMEINUSE)
        {
            logger.error("Nickname in use :" + code
                + ": Response :" + response);

            this.notifyChatRoomOperation(ERR_NICKNAMEINUSE);
        }
        else if (code == ERR_NONICKNAMEGIVEN)
        {
            logger.error("No nickname given :" + code
                + ": Response :" + response);

            this.notifyChatRoomOperation(ERR_NONICKNAMEGIVEN);
        }
        else if (code == ERR_NOTONCHANNEL)
        {
            logger.error("Not on channel :" + code
                + ": Response :" + response);

            this.notifyChatRoomOperation(ERR_NOTONCHANNEL);
        }
        else if (code == ERR_TOOMANYCHANNELS)
        {
            logger.error("Too many channels :" + code
                + ": Response :" + response);

            this.notifyChatRoomOperation(ERR_TOOMANYCHANNELS);
        }
        // reply responses
        else if (code == RPL_WHOISUSER)
        {
            StringTokenizer tokenizer = new StringTokenizer(response);
            tokenizer.nextToken();

            String nickname = tokenizer.nextToken();
            String login = tokenizer.nextToken();
            String hostname = tokenizer.nextToken();

            UserInfo userInfo = new UserInfo(nickname, login, hostname);

            this.userInfoTable.put(nickname, userInfo);
        }
        else if (code == RPL_WHOISSERVER)
        {
            StringTokenizer tokenizer = new StringTokenizer(response);
            tokenizer.nextToken();
            String userNickName = tokenizer.nextToken();

            int end = response.indexOf(':');
            String serverInfo = response.substring(end + 1);

            if (userInfoTable.containsKey(userNickName))
            {
                userInfoTable.get(userNickName).setServerInfo(serverInfo);
            }
        }
        else if (code == RPL_WHOISOPERATOR)
        {
            StringTokenizer tokenizer = new StringTokenizer(response);
            tokenizer.nextToken();
            String userNickName = tokenizer.nextToken();

            if (userInfoTable.containsKey(userNickName))
            {
                userInfoTable.get(userNickName).setIrcOp(true);
            }
        }
        else if (code == RPL_WHOISIDLE)
        {
            StringTokenizer tokenizer = new StringTokenizer(response);
            tokenizer.nextToken();
            String userNickName = tokenizer.nextToken();
            long idle = Long.parseLong(tokenizer.nextToken());

            if (userInfoTable.containsKey(userNickName))
            {
                userInfoTable.get(userNickName).setIdle(idle);
            }
        }
        else if (code == RPL_WHOISCHANNELS)
        {
            StringTokenizer tokenizer = new StringTokenizer(response);
            tokenizer.nextToken();
            String userNickName = tokenizer.nextToken();

            if (userInfoTable.containsKey(userNickName))
            {
                userInfoTable.get(userNickName).clearJoinedChatRoom();

                while(tokenizer.hasMoreTokens())
                {
                    String channel = tokenizer.nextToken();

                    if(channel.startsWith(":"))
                        channel = channel.substring(1);

                    userInfoTable.get(userNickName).addJoinedChatRoom(channel);
                }
            }
        }
        else if (code == RPL_ENDOFWHOIS)
        {
            StringTokenizer tokenizer = new StringTokenizer(response);
            tokenizer.nextToken();
            String userNickName = tokenizer.nextToken();

            if (userInfoTable.containsKey(userNickName))
            {
                UserInfo userInfo = userInfoTable.get(userNickName);
                
                this.onWhoIs(userInfo);
            }
        }
        else if (code == RPL_ENDOFMOTD)
        {
            this.isInitialized = true;

            ChatRoom[] joinCacheCopy
                = joinCache.toArray(new ChatRoom[joinCache.size()]);

            joinCache.clear();

            for (ChatRoom joinCacheElement : joinCacheCopy)
            {
                this.join(joinCacheElement);
            }
        }
        else if (code != RPL_LISTSTART
                    && code != RPL_LIST
                    && code != RPL_LISTEND
                    && code != RPL_ENDOFNAMES)
        {
            if (logger.isTraceEnabled())
                logger.trace(
                "Server response: Code : "
                + code
                + " Response : "
                + response);

            int delimiterIndex = response.indexOf(':');

            if(delimiterIndex != -1 && delimiterIndex < response.length() - 1)
                response = response.substring(delimiterIndex + 1);

            MessageIrcImpl message
                = new MessageIrcImpl(
                    response,
                    MessageIrcImpl.DEFAULT_MIME_TYPE,
                    MessageIrcImpl.DEFAULT_MIME_ENCODING,
                    null);

            ChatRoomIrcImpl serverRoom = ircMUCOpSet.findSystemRoom();

            ChatRoomMember serverMember = ircMUCOpSet.findSystemMember();

            serverRoom.fireMessageReceivedEvent(
                    message,
                    serverMember,
                    System.currentTimeMillis(),
                    ChatRoomMessageReceivedEvent.SYSTEM_MESSAGE_RECEIVED);
        }
    }

    /**
     * Called when a user (possibly us) gets banned from a channel. Being
     * banned from a channel prevents any user with a matching host mask from
     * joining the channel.  For this reason, most bans are usually directly
     * followed by the user being kicked .
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     * @param hostmask The host mask of the user that has been banned.
     */
    protected void onSetChannelBan( String channel,
                                    String sourceNick,
                                    String sourceLogin,
                                    String sourceHostname,
                                    String hostmask)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        // TODO: Implement IrcStack.onSetChannelBan().
    }

    /**
     * Called when a channel key is set.  When the channel key has been set,
     * other users may only join that channel if they know the key.  Channel
     * keys are sometimes referred to as passwords.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     * @param key The new key for the channel.
     */
    protected void onSetChannelKey(String channel, String sourceNick,
        String sourceLogin, String sourceHostname, String key)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

     // TODO: Implement IrcStack.onSetChannelKey().
    }

    /**
     * Called when a user limit is set for a channel.  The number of users in
     * the channel cannot exceed this limit.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     * @param limit The maximum number of users that may be in this channel at
     * the same time.
     */
    protected void onSetChannelLimit(   String channel,
                                        String sourceNick,
                                        String sourceLogin,
                                        String sourceHostname,
                                        int limit)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        // TODO: Implement IrcStack.onSetChannelLimit().
    }

    /**
     * Called when a channel is set to 'invite only' mode.  A user may only
     * join the channel if they are invited by someone who is already in the
     * channel.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     */
    protected void onSetInviteOnly( String channel,
                                    String sourceNick,
                                    String sourceLogin,
                                    String sourceHostname)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        // TODO: Implement IrcStack.onSetChannelLimit().
    }

    /**
     * Called when a channel is set to 'moderated' mode. If a channel is
     * moderated, then only users who have been 'voiced' or 'opp-ed' may speak
     * or change their nicks.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     */
    protected void onSetModerated(  String channel,
                                    String sourceNick,
                                    String sourceLogin,
                                    String sourceHostname)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        // TODO: Implement IrcStack.onSetModerated().
    }

    /**
     * Called when a channel is set to only allow messages from users that
     * are in the channel.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The hostname of the user that performed the mode
     * change.
     */
    protected void onSetNoExternalMessages( String channel,
                                            String sourceNick,
                                            String sourceLogin,
                                            String sourceHostname)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        // TODO: Implement IrcStack.onSetNoExternalMessages().
    }

    /**
     * Called when a channel is marked as being in private mode.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     */
    protected void onSetPrivate(String channel, String sourceNick,
        String sourceLogin, String sourceHostname)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        // TODO: Implement IrcStack.onSetPrivate().
    }

    /**
     * Called when a channel is set to be in 'secret' mode.  Such channels
     * typically do not appear on a server's channel listing.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     */
    protected void onSetSecret(String channel, String sourceNick,
        String sourceLogin, String sourceHostname)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        //TODO: Implement IrcStack.onSetPrivate().
    }

    /**
     * Called when topic protection is enabled for a channel.  Topic protection
     * means that only operators in a channel may change the topic.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     */
    protected void onSetTopicProtection(String channel, String sourceNick,
        String sourceLogin, String sourceHostname)
    {
        if (logger.isDebugEnabled())
            logger.debug("MODE on " + channel + ": Received from " + sourceNick
                + " " + sourceLogin + "@" + sourceHostname);

        //TODO: Implement IrcStack.onSetPrivate().
    }

    /**
     * This method is called whenever a user sets the topic, or when
     * PircBot joins a new channel and discovers its topic.
     * 
     * @param channel The channel that the topic belongs to.
     * @param topic The topic for the channel.
     * @param setBy The nick of the user that set the topic.
     * @param date When the topic was set (milliseconds since the epoch).
     * @param changed True if the topic has just been changed, false if
     *                the topic was already there.
     * 
     */
    protected void onTopic( String channel,
                            String topic,
                            String setBy,
                            long date,
                            boolean changed)
    {
        if (logger.isTraceEnabled())
            logger.trace("TOPIC on " + channel + ": " + topic + " setBy: "
                + setBy + " on: " + date);

        this.notifyChatRoomOperation(0);

        ChatRoomIrcImpl chatRoom = ircMUCOpSet.getChatRoom(channel);

        ChatRoomPropertyChangeEvent evt
            = new ChatRoomPropertyChangeEvent(
                chatRoom,
                ChatRoomPropertyChangeEvent.CHAT_ROOM_SUBJECT,
                chatRoom.getSubject(),
                topic);

        // After creating the event with the old and new value of the subject
        // we could change the subject property of the chat room.
        chatRoom.setSubjectFromServer(topic);

        chatRoom.firePropertyChangeEvent(evt);
    }

    /**
     * This method is called whenever we receive a line from the server that
     * the PircBot has not been programmed to recognize.
     * 
     * @param line The raw line that was received from the server.
     */
    protected void onUnknown(String line)
    {
        if (logger.isTraceEnabled())
            logger.trace("Unknown message received from the server : " + line);
    }

    /**
     * This method is called when we receive a user list from the server
     * after joining a channel.
     * 
     * @param channel The name of the channel.
     * @param users An array of User objects belonging to this channel.
     * 
     * @see User
     */
    protected void onUserList(String channel, User[] users)
    {
        if (logger.isDebugEnabled())
            logger.debug("NAMES on " + channel);

        ChatRoomIrcImpl chatRoom = ircMUCOpSet.getChatRoom(channel);

        chatRoom.clearChatRoomMemberList();

        for (User user : users)
        {
            String userPrefix = user.getPrefix();
            ChatRoomMemberRole newMemberRole;

            if (userPrefix.contains("@"))
                newMemberRole = ChatRoomMemberRole.ADMINISTRATOR;
            else if (userPrefix.contains("%"))
                newMemberRole = ChatRoomMemberRole.MODERATOR;
            else if (userPrefix.contains("+"))
                newMemberRole = ChatRoomMemberRole.MEMBER;
            else
                newMemberRole = ChatRoomMemberRole.GUEST;

            ChatRoomMemberIrcImpl newMember
                = new ChatRoomMemberIrcImpl(parentProvider,
                                            chatRoom,
                                            user.getNick(),
                                            newMemberRole);

            chatRoom.addChatRoomMember(user.getNick(), newMember);

            chatRoom.fireMemberPresenceEvent(
                newMember,
                null,
                ChatRoomMemberPresenceChangeEvent.MEMBER_JOINED,
                ChatRoomMemberPresenceChangeEvent.REASON_USER_LIST);
        }
    }

    /**
     * Called when a user (possibly us) gets voice status granted in a channel.
     * 
     * @param channel The channel in which the mode change took place.
     * @param sourceNick The nick of the user that performed the mode change.
     * @param sourceLogin The login of the user that performed the mode change.
     * @param sourceHostname The host name of the user that performed the mode
     * change.
     * @param recipient The nick of the user that got 'voiced'.
     */
    protected void onVoice(String channel, String sourceNick,
        String sourceLogin, String sourceHostname, String recipient)
    {
        if (logger.isDebugEnabled())
            logger.debug("VOICE on " + channel + ": Received from "
                + sourceNick + " " + sourceLogin + "@" + sourceHostname + "on "
                + recipient);

        ChatRoomIrcImpl chatRoom = ircMUCOpSet.getChatRoom(channel);

        if (chatRoom == null || !chatRoom.isJoined())
            return;

        ChatRoomMember sourceMember = chatRoom.getChatRoomMember(sourceNick);

        if (sourceMember == null)
            return;

        chatRoom.fireMemberRoleEvent(   sourceMember,
                                        ChatRoomMemberRole.GUEST);
    }

    /**
     * Returns the list of chat rooms on this server.
     * 
     * @return the list of chat rooms on this server
     */
    public List<String> getServerChatRoomList()
    {
        return serverChatRoomList;
    }

    /**
     * Tests if this chat room is joined
     * 
     * @param chatRoom the chat room we want to test
     * @return true if the chat room is joined, false otherwise
     */
    protected boolean isJoined(ChatRoomIrcImpl chatRoom)
    {
        // If we are asked for the status of the server channel, we return true
        // if the server is connected and false otherwise.
        if(ircMUCOpSet.findSystemRoom().equals(chatRoom))
            return isConnected();

        // Private rooms are joined if they exist.
        if(chatRoom.isPrivate())
            return true;

        // For all other channels on the server.
        if (this.isConnected())
        {
            String[] channels = this.getChannels();

            for (String channel : channels)
            {
                if (chatRoom.getName().equals(channel))
                    return true;
            }
            return false;
        }
        else
        {
            return false;
        }
    }

    /**
     * Join a chat room on this server.
     * 
     * @param chatRoom the chat room to join
     */
    public void join(ChatRoom chatRoom)
    {
        if (!isInitialized)
        {
            joinCache.add(chatRoom);

            return;
        }

        this.joinChannel(chatRoom.getName());

        Timer joinTimeoutTimer = new Timer();

        joinTimeoutTimers.put(chatRoom, joinTimeoutTimer);

        joinTimeoutTimer.schedule(new JoinTimeoutTask(chatRoom), TIMEOUT);
    }

    /**
     * Join a chat room on this server.
     * 
     * @param chatRoom the chat room to join
     * @param password the password of the chat room
     */
    public void join(ChatRoom chatRoom, byte[] password)
    {
        this.joinChannel(chatRoom.getName(), new String(password));

        Timer joinTimeoutTimer = new Timer();

        joinTimeoutTimers.put(chatRoom, joinTimeoutTimer);
        
        joinTimeoutTimer.schedule(new JoinTimeoutTask(chatRoom), TIMEOUT);
    }

    /**
     * Leaves the given chat room.
     * 
     * @param chatRoom the chat room we want to leave
     */
    protected void leave(ChatRoom chatRoom)
    {
        this.partChannel(chatRoom.getName());
    }

    /**
     * This method sends a command to the server which can also be an action or
     * a notice.
     * 
     * @param chatRoom the chat room corresponding to the command
     * @param command the command we want to send
     */
    protected void sendCommand(ChatRoomIrcImpl chatRoom, String command)
    {
        if (command.startsWith("/me"))
        {
            this.sendAction(chatRoom.getName(), command.substring(3));
        }
        else if (command.startsWith("/notice"))
        {
            this.sendNotice(chatRoom.getName(), command.substring(7));
        }
        else if (command.startsWith("/msg"))
        {
            StringTokenizer tokenizer = new StringTokenizer(command);

            String target = "";
            String messageContent = "";

            // We don't need the /msg command text.
            tokenizer.nextToken();

            if(tokenizer.hasMoreTokens())
                target = tokenizer.nextToken();

            while(tokenizer.hasMoreTokens())
            {
                messageContent += tokenizer.nextToken() + " ";
            }

            this.sendMessage(target, messageContent);
        }
        else if (command.startsWith("/query"))
        {
            StringTokenizer tokenizer = new StringTokenizer(command);

            String target = null;

            tokenizer.nextToken();

            if(tokenizer.hasMoreTokens())
                target = tokenizer.nextToken();

            this.createPrivateChatRoom(target);
        }
        else
        {
            this.sendRawLine(command.substring(1));
        }
    }

    /**
     * Called to ban the given user from the given channel. The reason is not
     * passed to the server, as it doesn't support this parameter.
     * 
     * @param chatRoom the chat room for which the user should be banned
     * @param hostmask the host mask of the user to ban
     * @param reason the reason of the ban
     * @throws OperationFailedException if something goes wrong
     */
    protected void banParticipant( String chatRoom,
                        String hostmask,
                        String reason)
        throws OperationFailedException
    {
        this.ban(chatRoom, hostmask);

        this.lockChatRoomOperation();

        if (operationResponseCode == ERR_NEEDMOREPARAMS)
            throw new OperationFailedException(
                "Need more parameters.",
                OperationFailedException.GENERAL_ERROR);
        else if (operationResponseCode == ERR_CHANOPRIVSNEEDED)
            throw new OperationFailedException(
                "Need more parameters.",
                OperationFailedException.NOT_ENOUGH_PRIVILEGES);
        else if (operationResponseCode == ERR_NOTONCHANNEL)
            throw new OperationFailedException(
                "Need more parameters.",
                OperationFailedException.GENERAL_ERROR);
        else if (operationResponseCode == ERR_USERSDONTMATCH)
            throw new OperationFailedException(
                "Need more parameters.",
                OperationFailedException.GENERAL_ERROR);
        else if (operationResponseCode == ERR_NOSUCHCHANNEL)
            throw new OperationFailedException(
                "Need more parameters.",
                OperationFailedException.GENERAL_ERROR);
        else if (operationResponseCode == ERR_NOSUCHNICK)
            throw new OperationFailedException(
                "Need more parameters.",
                OperationFailedException.GENERAL_ERROR);
        else if (operationResponseCode == ERR_KEYSET)
            throw new OperationFailedException(
                "Need more parameters.",
                OperationFailedException.GENERAL_ERROR);
        else if (operationResponseCode == ERR_UMODEUNKNOWNFLAG)
            throw new OperationFailedException(
                "Need more parameters.",
                OperationFailedException.GENERAL_ERROR);
        else if (operationResponseCode == ERR_UNKNOWNMODE)
            throw new OperationFailedException(
                "Need more parameters.",
                OperationFailedException.GENERAL_ERROR);
    }

    /**
     * Who is.
     * 
     * @param userInfo
     */
    private void onWhoIs(UserInfo userInfo)
    {
        ChatRoomIrcImpl chatRoom = ircMUCOpSet.findSystemRoom();

        if((chatRoom == null) || !chatRoom.isJoined())
            return;

        if (logger.isTraceEnabled())
            logger.trace("WHOIS on: " + userInfo.getNickName() + "!"
                + userInfo.getLogin() + "@" + userInfo.getHostname());

        String whoisMessage
            = "Nickname: " + userInfo.getNickName() + "\n"
                + "Host name: " + userInfo.getHostname() + "\n"
                + "Login: " + userInfo.getLogin() + "\n"
                + "Server info: " + userInfo.getServerInfo() + "\n"
                + "Joined chat rooms:";

        for (String joinedChatRoom : userInfo.getJoinedChatRooms())
            whoisMessage += " " + joinedChatRoom;

        MessageIrcImpl message
            = new MessageIrcImpl(   whoisMessage,
                                    MessageIrcImpl.DEFAULT_MIME_TYPE,
                                    MessageIrcImpl.DEFAULT_MIME_ENCODING,
                                    null);

        chatRoom.fireMessageReceivedEvent(
            message,
            ircMUCOpSet.findSystemMember(),
            System.currentTimeMillis(),
            ChatRoomMessageReceivedEvent.SYSTEM_MESSAGE_RECEIVED);
    }

    /**
     * Adds a chat room to the server chat room list.
     * 
     * @param chatRoomName the name of the chat room to add
     */
    private void addServerChatRoom(String chatRoomName)
    {
        synchronized (serverChatRoomList)
        {
            if (!serverChatRoomList.contains(chatRoomName))
                serverChatRoomList.add(chatRoomName);
        }
    }

    /**
     * After waiting a certain time notifies all interested listeners that a
     * join has failed, because there's no response from the server.
     */
    private class JoinTimeoutTask extends TimerTask
    {
        private ChatRoom chatRoom;

        /**
         * Creates an instance of <tt>JoinTimeoutTask</tt>.
         * 
         * @param chatRoom the chat room for which the join has been timed out.
         */
        public JoinTimeoutTask(ChatRoom chatRoom)
        {
            this.chatRoom = chatRoom;
        }
        
        /**
         * Notifies all interested listeners that a join has failed, because 
         * there's no response from the server.
         * @see java.util.TimerTask#run()
         */
        public void run()
        {
            ((OperationSetMultiUserChatIrcImpl) parentProvider
                .getOperationSet(OperationSetMultiUserChat.class))
                    .fireLocalUserPresenceEvent(chatRoom,
                    LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_JOIN_FAILED,
                    "Failed to join the  " + chatRoom.getName()
                    + " chat room, because there is no response from the server.");
        }
    }

    /**
     * Locks a chat room operation.
     */
    private void lockChatRoomOperation()
    {
        synchronized (operationLock)
        {
            try
            {
                operationLock.wait(5000);
            }
            catch (InterruptedException e)
            {
                logger.error("Chat Room operation lock thread interrupted.", e);
            }
        }
    }

    /**
     * Notifies the waiting chat room operation.
     * @param responseCode the response code of the operation to notify for
     */
    private void notifyChatRoomOperation(int responseCode)
    {
        this.operationResponseCode = responseCode;

        synchronized (operationLock)
        {
            operationLock.notify();
        }
    }

    /**
     * Kicks the participant with the given contact address from the given
     * channel.
     * 
     * @param chatRoomName the name of the chat room
     * @param contactAddress the address of the contact to kick
     * @param reason the reason of the kick
     * 
     * @throws OperationFailedException if we are not joined or we don't have
     * enough privileges to kick a participant.
     */
    public void kickParticipant(String chatRoomName,
                                String contactAddress,
                                String reason)
        throws OperationFailedException
    {
        this.kick(chatRoomName, contactAddress, reason);

        this.lockChatRoomOperation();

        if (operationResponseCode == ERR_CHANOPRIVSNEEDED)
            throw new OperationFailedException(
                "You need to have operator privileges"
                + "in order to kick a contact.",
                OperationFailedException.NOT_ENOUGH_PRIVILEGES);
        else if (operationResponseCode == ERR_NEEDMOREPARAMS)
            throw new OperationFailedException(
                "The server need more parameters in order to perform"
                + "this operation.",
                OperationFailedException.GENERAL_ERROR);
        else if (operationResponseCode == ERR_NOSUCHCHANNEL)
            throw new OperationFailedException(
                "The channel from which the contact should be kicked"
                + "was not found.",
                OperationFailedException.NOT_FOUND);
        else if (operationResponseCode == ERR_BADCHANMASK)
            throw new OperationFailedException(
                "The channel from which the contact should be kicked"
                + "was not found.",
                OperationFailedException.NOT_FOUND);
        else if (operationResponseCode == ERR_NOTONCHANNEL)
            throw new OperationFailedException(
                "You need to be joined to the chat room in order"
                + "to kick a contact from it.",
                OperationFailedException.CHAT_ROOM_NOT_JOINED);
    }

    /**
     * Changes the topic of the given channel.
     * 
     * @param channel the channel to change
     * @param topic the new topic to set
     * @throws OperationFailedException thrown if the user is not joined to the
     * channel or if he/she doesn't have enough privileges to change the
     * topic or if the topic is null.
     */
    public void setSubject(String channel, String topic)
        throws OperationFailedException
    {
        this.setTopic(channel, topic);

        this.lockChatRoomOperation();

        if (operationResponseCode == ERR_NEEDMOREPARAMS)
            new OperationFailedException(
                "More parameters should be provided to the server.",
                OperationFailedException.GENERAL_ERROR);
        else if (operationResponseCode == ERR_NOTONCHANNEL)
            new OperationFailedException(
                "You need to be joined in order to"
                + " change the subject of the chat room.",
                OperationFailedException.CHAT_ROOM_NOT_JOINED);
        else if (operationResponseCode == ERR_CHANOPRIVSNEEDED)
            new OperationFailedException(
                "You don't have enough privileges"
                + " to change the chat room subject.",
                OperationFailedException.NOT_ENOUGH_PRIVILEGES);
    }

    /**
     * Changes the user nick name on the IRC server.
     * 
     * @param nickname the new nickname
     * @throws OperationFailedException if the nickname is already used by
     * someone else
     */
    public void setUserNickname(String nickname)
        throws OperationFailedException
    {
        this.changeNick(nickname);

        this.lockChatRoomOperation();

        if (operationResponseCode == ERR_NICKNAMEINUSE)
            throw new OperationFailedException(
                "The nickname you chosed is already used by someone else.",
                OperationFailedException.IDENTIFICATION_CONFLICT);
        else if (operationResponseCode == ERR_NICKCOLLISION)
            throw new OperationFailedException(
                "The nickname you chosed is already used by someone else.",
                OperationFailedException.IDENTIFICATION_CONFLICT);
        else if (operationResponseCode == ERR_NONICKNAMEGIVEN)
            throw new OperationFailedException(
                "You need to enter a valid nickname.",
                OperationFailedException.ILLEGAL_ARGUMENT);
        else if (operationResponseCode == ERR_ERRONEUSNICKNAME)
            throw new OperationFailedException(
                "You need to enter a valid nickname.",
                OperationFailedException.ILLEGAL_ARGUMENT);
    }

    /**
     * Creates the chat room given by <tt>target</tt>.
     * @param target the name of the chat room to create
     */
    protected void createPrivateChatRoom(String target)
    {
        ChatRoomIrcImpl privateChatRoom
            = ircMUCOpSet.findPrivateChatRoom(target);

        if(privateChatRoom == null)
            return;

        ChatRoomMember sourceMember = privateChatRoom.getChatRoomMember(
            parentProvider.getAccountID().getService());

        if (sourceMember == null)
            sourceMember
                = new ChatRoomMemberIrcImpl(
                        parentProvider,
                        privateChatRoom,
                        parentProvider.getAccountID().getService(),
                        ChatRoomMemberRole.GUEST);

        MessageIrcImpl queryMessage
            = new MessageIrcImpl(   "Private conversation initiated.",
                                    MessageIrcImpl.DEFAULT_MIME_TYPE,
                                    MessageIrcImpl.DEFAULT_MIME_ENCODING,
                                    null);

        privateChatRoom.fireMessageReceivedEvent(
            queryMessage,
            sourceMember,
            System.currentTimeMillis(),
            ChatRoomMessageReceivedEvent.SYSTEM_MESSAGE_RECEIVED);
    }
}