move chathistory + redact + the history store into their own module file
This commit is contained in:
parent
ab7188bb70
commit
431ef2f5b3
6 changed files with 386 additions and 358 deletions
|
|
@ -2,8 +2,9 @@
|
|||
|
||||
use crate::channels::{glob_match, RANK_HALFOP, RANK_VOICE};
|
||||
use crate::command::{CmdResult, Command};
|
||||
use crate::modules::chathistory::{dm_key, record};
|
||||
use crate::numeric::*;
|
||||
use crate::server::{iso_time, parse_iso, HistMsg, Server, HISTORY_CAP};
|
||||
use crate::server::Server;
|
||||
use crate::Uid;
|
||||
|
||||
/// mIRC/IRC formatting control bytes (bold, colour, hex-colour, reset, …).
|
||||
|
|
@ -123,314 +124,9 @@ pub fn commands() -> Vec<Box<dyn Command>> {
|
|||
Box::new(PrivMsg),
|
||||
Box::new(Notice),
|
||||
Box::new(TagMsg),
|
||||
Box::new(ChatHistory),
|
||||
Box::new(Redact),
|
||||
]
|
||||
}
|
||||
|
||||
/// REDACT — delete a previously-sent channel message (draft/message-redaction).
|
||||
/// `REDACT <#chan> <msgid> [:reason]`. Allowed for the message's author, a channel
|
||||
/// half-op-or-above, or an oper. Relayed to channel members who enabled the cap,
|
||||
/// and the message is dropped from CHATHISTORY.
|
||||
struct Redact;
|
||||
impl Command for Redact {
|
||||
fn name(&self) -> &'static str {
|
||||
"REDACT"
|
||||
}
|
||||
fn min_params(&self) -> usize {
|
||||
2
|
||||
}
|
||||
fn handle(&self, s: &mut Server, uid: Uid, params: &[String]) -> CmdResult {
|
||||
let target = ¶ms[0];
|
||||
let msgid = ¶ms[1];
|
||||
let reason = params.get(2).cloned().unwrap_or_default();
|
||||
if !target.starts_with('#') {
|
||||
s.fail(
|
||||
uid,
|
||||
"REDACT",
|
||||
"INVALID_TARGET",
|
||||
"REDACT only supports channels",
|
||||
);
|
||||
return CmdResult::Fail;
|
||||
}
|
||||
let key = target.to_ascii_lowercase();
|
||||
let Some(author) = s.history.get(&key).and_then(|buf| {
|
||||
buf.iter().find(|m| m.msgid == *msgid).map(|m| {
|
||||
m.prefix
|
||||
.split('!')
|
||||
.next()
|
||||
.unwrap_or("")
|
||||
.to_ascii_lowercase()
|
||||
})
|
||||
}) else {
|
||||
s.fail(
|
||||
uid,
|
||||
"REDACT",
|
||||
"UNKNOWN_MSGID",
|
||||
&format!("No such message id {msgid}"),
|
||||
);
|
||||
return CmdResult::Fail;
|
||||
};
|
||||
let my_nick = s
|
||||
.users
|
||||
.get(&uid)
|
||||
.map(|u| u.nick.to_ascii_lowercase())
|
||||
.unwrap_or_default();
|
||||
if my_nick != author && s.rank(uid, &key) < RANK_HALFOP && !s.is_oper(uid) {
|
||||
s.fail(
|
||||
uid,
|
||||
"REDACT",
|
||||
"REDACT_FORBIDDEN",
|
||||
"You may only redact your own messages",
|
||||
);
|
||||
return CmdResult::Fail;
|
||||
}
|
||||
if let Some(buf) = s.history.get_mut(&key) {
|
||||
buf.retain(|m| m.msgid != *msgid);
|
||||
}
|
||||
let prefix = s.users.get(&uid).map(|u| u.prefix()).unwrap_or_default();
|
||||
let line = if reason.is_empty() {
|
||||
format!(":{prefix} REDACT {target} {msgid}")
|
||||
} else {
|
||||
format!(":{prefix} REDACT {target} {msgid} :{reason}")
|
||||
};
|
||||
let members: Vec<Uid> = s
|
||||
.channels
|
||||
.get(&key)
|
||||
.map(|c| c.members.keys().copied().collect())
|
||||
.unwrap_or_default();
|
||||
for m in members {
|
||||
if s.users
|
||||
.get(&m)
|
||||
.map(|u| u.caps.message_redaction)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
s.send(m, line.clone());
|
||||
}
|
||||
}
|
||||
CmdResult::Ok
|
||||
}
|
||||
}
|
||||
|
||||
/// Canonical CHATHISTORY key for a DM between two nicks (order-independent; the
|
||||
/// `\0` prefix keeps it from ever colliding with a `#channel` key).
|
||||
fn dm_key(a: &str, b: &str) -> String {
|
||||
let (a, b) = (a.to_ascii_lowercase(), b.to_ascii_lowercase());
|
||||
if a <= b {
|
||||
format!("\0{a}\0{b}")
|
||||
} else {
|
||||
format!("\0{b}\0{a}")
|
||||
}
|
||||
}
|
||||
|
||||
/// CHATHISTORY — replay recent messages (draft/chathistory), leveraging BATCH.
|
||||
/// `CHATHISTORY <LATEST|BEFORE|AFTER|AROUND|BETWEEN> <#chan|nick> <selector..>
|
||||
/// <limit>`; a `<selector>` is `*`, `timestamp=<iso>` or `msgid=<id>`. Channel
|
||||
/// history is members-only; a nick target replays that DM conversation. The reply
|
||||
/// is a `chathistory` batch of the original lines with their server-time + msgid.
|
||||
struct ChatHistory;
|
||||
impl Command for ChatHistory {
|
||||
fn name(&self) -> &'static str {
|
||||
"CHATHISTORY"
|
||||
}
|
||||
fn min_params(&self) -> usize {
|
||||
4
|
||||
}
|
||||
fn handle(&self, s: &mut Server, uid: Uid, params: &[String]) -> CmdResult {
|
||||
let sub = params[0].to_ascii_uppercase();
|
||||
// CHATHISTORY TARGETS <t1> <t2> <limit> — list conversations with activity
|
||||
// in the window, newest-in-window timestamp each. No target param.
|
||||
if sub == "TARGETS" {
|
||||
let bound = |i: usize, dflt: u64| {
|
||||
params
|
||||
.get(i)
|
||||
.and_then(|s| s.strip_prefix("timestamp="))
|
||||
.and_then(parse_iso)
|
||||
.unwrap_or(dflt)
|
||||
};
|
||||
let (a, b) = (bound(1, 0), bound(2, u64::MAX));
|
||||
let (lo, hi) = (a.min(b), a.max(b));
|
||||
let limit = params
|
||||
.get(3)
|
||||
.and_then(|l| l.parse::<usize>().ok())
|
||||
.unwrap_or(50)
|
||||
.clamp(1, HISTORY_CAP);
|
||||
let me = s
|
||||
.users
|
||||
.get(&uid)
|
||||
.map(|u| u.nick.to_ascii_lowercase())
|
||||
.unwrap_or_default();
|
||||
let mut targets: Vec<(String, u64)> = Vec::new();
|
||||
for (key, buf) in &s.history {
|
||||
let Some(ts) = buf
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|m| m.ts >= lo && m.ts <= hi)
|
||||
.map(|m| m.ts)
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
if key.starts_with('#') {
|
||||
if s.is_member(uid, key) {
|
||||
let name = buf
|
||||
.back()
|
||||
.map(|m| m.target.clone())
|
||||
.unwrap_or_else(|| key.clone());
|
||||
targets.push((name, ts));
|
||||
}
|
||||
} else if let Some(rest) = key.strip_prefix('\0') {
|
||||
let p: Vec<&str> = rest.split('\0').collect();
|
||||
if p.len() == 2 && (p[0] == me || p[1] == me) {
|
||||
let other = if p[0] == me { p[1] } else { p[0] };
|
||||
targets.push((other.to_string(), ts));
|
||||
}
|
||||
}
|
||||
}
|
||||
targets.sort_by_key(|(_, ts)| *ts);
|
||||
let start = targets.len().saturating_sub(limit);
|
||||
let bref = s.next_msgid().replace('-', "");
|
||||
s.send(
|
||||
uid,
|
||||
format!(":{} BATCH +{bref} draft/chathistory-targets", s.name),
|
||||
);
|
||||
for (t, ts) in &targets[start..] {
|
||||
s.send(
|
||||
uid,
|
||||
format!(
|
||||
"@batch={bref} :{} CHATHISTORY TARGETS {t} {}",
|
||||
s.name,
|
||||
iso_time(*ts)
|
||||
),
|
||||
);
|
||||
}
|
||||
s.send(uid, format!(":{} BATCH -{bref}", s.name));
|
||||
return CmdResult::Ok;
|
||||
}
|
||||
let target = params[1].clone();
|
||||
// channel target → channel key (members only); a nick → the DM pair key
|
||||
// (the requester is inherently part of it, so no membership check)
|
||||
let is_channel = target.starts_with('#');
|
||||
let key = if is_channel {
|
||||
target.to_ascii_lowercase()
|
||||
} else {
|
||||
let me = s
|
||||
.users
|
||||
.get(&uid)
|
||||
.map(|u| u.nick.clone())
|
||||
.unwrap_or_default();
|
||||
dm_key(&me, &target)
|
||||
};
|
||||
// BETWEEN takes two selectors then the limit; the rest take one + the limit
|
||||
let (sel, sel2, limit_s) = if sub == "BETWEEN" {
|
||||
(
|
||||
params[2].as_str(),
|
||||
params.get(3).map(|s| s.as_str()).unwrap_or("*"),
|
||||
params.get(4),
|
||||
)
|
||||
} else {
|
||||
(params[2].as_str(), "", params.get(3))
|
||||
};
|
||||
let limit = limit_s
|
||||
.and_then(|l| l.parse::<usize>().ok())
|
||||
.unwrap_or(50)
|
||||
.clamp(1, HISTORY_CAP);
|
||||
|
||||
let bref = s.next_msgid().replace('-', "");
|
||||
let mut lines: Vec<String> = Vec::new();
|
||||
if !is_channel || s.is_member(uid, &key) {
|
||||
if let Some(buf) = s.history.get(&key) {
|
||||
// Resolve the selector to a reference position. `msgid=` matches an
|
||||
// exact buffer index (so same-second messages aren't lost);
|
||||
// `timestamp=` and `*` fall back to a ts bound.
|
||||
let ref_idx = sel
|
||||
.strip_prefix("msgid=")
|
||||
.and_then(|id| buf.iter().position(|m| m.msgid == id));
|
||||
let ref_ts = sel.strip_prefix("timestamp=").and_then(parse_iso);
|
||||
// resolve any selector to a buffer index (for AROUND / BETWEEN)
|
||||
let idx_of = |sl: &str| -> Option<usize> {
|
||||
if let Some(id) = sl.strip_prefix("msgid=") {
|
||||
buf.iter().position(|m| m.msgid == id)
|
||||
} else if let Some(iso) = sl.strip_prefix("timestamp=") {
|
||||
parse_iso(iso).and_then(|b| buf.iter().position(|m| m.ts >= b))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
let picked: Vec<&HistMsg> = match sub.as_str() {
|
||||
"BEFORE" => {
|
||||
let end = ref_idx.unwrap_or_else(|| {
|
||||
let b = ref_ts.unwrap_or(u64::MAX);
|
||||
buf.iter().position(|m| m.ts >= b).unwrap_or(buf.len())
|
||||
});
|
||||
let start = end.saturating_sub(limit);
|
||||
buf.iter().take(end).skip(start).collect()
|
||||
}
|
||||
"AFTER" => {
|
||||
let begin = match ref_idx {
|
||||
Some(i) => i + 1,
|
||||
None => {
|
||||
let b = ref_ts.unwrap_or(0);
|
||||
buf.iter().position(|m| m.ts > b).unwrap_or(buf.len())
|
||||
}
|
||||
};
|
||||
buf.iter().skip(begin).take(limit).collect()
|
||||
}
|
||||
"AROUND" => {
|
||||
// messages centred on the selector: half before, half after
|
||||
let i = idx_of(sel).unwrap_or(buf.len() / 2);
|
||||
let start = i.saturating_sub(limit / 2);
|
||||
buf.iter().skip(start).take(limit).collect()
|
||||
}
|
||||
"BETWEEN" => {
|
||||
// messages strictly between the two selector points
|
||||
let a = idx_of(sel).unwrap_or(0);
|
||||
let b = idx_of(sel2).unwrap_or(buf.len());
|
||||
let (lo, hi) = (a.min(b), a.max(b));
|
||||
buf.iter()
|
||||
.skip(lo + 1)
|
||||
.take(hi.saturating_sub(lo + 1))
|
||||
.take(limit)
|
||||
.collect()
|
||||
}
|
||||
_ => {
|
||||
// LATEST: newest `limit`, optionally bounded below by the selector
|
||||
let begin = match (ref_idx, ref_ts) {
|
||||
(Some(i), _) => i + 1,
|
||||
(None, Some(b)) => {
|
||||
buf.iter().position(|m| m.ts > b).unwrap_or(buf.len())
|
||||
}
|
||||
_ => 0,
|
||||
};
|
||||
let n = buf.len() - begin;
|
||||
let start = begin + n.saturating_sub(limit);
|
||||
buf.iter().skip(start).collect()
|
||||
}
|
||||
};
|
||||
for m in picked {
|
||||
lines.push(format!(
|
||||
"@time={};msgid={};batch={bref} :{} {} {} :{}",
|
||||
iso_time(m.ts),
|
||||
m.msgid,
|
||||
m.prefix,
|
||||
m.verb,
|
||||
m.target,
|
||||
m.text
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
s.send(
|
||||
uid,
|
||||
format!(":{} BATCH +{bref} chathistory {target}", s.name),
|
||||
);
|
||||
for l in lines {
|
||||
s.send(uid, l);
|
||||
}
|
||||
s.send(uid, format!(":{} BATCH -{bref}", s.name));
|
||||
CmdResult::Ok
|
||||
}
|
||||
}
|
||||
|
||||
/// Shared PRIVMSG/NOTICE delivery. NOTICE never generates automatic replies.
|
||||
/// `pub(crate)` so the multiline module can replay an assembled batch through it.
|
||||
pub(crate) fn deliver(s: &mut Server, uid: Uid, params: &[String], notice: bool) -> CmdResult {
|
||||
|
|
@ -618,7 +314,7 @@ pub(crate) fn deliver(s: &mut Server, uid: Uid, params: &[String], notice: bool)
|
|||
let line = format!(":{prefix} {cmd} {target} :{body}");
|
||||
let ctags = s.line_ctags.clone();
|
||||
let msgid = s.next_msgid(); // one id shared by every recipient of this message
|
||||
s.store_history(&key, &prefix, cmd, target, &body, &msgid); // for CHATHISTORY
|
||||
record(s, &key, &prefix, cmd, target, &body, &msgid); // for CHATHISTORY
|
||||
let members: Vec<Uid> = s
|
||||
.channels
|
||||
.get(&key)
|
||||
|
|
@ -741,14 +437,7 @@ pub(crate) fn deliver(s: &mut Server, uid: Uid, params: &[String], notice: bool)
|
|||
s.send_tagged(tuid, uid, &ctags, &msgid, &pm);
|
||||
// store for CHATHISTORY under the canonical pair key (both parties share it)
|
||||
let sender_nick = prefix.split('!').next().unwrap_or_default();
|
||||
s.store_history(
|
||||
&dm_key(sender_nick, target),
|
||||
&prefix,
|
||||
cmd,
|
||||
target,
|
||||
text,
|
||||
&msgid,
|
||||
);
|
||||
record(s, &dm_key(sender_nick, target), &prefix, cmd, target, text, &msgid);
|
||||
}
|
||||
if s.users
|
||||
.get(&uid)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue