This repository was archived by the owner on Sep 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRC.cs
More file actions
executable file
·402 lines (331 loc) · 13.5 KB
/
IRC.cs
File metadata and controls
executable file
·402 lines (331 loc) · 13.5 KB
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
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Text.RegularExpressions;
namespace IRC_Library
{
public sealed class IRC
{
public IRC(IPEndPoint endpoint)
{
this.EndPoint = endpoint;
MessageReceived += (sender, id, message) =>
{
if (id.Equals("PRIVMSG", StringComparison.InvariantCultureIgnoreCase) && PrivateMessageReceived != null)
{
int index = message.IndexOf(' ');
PrivateMessageReceived(sender, message.Substring(0, index), message.Substring(index + 1));
}
if (id.Equals("NOTICE", StringComparison.InvariantCultureIgnoreCase) && PrivateMessageReceived != null)
{
int index = message.IndexOf(' ');
NoticeMessageReceived(sender, message.Substring(0, index), message.Substring(index + 1));
}
};
RawMessageReceived += (message) =>
{
if (message.StartsWith("PING ", StringComparison.InvariantCultureIgnoreCase))
SendRawMessage("PONG " + message.Substring(5));
};
}
public IRC(IPAddress address, int port = 6667) : this(new IPEndPoint(address, port))
{
}
public IRC(string address, int port = 6667) : this(new IPEndPoint(Dns.GetHostAddresses(address)[0], port))
{
}
/// <summary>
/// Address of remote server.
/// </summary>
/// <value>The end point.</value>
public IPEndPoint EndPoint
{
get;
private set;
}
private TcpClient _client = null;
private StreamReader _reader = null;
private StreamWriter _writer = null;
private Thread t_listener = null;
/// <summary>
/// Connect to Server using specified nick, username, real name and possibly with password.
/// The Server is specified in constructor.
/// </summary>
/// <param name="nick">Nick.</param>
/// <param name="username">Username.</param>
/// <param name="realName">Real name.</param>
/// <param name="password">Password (optional).</param>
public void Connect(string nick, string username, string realName, string password = null)
{
if (Connected)
throw new AlreadyConnectedException();
if (!IsValidNickname(nick))
throw new ArgumentRegexException(nameof(nick), "^[a-z_\\-\\[\\]\\^\\{}\\|`\\\\][a-z0-9_\\-\\[\\]\\^\\{}\\|`\\\\]*$");
if (!Regex.IsMatch(username, "^[a-z_\\-\\[\\]\\^\\{}\\|`\\\\][a-z0-9_\\-\\[\\]\\^\\{}\\|`\\\\]*$"))
throw new ArgumentRegexException(nameof(username), "^[a-z_\\-\\[\\]\\^\\{}\\|`\\\\][a-z0-9_\\-\\[\\]\\^\\{}\\|`\\\\]*$");
_client = new TcpClient();
_client.Connect(EndPoint);
_reader = new StreamReader(_client.GetStream());
t_listener = new Thread(() =>
{
while (Connected)
{
try
{
string line = _reader.ReadLine();
if (string.IsNullOrWhiteSpace(line))
continue;
if (RawMessageReceived != null)
RawMessageReceived(line);
if (line.StartsWith("ERROR ", StringComparison.InvariantCultureIgnoreCase))
Close();
if (MessageReceived == null)
continue;
if (line[0] != ':')
continue;
line = line.Substring(1);
int firstSpace = line.IndexOf(' ');
if (firstSpace == -1)
continue;
Sender sender = Sender.FromIRC(line.Substring(0, firstSpace));
line = line.Substring(firstSpace + 1);
firstSpace = line.IndexOf(' ');
if (firstSpace == -1)
continue;
string id = line.Substring(0, firstSpace);
line = line.Substring(firstSpace + 1);
MessageReceived(sender, id, line);
}
catch
{
break;
}
}
});
t_listener.Start();
_writer = new StreamWriter(_client.GetStream());
if (password != null)
SendRawMessage("PASS " + password);
SendRawMessage("NICK " + nick);
SendRawMessage($"USER {username} 8 * :{realName}");
}
/// <summary>
/// Informs you whenever you are connected to Server.
/// </summary>
public bool Connected
{
get
{
return _client != null && _client.Connected;
}
}
/// <summary>
/// Quit IRC and close connection.
/// </summary>
/// <param name="reason">Reason of quitting. The message is sent to other clients.</param>
public void Quit(string reason = null)
{
if (reason == null)
SendRawMessage("QUIT");
else
SendRawMessage($"QUIT :{reason}");
Close();
}
/// <summary>
/// Close IRC connection.
/// </summary>
public void Close()
{
if (!Connected)
return;
if (ConnectionClosed != null)
ConnectionClosed();
_writer.Dispose();
_reader.Dispose();
_client.Close();
_client = null;
}
public event EmptyEventHandler ConnectionClosed;
public delegate void EmptyEventHandler();
public event RawMessageHandler RawMessageReceived;
public event RawMessageHandler RawMessageSend;
public delegate void RawMessageHandler(string message);
public event MessageHandler MessageReceived;
public delegate void MessageHandler(Sender sender, string id, string message);
public event PrivateMessageHandler PrivateMessageReceived;
public event PrivateMessageHandler NoticeMessageReceived;
public delegate void PrivateMessageHandler(Sender sender, string channel, string message);
public void SendRawMessage(string message)
{
if (!Connected)
throw new NotConnectedException();
_writer.WriteLine(message);
_writer.Flush();
if (RawMessageSend != null)
RawMessageSend(message);
}
public delegate void ChannelNamesDelegate(Channel channel, string[] names);
public event ChannelNamesDelegate ChannelNames;
public delegate void ChannelJoinDelegate(Channel channel, string user);
public event ChannelJoinDelegate ChannelJoin;
public event ChannelJoinDelegate ChannelJoinSelf;
public event ChannelJoinDelegate ChannelJoinOther;
#region Channel
/// <summary>
/// Checks validity of channel name
/// </summary>
/// <param name="name">Name of the channel.</param>
public static bool IsValidChannelName(string name)
{
if (string.IsNullOrEmpty(name))
return false;
if (name.Length > 50)
return false;
if (name.IndexOfAny(new char[] { (char)7, ' ', ',' }) != -1)
return false;
var ch = name[0];
switch (ch)
{
case '&':
case '#':
case '+':
case '!':
return true;
default:
return false;
}
}
public static bool IsValidNickname(string name)
{
if (string.IsNullOrEmpty(name))
return false;
/*
//While the maximum length is limited to nine characters, clients SHOULD accept longer strings as they may become used in future evolutions of the protocol.
if (name.Length > 9)
return false;
*/
return Regex.IsMatch(name, "^[a-z_\\-\\[\\]\\^\\{}\\|`\\\\][a-z0-9_\\-\\[\\]\\^\\{}\\|`\\\\]*$");
}
public void JoinChannel(string channel)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
SendRawMessage($"JOIN {channel}");
}
public void LeaveChannel(string channel)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
SendRawMessage($"PART {channel}");
}
#endregion
#region Channel Topic
public void ChangeChannelTopic(string channel, string newTopic)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
if (string.IsNullOrWhiteSpace(newTopic))
throw new ArgumentNullException(nameof(newTopic));
SendRawMessage($"TOPIC {channel} :{newTopic}");
}
public void RemoveChannelTopic(string channel)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
SendRawMessage($"TOPIC {channel} :");
}
public void RequestChannelTopic(string channel)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
SendRawMessage($"TOPIC {channel}");
}
public void SetChannelTopicEditable(string channel, bool editable)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
SendRawMessage(editable ? $"MODE {channel} -t" : $"MODE {channel} +t");
}
//332 <self_nick> <channel> :<topic>
public delegate void TopicChangedDelegate(Channel channel, string topic);
public event TopicChangedDelegate TopicChanged;
//333 <self_nick> <channel> <user> <time>
public delegate void TopicChangedTimeDelegate(Channel channel, ChannelUser user, string topic);
public event TopicChangedTimeDelegate TopicChangedTime;
#endregion
#region Channel Invites
public void SetChannelInviteOnly(IRC lib, string channel, bool inviteOnly)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
lib.SendRawMessage(inviteOnly ? $"MODE {channel} +t" : $"MODE {channel} -t");
}
public void InviteToChannel(IRC lib, string channel, string nick)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
lib.SendRawMessage($"INVITE {nick} {channel}");
}
public void AllowJoinOnInviteOnly(IRC lib, string channel, string nick, bool allow = true)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
lib.SendRawMessage(allow ? $"MODE {channel} +I {nick}" : $"MODE {channel} -I {nick}");
}
#endregion
#region Channel Bad Words Filter
public void EnableBadWordsFilter(IRC lib, string channel)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
lib.SendRawMessage($"MODE {channel} +G");
}
public void DisableBadWordsFilter(IRC lib, string channel)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
lib.SendRawMessage($"MODE {channel} -G");
}
#endregion
#region Channel User Permissions
public void AddChannelOperator(string channel, string nick)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
SendRawMessage($"MODE {channel} +o {nick}");
}
public void RemoveChannelOperator(string channel, string nick)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
SendRawMessage($"MODE {channel} -o {nick}");
}
public void AddChannelHalfOperator(string channel, string nick)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
SendRawMessage($"MODE {channel} +h {nick}");
}
public void RemoveChannelHalfOperator(string channel, string nick)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
SendRawMessage($"MODE {channel} -h {nick}");
}
public void AddChannelVoice(string channel, string nick)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
SendRawMessage($"MODE {channel} +v {nick}");
}
public void RemoveChannelVoice(string channel, string nick)
{
if (!IsValidChannelName(channel))
throw new InvalidChannelNameException(channel);
SendRawMessage($"MODE {channel} -v {nick}");
}
#endregion
}
}