From f7f86cf06de62598ed635b154c45cdc8fe4e19da Mon Sep 17 00:00:00 2001 From: Jokler Date: Mon, 9 Oct 2017 01:56:04 +0200 Subject: Add command functionality --- src/lib.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index b0c1615..4cb6c44 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,8 +2,6 @@ #![cfg_attr(feature="clippy", plugin(clippy))] extern crate irc; -#[macro_use] -extern crate lazy_static; #[macro_use] mod plugin; @@ -12,9 +10,18 @@ mod plugins; use std::thread::spawn; use std::sync::{Arc, Mutex}; use irc::client::prelude::*; +use irc::proto::Command::PRIVMSG; +use irc::error::Error as IrcError; use plugin::Plugin; +#[derive(Clone, Debug)] +pub struct PluginCommand { + source: String, + target: String, + tokens: Vec, +} + pub fn run() { let server = IrcServer::new("config.toml").unwrap(); server.identify().unwrap(); @@ -23,13 +30,39 @@ pub fn run() { vec![Arc::new(Mutex::new(plugins::emoji::Emoji::new())), Arc::new(Mutex::new(plugins::currency::Currency::new()))]; + let plugin_names: Vec = plugins + .iter() + .map(|p| p.lock().unwrap().to_string().to_lowercase()) + .collect(); + server .for_each_incoming(|message| { let message = Arc::new(message); + let command = get_command(&server.current_nickname().to_lowercase(), &message); + + if let Some(ref c) = command { + if c.tokens.is_empty() { + let help = format!("Use \"{} help\" to get help", server.current_nickname()); + server.send_notice(&c.source, &help).unwrap(); + + } else if "help" == &c.tokens[0].to_lowercase() { + send_help_message(&server, c).unwrap(); + + } else if !plugin_names.contains(&c.tokens[0].to_lowercase()) { + + let help = format!("\"{} {}\" is not a command, \ + try \"{0} help\" instead.", + server.current_nickname(), + c.tokens[0]); + + server.send_notice(&c.source, &help).unwrap(); + } + } for plugin in plugins.clone() { let server = server.clone(); let message = Arc::clone(&message); + let command = command.clone(); spawn(move || { let mut plugin = match plugin.lock() { @@ -40,12 +73,53 @@ pub fn run() { if plugin.is_allowed(&server, &message) { plugin.execute(&server, &message).unwrap(); } + + if let Some(mut c) = command { + if !c.tokens.is_empty() && + plugin.to_string().to_lowercase() == c.tokens[0].to_lowercase() { + + c.tokens.remove(0); + plugin.command(&server, c).unwrap(); + } + } }); } }) .unwrap(); } +fn send_help_message(server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> { + server.send_notice(&command.source, "Help has not been added yet.") +} + +fn get_command(nick: &str, message: &Message) -> Option { + if let PRIVMSG(_, ref content) = message.command { + let mut tokens: Vec = content + .split(' ') + .filter(|&x| !x.is_empty()) + .map(ToOwned::to_owned) + .collect(); + + if tokens.is_empty() { + return None; + } + + if tokens[0].to_lowercase().starts_with(nick) { + tokens.remove(0); + + Some(PluginCommand { + source: message.source_nickname().unwrap().to_string(), + target: message.response_target().unwrap().to_string(), + tokens: tokens, + }) + } else { + None + } + } else { + None + } +} + #[cfg(test)] mod tests { use irc::client::prelude::*; -- cgit v1.2.3-70-g09d2