// ---------------- Functions ---------------- public void Init(PluginInitor initor) { this.chaskisEventCreator = initor.ChaskisEventCreator; this.eventSender = initor.ChaskisEventSender; this.ircConfig = initor.IrcConfig; this.logger = initor.Log; this.pluginDir = Path.Combine( initor.ChaskisConfigPluginRoot, "NewVersionNotifier" ); string configPath = Path.Combine( this.pluginDir, "NewVersionNotifierConfig.xml" ); this.config = XmlLoader.LoadConfig(configPath); this.cachedFilePath = Path.Combine( this.pluginDir, cacheFileName ); if (File.Exists(this.cachedFilePath) == false) { this.cachedVersion = string.Empty; } else { string[] lines = File.ReadAllLines(this.cachedFilePath); if (lines.Length == 0) { this.cachedVersion = string.Empty; } else { this.cachedVersion = lines[0].Trim(); } } ChaskisEventHandler eventHandler = this.chaskisEventCreator.CreatePluginEventHandler( "chaskis", this.HandleChaskisEvent ); this.ircHandlers.Add(eventHandler); JoinHandlerConfig joinHandlerConfig = new JoinHandlerConfig { JoinAction = this.OnJoinChannel, RespondToSelf = true }; JoinHandler joinHandler = new JoinHandler(joinHandlerConfig); this.ircHandlers.Add(joinHandler); }
/// <summary> /// An initor that is used if we already know the config object. /// </summary> public void Init(PluginInitor initor, WelcomeBotConfig config) { if (this.isLoaded == false) { this.eventCreator = initor.ChaskisEventCreator; this.eventSender = initor.ChaskisEventSender; if (config.EnableJoinMessages) { JoinHandlerConfig joinHandlerConfig = new JoinHandlerConfig { JoinAction = this.JoinMessage }; this.handlers.Add(new JoinHandler(joinHandlerConfig)); } if (config.EnablePartMessages) { PartHandlerConfig partHandlerConfig = new PartHandlerConfig { PartAction = PartMessage }; this.handlers.Add(new PartHandler(partHandlerConfig)); } if (config.EnableKickMessages) { KickHandlerConfig kickHandlerConfig = new KickHandlerConfig { KickAction = KickMessage }; this.handlers.Add(new KickHandler(kickHandlerConfig)); } if (config.EnableJoinMessages && config.KarmaBotIntegration) { ChaskisEventHandler karmaHandler = this.eventCreator.CreatePluginEventHandler( "karmabot", HandleKarmaQuery ); this.handlers.Add(karmaHandler); } this.isLoaded = true; } }
// ---------------- Functions ---------------- public void Init(PluginInitor pluginInit) { this.ircConfig = pluginInit.IrcConfig; this.chaskisEventCreator = pluginInit.ChaskisEventCreator; this.chaskisEventSender = pluginInit.ChaskisEventSender; this.AddPluginListHandler(); this.AddSourceHandler(); this.AddVersionHandler(); this.AddAboutHandler(); this.AddHelpHandler(); this.AddAdminHandler(); this.AddDebugHandlers(); this.AddCtcpPingHandler(); // Must always check for pings. this.handlers.Add(new PingHandler()); // Must always handle pongs. this.handlers.Add(new PongHandler()); }
// -------- Functions -------- /// <summary> /// Inits this plugin. /// </summary> /// <param name="pluginInit">The class that has information required for initing the plugin.</param> public void Init(PluginInitor initor) { string karmaBotRoot = Path.Combine( initor.ChaskisConfigPluginRoot, "KarmaBot" ); string dbPath = Path.Combine( karmaBotRoot, "karmabot.ldb" ); string configPath = Path.Combine( karmaBotRoot, "KarmaBotConfig.xml" ); if (File.Exists(configPath) == false) { throw new FileNotFoundException( "Can not open " + configPath ); } this.config = XmlLoader.LoadKarmaBotConfig(configPath); this.dataBase = new KarmaBotDatabase(dbPath); this.eventSender = initor.ChaskisEventSender; this.eventCreator = initor.ChaskisEventCreator; { MessageHandlerConfig msgConfig = new MessageHandlerConfig { LineRegex = this.config.IncreaseCommandRegex, LineAction = HandleIncreaseCommand }; MessageHandler increaseHandler = new MessageHandler( msgConfig ); this.handlers.Add(increaseHandler); } { MessageHandlerConfig msgConfig = new MessageHandlerConfig { LineRegex = this.config.DecreaseCommandRegex, LineAction = HandleDecreaseCommand }; MessageHandler decreaseCommand = new MessageHandler( msgConfig ); this.handlers.Add(decreaseCommand); } { MessageHandlerConfig msgConfig = new MessageHandlerConfig { LineRegex = this.config.QueryCommand, LineAction = HandleQueryCommand }; MessageHandler queryCommand = new MessageHandler( msgConfig ); this.handlers.Add(queryCommand); } { ChaskisEventHandler chaskisQuery = initor.ChaskisEventCreator.CreatePluginEventHandler( this.HandleChaskisQueryCommand ); this.handlers.Add(chaskisQuery); } }
// -------- 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); }