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

@ -1057,6 +1057,10 @@ pub trait Store {
fn memo_ignore_del(&mut self, account: &str, target: &str) -> bool; fn memo_ignore_del(&mut self, account: &str, target: &str) -> bool;
fn memo_ignores(&self, account: &str) -> Vec<String>; fn memo_ignores(&self, account: &str) -> Vec<String>;
fn memo_is_ignored(&self, account: &str, sender: &str) -> bool; 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<u32>) -> bool;
fn memo_notify_on(&self, account: &str) -> bool;
fn memo_limit_of(&self, account: &str) -> Option<u32>;
/// Read-status and timestamp of the most recent memo `sender` left for `account`. /// 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 memo_check(&self, account: &str, sender: &str) -> Option<(bool, u64)>;
fn set_entrymsg(&mut self, channel: &str, msg: &str) -> Result<(), ChanError>; fn set_entrymsg(&mut self, channel: &str, msg: &str) -> Result<(), ChanError>;

View file

@ -24,6 +24,8 @@ mod info;
mod sendall; mod sendall;
#[path = "ignore.rs"] #[path = "ignore.rs"]
mod ignore; 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."; 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: "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 <text>\x02\nLeaves a memo on every registered account. Requires the admin privilege." }, HelpEntry { cmd: "SENDALL", summary: "memo every account (admin)", detail: "Syntax: \x02SENDALL <text>\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 <nick> | DEL <nick> | LIST\x02\nMemos from an ignored account are silently dropped." }, HelpEntry { cmd: "IGNORE", summary: "block memos from an account", detail: "Syntax: \x02IGNORE ADD <nick> | DEL <nick> | 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 <n>|NONE\x02\nControls new-memo notifications and your mailbox size cap." },
]; ];
pub struct MemoServ { pub struct MemoServ {
@ -83,6 +86,7 @@ impl Service for MemoServ {
Some("INFO") => info::handle(me, from, account, ctx, db), Some("INFO") => info::handle(me, from, account, ctx, db),
Some("SENDALL") => sendall::handle(me, from, account, args, ctx, db), Some("SENDALL") => sendall::handle(me, from, account, args, ctx, db),
Some("IGNORE") => ignore::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.")), Some(other) => ctx.notice(me, from.uid, format!("I don't know the command \x02{other}\x02. Try \x02HELP\x02.")),
None => {} None => {}
} }

View file

@ -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.")); ctx.notice(me, from.uid, format!("\x02{target}\x02 isn't registered."));
return; 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.")); ctx.notice(me, from.uid, format!("\x02{target}\x02's mailbox is full — they'll need to clear some memos first."));
return; return;
} }

View file

@ -0,0 +1,38 @@
use echo_api::{Sender, ServiceCtx, Store};
// SET NOTIFY {ON|OFF} | SET LIMIT <n>|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::<u32>() {
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 <n>|NONE"),
}
}

View file

@ -65,7 +65,7 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
} }
// Let them know about waiting memos. // Let them know about waiting memos.
let unread = db.unread_memos(&account); 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.")); ctx.notice(me, from.uid, format!("You have \x02{unread}\x02 new memo(s). Read them with \x02/msg MemoServ READ NEW\x02."));
} }
} }

View file

