From 2c282d036ffa232ca4db0d90f4128b526d62a27b Mon Sep 17 00:00:00 2001 From: Jean Date: Thu, 16 Jul 2026 00:29:33 +0000 Subject: [PATCH] MemoServ: add SET NOTIFY and SET LIMIT SET NOTIFY {ON|OFF} controls whether you're told about new memos on login; SET LIMIT |NONE sets your mailbox cap (honoured on SEND, NONE = the network default). New memo_notify/memo_limit account fields + one MemoPrefsSet event. --- api/src/lib.rs | 4 ++++ modules/memoserv/src/lib.rs | 4 ++++ modules/memoserv/src/send.rs | 3 ++- modules/memoserv/src/set.rs | 38 ++++++++++++++++++++++++++++++ modules/nickserv/src/identify.rs | 2 +- src/engine/db/account.rs | 4 ++-- src/engine/db/channel.rs | 30 ++++++++++++++++++++++++ src/engine/db/event.rs | 8 +++++++ src/engine/db/mod.rs | 6 +++++ src/engine/db/store.rs | 12 ++++++++++ src/engine/db/tests.rs | 4 ++-- src/engine/mod.rs | 2 +- src/engine/tests.rs | 40 +++++++++++++++++++++++++++++++- src/grpc.rs | 3 +++ 14 files changed, 152 insertions(+), 8 deletions(-) create mode 100644 modules/memoserv/src/set.rs diff --git a/api/src/lib.rs b/api/src/lib.rs index 069cd2a..cdf6dc0 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -1057,6 +1057,10 @@ pub trait Store { fn memo_ignore_del(&mut self, account: &str, target: &str) -> bool; fn memo_ignores(&self, account: &str) -> Vec; fn memo_is_ignored(&self, account: &str, sender: &str) -> bool; + fn set_memo_notify(&mut self, account: &str, on: bool) -> bool; + fn set_memo_limit(&mut self, account: &str, limit: Option) -> bool; + fn memo_notify_on(&self, account: &str) -> bool; + fn memo_limit_of(&self, account: &str) -> Option; /// Read-status and timestamp of the most recent memo `sender` left for `account`. fn memo_check(&self, account: &str, sender: &str) -> Option<(bool, u64)>; fn set_entrymsg(&mut self, channel: &str, msg: &str) -> Result<(), ChanError>; diff --git a/modules/memoserv/src/lib.rs b/modules/memoserv/src/lib.rs index 20219a8..5eb992c 100644 --- a/modules/memoserv/src/lib.rs +++ b/modules/memoserv/src/lib.rs @@ -24,6 +24,8 @@ mod info; mod sendall; #[path = "ignore.rs"] mod ignore; +#[path = "set.rs"] +mod set; const BLURB: &str = "MemoServ delivers messages to registered users, online or not. You must be identified to use it."; @@ -40,6 +42,7 @@ const TOPICS: &[HelpEntry] = &[ HelpEntry { cmd: "INFO", summary: "your mailbox summary", detail: "Syntax: \x02INFO\x02\nShows how many memos you have, how many are unread, and your capacity." }, HelpEntry { cmd: "SENDALL", summary: "memo every account (admin)", detail: "Syntax: \x02SENDALL \x02\nLeaves a memo on every registered account. Requires the admin privilege." }, HelpEntry { cmd: "IGNORE", summary: "block memos from an account", detail: "Syntax: \x02IGNORE ADD | DEL | LIST\x02\nMemos from an ignored account are silently dropped." }, + HelpEntry { cmd: "SET", summary: "your memo preferences", detail: "Syntax: \x02SET NOTIFY {ON|OFF}\x02, \x02SET LIMIT |NONE\x02\nControls new-memo notifications and your mailbox size cap." }, ]; pub struct MemoServ { @@ -83,6 +86,7 @@ impl Service for MemoServ { Some("INFO") => info::handle(me, from, account, ctx, db), Some("SENDALL") => sendall::handle(me, from, account, args, ctx, db), Some("IGNORE") => ignore::handle(me, from, account, args, ctx, db), + Some("SET") => set::handle(me, from, account, args, ctx, db), Some(other) => ctx.notice(me, from.uid, format!("I don't know the command \x02{other}\x02. Try \x02HELP\x02.")), None => {} } diff --git a/modules/memoserv/src/send.rs b/modules/memoserv/src/send.rs index 1217b44..1a7f3d4 100644 --- a/modules/memoserv/src/send.rs +++ b/modules/memoserv/src/send.rs @@ -14,7 +14,8 @@ pub fn handle(me: &str, from: &Sender, account: &str, args: &[&str], ctx: &mut S ctx.notice(me, from.uid, format!("\x02{target}\x02 isn't registered.")); return; }; - if db.memo_list(&dest).len() >= MAX_MEMOS { + let limit = db.memo_limit_of(&dest).unwrap_or(MAX_MEMOS as u32) as usize; + if db.memo_list(&dest).len() >= limit { ctx.notice(me, from.uid, format!("\x02{target}\x02's mailbox is full — they'll need to clear some memos first.")); return; } diff --git a/modules/memoserv/src/set.rs b/modules/memoserv/src/set.rs new file mode 100644 index 0000000..b53c26a --- /dev/null +++ b/modules/memoserv/src/set.rs @@ -0,0 +1,38 @@ +use echo_api::{Sender, ServiceCtx, Store}; + +// SET NOTIFY {ON|OFF} | SET LIMIT |NONE: your MemoServ preferences. +pub fn handle(me: &str, from: &Sender, account: &str, args: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) { + match args.get(1).map(|s| s.to_ascii_uppercase()).as_deref() { + Some("NOTIFY") => { + let on = match args.get(2).map(|s| s.to_ascii_uppercase()).as_deref() { + Some("ON") => true, + Some("OFF") => false, + _ => { + ctx.notice(me, from.uid, "Syntax: SET NOTIFY {ON|OFF}"); + return; + } + }; + db.set_memo_notify(account, on); + ctx.notice(me, from.uid, format!("New-memo notification is now \x02{}\x02.", if on { "on" } else { "off" })); + } + Some("LIMIT") => { + let limit = match args.get(2) { + None => None, + Some(&n) if n.eq_ignore_ascii_case("NONE") || n.eq_ignore_ascii_case("OFF") => None, + Some(&n) => match n.parse::() { + Ok(v) if v <= super::MAX_MEMOS as u32 => Some(v), + _ => { + ctx.notice(me, from.uid, format!("The limit must be a number from 0 to {}, or NONE.", super::MAX_MEMOS)); + return; + } + }, + }; + db.set_memo_limit(account, limit); + match limit { + Some(v) => ctx.notice(me, from.uid, format!("Your mailbox limit is now \x02{v}\x02.")), + None => ctx.notice(me, from.uid, format!("Your mailbox limit is now the network default (\x02{}\x02).", super::MAX_MEMOS)), + } + } + _ => ctx.notice(me, from.uid, "Syntax: SET NOTIFY {ON|OFF} | SET LIMIT |NONE"), + } +} diff --git a/modules/nickserv/src/identify.rs b/modules/nickserv/src/identify.rs index 9c60e3b..a199126 100644 --- a/modules/nickserv/src/identify.rs +++ b/modules/nickserv/src/identify.rs @@ -65,7 +65,7 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: } // Let them know about waiting memos. let unread = db.unread_memos(&account); - if unread > 0 { + if unread > 0 && db.memo_notify_on(&account) { ctx.notice(me, from.uid, format!("You have \x02{unread}\x02 new memo(s). Read them with \x02/msg MemoServ READ NEW\x02.")); } } diff --git a/src/engine/db/account.rs b/src/engine/db/account.rs index 083cb21..0836bc9 100644 --- a/src/engine/db/account.rs +++ b/src/engine/db/account.rs @@ -20,7 +20,7 @@ impl Db { verified, ajoin: Vec::new(), suspension: None, - memos: Vec::new(), memo_ignore: Vec::new(), + memos: Vec::new(), memo_ignore: Vec::new(), memo_notify: true, memo_limit: None, greet: String::new(), vhost: None, vhost_request: None, @@ -59,7 +59,7 @@ impl Db { verified: true, // the external authority vouches for it ajoin: Vec::new(), suspension: None, - memos: Vec::new(), memo_ignore: Vec::new(), + memos: Vec::new(), memo_ignore: Vec::new(), memo_notify: true, memo_limit: None, greet: String::new(), vhost: None, vhost_request: None, diff --git a/src/engine/db/channel.rs b/src/engine/db/channel.rs index 601b900..e08e727 100644 --- a/src/engine/db/channel.rs +++ b/src/engine/db/channel.rs @@ -688,6 +688,36 @@ impl Db { self.accounts.get(&key(account)).is_some_and(|a| a.memo_ignore.iter().any(|t| t.eq_ignore_ascii_case(sender))) } + /// Set whether `account` is notified about new memos on login. + pub fn set_memo_notify(&mut self, account: &str, on: bool) -> bool { + let k = key(account); + let Some(a) = self.accounts.get(&k) else { return false }; + let limit = a.memo_limit; + let _ = self.log.append(Event::MemoPrefsSet { account: account.to_string(), notify: on, limit }); + self.accounts.get_mut(&k).unwrap().memo_notify = on; + true + } + + /// Set `account`'s mailbox cap (None = the network default). + pub fn set_memo_limit(&mut self, account: &str, limit: Option) -> bool { + let k = key(account); + let Some(a) = self.accounts.get(&k) else { return false }; + let notify = a.memo_notify; + let _ = self.log.append(Event::MemoPrefsSet { account: account.to_string(), notify, limit }); + self.accounts.get_mut(&k).unwrap().memo_limit = limit; + true + } + + /// Whether `account` wants new-memo notifications (default on / unknown = on). + pub fn memo_notify_on(&self, account: &str) -> bool { + self.accounts.get(&key(account)).is_none_or(|a| a.memo_notify) + } + + /// `account`'s configured mailbox cap, if any. + pub fn memo_limit_of(&self, account: &str) -> Option { + self.accounts.get(&key(account)).and_then(|a| a.memo_limit) + } + /// How many unread memos an account has. pub fn unread_memos(&self, account: &str) -> usize { self.accounts.get(&key(account)).map_or(0, |a| a.memos.iter().filter(|m| !m.read).count()) diff --git a/src/engine/db/event.rs b/src/engine/db/event.rs index f549c00..2c5e312 100644 --- a/src/engine/db/event.rs +++ b/src/engine/db/event.rs @@ -31,6 +31,7 @@ pub enum Event { MemoDeleted { account: String, index: usize }, MemoIgnoreAdd { account: String, target: String }, MemoIgnoreDel { account: String, target: String }, + MemoPrefsSet { account: String, notify: bool, limit: Option }, NickGrouped { nick: String, account: String }, NickUngrouped { nick: String }, ChannelRegistered { name: String, founder: String, ts: u64 }, @@ -160,6 +161,7 @@ impl Event { | Event::MemoDeleted { .. } | Event::MemoIgnoreAdd { .. } | Event::MemoIgnoreDel { .. } + | Event::MemoPrefsSet { .. } | Event::NickGrouped { .. } | Event::NickUngrouped { .. } | Event::AccountSeen { .. } @@ -354,6 +356,12 @@ pub(crate) fn apply(accounts: &mut HashMap, channels: &mut Hash a.memo_ignore.retain(|t| !t.eq_ignore_ascii_case(&target)); } } + Event::MemoPrefsSet { account, notify, limit } => { + if let Some(a) = accounts.get_mut(&key(&account)) { + a.memo_notify = notify; + a.memo_limit = limit; + } + } Event::NickGrouped { nick, account } => { grouped.insert(key(&nick), account); } diff --git a/src/engine/db/mod.rs b/src/engine/db/mod.rs index 6e50f75..10135a9 100644 --- a/src/engine/db/mod.rs +++ b/src/engine/db/mod.rs @@ -82,6 +82,12 @@ pub struct Account { // Accounts this user won't receive memos from (MemoServ IGNORE). #[serde(default)] pub memo_ignore: Vec, + // MemoServ SET NOTIFY: be told about new memos on login (default on). + #[serde(default = "verified_default")] + pub memo_notify: bool, + // MemoServ SET LIMIT: mailbox cap (None = the network default). + #[serde(default)] + pub memo_limit: Option, // Personal greet a bot shows when this account joins a greet-enabled channel. #[serde(default)] pub greet: String, diff --git a/src/engine/db/store.rs b/src/engine/db/store.rs index 52c1d35..f71d58e 100644 --- a/src/engine/db/store.rs +++ b/src/engine/db/store.rs @@ -468,6 +468,18 @@ impl Store for Db { fn memo_is_ignored(&self, account: &str, sender: &str) -> bool { Db::memo_is_ignored(self, account, sender) } + fn set_memo_notify(&mut self, account: &str, on: bool) -> bool { + Db::set_memo_notify(self, account, on) + } + fn set_memo_limit(&mut self, account: &str, limit: Option) -> bool { + Db::set_memo_limit(self, account, limit) + } + fn memo_notify_on(&self, account: &str) -> bool { + Db::memo_notify_on(self, account) + } + fn memo_limit_of(&self, account: &str) -> Option { + Db::memo_limit_of(self, account) + } fn memo_check(&self, account: &str, sender: &str) -> Option<(bool, u64)> { Db::memo_check(self, account, sender) } diff --git a/src/engine/db/tests.rs b/src/engine/db/tests.rs index 4b25f12..efcd226 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![], greet: String::new(), 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(), 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()); @@ -329,7 +329,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![], greet: String::new(), 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(), 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(); diff --git a/src/engine/mod.rs b/src/engine/mod.rs index f893144..37aadc3 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -1171,7 +1171,7 @@ fn audit_summary(event: &db::Event) -> Option { // Private, self-service, or cosmetic — not surfaced. AjoinAdded { .. } | AjoinRemoved { .. } | AccountGreetSet { .. } | VhostRequested { .. } | VhostRequestCleared { .. } | MemoSent { .. } | MemoRead { .. } | MemoDeleted { .. } - | MemoIgnoreAdd { .. } | MemoIgnoreDel { .. } + | MemoIgnoreAdd { .. } | MemoIgnoreDel { .. } | MemoPrefsSet { .. } | ChannelMlock { .. } | ChannelDescSet { .. } | ChannelEntryMsgSet { .. } | ChannelSettingsSet { .. } | ChannelKickerSet { .. } | ChannelBadwordsSet { .. } | ChannelTriggersSet { .. } | ChannelTopicSet { .. } | AccountSeen { .. } | ChannelUsed { .. } diff --git a/src/engine/tests.rs b/src/engine/tests.rs index 12fb9a9..75e937d 100644 --- a/src/engine/tests.rs +++ b/src/engine/tests.rs @@ -411,7 +411,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![], greet: String::new(), 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(), 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(); @@ -875,6 +875,44 @@ assert_eq!(e.db.unread_memos("alice"), 1, "delivered once no longer ignored"); } + // MemoServ SET NOTIFY/LIMIT change the account's memo preferences, and LIMIT + // caps the mailbox on SEND. + #[test] + fn memoserv_set_notify_and_limit() { + use echo_memoserv::MemoServ; + use echo_nickserv::NickServ; + let path = std::env::temp_dir().join("echo-msset.jsonl"); + let _ = std::fs::remove_file(&path); + let mut db = Db::open(&path, "42S"); + db.scram_iterations = 4096; + db.register("alice", "pw", None).unwrap(); + db.register("bob", "pw", None).unwrap(); + let mut e = Engine::new( + vec![ + Box::new(NickServ { uid: "42SAAAAAA".into(), guest_nick: "Guest".into(), guest_seq: 0 }), + Box::new(MemoServ { uid: "42SAAAAAE".into() }), + ], + db, + ); + let ms = |e: &mut Engine, uid: &str, t: &str| e.handle(NetEvent::Privmsg { from: uid.into(), to: "42SAAAAAE".into(), text: t.into() }); + let notice = |out: &[NetAction], n: &str| out.iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains(n))); + e.handle(NetEvent::UserConnect { uid: "000AAAAAB".into(), nick: "alice".into(), host: "h".into(), ip: "0.0.0.0".into() }); + e.handle(NetEvent::Privmsg { from: "000AAAAAB".into(), to: "42SAAAAAA".into(), text: "IDENTIFY pw".into() }); + e.handle(NetEvent::UserConnect { uid: "000AAAAAC".into(), nick: "bob".into(), host: "h".into(), ip: "0.0.0.0".into() }); + e.handle(NetEvent::Privmsg { from: "000AAAAAC".into(), to: "42SAAAAAA".into(), text: "IDENTIFY pw".into() }); + + assert!(notice(&ms(&mut e, "000AAAAAB", "SET NOTIFY OFF"), "off"), "notify off"); + assert!(!e.db.memo_notify_on("alice"), "notify pref stored"); + assert!(notice(&ms(&mut e, "000AAAAAB", "SET LIMIT 2"), "2"), "limit set"); + assert_eq!(e.db.memo_limit_of("alice"), Some(2), "limit stored"); + + // bob fills alice's 2-memo mailbox; the third is rejected. + ms(&mut e, "000AAAAAC", "SEND alice one"); + ms(&mut e, "000AAAAAC", "SEND alice two"); + assert!(notice(&ms(&mut e, "000AAAAAC", "SEND alice three"), "mailbox is full"), "limit enforced on SEND"); + assert_eq!(e.db.unread_memos("alice"), 2, "capped at the limit"); + } + // ChanServ AKICK CLEAR empties the auto-kick list. #[test] fn chanserv_akick_clear() { diff --git a/src/grpc.rs b/src/grpc.rs index 34e38d8..a063861 100644 --- a/src/grpc.rs +++ b/src/grpc.rs @@ -141,6 +141,7 @@ fn to_wire(entry: &LogEntry) -> Option { | Event::MemoDeleted { .. } | Event::MemoIgnoreAdd { .. } | Event::MemoIgnoreDel { .. } + | Event::MemoPrefsSet { .. } | Event::ChannelMlock { .. } | Event::ChannelAccessAdd { .. } | Event::ChannelAccessDel { .. } @@ -457,6 +458,8 @@ mod tests { suspension: None, memos: vec![], memo_ignore: vec![], + memo_notify: true, + memo_limit: None, greet: String::new(), vhost: None, vhost_request: None,