From 869b22874fb64935bff0f25f9a5dacb18b9d1548 Mon Sep 17 00:00:00 2001 From: Jokler Date: Wed, 20 Feb 2019 22:53:56 +0100 Subject: Plugins: Replace Emoji with Unicode The new plugin adds a command which allows checking the name and some info of a character. --- src/plugins/unicode.rs | 100 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/plugins/unicode.rs (limited to 'src/plugins/unicode.rs') 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 { + phantom: PhantomData, +} + +impl Unicode { + pub fn new() -> Unicode { + 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::>() + .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 Plugin for Unicode { + 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 { + let tokens = command.tokens; + + if tokens.is_empty() { + return Err(String::from("No non-space character was found.")); + } + + Ok(self.format_response(&tokens[0])) + } +} -- cgit v1.2.3-70-g09d2 From b7e96ba8f1adf4426780de060d053437662c2d66 Mon Sep 17 00:00:00 2001 From: Jokler Date: Thu, 21 Feb 2019 00:25:31 +0100 Subject: Unicode: Fix buffer size --- src/plugins/unicode.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/plugins/unicode.rs') diff --git a/src/plugins/unicode.rs b/src/plugins/unicode.rs index 7633c2d..60f01d5 100644 --- a/src/plugins/unicode.rs +++ b/src/plugins/unicode.rs @@ -35,7 +35,7 @@ impl Unicode { fn format_response(&self, content: &str) -> String { let character = content.chars().next(); if let Some(c) = character { - let mut buf = [0; 2]; + let mut buf = [0; 4]; let byte_string = c .encode_utf8(&mut buf) -- cgit v1.2.3-70-g09d2 From 79146633203db377a6669ede5ff5bcb1b34d4d05 Mon Sep 17 00:00:00 2001 From: Jokler Date: Thu, 21 Feb 2019 00:33:06 +0100 Subject: Main: Fix doc test and warnings --- src/lib.rs | 7 ++++--- src/plugins/unicode.rs | 4 +--- 2 files changed, 5 insertions(+), 6 deletions(-) (limited to 'src/plugins/unicode.rs') diff --git a/src/lib.rs b/src/lib.rs index c257544..50e6688 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,7 @@ //! let mut bot = Bot::new("."); //! //! bot.add_plugin(plugins::help::Help::new()); -//! bot.add_plugin(plugins::emoji::Emoji::new()); +//! bot.add_plugin(plugins::unicode::Unicode::new()); //! //! bot.connect(&mut reactor, &config).unwrap(); //! reactor.run().unwrap(); @@ -50,11 +50,11 @@ extern crate chrono; extern crate circular_queue; extern crate humantime; extern crate irc; +extern crate rand; extern crate regex; extern crate reqwest; extern crate serde_json; extern crate time; -extern crate rand; pub mod error; pub mod plugin; @@ -326,7 +326,8 @@ impl ThreadedPlugins { impl fmt::Display for ThreadedPlugins { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let plugin_names = self.plugins + let plugin_names = self + .plugins .iter() .map(|(_, p)| p.name().to_owned()) .collect::>(); diff --git a/src/plugins/unicode.rs b/src/plugins/unicode.rs index 60f01d5..0736ac7 100644 --- a/src/plugins/unicode.rs +++ b/src/plugins/unicode.rs @@ -1,7 +1,6 @@ extern crate unicode_names; use std::marker::PhantomData; -use std::fmt; use irc::client::prelude::*; @@ -11,7 +10,6 @@ use FrippyClient; use error::ErrorKind as FrippyErrorKind; use error::FrippyError; use failure::Fail; -use failure::ResultExt; #[derive(PluginName, Default, Debug)] pub struct Unicode { @@ -60,7 +58,7 @@ impl Unicode { impl Plugin for Unicode { type Client = C; - fn execute(&self, client: &Self::Client, message: &Message) -> ExecutionStatus { + fn execute(&self, _: &Self::Client, _: &Message) -> ExecutionStatus { ExecutionStatus::Done } -- cgit v1.2.3-70-g09d2 From f5554ef8a8af5e9d5c6604c9130d65c71e31be81 Mon Sep 17 00:00:00 2001 From: Jokler Date: Thu, 21 Feb 2019 04:11:52 +0100 Subject: Unicode: Always send a notice on error --- src/plugins/unicode.rs | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'src/plugins/unicode.rs') diff --git a/src/plugins/unicode.rs b/src/plugins/unicode.rs index 0736ac7..56c8666 100644 --- a/src/plugins/unicode.rs +++ b/src/plugins/unicode.rs @@ -31,27 +31,27 @@ impl Unicode { } fn format_response(&self, content: &str) -> String { - let character = content.chars().next(); - if let Some(c) = character { - let mut buf = [0; 4]; - - let byte_string = c - .encode_utf8(&mut buf) - .as_bytes() - .iter() - .map(|b| format!("{:#b}", b)) - .collect::>() - .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.") - } + let character = content + .chars() + .next() + .expect("content contains at least one character"); + + let mut buf = [0; 4]; + + let byte_string = character + .encode_utf8(&mut buf) + .as_bytes() + .iter() + .map(|b| format!("{:#b}", b)) + .collect::>() + .join(","); + + let name = self.get_name(character); + + format!( + "{} is '{}' | UTF-8: {2:#x} ({2}), Bytes: [{3}]", + character, name, character as u32, byte_string + ) } } @@ -67,7 +67,7 @@ impl Plugin for Unicode { } fn command(&self, client: &Self::Client, command: PluginCommand) -> Result<(), FrippyError> { - if command.tokens.is_empty() { + if command.tokens.is_empty() || command.tokens[0].is_empty() { let msg = "No non-space character was found."; if let Err(e) = client.send_notice(command.source, msg) { -- cgit v1.2.3-70-g09d2