efb9d60b01cc047192e7b329204331134ad54fcc
[reactos.git] / irc / TechBot / TechBot.Library / IrcService.cs
1 using System;
2 using System.Collections;
3 using System.Threading;
4 using TechBot.IRCLibrary;
5
6 namespace TechBot.Library
7 {
8 public class IrcService : IServiceOutput
9 {
10 private string hostname;
11 private int port;
12 private string channelnames;
13 private string botname;
14 private string chmPath;
15 private string mainChm;
16 private string ntstatusXml;
17 private string winerrorXml;
18 private string hresultXml;
19 private string svnCommand;
20 private IrcClient client;
21 private ArrayList channels = new ArrayList(); /* IrcChannel */
22 private TechBotService service;
23 private bool isStopped = false;
24
25 public IrcService(string hostname,
26 int port,
27 string channelnames,
28 string botname,
29 string chmPath,
30 string mainChm,
31 string ntstatusXml,
32 string winerrorXml,
33 string hresultXml,
34 string svnCommand)
35 {
36 this.hostname = hostname;
37 this.port = port;
38 this.channelnames = channelnames;
39 this.botname = botname;
40 this.chmPath = chmPath;
41 this.mainChm = mainChm;
42 this.ntstatusXml = ntstatusXml;
43 this.winerrorXml = winerrorXml;
44 this.hresultXml = hresultXml;
45 this.svnCommand = svnCommand;
46 }
47
48 public void Run()
49 {
50 service = new TechBotService(this,
51 chmPath,
52 mainChm,
53 ntstatusXml,
54 winerrorXml,
55 hresultXml,
56 svnCommand);
57 service.Run();
58
59 client = new IrcClient();
60 client.Encoding = System.Text.Encoding.GetEncoding("iso-8859-1");
61 client.MessageReceived += new MessageReceivedHandler(client_MessageReceived);
62 client.ChannelUserDatabaseChanged += new ChannelUserDatabaseChangedHandler(client_ChannelUserDatabaseChanged);
63 System.Console.WriteLine(String.Format("Connecting to {0} port {1}",
64 hostname, port));
65 client.Connect(hostname, port);
66 System.Console.WriteLine("Connected...");
67 client.Register(botname, null);
68 System.Console.WriteLine(String.Format("Registered as {0}...", botname));
69 JoinChannels();
70
71 while (!isStopped)
72 {
73 Thread.Sleep(1000);
74 }
75
76 PartChannels();
77 client.Diconnect();
78 System.Console.WriteLine("Disconnected...");
79 }
80
81 public void Stop()
82 {
83 isStopped = true;
84 }
85
86 private void JoinChannels()
87 {
88 foreach (string channelname in channelnames.Split(new char[] { ';' }))
89 {
90 IrcChannel channel = client.JoinChannel(channelname);
91 channels.Add(channel);
92 System.Console.WriteLine(String.Format("Joined channel #{0}...",
93 channel.Name));
94 }
95 }
96
97 private void PartChannels()
98 {
99 foreach (IrcChannel channel in channels)
100 {
101 client.PartChannel(channel, "Caught in the bitstream...");
102 System.Console.WriteLine(String.Format("Parted channel #{0}...",
103 channel.Name));
104 }
105 }
106
107 public void WriteLine(MessageContext context,
108 string message)
109 {
110 Console.WriteLine(String.Format("Sending: {0} to #{1}",
111 message,
112 context.Channel != null ? context.Channel.Name : "(null)"));
113 context.Channel.Talk(message);
114 }
115
116 private void ExtractMessage(string parameters,
117 out string message)
118 {
119 int startIndex = parameters.IndexOf(':');
120 if (startIndex != -1)
121 {
122 message = parameters.Substring(startIndex + 1);
123 }
124 else
125 {
126 message = parameters;
127 }
128 }
129
130 private bool GetChannelName(IrcMessage message,
131 out string channelName)
132 {
133 if (message.Parameters == null || !message.Parameters.StartsWith("#"))
134 {
135 channelName = null;
136 return false;
137 }
138
139 int index = message.Parameters.IndexOf(' ');
140 if (index == -1)
141 index = message.Parameters.Length;
142 channelName = message.Parameters.Substring(1, index - 1);
143 return true;
144 }
145
146 private bool ShouldAcceptMessage(IrcMessage message,
147 out MessageContext context)
148 {
149 if (message.Command.ToUpper().Equals("PRIVMSG"))
150 {
151 string channelName;
152 if (GetChannelName(message,
153 out channelName))
154 {
155 foreach (IrcChannel channel in channels)
156 {
157 if (String.Compare(channel.Name, channelName, true) == 0)
158 {
159 context = new MessageContext(channel);
160 return true;
161 }
162 }
163 }
164 }
165 context = null;
166 return false;
167 }
168
169 private void client_MessageReceived(IrcMessage message)
170 {
171 try
172 {
173 if (message.Command != null &&
174 message.Parameters != null)
175 {
176 string injectMessage;
177 ExtractMessage(message.Parameters,
178 out injectMessage);
179 MessageContext context;
180 if (ShouldAcceptMessage(message,
181 out context))
182 {
183 Console.WriteLine(String.Format("Injecting: {0} from #{1}",
184 injectMessage,
185 context.Channel.Name));
186 service.InjectMessage(context,
187 injectMessage);
188 }
189 else
190 {
191 Console.WriteLine("Received: " + message.Line);
192 }
193 }
194 else
195 {
196 Console.WriteLine("Received: " + message.Line);
197 }
198 }
199 catch (Exception ex)
200 {
201 Console.WriteLine(String.Format("Exception: {0}", ex));
202 }
203 }
204
205 private void client_ChannelUserDatabaseChanged(IrcChannel channel)
206 {
207 }
208 }
209 }