// -------- Functions -------- /// <summary> /// Initializes the plugin. This includes loading any configuration files, /// starting services, etc. Allowed to throw Exceptions. /// /// This function should be used to validates that the environment is good for the plugin. /// For example, it has all dependencies installed, config files are in the correct spot, etc. /// It should also load GetHandlers() with the handlers. /// </summary> /// <param name="pluginInit">The class that has information required for initing the plugin.</param> public void Init(PluginInitor initor) { string configPath = Path.Combine( initor.ChaskisConfigPluginRoot, "CapsWatcher", "CapsWatcherConfig.xml" ); this.log = initor.Log; if (File.Exists(configPath) == false) { throw new FileNotFoundException( "Can not open " + configPath ); } this.config = XmlLoader.LoadCapsWatcherConfig(configPath); this.ignoreRegex = new Regex(CollectionToRegex(config.Ignores), RegexOptions.Compiled | RegexOptions.ExplicitCapture); this.config.Ignores.Clear(); // No need to eat-up RAM, we won't need this again. MessageHandlerConfig msgConfig = new MessageHandlerConfig { LineRegex = ".+", LineAction = this.HandleMessage }; MessageHandler handler = new MessageHandler( msgConfig ); this.handlers.Add(handler); }
// -------- Functions -------- /// <summary> /// Parses the given XML file to create a CowSayBotConfig and returns that. /// </summary> /// <param name="xmlFilePath">Path to the XML file.</param> /// <exception cref="XmlException">If the XML is not correct</exception> /// <exception cref="FormatException">If the XML can not convert the string to a number</exception> /// <exception cref="InvalidOperationException">If the configuration object is not valid.</exception> /// <returns>A cowsay bot config based on the given XML.</returns> public static CapsWatcherConfig LoadCapsWatcherConfig(string xmlFilePath) { if (File.Exists(xmlFilePath) == false) { throw new FileNotFoundException("Could not find caps watcher bot config Config file " + xmlFilePath); } XmlDocument doc = new XmlDocument(); doc.Load(xmlFilePath); XmlElement rootNode = doc.DocumentElement; if (rootNode.Name != capsWatcherConfigRootNodeName) { throw new XmlException( "Root XML node should be named \"" + capsWatcherConfigRootNodeName + "\". Got: " + rootNode.Name ); } CapsWatcherConfig config = new CapsWatcherConfig(); foreach (XmlNode childNode in rootNode.ChildNodes) { switch (childNode.Name) { case "message": config.Messages.Add(childNode.InnerText); break; case "ignores": foreach (XmlNode ignoreNode in childNode.ChildNodes) { if (ignoreNode.Name == "ignore") { config.Ignores.Add(ignoreNode.InnerText); } } break; } } config.Validate(); return(config); }
// -------- Functions -------- /// <summary> /// Initializes the plugin. This includes loading any configuration files, /// starting services, etc. Allowed to throw Exceptions. /// /// This function should be used to validates that the environment is good for the plugin. /// For example, it has all dependencies installed, config files are in the correct spot, etc. /// It should also load GetHandlers() with the handlers. /// </summary> /// <param name="pluginPath"> /// The absolute path to the plugin, including the file name. To just get /// the path to the plugin, call Path.GetDirectoryName on this argument. /// </param> /// <param name="ircConfig">The IRC config we are using.</param> public void Init(string pluginPath, IIrcConfig ircConfig) { string configPath = Path.Combine( Path.GetDirectoryName(pluginPath), "CapsWatcherConfig.xml" ); if (File.Exists(configPath) == false) { throw new FileNotFoundException( "Can not open " + configPath ); } this.config = XmlLoader.LoadCapsWatcherConfig(configPath); MessageHandler handler = new MessageHandler( ".+", this.HandleMessage ); this.handlers.Add(handler); }