Exemplo n.º 1
0
        public void SaveConfig()
        {
            var rootEl = new XElement("bot");

            BotModule.SaveProperties(this, rootEl);

            _startup.ForEach((startup) =>
            {
                var startupEl = new XElement("startup", startup);
                rootEl.Add(startupEl);
            });

            _modules.ForEach((module) =>
            {
                var moduleEl = new XElement("module",
                                            new XAttribute("type", module.GetType().FullName));
                module.DoSaveConfig(moduleEl);
                rootEl.Add(moduleEl);
            });

            var doc = new XDocument(rootEl);

            try
            {
                doc.Save(_configPath, SaveOptions.None);
            }
            catch (Exception ex)
            {
                throw new BotConfigException(ex);
            }
        }
Exemplo n.º 2
0
        protected override void LoadConfig(XElement moduleEl)
        {
            base.LoadConfig(moduleEl);

            foreach (var feedEl in moduleEl.Elements("feed"))
            {
                var feed = new RssFeed();
                BotModule.LoadProperties(feed, feedEl);
                if (_feeds.ContainsKey(feed.Name))
                {
                    throw new BotConfigException(string.Format(
                                                     "An RSS feed with the name {0} already exists.", feed.Name));
                }
                try
                {
                    feed.CatchUp();
                    _feeds.Add(feed.Name, feed);
                }
                catch (Exception ex)
                {
                    throw new BotConfigException(string.Format(
                                                     "An RSS feed with the name {0} could not be loaded: {1}", feed.Name, ex.Message));
                }
            }
        }
Exemplo n.º 3
0
 public static void SaveProperties(object obj, XElement configEl)
 {
     foreach (var property in obj.GetType().GetProperties(
                  BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
     {
         var attr = property.GetCustomAttributes(
             typeof(ModulePropertyAttribute), true).OfType <ModulePropertyAttribute>().FirstOrDefault();
         if (attr != null)
         {
             try
             {
                 object val = property.GetValue(obj, null);
                 if (val != null)
                 {
                     var converter = BotModule.GetTypeConverter(property);
                     configEl.Add(new XAttribute(attr.ConfigName, converter.ConvertToString(val)));
                 }
             }
             catch (Exception ex)
             {
                 throw new BotConfigException(string.Format(
                                                  "Could not save configuration property {0}", attr.ConfigName), ex);
             }
         }
     }
 }
Exemplo n.º 4
0
 public static void LoadProperties(object obj, XElement configEl)
 {
     foreach (var property in obj.GetType().GetProperties(
                  BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
     {
         var attr = property.GetCustomAttributes(
             typeof(ModulePropertyAttribute), true).OfType <ModulePropertyAttribute>().FirstOrDefault();
         if (attr != null)
         {
             var configAt = configEl.Attribute(attr.ConfigName);
             if (configAt != null)
             {
                 try
                 {
                     var converter = BotModule.GetTypeConverter(property);
                     property.SetValue(obj, converter.ConvertFromString(configAt.Value), null);
                 }
                 catch (Exception ex)
                 {
                     throw new BotConfigException(string.Format(
                                                      "Could not load configuration property {0}", attr.ConfigName), ex);
                 }
             }
         }
     }
 }
Exemplo n.º 5
0
        public void LoadConfig()
        {
            if (string.IsNullOrEmpty(_configPath))
            {
                throw new BotConfigException("No configuration path was provided.");
            }

            XDocument doc;

            try
            {
                doc = XDocument.Load(_configPath);
            }
            catch (Exception ex)
            {
                throw new BotConfigException(ex);
            }

            var root = doc.Root;

            if (root.Name != "bot")
            {
                throw new BotConfigException("Missing 'bot' element.");
            }

            BotModule.LoadProperties(this, root);

            foreach (var startupEl in root.Elements("startup"))
            {
                _startup.Add(startupEl.Value);
            }

            foreach (var moduleEl in root.Elements("module"))
            {
                this.LoadModule(moduleEl);
            }

            this.Irc = new IrcSession();
            BotModule.LoadProperties(this.Irc, root);

            this.Security = this.GetModule(typeof(Security)) as Security;
            if (this.Security == null)
            {
                throw new InvalidOperationException("There must be a configured Security module.");
            }
            this.Aliases = this.GetModule(typeof(Aliases)) as Aliases;
            if (this.Aliases == null)
            {
                throw new InvalidOperationException("There must be a configured Aliases module.");
            }
            this.Channels = this.GetModule(typeof(Channels)) as Channels;
            if (this.Channels == null)
            {
                throw new InvalidOperationException("There must be a configured Channels module.");
            }
        }
Exemplo n.º 6
0
        protected override void SaveConfig(System.Xml.Linq.XElement moduleEl)
        {
            base.SaveConfig(moduleEl);

            foreach (var user in _users)
            {
                var userEl = new XElement("user");
                BotModule.SaveProperties(user, userEl);
                moduleEl.Add(userEl);
            }
        }
Exemplo n.º 7
0
        protected override void SaveConfig(XElement moduleEl)
        {
            base.SaveConfig(moduleEl);

            foreach (var feed in _feeds.Values)
            {
                var feedEl = new XElement("feed");
                BotModule.SaveProperties(feed, feedEl);
                moduleEl.Add(feedEl);
            }
        }
Exemplo n.º 8
0
        protected override void LoadConfig(System.Xml.Linq.XElement moduleEl)
        {
            base.LoadConfig(moduleEl);

            foreach (var userEl in moduleEl.Elements("user"))
            {
                var user = new User();
                BotModule.LoadProperties(user, userEl);
                if (_userIndex.ContainsKey(user.Name))
                {
                    throw new BotConfigException(string.Format(
                                                     "There are multiple users with the name {0}.", user.Name));
                }
                _users.Add(user);
                _userIndex.Add(user.Name, user);
            }
        }
Exemplo n.º 9
0
 protected virtual void SaveConfig(XElement moduleEl)
 {
     BotModule.SaveProperties(this, moduleEl);
 }
Exemplo n.º 10
0
 protected virtual void LoadConfig(XElement moduleEl)
 {
     BotModule.LoadProperties(this, moduleEl);
 }