protocol: learn channel-mode arity + prefix modes (incl custom Y/ojoin) from CAPAB CHANMODES; services MODE uses the authoritative set

This commit is contained in:
Jean Chevronnet 2026-07-18 01:15:23 +00:00
parent d313ca3523
commit e6f05a3ede
No known key found for this signature in database
9 changed files with 163 additions and 26 deletions

View file

@ -25,7 +25,7 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
return;
}
// Reject an extban the network has turned off, before relaying anything.
for mask in ban_masks(&args[2..]) {
for mask in ban_masks(&args[2..], |m, adding| db.chanmode_takes_param(m, adding)) {
let core = mask.strip_prefix('!').unwrap_or(mask); // extbans may be inverted
if let echo_api::AkickMask::Ext(eb, _) = echo_api::AkickMask::parse(core) {
if !db.extban_offered(eb.name) {
@ -44,9 +44,9 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
}
// The parameters attached to +b/-b/+e/-e/+I/-I in a `<modes> [params...]` change.
// Every param-taking mode is accounted for (via the shared arity helper) so ban
// masks pair to the right mode in IRC order.
fn ban_masks<'a>(tokens: &'a [&'a str]) -> Vec<&'a str> {
// `takes_param` (the ircd's advertised arity) accounts for every param-taking mode
// so ban masks pair to the right mode in IRC order.
fn ban_masks<'a>(tokens: &'a [&'a str], takes_param: impl Fn(char, bool) -> bool) -> Vec<&'a str> {
let modes = tokens.first().copied().unwrap_or("");
let mut params = tokens.iter().skip(1);
let mut adding = true;
@ -55,7 +55,7 @@ fn ban_masks<'a>(tokens: &'a [&'a str]) -> Vec<&'a str> {
match ch {
'+' => adding = true,
'-' => adding = false,
m if echo_api::chanmode_takes_param(m, adding) => {
m if takes_param(m, adding) => {
if let Some(&p) = params.next() {
if matches!(m, 'b' | 'e' | 'I') {
out.push(p);
@ -75,14 +75,14 @@ mod tests {
#[test]
fn ban_masks_pairs_params_in_mode_order() {
// A lone ban, and a ban after a prefix mode: the mask must pair to +b.
assert_eq!(ban_masks(&["+b", "account:x"]), ["account:x"]);
assert_eq!(ban_masks(&["+ob", "nick", "account:x"]), ["account:x"]);
assert_eq!(ban_masks(&["+b", "account:x"], echo_api::chanmode_takes_param), ["account:x"]);
assert_eq!(ban_masks(&["+ob", "nick", "account:x"], echo_api::chanmode_takes_param), ["account:x"]);
// The key's value is not a ban mask even when it looks like an extban.
assert!(ban_masks(&["+k", "account:secret"]).is_empty());
assert!(ban_masks(&["+k", "account:secret"], echo_api::chanmode_takes_param).is_empty());
// Exceptions and invex count; plain flag modes carry nothing.
assert_eq!(ban_masks(&["+eI", "R:a", "r:b"]), ["R:a", "r:b"]);
assert!(ban_masks(&["+nt"]).is_empty());
assert_eq!(ban_masks(&["+eI", "R:a", "r:b"], echo_api::chanmode_takes_param), ["R:a", "r:b"]);
assert!(ban_masks(&["+nt"], echo_api::chanmode_takes_param).is_empty());
// Removal still carries a mask.
assert_eq!(ban_masks(&["-b", "*!*@h"]), ["*!*@h"]);
assert_eq!(ban_masks(&["-b", "*!*@h"], echo_api::chanmode_takes_param), ["*!*@h"]);
}
}

View file

@ -82,7 +82,7 @@ impl Service for OperServ {
Some(cmd) if cmd.eq_ignore_ascii_case("GLOBAL") => global::handle(me, from, args, ctx),
Some(cmd) if cmd.eq_ignore_ascii_case("KILL") => kill::handle(me, from, args, ctx, net),
Some(cmd) if cmd.eq_ignore_ascii_case("KICK") => kick::handle(me, from, args, ctx, net),
Some(cmd) if cmd.eq_ignore_ascii_case("MODE") => mode::handle(me, from, args, ctx, net),
Some(cmd) if cmd.eq_ignore_ascii_case("MODE") => mode::handle(me, from, args, ctx, net, db),
Some(cmd) if cmd.eq_ignore_ascii_case("IGNORE") => ignore::handle(me, from, args, ctx, db),
Some(cmd) if cmd.eq_ignore_ascii_case("STATS") => stats::handle(me, from, db, ctx),
Some(cmd) if cmd.eq_ignore_ascii_case("SVSNICK") => svs::nick(me, from, args, ctx, net),

View file

@ -1,9 +1,9 @@
use echo_api::{chanmode_takes_param, NetView, Priv, Sender, ServiceCtx, STATUS_MODES};
use echo_api::{NetView, Priv, Sender, ServiceCtx, Store};
// MODE <#channel> <modes> [params]: set channel modes as a services override
// (forced, so it applies regardless of the current TS). Admin-only. Status-mode
// targets may be given as nicks — they're resolved to uids for the ircd.
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: &dyn NetView) {
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: &dyn NetView, db: &dyn Store) {
if !from.privs.has(Priv::Admin) {
ctx.notice(me, from.uid, "Access denied — MODE needs the \x02admin\x02 privilege.");
return;
@ -19,17 +19,19 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net:
let params = &args[3..];
// Walk the change alongside its params: each param-taking mode consumes one,
// and a status mode's param is a nick we translate to its uid.
// and a status mode's param is a nick we translate to its uid. Both arity and
// the prefix set come from the ircd's advertised CHANMODES (incl custom modes).
let status_modes = db.status_modes();
let (mut adding, mut pi) = (true, 0);
let mut out_params: Vec<String> = Vec::new();
for m in modes.chars() {
match m {
'+' => adding = true,
'-' => adding = false,
_ if chanmode_takes_param(m, adding) => {
_ if db.chanmode_takes_param(m, adding) => {
if let Some(&p) = params.get(pi) {
pi += 1;
if STATUS_MODES.contains(m) {
if status_modes.contains(m) {
out_params.push(net.uid_by_nick(p).map(str::to_string).unwrap_or_else(|| p.to_string()));
} else {
out_params.push(p.to_string());

View file

@ -312,19 +312,26 @@ impl Protocol for InspIrcd {
}
}
// CAPAB EXTBANS :matching:account=R acting:mute=m … — the ircd's live
// extban set. Other CAPAB subcommands (MODULES, CHANMODES, …) are noise.
"CAPAB" => {
if tokens.next().is_some_and(|s| s.eq_ignore_ascii_case("EXTBANS")) {
// extban / channel-mode sets. Other CAPAB subcommands are noise.
"CAPAB" => match tokens.next() {
Some(s) if s.eq_ignore_ascii_case("EXTBANS") => {
let entries: Vec<_> = trailing(rest).split_whitespace().filter_map(parse_extban_cap).collect();
if entries.is_empty() {
vec![]
} else {
vec![NetEvent::ExtbanRegistry { entries }]
}
} else {
vec![]
}
}
Some(s) if s.eq_ignore_ascii_case("CHANMODES") => {
let modes: Vec<_> = trailing(rest).split_whitespace().filter_map(parse_chanmode_cap).collect();
if modes.is_empty() {
vec![]
} else {
vec![NetEvent::ChanModeRegistry { modes }]
}
}
_ => vec![],
},
_ => vec![NetEvent::Unknown { line: line.to_string() }],
}
}
@ -555,6 +562,30 @@ fn parse_extban_cap(token: &str) -> Option<echo_api::ExtbanCap> {
(!name.is_empty()).then(|| echo_api::ExtbanCap { name: name.to_string(), letter, acting })
}
// Parse one `CAPAB CHANMODES` token into a ChanModeCap:
// list:ban=b param:key=k param-set:limit=l simple:moderated=m
// prefix:<rank>:op=@o (value is <symbol><letter>)
fn parse_chanmode_cap(token: &str) -> Option<echo_api::ChanModeCap> {
use echo_api::ChanModeKind::*;
let (ty, spec) = token.split_once(':')?;
if ty == "prefix" {
let (rank, rest) = spec.split_once(':')?;
let (_name, val) = rest.split_once('=')?;
let (symbol, letter) = (val.chars().next()?, val.chars().nth(1)?);
return Some(echo_api::ChanModeCap { letter, kind: Prefix { symbol, rank: rank.parse().ok()? } });
}
let (_name, val) = spec.split_once('=')?;
let letter = val.chars().next()?;
let kind = match ty {
"list" => List,
"param" => ParamAlways,
"param-set" => ParamSet,
"simple" => Simple,
_ => return None,
};
Some(echo_api::ChanModeCap { letter, kind })
}
fn trailing(rest: &str) -> String {
match rest.find(" :") {
Some(i) => rest[i + 2..].to_string(),
@ -620,6 +651,27 @@ mod tests {
assert!(p.parse("CAPAB CHANMODES :ban=b").is_empty(), "other CAPAB subcommands ignored");
}
// CAPAB CHANMODES surfaces mode arity + prefix modes, including a custom prefix
// like ojoin's Y (symbol !, rank 9000000) that the hardcoded set doesn't know.
#[test]
fn parses_capab_chanmodes() {
use echo_api::ChanModeKind::*;
let mut p = proto();
let ev = p.parse("CAPAB CHANMODES :list:ban=b param-set:limit=l param:key=k prefix:30000:op=@o prefix:9000000:official-join=!Y simple:moderated=m");
let [NetEvent::ChanModeRegistry { modes }] = ev.as_slice() else { panic!("{ev:?}") };
let find = |c: char| modes.iter().find(|m| m.letter == c).copied();
assert_eq!(find('b').unwrap().kind, List);
assert_eq!(find('l').unwrap().kind, ParamSet);
assert_eq!(find('k').unwrap().kind, ParamAlways);
assert_eq!(find('m').unwrap().kind, Simple);
assert_eq!(find('o').unwrap().kind, Prefix { symbol: '@', rank: 30000 });
assert_eq!(find('Y').unwrap().kind, Prefix { symbol: '!', rank: 9000000 }, "custom ojoin prefix");
// Arity: limit takes a param only when set; the key both ways.
assert!(find('l').unwrap().takes_param(true) && !find('l').unwrap().takes_param(false));
assert!(find('k').unwrap().takes_param(true) && find('k').unwrap().takes_param(false));
assert!(find('Y').unwrap().is_prefix());
}
// Both SERVER forms surface a ServerInfo (SID -> name) for the `server` extban:
// the uplink handshake (name password sid) and a sourced downstream link (name sid).
#[test]