OperServ: MODE override and KICK

Two channel operator tools:

- MODE <#chan> <modes> [params] forces a channel mode change (TS 1, so it
  applies regardless of the current TS). A status-mode target may be given
  as a nick — it's resolved to the uid the ircd's FMODE expects.
- KICK <#chan> <nick> [reason] removes a user, sourced from OperServ and
  attributed to the operator.

Both admin-gated. To resolve status-mode params, channel-mode parameter
arity became one shared helper (chanmode_takes_param + STATUS_MODES in the
api): the ircd module now delegates to it instead of keeping its own copy,
so burst-parsing and MODE-building can never drift.
This commit is contained in:
Jean Chevronnet 2026-07-14 00:54:29 +00:00
parent 930198b826
commit c5d93f29c1
No known key found for this signature in database
6 changed files with 147 additions and 11 deletions

View file

@ -130,6 +130,23 @@ pub trait Protocol: Send {
fn sid(&self) -> &str; fn sid(&self) -> &str;
} }
// Whether a channel mode consumes a parameter, given the direction (`adding`).
// The canonical arity for the standard chanmodes, shared by the protocol module
// (parsing bursts) and services (building a MODE change): list and prefix modes
// and the key take a parameter both ways; the rest that take one do so only when
// set. A single source of truth so the two never drift.
pub fn chanmode_takes_param(m: char, adding: bool) -> bool {
match m {
'b' | 'e' | 'I' | 'q' | 'a' | 'o' | 'h' | 'v' | 'k' => true,
'l' | 'L' | 'f' | 'j' | 'J' | 'F' | 'H' => adding,
_ => false,
}
}
// The channel prefix (status) modes, whose parameter is a nick/uid rather than a
// mask or value.
pub const STATUS_MODES: &str = "qaohv";
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Service vocabulary // Service vocabulary
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------

View file

@ -343,16 +343,9 @@ impl Protocol for InspIrcd {
} }
} }
// Whether a channel mode consumes a parameter, for standard InspIRCd chanmodes. // Whether a channel mode consumes a parameter — the shared canonical arity, so
// List and prefix modes take one on both set and unset; the key takes one on // parsing here and MODE-building in services never drift.
// both; the rest that take one do so only on set. use fedserv_api::chanmode_takes_param as takes_param;
fn takes_param(m: char, adding: bool) -> bool {
match m {
'b' | 'e' | 'I' | 'q' | 'a' | 'o' | 'h' | 'v' | 'k' => true,
'l' | 'L' | 'f' | 'j' | 'J' | 'F' | 'H' => adding,
_ => false,
}
}
// Walk a mode change and its params for a key (+k/-k). Returns Some(Some(key)) // Walk a mode change and its params for a key (+k/-k). Returns Some(Some(key))
// when a key is set, Some(None) when cleared, None when `k` isn't in the change. // when a key is set, Some(None) when cleared, None when `k` isn't in the change.

26
operserv/src/kick.rs Normal file
View file

@ -0,0 +1,26 @@
use fedserv_api::{NetView, Priv, Sender, ServiceCtx};
// KICK <#channel> <nick> [reason]: remove a user from a channel, sourced from
// OperServ so it's clearly a staff action. Admin-only.
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: &dyn NetView) {
if !from.privs.has(Priv::Admin) {
ctx.notice(me, from.uid, "Access denied — KICK needs the \x02admin\x02 privilege.");
return;
}
let Some(&chan) = args.get(1).filter(|c| c.starts_with('#') || c.starts_with('&')) else {
ctx.notice(me, from.uid, "Syntax: KICK <#channel> <nick> [reason]");
return;
};
let Some(&target) = args.get(2) else {
ctx.notice(me, from.uid, "Syntax: KICK <#channel> <nick> [reason]");
return;
};
let Some(uid) = net.uid_by_nick(target).map(str::to_string) else {
ctx.notice(me, from.uid, format!("There's no \x02{target}\x02 online."));
return;
};
let by = from.account.unwrap_or(from.nick);
let reason = if args.len() > 3 { args[3..].join(" ") } else { "Removed by services".to_string() };
ctx.kick(me, chan, &uid, &format!("({by}) {reason}"));
ctx.notice(me, from.uid, format!("\x02{target}\x02 kicked from \x02{chan}\x02."));
}

View file

