extern crate rlua; use irc::client::prelude::*; use irc::error::Error as IrcError; use self::rlua::Lua; use std::collections::HashMap; use std::sync::Mutex; use plugin::*; #[derive(PluginName, Debug)] pub struct Factoids { factoids: Mutex>, } 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 name = &command.tokens[0]; let factoids = try_lock!(self.factoids); let factoid = match factoids.get(name) { Some(v) => v, None => return self.invalid_command(server, command), }; server.send_privmsg(&command.target, &format!("{}: {}", name, factoid)) } } fn exec(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> { if command.tokens.len() < 1 { self.invalid_command(server, command) } else { let name = &command.tokens[0]; let factoids = try_lock!(self.factoids); let factoid = match factoids.get(name) { Some(v) => v, None => return self.invalid_command(server, command), }; let value = match self.run_lua(name, factoid) { Ok(v) => v, Err(e) => format!("{}", e), }; server.send_privmsg(&command.target, &value) } } fn run_lua(&self, name: &str, code: &str) -> Result { let lua = Lua::new(); lua.eval::(code, Some(name)) } 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 = content.split(' ').map(ToOwned::to_owned).collect(); let mut c = PluginCommand { source: message.source_nickname().unwrap().to_string(), target: message.response_target().unwrap().to_string(), tokens: t, }; self.exec(server, &mut c) } else { Ok(()) } } fn command(&self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> { if command.tokens.is_empty() { return self.invalid_command(server, &command); } let sub_command = command.tokens.remove(0); match sub_command.as_ref() { "add" => self.add(server, &mut command), "get" => self.get(server, &mut command), "exec" => self.exec(server, &mut command), _ => self.invalid_command(server, &command), } } }