diff options
| author | Jokler <jokler.contact@gmail.com> | 2017-10-13 17:15:50 +0200 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2017-12-23 23:51:34 +0100 |
| commit | 7905adee9fc5a9664560de9500c18bb2ec5ca060 (patch) | |
| tree | c9fcc0572b25680182ff0ffb7a5f60ed2b9b225e /src/plugins | |
| parent | 92ea5a1c2e0b7ddaa102d6b602d180e84964c3be (diff) | |
| download | frippy-7905adee9fc5a9664560de9500c18bb2ec5ca060.tar.gz frippy-7905adee9fc5a9664560de9500c18bb2ec5ca060.zip | |
First prototype for the Factoids plugin
Diffstat (limited to 'src/plugins')
| -rw-r--r-- | src/plugins/factoids.rs | 108 | ||||
| -rw-r--r-- | src/plugins/mod.rs | 2 |
2 files changed, 110 insertions, 0 deletions
diff --git a/src/plugins/factoids.rs b/src/plugins/factoids.rs new file mode 100644 index 0000000..f13cada --- /dev/null +++ b/src/plugins/factoids.rs @@ -0,0 +1,108 @@ + +use irc::client::prelude::*; +use irc::error::Error as IrcError; +use std::collections::HashMap; +use std::sync::Mutex; + +use plugin::*; + +#[derive(PluginName, Debug)] +pub struct Factoids { + factoids: Mutex<HashMap<String, String>>, +} + +macro_rules! try_lock { + ( $m:expr ) => { + match $m.lock() { + Ok(guard) => guard, + Err(poisoned) => poisoned.into_inner(), + } + } +} + +impl Factoids { + pub fn new() -> Factoids { + Factoids { factoids: Mutex::new(HashMap::new()) } + } + + fn add(&self, server: &IrcServer, command: &mut PluginCommand) -> Result<(), IrcError> { + + if command.tokens.len() < 2 { + return self.invalid_command(server, command); + } + + let name = command.tokens.remove(0); + + try_lock!(self.factoids) + .insert(name, command.tokens.join(" ")); + + server.send_notice(&command.source, "Successfully added") + } + + fn get(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> { + if command.tokens.len() < 1 { + self.invalid_command(server, command) + + } else { + let factoids = try_lock!(self.factoids); + let factoid = match factoids.get(&command.tokens[0]) { + Some(v) => v, + None => return self.invalid_command(server, command), + }; + + server.send_privmsg(&command.target, factoid) + } + } + + fn invalid_command(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> { + server.send_notice(&command.source, "Invalid Command") + } +} + +impl Plugin for Factoids { + fn is_allowed(&self, _: &IrcServer, message: &Message) -> bool { + match message.command { + Command::PRIVMSG(_, ref content) => content.starts_with('!'), + _ => false, + } + } + + fn execute(&self, server: &IrcServer, message: &Message) -> Result<(), IrcError> { + if let Command::PRIVMSG(_, mut content) = message.command.clone() { + content.remove(0); + + let t: Vec<String> = content + .split(' ') + .map(ToOwned::to_owned) + .collect(); + + let c = PluginCommand { + source: message.source_nickname().unwrap().to_string(), + target: message.response_target().unwrap().to_string(), + tokens: t, + }; + + self.get(server, &c) + + } else { + Ok(()) + } + } + + fn command(&self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> { + if command.tokens.is_empty() { + self.invalid_command(server, &command) + + } else if command.tokens[0].to_lowercase() == "add" { + command.tokens.remove(0); + self.add(server, &mut command) + + } else if command.tokens[0].to_lowercase() == "get" { + command.tokens.remove(0); + self.get(server, &command) + + } else { + self.invalid_command(server, &command) + } + } +} diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index e8c4622..f860d88 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -3,10 +3,12 @@ mod help; mod url; mod emoji; mod currency; +mod factoids; mod keepnick; pub use self::help::Help; pub use self::url::Url; pub use self::emoji::Emoji; pub use self::currency::Currency; +pub use self::factoids::Factoids; pub use self::keepnick::KeepNick; |
