From b4c1722bbee9fa822a6063cbb0540c0a7fb2e430 Mon Sep 17 00:00:00 2001 From: Jokler Date: Fri, 15 Dec 2017 19:27:43 +0100 Subject: Add option for disabled plugins in the toml files --- bin/main.rs | 16 ++++++++++++++++ configs/config.toml | 3 +++ src/lib.rs | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/bin/main.rs b/bin/main.rs index 08787ff..420e016 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -81,6 +81,14 @@ fn main() { // Open a connection and add work for each config for config in configs { + + let mut disabled_plugins = None; + if let &Some(ref options) = &config.options { + if let Some(disabled) = options.get("disabled_plugins") { + disabled_plugins = Some(disabled.split(",").map(|p| p.trim()).collect::>()); + } + } + let mut bot = frippy::Bot::new(); bot.add_plugin(plugins::Help::new()); bot.add_plugin(plugins::Url::new(1024)); @@ -88,6 +96,14 @@ fn main() { bot.add_plugin(plugins::Currency::new()); bot.add_plugin(plugins::KeepNick::new()); + if let Some(disabled_plugins) = disabled_plugins { + for name in disabled_plugins { + if let None = bot.remove_plugin(name) { + error!("{:?} was not found - could not disable", name); + } + } + } + bot.connect(&mut reactor, &config); } diff --git a/configs/config.toml b/configs/config.toml index 5ad66e2..02f86fd 100644 --- a/configs/config.toml +++ b/configs/config.toml @@ -23,3 +23,6 @@ source = "https://github.com/Mavulp/frippy" #[channel_keys] #"#frippy" = "" + +#[options] +#disabled_plugins = "Url" diff --git a/src/lib.rs b/src/lib.rs index 4a511c3..d34a728 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -90,6 +90,22 @@ impl Bot { self.plugins.add(plugin); } + /// Removes a plugin based on its name. + /// The binary currently uses this to disable plugins + /// based on user configuration. + /// + /// # Examples + /// ``` + /// use frippy::{plugins, Bot}; + /// + /// let mut bot = frippy::Bot::new(); + /// bot.add_plugin(plugins::Help::new()); + /// bot.remove_plugin("Help"); + /// ``` + pub fn remove_plugin(&mut self, name: &str) -> Option<()> { + self.plugins.remove(name) + } + /// This connects the `Bot` to IRC and adds a task /// to the Core that was supplied. /// @@ -189,6 +205,10 @@ impl ThreadedPlugins { self.plugins.insert(name, safe_plugin); } + pub fn remove(&mut self, name: &str) -> Option<()> { + self.plugins.remove(&name.to_lowercase()).map(|_| ()) + } + pub fn execute_plugins(&mut self, server: &IrcServer, message: Message) { let message = Arc::new(message); -- cgit v1.2.3-70-g09d2