summaryrefslogtreecommitdiffstats
path: root/src/plugins
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2017-10-29 23:23:12 +0100
committerJokler <jokler.contact@gmail.com>2017-10-29 23:23:12 +0100
commitcb07b259950d4762ceb609266cd1e8ae0ef60dad (patch)
treec7bdd86b5bed18ce4d16a6bacde9647f0dfc6de3 /src/plugins
parent04e195af65b209e4812b1a076dd04e2f5a8ec21c (diff)
parent45f70129ce94c0511fc5cd2cbdc625f8ef00ea4b (diff)
downloadfrippy-cb07b259950d4762ceb609266cd1e8ae0ef60dad.tar.gz
frippy-cb07b259950d4762ceb609266cd1e8ae0ef60dad.zip
Merge branch 'dev'v0.3.0
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/currency.rs58
-rw-r--r--src/plugins/emoji.rs74
-rw-r--r--src/plugins/help.rs34
-rw-r--r--src/plugins/mod.rs9
4 files changed, 127 insertions, 48 deletions
diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs
index bb16cd9..78ae593 100644
--- a/src/plugins/currency.rs
+++ b/src/plugins/currency.rs
@@ -4,8 +4,11 @@ extern crate serde_json;
extern crate regex;
use std::io::Read;
+use std::num::ParseFloatError;
+
use irc::client::prelude::*;
use irc::error::Error as IrcError;
+
use self::reqwest::Client;
use self::reqwest::header::Connection;
use self::serde_json::Value;
@@ -66,25 +69,23 @@ impl Currency {
Currency {}
}
- fn eval_command<'a>(&self, tokens: &'a [String]) -> Option<ConvertionRequest<'a>> {
- let parsed = match tokens[0].parse() {
- Ok(v) => v,
- Err(_) => {
- return None;
- }
- };
-
- Some(ConvertionRequest {
- value: parsed,
- source: &tokens[1],
- target: &tokens[2],
- })
+ fn eval_command<'a>(&self, tokens: &'a [String]) -> Result<ConvertionRequest<'a>, ParseFloatError> {
+ Ok(ConvertionRequest {
+ value: tokens[0].parse()?,
+ source: &tokens[1],
+ target: &tokens[2],
+ })
}
fn convert(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> {
+
+ if command.tokens.len() < 3 {
+ return self.invalid_command(server, &command);
+ }
+
let request = match self.eval_command(&command.tokens) {
- Some(request) => request,
- None => {
+ Ok(request) => request,
+ Err(_) => {
return self.invalid_command(server, &command);
}
};
@@ -103,7 +104,7 @@ impl Currency {
}
}
- fn help(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> {
+ fn help(&self, server: &IrcServer, command: &mut PluginCommand) -> Result<(), IrcError> {
let help = format!("usage: {} currency value from_currency to_currency\r\n\
example: 1.5 eur usd\r\n\
available currencies: AUD, BGN, BRL, CAD, \
@@ -111,14 +112,14 @@ impl Currency {
IDR, ILS, INR, JPY, KRW, MXN, MYR, NOK, \
NZD, PHP, PLN, RON, RUB, SEK, SGD, THB, \
TRY, USD, ZAR",
- server.current_nickname());
+ server.current_nickname());
server.send_notice(&command.source, &help)
}
fn invalid_command(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> {
- let help = format!("Incorrect value. \
- Send \"{} help currency\" for help.",
+ let help = format!("Incorrect Command. \
+ Send \"{} currency help\" for help.",
server.current_nickname());
server.send_notice(&command.source, &help)
@@ -131,21 +132,18 @@ impl Plugin for Currency {
}
fn execute(&mut self, _: &IrcServer, _: &Message) -> Result<(), IrcError> {
- Ok(())
+ panic!("Currency does not implement the execute function!")
}
- fn command(&mut self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> {
- if command.tokens.is_empty() {
- self.invalid_command(server, &command)
+ fn command(&mut self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> {
- } else if command.tokens[0].to_lowercase() == "help" {
- self.help(server, command)
-
- } else if command.tokens.len() >= 3 {
- self.convert(server, command)
+ if command.tokens.is_empty() {
+ return self.invalid_command(server, &command);
+ }
- } else {
- self.invalid_command(server, &command)
+ match command.tokens[0].as_ref() {
+ "help" => self.help(server, &mut command),
+ _ => self.convert(server, command),
}
}
}
diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs
index d2ed956..09d5c27 100644
--- a/src/plugins/emoji.rs
+++ b/src/plugins/emoji.rs
@@ -1,43 +1,83 @@
extern crate unicode_names;
+use std::fmt;
+
use irc::client::prelude::*;
use irc::error::Error as IrcError;
use plugin::*;
+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, Debug)]
pub struct Emoji;
+
impl Emoji {
pub fn new() -> Emoji {
Emoji {}
}
fn emoji(&self, server: &IrcServer, content: &str, target: &str) -> Result<(), IrcError> {
-
- let mut names: Vec<String> = Vec::new();
- for emoji in self.return_emojis(content) {
-
- let name = match unicode_names::name(emoji) {
- Some(v) => format!("{}", v).to_lowercase(),
- None => "UNKNOWN".to_string(),
- };
-
- names.push(name);
- }
+ let names = self.return_emojis(content)
+ .iter()
+ .map(|e| e.to_string())
+ .collect::<Vec<String>>();
server.send_privmsg(target, &names.join(", "))
}
- fn return_emojis(&self, string: &str) -> Vec<char> {
+ fn return_emojis(&self, string: &str) -> Vec<EmojiHandle> {
+ let mut emojis: Vec<EmojiHandle> = Vec::new();
+
+ let mut current = EmojiHandle {
+ symbol: ' ',
+ count: 0,
+ };
- let mut emojis: Vec<char> = Vec::new();
for c in string.chars() {
- if self.is_emoji(&c) {
- emojis.push(c);
+ 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
}
@@ -67,7 +107,9 @@ impl Plugin for Emoji {
fn execute(&mut self, server: &IrcServer, message: &Message) -> Result<(), IrcError> {
match message.command {
- Command::PRIVMSG(ref target, ref content) => self.emoji(server, content, target),
+ Command::PRIVMSG(_, ref content) => {
+ self.emoji(server, content, message.response_target().unwrap())
+ }
_ => Ok(()),
}
}
diff --git a/src/plugins/help.rs b/src/plugins/help.rs
new file mode 100644
index 0000000..c4ddcd4
--- /dev/null
+++ b/src/plugins/help.rs
@@ -0,0 +1,34 @@
+use irc::client::prelude::*;
+use irc::error::Error as IrcError;
+
+use plugin::*;
+
+#[derive(PluginName, Debug)]
+pub struct Help;
+
+impl Help {
+ pub fn new() -> Help {
+ Help {}
+ }
+
+ fn help(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> {
+ server.send_notice(&command.source, "Help has not been added yet.")
+ }
+}
+
+impl Plugin for Help {
+ fn is_allowed(&self, _: &IrcServer, _: &Message) -> bool {
+ false
+ }
+
+ fn execute(&mut self, _: &IrcServer, _: &Message) -> Result<(), IrcError> {
+ panic!("Help does not implement the execute function!")
+ }
+
+ fn command(&mut self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> {
+ self.help(server, command)
+ }
+}
+
+#[cfg(test)]
+mod tests {}
diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs
index adf54b2..0dea596 100644
--- a/src/plugins/mod.rs
+++ b/src/plugins/mod.rs
@@ -1,2 +1,7 @@
-pub mod emoji;
-pub mod currency;
+mod help;
+mod emoji;
+mod currency;
+
+pub use self::help::Help;
+pub use self::emoji::Emoji;
+pub use self::currency::Currency;