diff --git a/api/src/lib.rs b/api/src/lib.rs index e80b073..069cd2a 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -1053,6 +1053,10 @@ pub trait Store { fn unread_memos(&self, account: &str) -> usize; /// Recall the most recent unread memo `sender` left for `account` (true if one was). fn memo_cancel(&mut self, account: &str, sender: &str) -> bool; + fn memo_ignore_add(&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; + fn memo_is_ignored(&self, account: &str, sender: &str) -> bool; /// 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/ignore.rs b/modules/memoserv/src/ignore.rs new file mode 100644 index 0000000..8daaa79 --- /dev/null +++ b/modules/memoserv/src/ignore.rs @@ -0,0 +1,41 @@ +use echo_api::{Sender, ServiceCtx, Store}; + +// IGNORE ADD | DEL | LIST: manage your memo-ignore list. Memos +// from an ignored account are silently dropped. +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("ADD") => { + let Some(&nick) = args.get(2) else { + ctx.notice(me, from.uid, "Syntax: IGNORE ADD "); + return; + }; + let target = db.resolve_account(nick).map(str::to_string).unwrap_or_else(|| nick.to_string()); + if db.memo_ignore_add(account, &target) { + ctx.notice(me, from.uid, format!("Now ignoring memos from \x02{target}\x02.")); + } else { + ctx.notice(me, from.uid, format!("You're already ignoring \x02{target}\x02.")); + } + } + Some("DEL") | Some("REMOVE") => { + let Some(&nick) = args.get(2) else { + ctx.notice(me, from.uid, "Syntax: IGNORE DEL "); + return; + }; + let target = db.resolve_account(nick).map(str::to_string).unwrap_or_else(|| nick.to_string()); + if db.memo_ignore_del(account, &target) { + ctx.notice(me, from.uid, format!("No longer ignoring \x02{target}\x02.")); + } else { + ctx.notice(me, from.uid, format!("You weren't ignoring \x02{target}\x02.")); + } + } + Some("LIST") | None => { + let list = db.memo_ignores(account); + if list.is_empty() { + ctx.notice(me, from.uid, "Your memo-ignore list is empty."); + } else { + ctx.notice(me, from.uid, format!("You're ignoring memos from: {}", list.join(", "))); + } + } + _ => ctx.notice(me, from.uid, "Syntax: IGNORE ADD | DEL | LIST"), + } +} diff --git a/modules/memoserv/src/lib.rs b/modules/memoserv/src/lib.rs index ec1ca6e..20219a8 100644 --- a/modules/memoserv/src/lib.rs +++ b/modules/memoserv/src/lib.rs @@ -22,6 +22,8 @@ mod check; mod info; #[path = "sendall.rs"] mod sendall; +#[path = "ignore.rs"] +mod ignore; const BLURB: &str = "MemoServ delivers messages to registered users, online or not. You must be identified to use it."; @@ -37,6 +39,7 @@ const TOPICS: &[HelpEntry] = &[ HelpEntry { cmd: "CHECK", summary: "see if a memo was read", detail: "Syntax: \x02CHECK \x02\nReports whether the last memo you sent them has been read." }, 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." }, ]; pub struct MemoServ { @@ -79,6 +82,7 @@ impl Service for MemoServ { Some("CHECK") => check::handle(me, from, account, args, ctx, db), 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(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 d7e0c01..1217b44 100644 --- a/modules/memoserv/src/send.rs +++ b/modules/memoserv/src/send.rs @@ -18,6 +18,12 @@ pub fn handle(me: &str, from: &Sender, account: &str, args: &[&str], ctx: &mut S ctx.notice(me, from.uid, format!("\x02{target}\x02's mailbox is full — they'll need to clear some memos first.")); return; } + // If the recipient is ignoring the sender, drop it silently — the sender is + // told it was sent, so the ignore isn't revealed. + if db.memo_is_ignored(&dest, account) { + ctx.notice(me, from.uid, format!("Memo sent to \x02{target}\x02.")); + return; + } match db.memo_send(&dest, account, &text) { Ok(()) => ctx.notice(me, from.uid, format!("Memo sent to \x02{target}\x02.")), Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."), diff --git a/src/engine/db/account.rs b/src/engine/db/account.rs index f0e6efb..083cb21 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(), + memos: Vec::new(), memo_ignore: Vec::new(), 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(), + memos: Vec::new(), memo_ignore: Vec::new(), greet: String::new(), vhost: None, vhost_request: None, diff --git a/src/engine/db/channel.rs b/src/engine/db/channel.rs index fa64180..601b900 100644 --- a/src/engine/db/channel.rs +++ b/src/engine/db/channel.rs @@ -654,6 +654,40 @@ impl Db { }) } + /// Add `target` to `account`'s memo-ignore list. Returns whether it was new. + pub fn memo_ignore_add(&mut self, account: &str, target: &str) -> bool { + let k = key(account); + let Some(a) = self.accounts.get(&k) else { return false }; + if a.memo_ignore.iter().any(|t| t.eq_ignore_ascii_case(target)) { + return false; + } + let _ = self.log.append(Event::MemoIgnoreAdd { account: account.to_string(), target: target.to_string() }); + self.accounts.get_mut(&k).unwrap().memo_ignore.push(target.to_string()); + true + } + + /// Remove `target` from `account`'s memo-ignore list. Returns whether it existed. + pub fn memo_ignore_del(&mut self, account: &str, target: &str) -> bool { + let k = key(account); + let Some(a) = self.accounts.get(&k) else { return false }; + if !a.memo_ignore.iter().any(|t| t.eq_ignore_ascii_case(target)) { + return false; + } + let _ = self.log.append(Event::MemoIgnoreDel { account: account.to_string(), target: target.to_string() }); + self.accounts.get_mut(&k).unwrap().memo_ignore.retain(|t| !t.eq_ignore_ascii_case(target)); + true + } + + /// `account`'s memo-ignore list. + pub fn memo_ignores(&self, account: &str) -> Vec { + self.accounts.get(&key(account)).map_or(Vec::new(), |a| a.memo_ignore.clone()) + } + + /// Whether `account` is ignoring memos from `sender`. + pub fn memo_is_ignored(&self, account: &str, sender: &str) -> bool { + self.accounts.get(&key(account)).is_some_and(|a| a.memo_ignore.iter().any(|t| t.eq_ignore_ascii_case(sender))) + } + /// 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 da1b389..f549c00 100644 --- a/src/engine/db/event.rs +++ b/src/engine/db/event.rs @@ -29,6 +29,8 @@ pub enum Event { MemoSent { account: String, from: String, text: String, ts: u64 }, MemoRead { account: String, index: usize }, MemoDeleted { account: String, index: usize }, + MemoIgnoreAdd { account: String, target: String }, + MemoIgnoreDel { account: String, target: String }, NickGrouped { nick: String, account: String }, NickUngrouped { nick: String }, ChannelRegistered { name: String, founder: String, ts: u64 }, @@ -156,6 +158,8 @@ impl Event { | Event::MemoSent { .. } | Event::MemoRead { .. } | Event::MemoDeleted { .. } + | Event::MemoIgnoreAdd { .. } + | Event::MemoIgnoreDel { .. } | Event::NickGrouped { .. } | Event::NickUngrouped { .. } | Event::AccountSeen { .. } @@ -338,6 +342,18 @@ pub(crate) fn apply(accounts: &mut HashMap, channels: &mut Hash } } } + Event::MemoIgnoreAdd { account, target } => { + if let Some(a) = accounts.get_mut(&key(&account)) { + if !a.memo_ignore.iter().any(|t| t.eq_ignore_ascii_case(&target)) { + a.memo_ignore.push(target); + } + } + } + Event::MemoIgnoreDel { account, target } => { + if let Some(a) = accounts.get_mut(&key(&account)) { + a.memo_ignore.retain(|t| !t.eq_ignore_ascii_case(&target)); + } + } Event::NickGrouped { nick, account } => { grouped.insert(key(&nick), account); } diff --git a/src/engine/db/mod.rs b/src/engine/db/mod.rs index 1e01f58..6e50f75 100644 --- a/src/engine/db/mod.rs +++ b/src/engine/db/mod.rs @@ -79,6 +79,9 @@ pub struct Account { // Memos left for this account (MemoServ), oldest first. #[serde(default)] pub memos: Vec, + // Accounts this user won't receive memos from (MemoServ IGNORE). + #[serde(default)] + pub memo_ignore: Vec, // 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 d98a39f..52c1d35 100644 --- a/src/engine/db/store.rs +++ b/src/engine/db/store.rs @@ -456,6 +456,18 @@ impl Store for Db { fn memo_cancel(&mut self, account: &str, sender: &str) -> bool { Db::memo_cancel(self, account, sender) } + fn memo_ignore_add(&mut self, account: &str, target: &str) -> bool { + Db::memo_ignore_add(self, account, target) + } + fn memo_ignore_del(&mut self, account: &str, target: &str) -> bool { + Db::memo_ignore_del(self, account, target) + } + fn memo_ignores(&self, account: &str) -> Vec { + Db::memo_ignores(self, account) + } + fn memo_is_ignored(&self, account: &str, sender: &str) -> bool { + Db::memo_is_ignored(self, account, sender) + } 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 4572765..4b25f12 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![], 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![], 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![], 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![], 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 3784d8a..f893144 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -1171,6 +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 { .. } | ChannelMlock { .. } | ChannelDescSet { .. } | ChannelEntryMsgSet { .. } | ChannelSettingsSet { .. } | ChannelKickerSet { .. } | ChannelBadwordsSet { .. } | ChannelTriggersSet { .. } | ChannelTopicSet { .. } | AccountSeen { .. } | ChannelUsed { .. } diff --git a/src/engine/tests.rs b/src/engine/tests.rs index c2c81a2..e5059b2 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![], 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![], 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(); @@ -837,6 +837,44 @@ assert_eq!(e.db.unread_memos("bob"), 1); } + // MemoServ IGNORE: a memo from an ignored sender is silently dropped, and the + // sender is still told it was sent (so the ignore isn't revealed). + #[test] + fn memoserv_ignore_drops_memo() { + use echo_memoserv::MemoServ; + use echo_nickserv::NickServ; + let path = std::env::temp_dir().join("echo-msignore.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() }); + + // alice ignores bob. + assert!(notice(&ms(&mut e, "000AAAAAB", "IGNORE ADD bob"), "ignoring"), "ignore added"); + // bob's memo is accepted-looking but dropped. + assert!(notice(&ms(&mut e, "000AAAAAC", "SEND alice hey"), "Memo sent"), "sender told it was sent"); + assert_eq!(e.db.unread_memos("alice"), 0, "the memo was dropped"); + // After un-ignoring, delivery resumes. + ms(&mut e, "000AAAAAB", "IGNORE DEL bob"); + ms(&mut e, "000AAAAAC", "SEND alice hey again"); + assert_eq!(e.db.unread_memos("alice"), 1, "delivered once no longer ignored"); + } + // ChanServ AKICK CLEAR empties the auto-kick list. #[test] fn chanserv_akick_clear() { diff --git a/src/grpc.rs b/src/grpc.rs index f7fed03..34e38d8 100644 --- a/src/grpc.rs +++ b/src/grpc.rs @@ -139,6 +139,8 @@ fn to_wire(entry: &LogEntry) -> Option { | Event::MemoSent { .. } | Event::MemoRead { .. } | Event::MemoDeleted { .. } + | Event::MemoIgnoreAdd { .. } + | Event::MemoIgnoreDel { .. } | Event::ChannelMlock { .. } | Event::ChannelAccessAdd { .. } | Event::ChannelAccessDel { .. } @@ -454,6 +456,7 @@ mod tests { ajoin: vec![], suspension: None, memos: vec![], + memo_ignore: vec![], greet: String::new(), vhost: None, vhost_request: None,