~ircdotnet-dev/ircdotnet/devel

« back to all changes in this revision

Viewing changes to Samples/Common/BasicIrcBot.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:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using System.Linq;
 
4
using System.Text;
 
5
 
 
6
namespace IrcDotNet.Samples.Common
 
7
{
 
8
    // Provides access to basic commands for controlling an IRC bot.
 
9
    public abstract class BasicIrcBot : IrcBot
 
10
    {
 
11
        public BasicIrcBot()
 
12
            : base()
 
13
        {
 
14
        }
 
15
 
 
16
        public abstract IrcRegistrationInfo RegistrationInfo
 
17
        {
 
18
            get;
 
19
        }
 
20
 
 
21
        protected override void InitializeChatCommandProcessors()
 
22
        {
 
23
            this.ChatCommandProcessors.Add("help", ProcessChatCommandHelp);
 
24
        }
 
25
 
 
26
        #region Chat Command Processors
 
27
 
 
28
        private void ProcessChatCommandHelp(IrcClient client, IIrcMessageSource source,
 
29
            IList<IIrcMessageTarget> targets, string command, IList<string> parameters)
 
30
        {
 
31
            if (parameters.Count != 0)
 
32
                throw new InvalidCommandParametersException(1);
 
33
 
 
34
            // List all commands recognized by this bot.
 
35
            var replyTarget = GetDefaultReplyTarget(client, source, targets);
 
36
            client.LocalUser.SendMessage(replyTarget, "Commands recognized by bot:");
 
37
            client.LocalUser.SendMessage(replyTarget, string.Join(", ",
 
38
                this.ChatCommandProcessors.Select(kvPair => kvPair.Key)));
 
39
        }
 
40
 
 
41
        #endregion
 
42
 
 
43
        protected override void InitializeCommandProcessors()
 
44
        {
 
45
            this.CommandProcessors.Add("exit", ProcessCommandExit);
 
46
            this.CommandProcessors.Add("connect", ProcessCommandConnect);
 
47
            this.CommandProcessors.Add("c", ProcessCommandConnect);
 
48
            this.CommandProcessors.Add("disconnect", ProcessCommandDisconnect);
 
49
            this.CommandProcessors.Add("d", ProcessCommandDisconnect);
 
50
            this.CommandProcessors.Add("join", ProcessCommandJoin);
 
51
            this.CommandProcessors.Add("j", ProcessCommandJoin);
 
52
            this.CommandProcessors.Add("leave", ProcessCommandLeave);
 
53
            this.CommandProcessors.Add("l", ProcessCommandLeave);
 
54
            this.CommandProcessors.Add("list", ProcessCommandList);
 
55
        }
 
56
 
 
57
        #region Command Processors
 
58
 
 
59
        private void ProcessCommandExit(string command, IList<string> parameters)
 
60
        {
 
61
            Stop();
 
62
        }
 
63
 
 
64
        private void ProcessCommandConnect(string command, IList<string> parameters)
 
65
        {
 
66
            if (parameters.Count < 1)
 
67
                throw new ArgumentException(Properties.Resources.ErrorMessageNotEnoughArgs);
 
68
 
 
69
            Connect(parameters[0], this.RegistrationInfo);
 
70
        }
 
71
 
 
72
        private void ProcessCommandDisconnect(string command, IList<string> parameters)
 
73
        {
 
74
            if (parameters.Count < 1)
 
75
                throw new ArgumentException(Properties.Resources.ErrorMessageNotEnoughArgs);
 
76
 
 
77
            Disconnect(parameters[0]);
 
78
        }
 
79
 
 
80
        private void ProcessCommandJoin(string command, IList<string> parameters)
 
81
        {
 
82
            if (parameters.Count < 2)
 
83
                throw new ArgumentException(Properties.Resources.ErrorMessageNotEnoughArgs);
 
84
 
 
85
            // Join given channel on given server.
 
86
            var client = GetClientFromServerNameMask(parameters[0]);
 
87
            var channelName = parameters[1];
 
88
            client.Channels.Join(channelName);
 
89
        }
 
90
 
 
91
        private void ProcessCommandLeave(string command, IList<string> parameters)
 
92
        {
 
93
            if (parameters.Count < 2)
 
94
                throw new ArgumentException(Properties.Resources.ErrorMessageNotEnoughArgs);
 
95
 
 
96
            // Leave given channel on the given server.
 
97
            var client = GetClientFromServerNameMask(parameters[0]);
 
98
            var channelName = parameters[1];
 
99
            client.Channels.Leave(channelName);
 
100
        }
 
101
 
 
102
        private void ProcessCommandList(string command, IList<string> parameters)
 
103
        {
 
104
            // List all active server connections and channels of which local user is currently member.
 
105
            foreach (var client in this.Clients)
 
106
            {
 
107
                Console.Out.WriteLine("Server: {0}", client.ServerName ?? "(unknown)");
 
108
                foreach (var channel in client.Channels)
 
109
                {
 
110
                    if (channel.Users.Any(u => u.User == client.LocalUser))
 
111
                    {
 
112
                        Console.Out.WriteLine(" * {0}", channel.Name);
 
113
                    }
 
114
                }
 
115
            }
 
116
        }
 
117
 
 
118
        #endregion
 
119
    }
 
120
}