-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotBase.cs
More file actions
330 lines (274 loc) · 10.5 KB
/
Copy pathBotBase.cs
File metadata and controls
330 lines (274 loc) · 10.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
#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using IrcDotNet;
namespace trurl;
// adapted from the multiclient framework in IrcDotNet samples.
abstract partial class BotBase
{
private const int clientQuitTimeout = 1000;
// Regex for splitting space-separated list of command parts until first parameter that begins with '/'.
private static readonly Regex commandPartsSplitRegex = CommandPartsSplit();
[GeneratedRegex("(?<! /.*) ", RegexOptions.None)]
private static partial Regex CommandPartsSplit();
private readonly bool ignoreEOF;
protected readonly IReadOnlyDictionary<string, Command> commands;
private bool isRunning;
private IrcClient client;
public BotBase(bool ignoreEOF)
{
this.ignoreEOF = ignoreEOF;
isRunning = false;
commands = InitializeCommands().ToDictionary(cmd => cmd.Name, cmd => cmd, StringComparer.CurrentCultureIgnoreCase);
}
public virtual string QuitMessage => "Bot exited.";
public void Run(string server, IrcRegistrationInfo registrationInfo, IEnumerable<string> channels)
{
isRunning = true;
client = Connect(server, registrationInfo);
client.Registered += (s, e) =>
{
foreach (var channel in channels)
{
Join(channel);
}
};
// Read commands from stdin until bot terminates.
while (isRunning)
{
Console.WriteLine("bot running - type 'exit' or send !quit in irc to exit");
var line = Console.ReadLine();
if (line == null)
{
Console.WriteLine("EOF");
if (ignoreEOF)
{
Thread.Sleep(Timeout.Infinite);
}
else
{
break;
}
}
if (line.Length == 0)
{
continue;
}
if (line.Equals("exit"))
{
isRunning = false;
}
else
{
Console.WriteLine("unrecognised command (use 'exit' to quit)");
}
}
Disconnect(client);
}
public void Stop()
{
isRunning = false;
Disconnect(client);
Environment.Exit(0);
}
private IrcClient Connect(string server, IrcRegistrationInfo registrationInfo)
{
// Create new IRC client and connect to given server.
var client = new StandardIrcClient();
client.FloodPreventer = new IrcStandardFloodPreventer(4, 1000);
client.Connected += IrcClient_Connected;
client.Disconnected += IrcClient_Disconnected;
client.Registered += IrcClient_Registered;
// Wait until connection has succeeded or timed out.
using (var connectedEvent = new ManualResetEventSlim(false))
{
client.Connected += (sender2, e2) => connectedEvent.Set();
client.Connect(server, false, registrationInfo);
if (!connectedEvent.Wait(10000))
{
client.Dispose();
Console.WriteLine("Connection to '{0}' timed out.", server);
return null;
}
}
Console.WriteLine("Now connected to '{0}'.", server);
// Add new client to collection.
return client;
}
private void Disconnect(IrcClient client)
{
var serverName = client.ServerName;
client.Quit(clientQuitTimeout, this.QuitMessage);
client.Dispose();
Console.WriteLine("Disconnected from '{0}'.", serverName);
}
protected void Join(string channel)
{
client.Channels.Join(channel);
Console.WriteLine("Joined '{0}'.", channel);
}
protected void Leave(string channel)
{
client.Channels.Leave(channel);
Console.WriteLine("Left '{0}'.", channel);
}
private bool ReadChatCommand(IrcClient client, IrcMessageEventArgs eventArgs)
{
// Check if given message represents chat command.
var line = eventArgs.Text;
if (line.Length > 1 && line.StartsWith("!"))
{
// Process command.
var parts = commandPartsSplitRegex.Split(line.Substring(1)).Select(p => p.TrimStart('/')).ToArray();
var command = parts.First();
var parameters = parts.Skip(1).ToArray();
ReadChatCommand(client, eventArgs.Source, eventArgs.Targets, command, parameters);
return true;
}
return false;
}
private void ReadChatCommand(IrcClient client, IIrcMessageSource source, IList<IIrcMessageTarget> targets, string name, string[] parameters)
{
var defaultReplyTarget = GetDefaultReplyTarget(client, source, targets);
if (commands.TryGetValue(name, out var command))
{
try
{
command.Processor(client, source, targets, name, parameters);
}
catch (BotException be)
{
client.LocalUser.SendNotice(defaultReplyTarget, be.GetMessage(name));
}
catch (Exception ex)
{
if (source is IIrcMessageTarget)
{
client.LocalUser.SendNotice(defaultReplyTarget, $"Error processing '{name}' command: {ex.Message}");
}
Console.WriteLine(ex.ToString());
}
}
else
{
if (source is IIrcMessageTarget && defaultReplyTarget.All(t => !t.Name.StartsWith("#")))
{
client.LocalUser.SendNotice(defaultReplyTarget, $"Command '{name}' not recognized.");
}
}
}
protected IList<IIrcMessageTarget> GetDefaultReplyTarget(IrcClient client, IIrcMessageSource source, IList<IIrcMessageTarget> targets)
{
if (targets.Contains(client.LocalUser) && source is IIrcMessageTarget)
{
return [(IIrcMessageTarget)source];
}
else
{
return targets;
}
}
#region IRC Client Event Handlers
private void IrcClient_Connected(object sender, EventArgs e)
{
var client = (IrcClient)sender;
OnClientConnect(client);
}
private void IrcClient_Disconnected(object sender, EventArgs e)
{
var client = (IrcClient)sender;
OnClientDisconnect(client);
}
private void IrcClient_Registered(object sender, EventArgs e)
{
var client = (IrcClient)sender;
client.LocalUser.NoticeReceived += IrcClient_LocalUser_NoticeReceived;
client.LocalUser.MessageReceived += IrcClient_LocalUser_MessageReceived;
client.LocalUser.JoinedChannel += IrcClient_LocalUser_JoinedChannel;
client.LocalUser.LeftChannel += IrcClient_LocalUser_LeftChannel;
Console.Beep();
OnClientRegistered(client);
}
private void IrcClient_LocalUser_NoticeReceived(object sender, IrcMessageEventArgs e)
{
var localUser = (IrcLocalUser)sender;
OnLocalUserNoticeReceived(localUser, e);
}
private void IrcClient_LocalUser_MessageReceived(object sender, IrcMessageEventArgs e)
{
var localUser = (IrcLocalUser)sender;
if (e.Source is IrcUser)
{
// Read message and process if it is chat command.
if (ReadChatCommand(localUser.Client, e))
{
return;
}
}
OnLocalUserMessageReceived(localUser, e);
}
private void IrcClient_LocalUser_JoinedChannel(object sender, IrcChannelEventArgs e)
{
var localUser = (IrcLocalUser)sender;
e.Channel.UserJoined += IrcClient_Channel_UserJoined;
e.Channel.UserLeft += IrcClient_Channel_UserLeft;
e.Channel.MessageReceived += IrcClient_Channel_MessageReceived;
e.Channel.NoticeReceived += IrcClient_Channel_NoticeReceived;
OnLocalUserJoinedChannel(localUser, e);
}
private void IrcClient_LocalUser_LeftChannel(object sender, IrcChannelEventArgs e)
{
var localUser = (IrcLocalUser)sender;
e.Channel.UserJoined -= IrcClient_Channel_UserJoined;
e.Channel.UserLeft -= IrcClient_Channel_UserLeft;
e.Channel.MessageReceived -= IrcClient_Channel_MessageReceived;
e.Channel.NoticeReceived -= IrcClient_Channel_NoticeReceived;
OnLocalUserJoinedChannel(localUser, e);
}
private void IrcClient_Channel_UserLeft(object sender, IrcChannelUserEventArgs e)
{
var channel = (IrcChannel)sender;
OnChannelUserJoined(channel, e);
}
private void IrcClient_Channel_UserJoined(object sender, IrcChannelUserEventArgs e)
{
var channel = (IrcChannel)sender;
OnChannelUserLeft(channel, e);
}
private void IrcClient_Channel_NoticeReceived(object sender, IrcMessageEventArgs e)
{
var channel = (IrcChannel)sender;
OnChannelNoticeReceived(channel, e);
}
private void IrcClient_Channel_MessageReceived(object sender, IrcMessageEventArgs e)
{
var channel = (IrcChannel)sender;
if (e.Source is IrcUser)
{
// Read message and process if it is chat command.
if (ReadChatCommand(channel.Client, e))
{
return;
}
}
OnChannelMessageReceived(channel, e);
}
#endregion
#region "Subclass API"
protected abstract IEnumerable<Command> InitializeCommands();
protected virtual void OnClientConnect(IrcClient client) { }
protected virtual void OnClientDisconnect(IrcClient client) { }
protected virtual void OnClientRegistered(IrcClient client) { }
protected virtual void OnLocalUserJoinedChannel(IrcLocalUser localUser, IrcChannelEventArgs e) { }
protected virtual void OnLocalUserLeftChannel(IrcLocalUser localUser, IrcChannelEventArgs e) { }
protected virtual void OnLocalUserNoticeReceived(IrcLocalUser localUser, IrcMessageEventArgs e) { }
protected virtual void OnLocalUserMessageReceived(IrcLocalUser localUser, IrcMessageEventArgs e) { }
protected virtual void OnChannelUserJoined(IrcChannel channel, IrcChannelUserEventArgs e) { }
protected virtual void OnChannelUserLeft(IrcChannel channel, IrcChannelUserEventArgs e) { }
protected virtual void OnChannelNoticeReceived(IrcChannel channel, IrcMessageEventArgs e) { }
protected virtual void OnChannelMessageReceived(IrcChannel channel, IrcMessageEventArgs e) { }
#endregion
}