From c8be5b4e954919c739f704fe733ee92d3acdc742 Mon Sep 17 00:00:00 2001 From: Jean Date: Mon, 13 Jul 2026 17:55:19 +0000 Subject: [PATCH] BotServ: COPY <#src> <#dst> to clone bot config Copies a channel's kickers, badword list, greet and nobot settings onto another channel (founder-or-admin on both). Cheap because the config is a typed KickerSettings + Vec, not a bag of stringly extensibles. --- api/src/lib.rs | 2 ++ botserv/src/copy.rs | 17 +++++++++++++++++ botserv/src/lib.rs | 3 +++ src/engine/db.rs | 22 ++++++++++++++++++++++ src/engine/mod.rs | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+) create mode 100644 botserv/src/copy.rs diff --git a/api/src/lib.rs b/api/src/lib.rs index 15c8e23..e42b155 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -574,6 +574,8 @@ pub trait Store { fn badwords(&self, channel: &str) -> Vec; // Dry-run the content kickers against a line; Some(reason) if it would kick. fn kicker_test(&self, channel: &str, text: &str) -> Option; + // Copy one channel's BotServ config (kickers/badwords/greet/nobot) to another. + fn copy_bot_config(&mut self, src: &str, dst: &str) -> Result<(), ChanError>; fn set_channel_topic(&mut self, channel: &str, topic: &str) -> Result<(), ChanError>; fn suspend_channel(&mut self, channel: &str, by: &str, reason: &str, expires: Option) -> Result<(), ChanError>; fn unsuspend_channel(&mut self, channel: &str) -> Result; diff --git a/botserv/src/copy.rs b/botserv/src/copy.rs new file mode 100644 index 0000000..3ddd0cd --- /dev/null +++ b/botserv/src/copy.rs @@ -0,0 +1,17 @@ +use fedserv_api::{Sender, ServiceCtx, Store}; + +// COPY <#source> <#dest>: copy a channel's bot configuration — kickers, +// badwords, greet and nobot — onto another. Requires founder-or-admin on both. +pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) { + let (Some(&src), Some(&dst)) = (args.get(1), args.get(2)) else { + ctx.notice(me, from.uid, "Syntax: COPY <#source> <#dest>"); + return; + }; + if !super::require_channel_admin(me, from, src, ctx, db) || !super::require_channel_admin(me, from, dst, ctx, db) { + return; + } + match db.copy_bot_config(src, dst) { + Ok(()) => ctx.notice(me, from.uid, format!("Copied \x02{src}\x02's bot settings (kickers, badwords, greet, nobot) to \x02{dst}\x02.")), + Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."), + } +} diff --git a/botserv/src/lib.rs b/botserv/src/lib.rs index baeda37..52b8083 100644 --- a/botserv/src/lib.rs +++ b/botserv/src/lib.rs @@ -18,6 +18,8 @@ mod set; mod kick; #[path = "badwords.rs"] mod badwords; +#[path = "copy.rs"] +mod copy; // Shared gate: the sender may administer 's bot options only as its // founder or a services admin. Notices and returns false on failure. @@ -60,6 +62,7 @@ impl Service for BotServ { Some("SET") => set::handle(me, from, args, ctx, db), Some("KICK") => kick::handle(me, from, args, ctx, db), Some("BADWORDS") => badwords::handle(me, from, args, ctx, db), + Some("COPY") => copy::handle(me, from, args, ctx, db), Some("HELP") | None => ctx.notice(me, from.uid, "BotServ keeps service bots for your channels: \x02ASSIGN\x02 <#channel> puts a bot in your channel, \x02UNASSIGN\x02 <#channel> removes it, \x02INFO\x02 shows details, \x02SAY\x02/\x02ACT\x02 <#channel> speaks through the bot, \x02SET\x02 <#channel> GREET toggles greets, \x02KICK\x02 <#channel> 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.")), } diff --git a/src/engine/db.rs b/src/engine/db.rs index cbd2d66..d4f3f08 100644 --- a/src/engine/db.rs +++ b/src/engine/db.rs @@ -1539,6 +1539,25 @@ impl Db { None } + /// Copy one channel's BotServ configuration — kickers, badwords, greet and + /// nobot — onto another. Both channels must be registered. + pub fn copy_bot_config(&mut self, src: &str, dst: &str) -> Result<(), ChanError> { + let (kickers, badwords, bot_greet, nobot) = { + let s = self.channels.get(&key(src)).ok_or(ChanError::NoChannel)?; + (s.kickers.clone(), s.badwords.clone(), s.settings.bot_greet, s.settings.nobot) + }; + let dk = key(dst); + if !self.channels.contains_key(&dk) { + return Err(ChanError::NoChannel); + } + self.log.append(Event::ChannelKickerSet { channel: dst.to_string(), kickers: kickers.clone() }).map_err(|_| ChanError::Internal)?; + self.channels.get_mut(&dk).unwrap().kickers = kickers; + self.write_badwords(dst, &dk, badwords)?; + self.set_channel_setting(dst, ChanSetting::BotGreet, bot_greet)?; + self.set_channel_setting(dst, ChanSetting::NoBot, nobot)?; + Ok(()) + } + // Persist a new badword list (whole-list event) and apply it. fn write_badwords(&mut self, channel: &str, k: &str, list: Vec) -> Result<(), ChanError> { self.log @@ -2221,6 +2240,9 @@ impl Store for Db { fn kicker_test(&self, channel: &str, text: &str) -> Option { Db::kicker_test(self, channel, text) } + fn copy_bot_config(&mut self, src: &str, dst: &str) -> Result<(), ChanError> { + Db::copy_bot_config(self, src, dst) + } fn set_channel_topic(&mut self, channel: &str, topic: &str) -> Result<(), ChanError> { Db::set_channel_topic(self, channel, topic) } diff --git a/src/engine/mod.rs b/src/engine/mod.rs index aaeae86..b77773b 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -2354,6 +2354,45 @@ mod tests { assert!(!say(&mut e, "[unclosed bracket text").iter().any(|a| matches!(a, NetAction::Kick { .. })), "bad pattern not stored"); } + // COPY clones one channel's kicker/badword config onto another. + #[test] + fn botserv_copy_bot_config() { + use fedserv_botserv::BotServ; + use fedserv_nickserv::NickServ; + let path = std::env::temp_dir().join("fedserv-bscopy.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(); + db.register_channel("#d", "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(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, "KICK #c CAPS ON"); + bs(&mut e, "BADWORDS #c ADD fr[ao]g"); + bs(&mut e, "KICK #c BADWORDS ON"); + let says = |out: &[NetAction], n: &str| out.iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains(n))); + + // #d starts blank, so nothing trips. + assert!(says(&bs(&mut e, "KICK #d TEST SHOUTING LOUD ALLCAPS"), "trips no content kicker"), "blank #d"); + bs(&mut e, "COPY #c #d"); + // Now #d has #c's caps and badword config. + assert!(says(&bs(&mut e, "KICK #d TEST SHOUTING LOUD ALLCAPS"), "would be kicked"), "caps copied"); + assert!(says(&bs(&mut e, "KICK #d TEST i love frogs"), "would be kicked"), "badwords copied"); + } + // KICK TEST dry-runs the content kickers against a line without kicking. #[test] fn botserv_kick_test_dry_run() {