Answer CTCP VERSION/PING/TIME/CLIENTINFO on service pseudo-clients and split over-long NOTICE/PRIVMSG lines under 512 bytes
All checks were successful
CI / check (push) Successful in 5m32s

This commit is contained in:
Jean Chevronnet 2026-07-20 14:29:02 +00:00
parent 3723617367
commit 58082248ec
No known key found for this signature in database
4 changed files with 138 additions and 5 deletions

View file

@ -1,6 +1,29 @@
use super::*;
impl Engine {
// Answer a CTCP query addressed to a service pseudo-client. Replies to the
// standard introspection queries via a CTCP NOTICE (the CTCP convention);
// ACTION and anything unrecognised get no reply at all — a services daemon must
// not treat `\x01VERSION\x01` as a bad command.
fn ctcp_reply(&self, svc_uid: &str, to: &str, text: &str) -> Option<NetAction> {
let body = text.trim_matches('\x01');
let mut parts = body.splitn(2, ' ');
let cmd = parts.next().unwrap_or("").to_ascii_uppercase();
let arg = parts.next().unwrap_or("");
let response = match cmd.as_str() {
"VERSION" => format!("VERSION echo services {}", env!("CARGO_PKG_VERSION")),
"PING" => format!("PING {arg}"),
"TIME" => format!("TIME {}", echo_api::human_time(self.now_secs())),
"CLIENTINFO" => "CLIENTINFO ACTION CLIENTINFO PING TIME VERSION".to_string(),
_ => return None, // ACTION and unknown CTCPs: stay silent
};
Some(NetAction::Notice {
from: svc_uid.to_string(),
to: to.to_string(),
text: format!("\x01{response}\x01"),
})
}
// Route a PRIVMSG addressed to a service (by uid or nick) into that service,
// handing it the sender's resolved nick, login state, and the account store.
pub(crate) fn dispatch(&mut self, from: &str, to: &str, text: &str) -> Vec<NetAction> {
@ -71,6 +94,21 @@ impl Engine {
}
}
}
// A CTCP query to a service (a PRIVMSG wrapped in \x01) gets a proper
// CTCP NOTICE reply for VERSION/PING/TIME/CLIENTINFO, and is otherwise
// ignored — never bounced back as an "I don't know the command" notice.
if text.starts_with('\x01') {
let svc_uid = self.services.iter()
.find(|s| to.eq_ignore_ascii_case(s.uid()) || to.eq_ignore_ascii_case(s.nick()))
.map(|s| s.uid().to_string());
let mut out = Vec::new();
if let Some(uid) = svc_uid {
if let Some(reply) = self.ctcp_reply(&uid, from, text) {
out.push(reply);
}
}
return out;
}
let mut matched: Option<String> = None;
{
let Self { services, network, db, .. } = self;

View file

@ -4017,6 +4017,34 @@
assert!(os(&mut e, "000AAAAAS", "AKILL LIST").iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains("No matching"))), "list now empty");
}
#[test]
fn ctcp_query_to_a_service_replies_or_stays_silent() {
use echo_nickserv::NickServ;
let path = std::env::temp_dir().join("echo-ctcp.jsonl");
let _ = std::fs::remove_file(&path);
let db = Db::open(&path, "42S");
let mut e = Engine::new(
vec![Box::new(NickServ { uid: "42SAAAAAA".into(), guest_nick: "Guest".into(), guest_seq: 0 })],
db,
);
e.set_sid("42S".into());
e.handle(NetEvent::UserConnect { uid: "000AAAAAB".into(), nick: "alice".into(), host: "h".into(), ip: "0.0.0.0".into() });
let ns = |e: &mut Engine, t: &str| e.handle(NetEvent::Privmsg { from: "000AAAAAB".into(), to: "42SAAAAAA".into(), text: t.into() });
// VERSION → a CTCP NOTICE from the service, never an "unknown command".
let ver = ns(&mut e, "\x01VERSION\x01");
assert!(ver.iter().any(|a| matches!(a, NetAction::Notice { from, text, .. } if from == "42SAAAAAA" && text.starts_with("\x01VERSION echo") && text.ends_with('\x01'))), "VERSION reply: {ver:?}");
assert!(!ver.iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains("don't know"))), "no unknown-command bounce");
// PING echoes the token back verbatim.
assert!(ns(&mut e, "\x01PING tok123\x01").iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text == "\x01PING tok123\x01")), "PING echo");
// ACTION and unrecognised CTCPs get no reply at all.
assert!(ns(&mut e, "\x01ACTION waves\x01").iter().all(|a| !matches!(a, NetAction::Notice { .. })), "ACTION silent");
assert!(ns(&mut e, "\x01FLOOBLE\x01").iter().all(|a| !matches!(a, NetAction::Notice { .. })), "unknown CTCP silent");
// A normal (non-CTCP) unknown command is still answered — the intercept
// didn't swallow ordinary routing.
assert!(ns(&mut e, "FLOOBLE").iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains("don't know"))), "normal unknown command still answered");
}
// An account approaching expiry with an email on file is warned once by
// email, isn't dropped while still in the window, and isn't warned twice.
#[test]