BotServ BADWORDS: user-configurable regex kicker
BADWORDS <#channel> ADD|DEL|LIST|CLEAR manages a channel's badword patterns, each a regular expression, so a channel matches whatever it wants — an improvement over Anope's fixed ANY/SINGLE/START/END. Enable with KICK <#channel> BADWORDS ON. Uses the regex crate: it's finite-automata based with linear-time matching, so untrusted user patterns can't cause catastrophic backtracking (ReDoS). A channel's patterns compile into one RegexSet (single-pass match against all of them), cached in the engine and rebuilt only when the list's revision changes — so the hot path is one is_match call, no per-message compilation or allocation. Patterns are validated (and size-limited) at add time; matching is case-insensitive by default.
This commit is contained in:
parent
43def8ee23
commit
ca95184359
9 changed files with 238 additions and 7 deletions
63
botserv/src/badwords.rs
Normal file
63
botserv/src/badwords.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
use fedserv_api::{ChanError, Sender, ServiceCtx, Store};
|
||||
|
||||
// BADWORDS <#channel> ADD <regex> | DEL <regex> | LIST | CLEAR: manage the
|
||||
// channel's badword patterns. Each entry is a regular expression, so a channel
|
||||
// can match whatever it likes (improving on Anope's fixed ANY/SINGLE/START/END).
|
||||
// Enable the kicker itself with KICK <#channel> BADWORDS ON. Founder-or-admin.
|
||||
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) {
|
||||
let Some(&chan) = args.get(1) else {
|
||||
ctx.notice(me, from.uid, "Syntax: BADWORDS <#channel> ADD|DEL|LIST|CLEAR [pattern]");
|
||||
return;
|
||||
};
|
||||
// LIST is readable by the founder/admin too; gate everything the same way.
|
||||
if !super::require_channel_admin(me, from, chan, ctx, db) {
|
||||
return;
|
||||
}
|
||||
match args.get(2).map(|s| s.to_ascii_uppercase()).as_deref() {
|
||||
Some("ADD") => {
|
||||
if args.len() < 4 {
|
||||
ctx.notice(me, from.uid, "Syntax: BADWORDS <#channel> ADD <regex>");
|
||||
return;
|
||||
}
|
||||
let pattern = args[3..].join(" ");
|
||||
match db.badword_add(chan, &pattern) {
|
||||
Ok(true) => ctx.notice(me, from.uid, format!("Added badword pattern to \x02{chan}\x02: {pattern}")),
|
||||
Ok(false) => ctx.notice(me, from.uid, "That pattern is already on the list."),
|
||||
Err(ChanError::InvalidPattern) => ctx.notice(me, from.uid, format!("\x02{pattern}\x02 isn't a valid regular expression.")),
|
||||
Err(_) => reg_error(me, from, chan, ctx),
|
||||
}
|
||||
}
|
||||
Some("DEL") => {
|
||||
if args.len() < 4 {
|
||||
ctx.notice(me, from.uid, "Syntax: BADWORDS <#channel> DEL <regex>");
|
||||
return;
|
||||
}
|
||||
let pattern = args[3..].join(" ");
|
||||
match db.badword_del(chan, &pattern) {
|
||||
Ok(true) => ctx.notice(me, from.uid, format!("Removed badword pattern from \x02{chan}\x02.")),
|
||||
Ok(false) => ctx.notice(me, from.uid, "That pattern isn't on the list."),
|
||||
Err(_) => reg_error(me, from, chan, ctx),
|
||||
}
|
||||
}
|
||||
Some("CLEAR") => match db.badword_clear(chan) {
|
||||
Ok(n) => ctx.notice(me, from.uid, format!("Cleared \x02{n}\x02 badword pattern(s) from \x02{chan}\x02.")),
|
||||
Err(_) => reg_error(me, from, chan, ctx),
|
||||
},
|
||||
None | Some("LIST") => {
|
||||
let words = db.badwords(chan);
|
||||
if words.is_empty() {
|
||||
ctx.notice(me, from.uid, format!("\x02{chan}\x02 has no badword patterns."));
|
||||
return;
|
||||
}
|
||||
ctx.notice(me, from.uid, format!("Badword patterns for \x02{chan}\x02 ({}):", words.len()));
|
||||
for (i, w) in words.iter().enumerate() {
|
||||
ctx.notice(me, from.uid, format!(" {}. {w}", i + 1));
|
||||
}
|
||||
}
|
||||
Some(other) => ctx.notice(me, from.uid, format!("Unknown BADWORDS command \x02{other}\x02. Use ADD, DEL, LIST or CLEAR.")),
|
||||
}
|
||||
}
|
||||
|
||||
fn reg_error(me: &str, from: &Sender, chan: &str, ctx: &mut ServiceCtx) {
|
||||
ctx.notice(me, from.uid, format!("Couldn't update \x02{chan}\x02 — please try again in a moment."));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue