Add history module

This commit is contained in:
2026-01-19 13:41:34 -05:00
parent a0fcb5d4a8
commit 4df1540bc8
4 changed files with 52 additions and 12 deletions

View File

@@ -6,14 +6,14 @@ use regex::Regex;
//is this the best way to do this? probably not //is this the best way to do this? probably not
mod modules; 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<Message>)->String; type ModuleFunc = fn(regex::Captures, &Message, &VecDeque<Message>)->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 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)]; 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<Vec<(Regex, ModuleFunc)>, regex::Error> { pub fn build_modules() -> Result<Vec<(Regex, ModuleFunc)>, regex::Error> {
let mut regex_array: Vec<(Regex, ModuleFunc)> = Vec::with_capacity(NUM_MODS); let mut regex_array: Vec<(Regex, ModuleFunc)> = Vec::with_capacity(NUM_MODS);

View File

@@ -31,15 +31,15 @@ async fn main() -> Result<(), Error>{
print!("{}",message); print!("{}",message);
sender.send_privmsg(target,msg)?; sender.send_privmsg(target,msg)?;
} }
} } else {
if message_buf.len() < max_len {
/*if message_buf.len() < max_len {
message_buf.push_front(message); message_buf.push_front(message);
} }
else { else {
let _ = message_buf.pop_back(); let _ = message_buf.pop_back();
message_buf.push_front(message); message_buf.push_front(message);
}*/ }
}
} }
Ok(()) Ok(())

39
src/modules/history.rs Normal file
View File

@@ -0,0 +1,39 @@
use irc::proto::Message;
use std::collections::VecDeque;
use irc::proto::Command::*;
pub const PATTERN: &str = "^\\$history (?P<nick>[^\\s]+) (?P<amount>[0-9]+)";
pub const NAME: &str = "history";
pub const USAGE: &str = "Usage: $history <nick> <amount\r\nThis replays the last <amount> of messages from a user";
pub fn mod_message(captures: regex::Captures, _message: &Message, message_buf: &VecDeque<Message>) -> String {
let amount: usize = captures.get(2).unwrap().as_str().parse().unwrap();
let mut messages: Vec<String> = 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())
}

View File

@@ -8,3 +8,4 @@ pub mod help;
pub mod repo; pub mod repo;
pub mod rtfm; pub mod rtfm;
pub mod kick; pub mod kick;
pub mod history;