This commit is contained in:
parent
2850620be1
commit
d819b2c75f
11 changed files with 80 additions and 22 deletions
|
|
@ -606,6 +606,8 @@ pub struct MemoView {
|
|||
pub text: String,
|
||||
pub ts: u64,
|
||||
pub read: bool,
|
||||
// The sender asked to be told when this memo is read (RSEND).
|
||||
pub receipt: bool,
|
||||
}
|
||||
|
||||
// A service bot registered with BotServ.
|
||||
|
|
@ -1048,7 +1050,7 @@ pub trait Store {
|
|||
fn assign_bot(&mut self, channel: &str, bot: &str) -> Result<(), ChanError>;
|
||||
fn unassign_bot(&mut self, channel: &str) -> Result<bool, ChanError>;
|
||||
// MemoServ: per-account memos (index is a 0-based position in memo_list).
|
||||
fn memo_send(&mut self, account: &str, from: &str, text: &str) -> Result<(), RegError>;
|
||||
fn memo_send(&mut self, account: &str, from: &str, text: &str, receipt: bool) -> Result<(), RegError>;
|
||||
fn memo_list(&self, account: &str) -> Vec<MemoView>;
|
||||
fn memo_read(&mut self, account: &str, index: usize) -> Option<MemoView>;
|
||||
fn memo_del(&mut self, account: &str, index: usize) -> bool;
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ pub(crate) const MAX_MEMOS: usize = 30;
|
|||
|
||||
const TOPICS: &[HelpEntry] = &[
|
||||
HelpEntry { cmd: "SEND", summary: "leave a memo for an account", detail: "Syntax: \x02SEND <nick> <text>\x02\nLeaves a memo on a registered account's mailbox." },
|
||||
HelpEntry { cmd: "RSEND", summary: "leave a memo and get a read receipt", detail: "Syntax: \x02RSEND <nick> <text>\x02\nLike SEND, but memos you back when the recipient reads it." },
|
||||
HelpEntry { cmd: "LIST", summary: "list your memos", detail: "Syntax: \x02LIST\x02\nLists the memos in your mailbox." },
|
||||
HelpEntry { cmd: "READ", summary: "read a memo", detail: "Syntax: \x02READ <num>|NEW|ALL\x02\nReads a memo by number, your unread ones, or all of them." },
|
||||
HelpEntry { cmd: "DEL", summary: "delete a memo", detail: "Syntax: \x02DEL <num>|ALL\x02\nDeletes a memo by number, or your whole mailbox." },
|
||||
|
|
@ -77,7 +78,8 @@ impl Service for MemoServ {
|
|||
return;
|
||||
};
|
||||
match cmd.as_deref() {
|
||||
Some("SEND") => send::handle(me, from, account, args, ctx, db),
|
||||
Some("SEND") => send::handle(me, from, account, args, ctx, db, false),
|
||||
Some("RSEND") => send::handle(me, from, account, args, ctx, db, true),
|
||||
Some("LIST") => list::handle(me, from, account, ctx, db),
|
||||
Some("READ") => read::handle(me, from, account, args, ctx, db),
|
||||
Some("DEL") | Some("DELETE") => del::handle(me, from, account, args, ctx, db),
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ pub fn handle(me: &str, from: &Sender, account: &str, args: &[&str], ctx: &mut S
|
|||
for i in targets {
|
||||
if let Some(m) = db.memo_read(account, i) {
|
||||
show(me, from, i, &m, ctx);
|
||||
send_receipt(account, &m, db);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -28,7 +29,10 @@ pub fn handle(me: &str, from: &Sender, account: &str, args: &[&str], ctx: &mut S
|
|||
return;
|
||||
};
|
||||
match db.memo_read(account, n - 1) {
|
||||
Some(m) => show(me, from, n - 1, &m, ctx),
|
||||
Some(m) => {
|
||||
show(me, from, n - 1, &m, ctx);
|
||||
send_receipt(account, &m, db);
|
||||
}
|
||||
None => ctx.notice(me, from.uid, format!("You have no memo #\x02{n}\x02.")),
|
||||
}
|
||||
}
|
||||
|
|
@ -40,3 +44,12 @@ fn show(me: &str, from: &Sender, index: usize, m: &MemoView, ctx: &mut ServiceCt
|
|||
ctx.notice(me, from.uid, format!("Memo #\x02{}\x02 from \x02{}\x02 ({}):", index + 1, m.from, human_time(m.ts)));
|
||||
ctx.notice(me, from.uid, format!(" {}", m.text));
|
||||
}
|
||||
|
||||
// If this was an unread RSEND memo, memo the sender that it's now been read.
|
||||
// `m.read` is the pre-read state, so we only fire once, on first read.
|
||||
fn send_receipt(reader: &str, m: &MemoView, db: &mut dyn Store) {
|
||||
if m.receipt && !m.read {
|
||||
let text = format!("{reader} has read the memo you sent them of {}.", human_time(m.ts));
|
||||
let _ = db.memo_send(&m.from, "MemoServ", &text, false);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,12 @@ use echo_api::{Sender, ServiceCtx, Store};
|
|||
|
||||
use super::MAX_MEMOS;
|
||||
|
||||
// SEND <nick> <text>: leave a memo on a registered account's mailbox.
|
||||
pub fn handle(me: &str, from: &Sender, account: &str, args: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) {
|
||||
// SEND/RSEND <nick> <text>: leave a memo on a registered account's mailbox.
|
||||
// With `receipt`, the sender is told when the recipient reads it (RSEND).
|
||||
pub fn handle(me: &str, from: &Sender, account: &str, args: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store, receipt: bool) {
|
||||
let cmd = if receipt { "RSEND" } else { "SEND" };
|
||||
if args.len() < 3 {
|
||||
ctx.notice(me, from.uid, "Syntax: SEND <nick> <text>");
|
||||
ctx.notice(me, from.uid, format!("Syntax: {cmd} <nick> <text>"));
|
||||
return;
|
||||
}
|
||||
let target = args[1];
|
||||
|
|
@ -25,7 +27,7 @@ pub fn handle(me: &str, from: &Sender, account: &str, args: &[&str], ctx: &mut S
|
|||
ctx.notice(me, from.uid, format!("Memo sent to \x02{target}\x02."));
|
||||
return;
|
||||
}
|
||||
match db.memo_send(&dest, account, &text) {
|
||||
match db.memo_send(&dest, account, &text, receipt) {
|
||||
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."),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ pub fn handle(me: &str, from: &Sender, account: &str, args: &[&str], ctx: &mut S
|
|||
let names: Vec<String> = db.accounts_matching("*").into_iter().map(|a| a.name).collect();
|
||||
let mut sent = 0usize;
|
||||
for name in &names {
|
||||
if db.memo_send(name, account, &text).is_ok() {
|
||||
if db.memo_send(name, account, &text, false).is_ok() {
|
||||
sent += 1;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -587,28 +587,28 @@ impl Db {
|
|||
}
|
||||
|
||||
/// Append a memo to an account's mailbox.
|
||||
pub fn memo_send(&mut self, account: &str, from: &str, text: &str) -> Result<(), RegError> {
|
||||
pub fn memo_send(&mut self, account: &str, from: &str, text: &str, receipt: bool) -> Result<(), RegError> {
|
||||
let k = key(account);
|
||||
if !self.accounts.contains_key(&k) {
|
||||
return Err(RegError::Internal);
|
||||
}
|
||||
let ts = now();
|
||||
self.log.append(Event::MemoSent { account: account.to_string(), from: from.to_string(), text: text.to_string(), ts }).map_err(|_| RegError::Internal)?;
|
||||
self.accounts.get_mut(&k).unwrap().memos.push(Memo { from: from.to_string(), text: text.to_string(), ts, read: false });
|
||||
self.log.append(Event::MemoSent { account: account.to_string(), from: from.to_string(), text: text.to_string(), ts, receipt }).map_err(|_| RegError::Internal)?;
|
||||
self.accounts.get_mut(&k).unwrap().memos.push(Memo { from: from.to_string(), text: text.to_string(), ts, read: false, receipt });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// An account's memos, oldest first.
|
||||
pub fn memo_list(&self, account: &str) -> Vec<MemoView> {
|
||||
self.accounts.get(&key(account)).map_or(Vec::new(), |a| {
|
||||
a.memos.iter().map(|m| MemoView { from: m.from.clone(), text: m.text.clone(), ts: m.ts, read: m.read }).collect()
|
||||
a.memos.iter().map(|m| MemoView { from: m.from.clone(), text: m.text.clone(), ts: m.ts, read: m.read, receipt: m.receipt }).collect()
|
||||
})
|
||||
}
|
||||
|
||||
/// Read one memo by index (marks it read), returning its contents.
|
||||
pub fn memo_read(&mut self, account: &str, index: usize) -> Option<MemoView> {
|
||||
let k = key(account);
|
||||
let view = self.accounts.get(&k).and_then(|a| a.memos.get(index)).map(|m| MemoView { from: m.from.clone(), text: m.text.clone(), ts: m.ts, read: m.read })?;
|
||||
let view = self.accounts.get(&k).and_then(|a| a.memos.get(index)).map(|m| MemoView { from: m.from.clone(), text: m.text.clone(), ts: m.ts, read: m.read, receipt: m.receipt })?;
|
||||
if !view.read {
|
||||
let _ = self.log.append(Event::MemoRead { account: account.to_string(), index });
|
||||
if let Some(m) = self.accounts.get_mut(&k).and_then(|a| a.memos.get_mut(index)) {
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ pub enum Event {
|
|||
VhostRequestCleared { account: String },
|
||||
AccountSuspended { account: String, by: String, reason: String, ts: u64, expires: Option<u64> },
|
||||
AccountUnsuspended { account: String },
|
||||
MemoSent { account: String, from: String, text: String, ts: u64 },
|
||||
MemoSent { account: String, from: String, text: String, ts: u64, #[serde(default)] receipt: bool },
|
||||
MemoRead { account: String, index: usize },
|
||||
MemoDeleted { account: String, index: usize },
|
||||
MemoIgnoreAdd { account: String, target: String },
|
||||
|
|
@ -327,9 +327,9 @@ pub(crate) fn apply(accounts: &mut HashMap<String, Account>, channels: &mut Hash
|
|||
a.suspension = None;
|
||||
}
|
||||
}
|
||||
Event::MemoSent { account, from, text, ts } => {
|
||||
Event::MemoSent { account, from, text, ts, receipt } => {
|
||||
if let Some(a) = accounts.get_mut(&key(&account)) {
|
||||
a.memos.push(Memo { from, text, ts, read: false });
|
||||
a.memos.push(Memo { from, text, ts, read: false, receipt });
|
||||
}
|
||||
}
|
||||
Event::MemoRead { account, index } => {
|
||||
|
|
|
|||
|
|
@ -313,6 +313,9 @@ pub struct Memo {
|
|||
pub ts: u64,
|
||||
#[serde(default)]
|
||||
pub read: bool,
|
||||
// The sender asked to be told when it's read (MemoServ RSEND).
|
||||
#[serde(default)]
|
||||
pub receipt: bool,
|
||||
}
|
||||
|
||||
// A service bot: a pseudo-client BotServ can assign to sit in channels.
|
||||
|
|
|
|||
|
|
@ -441,8 +441,8 @@ impl Store for Db {
|
|||
fn unassign_bot(&mut self, channel: &str) -> Result<bool, ChanError> {
|
||||
Db::unassign_bot(self, channel)
|
||||
}
|
||||
fn memo_send(&mut self, account: &str, from: &str, text: &str) -> Result<(), RegError> {
|
||||
Db::memo_send(self, account, from, text)
|
||||
fn memo_send(&mut self, account: &str, from: &str, text: &str, receipt: bool) -> Result<(), RegError> {
|
||||
Db::memo_send(self, account, from, text, receipt)
|
||||
}
|
||||
fn memo_list(&self, account: &str) -> Vec<MemoView> {
|
||||
Db::memo_list(self, account)
|
||||
|
|
|
|||
|
|
@ -219,8 +219,8 @@
|
|||
db.scram_iterations = 4096;
|
||||
db.register("alice", "password1", None).unwrap();
|
||||
assert_eq!(db.unread_memos("alice"), 0);
|
||||
db.memo_send("alice", "bob", "hello there").unwrap();
|
||||
db.memo_send("alice", "carol", "second").unwrap();
|
||||
db.memo_send("alice", "bob", "hello there", false).unwrap();
|
||||
db.memo_send("alice", "carol", "second", false).unwrap();
|
||||
assert_eq!(db.unread_memos("alice"), 2);
|
||||
let m = db.memo_read("alice", 0).unwrap();
|
||||
assert_eq!(m.from, "bob");
|
||||
|
|
@ -240,8 +240,8 @@
|
|||
let mut db = Db::open(tmp("memo-cc"), "N1");
|
||||
db.scram_iterations = 4096;
|
||||
db.register("alice", "pw", None).unwrap();
|
||||
db.memo_send("alice", "bob", "hi").unwrap();
|
||||
db.memo_send("alice", "bob", "you there?").unwrap();
|
||||
db.memo_send("alice", "bob", "hi", false).unwrap();
|
||||
db.memo_send("alice", "bob", "you there?", false).unwrap();
|
||||
|
||||
assert_eq!(db.memo_check("alice", "bob").map(|(read, _)| read), Some(false), "unread so far");
|
||||
assert!(db.memo_cancel("alice", "bob"), "recalls the most recent unread");
|
||||
|
|
|
|||
|
|
@ -913,6 +913,42 @@
|
|||
assert_eq!(e.db.unread_memos("alice"), 2, "capped at the limit");
|
||||
}
|
||||
|
||||
// MemoServ RSEND: when the recipient reads the memo, the sender is memoed back.
|
||||
#[test]
|
||||
fn memoserv_rsend_read_receipt() {
|
||||
use echo_memoserv::MemoServ;
|
||||
use echo_nickserv::NickServ;
|
||||
let path = std::env::temp_dir().join("echo-msrsend.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, "000AAAAAC", "RSEND alice testing"), "Memo sent"), "rsend delivered");
|
||||
assert_eq!(e.db.unread_memos("bob"), 0, "bob has no receipt yet");
|
||||
// alice reads it; bob gets a receipt memo.
|
||||
assert!(notice(&ms(&mut e, "000AAAAAB", "READ 1"), "testing"), "alice reads the memo");
|
||||
assert_eq!(e.db.unread_memos("bob"), 1, "bob got a read receipt");
|
||||
assert!(notice(&ms(&mut e, "000AAAAAC", "READ 1"), "has read the memo"), "receipt text delivered");
|
||||
// Reading again does not fire a second receipt.
|
||||
ms(&mut e, "000AAAAAB", "READ 1");
|
||||
assert_eq!(e.db.unread_memos("bob"), 0, "no duplicate receipt on re-read");
|
||||
}
|
||||
|
||||
// ChanServ AKICK CLEAR empties the auto-kick list.
|
||||
#[test]
|
||||
fn chanserv_akick_clear() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue