- code refactoring
[reactos.git] / irc / TechBot / TechBot.Library / TechBotService.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Data;
6 using System.Threading;
7
8 using TechBot.IRCLibrary;
9
10 namespace TechBot.Library
11 {
12 public abstract class TechBotService
13 {
14 protected IServiceOutput serviceOutput;
15 private string chmPath;
16 private string mainChm;
17
18 public TechBotService(IServiceOutput serviceOutput,
19 string chmPath,
20 string mainChm)
21 {
22 this.serviceOutput = serviceOutput;
23 this.chmPath = chmPath;
24 this.mainChm = mainChm;
25 }
26
27 public virtual void Run()
28 {
29 CommandFactory.LoadPlugins();
30 }
31
32 public IServiceOutput ServiceOutput
33 {
34 get { return serviceOutput; }
35 }
36
37 public CommandBuilderCollection Commands
38 {
39 get { return CommandFactory.Commands; }
40 }
41
42 public void InjectMessage(MessageContext context, string message)
43 {
44 ParseCommandMessage(context,
45 message);
46 }
47
48 private bool IsCommandMessage(string message)
49 {
50 return message.StartsWith("!");
51 }
52
53 public void ParseCommandMessage(MessageContext context,
54 string message)
55 {
56 if (!IsCommandMessage(message))
57 return;
58
59 message = message.Substring(1).Trim();
60 int index = message.IndexOf(' ');
61 string commandName;
62 string commandParams = "";
63 if (index != -1)
64 {
65 commandName = message.Substring(0, index).Trim();
66 commandParams = message.Substring(index).Trim();
67 }
68 else
69 commandName = message.Trim();
70
71 foreach (CommandBuilder command in Commands)
72 {
73 if (command.Name == commandName)
74 {
75 //Create a new instance of the required command type
76 Command cmd = command.CreateCommand();
77
78 cmd.TechBot = this;
79 cmd.Context = context;
80 cmd.ParseParameters(message);
81 cmd.ExecuteCommand();
82
83 return;
84 }
85 }
86 }
87 }
88 }