19
src/lib.rs
19
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<Message>)->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<Vec<(Regex, ModuleFunc)>, 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()),
|
||||
_ => ()
|
||||
}
|
||||
|
||||
|
||||
10
src/main.rs
10
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 {
|
||||
|
||||
35
src/modules/kick.rs
Normal file
35
src/modules/kick.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use irc::proto::Message;
|
||||
use std::collections::VecDeque;
|
||||
use regex::Regex;
|
||||
|
||||
pub const PATTERN: &str = "^\\$kick (?P<nick>[^\\s]+)";
|
||||
pub const USAGE: &str = "";
|
||||
pub const NAME: &str = "kick";
|
||||
|
||||
|
||||
|
||||
pub fn mod_message(captures: regex::Captures, message: &Message, _message_buf: &VecDeque<Message>) -> 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
|
||||
}
|
||||
}
|
||||
@@ -7,4 +7,4 @@ pub mod noemo;
|
||||
pub mod help;
|
||||
pub mod repo;
|
||||
pub mod rtfm;
|
||||
|
||||
pub mod kick;
|
||||
|
||||
Reference in New Issue
Block a user