@ -11,6 +11,10 @@ mod xline;
mod global; mod global;
#[path = "kill.rs"] #[path = "kill.rs"]
mod kill; mod kill;
#[path = "kick.rs"]
mod kick;
#[path = "mode.rs"]
mod mode;
pub struct OperServ { pub struct OperServ {
pub uid: String, pub uid: String,
@ -39,6 +43,8 @@ impl Service for OperServ {
Some(cmd) if cmd.eq_ignore_ascii_case("SQLINE") => xline::SQLINE.handle(me, from, args, ctx, db), Some(cmd) if cmd.eq_ignore_ascii_case("SQLINE") => xline::SQLINE.handle(me, from, args, ctx, db),
Some(cmd) if cmd.eq_ignore_ascii_case("GLOBAL") => global::handle(me, from, args, ctx), 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("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("HELP") => help(me, from, ctx), Some(cmd) if cmd.eq_ignore_ascii_case("HELP") => help(me, from, ctx),
None => help(me, from, ctx), None => help(me, from, ctx),
Some(other) => ctx.notice(me, from.uid, format!("I don't know \x02{other}\x02. Try \x02HELP\x02.")), Some(other) => ctx.notice(me, from.uid, format!("I don't know \x02{other}\x02. Try \x02HELP\x02.")),
@ -47,5 +53,5 @@ impl Service for OperServ {
} }
fn help(me: &str, from: &Sender, ctx: &mut ServiceCtx) { fn help(me: &str, from: &Sender, ctx: &mut ServiceCtx) {
ctx.notice(me, from.uid, "OperServ holds network operator tools (Priv::Admin): \x02AKILL\x02 ADD|DEL|LIST (user@host bans), \x02SQLINE\x02 ADD|DEL|LIST (nick bans), \x02GLOBAL\x02 <message> (announce to everyone), \x02KILL\x02 <nick> [reason] (disconnect a user)."); ctx.notice(me, from.uid, "OperServ holds network operator tools (Priv::Admin): \x02AKILL\x02 ADD|DEL|LIST (user@host bans), \x02SQLINE\x02 ADD|DEL|LIST (nick bans), \x02GLOBAL\x02 <message> (announce to everyone), \x02KILL\x02 <nick> [reason] (disconnect a user), \x02KICK\x02 <#chan> <nick> [reason], \x02MODE\x02 <#chan> <modes> [params].");
} }

45
operserv/src/mode.rs Normal file
View file

@ -0,0 +1,45 @@
use fedserv_api::{chanmode_takes_param, NetView, Priv, Sender, ServiceCtx, STATUS_MODES};
// 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) {
if !from.privs.has(Priv::Admin) {
ctx.notice(me, from.uid, "Access denied — MODE needs the \x02admin\x02 privilege.");
return;
}
let Some(&chan) = args.get(1).filter(|c| c.starts_with('#') || c.starts_with('&')) else {
ctx.notice(me, from.uid, "Syntax: MODE <#channel> <modes> [params]");
return;
};
let Some(&modes) = args.get(2) else {
ctx.notice(me, from.uid, "Syntax: MODE <#channel> <modes> [params]");
return;
};
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.
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 let Some(&p) = params.get(pi) {
pi += 1;
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());
}
}
}
_ => {}
}
}
let full = if out_params.is_empty() { modes.to_string() } else { format!("{} {}", modes, out_params.join(" ")) };
ctx.channel_mode(me, chan, &full);
ctx.notice(me, from.uid, format!("Set \x02{modes}\x02 on \x02{chan}\x02."));
}

View file

@ -3957,6 +3957,55 @@ mod tests {
} }
} }
// OperServ MODE (forced channel mode override, resolving a status-mode nick
// to its uid) and KICK (remove a user from a channel), both admin-gated.
#[test]
fn operserv_mode_and_kick() {
use fedserv_operserv::OperServ;
let path = std::env::temp_dir().join("fedserv-osmodekick.jsonl");
let _ = std::fs::remove_file(&path);
let mut db = Db::open(&path, "42S");
db.scram_iterations = 4096;
db.register("staff", "password1", None).unwrap();
db.register("plain", "password1", None).unwrap();
let mut e = Engine::new(
vec![
Box::new(NickServ { uid: "42SAAAAAA".into(), guest_nick: "Guest".into(), guest_seq: 0 }),
Box::new(OperServ { uid: "42SAAAAAH".into() }),
],
db,
);
e.set_sid("42S".into());
let mut opers = std::collections::HashMap::new();
opers.insert("staff".to_string(), Privs::default().with(fedserv_api::Priv::Admin));
e.set_opers(opers);
let os = |e: &mut Engine, uid: &str, t: &str| e.handle(NetEvent::Privmsg { from: uid.into(), to: "42SAAAAAH".into(), text: t.into() });
e.handle(NetEvent::UserConnect { uid: "000AAAAAS".into(), nick: "staff".into(), host: "h".into() });
e.handle(NetEvent::Privmsg { from: "000AAAAAS".into(), to: "42SAAAAAA".into(), text: "IDENTIFY password1".into() });
e.handle(NetEvent::UserConnect { uid: "000AAAAAP".into(), nick: "plain".into(), host: "h".into() });
e.handle(NetEvent::Privmsg { from: "000AAAAAP".into(), to: "42SAAAAAA".into(), text: "IDENTIFY password1".into() });
e.handle(NetEvent::UserConnect { uid: "000AAAAAT".into(), nick: "target".into(), host: "h".into() });
// A status mode's nick target is resolved to its uid in the FMODE.
let out = os(&mut e, "000AAAAAS", "MODE #room +o target");
assert!(out.iter().any(|a| matches!(a, NetAction::ChannelMode { from, channel, modes } if from == "42SAAAAAH" && channel == "#room" && modes == "+o 000AAAAAT")), "status mode resolved: {out:?}");
// A paramless mode passes straight through.
let out = os(&mut e, "000AAAAAS", "MODE #room +mn");
assert!(out.iter().any(|a| matches!(a, NetAction::ChannelMode { modes, .. } if modes == "+mn")), "paramless mode: {out:?}");
// KICK removes the user, attributed to the operator.
let out = os(&mut e, "000AAAAAS", "KICK #room target trolling");
assert!(out.iter().any(|a| matches!(a, NetAction::Kick { from, channel, uid, reason } if from == "42SAAAAAH" && channel == "#room" && uid == "000AAAAAT" && reason.contains("trolling") && reason.contains("staff"))), "kick issued: {out:?}");
// A non-admin gets nothing but a refusal.
for cmd in ["MODE #room +o target", "KICK #room target go"] {
let out = os(&mut e, "000AAAAAP", cmd);
assert!(out.iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains("Access denied"))), "non-admin refused {cmd}");
assert!(!out.iter().any(|a| matches!(a, NetAction::ChannelMode { .. } | NetAction::Kick { .. })), "no action from non-admin {cmd}");
}
}
// NOEXPIRE is oper-only. // NOEXPIRE is oper-only.
#[test] #[test]
fn noexpire_command_is_oper_gated() { fn noexpire_command_is_oper_gated() {