aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/tell/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/tell/mod.rs')
-rw-r--r--src/plugins/tell/mod.rs72
1 files changed, 45 insertions, 27 deletions
diff --git a/src/plugins/tell/mod.rs b/src/plugins/tell/mod.rs
index a2c49f2..f781ed8 100644
--- a/src/plugins/tell/mod.rs
+++ b/src/plugins/tell/mod.rs
@@ -1,5 +1,4 @@
use irc::client::prelude::*;
-use irc::error::IrcError;
use std::time::Duration;
use std::sync::Mutex;
@@ -10,6 +9,11 @@ use humantime::format_duration;
use plugin::*;
+use error::FrippyError;
+use error::ErrorKind as FrippyErrorKind;
+use failure::Fail;
+use failure::ResultExt;
+
pub mod database;
use self::database::{Database, DbResponse};
@@ -34,22 +38,27 @@ impl<T: Database> Tell<T> {
}
}
- fn tell_command(&self, client: &IrcClient, command: &PluginCommand) -> Result<&str, String> {
+ fn tell_command(&self, client: &IrcClient, command: PluginCommand) -> Result<&str, String> {
if command.tokens.len() < 2 {
return Err(self.invalid_command(client));
}
- let receiver = command.tokens[0].to_string();
- let sender = command.source.to_owned();
+ let receiver = &command.tokens[0];
+ let sender = command.source;
- if receiver == sender {
- //return Err(String::from("That's your name!"));
+ if receiver.eq_ignore_ascii_case(&sender) {
+ return Err(String::from("That's your name!"));
}
- if command.source != command.target {
- if let Some(users) = client.list_users(&command.target) {
- if users.iter().any(|u| u.get_nickname() == receiver) {
- //return Err(format!("{} is in this channel.", receiver));
+ if let Some(channels) = client.list_channels() {
+ for channel in channels {
+ if let Some(users) = client.list_users(&channel) {
+ if users
+ .iter()
+ .any(|u| u.get_nickname().eq_ignore_ascii_case(&receiver))
+ {
+ return Err(format!("{} is currently online.", receiver));
+ }
}
}
}
@@ -58,7 +67,7 @@ impl<T: Database> Tell<T> {
let message = command.tokens[1..].join(" ");
let tell = database::NewTellMessage {
sender: &sender,
- receiver: &receiver,
+ receiver: &receiver.to_lowercase(),
time: NaiveDateTime::from_timestamp(tm.sec, 0u32),
message: &message,
};
@@ -69,9 +78,9 @@ impl<T: Database> Tell<T> {
}
}
- fn send_tell(&self, client: &IrcClient, receiver: &str) -> ExecutionStatus {
+ fn send_tells(&self, client: &IrcClient, receiver: &str) -> ExecutionStatus {
let mut tells = try_lock!(self.tells);
- if let Some(tell_messages) = tells.get_tells(receiver) {
+ if let Some(tell_messages) = tells.get_tells(&receiver.to_lowercase()) {
for tell in tell_messages {
let now = Duration::new(time::now().to_timespec().sec as u64, 0);
let dur = now - Duration::new(tell.time.timestamp() as u64, 0);
@@ -84,7 +93,7 @@ impl<T: Database> Tell<T> {
tell.sender, human_dur, tell.message
),
) {
- return ExecutionStatus::Err(Box::new(e));
+ return ExecutionStatus::Err(e.context(FrippyErrorKind::Connection).into());
}
debug!(
"Sent {:?} from {:?} to {:?}",
@@ -92,7 +101,8 @@ impl<T: Database> Tell<T> {
);
}
}
- tells.delete_tells(receiver);
+ tells.delete_tells(&receiver.to_lowercase());
+
ExecutionStatus::Done
}
@@ -116,29 +126,37 @@ impl<T: Database> Tell<T> {
impl<T: Database> Plugin for Tell<T> {
fn execute(&self, client: &IrcClient, message: &Message) -> ExecutionStatus {
match message.command {
- Command::JOIN(_, _, _) | Command::PRIVMSG(_, _) => {
- self.send_tell(client, message.source_nickname().unwrap())
- }
+ Command::JOIN(_, _, _) => self.send_tells(client, message.source_nickname().unwrap()),
_ => ExecutionStatus::Done,
}
}
- fn execute_threaded(&self, _: &IrcClient, _: &Message) -> Result<(), IrcError> {
+ fn execute_threaded(&self, _: &IrcClient, _: &Message) -> Result<(), FrippyError> {
panic!("Tell should not use threading")
}
- fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), IrcError> {
+ fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), FrippyError> {
if command.tokens.is_empty() {
- return client.send_notice(&command.source, &self.invalid_command(client));
+ return Ok(client
+ .send_notice(&command.source, &self.invalid_command(client))
+ .context(FrippyErrorKind::Connection)?);
}
- match command.tokens[0].as_ref() {
- "help" => client.send_notice(&command.source, &self.help(client)),
- _ => match self.tell_command(client, &command) {
- Ok(msg) => client.send_notice(&command.source, msg),
- Err(msg) => client.send_notice(&command.source, &msg),
+ let sender = command.source.to_owned();
+
+ Ok(match command.tokens[0].as_ref() {
+ "help" => client
+ .send_notice(&command.source, &self.help(client))
+ .context(FrippyErrorKind::Connection),
+ _ => match self.tell_command(client, command) {
+ Ok(msg) => client
+ .send_notice(&sender, msg)
+ .context(FrippyErrorKind::Connection),
+ Err(msg) => client
+ .send_notice(&sender, &msg)
+ .context(FrippyErrorKind::Connection),
},
- }
+ }?)
}
fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result<String, String> {