// ---------------- 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 configPath = Path.Combine( initor.ChaskisConfigPluginRoot, "RssBot", "RssBotConfig.xml" ); this.admins = initor.IrcConfig.Admins; this.pluginLogger = initor.Log; if (File.Exists(configPath) == false) { throw new FileNotFoundException( "Can not open " + configPath ); } this.scheduler = initor.EventScheduler; this.rssConfig = XmlLoader.ParseConfig(configPath); foreach (Feed feed in this.rssConfig.Feeds) { FeedReader reader = new FeedReader(feed, initor.HttpClient); reader.Init(); int eventId = this.scheduler.ScheduleRecurringEvent( feed.RefreshInterval, delegate(IIrcWriter writer) { CheckForUpdates(reader, writer, feed.Channels); } ); this.feedReaders.Add(eventId, reader); } MessageHandlerConfig msgConfig = new MessageHandlerConfig { LineRegex = @"!debug\s+rssbot\s+updatefeed\s+(?<url>\S+)", LineAction = this.HandleDebug }; MessageHandler debugHandler = new MessageHandler( msgConfig ); this.handlers.Add(debugHandler); }
// ---------------- Constructor ---------------- /// <summary> /// Constructor /// </summary> public RssBot() { this.handlers = new List <IIrcHandler>(); this.feedReaders = new Dictionary <int, FeedReader>(); this.scheduler = null; }
// -------- Functions -------- /// <summary> /// Loads the given list of plugins. /// Any errors are logged to the passed in logger. /// </summary> /// <param name="assemblyList">List of assemblies we are to load.</param> /// <param name="existingPlugins">Already created plugins that do not need to be inited via reflection.</param> /// <param name="ircConfig">The irc config we are using.</param> /// <param name="chaskisConfigRoot">The root of the chaskis config.</param> public bool LoadPlugins( IList <AssemblyConfig> assemblyList, IList <PluginConfig> existingPlugins, IIrcConfig ircConfig, IChaskisEventScheduler scheduler, IChaskisEventSender eventSender, HttpClient httpClient, string chaskisConfigRoot ) { bool success = true; foreach (AssemblyConfig assemblyConfig in assemblyList) { try { Assembly dll = Assembly.LoadFrom(assemblyConfig.AssemblyPath); // Grab all the plugins, which have the ChaskisPlugin Attribute attached to them. var types = from type in dll.GetTypes() where type.IsDefined(typeof(ChaskisPlugin), false) select type; foreach (Type type in types) { // Make instance object instance = Activator.CreateInstance(type); IPlugin plugin = instance as IPlugin; if (plugin == null) { string errorString = string.Format( "Can not cast {0} to {1}, make sure your {0} class implements {1}", type.Name, nameof(IPlugin) ); throw new InvalidCastException(errorString); } ChaskisPlugin chaskisPlugin = type.GetCustomAttribute <ChaskisPlugin>(); this.plugins.Add( chaskisPlugin.PluginName, new PluginConfig( assemblyConfig.AssemblyPath, chaskisPlugin.PluginName, assemblyConfig.BlackListedChannels, plugin, new GenericLogger() ) ); StaticLogger.Log.WriteLine("Successfully loaded plugin: " + chaskisPlugin.PluginName); } } catch (Exception e) { StringBuilder errorString = new StringBuilder(); errorString.AppendLine("*************"); errorString.AppendLine("Warning! Error when loading assembly " + assemblyConfig.AssemblyPath + ":"); errorString.AppendLine(e.Message); errorString.AppendLine(); errorString.AppendLine(e.StackTrace); errorString.AppendLine(); if (e.InnerException != null) { errorString.AppendLine("\tInner Exception:"); errorString.AppendLine("\t\t" + e.InnerException.Message); errorString.AppendLine("\t\t" + e.InnerException.StackTrace); } errorString.AppendLine("*************"); success = false; StaticLogger.Log.ErrorWriteLine(errorString.ToString()); } } foreach (PluginConfig existingPlugin in existingPlugins) { this.plugins.Add(existingPlugin.Name, existingPlugin); } this.eventFactory = ChaskisEventFactory.CreateInstance(this.plugins.Keys.ToList()); foreach (KeyValuePair <string, PluginConfig> plugin in this.plugins) { try { PluginInitor initor = new PluginInitor { PluginPath = plugin.Value.AssemblyPath, IrcConfig = ircConfig, EventScheduler = scheduler, ChaskisEventSender = eventSender, ChaskisConfigRoot = chaskisConfigRoot, ChaskisEventCreator = this.eventFactory.EventCreators[plugin.Key], HttpClient = httpClient, Log = plugin.Value.Log }; initor.Log.OnWriteLine += delegate(string msg) { StaticLogger.Log.WriteLine("{0}> {1}", plugin.Value.Name, msg); }; initor.Log.OnErrorWriteLine += delegate(string msg) { StaticLogger.Log.ErrorWriteLine("{0}> {1}", plugin.Value.Name, msg); }; plugin.Value.Plugin.Init(initor); StaticLogger.Log.WriteLine("Successfully inited plugin: " + plugin.Value.Name); } catch (Exception e) { StringBuilder errorString = new StringBuilder(); errorString.AppendLine("*************"); errorString.AppendLine("Warning! Error when initing plugin " + plugin.Key + ":"); errorString.AppendLine(e.Message); errorString.AppendLine(); errorString.AppendLine(e.StackTrace); errorString.AppendLine(); Exception innerException = e.InnerException; if (innerException != null) { errorString.AppendLine("\tInner Exception:"); errorString.AppendLine("\t\t" + e.InnerException.Message); errorString.AppendLine("\t\t" + e.InnerException.StackTrace); innerException = innerException.InnerException; } errorString.AppendLine("*************"); success = false; StaticLogger.Log.ErrorWriteLine(errorString.ToString()); } } return(success); }