diff --git a/api/src/lib.rs b/api/src/lib.rs index fb475c7..27dbb97 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -1814,6 +1814,8 @@ pub trait Store { // NickServ SET HIDE STATUS: whether this account hides its last-seen line. fn set_account_hide_status(&mut self, account: &str, on: bool) -> Result<(), RegError>; fn account_hides_status(&self, account: &str) -> bool; + fn set_account_snotice(&mut self, account: &str, on: bool) -> Result<(), RegError>; + fn account_wants_snotice(&self, account: &str) -> bool; // HostServ vhosts. fn set_vhost(&mut self, account: &str, host: &str, setter: &str, ttl: Option) -> Result<(), RegError>; fn del_vhost(&mut self, account: &str) -> Result; diff --git a/modules/nickserv/src/set.rs b/modules/nickserv/src/set.rs index ea645c3..9af49d8 100644 --- a/modules/nickserv/src/set.rs +++ b/modules/nickserv/src/set.rs @@ -92,6 +92,18 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."), } } + Some("SNOTICE") | Some("SNOTICES") => { + let Some(on) = args.get(2).and_then(|s| parse_toggle(s)) else { + let state = if db.account_wants_snotice(account) { "ON" } else { "OFF" }; + ctx.notice(me, from.uid, format!("SNOTICE is \x02{state}\x02. Syntax: SET SNOTICE {{ON|OFF}}")); + return; + }; + match db.set_account_snotice(account, on) { + Ok(()) if on => ctx.notice(me, from.uid, "Service replies now arrive as server notices (\x02*** NickServ: …\x02)."), + Ok(()) => ctx.notice(me, from.uid, "Service replies now arrive as normal notices from the service."), + Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."), + } + } Some("GREET") => { // A bot shows this when you join a greet-enabled channel; no arg clears it. let greet = if args.len() > 2 { args[2..].join(" ") } else { String::new() }; @@ -102,7 +114,7 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."), } } - _ => ctx.notice(me, from.uid, "Syntax: SET PASSWORD | SET EMAIL [address] | SET GREET [message] | SET AUTOOP {ON|OFF} | SET KILL {ON|OFF} | SET HIDE STATUS {ON|OFF}"), + _ => ctx.notice(me, from.uid, "Syntax: SET PASSWORD | SET EMAIL [address] | SET GREET [message] | SET AUTOOP {ON|OFF} | SET KILL {ON|OFF} | SET HIDE STATUS {ON|OFF} | SET SNOTICE {ON|OFF}"), } } diff --git a/src/engine/db/account.rs b/src/engine/db/account.rs index b7248c6..4fb7e57 100644 --- a/src/engine/db/account.rs +++ b/src/engine/db/account.rs @@ -22,7 +22,7 @@ impl Db { ajoin: Vec::new(), suspension: None, memos: Vec::new(), memo_ignore: Vec::new(), memo_notify: true, memo_limit: None, - greet: String::new(), no_autoop: false, no_protect: false, hide_status: false, + greet: String::new(), no_autoop: false, no_protect: false, hide_status: false, snotice: false, vhost: None, vhost_request: None, last_seen: ts, @@ -78,7 +78,7 @@ impl Db { ajoin: Vec::new(), suspension: None, memos: Vec::new(), memo_ignore: Vec::new(), memo_notify: true, memo_limit: None, - greet: String::new(), no_autoop: false, no_protect: false, hide_status: false, + greet: String::new(), no_autoop: false, no_protect: false, hide_status: false, snotice: false, vhost: None, vhost_request: None, last_seen: ts, @@ -563,6 +563,22 @@ impl Db { self.accounts.get(&key(account)).is_some_and(|a| a.hide_status) } + /// NickServ SET SNOTICE: whether service replies come as a server notice. + pub fn set_account_snotice(&mut self, account: &str, on: bool) -> Result<(), RegError> { + let k = key(account); + if !self.accounts.contains_key(&k) { + return Err(RegError::Internal); + } + self.log.append(Event::AccountSnoticeSet { account: account.to_string(), on }).map_err(|_| RegError::Internal)?; + self.accounts.get_mut(&k).unwrap().snotice = on; + Ok(()) + } + + /// Whether `account` wants server-notice-style service replies (default false). + pub fn account_wants_snotice(&self, account: &str) -> bool { + self.accounts.get(&key(account)).is_some_and(|a| a.snotice) + } + /// Replace `account`'s password with freshly derived credentials. pub fn set_credentials(&mut self, account: &str, creds: Credentials) -> Result<(), RegError> { let k = key(account); diff --git a/src/engine/db/event.rs b/src/engine/db/event.rs index bb5ac88..7a959f5 100644 --- a/src/engine/db/event.rs +++ b/src/engine/db/event.rs @@ -18,6 +18,7 @@ pub enum Event { AccountAutoOpSet { account: String, on: bool }, AccountKillSet { account: String, on: bool }, AccountHideStatusSet { account: String, on: bool }, + AccountSnoticeSet { account: String, on: bool }, AccountPasswordSet { account: String, scram256: String, scram512: String }, AccountDropped { account: String }, AccountVerified { account: String }, @@ -176,6 +177,7 @@ impl Event { | Event::AccountAutoOpSet { .. } | Event::AccountKillSet { .. } | Event::AccountHideStatusSet { .. } + | Event::AccountSnoticeSet { .. } | Event::AccountPasswordSet { .. } | Event::AccountDropped { .. } | Event::AccountVerified { .. } @@ -352,6 +354,11 @@ pub(crate) fn apply(accounts: &mut HashMap, channels: &mut Hash a.hide_status = on; } } + Event::AccountSnoticeSet { account, on } => { + if let Some(a) = accounts.get_mut(&key(&account)) { + a.snotice = on; + } + } Event::AccountPasswordSet { account, scram256, scram512 } => { if let Some(a) = accounts.get_mut(&key(&account)) { a.scram256 = Some(scram256); diff --git a/src/engine/db/mod.rs b/src/engine/db/mod.rs index 7598131..a96f07d 100644 --- a/src/engine/db/mod.rs +++ b/src/engine/db/mod.rs @@ -106,6 +106,11 @@ pub struct Account { // (visible) is the zero value. #[serde(default)] pub hide_status: bool, + // NickServ SET SNOTICE: when set, service replies to this user come as a + // server notice ("*** NickServ: …") instead of a normal notice from the + // pseudoclient. Default (normal notice) is the zero value. + #[serde(default)] + pub snotice: bool, // Assigned vhost (HostServ), applied to the displayed host on identify. #[serde(default)] pub vhost: Option, diff --git a/src/engine/db/store.rs b/src/engine/db/store.rs index d834d11..48269aa 100644 --- a/src/engine/db/store.rs +++ b/src/engine/db/store.rs @@ -123,6 +123,12 @@ impl Store for Db { fn account_hides_status(&self, account: &str) -> bool { Db::account_hides_status(self, account) } + fn set_account_snotice(&mut self, account: &str, on: bool) -> Result<(), RegError> { + Db::set_account_snotice(self, account, on) + } + fn account_wants_snotice(&self, account: &str) -> bool { + Db::account_wants_snotice(self, account) + } fn set_vhost(&mut self, account: &str, host: &str, setter: &str, ttl: Option) -> Result<(), RegError> { Db::set_vhost(self, account, host, setter, ttl) } diff --git a/src/engine/db/tests.rs b/src/engine/db/tests.rs index ac28d11..1b11aa6 100644 --- a/src/engine/db/tests.rs +++ b/src/engine/db/tests.rs @@ -32,7 +32,7 @@ // which of the two competing registrations we're looking at. let alice = |tag: &str, ts: u64, home: &str| Account { name: "alice".into(), email: Some(tag.into()), - ts, home: home.into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], memo_ignore: vec![], memo_notify: true, memo_limit: None, greet: String::new(), no_autoop: false, no_protect: false, hide_status: false, vhost: None, vhost_request: None, last_seen: ts, noexpire: false, expiry_warned: false, oper_note: None, + ts, home: home.into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], memo_ignore: vec![], memo_notify: true, memo_limit: None, greet: String::new(), no_autoop: false, no_protect: false, hide_status: false, snotice: false, vhost: None, vhost_request: None, last_seen: ts, noexpire: false, expiry_warned: false, oper_note: None, }; let converge = |first: &Account, second: &Account| { let (mut acc, mut ch, mut gr, mut bo, mut hc, mut nd) = (HashMap::new(), HashMap::new(), HashMap::new(), HashMap::new(), HostConfig::default(), NetData::default()); @@ -64,7 +64,7 @@ scram256: Some(verifier.into()), scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], memo_ignore: vec![], memo_notify: true, memo_limit: None, greet: String::new(), no_autoop: false, no_protect: false, - hide_status: false, vhost: None, vhost_request: None, last_seen: ts, noexpire: false, + hide_status: false, snotice: false, vhost: None, vhost_request: None, last_seen: ts, noexpire: false, expiry_warned: false, oper_note: None, }; let reg = |origin: &str, a: Account| LogEntry::for_test(origin, 1, 1, Event::AccountRegistered(Box::new(a))); @@ -134,6 +134,18 @@ assert!(!db.akick_del("#c", "*!*@bad.host").unwrap()); } + // SET SNOTICE toggles the per-account server-notice preference (default off). + #[test] + fn snotice_pref_toggles() { + let mut db = Db::open(tmp("snotice"), "N1"); + db.register("bob", "pw", None).unwrap(); + assert!(!db.account_wants_snotice("bob"), "default is a normal notice"); + db.set_account_snotice("bob", true).unwrap(); + assert!(db.account_wants_snotice("bob")); + db.set_account_snotice("bob", false).unwrap(); + assert!(!db.account_wants_snotice("bob")); + } + // LEVELS grants are additive (default = unchanged), tier-gated, and reversible. #[test] fn levels_grant_is_additive_and_tier_gated() { @@ -410,7 +422,7 @@ db.register("alice", "pw", None).unwrap(); let bob = Account { name: "bob".into(), email: None, - ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], memo_ignore: vec![], memo_notify: true, memo_limit: None, greet: String::new(), no_autoop: false, no_protect: false, hide_status: false, vhost: None, vhost_request: None, last_seen: 0, noexpire: false, expiry_warned: false, oper_note: None, + ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], memo_ignore: vec![], memo_notify: true, memo_limit: None, greet: String::new(), no_autoop: false, no_protect: false, hide_status: false, snotice: false, vhost: None, vhost_request: None, last_seen: 0, noexpire: false, expiry_warned: false, oper_note: None, }; let entry = LogEntry { origin: "peer".into(), seq: 0, lamport: 1, event: Event::AccountRegistered(Box::new(bob)) }; db.ingest(entry).unwrap(); @@ -477,7 +489,7 @@ scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], memo_ignore: vec![], memo_notify: true, memo_limit: None, greet: String::new(), no_autoop: false, no_protect: false, - hide_status: false, vhost: None, vhost_request: None, last_seen: 1, + hide_status: false, snotice: false, vhost: None, vhost_request: None, last_seen: 1, noexpire: false, expiry_warned: false, oper_note: None, }))).unwrap(); } diff --git a/src/engine/dispatch.rs b/src/engine/dispatch.rs index 205a69a..7bbf84b 100644 --- a/src/engine/dispatch.rs +++ b/src/engine/dispatch.rs @@ -67,9 +67,27 @@ impl Engine { out.extend(self.reconcile_bots()); // Announce whatever this command changed to the staff audit channel. out.extend(self.audit_feed(audit_mark, &nick, account.as_deref())); + self.apply_msg_style(&mut out); out } + // Rewrite each service→user NOTICE to a server-notice ("*** NickServ: …", + // sourced from our server) for users who opted into SET SNOTICE. Everyone + // else's reply is left as a normal notice from the pseudoclient. Per target, + // so notices to channels or to users who didn't opt in are untouched. + fn apply_msg_style(&self, out: &mut [NetAction]) { + for a in out.iter_mut() { + let NetAction::Notice { from, to, text } = a else { continue }; + let Some(account) = self.network.account_of(to) else { continue }; + if !self.db.account_wants_snotice(account) { + continue; + } + let Some(nick) = self.services.iter().find(|s| s.uid() == *from).map(|s| s.nick()) else { continue }; + *text = format!("*** {nick}: {text}"); + *from = self.sid.clone(); + } + } + // fantasy word becomes the command and the channel is injected as its first // argument — then re-source the resulting actions from the bot, so the bot is // the visible actor. diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 4ef4f53..6f3bda8 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -1776,7 +1776,7 @@ fn audit_summary(event: &db::Event) -> Option { format!("{verb} channel \x02{channel}\x02 against expiry") } // Private, self-service, or cosmetic — not surfaced. - AjoinAdded { .. } | AjoinRemoved { .. } | AccountGreetSet { .. } | AccountAutoOpSet { .. } | AccountKillSet { .. } | AccountHideStatusSet { .. } | VhostRequested { .. } + AjoinAdded { .. } | AjoinRemoved { .. } | AccountGreetSet { .. } | AccountAutoOpSet { .. } | AccountKillSet { .. } | AccountHideStatusSet { .. } | AccountSnoticeSet { .. } | VhostRequested { .. } | VhostRequestCleared { .. } | MemoSent { .. } | MemoRead { .. } | MemoDeleted { .. } | MemoIgnoreAdd { .. } | MemoIgnoreDel { .. } | MemoPrefsSet { .. } | ChannelMlock { .. } | ChannelDescSet { .. } | ChannelEntryMsgSet { .. } | ChannelUrlSet { .. } | ChannelEmailSet { .. } | ChannelSettingsSet { .. } diff --git a/src/engine/tests.rs b/src/engine/tests.rs index 96db6c0..18c2b0c 100644 --- a/src/engine/tests.rs +++ b/src/engine/tests.rs @@ -850,7 +850,7 @@ // An earlier claim from another node wins and takes the name over. let winner = db::Account { name: "alice".into(), email: None, - ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], memo_ignore: vec![], memo_notify: true, memo_limit: None, greet: String::new(), no_autoop: false, no_protect: false, hide_status: false, vhost: None, vhost_request: None, last_seen: 0, noexpire: false, expiry_warned: false, oper_note: None, + ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], memo_ignore: vec![], memo_notify: true, memo_limit: None, greet: String::new(), no_autoop: false, no_protect: false, hide_status: false, snotice: false, vhost: None, vhost_request: None, last_seen: 0, noexpire: false, expiry_warned: false, oper_note: None, }; let entry = LogEntry::for_test("peer", 0, 1, db::Event::AccountRegistered(Box::new(winner))); e.gossip_ingest(entry).unwrap(); diff --git a/src/grpc.rs b/src/grpc.rs index 18545c8..8e1926b 100644 --- a/src/grpc.rs +++ b/src/grpc.rs @@ -131,6 +131,7 @@ fn to_wire(entry: &LogEntry) -> Option { | Event::AccountAutoOpSet { .. } | Event::AccountKillSet { .. } | Event::AccountHideStatusSet { .. } + | Event::AccountSnoticeSet { .. } | Event::AjoinAdded { .. } | Event::AjoinRemoved { .. } | Event::VhostSet { .. } @@ -486,7 +487,7 @@ mod tests { memo_ignore: vec![], memo_notify: true, memo_limit: None, - greet: String::new(), no_autoop: false, no_protect: false, hide_status: false, + greet: String::new(), no_autoop: false, no_protect: false, hide_status: false, snotice: false, vhost: None, vhost_request: None, last_seen: 111, diff --git a/src/migrate.rs b/src/migrate.rs index e9729c7..ce14a26 100644 --- a/src/migrate.rs +++ b/src/migrate.rs @@ -200,7 +200,7 @@ pub fn import_anope(anope_path: &str, out_path: &str, node: &str) -> std::io::Re greet: String::new(), no_autoop: !flag(nc, "AUTOOP"), no_protect: !flag(nc, "PROTECT"), - hide_status: flag(nc, "HIDE_MASK"), + hide_status: flag(nc, "HIDE_MASK"), snotice: false, vhost, vhost_request: None, last_seen: last_seen_of.get(name).copied().unwrap_or(ts),