From c055beb4e88da972a0660db3cf9ca66dfbafb788 Mon Sep 17 00:00:00 2001 From: Colin McKechney Date: Mon, 5 May 2025 20:52:54 -0700 Subject: [PATCH] Add kick (#2) * Add kick command * Made kick only work with my nick --- src/lib.rs | 19 +++++++++++++------ src/main.rs | 10 ++++++++-- src/modules/kick.rs | 35 +++++++++++++++++++++++++++++++++++ src/modules/mod.rs | 2 +- 4 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 src/modules/kick.rs diff --git a/src/lib.rs b/src/lib.rs index 786d26a..cae9665 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}; +use modules::{bully, lenny, join_rude, grass, noemo, ttb, help, repo,rtfm, kick}; type ModuleFunc = fn(regex::Captures, &Message, &VecDeque)->String; -const NUM_MODS:usize = 8; +const NUM_MODS:usize = 9; -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)]; -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)]; +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)]; pub fn build_modules() -> Result, regex::Error> { let mut regex_array: Vec<(Regex, ModuleFunc)> = Vec::with_capacity(NUM_MODS); @@ -29,10 +29,17 @@ pub fn handle(modules: &Vec<(Regex, ModuleFunc)>, message: &Message, message_buf match &message.command { PRIVMSG(_,msg) => for (regex, function) in modules{ if let Some(captures) = regex.captures(msg.as_str()) { - return Some((message.response_target().unwrap_or("#lug").to_string(), function(captures, message, message_buf))); + if msg.contains("$kick") { + if message.source_nickname().unwrap_or_default() == "L3gion" { + return Some((message.response_target().unwrap_or("#lug").to_string(), function(captures, message, message_buf))); + } + } + else { + return Some((message.response_target().unwrap_or("#lug").to_string(), function(captures, message, message_buf))); + } } }, - //JOIN(ref channel,_,_) => return join_rude::join_rude(message.source_nickname().unwrap_or("unknown user"), channel.as_str()), + JOIN(ref channel,_,_) => return kick::bad_user(message.source_nickname().unwrap_or("unknown user"), channel.as_str()), _ => () } diff --git a/src/main.rs b/src/main.rs index 661459a..3fc3fb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,8 +23,14 @@ async fn main() -> Result<(), Error>{ let response = handle(&module_pair, &message, &message_buf); if let Some((target,msg))= response { - print!("{}",message); - sender.send_privmsg(target,msg)?; + if msg.contains("/kick") { + let message: Vec<&str> = msg.split(" ").collect(); + sender.send_kick(target, message[1], "")?; + } + else { + print!("{}",message); + sender.send_privmsg(target,msg)?; + } } /*if message_buf.len() < max_len { diff --git a/src/modules/kick.rs b/src/modules/kick.rs new file mode 100644 index 0000000..804c844 --- /dev/null +++ b/src/modules/kick.rs @@ -0,0 +1,35 @@ +use irc::proto::Message; +use std::collections::VecDeque; +use regex::Regex; + +pub const PATTERN: &str = "^\\$kick (?P[^\\s]+)"; +pub const USAGE: &str = ""; +pub const NAME: &str = "kick"; + + + +pub fn mod_message(captures: regex::Captures, message: &Message, _message_buf: &VecDeque) -> String { + let to_be_kicked = captures.get(1).unwrap().as_str(); + + format!("/kick {}", to_be_kicked) +} + +pub fn usage(message: &Message) -> (String, String) { + (message.response_target().unwrap_or("#lug").to_string(), USAGE.to_string()) +} + +pub fn bad_user(user: &str, channel: &str) -> Option<(String, String)> { + let regex = match Regex::new(".*[dD]ick[rR]ider.*") { + Ok(r) => r, + Err(_) => { + return None; + } + }; + + if regex.is_match(user) { + Some((channel.to_string(), format!("/kick {}", user))) + } + else { + None + } +} diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 83abf98..d21beb7 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -7,4 +7,4 @@ pub mod noemo; pub mod help; pub mod repo; pub mod rtfm; - +pub mod kick;