From bea4d2a63b15eb3f908d8612bdc94dca1829229f Mon Sep 17 00:00:00 2001 From: Jokler Date: Sat, 14 Oct 2017 04:44:26 +0200 Subject: Improve readability of the Currency plugin --- src/plugins/currency.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index bb16cd9..c6f0b2f 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -82,6 +82,11 @@ impl Currency { } fn convert(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { + + if command.tokens.len() < 3 { + return self.invalid_command(server, &command); + } + let request = match self.eval_command(&command.tokens) { Some(request) => request, None => { @@ -103,7 +108,7 @@ impl Currency { } } - fn help(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { + fn help(&self, server: &IrcServer, command: &mut PluginCommand) -> Result<(), IrcError> { let help = format!("usage: {} currency value from_currency to_currency\r\n\ example: 1.5 eur usd\r\n\ available currencies: AUD, BGN, BRL, CAD, \ @@ -117,7 +122,7 @@ impl Currency { } fn invalid_command(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> { - let help = format!("Incorrect value. \ + let help = format!("Incorrect Command. \ Send \"{} help currency\" for help.", server.current_nickname()); @@ -134,18 +139,15 @@ impl Plugin for Currency { Ok(()) } - fn command(&mut self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { - if command.tokens.is_empty() { - self.invalid_command(server, &command) + fn command(&mut self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> { - } else if command.tokens[0].to_lowercase() == "help" { - self.help(server, command) - - } else if command.tokens.len() >= 3 { - self.convert(server, command) + if command.tokens.is_empty() { + return self.invalid_command(server, &command); + } - } else { - self.invalid_command(server, &command) + match command.tokens[0].as_ref() { + "help" => self.help(server, &mut command), + _ => self.convert(server, command), } } } -- cgit v1.2.3-70-g09d2 From 4130d9b299265df7c06a9dcfa426746a870be615 Mon Sep 17 00:00:00 2001 From: Jokler Date: Sat, 14 Oct 2017 04:46:05 +0200 Subject: Adjust formatting with cargo fmt --- src/lib.rs | 14 ++++++++------ src/plugins/currency.rs | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'src/plugins') diff --git a/src/lib.rs b/src/lib.rs index c556a32..873358a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,7 +105,9 @@ pub fn run() { if let Some(mut c) = command.clone() { // Skip empty commands - if c.tokens.is_empty() { continue; } + if c.tokens.is_empty() { + continue; + } if lock_plugin!(plugin).name().to_lowercase() == c.tokens[0].to_lowercase() { @@ -132,10 +134,7 @@ fn get_command(nick: &str, message: &Message) -> Option { if let PRIVMSG(_, ref content) = message.command { // Split content by spaces and filter empty tokens - let mut tokens: Vec = content - .split(' ') - .map(ToOwned::to_owned) - .collect(); + let mut tokens: Vec = content.split(' ').map(ToOwned::to_owned).collect(); // Commands start with our name if tokens[0].to_lowercase().starts_with(nick) { @@ -145,7 +144,10 @@ fn get_command(nick: &str, message: &Message) -> Option { // We assume that only ':' and ',' are used as suffixes on IRC // If there are any other chars we assume that it is not ment for the bot - tokens[0] = tokens[0].chars().filter(|&c| !":,".contains(c)).collect(); + tokens[0] = tokens[0] + .chars() + .filter(|&c| !":,".contains(c)) + .collect(); if !tokens[0].is_empty() { return None; } diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index c6f0b2f..bc87592 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -116,7 +116,7 @@ impl Currency { IDR, ILS, INR, JPY, KRW, MXN, MYR, NOK, \ NZD, PHP, PLN, RON, RUB, SEK, SGD, THB, \ TRY, USD, ZAR", - server.current_nickname()); + server.current_nickname()); server.send_notice(&command.source, &help) } -- cgit v1.2.3-70-g09d2 From 44cec3059fcc5697f7347260eb77a952cfca18ab Mon Sep 17 00:00:00 2001 From: Jokler Date: Tue, 17 Oct 2017 05:14:40 +0200 Subject: Remove a few unwraps and make error handling more concise --- src/lib.rs | 43 ++++++++++++++++++++----------------------- src/plugins/currency.rs | 7 +------ 2 files changed, 21 insertions(+), 29 deletions(-) (limited to 'src/plugins') diff --git a/src/lib.rs b/src/lib.rs index cc9f016..da36d91 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,7 +64,7 @@ pub fn run() { Ok(v) => configs.push(v), Err(e) => error!("Incorrect config file {}", e), } - }, + } Err(e) => error!("Failed to read path {}", e), } } @@ -94,24 +94,16 @@ pub fn run() { // Open a connection and add work for each config for config in configs { - let fut = match IrcServer::new_future(reactor.handle(), &config) { - Ok(v) => v, - Err(e) => { - error!("Failed to connect: {}", e); - return; - } - }; + let server = + match IrcServer::new_future(reactor.handle(), &config).and_then(|f| reactor.run(f)) { + Ok(v) => v, + Err(e) => { + error!("Failed to connect: {}", e); + return; + } + }; - let server = match reactor.run(fut) { - Ok(v) => { - info!("Connected to server"); - v - } - Err(e) => { - error!("Failed to connect: {}", e); - return; - } - }; + info!("Connected to server"); match server.identify() { Ok(_) => info!("Identified"), @@ -125,7 +117,7 @@ pub fn run() { let task = server .stream() .for_each(move |message| process_msg(&server, &plugin_names, plugins.clone(), message)) - .map_err(|e| Err(e).unwrap()); + .map_err(|e| error!("Failed to process message: {}", e)); reactor.handle().spawn(task); } @@ -154,10 +146,10 @@ fn process_msg(server: &IrcServer, 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(); + server.send_notice(&c.source, &help)?; } else if "help" == &c.tokens[0].to_lowercase() { - send_help_message(server, c).unwrap(); + send_help_message(server, c)?; } else if !plugin_names.contains(&c.tokens[0].to_lowercase()) { @@ -166,7 +158,7 @@ fn process_msg(server: &IrcServer, server.current_nickname(), c.tokens[0]); - server.send_notice(&c.source, &help).unwrap(); + server.send_notice(&c.source, &help)?; } } @@ -181,7 +173,12 @@ fn process_msg(server: &IrcServer, let server = server.clone(); // Execute the plugin in another thread - spawn(move || { lock_plugin!(plugin).execute(&server, &message).unwrap(); }); + spawn(move || { + if let Err(e) = lock_plugin!(plugin).execute(&server, &message) { + let name = lock_plugin!(plugin).name().to_string(); + error!("Error in {} - {}", name, e); + }; + }); } // Check if the command is for this plugin diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index bc87592..0db8240 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -67,12 +67,7 @@ impl Currency { } fn eval_command<'a>(&self, tokens: &'a [String]) -> Option> { - let parsed = match tokens[0].parse() { - Ok(v) => v, - Err(_) => { - return None; - } - }; + let parsed = tokens[0].parse().ok()?; Some(ConvertionRequest { value: parsed, -- cgit v1.2.3-70-g09d2 From 8802b4a2839cb2e4d4cd2ed3d3e1b27ae4b250e6 Mon Sep 17 00:00:00 2001 From: Jokler Date: Tue, 17 Oct 2017 05:28:21 +0200 Subject: Handle one error in a verbose way again to support rust stable --- src/plugins/currency.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index 0db8240..62ba847 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -67,13 +67,15 @@ impl Currency { } fn eval_command<'a>(&self, tokens: &'a [String]) -> Option> { - let parsed = tokens[0].parse().ok()?; - - Some(ConvertionRequest { - value: parsed, - source: &tokens[1], - target: &tokens[2], - }) + if let Some(parsed) = tokens[0].parse().ok() { + Some(ConvertionRequest { + value: parsed, + source: &tokens[1], + target: &tokens[2], + }) + } else { + None + } } fn convert(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { -- cgit v1.2.3-70-g09d2 From 8c51489bdd2d0c383c7d5b8e490cf2728702ba7b Mon Sep 17 00:00:00 2001 From: Jokler Date: Wed, 18 Oct 2017 15:42:00 +0200 Subject: Pass on a Result from the Currency eval function --- src/plugins/currency.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index 62ba847..b15c852 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -4,8 +4,11 @@ extern crate serde_json; extern crate regex; use std::io::Read; +use std::num::ParseFloatError; + use irc::client::prelude::*; use irc::error::Error as IrcError; + use self::reqwest::Client; use self::reqwest::header::Connection; use self::serde_json::Value; @@ -66,16 +69,12 @@ impl Currency { Currency {} } - fn eval_command<'a>(&self, tokens: &'a [String]) -> Option> { - if let Some(parsed) = tokens[0].parse().ok() { - Some(ConvertionRequest { - value: parsed, - source: &tokens[1], - target: &tokens[2], - }) - } else { - None - } + fn eval_command<'a>(&self, tokens: &'a [String]) -> Result, ParseFloatError> { + Ok(ConvertionRequest { + value: tokens[0].parse()?, + source: &tokens[1], + target: &tokens[2], + }) } fn convert(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { @@ -85,8 +84,8 @@ impl Currency { } let request = match self.eval_command(&command.tokens) { - Some(request) => request, - None => { + Ok(request) => request, + Err(_) => { return self.invalid_command(server, &command); } }; -- cgit v1.2.3-70-g09d2 From 7e3b3fa74f5ddf9d69538b2c4f87e0288d0dcb9e Mon Sep 17 00:00:00 2001 From: Jokler Date: Thu, 26 Oct 2017 22:33:02 +0200 Subject: Make the Emoji plugin respond to messages in private chats --- src/plugins/emoji.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/plugins') diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs index d2ed956..5d37feb 100644 --- a/src/plugins/emoji.rs +++ b/src/plugins/emoji.rs @@ -67,7 +67,9 @@ impl Plugin for Emoji { fn execute(&mut self, server: &IrcServer, message: &Message) -> Result<(), IrcError> { match message.command { - Command::PRIVMSG(ref target, ref content) => self.emoji(server, content, target), + Command::PRIVMSG(_, ref content) => { + self.emoji(server, content, message.response_target().unwrap()) + } _ => Ok(()), } } -- cgit v1.2.3-70-g09d2 From 4c6f2f088521cf0876b67f21a25e524983b7ada7 Mon Sep 17 00:00:00 2001 From: Jokler Date: Fri, 27 Oct 2017 15:35:30 +0200 Subject: Fix currency response on bad command --- src/plugins/currency.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/plugins') diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index b15c852..fd447e3 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -119,7 +119,7 @@ impl Currency { fn invalid_command(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> { let help = format!("Incorrect Command. \ - Send \"{} help currency\" for help.", + Send \"{} currency help\" for help.", server.current_nickname()); server.send_notice(&command.source, &help) -- cgit v1.2.3-70-g09d2 From afa5eb93e9e14b65e3c846e70e22c6134ca95f80 Mon Sep 17 00:00:00 2001 From: Jokler Date: Fri, 27 Oct 2017 15:45:29 +0200 Subject: Panic if execute is called on the Currency Plugin --- src/plugins/currency.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/plugins') diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index fd447e3..78ae593 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -132,7 +132,7 @@ impl Plugin for Currency { } fn execute(&mut self, _: &IrcServer, _: &Message) -> Result<(), IrcError> { - Ok(()) + panic!("Currency does not implement the execute function!") } fn command(&mut self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> { -- cgit v1.2.3-70-g09d2 From 8b481420cd3c0dd02cb5671e753dc5ff4a40e7eb Mon Sep 17 00:00:00 2001 From: Jokler Date: Fri, 27 Oct 2017 15:55:11 +0200 Subject: Move help into a Plugin --- src/lib.rs | 1 + src/plugin.rs | 8 -------- src/plugins/mod.rs | 1 + 3 files changed, 2 insertions(+), 8 deletions(-) (limited to 'src/plugins') diff --git a/src/lib.rs b/src/lib.rs index 55b121e..5b8d479 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,6 +65,7 @@ pub fn run() { // The list of plugins in use let mut plugins = ThreadedPlugins::new(); + plugins.add(plugins::help::Help::new()); plugins.add(plugins::emoji::Emoji::new()); plugins.add(plugins::currency::Currency::new()); info!("Plugins loaded: {}", plugins); diff --git a/src/plugin.rs b/src/plugin.rs index d1a1f3d..e0a4ce2 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -121,10 +121,6 @@ impl ThreadedPlugins { return server.send_notice(&command.source, &help); } - if &command.tokens[0].to_lowercase() == "help" { - return self.send_help_message(server, &command); - } - // Check if the command is for this plugin if let Some(plugin) = self.plugins.get(&command.tokens[0].to_lowercase()) { @@ -151,10 +147,6 @@ impl ThreadedPlugins { server.send_notice(&command.source, &help) } } - - fn send_help_message(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> { - server.send_notice(&command.source, "Help has not been added yet.") - } } impl fmt::Display for ThreadedPlugins { diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index adf54b2..d07ead6 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -1,2 +1,3 @@ +pub mod help; pub mod emoji; pub mod currency; -- cgit v1.2.3-70-g09d2 From 2b943fa26f1d3c199cbc03e65c567e5c29887465 Mon Sep 17 00:00:00 2001 From: Jokler Date: Fri, 27 Oct 2017 18:19:39 +0200 Subject: Add emoji counting --- src/plugins/emoji.rs | 67 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 14 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs index 5d37feb..99f6c26 100644 --- a/src/plugins/emoji.rs +++ b/src/plugins/emoji.rs @@ -1,43 +1,82 @@ extern crate unicode_names; +use std::fmt; + use irc::client::prelude::*; use irc::error::Error as IrcError; use plugin::*; +struct EmojiHandle { + symbol: char, + count: i32, +} + +impl fmt::Display for EmojiHandle { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + + let name = match unicode_names::name(self.symbol) { + Some(sym) => sym.to_string().to_lowercase(), + None => String::from("UNKNOWN"), + }; + + if self.count > 1 { + write!(f, "{}x {}", self.count, name) + } else { + write!(f, "{}", name) + } + } +} + #[derive(PluginName, Debug)] pub struct Emoji; + impl Emoji { pub fn new() -> Emoji { Emoji {} } fn emoji(&self, server: &IrcServer, content: &str, target: &str) -> Result<(), IrcError> { - - let mut names: Vec = Vec::new(); - for emoji in self.return_emojis(content) { - - let name = match unicode_names::name(emoji) { - Some(v) => format!("{}", v).to_lowercase(), - None => "UNKNOWN".to_string(), - }; - - names.push(name); - } + let names = self.return_emojis(content) + .iter() + .map(|e| e.to_string()) + .collect::>(); server.send_privmsg(target, &names.join(", ")) } - fn return_emojis(&self, string: &str) -> Vec { + fn return_emojis(&self, string: &str) -> Vec { + + let mut emojis: Vec = Vec::new(); + + let mut current = EmojiHandle { + symbol: ' ', + count: 0, + }; - let mut emojis: Vec = Vec::new(); for c in string.chars() { if self.is_emoji(&c) { - emojis.push(c); + if current.symbol == c { + current.count = current.count + 1; + + } else { + if current.count > 0 { + emojis.push(current); + } + + current = EmojiHandle { + symbol: c, + count: 1, + } + } } } + if current.count > 0 { + emojis.push(current); + } + emojis } -- cgit v1.2.3-70-g09d2 From 9d77de5e5d133b990983040b01cc0cb22b2f526b Mon Sep 17 00:00:00 2001 From: Jokler Date: Fri, 27 Oct 2017 20:24:44 +0200 Subject: Add missing help plugin --- src/plugins/help.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/plugins/help.rs (limited to 'src/plugins') diff --git a/src/plugins/help.rs b/src/plugins/help.rs new file mode 100644 index 0000000..c4ddcd4 --- /dev/null +++ b/src/plugins/help.rs @@ -0,0 +1,34 @@ +use irc::client::prelude::*; +use irc::error::Error as IrcError; + +use plugin::*; + +#[derive(PluginName, Debug)] +pub struct Help; + +impl Help { + pub fn new() -> Help { + Help {} + } + + fn help(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { + server.send_notice(&command.source, "Help has not been added yet.") + } +} + +impl Plugin for Help { + fn is_allowed(&self, _: &IrcServer, _: &Message) -> bool { + false + } + + fn execute(&mut self, _: &IrcServer, _: &Message) -> Result<(), IrcError> { + panic!("Help does not implement the execute function!") + } + + fn command(&mut self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { + self.help(server, command) + } +} + +#[cfg(test)] +mod tests {} -- cgit v1.2.3-70-g09d2 From d203f2689ac7addf36a32150691f459212199f09 Mon Sep 17 00:00:00 2001 From: Jokler Date: Sat, 28 Oct 2017 23:34:30 +0200 Subject: Reformat the emoji counting loop --- src/plugins/emoji.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs index 99f6c26..09d5c27 100644 --- a/src/plugins/emoji.rs +++ b/src/plugins/emoji.rs @@ -46,7 +46,6 @@ impl Emoji { } fn return_emojis(&self, string: &str) -> Vec { - let mut emojis: Vec = Vec::new(); let mut current = EmojiHandle { @@ -56,19 +55,21 @@ impl Emoji { for c in string.chars() { - if self.is_emoji(&c) { - if current.symbol == c { - current.count = current.count + 1; - - } else { - if current.count > 0 { - emojis.push(current); - } - - current = EmojiHandle { - symbol: c, - count: 1, - } + if !self.is_emoji(&c) { + continue; + } + + if current.symbol == c { + current.count += 1; + + } else { + if current.count > 0 { + emojis.push(current); + } + + current = EmojiHandle { + symbol: c, + count: 1, } } } -- cgit v1.2.3-70-g09d2 From fa558e306d33750b391759affb9628b31693e572 Mon Sep 17 00:00:00 2001 From: Jokler Date: Sun, 29 Oct 2017 02:41:47 +0100 Subject: Export plugins directly instead of their modules --- src/lib.rs | 6 +++--- src/plugins/mod.rs | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'src/plugins') diff --git a/src/lib.rs b/src/lib.rs index d912973..324e273 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,9 +69,9 @@ pub fn run() { // The list of plugins in use let mut plugins = ThreadedPlugins::new(); - plugins.add(plugins::help::Help::new()); - plugins.add(plugins::emoji::Emoji::new()); - plugins.add(plugins::currency::Currency::new()); + plugins.add(plugins::Help::new()); + plugins.add(plugins::Emoji::new()); + plugins.add(plugins::Currency::new()); info!("Plugins loaded: {}", plugins); // Create an event loop to run the connections on. diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index d07ead6..0dea596 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -1,3 +1,7 @@ -pub mod help; -pub mod emoji; -pub mod currency; +mod help; +mod emoji; +mod currency; + +pub use self::help::Help; +pub use self::emoji::Emoji; +pub use self::currency::Currency; -- cgit v1.2.3-70-g09d2