1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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]))
}
}
|