diff options
Diffstat (limited to 'src/plugins/quote')
| -rw-r--r-- | src/plugins/quote/database.rs | 142 | ||||
| -rw-r--r-- | src/plugins/quote/mod.rs | 278 |
2 files changed, 420 insertions, 0 deletions
diff --git a/src/plugins/quote/database.rs b/src/plugins/quote/database.rs new file mode 100644 index 0000000..49d6058 --- /dev/null +++ b/src/plugins/quote/database.rs @@ -0,0 +1,142 @@ +use std::collections::HashMap; +#[cfg(feature = "mysql")] +use std::sync::Arc; + +#[cfg(feature = "mysql")] +use diesel::mysql::MysqlConnection; +#[cfg(feature = "mysql")] +use diesel::prelude::*; +#[cfg(feature = "mysql")] +use failure::ResultExt; +#[cfg(feature = "mysql")] +use r2d2::Pool; +#[cfg(feature = "mysql")] +use r2d2_diesel::ConnectionManager; + +use chrono::NaiveDateTime; + +use super::error::*; + +#[cfg_attr(feature = "mysql", derive(Queryable))] +#[derive(Clone, Debug)] +pub struct Quote { + pub quotee: String, + pub channel: String, + pub idx: i32, + pub content: String, + pub author: String, + pub created: NaiveDateTime, +} + +#[cfg_attr(feature = "mysql", derive(Insertable))] +#[cfg_attr(feature = "mysql", table_name = "quotes")] +pub struct NewQuote<'a> { + pub quotee: &'a str, + pub channel: &'a str, + pub idx: i32, + pub content: &'a str, + pub author: &'a str, + pub created: NaiveDateTime, +} + +pub trait Database: Send + Sync { + fn insert_quote(&mut self, quote: &NewQuote) -> Result<(), QuoteError>; + fn get_quote(&self, quotee: &str, channel: &str, idx: i32) -> Result<Quote, QuoteError>; + fn count_quotes(&self, quotee: &str, channel: &str) -> Result<i32, QuoteError>; +} + +// HashMap +impl<S: ::std::hash::BuildHasher + Send + Sync> Database + for HashMap<(String, String, i32), Quote, S> +{ + fn insert_quote(&mut self, quote: &NewQuote) -> Result<(), QuoteError> { + let quote = Quote { + quotee: quote.quotee.to_owned(), + channel: quote.channel.to_owned(), + idx: quote.idx, + content: quote.content.to_owned(), + author: quote.author.to_owned(), + created: quote.created, + }; + + let quotee = quote.quotee.clone(); + let channel = quote.channel.clone(); + match self.insert((quotee, channel, quote.idx), quote) { + None => Ok(()), + Some(_) => Err(ErrorKind::Duplicate)?, + } + } + + fn get_quote(&self, quotee: &str, channel: &str, idx: i32) -> Result<Quote, QuoteError> { + Ok(self + .get(&(quotee.to_owned(), channel.to_owned(), idx)) + .cloned() + .ok_or(ErrorKind::NotFound)?) + } + + fn count_quotes(&self, quotee: &str, channel: &str) -> Result<i32, QuoteError> { + Ok(self + .iter() + .filter(|&(&(ref n, ref c, _), _)| n == quotee && c == channel) + .count() as i32) + } +} + +// Diesel automatically defines the quotes module as public. +// We create a schema module to keep it private. +#[cfg(feature = "mysql")] +mod schema { + table! { + quotes (quotee, channel, idx) { + quotee -> Varchar, + channel -> Varchar, + idx -> Integer, + content -> Text, + author -> Varchar, + created -> Timestamp, + } + } +} + +#[cfg(feature = "mysql")] +use self::schema::quotes; + +#[cfg(feature = "mysql")] +impl Database for Arc<Pool<ConnectionManager<MysqlConnection>>> { + fn insert_quote(&mut self, quote: &NewQuote) -> Result<(), QuoteError> { + use diesel; + + let conn = &*self.get().context(ErrorKind::NoConnection)?; + diesel::insert_into(quotes::table) + .values(quote) + .execute(conn) + .context(ErrorKind::MysqlError)?; + + Ok(()) + } + + fn get_quote(&self, quotee: &str, channel: &str, idx: i32) -> Result<Quote, QuoteError> { + let conn = &*self.get().context(ErrorKind::NoConnection)?; + Ok(quotes::table + .find((quotee, channel, idx)) + .first(conn) + .context(ErrorKind::MysqlError)?) + } + + fn count_quotes(&self, quotee: &str, channel: &str) -> Result<i32, QuoteError> { + use diesel; + + let conn = &*self.get().context(ErrorKind::NoConnection)?; + let count: Result<i64, _> = quotes::table + .filter(quotes::columns::quotee.eq(quotee)) + .filter(quotes::columns::channel.eq(channel)) + .count() + .get_result(conn); + + match count { + Ok(c) => Ok(c as i32), + Err(diesel::NotFound) => Ok(0), + Err(e) => Err(e).context(ErrorKind::MysqlError)?, + } + } +} diff --git a/src/plugins/quote/mod.rs b/src/plugins/quote/mod.rs new file mode 100644 index 0000000..edeed40 --- /dev/null +++ b/src/plugins/quote/mod.rs @@ -0,0 +1,278 @@ +use std::fmt; +use std::marker::PhantomData; +use std::str::FromStr; + +use antidote::RwLock; +use chrono::NaiveDateTime; +use irc::client::prelude::*; +use rand::{thread_rng, Rng}; +use time; + +use plugin::*; +use FrippyClient; +pub mod database; +use self::database::Database; + +use self::error::*; +use error::ErrorKind as FrippyErrorKind; +use error::FrippyError; +use failure::ResultExt; + +enum QuoteResponse { + Public(String), + Private(String), +} + +#[derive(PluginName)] +pub struct Quote<T: Database, C: Client> { + quotes: RwLock<T>, + phantom: PhantomData<C>, +} + +impl<T: Database, C: Client> Quote<T, C> { + pub fn new(db: T) -> Self { + Quote { + quotes: RwLock::new(db), + phantom: PhantomData, + } + } + + fn create_quote( + &self, + quotee: &str, + channel: &str, + content: &str, + author: &str, + ) -> Result<&str, QuoteError> { + let count = self.quotes.read().count_quotes(quotee, channel)?; + let tm = time::now().to_timespec(); + + let quote = database::NewQuote { + quotee, + channel, + idx: count + 1, + content, + author, + created: NaiveDateTime::from_timestamp(tm.sec, 0u32), + }; + + Ok(self + .quotes + .write() + .insert_quote("e) + .map(|()| "Successfully added!")?) + } + + fn add(&self, command: &mut PluginCommand) -> Result<&str, QuoteError> { + if command.tokens.len() < 2 { + Err(ErrorKind::InvalidCommand)?; + } + + if command.target == command.source { + Err(ErrorKind::PrivateMessageNotAllowed)?; + } + + let quotee = command.tokens.remove(0); + let channel = &command.target; + let content = command.tokens.join(" "); + + Ok(self.create_quote("ee, channel, &content, &command.source)?) + } + + fn get(&self, command: &PluginCommand) -> Result<String, QuoteError> { + if command.tokens.is_empty() { + Err(ErrorKind::InvalidCommand)?; + } + + let quotee = &command.tokens[0]; + let channel = &command.target; + let count = self.quotes.read().count_quotes(quotee, channel)?; + + if count < 1 { + Err(ErrorKind::NotFound)?; + } + + let len = command.tokens.len(); + let idx = if len < 2 || command.tokens[1].is_empty() { + thread_rng().gen_range(1, count + 1) + } else { + let idx_string = &command.tokens[1]; + let idx = match i32::from_str(idx_string) { + Ok(i) => i, + Err(_) => Err(ErrorKind::InvalidIndex)?, + }; + + if idx < 0 { + count + idx + 1 + } else { + idx + } + }; + + let quote = self + .quotes + .read() + .get_quote(quotee, channel, idx) + .context(ErrorKind::NotFound)?; + + Ok(format!( + "\"{}\" - {}[{}/{}]", + quote.content, quote.quotee, idx, count + )) + } + + fn info(&self, command: &PluginCommand) -> Result<String, QuoteError> { + match command.tokens.len() { + 0 => Err(ErrorKind::InvalidCommand)?, + 1 => { + let quotee = &command.tokens[0]; + let channel = &command.target; + let count = self.quotes.read().count_quotes(quotee, channel)?; + + Ok(match count { + 0 => Err(ErrorKind::NotFound)?, + 1 => format!("{} has 1 quote", quotee), + _ => format!("{} has {} quotes", quotee, count), + }) + } + _ => { + let quotee = &command.tokens[0]; + let channel = &command.target; + let idx = i32::from_str(&command.tokens[1]).context(ErrorKind::InvalidIndex)?; + + let idx = if idx < 0 { + self.quotes.read().count_quotes(quotee, channel)? + idx + 1 + } else { + idx + }; + + let quote = self + .quotes + .read() + .get_quote(quotee, channel, idx) + .context(ErrorKind::NotFound)?; + + Ok(format!( + "{}'s quote was added by {} at {} UTC", + quotee, quote.author, quote.created + )) + } + } + } + + fn help(&self) -> &str { + "usage: quotes <subcommand>\r\n\ + subcommands: add, get, info, help" + } +} + +impl<T: Database, C: FrippyClient> Plugin for Quote<T, C> { + type Client = C; + fn execute(&self, _: &Self::Client, _: &Message) -> ExecutionStatus { + ExecutionStatus::Done + } + + fn execute_threaded(&self, _: &Self::Client, _: &Message) -> Result<(), FrippyError> { + panic!("Quotes should not use threading") + } + + fn command( + &self, + client: &Self::Client, + mut command: PluginCommand, + ) -> Result<(), FrippyError> { + use self::QuoteResponse::{Private, Public}; + + if command.tokens.is_empty() { + client + .send_notice(&command.source, &ErrorKind::InvalidCommand.to_string()) + .context(FrippyErrorKind::Connection)?; + + return Ok(()); + } + + let target = command.target.clone(); + let source = command.source.clone(); + + let sub_command = command.tokens.remove(0); + let result = match sub_command.as_ref() { + "add" => self.add(&mut command).map(|s| Private(s.to_owned())), + "get" => self.get(&command).map(Public), + "info" => self.info(&command).map(Public), + "help" => Ok(Private(self.help().to_owned())), + _ => Err(ErrorKind::InvalidCommand.into()), + }; + + match result { + Ok(v) => match v { + Public(m) => client + .send_privmsg(&target, &m) + .context(FrippyErrorKind::Connection)?, + Private(m) => client + .send_notice(&source, &m) + .context(FrippyErrorKind::Connection)?, + }, + Err(e) => { + let message = e.to_string(); + client + .send_notice(&source, &message) + .context(FrippyErrorKind::Connection)?; + Err(e).context(FrippyErrorKind::Quote)? + } + } + + Ok(()) + } + + fn evaluate(&self, _: &Self::Client, _: PluginCommand) -> Result<String, String> { + Err(String::from( + "Evaluation of commands is not implemented for Quote at this time", + )) + } +} + +impl<T: Database, C: FrippyClient> fmt::Debug for Quote<T, C> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Quote {{ ... }}") + } +} + +pub mod error { + #[derive(Copy, Clone, Eq, PartialEq, Debug, Fail, Error)] + #[error = "QuoteError"] + pub enum ErrorKind { + /// Invalid command error + #[fail(display = "Incorrect command. Send \"quote help\" for help")] + InvalidCommand, + + /// Invalid index error + #[fail(display = "Invalid index")] + InvalidIndex, + + /// Private message error + #[fail(display = "You can only add quotes in channel messages")] + PrivateMessageNotAllowed, + + /// Download error + #[fail(display = "Download failed")] + Download, + + /// Duplicate error + #[fail(display = "Entry already exists")] + Duplicate, + + /// Not found error + #[fail(display = "Quote was not found")] + NotFound, + + /// MySQL error + #[cfg(feature = "mysql")] + #[fail(display = "Failed to execute MySQL Query")] + MysqlError, + + /// No connection error + #[cfg(feature = "mysql")] + #[fail(display = "No connection to the database")] + NoConnection, + } +} |
