-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
314 lines (289 loc) · 11.8 KB
/
Program.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
#region Used Namespaces
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using Squishy.Irc;
using Squishy.Irc.Account;
using Squishy.Irc.Commands;
using Squishy.Irc.Protocol;
using Squishy.Network;
#endregion
namespace DarkwaterSupportBot
{
public class DarkWaterBot : IrcClient
{
#region MainExecution
#region Fields
#region Lists
public static readonly List<string> ChannelList = new List<string> {"#woc","#dogfighter","#dogfighterbeta"};
#endregion
#region IRC Connection info
public static int SendQueue
{
get { return ThrottledSendQueue.CharsPerSecond; }
set { ThrottledSendQueue.CharsPerSecond = value; }
}
private const int Port = 6667;
public static readonly DarkWaterBot Irc = new DarkWaterBot
{
Nicks = new[] {"DFBot","DFBot_","DFHelper"},
UserName = "DFBot",
Info = "Dogfighter Helper",
_network = Dns.GetHostAddresses("irc.quakenet.org")
};
public static System.Timers.Timer SpamTimer = new System.Timers.Timer();
private IPAddress[] _network;
#endregion
#region Other
public static string LogFile;
public static string ReplyChan = "#woc";
public static readonly Stopwatch Runtimer = new Stopwatch();
public static Process Utility = Process.GetCurrentProcess();
public static bool DisplayIrcPackets = false;
#endregion
#endregion Fields
public static void Main()
{
Irc.ProtocolHandler.PacketReceived += OnReceive;
try
{
SpamTimer.Interval = 5000;
SpamTimer.Elapsed += SpamTimer_Elapsed;
Console.ForegroundColor = ConsoleColor.Green;
#region IRC Connecting
SendQueue = 80;
Irc.Client.Connecting += OnConnecting;
Irc.Client.Connected += Client_Connected;
Irc.BeginConnect(Irc._network[0].ToString(), Port);
#endregion
Runtimer.Start();
System.Windows.Forms.Application.Run();
}
#region Main Exception Handling
catch (Exception e)
{
UtilityMethods.Print(string.Format("Exception {0} \n {1}", e.Message, e.StackTrace), true);
WriteErrorSystem.WriteError(new List<string> {"Exception:", e.Message, e.StackTrace});
foreach (var chan in ChannelList)
{
Irc.CommandHandler.Msg(chan, "Main Execution error, shutting down!");
}
Console.WriteLine("Closing in 5 seconds");
Thread.Sleep(5000);
Environment.Exit(0);
}
#endregion
}
static void SpamTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
SpamTimer.Stop();
}
protected static void OnReceive(IrcPacket packet)
{
if(DisplayIrcPackets)
Console.WriteLine("<-- " + packet);
}
protected override void OnUserEncountered(IrcUser user)
{
if (user == null || user.Nick == null)
{
return;
}
using (var db = new MessagesContainer())
{
foreach (var message in db.Messages.Where(message => message.IrcNick.ToLower() == user.Nick.ToLower()))
{
CommandHandler.Msg(user.ToString(), "Date Left: " + message.DateLeft + "\nFrom Nick: " + message.FromIrcNick + "\nMessage Sent: " + message.MessageText);
db.Messages.DeleteObject(message);
db.SaveChanges();
}
}
}
protected override void OnBeforeSend(string text)
{
if(DisplayIrcPackets)
Console.WriteLine("--> " + text);
}
public static void Client_Connected(Connection con)
{
UtilityMethods.Print("Connected to IRC Server", true);
}
public static void Irc_Disconnected(IrcClient arg1, bool arg2)
{
try
{
UtilityMethods.Print("Disconnected from IRC server, Attempting reconnect in 5 seconds", true);
Thread.Sleep(5000);
Irc.BeginConnect(Irc._network[0].ToString(), Port);
}
catch (Exception e)
{
UtilityMethods.Print(e.Data + e.StackTrace,true);
}
}
#region IrcSystem
public static void OnConnecting(Connection con)
{
UtilityMethods.Print("Connecting to IRC server", true);
UtilityMethods.Print(DateTime.Now + " : Connecting to server");
}
protected override void Perform()
{
try
{
IrcCommandHandler.Initialize(); // This is what parses all coded commands
CommandHandler.RemoteCommandPrefix = "!";
Irc.CommandHandler.Mode(Me.Nick + " +x");
Irc.CommandHandler.Msg("[email protected]","auth DFBot mehlolkek1");
foreach (var chan in ChannelList)
{
if (chan.Contains(","))
{
var chaninfo = chan.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (chaninfo.Length > 1)
CommandHandler.Join(chaninfo[0], chaninfo[1]);
else
CommandHandler.Join(chaninfo[0]);
}
else
{
CommandHandler.Join(chan);
}
}
}
catch (Exception e)
{
UtilityMethods.Print(e.Message + e.Data + e.StackTrace, true);
}
}
protected override void OnUnknownCommandUsed(CmdTrigger trigger)
{
if (string.IsNullOrEmpty(trigger.Alias))
{
trigger.Reply("You entered a null query for a command, please try again or use !help");
return;
}
HelpCommand cmd = HelpCommandsManager.Search(trigger.Alias.ToLower());
if(trigger.User.AccountLevel == AccountMgr.AccountLevel.User || trigger.User.AccountLevel == AccountMgr.AccountLevel.Admin)
trigger.Reply(cmd != null
? cmd.HelpText
: "This command does not exist, try !help or suggest the creation of the command");
else
trigger.User.Msg(cmd != null
? cmd.HelpText
: "This command does not exist, try !help or suggest the creation of the command");
}
protected override void OnQueryMsg(IrcUser user, StringStream text)
{
UtilityMethods.Print(user + text.String, true);
}
protected override void OnText(IrcUser user, IrcChannel chan, StringStream text)
{
try
{
if (text.String.ToLower().Contains("badger"))
{
Random rand = new Random();
var randomint = rand.Next(0, 100);
if(randomint > 50)
{
chan.Msg("Ahhhh Snakkeeee its a snaakee!!!! http://www.youtube.com/watch?v=EIyixC9NsLI");
}
else
{
chan.Msg("Mushroom Mushroom Mushroom! http://www.youtube.com/watch?v=EIyixC9NsLI");
}
}
else
{
if (text.String.ToLower().Contains("snake"))
{
chan.Msg("Badger...Badger...Badger...Badger.. http://www.youtube.com/watch?v=EIyixC9NsLI");
}
else
{
if (text.String.ToLower().Contains("mushroom"))
{
chan.Msg("Badger..Badger..Badger! http://www.youtube.com/watch?v=EIyixC9NsLI");
}
}
}
if (text.String.Contains("ACTION") && text.String.ToLower().Contains("help") && text.String.ToLower().Contains("bot"))
{
if (chan != null)
Irc.CommandHandler.Describe(chan, FunCommands.ReactToAction(), chan.Args);
else
Irc.CommandHandler.Describe(user, FunCommands.ReactToAction(), user.Args);
}
#region MessagesSent
if(chan != null)
UtilityMethods.Print(string.Format("User {0} on channel {1} Sent {2}", user, chan, text), true);
#endregion
}
catch (Exception e)
{
CommandHandler.Msg("#woc", e.Message);
UtilityMethods.Print(e.StackTrace + e.Message, true);
}
}
public override bool MayTriggerCommand(CmdTrigger trigger, Command cmd)
{
try
{
if (base.MayTriggerCommand(trigger, cmd))
{
if (trigger.Channel != null && trigger.Target == trigger.Channel && !trigger.Args.String.ToLower().StartsWith(CommandHandler.RemoteCommandPrefix + "help"))
{
if (!SpamTimer.Enabled)
{
SpamTimer.Start();
return true;
}
else
{
trigger.User.Msg("Don't try to make me spam!");
return false;
}
}
else
{
return true;
}
}
return false;
}
catch (Exception e)
{
Console.WriteLine(DateTime.Now.ToString() + ":" + e.ToString());
return false;
}
/*
try
{
if (File.Exists("auth.txt") && cmd.Name.ToLower() != "addauth")
{
using (var reader = new StreamReader("auth.txt"))
{
var usernames = new List<string>();
while (!reader.EndOfStream)
{
usernames.Add(reader.ReadLine());
}
if (usernames.Any(username => username == trigger.User.AuthName))
{
return true;
}
}
}
return trigger.User.IsOn("#wcell.dev") || trigger.User.IsOn("#woc") || trigger.User.IsOn("#wcell");*/
//TODO: Update to add a proper suited auth system.);
//return true;
}
#endregion
#endregion
}
}