diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 4 | ||||
| -rw-r--r-- | src/plugins/emoji.rs | 152 | ||||
| -rw-r--r-- | src/plugins/mod.rs | 2 | ||||
| -rw-r--r-- | src/plugins/unicode.rs | 100 |
4 files changed, 103 insertions, 155 deletions
diff --git a/src/main.rs b/src/main.rs index 03f3c7e..ef24e4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ use std::sync::Arc; use glob::glob; use irc::client::reactor::IrcReactor; -use frippy::plugins::emoji::Emoji; +use frippy::plugins::unicode::Unicode; use frippy::plugins::factoid::Factoid; use frippy::plugins::help::Help; use frippy::plugins::keepnick::KeepNick; @@ -108,7 +108,7 @@ fn run() -> Result<(), Error> { bot.add_plugin(Help::new()); bot.add_plugin(UrlTitles::new(1024)); bot.add_plugin(Sed::new(60)); - bot.add_plugin(Emoji::new()); + bot.add_plugin(Unicode::new()); bot.add_plugin(KeepNick::new()); #[cfg(feature = "mysql")] diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs deleted file mode 100644 index aee61b1..0000000 --- a/src/plugins/emoji.rs +++ /dev/null @@ -1,152 +0,0 @@ -extern crate unicode_names; - -use std::fmt; -use std::marker::PhantomData; - -use irc::client::prelude::*; - -use plugin::*; -use FrippyClient; - -use error::ErrorKind as FrippyErrorKind; -use error::FrippyError; -use failure::Fail; -use failure::ResultExt; - -#[derive(Default, Debug)] -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, Default, Debug)] -pub struct Emoji<C> { - phantom: PhantomData<C>, -} - -impl<C: FrippyClient> Emoji<C> { - pub fn new() -> Self { - Emoji { - phantom: PhantomData, - } - } - - fn emoji(&self, content: &str) -> Option<String> { - let emojis = self.return_emojis(content); - if emojis.is_empty() { - None - } else { - Some( - emojis - .iter() - .map(|e| e.to_string()) - .collect::<Vec<String>>() - .join(", "), - ) - } - } - - fn return_emojis(&self, string: &str) -> Vec<EmojiHandle> { - let mut emojis: Vec<EmojiHandle> = Vec::new(); - - let mut current = EmojiHandle::default(); - - for c in string.chars() { - 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, - } - } - } - - if current.count > 0 { - emojis.push(current); - } - - emojis - } - - fn is_emoji(&self, c: char) -> bool { - // Emoji ranges from stackoverflow: - // https://stackoverflow.com/questions/30757193/find-out-if-character-in-string-is-emoji - match c { '\u{1F600}'...'\u{1F64F}' // Emoticons - | '\u{1F300}'...'\u{1F5FF}' // Misc Symbols and Pictographs - | '\u{1F680}'...'\u{1F6FF}' // Transport and Map - | '\u{2600}' ...'\u{26FF}' // Misc symbols - | '\u{2700}' ...'\u{27BF}' // Dingbats - | '\u{FE00}' ...'\u{FE0F}' // Variation Selectors - | '\u{1F900}'...'\u{1F9FF}' // Supplemental Symbols and Pictographs - | '\u{20D0}' ...'\u{20FF}' => true, // Combining Diacritical Marks for Symbols - _ => false, - } - } -} - -impl<C: FrippyClient> Plugin for Emoji<C> { - type Client = C; - fn execute(&self, client: &Self::Client, message: &Message) -> ExecutionStatus { - match message.command { - Command::PRIVMSG(_, ref content) => { - if let Some(emojis) = self.emoji(content) { - match client.send_privmsg(message.response_target().unwrap(), &emojis) { - Ok(_) => ExecutionStatus::Done, - Err(e) => { - ExecutionStatus::Err(e.context(FrippyErrorKind::Connection).into()) - } - } - } else { - ExecutionStatus::Done - } - } - _ => ExecutionStatus::Done, - } - } - - fn execute_threaded(&self, _: &Self::Client, _: &Message) -> Result<(), FrippyError> { - panic!("Emoji should not use threading") - } - - fn command(&self, client: &Self::Client, command: PluginCommand) -> Result<(), FrippyError> { - client - .send_notice( - &command.source, - "This Plugin does not implement any commands.", - ).context(FrippyErrorKind::Connection)?; - - Ok(()) - } - - fn evaluate(&self, _: &Self::Client, command: PluginCommand) -> Result<String, String> { - if let Some(emojis) = self.emoji(&command.tokens[0]) { - Ok(emojis) - } else { - Err(String::from("No emojis were found.")) - } - } -} diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index 8aa19a0..0dfb011 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -1,5 +1,4 @@ //! Collection of plugins included -pub mod emoji; pub mod factoid; pub mod help; pub mod keepnick; @@ -7,4 +6,5 @@ pub mod quote; pub mod remind; pub mod sed; pub mod tell; +pub mod unicode; pub mod url; diff --git a/src/plugins/unicode.rs b/src/plugins/unicode.rs new file mode 100644 index 0000000..7633c2d --- /dev/null +++ b/src/plugins/unicode.rs @@ -0,0 +1,100 @@ +extern crate unicode_names; + +use std::marker::PhantomData; +use std::fmt; + +use irc::client::prelude::*; + +use plugin::*; +use FrippyClient; + +use error::ErrorKind as FrippyErrorKind; +use error::FrippyError; +use failure::Fail; +use failure::ResultExt; + +#[derive(PluginName, Default, Debug)] +pub struct Unicode<C> { + phantom: PhantomData<C>, +} + +impl<C: FrippyClient> Unicode<C> { + pub fn new() -> Unicode<C> { + Unicode { + phantom: PhantomData, + } + } + + fn get_name(&self, symbol: char) -> String { + match unicode_names::name(symbol) { + Some(sym) => sym.to_string().to_lowercase(), + None => String::from("UNKNOWN"), + } + } + + fn format_response(&self, content: &str) -> String { + let character = content.chars().next(); + if let Some(c) = character { + let mut buf = [0; 2]; + + let byte_string = c + .encode_utf8(&mut buf) + .as_bytes() + .iter() + .map(|b| format!("{:#b}", b)) + .collect::<Vec<String>>() + .join(","); + + let name = self.get_name(c); + + format!( + "{} is '{}' | UTF-8: {2:#x} ({2}), Bytes: [{3}]", + c, name, c as u32, byte_string + ) + } else { + String::from("No non-space character was found.") + } + } +} + +impl<C: FrippyClient> Plugin for Unicode<C> { + type Client = C; + + fn execute(&self, client: &Self::Client, message: &Message) -> ExecutionStatus { + ExecutionStatus::Done + } + + fn execute_threaded(&self, _: &Self::Client, _: &Message) -> Result<(), FrippyError> { + panic!("Unicode should not use threading") + } + + fn command(&self, client: &Self::Client, command: PluginCommand) -> Result<(), FrippyError> { + if command.tokens.is_empty() { + let msg = "No non-space character was found."; + + if let Err(e) = client.send_notice(command.source, msg) { + Err(e.context(FrippyErrorKind::Connection))?; + } + + return Ok(()); + } + + let content = &command.tokens[0]; + + if let Err(e) = client.send_privmsg(command.target, &self.format_response(&content)) { + Err(e.context(FrippyErrorKind::Connection))?; + } + + Ok(()) + } + + fn evaluate(&self, _: &Self::Client, command: PluginCommand) -> Result<String, String> { + let tokens = command.tokens; + + if tokens.is_empty() { + return Err(String::from("No non-space character was found.")); + } + + Ok(self.format_response(&tokens[0])) + } +} |
