From da5c2c8e4893bfb095a8e2122b943c4dca61c41d Mon Sep 17 00:00:00 2001 From: Jokler Date: Mon, 12 Feb 2018 19:16:59 +0100 Subject: Replace is_allowed with a single-threaded execute function The old execute got renamed to exeute_threaded. --- src/plugins/currency.rs | 43 +++++++++++++++++++++---------------------- src/plugins/emoji.rs | 26 +++++++++++++------------- src/plugins/help.rs | 12 ++++++------ src/plugins/keepnick.rs | 42 +++++++++++++++++++++--------------------- src/plugins/url.rs | 22 +++++++++++++--------- 5 files changed, 74 insertions(+), 71 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index 32a2506..21330a1 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -78,16 +78,15 @@ impl Currency { }) } - fn convert(&self, server: &IrcClient, command: &mut PluginCommand) -> Result { - + fn convert(&self, client: &IrcClient, command: &mut PluginCommand) -> Result { if command.tokens.len() < 3 { - return Err(self.invalid_command(server)); + return Err(self.invalid_command(client)); } let request = match self.eval_command(&command.tokens) { Ok(request) => request, Err(_) => { - return Err(self.invalid_command(server)); + return Err(self.invalid_command(client)); } }; @@ -105,56 +104,56 @@ impl Currency { } } - fn help(&self, server: &IrcClient) -> String { + fn help(&self, client: &IrcClient) -> String { format!("usage: {} currency value from_currency to_currency\r\n\ - example: 1.5 eur usd\r\n\ + example: {0} currency 1.5 eur usd\r\n\ available currencies: AUD, BGN, BRL, CAD, \ CHF, CNY, CZK, DKK, GBP, HKD, HRK, HUF, \ IDR, ILS, INR, JPY, KRW, MXN, MYR, NOK, \ NZD, PHP, PLN, RON, RUB, SEK, SGD, THB, \ TRY, USD, ZAR", - server.current_nickname()) + client.current_nickname()) } - fn invalid_command(&self, server: &IrcClient) -> String { + fn invalid_command(&self, client: &IrcClient) -> String { format!("Incorrect Command. \ Send \"{} currency help\" for help.", - server.current_nickname()) + client.current_nickname()) } } impl Plugin for Currency { - fn is_allowed(&self, _: &IrcClient, _: &Message) -> bool { - false + fn execute(&self, _: &IrcClient, _: &Message) -> ExecutionStatus { + ExecutionStatus::Done } - fn execute(&self, _: &IrcClient, _: &Message) -> Result<(), IrcError> { + fn execute_threaded(&self, _: &IrcClient, _: &Message) -> Result<(), IrcError> { panic!("Currency does not implement the execute function!") } - fn command(&self, server: &IrcClient, mut command: PluginCommand) -> Result<(), IrcError> { + fn command(&self, client: &IrcClient, mut command: PluginCommand) -> Result<(), IrcError> { if command.tokens.is_empty() { - return server.send_notice(&command.source, &self.invalid_command(server)); + return client.send_notice(&command.source, &self.invalid_command(client)); } match command.tokens[0].as_ref() { - "help" => server.send_notice(&command.source, &self.help(server)), - _ => match self.convert(server, &mut command) { - Ok(msg) => server.send_privmsg(&command.target, &msg), - Err(msg) => server.send_notice(&command.source, &msg), + "help" => client.send_notice(&command.source, &self.help(client)), + _ => match self.convert(client, &mut command) { + Ok(msg) => client.send_privmsg(&command.target, &msg), + Err(msg) => client.send_notice(&command.source, &msg), } } } - fn evaluate(&self, server: &IrcClient, mut command: PluginCommand) -> Result{ + fn evaluate(&self, client: &IrcClient, mut command: PluginCommand) -> Result{ if command.tokens.is_empty() { - return Err(self.invalid_command(server)); + return Err(self.invalid_command(client)); } match command.tokens[0].as_ref() { - "help" => Ok(self.help(server)), - _ => self.convert(server, &mut command), + "help" => Ok(self.help(client)), + _ => self.convert(client, &mut command), } } } diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs index c19593d..02a31f8 100644 --- a/src/plugins/emoji.rs +++ b/src/plugins/emoji.rs @@ -97,25 +97,25 @@ impl Emoji { } impl Plugin for Emoji { - fn is_allowed(&self, _: &IrcClient, message: &Message) -> bool { - match message.command { - Command::PRIVMSG(_, _) => true, - _ => false, - } - } - - fn execute(&self, server: &IrcClient, message: &Message) -> Result<(), IrcError> { + fn execute(&self, client: &IrcClient, message: &Message) -> ExecutionStatus { match message.command { Command::PRIVMSG(_, ref content) => { - server.send_privmsg(message.response_target().unwrap(), - &self.emoji(content)) + match client.send_privmsg(message.response_target().unwrap(), + &self.emoji(content)) { + Ok(_) => ExecutionStatus::Done, + Err(e) => ExecutionStatus::Err(e), + } } - _ => Ok(()), + _ => ExecutionStatus::Done, } } - fn command(&self, server: &IrcClient, command: PluginCommand) -> Result<(), IrcError> { - server.send_notice(&command.source, + fn execute_threaded(&self, _: &IrcClient, _: &Message) -> Result<(), IrcError> { + panic!("Emoji should not use threading") + } + + fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), IrcError> { + client.send_notice(&command.source, "This Plugin does not implement any commands.") } diff --git a/src/plugins/help.rs b/src/plugins/help.rs index 1bb15e1..4dd93d7 100644 --- a/src/plugins/help.rs +++ b/src/plugins/help.rs @@ -13,16 +13,16 @@ impl Help { } impl Plugin for Help { - fn is_allowed(&self, _: &IrcClient, _: &Message) -> bool { - false + fn execute(&self, _: &IrcClient, _: &Message) -> ExecutionStatus { + ExecutionStatus::Done } - fn execute(&self, _: &IrcClient, _: &Message) -> Result<(), IrcError> { - panic!("Help does not implement the execute function!") + fn execute_threaded(&self, _: &IrcClient, _: &Message) -> Result<(), IrcError> { + panic!("Help should not use threading") } - fn command(&self, server: &IrcClient, command: PluginCommand) -> Result<(), IrcError> { - server.send_notice(&command.source, "Help has not been added yet.") + fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), IrcError> { + client.send_notice(&command.source, "Help has not been added yet.") } fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result { diff --git a/src/plugins/keepnick.rs b/src/plugins/keepnick.rs index 4a40e8f..e973769 100644 --- a/src/plugins/keepnick.rs +++ b/src/plugins/keepnick.rs @@ -11,47 +11,47 @@ impl KeepNick { KeepNick {} } - fn check_nick(&self, server: &IrcClient, leaver: &str) -> Result<(), IrcError> { - let cfg_nick = match server.config().nickname { + fn check_nick(&self, client: &IrcClient, leaver: &str) -> ExecutionStatus { + let cfg_nick = match client.config().nickname { Some(ref nick) => nick.clone(), - None => return Ok(()), + None => return ExecutionStatus::Done, }; if leaver != cfg_nick { - return Ok(()); + return ExecutionStatus::Done; } - let server_nick = server.current_nickname(); + let client_nick = client.current_nickname(); - if server_nick != cfg_nick { - info!("Trying to switch nick from {} to {}", server_nick, cfg_nick); - server.send(Command::NICK(cfg_nick)) + if client_nick != cfg_nick { + info!("Trying to switch nick from {} to {}", client_nick, cfg_nick); + match client.send(Command::NICK(cfg_nick)) { + Ok(_) => ExecutionStatus::Done, + Err(e) => ExecutionStatus::Err(e), + } } else { - Ok(()) + ExecutionStatus::Done } } } impl Plugin for KeepNick { - fn is_allowed(&self, _: &IrcClient, message: &Message) -> bool { - match message.command { - Command::QUIT(_) => true, - _ => false, - } - } - - fn execute(&self, server: &IrcClient, message: &Message) -> Result<(), IrcError> { + fn execute(&self, client: &IrcClient, message: &Message) -> ExecutionStatus { match message.command { Command::QUIT(ref nick) => { - self.check_nick(server, &nick.clone().unwrap_or_else(|| String::new())) + self.check_nick(client, &nick.clone().unwrap_or_else(String::new)) } - _ => Ok(()), + _ => ExecutionStatus::Done, } } - fn command(&self, server: &IrcClient, command: PluginCommand) -> Result<(), IrcError> { - server.send_notice(&command.source, + fn execute_threaded(&self, _: &IrcClient, _: &Message) -> Result<(), IrcError> { + panic!("Tell should not use threading") + } + + fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), IrcError> { + client.send_notice(&command.source, "This Plugin does not implement any commands.") } diff --git a/src/plugins/url.rs b/src/plugins/url.rs index b980d3e..9ce5a6a 100644 --- a/src/plugins/url.rs +++ b/src/plugins/url.rs @@ -90,7 +90,7 @@ impl Url { } Err(e) => { debug!("Bad response from {:?}: ({})", url, e); - return None; + None } } } @@ -126,18 +126,22 @@ impl Url { } impl Plugin for Url { - fn is_allowed(&self, _: &IrcClient, message: &Message) -> bool { + fn execute(&self, _: &IrcClient, message: &Message) -> ExecutionStatus { match message.command { - Command::PRIVMSG(_, ref msg) => RE.is_match(msg), - _ => false, + Command::PRIVMSG(_, ref msg) => if RE.is_match(msg) { + ExecutionStatus::RequiresThread + } else { + ExecutionStatus::Done + }, + _ => ExecutionStatus::Done, } } - fn execute(&self, server: &IrcClient, message: &Message) -> Result<(), IrcError> { + fn execute_threaded(&self, client: &IrcClient, message: &Message) -> Result<(), IrcError> { match message.command { Command::PRIVMSG(_, ref content) => { match self.url(content) { - Ok(title) => server.send_privmsg(&message.response_target().unwrap(), &title), + Ok(title) => client.send_privmsg(message.response_target().unwrap(), &title), Err(_) => Ok(()), } } @@ -145,13 +149,13 @@ impl Plugin for Url { } } - fn command(&self, server: &IrcClient, command: PluginCommand) -> Result<(), IrcError> { - server.send_notice(&command.source, + fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), IrcError> { + client.send_notice(&command.source, "This Plugin does not implement any commands.") } fn evaluate(&self, _: &IrcClient, command: PluginCommand) -> Result { - self.url(&command.tokens[0]).map_err(|e| String::from(e)) + self.url(&command.tokens[0]).map_err(String::from) } } -- cgit v1.2.3-70-g09d2