/// <summary> /// Deep-copies this class. /// </summary> /// <returns>A deep copy of this object.</returns> public CowSayBotConfig Clone() { CowSayBotConfig clone = (CowSayBotConfig)this.MemberwiseClone(); clone.CowFileInfoList = this.CowFileInfoList.Clone(); return(clone); }
/// <summary> /// Constructs the regex the that calls the Cow Say Handler. /// </summary> /// <param name="config">The cowsay bot config to use.</param> /// <returns>The regex that calls the cowsay handler.</returns> private string ConstructRegex(CowSayBotConfig config) { string commandRegex = @"(?<command>("; foreach (string command in config.CowFileInfoList.CommandList.Keys) { commandRegex += command + ")|("; } commandRegex = commandRegex.TrimEnd('|', '('); commandRegex += ")"; string regex = config.ListenRegex.Replace("{%saycmd%}", commandRegex); return(regex); }
// -------- Functions -------- /// <summary> /// Initializes the plugin. This includes loading any configuration files, /// starting services, etc. /// </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, "CowSayBot", "CowSayBotConfig.xml" ); if (File.Exists(configPath) == false) { throw new FileNotFoundException( "Can not open " + configPath ); } this.cowSayConfig = XmlLoader.LoadCowSayBotConfig(configPath); if (File.Exists(cowSayConfig.ExeCommand) == false) { throw new InvalidOperationException("Can not load cowsay program from " + cowSayConfig.ExeCommand); } this.cowSayInfo.FileName = cowSayConfig.ExeCommand; this.cowsayRegex = ConstructRegex(this.cowSayConfig); Console.WriteLine("CowSayBot: Using Regex '" + this.cowsayRegex + "'"); MessageHandlerConfig cowsayHandlerConfig = new MessageHandlerConfig { LineRegex = this.cowsayRegex, LineAction = this.HandleCowsayCommand, CoolDown = (int)cowSayConfig.CoolDownTimeSeconds, ResponseOption = ResponseOptions.ChannelOnly }; IIrcHandler cowSayHandler = new MessageHandler( cowsayHandlerConfig ); this.handlers.Add( cowSayHandler ); }
// -------- Functions -------- /// <summary> /// Initializes the plugin. This includes loading any configuration files, /// starting services, etc. /// </summary> /// <param name="pluginPath">Path to the plugin dll.</param> /// <param name="config">The irc config to use.</param> public void Init(string pluginPath, IIrcConfig config) { string configPath = Path.Combine( Path.GetDirectoryName(pluginPath), "CowSayBotConfig.xml" ); if (File.Exists(configPath) == false) { throw new FileNotFoundException( "Can not open " + configPath ); } this.ircConfig = config; this.cowSayConfig = XmlLoader.LoadCowSayBotConfig(configPath); if (File.Exists(cowSayConfig.ExeCommand) == false) { throw new InvalidOperationException("Can not load cowsay program from " + cowSayConfig.ExeCommand); } this.cowSayInfo.FileName = cowSayConfig.ExeCommand; this.cowsayRegex = ConstructRegex(this.cowSayConfig); Console.WriteLine("CowSayBot: Using Regex '" + this.cowsayRegex + "'"); IIrcHandler cowSayHandler = new MessageHandler( this.cowsayRegex, HandleCowsayCommand, (int)cowSayConfig.CoolDownTimeSeconds, ResponseOptions.RespondOnlyToChannel ); this.handlers.Add( cowSayHandler ); }
// -------- Constructor -------- /// <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 CowSayBotConfig LoadCowSayBotConfig(string xmlFilePath) { if (File.Exists(xmlFilePath) == false) { throw new FileNotFoundException("Could not find cowsay bot config Config file " + xmlFilePath); } XmlDocument doc = new XmlDocument(); doc.Load(xmlFilePath); XmlElement rootNode = doc.DocumentElement; if (rootNode.Name != cowSayConfigRootNodeName) { throw new XmlException( "Root XML node should be named \"" + cowSayConfigRootNodeName + "\". Got: " + rootNode.Name ); } CowSayBotConfig config = new CowSayBotConfig(); foreach (XmlNode childNode in rootNode.ChildNodes) { switch (childNode.Name) { case "command": config.ListenRegex = childNode.InnerText; break; case "path": config.ExeCommand = childNode.InnerText; break; case "cowsaycooldown": config.CoolDownTimeSeconds = uint.Parse(childNode.InnerText); break; case "cowfiles": foreach (XmlNode cowFileNode in childNode.ChildNodes) { if (cowFileNode.Name == "cowfile") { string name = string.Empty; string command = string.Empty; foreach (XmlAttribute attribute in cowFileNode.Attributes) { switch (attribute.Name) { case "name": name = attribute.Value; break; case "command": command = attribute.Value; break; } } config.CowFileInfoList.CommandList[command] = name; } } break; } } config.Validate(); return(config); }