From a9a3408de0a3977253b3370ceb55513defa9fae4 Mon Sep 17 00:00:00 2001 From: Jean Date: Thu, 16 Jul 2026 10:03:00 +0000 Subject: [PATCH] engine: a kicked services bot rejoins its assigned channel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A PART/KICK of a bot uid updated channel membership but not bot_channels, so reconcile still believed the bot was present and never rejoined it — an op could evict a services bot for good. The Part handler now drops the stale membership and reconciles, so a bot assigned to the channel comes right back. --- src/engine/mod.rs | 8 ++++++++ src/engine/tests.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/engine/mod.rs b/src/engine/mod.rs index b88102f..0e9ff7d 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -981,6 +981,14 @@ impl Engine { NetEvent::Part { uid, channel } => { self.network.channel_part(&channel, &uid); self.forget_chatter(&channel, &uid); + // A services bot kicked/parted from a channel it's still assigned + // to rejoins — an op can't evict it. Drop the stale membership so + // reconcile re-adds it. + if let Some(bot_lc) = self.bot_uids.iter().find_map(|(lc, u)| (u == &uid).then(|| lc.clone())) { + if self.bot_channels.remove(&(bot_lc, channel.clone())) { + return self.reconcile_bots(); + } + } Vec::new() } NetEvent::ChannelOp { channel, uid, op } => { diff --git a/src/engine/tests.rs b/src/engine/tests.rs index aee424a..9cc9c1d 100644 --- a/src/engine/tests.rs +++ b/src/engine/tests.rs @@ -1822,6 +1822,44 @@ assert!(out.iter().any(|a| matches!(a, NetAction::ServicePart { channel, .. } if channel == "#c")), "parts on unassign: {out:?}"); } + // A services bot kicked from a channel it's assigned to rejoins at once — an + // op can't evict it. + #[test] + fn kicked_bot_rejoins_its_channel() { + use echo_botserv::BotServ; + use echo_nickserv::NickServ; + let path = std::env::temp_dir().join("echo-kickbot.jsonl"); + let _ = std::fs::remove_file(&path); + let mut db = Db::open(&path, "42S"); + db.scram_iterations = 4096; + db.register("boss", "password1", None).unwrap(); + db.register_channel("#c", "boss").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(echo_api::Priv::Admin)); + e.set_opers(opers); + let ns = |e: &mut Engine, t: &str| e.handle(NetEvent::Privmsg { from: "000AAAAAB".into(), to: "42SAAAAAA".into(), text: t.into() }); + 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() , ip: "0.0.0.0".into() }); + ns(&mut e, "IDENTIFY password1"); + bs(&mut e, "BOT ADD Bendy bot serv.host Helper"); + let bot_uid = bs(&mut e, "ASSIGN #c Bendy").iter().find_map(|a| match a { + NetAction::ServiceJoin { uid, channel } if channel == "#c" => Some(uid.clone()), + _ => None, + }).expect("bot joined on assign"); + + // The bot is kicked out (the ircd relays this as a PART for its uid). + let out = e.handle(NetEvent::Part { uid: bot_uid.clone(), channel: "#c".into() }); + assert!(out.iter().any(|a| matches!(a, NetAction::ServiceJoin { uid, channel } if uid == &bot_uid && channel == "#c")), "kicked bot rejoins: {out:?}"); + } + // A channel that expires with an assigned bot parts the bot in the same // sweep — it must not linger in a channel it no longer serves. #[test]