Add history module
This commit is contained in:
@@ -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<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 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<Vec<(Regex, ModuleFunc)>, regex::Error> {
|
||||
let mut regex_array: Vec<(Regex, ModuleFunc)> = Vec::with_capacity(NUM_MODS);
|
||||
|
||||
16
src/main.rs
16
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(())
|
||||
|
||||
39
src/modules/history.rs
Normal file
39
src/modules/history.rs
Normal 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())
|
||||
}
|
||||
@@ -8,3 +8,4 @@ pub mod help;
|
||||
pub mod repo;
|
||||
pub mod rtfm;
|
||||
pub mod kick;
|
||||
pub mod history;
|
||||
|
||||
Reference in New Issue
Block a user