summaryrefslogtreecommitdiffstats
path: root/src/plugins/emoji.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/emoji.rs')
-rw-r--r--src/plugins/emoji.rs62
1 files changed, 36 insertions, 26 deletions
diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs
index 1bb714c..f1d9376 100644
--- a/src/plugins/emoji.rs
+++ b/src/plugins/emoji.rs
@@ -3,10 +3,14 @@ extern crate unicode_names;
use std::fmt;
use irc::client::prelude::*;
-use irc::error::Error as IrcError;
use plugin::*;
+use error::FrippyError;
+use error::ErrorKind as FrippyErrorKind;
+use failure::Fail;
+use failure::ResultExt;
+
struct EmojiHandle {
symbol: char,
count: i32,
@@ -14,7 +18,6 @@ struct EmojiHandle {
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"),
@@ -28,7 +31,7 @@ impl fmt::Display for EmojiHandle {
}
}
-#[derive(PluginName, Debug)]
+#[derive(PluginName, Default, Debug)]
pub struct Emoji;
impl Emoji {
@@ -36,13 +39,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> {
@@ -53,7 +55,6 @@ impl Emoji {
count: 0,
};
-
for c in string.chars() {
if !self.is_emoji(&c) {
continue;
@@ -61,7 +62,6 @@ impl Emoji {
if current.symbol == c {
current.count += 1;
-
} else {
if current.count > 0 {
emojis.push(current);
@@ -98,27 +98,37 @@ impl Emoji {
}
impl Plugin for Emoji {
- fn is_allowed(&self, _: &IrcServer, message: &Message) -> bool {
+ fn execute(&self, client: &IrcClient, message: &Message) -> ExecutionStatus {
match message.command {
- Command::PRIVMSG(_, _) => true,
- _ => false,
+ Command::PRIVMSG(_, ref content) => match client
+ .send_privmsg(message.response_target().unwrap(), &self.emoji(content))
+ {
+ Ok(_) => ExecutionStatus::Done,
+ Err(e) => ExecutionStatus::Err(e.context(FrippyErrorKind::Connection).into()),
+ },
+ _ => ExecutionStatus::Done,
}
}
- fn execute(&self, server: &IrcServer, message: &Message) -> Result<(), IrcError> {
- match message.command {
- Command::PRIVMSG(_, ref content) => {
- self.emoji(server, content, message.response_target().unwrap())
- }
- _ => Ok(()),
- }
+ fn execute_threaded(&self, _: &IrcClient, _: &Message) -> Result<(), FrippyError> {
+ panic!("Emoji should not use threading")
}
- fn command(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> {
- server.send_notice(&command.source,
- "This Plugin does not implement any commands.")
+ fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), FrippyError> {
+ Ok(client
+ .send_notice(
+ &command.source,
+ "This Plugin does not implement any commands.",
+ )
+ .context(FrippyErrorKind::Connection)?)
}
-}
-#[cfg(test)]
-mod tests {}
+ fn evaluate(&self, _: &IrcClient, 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."))
+ }
+ }
+}