diff --git a/api/src/lib.rs b/api/src/lib.rs index 23c2818..6e6ebd0 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -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_set_private(&mut self, nick: &str, private: bool) -> Result; fn bot_del(&mut self, nick: &str) -> Result; + fn bot_del_all(&mut self) -> Result; fn bots(&self) -> Vec; fn assign_bot(&mut self, channel: &str, bot: &str) -> Result<(), ChanError>; fn unassign_bot(&mut self, channel: &str) -> Result; diff --git a/botserv/src/bot.rs b/botserv/src/bot.rs index 7da2d46..227f162 100644 --- a/botserv/src/bot.rs +++ b/botserv/src/bot.rs @@ -40,9 +40,16 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: } Some("DEL") => { let Some(&nick) = args.get(2) else { - ctx.notice(me, from.uid, "Syntax: BOT DEL "); + ctx.notice(me, from.uid, "Syntax: BOT DEL (or \x02*\x02 for all)"); 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) { 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.")), diff --git a/src/engine/db.rs b/src/engine/db.rs index fc3a4b3..8b41a39 100644 --- a/src/engine/db.rs +++ b/src/engine/db.rs @@ -1610,6 +1610,16 @@ impl Db { Ok(true) } + /// Delete every service bot at once (BOT DEL *). Returns how many went. + pub fn bot_del_all(&mut self) -> Result { + let nicks: Vec = 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. pub fn bot_del(&mut self, nick: &str) -> Result { let k = key(nick); @@ -2211,6 +2221,9 @@ impl Store for Db { fn bot_del(&mut self, nick: &str) -> Result { Db::bot_del(self, nick) } + fn bot_del_all(&mut self) -> Result { + Db::bot_del_all(self) + } fn bots(&self) -> Vec { 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() } diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 8da31c2..846e0ba 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -2078,6 +2078,40 @@ mod tests { 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 // it when only the identity changes; the network client is refreshed both ways. #[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"); } + // 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. #[test] fn botserv_say_speaks_through_bot() {