-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathIRCBot.cs
427 lines (401 loc) · 17 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
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCForge)
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.osedu.org/licenses/ECL-2.0
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using System.Text;
using Meebey.SmartIrc4net;
using System.Threading;
namespace MCForge
{
class IRCBot
{
static IrcClient irc = new IrcClient();
static string server = Server.ircServer;
static string channel = Server.ircChannel;
static string opchannel = Server.ircOpChannel;
static string nick = Server.ircNick;
static Thread ircThread;
static string[] names;
public IRCBot()
{
// The IRC Bot must run in a seperate thread, or else the server will freeze.
ircThread = new Thread(new ThreadStart(delegate
{
// Attach event handlers
irc.OnConnecting += new EventHandler(OnConnecting);
irc.OnConnected += new EventHandler(OnConnected);
irc.OnChannelMessage += new IrcEventHandler(OnChanMessage);
irc.OnJoin += new JoinEventHandler(OnJoin);
irc.OnPart += new PartEventHandler(OnPart);
irc.OnQuit += new QuitEventHandler(OnQuit);
irc.OnNickChange += new NickChangeEventHandler(OnNickChange);
irc.OnDisconnected += new EventHandler(OnDisconnected);
irc.OnQueryMessage += new IrcEventHandler(OnPrivMsg);
irc.OnNames += new NamesEventHandler(OnNames);
irc.OnChannelAction += new ActionEventHandler(OnAction);
// Attempt to connect to the IRC server
try { irc.Connect(server, Server.ircPort); }
catch (Exception ex) { Console.WriteLine("Unable to connect to IRC server: {0}", ex.Message); }
}));
ircThread.Start();
}
// While connecting
void OnConnecting(object sender, EventArgs e)
{
Server.s.Log("Connecting to IRC");
}
// When connected
void OnConnected(object sender, EventArgs e)
{
Server.s.Log("Connected to IRC");
irc.Login(nick, nick, 0, nick);
// Check to see if we want to register our bot with nickserv
if (Server.ircIdentify && Server.ircPassword != string.Empty)
{
Server.s.Log("Identifying with Nickserv");
irc.SendMessage(SendType.Message, "nickserv", "IDENTIFY " + Server.ircPassword);
}
Server.s.Log("Joining channels");
irc.RfcJoin(channel);
irc.RfcJoin(opchannel);
irc.Listen();
}
void OnNames(object sender, NamesEventArgs e)
{
names = e.UserList;
}
void OnDisconnected(object sender, EventArgs e)
{
try { irc.Connect(server, 6667); }
catch { Console.WriteLine("Failed to reconnect to IRC"); }
}
// On public channel message
void OnChanMessage(object sender, IrcEventArgs e)
{
string temp = e.Data.Message; string storedNick = e.Data.Nick;
string allowedchars = "1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./!@#$%^*()_+QWERTYUIOPASDFGHJKL:\"ZXCVBNM<>? ";
foreach (char ch in temp)
{
if (allowedchars.IndexOf(ch) == -1)
temp = temp.Replace(ch.ToString(), "*");
}
if (e.Data.Channel == opchannel)
{
Server.s.Log("[(Op) IRC] " + e.Data.Nick + ": " + temp);
Player.GlobalMessageOps(Server.IRCColour + "[(Op) IRC] " + storedNick + ": &f" + temp);
}
else
{
Server.s.Log("[IRC] " + e.Data.Nick + ": " + temp);
Player.GlobalChat(null, Server.IRCColour + "[IRC] " + storedNick + ": &f" + temp, false);
}
//if (temp.IndexOf(':') < temp.IndexOf(' ')) {
// storedNick = temp.Substring(0, temp.IndexOf(':'));
// temp = temp.Substring(temp.IndexOf(' ') + 1);
//}
//s.Log("IRC: " + e.Data.Nick + ": " + e.Data.Message);
//Player.GlobalMessage("IRC: &1" + e.Data.Nick + ": &f" + e.Data.Message);
}
// When someone joins the IRC
void OnJoin(object sender, JoinEventArgs e)
{
Server.s.Log(e.Data.Nick + " has joined channel " + e.Data.Channel);
if (e.Data.Channel == opchannel)
{
Player.GlobalChat(null, Server.IRCColour + e.Data.Nick + Server.DefaultColor + " has joined the operator channel", false);
}
else
{
Player.GlobalChat(null, Server.IRCColour + e.Data.Nick + Server.DefaultColor + " has joined the channel", false);
}
irc.RfcNames(channel);
irc.RfcNames(opchannel);
}
// When someone leaves the IRC
void OnPart(object sender, PartEventArgs e)
{
Server.s.Log(e.Data.Nick + " has left channel " + e.Data.Channel);
if (e.Data.Channel == opchannel)
{
Player.GlobalChat(null, Server.IRCColour + e.Data.Nick + Server.DefaultColor + " has left the operator channel", false);
}
else
{
Player.GlobalChat(null, Server.IRCColour + e.Data.Nick + Server.DefaultColor + " has left the channel", false);
}
irc.RfcNames(channel);
irc.RfcNames(opchannel);
}
void OnQuit(object sender, QuitEventArgs e)
{
Server.s.Log(e.Data.Nick + " has left IRC");
Player.GlobalChat(null, Server.IRCColour + e.Data.Nick + Server.DefaultColor + " has left IRC", false);
irc.RfcNames(channel);
irc.RfcNames(opchannel);
}
void OnPrivMsg(object sender, IrcEventArgs e)
{
Server.s.Log("IRC RECEIVING MESSAGE");
if (Server.ircControllers.Contains(e.Data.Nick))
{
string cmd;
string msg;
int len = e.Data.Message.Split(' ').Length;
cmd = e.Data.Message.Split(' ')[0];
if (len > 1)
{
msg = e.Data.Message.Substring(e.Data.Message.IndexOf(' ')).Trim();
}
else
{
msg = "";
}
if (msg != "" || cmd == "restart" || cmd == "update")
{
Server.s.Log(cmd + " : " + msg);
switch (cmd)
{
case "kick":
if (Player.Find(msg.Split()[0]) != null)
{
Command.all.Find("kick").Use(null, msg);
}
else
{
irc.SendMessage(SendType.Message, e.Data.Nick, "Player not found.");
}
break;
case "ban":
if (Player.Find(msg) != null)
{
Command.all.Find("ban").Use(null, msg);
}
else
{
irc.SendMessage(SendType.Message, e.Data.Nick, "Player not found.");
}
break;
case "banip":
if (Player.Find(msg) != null)
{
Command.all.Find("banip").Use(null, msg);
}
else
{
irc.SendMessage(SendType.Message, e.Data.Nick, "Player not found.");
}
break;
case "say":
irc.SendMessage(SendType.Message, channel, msg); break;
case "setrank":
if (Player.Find(msg.Split(' ')[0]) != null)
{
Command.all.Find("setrank").Use(null, msg);
}
else
{
irc.SendMessage(SendType.Message, e.Data.Nick, "Player not found.");
}
break;
case "promote":
if (Player.Find(msg.Split(' ')[0]) != null)
{
Command.all.Find("promote").Use(null, msg);
}
else
{
irc.SendMessage(SendType.Message, e.Data.Nick, "Player not found.");
}
break;
case "demote":
if (Player.Find(msg.Split(' ')[0]) != null)
{
Command.all.Find("demote").Use(null, msg);
}
else
{
irc.SendMessage(SendType.Message, e.Data.Nick, "Player not found.");
}
break;
case "mute":
if (Player.Find(msg) != null)
{
Command.all.Find("mute").Use(null, msg);
}
else
{
irc.SendMessage(SendType.Message, e.Data.Nick, "Player not found.");
}
break;
case "joker":
if (Player.Find(msg) != null)
{
Command.all.Find("joker").Use(null, msg);
}
else
{
irc.SendMessage(SendType.Message, e.Data.Nick, "Player not found.");
}
break;
case "physics":
if (Level.Find(msg.Split(' ')[0]) != null)
{
Command.all.Find("physics").Use(null, msg);
}
else
{
irc.SendMessage(SendType.Message, e.Data.Nick, "Map not found.");
}
break;
case "load":
if (Level.Find(msg.Split(' ')[0]) != null)
{
Command.all.Find("load").Use(null, msg);
}
else
{
irc.SendMessage(SendType.Message, e.Data.Nick, "Map not found.");
}
break;
case "unload":
if (Level.Find(msg) != null || msg == "empty")
{
Command.all.Find("unload").Use(null, msg);
}
else
{
irc.SendMessage(SendType.Message, e.Data.Nick, "Map not found.");
}
break;
case "save":
if (Level.Find(msg) != null)
{
Command.all.Find("save").Use(null, msg);
}
else
{
irc.SendMessage(SendType.Message, e.Data.Nick, "Map not found.");
}
break;
case "map":
if (Level.Find(msg.Split(' ')[0]) != null)
{
Command.all.Find("map").Use(null, msg);
}
else
{
irc.SendMessage(SendType.Message, e.Data.Nick, "Map not found.");
}
break;
case "restart":
Player.GlobalMessage("Restart initiated by " + e.Data.Nick);
IRCBot.Say("Restart initiated by " + e.Data.Nick);
Command.all.Find("restart").Use(null, "");
break;
case "update":
Player.GlobalMessage("Update check initiated by " + e.Data.Nick);
IRCBot.Say("Update check initiated by " + e.Data.Nick);
Command.all.Find("update").Use(null, "");
break;
default:
irc.SendMessage(SendType.Message, e.Data.Nick, "Invalid command."); break;
}
}
else
{
irc.SendMessage(SendType.Message, e.Data.Nick, "Invalid command format.");
}
}
}
void OnNickChange(object sender, NickChangeEventArgs e)
{
string key;
if (e.NewNickname.Split('|').Length == 2)
{
key = e.NewNickname.Split('|')[1];
if (key != null && key != "")
{
switch (key)
{
case "AFK":
Player.GlobalMessage("[IRC] " + Server.IRCColour + e.OldNickname + Server.DefaultColor + " is AFK"); Server.afkset.Add(e.OldNickname); break;
case "Away":
Player.GlobalMessage("[IRC] " + Server.IRCColour + e.OldNickname + Server.DefaultColor + " is Away"); Server.afkset.Add(e.OldNickname); break;
}
}
}
else if (Server.afkset.Contains(e.NewNickname))
{
Player.GlobalMessage("[IRC] " + Server.IRCColour + e.NewNickname + Server.DefaultColor + " is no longer away");
Server.afkset.Remove(e.NewNickname);
}
else
Player.GlobalMessage("[IRC] " + Server.IRCColour + e.OldNickname + Server.DefaultColor + " is now known as " + e.NewNickname);
irc.RfcNames(channel);
irc.RfcNames(opchannel);
}
void OnAction(object sender, ActionEventArgs e)
{
Player.GlobalMessage("* " + e.Data.Nick + " " + e.ActionMessage);
}
/// <summary>
/// A simple say method for use outside the bot class
/// </summary>
/// <param name="msg">what to send</param>
public static void Say(string msg, bool opchat = false)
{
if (irc != null && irc.IsConnected && Server.irc)
if (opchat == false)
{
irc.SendMessage(SendType.Message, channel, msg);
}
else
{
irc.SendMessage(SendType.Message, opchannel, msg);
}
}
public static bool IsConnected()
{
if (irc.IsConnected)
return true;
else
return false;
}
public static void Reset()
{
if (irc.IsConnected)
irc.Disconnect();
ircThread = new Thread(new ThreadStart(delegate
{
try { irc.Connect(server, Server.ircPort); }
catch (Exception e)
{
Server.s.Log("Error Connecting to IRC");
Server.s.Log(e.ToString());
}
}));
ircThread.Start();
}
public static string[] GetConnectedUsers()
{
return names;
}
public static void ShutDown()
{
irc.Disconnect();
ircThread.Abort();
}
}
}