BotServ: BOT DEL * mass removal (Anope #315)

BOT DEL * removes every registered bot at once, quitting each pseudo-
client, for cleaning up after a bot spree.
This commit is contained in:
Jean Chevronnet 2026-07-13 17:50:50 +00:00
parent 0e90dd8020
commit 9f5f0ed0d2
No known key found for this signature in database
4 changed files with 103 additions and 1 deletions

View file

@ -582,6 +582,7 @@ pub trait Store {
fn bot_change(&mut self, old: &str, new_nick: &str, user: &str, host: &str, gecos: &str) -> Result<(), ChanError>; fn bot_change(&mut self, old: &str, new_nick: &str, user: &str, host: &str, gecos: &str) -> Result<(), ChanError>;
fn bot_set_private(&mut self, nick: &str, private: bool) -> Result<bool, ChanError>; fn bot_set_private(&mut self, nick: &str, private: bool) -> Result<bool, ChanError>;
fn bot_del(&mut self, nick: &str) -> Result<bool, ChanError>; fn bot_del(&mut self, nick: &str) -> Result<bool, ChanError>;
fn bot_del_all(&mut self) -> Result<usize, ChanError>;
fn bots(&self) -> Vec<BotView>; fn bots(&self) -> Vec<BotView>;
fn assign_bot(&mut self, channel: &str, bot: &str) -> Result<(), ChanError>; fn assign_bot(&mut self, channel: &str, bot: &str) -> Result<(), ChanError>;
fn unassign_bot(&mut self, channel: &str) -> Result<bool, ChanError>; fn unassign_bot(&mut self, channel: &str) -> Result<bool, ChanError>;

View file

@ -40,9 +40,16 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
} }
Some("DEL") => { Some("DEL") => {
let Some(&nick) = args.get(2) else { let Some(&nick) = args.get(2) else {
ctx.notice(me, from.uid, "Syntax: BOT DEL <nick>"); ctx.notice(me, from.uid, "Syntax: BOT DEL <nick> (or \x02*\x02 for all)");
return; return;
}; };
if nick == "*" {
match db.bot_del_all() {
Ok(n) => ctx.notice(me, from.uid, format!("Removed all \x02{n}\x02 bot(s).")),
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
}
return;
}
match db.bot_del(nick) { match db.bot_del(nick) {
Ok(true) => ctx.notice(me, from.uid, format!("Bot \x02{nick}\x02 deleted.")), Ok(true) => ctx.notice(me, from.uid, format!("Bot \x02{nick}\x02 deleted.")),
Ok(false) => ctx.notice(me, from.uid, format!("There's no bot named \x02{nick}\x02.")), Ok(false) => ctx.notice(me, from.uid, format!("There's no bot named \x02{nick}\x02.")),

View file

@ -1610,6 +1610,16 @@ impl Db {
Ok(true) Ok(true)
} }
/// Delete every service bot at once (BOT DEL *). Returns how many went.
pub fn bot_del_all(&mut self) -> Result<usize, ChanError> {
let nicks: Vec<String> = self.bots.values().map(|b| b.nick.clone()).collect();
for n in &nicks {
self.log.append(Event::BotRemoved { nick: n.clone() }).map_err(|_| ChanError::Internal)?;
}
self.bots.clear();
Ok(nicks.len())
}
/// Delete a service bot. Returns whether it existed. /// Delete a service bot. Returns whether it existed.
pub fn bot_del(&mut self, nick: &str) -> Result<bool, ChanError> { pub fn bot_del(&mut self, nick: &str) -> Result<bool, ChanError> {
let k = key(nick); let k = key(nick);
@ -2211,6 +2221,9 @@ impl Store for Db {
fn bot_del(&mut self, nick: &str) -> Result<bool, ChanError> { fn bot_del(&mut self, nick: &str) -> Result<bool, ChanError> {
Db::bot_del(self, nick) Db::bot_del(self, nick)
} }
fn bot_del_all(&mut self) -> Result<usize, ChanError> {
Db::bot_del_all(self)
}
fn bots(&self) -> Vec<BotView> { fn bots(&self) -> Vec<BotView> {
Db::bots(self).map(|b| BotView { nick: b.nick.clone(), user: b.user.clone(), host: b.host.clone(), gecos: b.gecos.clone(), private: b.private }).collect() Db::bots(self).map(|b| BotView { nick: b.nick.clone(), user: b.user.clone(), host: b.host.clone(), gecos: b.gecos.clone(), private: b.private }).collect()
} }

View file

@ -2078,6 +2078,40 @@ mod tests {
assert!(out.iter().any(|a| matches!(a, NetAction::ServicePart { channel, .. } if channel == "#c")), "parts on unassign: {out:?}"); assert!(out.iter().any(|a| matches!(a, NetAction::ServicePart { channel, .. } if channel == "#c")), "parts on unassign: {out:?}");
} }
// BOT DEL * removes every bot at once (Anope issue #315), quitting each.
#[test]
fn botserv_mass_bot_removal() {
use fedserv_botserv::BotServ;
use fedserv_nickserv::NickServ;
let path = std::env::temp_dir().join("fedserv-bsmass.jsonl");
let _ = std::fs::remove_file(&path);
let mut db = Db::open(&path, "42S");
db.scram_iterations = 4096;
db.register("boss", "password1", None).unwrap();
let mut e = Engine::new(
vec![
Box::new(NickServ { uid: "42SAAAAAA".into(), guest_nick: "Guest".into(), guest_seq: 0 }),
Box::new(BotServ { uid: "42SAAAAAD".into() }),
],
db,
);
e.set_sid("42S".into());
let mut opers = std::collections::HashMap::new();
opers.insert("boss".to_string(), Privs::default().with(fedserv_api::Priv::Admin));
e.set_opers(opers);
let bs = |e: &mut Engine, t: &str| e.handle(NetEvent::Privmsg { from: "000AAAAAB".into(), to: "42SAAAAAD".into(), text: t.into() });
e.handle(NetEvent::UserConnect { uid: "000AAAAAB".into(), nick: "boss".into(), host: "h".into() });
e.handle(NetEvent::Privmsg { from: "000AAAAAB".into(), to: "42SAAAAAA".into(), text: "IDENTIFY password1".into() });
bs(&mut e, "BOT ADD One a serv.host Bot");
bs(&mut e, "BOT ADD Two b serv.host Bot");
let out = bs(&mut e, "BOT DEL *");
let quits = out.iter().filter(|a| matches!(a, NetAction::QuitUser { .. })).count();
assert_eq!(quits, 2, "both bots quit: {out:?}");
// The registry is empty afterwards.
assert!(!bs(&mut e, "BOT LIST").iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains("One") || text.contains("Two"))), "registry cleared");
}
// BOT CHANGE renames a bot (moving its channel assignments) and reintroduces // BOT CHANGE renames a bot (moving its channel assignments) and reintroduces
// it when only the identity changes; the network client is refreshed both ways. // it when only the identity changes; the network client is refreshed both ways.
#[test] #[test]
@ -2121,6 +2155,53 @@ mod tests {
assert!(out.iter().any(|a| matches!(a, NetAction::ServiceJoin { channel, .. } if channel == "#c")), "rejoins #c after re-identify"); assert!(out.iter().any(|a| matches!(a, NetAction::ServiceJoin { channel, .. } if channel == "#c")), "rejoins #c after re-identify");
} }
// NOBOT (channel) and PRIVATE (bot) both reserve assignment for operators:
// the founder is refused, a services admin is allowed.
#[test]
fn botserv_nobot_and_private_gate_assign() {
use fedserv_botserv::BotServ;
use fedserv_nickserv::NickServ;
let path = std::env::temp_dir().join("fedserv-bsprot.jsonl");
let _ = std::fs::remove_file(&path);
let mut db = Db::open(&path, "42S");
db.scram_iterations = 4096;
db.register("boss", "password1", None).unwrap(); // admin
db.register("owner", "password1", None).unwrap(); // plain founder
db.register_channel("#c", "owner").unwrap();
db.register_channel("#d", "owner").unwrap();
let mut e = Engine::new(
vec![
Box::new(NickServ { uid: "42SAAAAAA".into(), guest_nick: "Guest".into(), guest_seq: 0 }),
Box::new(BotServ { uid: "42SAAAAAD".into() }),
],
db,
);
e.set_sid("42S".into());
let mut opers = std::collections::HashMap::new();
opers.insert("boss".to_string(), Privs::default().with(fedserv_api::Priv::Admin));
e.set_opers(opers);
let ns = |e: &mut Engine, uid: &str, t: &str| e.handle(NetEvent::Privmsg { from: uid.into(), to: "42SAAAAAA".into(), text: t.into() });
let boss = |e: &mut Engine, t: &str| e.handle(NetEvent::Privmsg { from: "000AAAAAB".into(), to: "42SAAAAAD".into(), text: t.into() });
let owner = |e: &mut Engine, t: &str| e.handle(NetEvent::Privmsg { from: "000AAAAAO".into(), to: "42SAAAAAD".into(), text: t.into() });
e.handle(NetEvent::UserConnect { uid: "000AAAAAB".into(), nick: "boss".into(), host: "h".into() });
e.handle(NetEvent::UserConnect { uid: "000AAAAAO".into(), nick: "owner".into(), host: "h".into() });
ns(&mut e, "000AAAAAB", "IDENTIFY password1");
ns(&mut e, "000AAAAAO", "IDENTIFY password1");
boss(&mut e, "BOT ADD Bendy bot serv.host Helper");
let joined = |out: &[NetAction], ch: &str| out.iter().any(|a| matches!(a, NetAction::ServiceJoin { channel, .. } if channel == ch));
let denied = |out: &[NetAction], word: &str| out.iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains(word)));
// NOBOT on #c: the founder is refused, the admin isn't.
boss(&mut e, "SET #c NOBOT ON");
assert!(denied(&owner(&mut e, "ASSIGN #c Bendy"), "NOBOT"), "founder blocked by NOBOT");
assert!(joined(&boss(&mut e, "ASSIGN #c Bendy"), "#c"), "admin overrides NOBOT");
// Private bot on #d: the founder is refused, the admin isn't.
boss(&mut e, "SET Bendy PRIVATE ON");
assert!(denied(&owner(&mut e, "ASSIGN #d Bendy"), "private"), "founder blocked from private bot");
assert!(joined(&boss(&mut e, "ASSIGN #d Bendy"), "#d"), "admin assigns private bot");
}
// SAY/ACT speak through the channel's assigned bot, sourced from the bot's uid. // SAY/ACT speak through the channel's assigned bot, sourced from the bot's uid.
#[test] #[test]
fn botserv_say_speaks_through_bot() { fn botserv_say_speaks_through_bot() {