~ircdotnet-dev/ircdotnet/devel

« back to all changes in this revision

Viewing changes to Samples/Common/IrcBot.cs

  • Committer: Alex Regueiro
  • Date: 2010-08-27 22:03:06 UTC
  • Revision ID: alexreg@gmail.com-20100827220306-iqf7ahtgq3ryx2vr
TwitterBot sample is now working. Bot provides the ability for IRC users to log in to Twitter, log out, fetch recent tweets, and send a tweet, among other things. Can handle multiple IRC/Twitter users simultaneously.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 
9
9
namespace IrcDotNet.Samples.Common
10
10
{
 
11
    // Provides core functionality for an IRC bot that operates via multiple clients.
11
12
    public abstract class IrcBot : IDisposable
12
13
    {
13
14
        private const int clientQuitTimeout = 1000;
14
15
 
 
16
        private static readonly Regex commandSplitRegex = new Regex("(?<! /.*) ", RegexOptions.None);
 
17
 
15
18
        // Dictionary of all chat command processors, keyed by name.
16
19
        private IDictionary<string, ChatCommandProcessor> chatCommandProcessors;
17
20
 
203
206
            if (line.Length > 1 && line.StartsWith("."))
204
207
            {
205
208
                // Process command.
206
 
                var parts = line.Substring(1).Split(' ');
207
 
                var command = parts[0];
 
209
                var parts = commandSplitRegex.Split(line.Substring(1)).Select(p => p.TrimStart('/')).ToArray();
 
210
                var command = parts.First();
208
211
                var parameters = parts.Skip(1).ToArray();
209
212
                ReadChatCommand(client, eventArgs.Source, eventArgs.Targets, command, parameters);
210
213
                return true;
215
218
        private void ReadChatCommand(IrcClient client, IIrcMessageSource source, IList<IIrcMessageTarget> targets,
216
219
            string command, string[] parameters)
217
220
        {
 
221
            var defaultReplyTarget = GetDefaultReplyTarget(client, source, targets);
 
222
 
218
223
            ChatCommandProcessor processor;
219
224
            if (this.chatCommandProcessors.TryGetValue(command, out processor))
220
225
            {
222
227
                {
223
228
                    processor(client, source, targets, command, parameters);
224
229
                }
 
230
                catch (InvalidCommandParametersException exInvalidCommandParameters)
 
231
                {
 
232
                    client.LocalUser.SendNotice(defaultReplyTarget,
 
233
                        exInvalidCommandParameters.GetMessage(command));
 
234
                }
225
235
                catch (Exception ex)
226
236
                {
227
237
                    if (source is IIrcMessageTarget)
228
238
                    {
229
 
                        client.LocalUser.SendNotice((IIrcMessageTarget)source, string.Format(
230
 
                            "Error processing '{0}' command: {1}", command, ex.Message));
 
239
                        client.LocalUser.SendNotice(defaultReplyTarget,
 
240
                            "Error processing '{0}' command: {1}", command, ex.Message);
231
241
                    }
232
242
                }
233
243
            }
235
245
            {
236
246
                if (source is IIrcMessageTarget)
237
247
                {
238
 
                    client.LocalUser.SendNotice((IIrcMessageTarget)source, "Command '{0}' not recognized.", command);
 
248
                    client.LocalUser.SendNotice(defaultReplyTarget, "Command '{0}' not recognized.", command);
239
249
                }
240
250
            }
241
251
        }
254
264
 
255
265
        protected abstract void OnLocalUserMessageReceived(IrcLocalUser localUser, IrcMessageEventArgs e);
256
266
 
 
267
        protected abstract void OnChannelUserJoined(IrcChannel channel, IrcChannelUserEventArgs e);
 
268
 
 
269
        protected abstract void OnChannelUserLeft(IrcChannel channel, IrcChannelUserEventArgs e);
 
270
 
257
271
        protected abstract void OnChannelNoticeReceived(IrcChannel channel, IrcMessageEventArgs e);
258
272
 
259
273
        protected abstract void OnChannelMessageReceived(IrcChannel channel, IrcMessageEventArgs e);
283
297
            client.LocalUser.JoinedChannel += IrcClient_LocalUser_JoinedChannel;
284
298
            client.LocalUser.LeftChannel += IrcClient_LocalUser_LeftChannel;
285
299
 
 
300
            Console.Beep();
 
301
 
286
302
            OnClientRegistered(client);
287
303
        }
288
304
 
311
327
        {
312
328
            var localUser = (IrcLocalUser)sender;
313
329
 
 
330
            e.Channel.UserJoined += IrcClient_Channel_UserJoined;
 
331
            e.Channel.UserLeft += IrcClient_Channel_UserLeft;
314
332
            e.Channel.MessageReceived += IrcClient_Channel_MessageReceived;
315
333
            e.Channel.NoticeReceived += IrcClient_Channel_NoticeReceived;
316
334
 
321
339
        {
322
340
            var localUser = (IrcLocalUser)sender;
323
341
 
 
342
            e.Channel.UserJoined -= IrcClient_Channel_UserJoined;
 
343
            e.Channel.UserLeft -= IrcClient_Channel_UserLeft;
324
344
            e.Channel.MessageReceived -= IrcClient_Channel_MessageReceived;
325
345
            e.Channel.NoticeReceived -= IrcClient_Channel_NoticeReceived;
326
346
 
327
347
            OnLocalUserJoinedChannel(localUser, e);
328
348
        }
329
349
 
 
350
        private void IrcClient_Channel_UserLeft(object sender, IrcChannelUserEventArgs e)
 
351
        {
 
352
            var channel = (IrcChannel)sender;
 
353
 
 
354
            OnChannelUserJoined(channel, e);
 
355
        }
 
356
 
 
357
        private void IrcClient_Channel_UserJoined(object sender, IrcChannelUserEventArgs e)
 
358
        {
 
359
            var channel = (IrcChannel)sender;
 
360
 
 
361
            OnChannelUserLeft(channel, e);
 
362
        }
 
363
 
330
364
        private void IrcClient_Channel_NoticeReceived(object sender, IrcMessageEventArgs e)
331
365
        {
332
366
            var channel = (IrcChannel)sender;