- Add support for "!wm <value>" and "!wm <name>" commands.
[reactos.git] / irc / TechBot / TechBot.Library / TechBotService.cs
1 using System;
2 using System.Collections;
3 using System.IO;
4 using System.Data;
5 using System.Threading;
6 using TechBot.IRCLibrary;
7
8 namespace TechBot.Library
9 {
10 public class TechBotService
11 {
12 private IServiceOutput serviceOutput;
13 private string chmPath;
14 private string mainChm;
15 private string ntstatusXml;
16 private string winerrorXml;
17 private string hresultXml;
18 private string wmXml;
19 private string svnCommand;
20 private ArrayList commands = new ArrayList();
21
22 public TechBotService(IServiceOutput serviceOutput,
23 string chmPath,
24 string mainChm,
25 string ntstatusXml,
26 string winerrorXml,
27 string hresultXml,
28 string wmXml,
29 string svnCommand)
30 {
31 this.serviceOutput = serviceOutput;
32 this.chmPath = chmPath;
33 this.mainChm = mainChm;
34 this.ntstatusXml = ntstatusXml;
35 this.winerrorXml = winerrorXml;
36 this.hresultXml = hresultXml;
37 this.wmXml = wmXml;
38 this.svnCommand = svnCommand;
39 }
40
41 public void Run()
42 {
43 commands.Add(new HelpCommand(serviceOutput,
44 commands));
45 commands.Add(new ApiCommand(serviceOutput,
46 chmPath,
47 mainChm));
48 commands.Add(new NtStatusCommand(serviceOutput,
49 ntstatusXml));
50 commands.Add(new WinerrorCommand(serviceOutput,
51 winerrorXml));
52 commands.Add(new HresultCommand(serviceOutput,
53 hresultXml));
54 commands.Add(new WmCommand(serviceOutput,
55 wmXml));
56 commands.Add(new SvnCommand(serviceOutput,
57 svnCommand));
58 }
59
60 public void InjectMessage(MessageContext context,
61 string message)
62 {
63 if (message.StartsWith("!"))
64 ParseCommandMessage(context,
65 message);
66 }
67
68 private bool IsCommandMessage(string message)
69 {
70 return message.StartsWith("!");
71 }
72
73 public void ParseCommandMessage(MessageContext context,
74 string message)
75 {
76 if (!IsCommandMessage(message))
77 return;
78
79 message = message.Substring(1).Trim();
80 int index = message.IndexOf(' ');
81 string commandName;
82 string parameters = "";
83 if (index != -1)
84 {
85 commandName = message.Substring(0, index).Trim();
86 parameters = message.Substring(index).Trim();
87 }
88 else
89 commandName = message.Trim();
90
91 foreach (ICommand command in commands)
92 {
93 if (command.CanHandle(commandName))
94 {
95 command.Handle(context,
96 commandName, parameters);
97 return;
98 }
99 }
100 }
101 }
102 }