- Set better/updated default values
[reactos.git] / irc / TechBot / TechBot.Library / TechBotService.cs
index aede24d..025ff46 100644 (file)
-using System;\r
-using System.Collections;\r
-using System.IO;\r
-using System.Data;\r
-using System.Threading;\r
-using TechBot.IRCLibrary;\r
-\r
-namespace TechBot.Library\r
-{\r
-       public class TechBotService\r
-       {\r
-               private IServiceOutput serviceOutput;\r
-               private string chmPath;\r
-               private string mainChm;\r
-               private string ntstatusXml;\r
-               private string winerrorXml;\r
-               private string hresultXml;\r
-               private string svnCommand;\r
-               private ArrayList commands = new ArrayList();\r
-               \r
-               public TechBotService(IServiceOutput serviceOutput,\r
-                                     string chmPath,\r
-                                         string mainChm,\r
-                                         string ntstatusXml,\r
-                                         string winerrorXml,\r
-                                         string hresultXml,\r
-                                         string svnCommand)\r
-               {\r
-                       this.serviceOutput = serviceOutput;\r
-                       this.chmPath = chmPath;\r
-                       this.mainChm = mainChm;\r
-                       this.ntstatusXml = ntstatusXml;\r
-                       this.winerrorXml = winerrorXml;\r
-                       this.hresultXml = hresultXml;\r
-                       this.svnCommand = svnCommand;\r
-               }\r
-               \r
-               public void Run()\r
-               {\r
-                       commands.Add(new HelpCommand(serviceOutput,\r
-                                                    commands));\r
-                       commands.Add(new ApiCommand(serviceOutput,\r
-                                                   chmPath,\r
-                                                   mainChm));\r
-                       commands.Add(new NtStatusCommand(serviceOutput,\r
-                                                        ntstatusXml));\r
-                       commands.Add(new WinerrorCommand(serviceOutput,\r
-                                                        winerrorXml));\r
-                       commands.Add(new HresultCommand(serviceOutput,\r
-                                                       hresultXml));\r
-                       commands.Add(new SvnCommand(serviceOutput,\r
-                                                   svnCommand));\r
-               }\r
-               \r
-               public void InjectMessage(MessageContext context,\r
-                                         string message)\r
-               {\r
-                       if (message.StartsWith("!"))\r
-                               ParseCommandMessage(context,\r
-                                                   message);\r
-               }\r
-               \r
-               private bool IsCommandMessage(string message)\r
-               {\r
-                       return message.StartsWith("!");\r
-               }\r
-\r
-               public void ParseCommandMessage(MessageContext context,\r
-                                               string message)\r
-               {\r
-                       if (!IsCommandMessage(message))\r
-                               return;\r
-\r
-                       message = message.Substring(1).Trim();\r
-                       int index = message.IndexOf(' ');\r
-                       string commandName;\r
-                       string parameters = "";\r
-                       if (index != -1)\r
-                       {\r
-                               commandName = message.Substring(0, index).Trim();\r
-                               parameters = message.Substring(index).Trim();\r
-                       }\r
-                       else\r
-                               commandName = message.Trim();\r
-\r
-                       foreach (ICommand command in commands)\r
-                       {\r
-                               if (command.CanHandle(commandName))\r
-                               {\r
-                                       command.Handle(context,\r
-                                                      commandName, parameters);\r
-                                       return;\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-}\r
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.IO;
+using System.Data;
+using System.Threading;
+
+using TechBot.IRCLibrary;
+
+namespace TechBot.Library
+{
+       public abstract class TechBotService
+       {
+               protected IServiceOutput m_ServiceOutput;
+               
+               public TechBotService(IServiceOutput serviceOutput)
+               {
+                       m_ServiceOutput = serviceOutput;
+               }
+
+        public virtual void Run()
+        {
+            CommandFactory.LoadPlugins();
+        }
+
+        public IServiceOutput ServiceOutput
+        {
+            get { return m_ServiceOutput; }
+        }
+
+        public CommandBuilderCollection Commands
+        {
+            get { return CommandFactory.Commands; }
+        }
+
+        public void InjectMessage(MessageContext context, string message)
+        {
+            ParseCommandMessage(context,
+                                message);
+        }
+               
+               private bool IsCommandMessage(string message)
+               {
+            return message.StartsWith(Settings.Default.CommandPrefix);
+               }
+
+        public void InjectMessage(string message)
+        {
+            ParseCommandMessage(null, message);
+        }
+
+               public void ParseCommandMessage(MessageContext context,
+                                               string message)
+               {
+                       if (!IsCommandMessage(message))
+                               return;
+
+                       message = message.Substring(1).Trim();
+                       int index = message.IndexOf(' ');
+                       string commandName;
+                       string commandParams = "";
+                       if (index != -1)
+                       {
+                               commandName = message.Substring(0, index).Trim();
+                               commandParams = message.Substring(index).Trim();
+                       }
+                       else
+                               commandName = message.Trim();
+
+            foreach (CommandBuilder command in Commands)
+            {
+                if (command.Name == commandName)
+                {
+                    //Create a new instance of the required command type
+                    Command cmd = command.CreateCommand();
+
+                    cmd.TechBot = this;
+                    cmd.Context = context;
+                    cmd.Parameters = commandParams;
+
+                    try
+                    {
+                        cmd.Initialize();
+                        cmd.Run();
+                        cmd.DeInitialize();
+                    }
+                    catch (Exception e)
+                    {
+                        ServiceOutput.WriteLine(context, string.Format("Uops! Just crashed with exception '{0}' at {1}",
+                            e.Message,
+                            e.Source));
+
+                        ServiceOutput.WriteLine(context, e.StackTrace);
+                    }
+
+                    return;
+                }
+            }
+               }
+       }
+}