botserv: ASSIGN / UNASSIGN a bot to a channel
A channel gains a typed assigned_bot (event-logged, snapshotted). ASSIGN <#channel> <bot> / UNASSIGN <#channel> is founder-gated (or admin oper). The engine reconcile now also diffs channel assignments against tracked bot memberships: a live bot IJOINs its assigned channels and PARTs unassigned ones (new ServiceJoin/ServicePart actions), at burst and after commands; a deleted bot's memberships are dropped. Engine test for join-on-assign / part-on-unassign.
This commit is contained in:
parent
f94b0afcd9
commit
b25870e14b
6 changed files with 170 additions and 4 deletions
|
|
@ -97,6 +97,8 @@ pub enum Event {
|
|||
ChannelTopicSet { channel: String, topic: String },
|
||||
ChannelSuspended { channel: String, by: String, reason: String, ts: u64, expires: Option<u64> },
|
||||
ChannelUnsuspended { channel: String },
|
||||
ChannelBotAssigned { channel: String, bot: String },
|
||||
ChannelBotUnassigned { channel: String },
|
||||
BotAdded(Bot),
|
||||
BotRemoved { nick: String },
|
||||
}
|
||||
|
|
@ -144,6 +146,8 @@ impl Event {
|
|||
| Event::ChannelTopicSet { .. }
|
||||
| Event::ChannelSuspended { .. }
|
||||
| Event::ChannelUnsuspended { .. }
|
||||
| Event::ChannelBotAssigned { .. }
|
||||
| Event::ChannelBotUnassigned { .. }
|
||||
| Event::BotAdded(_)
|
||||
| Event::BotRemoved { .. } => Scope::Local,
|
||||
}
|
||||
|
|
@ -257,6 +261,9 @@ pub struct ChannelInfo {
|
|||
// Services suspension, if any (channel frozen while set and unexpired).
|
||||
#[serde(default)]
|
||||
pub suspension: Option<Suspension>,
|
||||
// BotServ bot assigned to sit in this channel, if any (its nick).
|
||||
#[serde(default)]
|
||||
pub assigned_bot: Option<String>,
|
||||
}
|
||||
|
||||
impl ChannelInfo {
|
||||
|
|
@ -672,6 +679,9 @@ impl Db {
|
|||
if let Some(s) = &c.suspension {
|
||||
snapshot.push(Event::ChannelSuspended { channel: c.name.clone(), by: s.by.clone(), reason: s.reason.clone(), ts: s.ts, expires: s.expires });
|
||||
}
|
||||
if let Some(bot) = &c.assigned_bot {
|
||||
snapshot.push(Event::ChannelBotAssigned { channel: c.name.clone(), bot: bot.clone() });
|
||||
}
|
||||
}
|
||||
for (nick, account) in &self.grouped {
|
||||
snapshot.push(Event::NickGrouped { nick: nick.clone(), account: account.clone() });
|
||||
|
|
@ -1102,7 +1112,7 @@ impl Db {
|
|||
self.log
|
||||
.append(Event::ChannelRegistered { name: name.to_string(), founder: founder.to_string(), ts })
|
||||
.map_err(|_| ChanError::Internal)?;
|
||||
self.channels.insert(k, ChannelInfo { name: name.to_string(), founder: founder.to_string(), ts, lock_on: String::new(), lock_off: String::new(), access: Vec::new(), akick: Vec::new(), desc: String::new(), entrymsg: String::new(), settings: ChanSettings::default(), topic: String::new(), suspension: None });
|
||||
self.channels.insert(k, ChannelInfo { name: name.to_string(), founder: founder.to_string(), ts, lock_on: String::new(), lock_off: String::new(), access: Vec::new(), akick: Vec::new(), desc: String::new(), entrymsg: String::new(), settings: ChanSettings::default(), topic: String::new(), suspension: None, assigned_bot: None });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -1334,6 +1344,30 @@ impl Db {
|
|||
self.bots.values()
|
||||
}
|
||||
|
||||
/// Assign a bot to a channel.
|
||||
pub fn assign_bot(&mut self, channel: &str, bot: &str) -> Result<(), ChanError> {
|
||||
let k = key(channel);
|
||||
if !self.channels.contains_key(&k) {
|
||||
return Err(ChanError::NoChannel);
|
||||
}
|
||||
self.log.append(Event::ChannelBotAssigned { channel: channel.to_string(), bot: bot.to_string() }).map_err(|_| ChanError::Internal)?;
|
||||
self.channels.get_mut(&k).unwrap().assigned_bot = Some(bot.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Unassign a channel's bot. Returns whether one was assigned.
|
||||
pub fn unassign_bot(&mut self, channel: &str) -> Result<bool, ChanError> {
|
||||
let k = key(channel);
|
||||
match self.channels.get(&k) {
|
||||
None => return Err(ChanError::NoChannel),
|
||||
Some(c) if c.assigned_bot.is_none() => return Ok(false),
|
||||
Some(_) => {}
|
||||
}
|
||||
self.log.append(Event::ChannelBotUnassigned { channel: channel.to_string() }).map_err(|_| ChanError::Internal)?;
|
||||
self.channels.get_mut(&k).unwrap().assigned_bot = None;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Append a memo to an account's mailbox.
|
||||
pub fn memo_send(&mut self, account: &str, from: &str, text: &str) -> Result<(), RegError> {
|
||||
let k = key(account);
|
||||
|
|
@ -1514,7 +1548,7 @@ fn apply(accounts: &mut HashMap<String, Account>, channels: &mut HashMap<String,
|
|||
grouped.remove(&key(&nick));
|
||||
}
|
||||
Event::ChannelRegistered { name, founder, ts } => {
|
||||
channels.insert(key(&name), ChannelInfo { name, founder, ts, lock_on: String::new(), lock_off: String::new(), access: Vec::new(), akick: Vec::new(), desc: String::new(), entrymsg: String::new(), settings: ChanSettings::default(), topic: String::new(), suspension: None });
|
||||
channels.insert(key(&name), ChannelInfo { name, founder, ts, lock_on: String::new(), lock_off: String::new(), access: Vec::new(), akick: Vec::new(), desc: String::new(), entrymsg: String::new(), settings: ChanSettings::default(), topic: String::new(), suspension: None, assigned_bot: None });
|
||||
}
|
||||
Event::ChannelDropped { name } => {
|
||||
channels.remove(&key(&name));
|
||||
|
|
@ -1577,6 +1611,16 @@ fn apply(accounts: &mut HashMap<String, Account>, channels: &mut HashMap<String,
|
|||
c.suspension = None;
|
||||
}
|
||||
}
|
||||
Event::ChannelBotAssigned { channel, bot } => {
|
||||
if let Some(c) = channels.get_mut(&key(&channel)) {
|
||||
c.assigned_bot = Some(bot);
|
||||
}
|
||||
}
|
||||
Event::ChannelBotUnassigned { channel } => {
|
||||
if let Some(c) = channels.get_mut(&key(&channel)) {
|
||||
c.assigned_bot = None;
|
||||
}
|
||||
}
|
||||
Event::BotAdded(b) => {
|
||||
bots.insert(key(&b.nick), b);
|
||||
}
|
||||
|
|
@ -1793,6 +1837,12 @@ impl Store for Db {
|
|||
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() }).collect()
|
||||
}
|
||||
fn assign_bot(&mut self, channel: &str, bot: &str) -> Result<(), ChanError> {
|
||||
Db::assign_bot(self, channel, bot)
|
||||
}
|
||||
fn unassign_bot(&mut self, channel: &str) -> Result<bool, ChanError> {
|
||||
Db::unassign_bot(self, channel)
|
||||
}
|
||||
fn memo_send(&mut self, account: &str, from: &str, text: &str) -> Result<(), RegError> {
|
||||
Db::memo_send(self, account, from, text)
|
||||
}
|
||||
|
|
@ -1847,6 +1897,7 @@ fn channel_view(c: &ChannelInfo) -> ChannelView {
|
|||
topiclock: c.settings.topiclock,
|
||||
topic: c.topic.clone(),
|
||||
suspended: c.suspension.as_ref().is_some_and(|s| s.expires.is_none_or(|e| e > now())),
|
||||
assigned_bot: c.assigned_bot.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -94,6 +94,7 @@ pub struct Engine {
|
|||
sid: String, // our services SID, for minting bot uids
|
||||
bot_uids: HashMap<String, String>, // casefolded bot nick -> live uid (per connection)
|
||||
next_bot_index: u32,
|
||||
bot_channels: std::collections::HashSet<(String, String)>, // (bot nick lc, channel) the bot has joined
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
|
|
@ -113,6 +114,7 @@ impl Engine {
|
|||
sid: String::new(),
|
||||
bot_uids: HashMap::new(),
|
||||
next_bot_index: 0,
|
||||
bot_channels: std::collections::HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -159,6 +161,28 @@ impl Engine {
|
|||
if let Some(uid) = self.bot_uids.remove(&k) {
|
||||
out.push(NetAction::QuitUser { uid, reason: "Bot removed".to_string() });
|
||||
}
|
||||
self.bot_channels.retain(|(b, _)| *b != k); // its channel memberships go with it
|
||||
}
|
||||
// Join assigned channels, part unassigned ones (for still-live bots).
|
||||
let assigned: Vec<(String, String)> = self.db.channels()
|
||||
.filter_map(|c| c.assigned_bot.as_ref().map(|b| (b.to_ascii_lowercase(), c.name.clone())))
|
||||
.collect();
|
||||
let desired: std::collections::HashSet<(String, String)> =
|
||||
assigned.into_iter().filter(|(b, _)| self.bot_uids.contains_key(b)).collect();
|
||||
for (bot_lc, chan) in &desired {
|
||||
if !self.bot_channels.contains(&(bot_lc.clone(), chan.clone())) {
|
||||
if let Some(uid) = self.bot_uids.get(bot_lc) {
|
||||
out.push(NetAction::ServiceJoin { uid: uid.clone(), channel: chan.clone() });
|
||||
self.bot_channels.insert((bot_lc.clone(), chan.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
let to_part: Vec<(String, String)> = self.bot_channels.iter().filter(|bc| !desired.contains(*bc)).cloned().collect();
|
||||
for (bot_lc, chan) in to_part {
|
||||
self.bot_channels.remove(&(bot_lc.clone(), chan.clone()));
|
||||
if let Some(uid) = self.bot_uids.get(&bot_lc) {
|
||||
out.push(NetAction::ServicePart { uid: uid.clone(), channel: chan });
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
|
@ -426,6 +450,7 @@ impl Engine {
|
|||
pub fn startup_actions(&mut self) -> Vec<NetAction> {
|
||||
// Fresh link: forget bot uids minted on any previous connection.
|
||||
self.bot_uids.clear();
|
||||
self.bot_channels.clear();
|
||||
self.next_bot_index = 0;
|
||||
let mut out = vec![NetAction::Burst];
|
||||
for svc in &self.services {
|
||||
|
|
@ -1674,6 +1699,46 @@ mod tests {
|
|||
assert!(e.startup_actions().iter().any(|a| matches!(a, NetAction::IntroduceUser { nick, .. } if nick == "Roger")), "introduced at burst");
|
||||
}
|
||||
|
||||
// ASSIGN puts the bot in the channel; UNASSIGN takes it out.
|
||||
#[test]
|
||||
fn botserv_assign_joins_and_unassign_parts() {
|
||||
use fedserv_botserv::BotServ;
|
||||
use fedserv_nickserv::NickServ;
|
||||
let path = std::env::temp_dir().join("fedserv-bsassign.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("staff", "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("staff".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 bs = |e: &mut Engine, uid: &str, t: &str| e.handle(NetEvent::Privmsg { from: uid.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: "000AAAAAC".into(), nick: "staff".into(), host: "h".into() });
|
||||
ns(&mut e, "000AAAAAB", "IDENTIFY password1");
|
||||
ns(&mut e, "000AAAAAC", "IDENTIFY password1");
|
||||
bs(&mut e, "000AAAAAC", "BOT ADD Bendy bot serv.host Helper"); // goes live
|
||||
|
||||
// The founder assigns the bot: it joins the channel.
|
||||
let out = bs(&mut e, "000AAAAAB", "ASSIGN #c Bendy");
|
||||
assert!(out.iter().any(|a| matches!(a, NetAction::ServiceJoin { channel, .. } if channel == "#c")), "joins on assign: {out:?}");
|
||||
// Unassigning parts it.
|
||||
let out = bs(&mut e, "000AAAAAB", "UNASSIGN #c");
|
||||
assert!(out.iter().any(|a| matches!(a, NetAction::ServicePart { channel, .. } if channel == "#c")), "parts on unassign: {out:?}");
|
||||
}
|
||||
|
||||
// MemoServ delivers a memo to an offline account and notifies them on login.
|
||||
#[test]
|
||||
fn memoserv_delivers_and_notifies_on_login() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue