diff --git a/api/src/lib.rs b/api/src/lib.rs index 844f85b..05745f9 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -130,6 +130,23 @@ pub trait Protocol: Send { 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 // --------------------------------------------------------------------------- diff --git a/inspircd/src/lib.rs b/inspircd/src/lib.rs index 5a22011..8a721a2 100644 --- a/inspircd/src/lib.rs +++ b/inspircd/src/lib.rs @@ -343,16 +343,9 @@ impl Protocol for InspIrcd { } } -// Whether a channel mode consumes a parameter, for standard InspIRCd chanmodes. -// List and prefix modes take one on both set and unset; the key takes one on -// both; the rest that take one do so only on set. -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, - } -} +// Whether a channel mode consumes a parameter — the shared canonical arity, so +// parsing here and MODE-building in services never drift. +use fedserv_api::chanmode_takes_param as takes_param; // 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. diff --git a/operserv/src/kick.rs b/operserv/src/kick.rs new file mode 100644 index 0000000..941c554 --- /dev/null +++ b/operserv/src/kick.rs @@ -0,0 +1,26 @@ +use fedserv_api::{NetView, Priv, Sender, ServiceCtx}; + +// KICK <#channel> [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> [reason]"); + return; + }; + let Some(&target) = args.get(2) else { + ctx.notice(me, from.uid, "Syntax: KICK <#channel> [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.")); +} diff --git a/operserv/src/lib.rs b/operserv/src/lib.rs index f9539df..9309cf5 100644 --- a/operserv/src/lib.rs +++ b/operserv/src/lib.rs @@ -11,6 +11,10 @@ mod xline; mod global; #[path = "kill.rs"] mod kill; +#[path = "kick.rs"] +mod kick; +#[path = "mode.rs"] +mod mode; pub struct OperServ { 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("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("HELP") => 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.")), @@ -47,5 +53,5 @@ impl Service for OperServ { } 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 (announce to everyone), \x02KILL\x02 [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 (announce to everyone), \x02KILL\x02 [reason] (disconnect a user), \x02KICK\x02 <#chan> [reason], \x02MODE\x02 <#chan> [params]."); } diff --git a/operserv/src/mode.rs b/operserv/src/mode.rs new file mode 100644 index 0000000..5780d5c --- /dev/null +++ b/operserv/src/mode.rs @@ -0,0 +1,45 @@ +use fedserv_api::{chanmode_takes_param, NetView, Priv, Sender, ServiceCtx, STATUS_MODES}; + +// MODE <#channel> [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> [params]"); + return; + }; + let Some(&modes) = args.get(2) else { + ctx.notice(me, from.uid, "Syntax: MODE <#channel> [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 = 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.")); +} diff --git a/src/engine/mod.rs b/src/engine/mod.rs index c1581e7..4771e0c 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -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. #[test] fn noexpire_command_is_oper_gated() {