@ -20,7 +20,7 @@ impl Db {
verified, verified,
ajoin: Vec::new(), ajoin: Vec::new(),
suspension: None, 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(), greet: String::new(),
vhost: None, vhost: None,
vhost_request: None, vhost_request: None,
@ -59,7 +59,7 @@ impl Db {
verified: true, // the external authority vouches for it verified: true, // the external authority vouches for it
ajoin: Vec::new(), ajoin: Vec::new(),
suspension: None, 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(), greet: String::new(),
vhost: None, vhost: None,
vhost_request: 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))) 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. /// How many unread memos an account has.
pub fn unread_memos(&self, account: &str) -> usize { 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()) 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 }, MemoDeleted { account: String, index: usize },
MemoIgnoreAdd { account: String, target: String }, MemoIgnoreAdd { account: String, target: String },
MemoIgnoreDel { account: String, target: String }, MemoIgnoreDel { account: String, target: String },
MemoPrefsSet { account: String, notify: bool, limit: Option<u32> },
NickGrouped { nick: String, account: String }, NickGrouped { nick: String, account: String },
NickUngrouped { nick: String }, NickUngrouped { nick: String },
ChannelRegistered { name: String, founder: String, ts: u64 }, ChannelRegistered { name: String, founder: String, ts: u64 },
@ -160,6 +161,7 @@ impl Event {
| Event::MemoDeleted { .. } | Event::MemoDeleted { .. }
| Event::MemoIgnoreAdd { .. } | Event::MemoIgnoreAdd { .. }
| Event::MemoIgnoreDel { .. } | Event::MemoIgnoreDel { .. }
| Event::MemoPrefsSet { .. }
| Event::NickGrouped { .. } | Event::NickGrouped { .. }
| Event::NickUngrouped { .. } | Event::NickUngrouped { .. }
| Event::AccountSeen { .. } | 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)); 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 } => { Event::NickGrouped { nick, account } => {
grouped.insert(key(&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). // Accounts this user won't receive memos from (MemoServ IGNORE).
#[serde(default)] #[serde(default)]
pub memo_ignore: Vec<String>, 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. // Personal greet a bot shows when this account joins a greet-enabled channel.
#[serde(default)] #[serde(default)]
pub greet: String, pub greet: String,

View file

@ -468,6 +468,18 @@ impl Store for Db {
fn memo_is_ignored(&self, account: &str, sender: &str) -> bool { fn memo_is_ignored(&self, account: &str, sender: &str) -> bool {
Db::memo_is_ignored(self, account, sender) 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)> { fn memo_check(&self, account: &str, sender: &str) -> Option<(bool, u64)> {
Db::memo_check(self, account, sender) Db::memo_check(self, account, sender)
} }

View file

@ -32,7 +32,7 @@
// which of the two competing registrations we're looking at. // which of the two competing registrations we're looking at.
let alice = |tag: &str, ts: u64, home: &str| Account { let alice = |tag: &str, ts: u64, home: &str| Account {
name: "alice".into(), email: Some(tag.into()), 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 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()); 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(); db.register("alice", "pw", None).unwrap();
let bob = Account { let bob = Account {
name: "bob".into(), email: None, 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)) }; let entry = LogEntry { origin: "peer".into(), seq: 0, lamport: 1, event: Event::AccountRegistered(Box::new(bob)) };
db.ingest(entry).unwrap(); 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. // Private, self-service, or cosmetic — not surfaced.
AjoinAdded { .. } | AjoinRemoved { .. } | AccountGreetSet { .. } | VhostRequested { .. } AjoinAdded { .. } | AjoinRemoved { .. } | AccountGreetSet { .. } | VhostRequested { .. }
| VhostRequestCleared { .. } | MemoSent { .. } | MemoRead { .. } | MemoDeleted { .. } | VhostRequestCleared { .. } | MemoSent { .. } | MemoRead { .. } | MemoDeleted { .. }
| MemoIgnoreAdd { .. } | MemoIgnoreDel { .. } | MemoIgnoreAdd { .. } | MemoIgnoreDel { .. } | MemoPrefsSet { .. }
| ChannelMlock { .. } | ChannelDescSet { .. } | ChannelEntryMsgSet { .. } | ChannelSettingsSet { .. } | ChannelMlock { .. } | ChannelDescSet { .. } | ChannelEntryMsgSet { .. } | ChannelSettingsSet { .. }
| ChannelKickerSet { .. } | ChannelBadwordsSet { .. } | ChannelTriggersSet { .. } | ChannelKickerSet { .. } | ChannelBadwordsSet { .. } | ChannelTriggersSet { .. }
| ChannelTopicSet { .. } | AccountSeen { .. } | ChannelUsed { .. } | ChannelTopicSet { .. } | AccountSeen { .. } | ChannelUsed { .. }

View file

@ -411,7 +411,7 @@
// An earlier claim from another node wins and takes the name over. // An earlier claim from another node wins and takes the name over.
let winner = db::Account { let winner = db::Account {
name: "alice".into(), email: None, 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))); let entry = LogEntry::for_test("peer", 0, 1, db::Event::AccountRegistered(Box::new(winner)));
e.gossip_ingest(entry).unwrap(); e.gossip_ingest(entry).unwrap();
@ -875,6 +875,44 @@
assert_eq!(e.db.unread_memos("alice"), 1, "delivered once no longer ignored"); 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. // ChanServ AKICK CLEAR empties the auto-kick list.
#[test] #[test]
fn chanserv_akick_clear() { fn chanserv_akick_clear() {

View file

@ -141,6 +141,7 @@ fn to_wire(entry: &LogEntry) -> Option<ReplicationEvent> {
| Event::MemoDeleted { .. } | Event::MemoDeleted { .. }
| Event::MemoIgnoreAdd { .. } | Event::MemoIgnoreAdd { .. }
| Event::MemoIgnoreDel { .. } | Event::MemoIgnoreDel { .. }
| Event::MemoPrefsSet { .. }
| Event::ChannelMlock { .. } | Event::ChannelMlock { .. }
| Event::ChannelAccessAdd { .. } | Event::ChannelAccessAdd { .. }
| Event::ChannelAccessDel { .. } | Event::ChannelAccessDel { .. }
@ -457,6 +458,8 @@ mod tests {
suspension: None, suspension: None,
memos: vec![], memos: vec![],
memo_ignore: vec![], memo_ignore: vec![],
memo_notify: true,
memo_limit: None,
greet: String::new(), greet: String::new(),
vhost: None, vhost: None,
vhost_request: None, vhost_request: None,