BotServ GREET + NickServ SET GREET

Members set a personal greet with NickServ SET GREET; a channel founder
enables display with BotServ SET <#channel> GREET ON. On join, if greets
are enabled and the member has channel access and a non-empty greet, the
assigned bot shows [account] greet in the channel.

Per-account greet is a typed field on Account (AccountGreetSet event, rides
the account snapshot); the per-channel toggle is a new ChanSetting::BotGreet
bool. Greet is public in NickServ INFO.
This commit is contained in:
Jean Chevronnet 2026-07-13 14:43:48 +00:00
parent 801bfc5c96
commit ad8041bd9d
No known key found for this signature in database
8 changed files with 172 additions and 5 deletions

View file

@ -55,6 +55,9 @@ pub struct Account {
// Memos left for this account (MemoServ), oldest first.
#[serde(default)]
pub memos: Vec<Memo>,
// Personal greet a bot shows when this account joins a greet-enabled channel.
#[serde(default)]
pub greet: String,
}
fn verified_default() -> bool {
@ -71,6 +74,7 @@ pub enum Event {
CertAdded { account: String, fp: String },
CertRemoved { account: String, fp: String },
AccountEmailSet { account: String, email: Option<String> },
AccountGreetSet { account: String, greet: String },
AccountPasswordSet { account: String, password_hash: String, scram256: String, scram512: String },
AccountDropped { account: String },
AccountVerified { account: String },
@ -120,6 +124,7 @@ impl Event {
| Event::CertAdded { .. }
| Event::CertRemoved { .. }
| Event::AccountEmailSet { .. }
| Event::AccountGreetSet { .. }
| Event::AccountPasswordSet { .. }
| Event::AccountDropped { .. }
| Event::AccountVerified { .. }
@ -229,6 +234,9 @@ pub struct ChanSettings {
// Revert topic changes made by users without op-level access.
#[serde(default)]
pub topiclock: bool,
// BotServ: show members' personal greets when they join.
#[serde(default)]
pub bot_greet: bool,
}
// A registered channel and who owns it.
@ -792,6 +800,7 @@ impl Db {
ajoin: Vec::new(),
suspension: None,
memos: Vec::new(),
greet: String::new(),
};
self.log.append(Event::AccountRegistered(account.clone())).map_err(|_| RegError::Internal)?;
self.accounts.insert(key(name), account);
@ -961,6 +970,17 @@ impl Db {
Ok(())
}
/// Set (or clear, when empty) `account`'s personal greet.
pub fn set_greet(&mut self, account: &str, greet: &str) -> Result<(), RegError> {
let k = key(account);
if !self.accounts.contains_key(&k) {
return Err(RegError::Internal);
}
self.log.append(Event::AccountGreetSet { account: account.to_string(), greet: greet.to_string() }).map_err(|_| RegError::Internal)?;
self.accounts.get_mut(&k).unwrap().greet = greet.to_string();
Ok(())
}
/// Replace `account`'s password with freshly derived credentials.
pub fn set_credentials(&mut self, account: &str, creds: Credentials) -> Result<(), RegError> {
let k = key(account);
@ -1260,6 +1280,7 @@ impl Db {
ChanSetting::SecureOps => settings.secureops = on,
ChanSetting::KeepTopic => settings.keeptopic = on,
ChanSetting::TopicLock => settings.topiclock = on,
ChanSetting::BotGreet => settings.bot_greet = on,
}
self.log
.append(Event::ChannelSettingsSet { channel: channel.to_string(), settings })
@ -1486,6 +1507,11 @@ fn apply(accounts: &mut HashMap<String, Account>, channels: &mut HashMap<String,
a.email = email;
}
}
Event::AccountGreetSet { account, greet } => {
if let Some(a) = accounts.get_mut(&key(&account)) {
a.greet = greet;
}
}
Event::AccountPasswordSet { account, password_hash, scram256, scram512 } => {
if let Some(a) = accounts.get_mut(&key(&account)) {
a.password_hash = password_hash;
@ -1703,6 +1729,7 @@ impl Store for Db {
email: a.email.clone(),
ts: a.ts,
verified: a.verified,
greet: a.greet.clone(),
})
}
fn resolve_account(&self, name: &str) -> Option<&str> {
@ -1759,6 +1786,9 @@ impl Store for Db {
fn set_email(&mut self, account: &str, email: Option<String>) -> Result<(), RegError> {
Db::set_email(self, account, email)
}
fn set_greet(&mut self, account: &str, greet: &str) -> Result<(), RegError> {
Db::set_greet(self, account, greet)
}
fn group_nick(&mut self, nick: &str, account: &str) -> Result<(), RegError> {
Db::group_nick(self, nick, account)
}
@ -1898,6 +1928,7 @@ fn channel_view(c: &ChannelInfo) -> ChannelView {
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(),
bot_greet: c.settings.bot_greet,
}
}
@ -1935,7 +1966,7 @@ mod tests {
fn account_conflict_resolves_deterministically() {
let alice = |hash: &str, ts: u64, home: &str| Account {
name: "alice".into(), password_hash: hash.into(), email: None,
ts, home: home.into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![],
ts, home: home.into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(),
};
let converge = |first: &Account, second: &Account| {
let (mut acc, mut ch, mut gr, mut bo) = (HashMap::new(), HashMap::new(), HashMap::new(), HashMap::new());
@ -2211,7 +2242,7 @@ mod tests {
db.register("alice", "pw", None).unwrap();
let bob = Account {
name: "bob".into(), password_hash: "x".into(), email: None,
ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![],
ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(),
};
let entry = LogEntry { origin: "peer".into(), seq: 0, lamport: 1, event: Event::AccountRegistered(bob) };
db.ingest(entry).unwrap();

View file

@ -529,6 +529,8 @@ impl Engine {
let from = self.chan_service.clone().unwrap_or_default();
let mode = account.as_deref().and_then(|a| self.db.channel(&channel).and_then(|c| c.join_mode(a)));
let entrymsg = self.db.channel(&channel).map(|c| c.entrymsg.clone()).filter(|m| !m.is_empty());
// Computed before the match below moves `channel` into its actions.
let greet = self.greet_on_join(&channel, account.as_deref());
let mut out = Vec::new();
// SECUREOPS: a user who arrives opped (FJOIN prefix) but lacks op-level
// access loses it, unless we're about to grant it to them anyway.
@ -566,6 +568,11 @@ impl Engine {
}
}
}
// GREET: if the channel shows greets and this member has channel
// access + a personal greet, the assigned bot displays it.
if let Some(g) = greet {
out.push(g);
}
out
}
NetEvent::Part { uid, channel } => {
@ -872,6 +879,24 @@ impl Engine {
out
}
// The greet a channel's bot shows when a member logged into `account` joins:
// only when greets are enabled for the channel, the member holds channel
// access, and has set a non-empty personal greet.
fn greet_on_join(&self, channel: &str, account: Option<&str>) -> Option<NetAction> {
let acc = account?;
let c = self.db.channel(channel)?;
if !c.settings.bot_greet || c.join_mode(acc).is_none() {
return None;
}
let bot = c.assigned_bot.clone()?;
let greet = self.db.account(acc)?.greet.clone();
if greet.is_empty() {
return None;
}
let botuid = self.network.uid_by_nick(&bot)?.to_string();
Some(NetAction::Privmsg { from: botuid, to: channel.to_string(), text: format!("[{acc}] {greet}") })
}
// Fantasy commands: `!op nick`, `!kick nick`, `!topic …` spoken in a channel
// that has a bot assigned. We reuse ChanServ's real command handlers — the
// fantasy word becomes the command and the channel is injected as its first
@ -1442,7 +1467,7 @@ mod tests {
// An earlier claim from another node wins and takes the name over.
let winner = db::Account {
name: "alice".into(), password_hash: "OTHER".into(), email: None,
ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![],
ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(),
};
let entry = LogEntry::for_test("peer", 0, 1, db::Event::AccountRegistered(winner));
e.gossip_ingest(entry).unwrap();
@ -1854,6 +1879,56 @@ mod tests {
assert!(out.iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains("Access denied"))), "denied notice: {out:?}");
}
// A member's personal greet is shown by the assigned bot on join, once the
// channel enables greets and the member has access.
#[test]
fn botserv_greet_shown_on_join() {
use fedserv_botserv::BotServ;
use fedserv_chanserv::ChanServ;
use fedserv_nickserv::NickServ;
let path = std::env::temp_dir().join("fedserv-bsgreet.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(ChanServ { uid: "42SAAAAAB".into() }),
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);
e.chan_service = Some("42SAAAAAB".into());
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() });
ns(&mut e, "000AAAAAB", "IDENTIFY password1");
ns(&mut e, "000AAAAAB", "SET GREET hello all");
bs(&mut e, "000AAAAAB", "BOT ADD Bendy bot serv.host Helper");
bs(&mut e, "000AAAAAB", "ASSIGN #c Bendy");
// Greets off by default: joining is silent.
assert!(
!e.handle(NetEvent::Join { uid: "000AAAAAB".into(), channel: "#c".into(), op: true }).iter().any(|a| matches!(a, NetAction::Privmsg { .. })),
"no greet before it is enabled"
);
e.handle(NetEvent::Part { uid: "000AAAAAB".into(), channel: "#c".into() });
bs(&mut e, "000AAAAAB", "SET #c GREET ON");
// Now the founder's greet is shown by the bot.
let out = e.handle(NetEvent::Join { uid: "000AAAAAB".into(), channel: "#c".into(), op: true });
assert!(
out.iter().any(|a| matches!(a, NetAction::Privmsg { from, to, text } if from.starts_with("42SB") && to == "#c" && text == "[boss] hello all")),
"greet shown: {out:?}"
);
}
// Fantasy commands typed in-channel route through ChanServ and are sourced
// from the assigned bot.
#[test]