- Moved commands outside TechBot.Library to TechBot.Commands.Common and TechBot.Comma...
[reactos.git] / irc / TechBot / TechBot.Library / TechBotService.cs
index d40ab91..ad20173 100644 (file)
-using System;\r
-using System.Collections;\r
-using System.Collections.Generic;\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 wmXml;\r
-               private string svnCommand;\r
-               private string bugUrl, WineBugUrl, SambaBugUrl;\r
-        private List<Command> commands = new List<Command>();\r
-               \r
-               public TechBotService(IServiceOutput serviceOutput,\r
-                                     string chmPath,\r
-                                     string mainChm)\r
-                              //string ntstatusXml,\r
-                              //string winerrorXml,\r
-                              //string hresultXml,\r
-                              //string wmXml,\r
-                                     //string svnCommand,\r
-                              //string bugUrl,\r
-                              //string WineBugUrl,\r
-                              //string SambaBugUrl)\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.wmXml = wmXml;\r
-                       this.svnCommand = svnCommand;\r
-                       this.bugUrl = bugUrl;\r
-                       this.WineBugUrl = WineBugUrl;\r
-                       this.SambaBugUrl = SambaBugUrl;\r
-               }\r
-\r
-        public void Run()\r
-        {\r
-            commands.Add(new HelpCommand(this));\r
-            /*commands.Add(new ApiCommand(serviceOutput,\r
-                                        chmPath,\r
-                                        mainChm));*/\r
-            commands.Add(new NtStatusCommand(this));\r
-            commands.Add(new WinerrorCommand(this));\r
-            commands.Add(new HResultCommand(this));\r
-            commands.Add(new ErrorCommand(this));\r
-            commands.Add(new WMCommand(this));\r
-            commands.Add(new SvnCommand(this));\r
-            commands.Add(new ReactOSBugUrl(this));\r
-            commands.Add(new SambaBugUrl(this));\r
-            commands.Add(new WineBugUrl(this));\r
-        }\r
-\r
-        public IServiceOutput ServiceOutput\r
-        {\r
-            get { return serviceOutput; }\r
-        }\r
-\r
-        public IList<Command> Commands\r
-        {\r
-            get { return commands; }\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 commandParams = "";\r
-                       if (index != -1)\r
-                       {\r
-                               commandName = message.Substring(0, index).Trim();\r
-                               commandParams = message.Substring(index).Trim();\r
-                       }\r
-                       else\r
-                               commandName = message.Trim();\r
-\r
-                       foreach (Command command in commands)\r
-                       {\r
-                foreach (string cmd in command.AvailableCommands)\r
-                {\r
-                    if (cmd == commandName)\r
-                    {\r
-                        command.Handle(context,\r
-                                       commandName, \r
-                                       commandParams);\r
-                        return;\r
-                    }\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.ExecuteCommand();
+                        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;
+                }
+            }
+               }
+       }
+}