diff --git a/src/lib.rs b/src/lib.rs index cae9665..0babf64 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,14 +6,14 @@ use regex::Regex; //is this the best way to do this? probably not mod modules; -use modules::{bully, lenny, join_rude, grass, noemo, ttb, help, repo,rtfm, kick}; +use modules::{bully, lenny, join_rude, grass, noemo, ttb, help, repo,rtfm, kick, history}; type ModuleFunc = fn(regex::Captures, &Message, &VecDeque)->String; -const NUM_MODS:usize = 9; +const NUM_MODS:usize = 10; -const MODULES: [(&str, ModuleFunc);NUM_MODS] = [(lenny::PATTERN, lenny::mod_message), (bully::PATTERN, bully::mod_message), (grass::PATTERN, grass::touch_grass), (noemo::PATTERN, noemo::no_emo), (ttb::PATTERN, ttb::time_to_baby), (help::PATTERN, help::help), (repo::PATTERN, repo::link), (rtfm::PATTERN, rtfm::rtfm), (kick::PATTERN, kick::mod_message)]; -const MODULE_USAGE: [(&str, &str); NUM_MODS] = [(lenny::NAME, lenny::USAGE), (bully::NAME, bully::USAGE), (grass::NAME, grass::USAGE), (noemo::NAME, noemo::USAGE), (ttb::NAME, ttb::USAGE), (help::NAME, help::USAGE), (repo::NAME, repo::USAGE),(rtfm::NAME, rtfm::USAGE), (kick::NAME, kick::USAGE)]; +const MODULES: [(&str, ModuleFunc);NUM_MODS] = [(lenny::PATTERN, lenny::mod_message), (bully::PATTERN, bully::mod_message), (grass::PATTERN, grass::touch_grass), (noemo::PATTERN, noemo::no_emo), (ttb::PATTERN, ttb::time_to_baby), (help::PATTERN, help::help), (repo::PATTERN, repo::link), (rtfm::PATTERN, rtfm::rtfm), (kick::PATTERN, kick::mod_message), (history::PATTERN, history::mod_message)]; +const MODULE_USAGE: [(&str, &str); NUM_MODS] = [(lenny::NAME, lenny::USAGE), (bully::NAME, bully::USAGE), (grass::NAME, grass::USAGE), (noemo::NAME, noemo::USAGE), (ttb::NAME, ttb::USAGE), (help::NAME, help::USAGE), (repo::NAME, repo::USAGE),(rtfm::NAME, rtfm::USAGE), (kick::NAME, kick::USAGE), (history::NAME, history::USAGE)]; pub fn build_modules() -> Result, regex::Error> { let mut regex_array: Vec<(Regex, ModuleFunc)> = Vec::with_capacity(NUM_MODS); diff --git a/src/main.rs b/src/main.rs index 3fc3fb8..2e41543 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,15 +31,15 @@ async fn main() -> Result<(), Error>{ print!("{}",message); sender.send_privmsg(target,msg)?; } + } else { + if message_buf.len() < max_len { + message_buf.push_front(message); + } + else { + let _ = message_buf.pop_back(); + message_buf.push_front(message); + } } - - /*if message_buf.len() < max_len { - message_buf.push_front(message); - } - else { - let _ = message_buf.pop_back(); - message_buf.push_front(message); - }*/ } Ok(()) diff --git a/src/modules/history.rs b/src/modules/history.rs new file mode 100644 index 0000000..4f3e1fd --- /dev/null +++ b/src/modules/history.rs @@ -0,0 +1,39 @@ +use irc::proto::Message; +use std::collections::VecDeque; +use irc::proto::Command::*; + +pub const PATTERN: &str = "^\\$history (?P[^\\s]+) (?P[0-9]+)"; +pub const NAME: &str = "history"; +pub const USAGE: &str = "Usage: $history of messages from a user"; + +pub fn mod_message(captures: regex::Captures, _message: &Message, message_buf: &VecDeque) -> String { + let amount: usize = captures.get(2).unwrap().as_str().parse().unwrap(); + let mut messages: Vec = Vec::with_capacity(amount); + let mut total_message: String = format!("No messages found for user: {}", captures.get(1).unwrap().as_str()); + + for message in message_buf { + if let Some(nick) = message.source_nickname() { + if nick == captures.get(1).unwrap().as_str() && messages.len() < amount { + match &message.command { + PRIVMSG(_,msg) => { + messages.push(msg.clone()); + }, + _ => () + }; + } + if messages.len() == amount { + break; + } + } + } + + if messages.len() > 0 { + messages.reverse(); + total_message = messages.join("\r\n"); + } + total_message +} + +pub fn usage(message: &Message) -> (String, String) { + (message.response_target().unwrap_or("#lug").to_string(), USAGE.to_string()) +} diff --git a/src/modules/mod.rs b/src/modules/mod.rs index d21beb7..192096f 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -8,3 +8,4 @@ pub mod help; pub mod repo; pub mod rtfm; pub mod kick; +pub mod history;