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."));
|
||||
}
|
||||
|
|
@ -66,6 +66,7 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
|
|||
"UNDERLINES" => Kicker::Underlines,
|
||||
"REVERSES" => Kicker::Reverses,
|
||||
"ITALICS" => Kicker::Italics,
|
||||
"BADWORDS" => Kicker::Badwords,
|
||||
"DONTKICKOPS" => Kicker::DontKickOps,
|
||||
other => {
|
||||
ctx.notice(me, from.uid, format!("Unknown kicker \x02{other}\x02. Try CAPS, FLOOD, REPEAT, BOLDS, COLORS, UNDERLINES, REVERSES, ITALICS or DONTKICKOPS."));
|
||||
|
|
@ -85,6 +86,7 @@ fn label(kicker: Kicker) -> &'static str {
|
|||
Kicker::Italics => "italics",
|
||||
Kicker::Flood => "flood",
|
||||
Kicker::Repeat => "repeat",
|
||||
Kicker::Badwords => "badwords",
|
||||
Kicker::DontKickOps => "dontkickops",
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ mod say;
|
|||
mod set;
|
||||
#[path = "kick.rs"]
|
||||
mod kick;
|
||||
#[path = "badwords.rs"]
|
||||
mod badwords;
|
||||
|
||||
// Shared gate: the sender may administer <chan>'s bot options only as its
|
||||
// founder or a services admin. Notices and returns false on failure.
|
||||
|
|
@ -57,7 +59,8 @@ impl Service for BotServ {
|
|||
Some("ACT") => say::handle(me, from, args, ctx, net, db, true),
|
||||
Some("SET") => set::handle(me, from, args, ctx, db),
|
||||
Some("KICK") => kick::handle(me, from, args, ctx, db),
|
||||
Some("HELP") | None => ctx.notice(me, from.uid, "BotServ keeps service bots for your channels: \x02ASSIGN\x02 <#channel> <bot> puts a bot in your channel, \x02UNASSIGN\x02 <#channel> removes it, \x02INFO\x02 <bot|#channel> shows details, \x02SAY\x02/\x02ACT\x02 <#channel> <text> speaks through the bot, \x02SET\x02 <#channel> GREET <on|off> toggles greets, \x02KICK\x02 <#channel> <type> <on|off> configures kickers. Operators also have \x02BOT\x02 ADD|DEL|LIST."),
|
||||
Some("BADWORDS") => badwords::handle(me, from, args, ctx, db),
|
||||
Some("HELP") | None => ctx.notice(me, from.uid, "BotServ keeps service bots for your channels: \x02ASSIGN\x02 <#channel> <bot> puts a bot in your channel, \x02UNASSIGN\x02 <#channel> removes it, \x02INFO\x02 <bot|#channel> shows details, \x02SAY\x02/\x02ACT\x02 <#channel> <text> speaks through the bot, \x02SET\x02 <#channel> GREET <on|off> toggles greets, \x02KICK\x02 <#channel> <type> <on|off> configures kickers, \x02BADWORDS\x02 <#channel> ADD|DEL|LIST manages badword regexes. Operators also have \x02BOT\x02 ADD|DEL|LIST."),
|
||||
Some(other) => ctx.notice(me, from.uid, format!("I don't know the command \x02{other}\x02. Try \x02HELP\x02.")),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue