Remove putpixel from strechblt and add code to write direcly to the memory. it incres...
[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 wmXml;
20 private string svnCommand;
21 private string bugUrl;
22 private IrcClient client;
23 private ArrayList channels = new ArrayList(); /* IrcChannel */
24 private TechBotService service;
25 private bool isStopped = false;
26
27 public IrcService(string hostname,
28 int port,
29 string channelnames,
30 string botname,
31 string chmPath,
32 string mainChm,
33 string ntstatusXml,
34 string winerrorXml,
35 string hresultXml,
36 string wmXml,
37 string svnCommand,
38 string bugUrl)
39 {
40 this.hostname = hostname;
41 this.port = port;
42 this.channelnames = channelnames;
43 this.botname = botname;
44 this.chmPath = chmPath;
45 this.mainChm = mainChm;
46 this.ntstatusXml = ntstatusXml;
47 this.winerrorXml = winerrorXml;
48 this.hresultXml = hresultXml;
49 this.wmXml = wmXml;
50 this.svnCommand = svnCommand;
51 this.bugUrl = bugUrl;
52 }
53
54 public void Run()
55 {
56 service = new TechBotService(this,
57 chmPath,
58 mainChm,
59 ntstatusXml,
60 winerrorXml,
61 hresultXml,
62 wmXml,
63 svnCommand,
64 bugUrl);
65 service.Run();
66
67 client = new IrcClient();
68 client.Encoding = System.Text.Encoding.GetEncoding("iso-8859-1");
69 client.MessageReceived += new MessageReceivedHandler(client_MessageReceived);
70 client.ChannelUserDatabaseChanged += new ChannelUserDatabaseChangedHandler(client_ChannelUserDatabaseChanged);
71 System.Console.WriteLine(String.Format("Connecting to {0} port {1}",
72 hostname, port));
73 client.Connect(hostname, port);
74 System.Console.WriteLine("Connected...");
75 client.Register(botname, null);
76 System.Console.WriteLine(String.Format("Registered as {0}...", botname));
77 JoinChannels();
78
79 while (!isStopped)
80 {
81 Thread.Sleep(1000);
82 }
83
84 PartChannels();
85 client.Diconnect();
86 System.Console.WriteLine("Disconnected...");
87 }
88
89 public void Stop()
90 {
91 isStopped = true;
92 }
93
94 private void JoinChannels()
95 {
96 foreach (string channelname in channelnames.Split(new char[] { ';' }))
97 {
98 IrcChannel channel = client.JoinChannel(channelname);
99 channels.Add(channel);
100 System.Console.WriteLine(String.Format("Joined channel #{0}...",
101 channel.Name));
102 }
103 }
104
105 private void PartChannels()
106 {
107 foreach (IrcChannel channel in channels)
108 {
109 client.PartChannel(channel, "Caught in the bitstream...");
110 System.Console.WriteLine(String.Format("Parted channel #{0}...",
111 channel.Name));
112 }
113 }
114
115 private string GetMessageSource(MessageContext context)
116 {
117 if (context is ChannelMessageContext)
118 {
119 ChannelMessageContext channelContext = context as ChannelMessageContext;
120 return String.Format("#{0}",
121 channelContext.Channel.Name);
122 }
123 else if (context is UserMessageContext)
124 {
125 UserMessageContext userContext = context as UserMessageContext;
126 return userContext.User.Nickname;
127 }
128 else
129 {
130 throw new InvalidOperationException(String.Format("Unhandled message context '{0}'",
131 context.GetType()));
132 }
133 }
134
135 public void WriteLine(MessageContext context,
136 string message)
137 {
138 if (context is ChannelMessageContext)
139 {
140 ChannelMessageContext channelContext = context as ChannelMessageContext;
141 channelContext.Channel.Talk(message);
142 }
143 else if (context is UserMessageContext)
144 {
145 UserMessageContext userContext = context as UserMessageContext;
146 userContext.User.Talk(message);
147 }
148 else
149 {
150 throw new InvalidOperationException(String.Format("Unhandled message context '{0}'",
151 context.GetType()));
152 }
153 }
154
155 private void ExtractMessage(string parameters,
156 out string message)
157 {
158 int startIndex = parameters.IndexOf(':');
159 if (startIndex != -1)
160 {
161 message = parameters.Substring(startIndex + 1);
162 }
163 else
164 {
165 message = parameters;
166 }
167 }
168
169 private bool GetChannelName(IrcMessage message,
170 out string channelName)
171 {
172 if (message.Parameters == null || !message.Parameters.StartsWith("#"))
173 {
174 channelName = null;
175 return false;
176 }
177
178 int index = message.Parameters.IndexOf(' ');
179 if (index == -1)
180 index = message.Parameters.Length;
181 else
182 index = index - 1;
183 channelName = message.Parameters.Substring(1, index);
184 return true;
185 }
186
187 private bool GetTargetNickname(IrcMessage message,
188 out string nickname)
189 {
190 if (message.Parameters == null)
191 {
192 nickname = null;
193 return false;
194 }
195
196 int index = message.Parameters.IndexOf(' ');
197 if (index == -1)
198 index = message.Parameters.Length;
199 nickname = message.Parameters.Substring(0, index);
200 Console.WriteLine("nickname: " + nickname);
201 return true;
202 }
203
204 private bool ShouldAcceptMessage(IrcMessage message,
205 out MessageContext context)
206 {
207 if (message.Command.ToUpper().Equals("PRIVMSG"))
208 {
209 string channelName;
210 string nickname;
211 if (GetChannelName(message,
212 out channelName))
213 {
214 foreach (IrcChannel channel in channels)
215 {
216 if (String.Compare(channel.Name, channelName, true) == 0)
217 {
218 context = new ChannelMessageContext(channel);
219 return true;
220 }
221 }
222 }
223 else if (GetTargetNickname(message,
224 out nickname))
225 {
226 IrcUser targetUser = new IrcUser(client,
227 nickname);
228 if (String.Compare(targetUser.Nickname, botname, true) == 0)
229 {
230 IrcUser sourceUser = new IrcUser(client,
231 message.PrefixNickname);
232 context = new UserMessageContext(sourceUser);
233 return true;
234 }
235 }
236 }
237 context = null;
238 return false;
239 }
240
241 private void client_MessageReceived(IrcMessage message)
242 {
243 try
244 {
245 if (message.Command != null &&
246 message.Parameters != null)
247 {
248 string injectMessage;
249 ExtractMessage(message.Parameters,
250 out injectMessage);
251 MessageContext context;
252 if (ShouldAcceptMessage(message,
253 out context))
254 {
255 Console.WriteLine(String.Format("Injecting: {0} from {1}",
256 injectMessage,
257 GetMessageSource(context)));
258 service.InjectMessage(context,
259 injectMessage);
260 }
261 else
262 {
263 Console.WriteLine("Received: " + message.Line);
264 }
265 }
266 else
267 {
268 Console.WriteLine("Received: " + message.Line);
269 }
270 }
271 catch (Exception ex)
272 {
273 Console.WriteLine(String.Format("Exception: {0}", ex));
274 }
275 }
276
277 private void client_ChannelUserDatabaseChanged(IrcChannel channel)
278 {
279 }
280 }
281 }