/// <summary> /// Ensures the given IRCConfig is not valid. /// </summary> /// <param name="config">The config to check.</param> private void CheckNotValid( IrcConfig config ) { Assert.Throws<ValidationException>( () => config.Validate() ); ReadOnlyIrcConfig roConfig = new ReadOnlyIrcConfig( config.Clone() ); Assert.Throws<ValidationException>( () => roConfig.Validate() ); IIrcConfig iircConfig = config; Assert.Throws<ValidationException>( () => iircConfig.Validate() ); }
/// <summary> /// Parses the given XML file and returns the IRC Config settings. /// </summary> /// <param name="fileName">The file name to parse.</param> /// <exception cref="FileNotFoundException">If the given filename does not exist.</exception> /// <exception cref="FormatException">If the port in the XML file is invalid.</exception> /// <exception cref="ApplicationException">If the irc config isn't valid.</exception> /// <returns>The IrcConfig objected based on the XML.</returns> public static IIrcConfig ParseIrcConfig(string fileName) { if (File.Exists(fileName) == false) { throw new FileNotFoundException("Could not find IRC Config file " + fileName); } XmlDocument doc = new XmlDocument(); doc.Load(fileName); XmlElement rootNode = doc.DocumentElement; if (rootNode.Name != ircConfigRootNodeName) { throw new XmlException( "Root XML node should be named \"" + ircConfigRootNodeName + "\". Got: " + rootNode.Name ); } IrcConfig config = new IrcConfig(); foreach (XmlNode childNode in rootNode.ChildNodes) { switch (childNode.Name) { case "server": config.Server = childNode.InnerText; break; case "channel": config.Channel = childNode.InnerText; break; case "port": config.Port = short.Parse(childNode.InnerText); break; case "username": config.UserName = childNode.InnerText; break; case "nick": config.Nick = childNode.InnerText; break; case "realname": config.RealName = childNode.InnerText; break; case "quitmessage": config.QuitMessage = childNode.InnerText; break; case "bridgebots": foreach (XmlNode bridgeBotNode in childNode.ChildNodes) { if (bridgeBotNode.Name == "bridgebot") { string botName = string.Empty; string botRegex = string.Empty; foreach (XmlAttribute attr in bridgeBotNode.Attributes) { switch (attr.Name) { case "botname": botName = attr.Value; break; case "botregex": botRegex = attr.Value; break; } } config.BridgeBots.Add(botName, botRegex); } } break; case "password": config.Password = childNode.InnerText; break; } } config.Validate(); return(config); }
/// <summary> /// Parses the given XML file and returns the IRC Config settings. /// </summary> /// <param name="fileName">The file name to parse.</param> /// <exception cref="FileNotFoundException">If the given filename does not exist.</exception> /// <exception cref="FormatException">If the port in the XML file is invalid.</exception> /// <exception cref="ApplicationException">If the irc config isn't valid.</exception> /// <returns>The IrcConfig objected based on the XML.</returns> public static IIrcConfig ParseIrcConfig(string fileName) { if (File.Exists(fileName) == false) { throw new FileNotFoundException("Could not find IRC Config file " + fileName); } XmlDocument doc = new XmlDocument(); doc.Load(fileName); XmlNode rootNode = doc.DocumentElement; if (rootNode.Name != ircConfigRootNodeName) { throw new XmlException( "Root XML node should be named \"" + ircConfigRootNodeName + "\". Got: " + rootNode.Name ); } IrcConfig config = new IrcConfig(); foreach (XmlNode childNode in rootNode.ChildNodes) { switch (childNode.Name.ToLower()) { case "server": config.Server = childNode.InnerText; break; case "channels": foreach (XmlNode channelNode in childNode.ChildNodes) { switch (channelNode.Name) { case "channel": config.Channels.Add(channelNode.InnerText); break; } } break; case "port": config.Port = short.Parse(childNode.InnerText); break; case "usessl": config.UseSsl = bool.Parse(childNode.InnerText); break; case "username": config.UserName = childNode.InnerText; break; case "nick": config.Nick = childNode.InnerText; break; case "realname": config.RealName = childNode.InnerText; break; case "quitmessage": config.QuitMessage = childNode.InnerText; break; case "bridgebots": foreach (XmlNode bridgeBotNode in childNode.ChildNodes) { if (bridgeBotNode.Name == "bridgebot") { string botName = string.Empty; string botRegex = string.Empty; foreach (XmlNode bridgeBotChild in bridgeBotNode.ChildNodes) { switch (bridgeBotChild.Name) { case "botname": botName = bridgeBotChild.InnerText; break; case "botregex": botRegex = bridgeBotChild.InnerText; break; } } config.BridgeBots.Add(botName, botRegex); } } break; case "admins": foreach (XmlNode adminNode in childNode.ChildNodes) { if (adminNode.Name == "admin") { config.Admins.Add(adminNode.InnerText.ToLower()); } } break; case "serverpasswordfile": config.ServerPassword = ReadFirstLineOfFile(childNode.InnerText, "Server Password File"); break; case "nickservpasswordfile": config.NickServPassword = ReadFirstLineOfFile(childNode.InnerText, "NickServ Password File"); break; case "ratelimit": config.RateLimit = int.Parse(childNode.InnerText); break; } } config.Validate(); return(config); }