summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2018-02-10 14:13:07 +0100
committerJokler <jokler.contact@gmail.com>2018-02-10 14:13:07 +0100
commit2ba26a37d27a637b7c0e02970419342a6f83462b (patch)
treec7d2323ddf7a0aa58d1b680200dc0acb9dad8558
parent1f69bfef7f2fd5fdc8787485d81461d68aa2d3ba (diff)
downloadfrippy-2ba26a37d27a637b7c0e02970419342a6f83462b.tar.gz
frippy-2ba26a37d27a637b7c0e02970419342a6f83462b.zip
Add evaluate function to plugins
This allows plugins to be used in combination with each other.
-rw-r--r--src/plugin.rs2
-rw-r--r--src/plugins/currency.rs58
-rw-r--r--src/plugins/emoji.rs21
-rw-r--r--src/plugins/help.rs10
-rw-r--r--src/plugins/keepnick.rs4
-rw-r--r--src/plugins/url.rs29
6 files changed, 79 insertions, 45 deletions
diff --git a/src/plugin.rs b/src/plugin.rs
index d14c129..63d3530 100644
--- a/src/plugin.rs
+++ b/src/plugin.rs
@@ -13,6 +13,8 @@ pub trait Plugin: PluginName + Send + Sync + fmt::Debug {
fn execute(&self, server: &IrcServer, message: &Message) -> Result<(), IrcError>;
/// Handles any command directed at this plugin.
fn command(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError>;
+ /// Should work like command but return a String instead of sending messages to IRC.
+ fn evaluate(&self, server: &IrcServer, command: PluginCommand) -> Result<String, String>;
}
/// `PluginName` is required by `Plugin`.
diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs
index 634faa2..4d44d9a 100644
--- a/src/plugins/currency.rs
+++ b/src/plugins/currency.rs
@@ -68,24 +68,26 @@ impl Currency {
Currency {}
}
- fn eval_command<'a>(&self, tokens: &'a [String]) -> Result<ConvertionRequest<'a>, ParseFloatError> {
+ fn eval_command<'a>(&self,
+ tokens: &'a [String])
+ -> Result<ConvertionRequest<'a>, ParseFloatError> {
Ok(ConvertionRequest {
- value: tokens[0].parse()?,
- source: &tokens[1],
- target: &tokens[2],
- })
+ value: tokens[0].parse()?,
+ source: &tokens[1],
+ target: &tokens[2],
+ })
}
- fn convert(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> {
+ fn convert(&self, server: &IrcServer, command: &mut PluginCommand) -> Result<String, String> {
if command.tokens.len() < 3 {
- return self.invalid_command(server, &command);
+ return Err(self.invalid_command(server));
}
let request = match self.eval_command(&command.tokens) {
Ok(request) => request,
Err(_) => {
- return self.invalid_command(server, &command);
+ return Err(self.invalid_command(server));
}
};
@@ -97,31 +99,27 @@ impl Currency {
response / 1.00000000,
request.target.to_lowercase());
- server.send_privmsg(&command.target, &response)
+ Ok(response)
}
- None => server.send_notice(&command.source, "Error while converting given currency"),
+ None => Err(String::from("An error occured during the conversion of the given currency")),
}
}
- fn help(&self, server: &IrcServer, command: &mut PluginCommand) -> Result<(), IrcError> {
- let help = format!("usage: {} currency value from_currency to_currency\r\n\
+ fn help(&self, server: &IrcServer) -> String {
+ format!("usage: {} currency value from_currency to_currency\r\n\
example: 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());
-
- server.send_notice(&command.source, &help)
+ server.current_nickname())
}
- fn invalid_command(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> {
- let help = format!("Incorrect Command. \
+ fn invalid_command(&self, server: &IrcServer) -> String {
+ format!("Incorrect Command. \
Send \"{} currency help\" for help.",
- server.current_nickname());
-
- server.send_notice(&command.source, &help)
+ server.current_nickname())
}
}
@@ -137,12 +135,26 @@ impl Plugin for Currency {
fn command(&self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> {
if command.tokens.is_empty() {
- return self.invalid_command(server, &command);
+ return server.send_notice(&command.source, &self.invalid_command(server));
+ }
+
+ 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),
+ }
+ }
+ }
+
+ fn evaluate(&self, server: &IrcServer, mut command: PluginCommand) -> Result<String, String>{
+ if command.tokens.is_empty() {
+ return Err(self.invalid_command(server));
}
match command.tokens[0].as_ref() {
- "help" => self.help(server, &mut command),
- _ => self.convert(server, command),
+ "help" => Ok(self.help(server)),
+ _ => self.convert(server, &mut command),
}
}
}
diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs
index 59e2fdd..512a62e 100644
--- a/src/plugins/emoji.rs
+++ b/src/plugins/emoji.rs
@@ -36,13 +36,12 @@ impl Emoji {
Emoji {}
}
- fn emoji(&self, server: &IrcServer, content: &str, target: &str) -> Result<(), IrcError> {
- let names = self.return_emojis(content)
+ fn emoji(&self, content: &str) -> String {
+ self.return_emojis(content)
.iter()
.map(|e| e.to_string())
- .collect::<Vec<String>>();
-
- server.send_privmsg(target, &names.join(", "))
+ .collect::<Vec<String>>()
+ .join(", ")
}
fn return_emojis(&self, string: &str) -> Vec<EmojiHandle> {
@@ -108,7 +107,8 @@ impl Plugin for Emoji {
fn execute(&self, server: &IrcServer, message: &Message) -> Result<(), IrcError> {
match message.command {
Command::PRIVMSG(_, ref content) => {
- self.emoji(server, content, message.response_target().unwrap())
+ server.send_privmsg(message.response_target().unwrap(),
+ &self.emoji(content))
}
_ => Ok(()),
}
@@ -118,6 +118,15 @@ impl Plugin for Emoji {
server.send_notice(&command.source,
"This Plugin does not implement any commands.")
}
+
+ fn evaluate(&self, _: &IrcServer, command: PluginCommand) -> Result<String, String> {
+ let emojis = self.emoji(&command.tokens[0]);
+ if emojis.is_empty() {
+ Ok(emojis)
+ } else {
+ Err(String::from("No emojis were found."))
+ }
+ }
}
#[cfg(test)]
diff --git a/src/plugins/help.rs b/src/plugins/help.rs
index 7b987d4..cea325f 100644
--- a/src/plugins/help.rs
+++ b/src/plugins/help.rs
@@ -10,10 +10,6 @@ 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 {
@@ -26,7 +22,11 @@ impl Plugin for Help {
}
fn command(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> {
- self.help(server, command)
+ server.send_notice(&command.source, "Help has not been added yet.")
+ }
+
+ fn evaluate(&self, _: &IrcServer, _: PluginCommand) -> Result<String, String> {
+ Err(String::from("Help has not been added yet."))
}
}
diff --git a/src/plugins/keepnick.rs b/src/plugins/keepnick.rs
index 1d4627d..2970857 100644
--- a/src/plugins/keepnick.rs
+++ b/src/plugins/keepnick.rs
@@ -54,6 +54,10 @@ impl Plugin for KeepNick {
server.send_notice(&command.source,
"This Plugin does not implement any commands.")
}
+
+ fn evaluate(&self, _: &IrcServer, _: PluginCommand) -> Result<String, String> {
+ Err(String::from("This Plugin does not implement any commands."))
+ }
}
#[cfg(test)]
diff --git a/src/plugins/url.rs b/src/plugins/url.rs
index f6e06f3..ab36833 100644
--- a/src/plugins/url.rs
+++ b/src/plugins/url.rs
@@ -95,11 +95,11 @@ impl Url {
}
}
- fn url(&self, server: &IrcServer, message: &str, target: &str) -> Result<(), IrcError> {
- let url = match self.grep_url(message) {
+ fn url(&self, text: &str) -> Result<String, &str> {
+ let url = match self.grep_url(text) {
Some(url) => url,
None => {
- return Ok(());
+ return Err("No Url was found.")
}
};
@@ -109,18 +109,18 @@ impl Url {
let doc = Document::from(body.as_ref());
if let Some(title) = doc.find(Name("title")).next() {
- let text = title.children().next().unwrap();
- let message = text.as_text().unwrap().trim().replace("\n", "|");
- debug!("Title: {:?}", text);
- debug!("Message: {:?}", message);
+ let title = title.children().next().unwrap();
+ let title_text = title.as_text().unwrap().trim().replace("\n", "|");
+ debug!("Title: {:?}", title);
+ debug!("Text: {:?}", title_text);
- server.send_privmsg(target, &message)
+ Ok(title_text)
} else {
- Ok(())
+ Err("No title was found.")
}
}
- None => Ok(()),
+ None => Err("Failed to download document.")
}
}
}
@@ -136,7 +136,10 @@ impl Plugin for Url {
fn execute(&self, server: &IrcServer, message: &Message) -> Result<(), IrcError> {
match message.command {
Command::PRIVMSG(_, ref content) => {
- self.url(server, content, message.response_target().unwrap())
+ match self.url(content) {
+ Ok(title) => server.send_privmsg(&message.response_target().unwrap(), &title),
+ Err(_) => Ok(()),
+ }
}
_ => Ok(()),
}
@@ -146,6 +149,10 @@ impl Plugin for Url {
server.send_notice(&command.source,
"This Plugin does not implement any commands.")
}
+
+ fn evaluate(&self, _: &IrcServer, command: PluginCommand) -> Result<String, String> {
+ self.url(&command.tokens[0]).map_err(|e| String::from(e))
+ }
}
#[cfg(test)]