MemoServ: add SET NOTIFY and SET LIMIT
All checks were successful
CI / check (push) Successful in 3m35s

SET NOTIFY {ON|OFF} controls whether you're told about new memos on login;
SET LIMIT <n>|NONE sets your mailbox cap (honoured on SEND, NONE = the
network default). New memo_notify/memo_limit account fields + one
MemoPrefsSet event.
This commit is contained in:
Jean Chevronnet 2026-07-16 00:29:33 +00:00
parent 125b8c7701
commit 2c282d036f
No known key found for this signature in database
14 changed files with 152 additions and 8 deletions

View file

@ -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,

View file

@ -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<u32>) -> 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<u32> {
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())

View file

@ -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<u32> },
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<String, Account>, 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);
}

View file

@ -82,6 +82,12 @@ pub struct Account {
// Accounts this user won't receive memos from (MemoServ IGNORE).
#[serde(default)]
pub memo_ignore: Vec<String>,
// 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<u32>,
// Personal greet a bot shows when this account joins a greet-enabled channel.
#[serde(default)]
pub greet: String,

View file

@ -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<u32>) -> 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<u32> {
Db::memo_limit_of(self, account)
}
fn memo_check(&self, account: &str, sender: &str) -> Option<(bool, u64)> {
Db::memo_check(self, account, sender)
}

View file

@ -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();

View file

@ -1171,7 +1171,7 @@ fn audit_summary(event: &db::Event) -> Option<String> {
// 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 { .. }

View file

@ -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() {

View file

@ -141,6 +141,7 @@ fn to_wire(entry: &LogEntry) -> Option<ReplicationEvent> {
| 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,