aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/unicode.rs
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2019-06-22 15:51:21 +0200
committerJokler <jokler.contact@gmail.com>2019-06-22 15:51:21 +0200
commit3592c7b6fb2522ff57c7f312b8927eb680d6dc5c (patch)
treed484a367c205afe43ba7327a888b06844fd24c0c /src/plugins/unicode.rs
parent237f6ebe59c90d4ceddd9af6a8a19e562d304aaa (diff)
parenta92e622a0d42911e8e46239c3bde17169ed60c92 (diff)
downloadfrippy-3592c7b6fb2522ff57c7f312b8927eb680d6dc5c.tar.gz
frippy-3592c7b6fb2522ff57c7f312b8927eb680d6dc5c.zip
Merge branch 'dev'HEADv0.5.0master
Diffstat (limited to 'src/plugins/unicode.rs')
-rw-r--r--src/plugins/unicode.rs98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/plugins/unicode.rs b/src/plugins/unicode.rs
new file mode 100644
index 0000000..56c8666
--- /dev/null
+++ b/src/plugins/unicode.rs
@@ -0,0 +1,98 @@
+extern crate unicode_names;
+
+use std::marker::PhantomData;
+
+use irc::client::prelude::*;
+
+use plugin::*;
+use FrippyClient;
+
+use error::ErrorKind as FrippyErrorKind;
+use error::FrippyError;
+use failure::Fail;
+
+#[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()
+ .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::<Vec<String>>()
+ .join(",");
+
+ let name = self.get_name(character);
+
+ format!(
+ "{} is '{}' | UTF-8: {2:#x} ({2}), Bytes: [{3}]",
+ character, name, character as u32, byte_string
+ )
+ }
+}
+
+impl<C: FrippyClient> Plugin for Unicode<C> {
+ type Client = C;
+
+ fn execute(&self, _: &Self::Client, _: &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() || command.tokens[0].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]))
+ }
+}