/
IrcClient.cs
3226 lines (2942 loc) · 128 KB
/
IrcClient.cs
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
/*
* $Id$
* $URL$
* $Rev$
* $Author$
* $Date$
*
* SmartIrc4net - the IRC library for .NET/C# <http://smartirc4net.sf.net>
*
* Copyright (c) 2003-2010, 2012-2014 Mirco Bauer <meebey@meebey.net>
* Copyright (c) 2008-2009 Thomas Bruderer <apophis@apophis.ch>
*
* Full LGPL License: <http://www.gnu.org/licenses/lgpl.txt>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace Meebey.SmartIrc4net
{
/// <summary>
/// This layer is an event driven high-level API with all features you could need for IRC programming.
/// </summary>
/// <threadsafety static="true" instance="true" />
public class IrcClient : IrcCommands
{
private string _Nickname = string.Empty;
private string[] _NicknameList;
private int _CurrentNickname;
private string _Realname = string.Empty;
private string _Usermode = string.Empty;
private int _IUsermode;
private string _Username = string.Empty;
private string _Password = string.Empty;
private bool _IsAway;
private string _CtcpVersion;
private bool _ActiveChannelSyncing;
private bool _PassiveChannelSyncing;
private bool _AutoJoinOnInvite;
private bool _AutoRejoin;
private Dictionary<string, string> _AutoRejoinChannels = new Dictionary<string, string>();
private bool _AutoRejoinOnKick;
private bool _AutoRelogin;
private bool _AutoNickHandling = true;
private bool _SupportNonRfc;
private bool _SupportNonRfcLocked;
private StringCollection _Motd = new StringCollection();
private bool _MotdReceived;
private Array _ReplyCodes = Enum.GetValues(typeof(ReplyCode));
private StringCollection _JoinedChannels = new StringCollection();
private Hashtable _Channels = Hashtable.Synchronized(new Hashtable(new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer()));
private Hashtable _IrcUsers = Hashtable.Synchronized(new Hashtable(new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer()));
private List<ChannelInfo> _ChannelList;
private Object _ChannelListSyncRoot = new Object();
private AutoResetEvent _ChannelListReceivedEvent;
private List<WhoInfo> _WhoList;
private Object _WhoListSyncRoot = new Object();
private AutoResetEvent _WhoListReceivedEvent;
private List<BanInfo> _BanList;
private Object _BanListSyncRoot = new Object();
private AutoResetEvent _BanListReceivedEvent;
private List<BanInfo> _BanExceptList;
private Object _BanExceptListSyncRoot = new Object();
private AutoResetEvent _BanExceptListReceivedEvent;
private List<BanInfo> _InviteExceptList;
private Object _InviteExceptListSyncRoot = new Object();
private AutoResetEvent _InviteExceptListReceivedEvent;
private ServerProperties _ServerProperties = new ServerProperties();
private static Regex _ReplyCodeRegex = new Regex("^:[^ ]+? ([0-9]{3}) .+$", RegexOptions.Compiled);
private static Regex _PingRegex = new Regex("^PING :.*", RegexOptions.Compiled);
private static Regex _ErrorRegex = new Regex("^ERROR :.*", RegexOptions.Compiled);
private static Regex _ActionRegex = new Regex("^:.*? PRIVMSG (.).* :"+"\x1"+"ACTION .*"+"\x1"+"$", RegexOptions.Compiled);
private static Regex _CtcpRequestRegex = new Regex("^:.*? PRIVMSG .* :"+"\x1"+".*"+"\x1"+"$", RegexOptions.Compiled);
private static Regex _MessageRegex = new Regex("^:.*? PRIVMSG (.).* :.*$", RegexOptions.Compiled);
private static Regex _CtcpReplyRegex = new Regex("^:.*? NOTICE .* :"+"\x1"+".*"+"\x1"+"$", RegexOptions.Compiled);
private static Regex _NoticeRegex = new Regex("^:.*? NOTICE (.).* :.*$", RegexOptions.Compiled);
private static Regex _InviteRegex = new Regex("^:.*? INVITE .* .*$", RegexOptions.Compiled);
private static Regex _JoinRegex = new Regex("^:.*? JOIN .*$", RegexOptions.Compiled);
private static Regex _TopicRegex = new Regex("^:.*? TOPIC .* :.*$", RegexOptions.Compiled);
private static Regex _NickRegex = new Regex("^:.*? NICK .*$", RegexOptions.Compiled);
private static Regex _KickRegex = new Regex("^:.*? KICK .* .*$", RegexOptions.Compiled);
private static Regex _PartRegex = new Regex("^:.*? PART .*$", RegexOptions.Compiled);
private static Regex _ModeRegex = new Regex("^:.*? MODE (.*) .*$", RegexOptions.Compiled);
private static Regex _QuitRegex = new Regex("^:.*? QUIT :.*$", RegexOptions.Compiled);
private static Regex _BounceMessageRegex = new Regex("^Try server (.+), port ([0-9]+)$", RegexOptions.Compiled);
ChannelModeMap ChannelModeMap { get; set; }
public event EventHandler OnRegistered;
public event PingEventHandler OnPing;
public event PongEventHandler OnPong;
public event IrcEventHandler OnRawMessage;
public event ErrorEventHandler OnError;
public event IrcEventHandler OnErrorMessage;
public event JoinEventHandler OnJoin;
public event NamesEventHandler OnNames;
public event ListEventHandler OnList;
public event PartEventHandler OnPart;
public event QuitEventHandler OnQuit;
public event KickEventHandler OnKick;
public event AwayEventHandler OnAway;
public event IrcEventHandler OnUnAway;
public event IrcEventHandler OnNowAway;
public event InviteEventHandler OnInvite;
public event BanEventHandler OnBan;
public event UnbanEventHandler OnUnban;
public event BanEventHandler OnBanException;
public event UnbanEventHandler OnUnBanException;
public event BanEventHandler OnInviteException;
public event UnbanEventHandler OnUnInviteException;
public event OwnerEventHandler OnOwner;
public event DeownerEventHandler OnDeowner;
public event ChannelAdminEventHandler OnChannelAdmin;
public event DeChannelAdminEventHandler OnDeChannelAdmin;
public event OpEventHandler OnOp;
public event DeopEventHandler OnDeop;
public event HalfopEventHandler OnHalfop;
public event DehalfopEventHandler OnDehalfop;
public event VoiceEventHandler OnVoice;
public event DevoiceEventHandler OnDevoice;
public event WhoEventHandler OnWho;
public event MotdEventHandler OnMotd;
public event TopicEventHandler OnTopic;
public event TopicChangeEventHandler OnTopicChange;
public event NickChangeEventHandler OnNickChange;
public event IrcEventHandler OnModeChange;
public event IrcEventHandler OnUserModeChange;
public event EventHandler<ChannelModeChangeEventArgs> OnChannelModeChange;
public event IrcEventHandler OnChannelMessage;
public event ActionEventHandler OnChannelAction;
public event IrcEventHandler OnChannelNotice;
public event IrcEventHandler OnChannelActiveSynced;
public event IrcEventHandler OnChannelPassiveSynced;
public event IrcEventHandler OnQueryMessage;
public event ActionEventHandler OnQueryAction;
public event IrcEventHandler OnQueryNotice;
public event CtcpEventHandler OnCtcpRequest;
public event CtcpEventHandler OnCtcpReply;
public event BounceEventHandler OnBounce;
/// <summary>
/// Enables/disables the active channel sync feature.
/// Default: false
/// </summary>
public bool ActiveChannelSyncing {
get {
return _ActiveChannelSyncing;
}
set {
#if LOG4NET
if (value) {
Logger.ChannelSyncing.Info("Active channel syncing enabled");
} else {
Logger.ChannelSyncing.Info("Active channel syncing disabled");
}
#endif
_ActiveChannelSyncing = value;
}
}
/// <summary>
/// Enables/disables the passive channel sync feature. Not implemented yet!
/// </summary>
public bool PassiveChannelSyncing {
get {
return _PassiveChannelSyncing;
}
/*
set {
#if LOG4NET
if (value) {
Logger.ChannelSyncing.Info("Passive channel syncing enabled");
} else {
Logger.ChannelSyncing.Info("Passive channel syncing disabled");
}
#endif
_PassiveChannelSyncing = value;
}
*/
}
/// <summary>
/// Sets the ctcp version that should be replied on ctcp version request.
/// </summary>
public string CtcpVersion {
get {
return _CtcpVersion;
}
set {
_CtcpVersion = value;
}
}
/// <summary>
/// Enables/disables auto joining of channels when invited.
/// Default: false
/// </summary>
public bool AutoJoinOnInvite {
get {
return _AutoJoinOnInvite;
}
set {
#if LOG4NET
if (value) {
Logger.ChannelSyncing.Info("AutoJoinOnInvite enabled");
} else {
Logger.ChannelSyncing.Info("AutoJoinOnInvite disabled");
}
#endif
_AutoJoinOnInvite = value;
}
}
/// <summary>
/// Enables/disables automatic rejoining of channels when a connection to the server is lost.
/// Default: false
/// </summary>
public bool AutoRejoin {
get {
return _AutoRejoin;
}
set {
#if LOG4NET
if (value) {
Logger.ChannelSyncing.Info("AutoRejoin enabled");
} else {
Logger.ChannelSyncing.Info("AutoRejoin disabled");
}
#endif
_AutoRejoin = value;
}
}
/// <summary>
/// Enables/disables auto rejoining of channels when kicked.
/// Default: false
/// </summary>
public bool AutoRejoinOnKick {
get {
return _AutoRejoinOnKick;
}
set {
#if LOG4NET
if (value) {
Logger.ChannelSyncing.Info("AutoRejoinOnKick enabled");
} else {
Logger.ChannelSyncing.Info("AutoRejoinOnKick disabled");
}
#endif
_AutoRejoinOnKick = value;
}
}
/// <summary>
/// Enables/disables auto relogin to the server after a reconnect.
/// Default: false
/// </summary>
public bool AutoRelogin {
get {
return _AutoRelogin;
}
set {
#if LOG4NET
if (value) {
Logger.ChannelSyncing.Info("AutoRelogin enabled");
} else {
Logger.ChannelSyncing.Info("AutoRelogin disabled");
}
#endif
_AutoRelogin = value;
}
}
/// <summary>
/// Enables/disables auto nick handling on nick collisions
/// Default: true
/// </summary>
public bool AutoNickHandling {
get {
return _AutoNickHandling;
}
set {
#if LOG4NET
if (value) {
Logger.ChannelSyncing.Info("AutoNickHandling enabled");
} else {
Logger.ChannelSyncing.Info("AutoNickHandling disabled");
}
#endif
_AutoNickHandling = value;
}
}
/// <summary>
/// Enables/disables support for non rfc features.
/// Default: false
/// </summary>
public bool SupportNonRfc {
get {
return _SupportNonRfc;
}
set {
if (_SupportNonRfcLocked) {
return;
}
#if LOG4NET
if (value) {
Logger.ChannelSyncing.Info("SupportNonRfc enabled");
} else {
Logger.ChannelSyncing.Info("SupportNonRfc disabled");
}
#endif
_SupportNonRfc = value;
}
}
/// <summary>
/// Gets the nickname of us.
/// </summary>
public string Nickname {
get {
return _Nickname;
}
}
/// <summary>
/// Gets the list of nicknames of us.
/// </summary>
public string[] NicknameList {
get {
return _NicknameList;
}
}
/// <summary>
/// Gets the supposed real name of us.
/// </summary>
public string Realname {
get {
return _Realname;
}
}
/// <summary>
/// Gets the username for the server.
/// </summary>
/// <remarks>
/// System username is set by default
/// </remarks>
public string Username {
get {
return _Username;
}
}
/// <summary>
/// Gets the alphanumeric mode mask of us.
/// </summary>
public string Usermode {
get {
return _Usermode;
}
}
/// <summary>
/// Gets the numeric mode mask of us.
/// </summary>
public int IUsermode {
get {
return _IUsermode;
}
}
/// <summary>
/// Returns if we are away on this connection
/// </summary>
public bool IsAway {
get {
return _IsAway;
}
}
/// <summary>
/// Gets the password for the server.
/// </summary>
public string Password {
get {
return _Password;
}
}
/// <summary>
/// Gets the list of channels we are joined.
/// </summary>
public StringCollection JoinedChannels {
get {
return _JoinedChannels;
}
}
/// <summary>
/// Gets the server message of the day.
/// </summary>
public StringCollection Motd {
get {
return _Motd;
}
}
public object BanListSyncRoot {
get {
return _BanListSyncRoot;
}
}
/// <summary>
/// Gets the special functionality supported by this server.
/// </summary>
public ServerProperties ServerProperties {
get {
return _ServerProperties;
}
}
/// <summary>
/// This class manages the connection server and provides access to all the objects needed to send and receive messages.
/// </summary>
public IrcClient()
{
#if LOG4NET
Logger.Main.Debug("IrcClient created");
#endif
OnReadLine += new ReadLineEventHandler(_Worker);
OnDisconnected += new EventHandler(_OnDisconnected);
OnConnectionError += new EventHandler(_OnConnectionError);
ChannelModeMap = new ChannelModeMap();
}
#if LOG4NET
~IrcClient()
{
Logger.Main.Debug("IrcClient destroyed");
}
#endif
/// <summary>
/// Connection parameters required to establish an server connection.
/// </summary>
/// <param name="addresslist">The list of server hostnames.</param>
/// <param name="port">The TCP port the server listens on.</param>
public new void Connect(string[] addresslist, int port)
{
_SupportNonRfcLocked = true;
ChannelModeMap = new ChannelModeMap();
base.Connect(addresslist, port);
}
/// <overloads>
/// Reconnects to the current server.
/// </overloads>
/// <param name="login">If the login data should be sent, after successful connect.</param>
/// <param name="channels">If the channels should be rejoined, after successful connect.</param>
public void Reconnect(bool login, bool channels)
{
if (channels) {
_StoreChannelsToRejoin();
}
base.Reconnect();
if (login) {
//reset the nick to the original nicklist
_CurrentNickname = 0;
// FIXME: honor _Nickname (last used nickname)
Login(_NicknameList, Realname, IUsermode, Username, Password);
}
if (channels) {
_RejoinChannels();
}
}
/// <param name="login">If the login data should be sent, after successful connect.</param>
public void Reconnect(bool login)
{
Reconnect(login, AutoRejoin);
}
/// <summary>
/// Login parameters required identify with server connection
/// </summary>
/// <remark>Login is used at the beginning of connection to specify the username, hostname and realname of a new user.</remark>
/// <param name="nicklist">The users list of 'nick' names which may NOT contain spaces</param>
/// <param name="realname">The users 'real' name which may contain space characters</param>
/// <param name="usermode">A numeric mode parameter.
/// <remark>
/// Set to 0 to recieve wallops and be invisible.
/// Set to 4 to be invisible and not receive wallops.
/// </remark>
/// </param>
/// <param name="username">The user's machine logon name</param>
/// <param name="password">The optional password can and MUST be set before any attempt to register
/// the connection is made.</param>
public void Login(string[] nicklist, string realname, int usermode, string username, string password)
{
#if LOG4NET
Logger.Connection.Info("logging in");
#endif
_NicknameList = (string[])nicklist.Clone();
// here we set the nickname which we will try first
_Nickname = _NicknameList[0].Replace(" ", "");
_Realname = realname;
_IUsermode = usermode;
if (username != null && username.Length > 0) {
_Username = username.Replace(" ", "");
} else {
_Username = Environment.UserName.Replace(" ", "");
}
if (password != null && password.Length > 0) {
_Password = password;
RfcPass(Password, Priority.Critical);
}
RfcNick(Nickname, Priority.Critical);
RfcUser(Username, IUsermode, Realname, Priority.Critical);
}
/// <summary>
/// Login parameters required identify with server connection
/// </summary>
/// <remark>Login is used at the beginning of connection to specify the username, hostname and realname of a new user.</remark>
/// <param name="nicklist">The users list of 'nick' names which may NOT contain spaces</param>
/// <param name="realname">The users 'real' name which may contain space characters</param>
/// <param name="usermode">A numeric mode parameter.
/// Set to 0 to recieve wallops and be invisible.
/// Set to 4 to be invisible and not receive wallops.</param>
/// <param name="username">The user's machine logon name</param>
public void Login(string[] nicklist, string realname, int usermode, string username)
{
Login(nicklist, realname, usermode, username, "");
}
/// <summary>
/// Login parameters required identify with server connection
/// </summary>
/// <remark>Login is used at the beginning of connection to specify the username, hostname and realname of a new user.</remark>
/// <param name="nicklist">The users list of 'nick' names which may NOT contain spaces</param>
/// <param name="realname">The users 'real' name which may contain space characters</param>
/// <param name="usermode">A numeric mode parameter.
/// Set to 0 to recieve wallops and be invisible.
/// Set to 4 to be invisible and not receive wallops.</param>
public void Login(string[] nicklist, string realname, int usermode)
{
Login(nicklist, realname, usermode, "", "");
}
/// <summary>
/// Login parameters required identify with server connection
/// </summary>
/// <remark>Login is used at the beginning of connection to specify the username, hostname and realname of a new user.</remark>
/// <param name="nicklist">The users list of 'nick' names which may NOT contain spaces</param>
/// <param name="realname">The users 'real' name which may contain space characters</param>
public void Login(string[] nicklist, string realname)
{
Login(nicklist, realname, 0, "", "");
}
/// <summary>
/// Login parameters required identify with server connection
/// </summary>
/// <remark>Login is used at the beginning of connection to specify the username, hostname and realname of a new user.</remark>
/// <param name="nick">The users 'nick' name which may NOT contain spaces</param>
/// <param name="realname">The users 'real' name which may contain space characters</param>
/// <param name="usermode">A numeric mode parameter.
/// Set to 0 to recieve wallops and be invisible.
/// Set to 4 to be invisible and not receive wallops.</param>
/// <param name="username">The user's machine logon name</param>
/// <param name="password">The optional password can and MUST be set before any attempt to register
/// the connection is made.</param>
public void Login(string nick, string realname, int usermode, string username, string password)
{
Login(new string[] {nick, nick+"_", nick+"__"}, realname, usermode, username, password);
}
/// <summary>
/// Login parameters required identify with server connection
/// </summary>
/// <remark>Login is used at the beginning of connection to specify the username, hostname and realname of a new user.</remark>
/// <param name="nick">The users 'nick' name which may NOT contain spaces</param>
/// <param name="realname">The users 'real' name which may contain space characters</param>
/// <param name="usermode">A numeric mode parameter.
/// Set to 0 to recieve wallops and be invisible.
/// Set to 4 to be invisible and not receive wallops.</param>
/// <param name="username">The user's machine logon name</param>
public void Login(string nick, string realname, int usermode, string username)
{
Login(new string[] {nick, nick+"_", nick+"__"}, realname, usermode, username, "");
}
/// <summary>
/// Login parameters required identify with server connection
/// </summary>
/// <remark>Login is used at the beginning of connection to specify the username, hostname and realname of a new user.</remark>
/// <param name="nick">The users 'nick' name which may NOT contain spaces</param>
/// <param name="realname">The users 'real' name which may contain space characters</param>
/// <param name="usermode">A numeric mode parameter.
/// Set to 0 to recieve wallops and be invisible.
/// Set to 4 to be invisible and not receive wallops.</param>
public void Login(string nick, string realname, int usermode)
{
Login(new string[] {nick, nick+"_", nick+"__"}, realname, usermode, "", "");
}
/// <summary>
/// Login parameters required identify with server connection
/// </summary>
/// <remark>Login is used at the beginning of connection to specify the username, hostname and realname of a new user.</remark>
/// <param name="nick">The users 'nick' name which may NOT contain spaces</param>
/// <param name="realname">The users 'real' name which may contain space characters</param>
public void Login(string nick, string realname)
{
Login(new string[] {nick, nick+"_", nick+"__"}, realname, 0, "", "");
}
/// <summary>
/// Determine if a specifier nickname is you
/// </summary>
/// <param name="nickname">The users 'nick' name which may NOT contain spaces</param>
/// <returns>True if nickname belongs to you</returns>
public bool IsMe(string nickname)
{
return String.Compare(Nickname, nickname, StringComparison.OrdinalIgnoreCase) == 0;
}
/// <summary>
/// Determines if your nickname can be found in a specified channel
/// </summary>
/// <param name="channelname">The name of the channel you wish to query</param>
/// <returns>True if you are found in channel</returns>
public bool IsJoined(string channelname)
{
return IsJoined(channelname, Nickname);
}
/// <summary>
/// Determine if a specified nickname can be found in a specified channel
/// </summary>
/// <param name="channelname">The name of the channel you wish to query</param>
/// <param name="nickname">The users 'nick' name which may NOT contain spaces</param>
/// <returns>True if nickname is found in channel</returns>
public bool IsJoined(string channelname, string nickname)
{
if (channelname == null) {
throw new System.ArgumentNullException("channelname");
}
if (nickname == null) {
throw new System.ArgumentNullException("nickname");
}
Channel channel = GetChannel(channelname);
if (channel != null &&
channel.UnsafeUsers != null &&
channel.UnsafeUsers.ContainsKey(nickname)) {
return true;
}
return false;
}
/// <summary>
/// Returns user information
/// </summary>
/// <param name="nickname">The users 'nick' name which may NOT contain spaces</param>
/// <returns>IrcUser object of requested nickname</returns>
public IrcUser GetIrcUser(string nickname)
{
if (nickname == null) {
throw new System.ArgumentNullException("nickname");
}
return (IrcUser)_IrcUsers[nickname];
}
/// <summary>
/// Returns extended user information including channel information
/// </summary>
/// <param name="channelname">The name of the channel you wish to query</param>
/// <param name="nickname">The users 'nick' name which may NOT contain spaces</param>
/// <returns>ChannelUser object of requested channelname/nickname</returns>
public ChannelUser GetChannelUser(string channelname, string nickname)
{
if (channelname == null) {
throw new System.ArgumentNullException("channel");
}
if (nickname == null) {
throw new System.ArgumentNullException("nickname");
}
Channel channel = GetChannel(channelname);
if (channel != null) {
return (ChannelUser)channel.UnsafeUsers[nickname];
} else {
return null;
}
}
/// <summary>
///
/// </summary>
/// <param name="channelname">The name of the channel you wish to query</param>
/// <returns>Channel object of requested channel</returns>
public Channel GetChannel(string channelname)
{
if (channelname == null) {
throw new System.ArgumentNullException("channelname");
}
return (Channel)_Channels[channelname];
}
/// <summary>
/// Gets a list of all joined channels on server
/// </summary>
/// <returns>String array of all joined channel names</returns>
public string[] GetChannels()
{
string[] channels = new string[_Channels.Values.Count];
int i = 0;
foreach (Channel channel in _Channels.Values) {
channels[i++] = channel.Name;
}
return channels;
}
/// <summary>
/// Fetches a fresh list of all available channels that match the passed mask
/// </summary>
/// <returns>List of ListInfo</returns>
public IList<ChannelInfo> GetChannelList(string mask)
{
List<ChannelInfo> list = new List<ChannelInfo>();
lock (_ChannelListSyncRoot) {
_ChannelList = list;
_ChannelListReceivedEvent = new AutoResetEvent(false);
// request list
RfcList(mask);
// wait till we have the complete list
_ChannelListReceivedEvent.WaitOne();
_ChannelListReceivedEvent = null;
_ChannelList = null;
}
return list;
}
/// <summary>
/// Fetches a fresh list of users that matches the passed mask
/// </summary>
/// <returns>List of ListInfo</returns>
public IList<WhoInfo> GetWhoList(string mask)
{
List<WhoInfo> list = new List<WhoInfo>();
lock (_WhoListSyncRoot) {
_WhoList = list;
_WhoListReceivedEvent = new AutoResetEvent(false);
// request list
RfcWho(mask);
// wait till we have the complete list
_WhoListReceivedEvent.WaitOne();
_WhoListReceivedEvent = null;
_WhoList = null;
}
return list;
}
/// <summary>
/// Fetches a fresh ban list of the specified channel
/// </summary>
/// <returns>List of ListInfo</returns>
public IList<BanInfo> GetBanList(string channel)
{
List<BanInfo> list = new List<BanInfo>();
lock (_BanListSyncRoot) {
_BanList = list;
_BanListReceivedEvent = new AutoResetEvent(false);
// request list
Ban(channel);
// wait till we have the complete list
_BanListReceivedEvent.WaitOne();
_BanListReceivedEvent = null;
_BanList = null;
}
return list;
}
/// <summary>
/// Fetches a fresh ban-exceptions list from the specified channel.
/// </summary>
public IList<BanInfo> GetBanExceptionList(string channel)
{
List<BanInfo> list = new List<BanInfo>();
if (!_ServerProperties.BanExceptionCharacter.HasValue) {
return list;
}
lock (_BanExceptListSyncRoot) {
_BanExceptList = list;
_BanExceptListReceivedEvent = new AutoResetEvent(false);
BanException(channel);
_BanExceptListReceivedEvent.WaitOne();
_BanExceptListReceivedEvent = null;
_BanExceptList = null;
}
return list;
}
/// <summary>
/// Fetches a fresh invite-exceptions list from the specified channel.
/// </summary>
public IList<BanInfo> GetInviteExceptionList(string channel)
{
List<BanInfo> list = new List<BanInfo>();
if (!_ServerProperties.InviteExceptionCharacter.HasValue) {
return list;
}
lock (_InviteExceptListSyncRoot) {
_InviteExceptList = list;
_InviteExceptListReceivedEvent = new AutoResetEvent(false);
InviteException(channel);
_InviteExceptListReceivedEvent.WaitOne();
_InviteExceptListReceivedEvent = null;
_InviteExceptList = null;
}
return list;
}
public IrcMessageData MessageParser(string rawline)
{
if (rawline == null) {
throw new ArgumentNullException("rawline");
}
string line;
string[] linear;
string messagecode;
string from;
string nick = null;
string ident = null;
string host = null;
string channel = null;
string message = null;
ReceiveType type;
ReplyCode replycode;
int exclamationpos;
int atpos;
int colonpos;
if (rawline.Length == 0) {
throw new ArgumentException("Value must not be empty.", "rawline");
}
if (rawline[0] == ':') {
line = rawline.Substring(1);
} else {
line = rawline;
}
linear = line.Split(new char[] {' '});
// conform to RFC 2812
from = linear[0];
messagecode = linear[1];
exclamationpos = from.IndexOf("!", StringComparison.Ordinal);
atpos = from.IndexOf("@", StringComparison.Ordinal);
colonpos = line.IndexOf(" :", StringComparison.Ordinal);
if (colonpos != -1) {
// we want the exact position of ":" not beginning from the space
colonpos += 1;
}
if (exclamationpos != -1) {
nick = from.Substring(0, exclamationpos);
} else {
if (atpos == -1) {
// no ident and no host, should be nick then
if (!from.Contains(".")) {
// HACK: from seems to be a nick instead of servername
nick = from;
}
} else {
nick = from.Substring(0, atpos);
}
}
if ((atpos != -1) &&
(exclamationpos != -1)) {
ident = from.Substring(exclamationpos+1, (atpos - exclamationpos)-1);
}
if (atpos != -1) {
host = from.Substring(atpos+1);
}
int tmp;
if (int.TryParse(messagecode, out tmp)) {
replycode = (ReplyCode) tmp;
} else {
replycode = ReplyCode.Null;
}
type = _GetMessageType(rawline);
if (colonpos != -1) {
message = line.Substring(colonpos + 1);
}
switch (type) {
case ReceiveType.Join:
case ReceiveType.Kick:
case ReceiveType.Part:
case ReceiveType.TopicChange:
case ReceiveType.ChannelModeChange:
case ReceiveType.ChannelMessage:
case ReceiveType.ChannelAction:
case ReceiveType.ChannelNotice:
channel = linear[2];
break;
case ReceiveType.Who:
case ReceiveType.Topic:
case ReceiveType.Invite:
case ReceiveType.BanList:
case ReceiveType.ChannelMode:
channel = linear[3];
break;
case ReceiveType.Name:
channel = linear[4];
break;
}
switch (replycode) {
case ReplyCode.List:
case ReplyCode.ListEnd:
case ReplyCode.ErrorNoChannelModes:
case ReplyCode.InviteList:
case ReplyCode.ExceptionList:
channel = linear[3];
break;
}
if ((channel != null) &&
(channel[0] == ':')) {
channel = channel.Substring(1);
}
IrcMessageData data;
data = new IrcMessageData(this, from, nick, ident, host, channel, message, rawline, type, replycode);
#if LOG4NET
Logger.MessageParser.Debug("IrcMessageData "+
"nick: '"+data.Nick+"' "+
"ident: '"+data.Ident+"' "+
"host: '"+data.Host+"' "+
"type: '"+data.Type.ToString()+"' "+
"from: '"+data.From+"' "+
"channel: '"+data.Channel+"' "+
"message: '"+data.Message+"' "
);
#endif
return data;