-
Notifications
You must be signed in to change notification settings - Fork 1
/
IrcBot.cs
503 lines (443 loc) · 19 KB
/
IrcBot.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Threading;
using Meebey.SmartIrc4net;
namespace ShowBot
{
#region Helper and data model classes
public class IrcServerInfo
{
public string ServerID;
public string ServerHost;
public int ServerPort;
public bool ServerSSL;
public string Nick;
public string Username;
public string RealName;
public string Password;
public string WebBase;
public string ApiAuth;
public List<string> Channels;
public List<string> AdditionalOps;
public IrcServerInfo()
{
ServerPort = 6667;
Channels = new List<string>();
AdditionalOps = new List<string>();
RealName = Password = string.Empty;
}
}
public class ResponseStatus
{
public bool Success;
public string Message;
}
public class Request
{
public string ApiAuth;
public string Function;
public string ServerID;
public string Channel;
public string Title;
}
public class Sugggestion : Request
{
public string User;
public string Title;
}
#endregion Helper and data model classes
public class IrcBot : IDisposable
{
public IrcServerInfo Conf;
Thread IRCConnection;
public IrcClient irc;
bool SentNickServLogin = false;
static Regex ChanCmdParser = new Regex(@"^!(?<cmd>\w+)\s*(?<arg>.+)?", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
static Regex QueryCmdParser = new Regex(@"^\s*!?(?<cmd>\w+)\s*(?<arg>.+)?", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
HttpClient WebClient;
public IrcBot(IrcServerInfo c)
{
Conf = c;
irc = new IrcClient();
irc.SendDelay = 200;
irc.AutoReconnect = true;
irc.AutoRejoin = true;
irc.AutoRejoinOnKick = false;
irc.AutoRelogin = true;
irc.AutoRetry = true;
irc.UseSsl = Conf.ServerSSL;
irc.ValidateServerCertificate = false;
irc.AutoRetryDelay = 30;
irc.CtcpVersion = "ShowBot by lkalif 1.0";
irc.Encoding = Encoding.UTF8;
irc.AutoNickHandling = true;
irc.ActiveChannelSyncing = true;
WebClient = new HttpClient();
WebClient.BaseAddress = new Uri(Conf.WebBase);
WebClient.DefaultRequestHeaders.Accept.Clear();
WebClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
irc.OnRawMessage += irc_OnRawMessage;
irc.OnJoin += irc_OnJoin;
irc.OnConnected += irc_OnConnected;
irc.OnChannelMessage += irc_OnChannelMessage;
irc.OnQueryMessage += irc_OnQueryMessage;
}
public void Dispose()
{
try
{
if (irc != null)
{
if (irc.IsConnected)
{
irc.RfcDie();
irc.Disconnect();
}
irc = null;
}
if (IRCConnection != null)
{
if (IRCConnection.IsAlive)
IRCConnection.Abort();
IRCConnection = null;
}
}
catch { }
}
List<string> SplitMessage(string message)
{
List<string> ret = new List<string>();
string[] lines = Regex.Split(message, "\n+");
for (int l = 0; l < lines.Length; l++)
{
string[] words = lines[l].Split(' ');
string outstr = string.Empty;
for (int i = 0; i < words.Length; i++)
{
outstr += words[i] + " ";
if (outstr.Length > 380)
{
ret.Add(outstr.Trim());
outstr = string.Empty;
}
}
ret.Add(outstr.Trim());
}
return ret;
}
void irc_OnConnected(object sender, EventArgs e)
{
PrintMsg("IRC - " + Conf.ServerID, string.Format("Connected"));
}
async Task<ResponseStatus> SendSuggestion(Sugggestion s)
{
try
{
var response = await WebClient.PostAsJsonAsync<Sugggestion>("api.php", s);
if (response.IsSuccessStatusCode)
{
ResponseStatus res = await response.Content.ReadAsAsync<ResponseStatus>();
if (res == null)
{
throw new InvalidOperationException("Failed to deserialize the result");
}
return res;
}
}
catch (Exception ex)
{
PrintMsg("HTTP Error", ex.Message);
}
return new ResponseStatus() { Success = false, Message = "HTTP request failed" };
}
async void irc_OnChannelMessage(object sender, IrcEventArgs e)
{
try
{
Match m = ChanCmdParser.Match(e.Data.Message);
if (m.Success)
{
string cmd = m.Groups["cmd"].Value;
string arg = m.Groups["arg"].Value;
switch (cmd)
{
case "s":
var req = new Sugggestion()
{
ApiAuth = Conf.ApiAuth,
Function = "suggestion_add",
ServerID = Conf.ServerID,
Channel = e.Data.Channel,
User = e.Data.Nick,
Title = arg
};
var res = await SendSuggestion(req);
irc.SendMessage(SendType.Message, e.Data.Nick, "Sending your suggestion " + (res.Success ? "was successful" : "failed") + ": " + res.Message);
break;
case "start":
case "reset":
break;
}
}
}
catch { }
}
async void irc_OnQueryMessage(object sender, IrcEventArgs e)
{
try
{
Match m = QueryCmdParser.Match(e.Data.Message);
if (m.Success)
{
string cmd = m.Groups["cmd"].Value;
string[] args = Regex.Split(m.Groups["arg"].Value, @"\s+");
switch (cmd)
{
case "s":
case "suggest":
{
if (args.Length < 2)
{
irc.SendReply(e.Data, "Usage: " + cmd + " #channel some suggestion here");
return;
}
var title = string.Join(" ", new ArraySegment<string>(args, 1, args.Length - 1));
var req = new Sugggestion()
{
ApiAuth = Conf.ApiAuth,
Function = "suggestion_add",
ServerID = Conf.ServerID,
Channel = args[0],
User = e.Data.Nick,
Title = title
};
var res = await SendSuggestion(req);
irc.SendMessage(SendType.Message, e.Data.Nick, "Sending your suggestion " + (res.Success ? "was successful" : "failed") + ": " + res.Message);
}
break;
case "delete":
{
if (args.Length < 2 || string.IsNullOrEmpty(args[0]))
{
irc.SendReply(e.Data, "Usage: " + cmd + " #channel title - delete title");
return;
}
if (!Conf.AdditionalOps.Contains(e.Data.Nick))
{
var user = irc.GetChannelUser(args[0], e.Data.Nick);
if (user == null)
{
irc.SendReply(e.Data, "Cannot find you in that channel");
return;
}
if (!(user.IsIrcOp || user.IsOp))
{
irc.SendReply(e.Data, "You need to be a channel operator on " + args[0]);
return;
}
}
var title = m.Groups["arg"].Value;
var t = Regex.Match(title, @"^.+? (?<title>.+)");
if (t.Success)
{
title = t.Groups["title"].Value;
}
var req = new Request()
{
ApiAuth = Conf.ApiAuth,
Function = "title_delete",
ServerID = Conf.ServerID,
Channel = args[0],
Title = title,
};
try
{
var response = await WebClient.PostAsJsonAsync<Request>("api.php", req);
ResponseStatus res = await response.Content.ReadAsAsync<ResponseStatus>();
irc.SendReply(e.Data, "Sending delete request " + (res.Success ? "was successful" : "failed") + ": " + res.Message);
}
catch (Exception ex)
{
irc.SendReply(e.Data, "Failed sending reset request: " + ex.Message);
return;
}
}
break;
case "start":
case "reset":
{
if (args.Length != 1 || string.IsNullOrEmpty(args[0]))
{
irc.SendReply(e.Data, "Usage: " + cmd + " #channel - delete all titles and votes for the title suggestion on #channel");
return;
}
if (!Conf.AdditionalOps.Contains(e.Data.Nick))
{
var user = irc.GetChannelUser(args[0], e.Data.Nick);
if (user == null)
{
irc.SendReply(e.Data, "Cannot find you in that channel");
return;
}
if (!(user.IsIrcOp || user.IsOp))
{
irc.SendReply(e.Data, "You need to be a channel operator on " + args[0]);
return;
}
}
var req = new Request()
{
ApiAuth = Conf.ApiAuth,
Function = "channel_reset",
ServerID = Conf.ServerID,
Channel = args[0],
};
try
{
var response = await WebClient.PostAsJsonAsync<Request>("api.php", req);
ResponseStatus res = await response.Content.ReadAsAsync<ResponseStatus>();
irc.SendReply(e.Data, "Sending reset request " + (res.Success ? "was successful" : "failed") + ": " + res.Message);
}
catch (Exception ex)
{
irc.SendReply(e.Data, "Failed sending reset request: " + ex.Message);
return;
}
}
break;
case "top":
{
if (args.Length != 1 || string.IsNullOrEmpty(args[0]))
{
irc.SendReply(e.Data, "Usage: " + cmd + " #channel - get the top 5 suggestions for #channel");
return;
}
var req = new Request()
{
ApiAuth = Conf.ApiAuth,
Function = "channel_top",
ServerID = Conf.ServerID,
Channel = args[0],
};
try
{
var response = await WebClient.PostAsJsonAsync<Request>("api.php", req);
var resp = await response.Content.ReadAsStringAsync();
ResponseStatus res = await response.Content.ReadAsAsync<ResponseStatus>();
if (res.Success)
{
foreach (var line in SplitMessage(res.Message))
{
irc.SendReply(e.Data, line);
}
}
else
{
irc.SendReply(e.Data, "Failed to get Top 5: " + res.Message);
}
}
catch (Exception ex)
{
irc.SendReply(e.Data, "Failed sending reset request: " + ex.Message);
return;
}
}
break;
case "help":
irc.SendReply(e.Data, "ShowBot by lkalif:\n");
irc.SendReply(e.Data, " suggest #channel some title suggestion for channel");
irc.SendReply(e.Data, " top #channel - displays current top 5 on #channel");
irc.SendReply(e.Data, " reset #channel - delete all suggestion and votes for #channel");
irc.SendReply(e.Data, " delete #channel title - delete title from #channel");
irc.SendReply(e.Data, " reset requires you to be an operator of #channel");
irc.SendReply(e.Data, " help - Displays this text");
break;
default:
irc.SendReply(e.Data, "Unknown command. Try help");
break;
}
}
}
catch { }
}
void irc_OnJoin(object sender, JoinEventArgs e)
{
PrintMsg("IRC - " + Conf.ServerID, string.Format("{1} joined {0}", e.Channel, e.Who));
}
void irc_OnRawMessage(object sender, IrcEventArgs e)
{
if (e.Data.Type == ReceiveType.Unknown) return;
PrintMsg(e.Data.Nick, string.Format("({0}) {1}", e.Data.Type, e.Data.Message));
try
{
if (!string.IsNullOrEmpty(e.Data.Nick) &&
e.Data.Nick.ToLower() == "nickserv" &&
!SentNickServLogin && !string.IsNullOrEmpty(Conf.Password))
{
SentNickServLogin = true;
irc.SendMessage(SendType.Message, "nickserv", "identify " + Conf.Password);
}
}
catch { }
}
void PrintMsg(string from, string msg)
{
Console.WriteLine("{0}: {1}", from, msg);
}
public void Connect()
{
if (IRCConnection != null)
{
if (IRCConnection.IsAlive)
IRCConnection.Abort();
IRCConnection = null;
}
IRCConnection = new Thread(new ThreadStart(IrcThread));
IRCConnection.Name = "IRC Thread";
IRCConnection.IsBackground = true;
IRCConnection.Start();
}
private void IrcThread()
{
PrintMsg("IRC - " + Conf.ServerID, "Connecting...");
try
{
irc.Connect(Conf.ServerHost, Conf.ServerPort);
PrintMsg("System", "Logging in...");
if (string.IsNullOrEmpty(Conf.Password))
{
irc.Login(Conf.Nick, Conf.RealName, 0, Conf.Username);
}
else
{
irc.Login(Conf.Nick, Conf.RealName, 0, Conf.Username, Conf.Password);
}
foreach (var chan in Conf.Channels)
{
irc.RfcJoin(chan);
}
}
catch (Exception ex)
{
PrintMsg("System", "An error has occured: " + ex.Message);
}
try
{
irc.Listen();
if (irc.IsConnected)
{
irc.AutoReconnect = false;
irc.RfcDie();
irc.Disconnect();
}
}
catch { }
}
}
}