-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForgeBot.cs
306 lines (281 loc) · 12.8 KB
/
ForgeBot.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
/*
Copyright 2011 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.opensource.org/licenses/ecl2.php
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.IO;
using System.Collections.Generic;
using Sharkbite.Irc;
using System.Text;
//using System.Threading;
namespace MCForge
{
public class ForgeBot
{
public static readonly string ColorSignal = "\x03";
public static readonly string ResetSignal = "\x0F";
private Connection connection;
private List<string> banCmd;
private string channel, opchannel;
private string nick;
private string server;
private bool reset = false;
private byte retries = 0;
public string usedCmd = "";
public ForgeBot(string channel, string opchannel, string nick, string server)
{
/*if (!File.Exists("Sharkbite.Thresher.dll"))
{
Server.irc = false;
Server.s.Log("[IRC] The IRC dll was not found!");
return;
}*/
this.channel = channel.Trim(); this.opchannel = opchannel.Trim(); this.nick = nick.Replace(" ", ""); this.server = server;
ConnectionArgs con = new ConnectionArgs(nick, server);
con.Port = Server.ircPort;
connection = new Connection(con, false, false);
banCmd = new List<string>();
if (Server.irc)
{
// Regster events for outgoing
Player.PlayerChat += new Player.OnPlayerChat(Player_PlayerChat);
Player.PlayerConnect += new Player.OnPlayerConnect(Player_PlayerConnect);
Player.PlayerDisconnect += new Player.OnPlayerDisconnect(Player_PlayerDisconnect);
// Regster events for incoming
connection.Listener.OnNick += new NickEventHandler(Listener_OnNick);
connection.Listener.OnRegistered += new RegisteredEventHandler(Listener_OnRegistered);
connection.Listener.OnPublic += new PublicMessageEventHandler(Listener_OnPublic);
connection.Listener.OnPrivate += new PrivateMessageEventHandler(Listener_OnPrivate);
connection.Listener.OnError += new ErrorMessageEventHandler(Listener_OnError);
connection.Listener.OnQuit += new QuitEventHandler(Listener_OnQuit);
connection.Listener.OnJoin += new JoinEventHandler(Listener_OnJoin);
connection.Listener.OnPart += new PartEventHandler(Listener_OnPart);
connection.Listener.OnDisconnected += new DisconnectedEventHandler(Listener_OnDisconnected);
// Load banned commands list
if (File.Exists("text/ircbancmd.txt")) // Backwards compatibility
{
using (StreamWriter sw = File.CreateText("text/irccmdblacklist.txt"))
{
sw.WriteLine("#Here you can put commands that cannot be used from the IRC bot.");
sw.WriteLine("#Lines starting with \"#\" are ignored.");
foreach (string line in File.ReadAllLines("text/ircbancmd.txt"))
sw.WriteLine(line);
}
File.Delete("text/ircbancmd.txt");
}
else
{
if (!File.Exists("text/irccmdblacklist.txt")) File.WriteAllLines("text/irccmdblacklist.txt", new String[] { "#Here you can put commands that cannot be used from the IRC bot.", "#Lines starting with \"#\" are ignored." });
foreach (string line in File.ReadAllLines("text/irccmdblacklist.txt"))
if (line[0] != '#') banCmd.Add(line);
}
}
}
public void Say(string message, bool opchat = false, bool color = true)
{
if (!Server.irc || !IsConnected()) return;
StringBuilder sb = new StringBuilder(message);
if (color)
{
for (int i = 0; i < 10; i++)
{
sb.Replace("%" + i, ColorSignal + c.MCtoIRC("&" + i));
sb.Replace("&" + i, ColorSignal + c.MCtoIRC("&" + i));
}
for (char ch = 'a'; ch <= 'f'; ch++)
{
sb.Replace("%" + ch, ColorSignal + c.MCtoIRC("&" + ch));
sb.Replace("&" + ch, ColorSignal + c.MCtoIRC("&" + ch));
}
}
connection.Sender.PublicMessage(opchat ? opchannel : channel, sb.ToString());
}
public void Pm(string user, string message)
{
if (Server.irc && IsConnected())
connection.Sender.PrivateMessage(user, message);
}
public void Reset()
{
if (!Server.irc) return;
reset = true;
retries = 0;
Disconnect("IRC Bot resetting...");
Connect();
}
void Listener_OnJoin(UserInfo user, string channel)
{
doJoinLeaveMessage(user.Nick, "joined", channel);
}
void Listener_OnPart(UserInfo user, string channel, string reason)
{
if (user.Nick == nick) return;
doJoinLeaveMessage(user.Nick, "left", channel);
}
private void doJoinLeaveMessage(string who, string verb, string channel)
{
Server.s.Log(String.Format("{0} has {1} channel {2}", who, verb, channel));
Player.GlobalMessage(String.Format("{0}[IRC] {1} has {2} the{3} channel", Server.IRCColour, who, verb, (channel == opchannel ? " operator" : "")));
}
void Player_PlayerDisconnect(Player p, string reason)
{
if (Server.irc && IsConnected())
if (Server.guestLeaveNotify == false && p.group.Permission <= LevelPermission.Guest)
{
return;
}
connection.Sender.PublicMessage(channel, p.name + " left the game (" + reason + ")");
}
void Player_PlayerConnect(Player p)
{
if (Server.irc && IsConnected())
if (Server.guestJoinNotify == false && p.group.Permission <= LevelPermission.Guest)
{
return;
}
connection.Sender.PublicMessage(channel, p.name + " joined the game");
}
void Listener_OnQuit(UserInfo user, string reason)
{
if (user.Nick == nick) return;
Server.s.Log(user.Nick + " has left IRC");
Player.GlobalMessage(Server.IRCColour + user.Nick + Server.DefaultColor + " has left IRC");
}
void Listener_OnError(ReplyCode code, string message)
{
Server.s.Log("IRC Error: " + message);
}
void Listener_OnPrivate(UserInfo user, string message)
{
if (!Server.ircControllers.Contains(user.Nick)) { Pm(user.Nick, "You are not an IRC controller!"); return; }
if (message.Split(' ')[0] == "resetbot" || banCmd.Contains(message.Split(' ')[0])) { Pm(user.Nick, "You cannot use this command from IRC!"); return; }
if (Player.CommandHasBadColourCodes(null, message)) { Pm(user.Nick, "Your command had invalid color codes!"); return; }
Command cmd = Command.all.Find(message.Split(' ')[0]);
if (cmd != null)
{
Server.s.Log("IRC Command: /" + message);
usedCmd = user.Nick;
try { cmd.Use(null, message.Split(' ').Length > 1 ? message.Substring(message.IndexOf(' ')).Trim() : ""); }
catch { Pm(user.Nick, "Failed command!"); }
usedCmd = "";
}
else
Pm(user.Nick, "Unknown command!");
}
void Listener_OnPublic(UserInfo user, string channel, string message)
{
//string allowedchars = "1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./!@#$%^*()_+QWERTYUIOPASDFGHJKL:\"ZXCVBNM<>? ";
// Allowed chars are any ASCII char between 20h/32 and 7Ah/122 inclusive, except for 26h/38 (&) and 60h/96 (`)
for (byte i = 10; i < 16; i++)
message = message.Replace(ColorSignal + i, c.IRCtoMC(i).Replace('&', '%'));
for (byte i = 0; i < 10; i++)
message = message.Replace(ColorSignal + i, c.IRCtoMC(i).Replace('&', '%'));
message = message.MCCharFilter();
if (Player.MessageHasBadColorCodes(null, message))
return;
if (channel == opchannel)
{
Server.s.Log(String.Format("(OPs): [IRC] {0}: {1}", user.Nick, message));
Player.GlobalMessageOps(String.Format("To Ops &f-{0}[IRC] {1}&f- {2}", Server.IRCColour, user.Nick, Server.profanityFilter ? ProfanityFilter.Parse(message) : message));
}
else
{
Server.s.Log(String.Format("[IRC] {0}: {1}", user.Nick, message));
Player.GlobalMessage(String.Format("{0}[IRC] {1}: &f{2}", Server.IRCColour, user.Nick, Server.profanityFilter ? ProfanityFilter.Parse(message) : message));
}
}
void Listener_OnRegistered()
{
Server.s.Log("Connected to IRC!");
reset = false;
retries = 0;
if (Server.ircIdentify && Server.ircPassword != "")
{
Server.s.Log("Identifying with NickServ");
connection.Sender.PrivateMessage("nickserv", "IDENTIFY " + Server.ircPassword);
}
Server.s.Log("Joining channels...");
if (!String.IsNullOrEmpty(channel))
connection.Sender.Join(channel);
if (!String.IsNullOrEmpty(opchannel))
connection.Sender.Join(opchannel);
}
void Listener_OnDisconnected()
{
if (!reset && retries < 3) { retries++; Connect(); }
}
void Listener_OnNick(UserInfo user, string newNick)
{
//Player.GlobalMessage(Server.IRCColour + "[IRC] " + user.Nick + " changed nick to " + newNick);
string key;
if (newNick.Split('|').Length == 2)
{
key = newNick.Split('|')[1];
if (key != null && key != "")
{
switch (key)
{
case "AFK":
Player.GlobalMessage("[IRC] " + Server.IRCColour + user.Nick + Server.DefaultColor + " is AFK"); Server.ircafkset.Add(user.Nick); break;
case "Away":
Player.GlobalMessage("[IRC] " + Server.IRCColour + user.Nick + Server.DefaultColor + " is Away"); Server.ircafkset.Add(user.Nick); break;
}
}
}
else if (Server.ircafkset.Contains(newNick))
{
Player.GlobalMessage("[IRC] " + Server.IRCColour + newNick + Server.DefaultColor + " is back");
Server.ircafkset.Remove(newNick);
}
else
Player.GlobalMessage("[IRC] " + Server.IRCColour + user.Nick + Server.DefaultColor + " is now known as " + newNick);
}
void Player_PlayerChat(Player p, string message)
{
if (Server.ircColorsEnable == true && Server.irc && IsConnected())
Say(p.color + p.prefix + p.name + ": &0" + message, p.opchat);
if (Server.ircColorsEnable == false && Server.irc && IsConnected())
Say(p.name + ": " + message, p.opchat);
}
public void Connect()
{
if (!Server.irc || Server.shuttingDown) return;
/*new Thread(new ThreadStart(delegate
{
try { connection.Connect(); }
catch (Exception e)
{
Server.s.Log("Failed to connect to IRC");
Server.ErrorLog(e);
}
})).Start();*/
Server.s.Log("Connecting to IRC...");
try { connection.Connect(); }
catch (Exception e)
{
Server.s.Log("Failed to connect to IRC!");
Server.ErrorLog(e);
}
}
public void Disconnect(string reason)
{
if (Server.irc && IsConnected()) { connection.Disconnect(reason); Server.s.Log("Disconnected from IRC!"); }
}
public bool IsConnected()
{
if (!Server.irc) return false;
try { return connection.Connected; }
catch { return false; }
}
}
}