// ---------------- Constructor ---------------- public QuoteBotParser(QuoteBotConfig config) { ArgumentChecker.IsNotNull(config, nameof(config)); this.addRegex = new Regex(config.AddCommand, RegexOptions.Compiled); this.deleteRegex = new Regex(config.DeleteCommand, RegexOptions.Compiled); this.randomRegex = new Regex(config.RandomCommand, RegexOptions.Compiled); this.getRegex = new Regex(config.GetCommand, RegexOptions.Compiled); }
// ---------------- Functions ---------------- /// <summary> /// Loads the given QuoteBotConfig from the given path, /// and returns the corresponding object. /// </summary> public static QuoteBotConfig LoadConfig(string configPath) { if (File.Exists(configPath) == false) { throw new FileNotFoundException("Could not find cowsay bot config Config file " + configPath); } XmlDocument doc = new XmlDocument(); doc.Load(configPath); XmlElement rootNode = doc.DocumentElement; if (rootNode.Name != quoteBotConfigRootNode) { throw new XmlException( "Root XML node should be named \"" + quoteBotConfigRootNode + "\". Got: " + rootNode.Name ); } QuoteBotConfig config = new QuoteBotConfig(); foreach (XmlNode childNode in rootNode.ChildNodes) { switch (childNode.Name) { case "addcommand": config.AddCommand = childNode.InnerText; break; case "deletecommand": config.DeleteCommand = childNode.InnerText; break; case "randomcommand": config.RandomCommand = childNode.InnerText; break; case "getcommand": config.GetCommand = childNode.InnerText; break; } } config.Validate(); return(config); }
// ---------------- Functions ---------------- /// <summary> /// Initializes the plugin. /// </summary> /// <param name="pluginInit">The class that has information required for initing the plugin.</param> public void Init(PluginInitor initor) { string quoteBotRoot = Path.Combine( initor.ChaskisConfigPluginRoot, "QuoteBot" ); string configPath = Path.Combine( quoteBotRoot, "QuoteBotConfig.xml" ); if (File.Exists(configPath) == false) { throw new FileNotFoundException( "Can not open " + configPath ); } this.ircConfig = initor.IrcConfig; this.quoteBotConfig = XmlLoader.LoadConfig(configPath); this.parser = new QuoteBotParser(this.quoteBotConfig); this.db = new QuoteBotDatabase(Path.Combine(quoteBotRoot, "quotes.ldb")); { MessageHandlerConfig msgConfig = new MessageHandlerConfig { LineRegex = this.quoteBotConfig.AddCommand, LineAction = this.AddHandler }; MessageHandler addHandler = new MessageHandler( msgConfig ); this.handlers.Add(addHandler); } { MessageHandlerConfig msgConfig = new MessageHandlerConfig { LineRegex = this.quoteBotConfig.DeleteCommand, LineAction = this.DeleteHandler }; MessageHandler deleteHandler = new MessageHandler( msgConfig ); this.handlers.Add(deleteHandler); } { MessageHandlerConfig msgConfig = new MessageHandlerConfig { LineRegex = this.quoteBotConfig.RandomCommand, LineAction = this.RandomHandler }; MessageHandler randomHandler = new MessageHandler( msgConfig ); this.handlers.Add(randomHandler); } { MessageHandlerConfig msgConfig = new MessageHandlerConfig { LineRegex = this.quoteBotConfig.GetCommand, LineAction = this.GetHandler }; MessageHandler getHandler = new MessageHandler( msgConfig ); this.handlers.Add(getHandler); } }