diff --git a/api/src/lib.rs b/api/src/lib.rs index 7b24186..5d90eaa 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -648,7 +648,11 @@ pub struct Caps { pub fn level_caps(level: &str) -> Caps { match level { "founder" => Caps { auto: Some("+o"), op: true, topic: true, invite: true, access: true, set: true, greet: true, rank: 3 }, + // SOP = op plus access-list management; AOP = op; HOP = halfop; VOP = voice. + // These are the tiers the XOP shortcuts store; ordered high to low. + "sop" => Caps { auto: Some("+o"), op: true, topic: true, invite: true, access: true, rank: 2, ..Caps::default() }, "op" => Caps { auto: Some("+o"), op: true, topic: true, invite: true, rank: 2, ..Caps::default() }, + "halfop" => Caps { auto: Some("+h"), rank: 1, ..Caps::default() }, "voice" => Caps { auto: Some("+v"), rank: 1, ..Caps::default() }, flags => { let has = |c: char| flags.contains(c); @@ -685,6 +689,66 @@ pub fn level_caps(level: &str) -> Caps { } } +/// The tiered "XOP" role a stored access level resolves to. The four shortcut +/// tiers rank VOP < HOP < AOP < SOP (SOP is AOP plus access-list management); +/// the channel owner is `Founder`, and a granular FLAGS entry that lines up with +/// no tier is `Custom`. Classifying by resolved capability rather than the raw +/// level string means XOP- and FLAGS-set entries agree on which tier they are. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum AccessRole { + Founder, + Sop, + Aop, + Hop, + Vop, + Custom, +} + +impl AccessRole { + /// How the role reads in ALIST / STATUS output. + pub fn label(self) -> &'static str { + match self { + Self::Founder => "founder", + Self::Sop => "sop", + Self::Aop => "op", + Self::Hop => "halfop", + Self::Vop => "voice", + Self::Custom => "access", + } + } + + /// The XOP command word this role is the exact tier of, if any. Founder and + /// Custom entries belong to no XOP list. + pub fn xop_word(self) -> Option<&'static str> { + Some(match self { + Self::Sop => "SOP", + Self::Aop => "AOP", + Self::Hop => "HOP", + Self::Vop => "VOP", + _ => return None, + }) + } +} + +/// Classify a stored access `level` into its tiered role by resolved capability, +/// so a FLAGS entry lands in the same tier a XOP ADD would have given it. +pub fn access_role(level: &str) -> AccessRole { + let c = level_caps(level); + if c.set && c.access && c.op { + AccessRole::Founder + } else if c.op && c.access { + AccessRole::Sop + } else if c.op { + AccessRole::Aop + } else if c.auto == Some("+h") { + AccessRole::Hop + } else if c.auto == Some("+v") { + AccessRole::Vop + } else { + AccessRole::Custom + } +} + // The granular group-membership flags (GroupServ), same primitive, different // letters: F founder, f modify the member/flags list, i invite members, s set // group options, c the group may be granted channel access, m group memos. @@ -1595,6 +1659,30 @@ mod tests { let t = level_caps("t"); // topic-only: can topic, not op assert!(t.topic && !t.op && t.auto.is_none()); assert!(level_caps("a").access && level_caps("f").set); + + // The four XOP tiers are distinct: SOP is AOP plus access-list rights, + // HOP is its own halfop tier. + assert!(level_caps("sop").op && level_caps("sop").access); + assert!(level_caps("op").op && !level_caps("op").access); + assert_eq!(level_caps("halfop").auto, Some("+h")); + } + + #[test] + fn access_role_classifies_each_tier_by_capability() { + // Presets round-trip to their own tier... + assert_eq!(access_role("sop"), AccessRole::Sop); + assert_eq!(access_role("op"), AccessRole::Aop); + assert_eq!(access_role("halfop"), AccessRole::Hop); + assert_eq!(access_role("voice"), AccessRole::Vop); + assert_eq!(access_role("founder"), AccessRole::Founder); + // ...and a granular FLAGS entry lands in the matching tier, so it lists + // under the same XOP shortcut a tier ADD would have used. + assert_eq!(access_role("otia"), AccessRole::Sop, "op + access = SOP"); + assert_eq!(access_role("oti"), AccessRole::Aop, "op without access = AOP"); + assert_eq!(access_role("v"), AccessRole::Vop); + assert_eq!(access_role("ti"), AccessRole::Custom, "no status mode = no XOP tier"); + assert_eq!(access_role("sop").xop_word(), Some("SOP")); + assert_eq!(access_role("founder").xop_word(), None); } #[test] diff --git a/modules/chanserv/src/access.rs b/modules/chanserv/src/access.rs index ea70a14..5728e0d 100644 --- a/modules/chanserv/src/access.rs +++ b/modules/chanserv/src/access.rs @@ -1,10 +1,14 @@ use echo_api::Store; use echo_api::{Sender, ServiceCtx}; -// ACCESS <#channel> LIST | ADD | DEL +// The named tiers ACCESS ADD accepts, high to low; the granular FLAGS command +// covers anything finer. Kept in step with the XOP presets in `level_caps`. +const TIERS: [&str; 4] = ["sop", "op", "halfop", "voice"]; + +// ACCESS <#channel> LIST | ADD | DEL pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) { let Some(&chan) = args.get(1) else { - ctx.notice(me, from.uid, "Syntax: ACCESS <#channel> LIST | ADD | DEL "); + ctx.notice(me, from.uid, "Syntax: ACCESS <#channel> LIST | ADD | DEL "); return; }; match args.get(2).map(|s| s.to_ascii_uppercase()).as_deref() { @@ -20,12 +24,12 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: }, Some("ADD") => { let (Some(&account), Some(&level)) = (args.get(3), args.get(4)) else { - ctx.notice(me, from.uid, "Syntax: ACCESS <#channel> ADD "); + ctx.notice(me, from.uid, "Syntax: ACCESS <#channel> ADD "); return; }; let level = level.to_ascii_lowercase(); - if level != "op" && level != "voice" { - ctx.notice(me, from.uid, "Level must be \x02op\x02 or \x02voice\x02."); + if !TIERS.contains(&level.as_str()) { + ctx.notice(me, from.uid, "Level must be \x02sop\x02, \x02op\x02, \x02halfop\x02 or \x02voice\x02."); return; } if !is_founder(me, from, chan, ctx, db) { @@ -50,7 +54,7 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."), } } - _ => ctx.notice(me, from.uid, "Syntax: ACCESS <#channel> LIST | ADD | DEL "), + _ => ctx.notice(me, from.uid, "Syntax: ACCESS <#channel> LIST | ADD | DEL "), } } diff --git a/modules/chanserv/src/lib.rs b/modules/chanserv/src/lib.rs index be40efc..9b99410 100644 --- a/modules/chanserv/src/lib.rs +++ b/modules/chanserv/src/lib.rs @@ -52,9 +52,9 @@ const TOPICS: &[HelpEntry] = &[ HelpEntry { cmd: "INFO", summary: "show channel information", detail: "Syntax: \x02INFO <#channel>\x02\nShows a channel's registration and settings." }, HelpEntry { cmd: "LIST", summary: "list registered channels", detail: "Syntax: \x02LIST\x02\nLists registered channels." }, HelpEntry { cmd: "SET", summary: "change founder or settings", detail: "Syntax: \x02SET <#channel> FOUNDER | DESC | URL [address] | EMAIL [address] | SUCCESSOR |OFF | SIGNKICK|PRIVATE|PEACE|SECUREOPS|RESTRICTED|AUTOOP|KEEPTOPIC|TOPICLOCK {ON|OFF}\x02\nTransfers the founder or changes a channel setting." }, - HelpEntry { cmd: "ACCESS", summary: "manage the access list", detail: "Syntax: \x02ACCESS <#channel> LIST | ADD | DEL \x02\nManages the channel access list. The target may be a GroupServ \x02!group\x02; each of its members holding the group's \x02c\x02 flag then inherits the access." }, + HelpEntry { cmd: "ACCESS", summary: "manage the access list", detail: "Syntax: \x02ACCESS <#channel> LIST | ADD | DEL \x02\nManages the channel access list. The target may be a GroupServ \x02!group\x02; each of its members holding the group's \x02c\x02 flag then inherits the access." }, HelpEntry { cmd: "FLAGS", summary: "granular per-account flags", detail: "Syntax: \x02FLAGS <#channel> [account [+/-flags]]\x02\nViews or changes granular per-account channel flags." }, - HelpEntry { cmd: "AOP/SOP/VOP", summary: "tiered access shortcuts", detail: "Syntax: \x02AOP|SOP|VOP <#channel> ADD | DEL | LIST\x02\nTiered shortcuts over ACCESS: AOP and SOP grant op, VOP grants voice." }, + HelpEntry { cmd: "SOP/AOP/HOP/VOP", summary: "tiered access shortcuts", detail: "Syntax: \x02SOP|AOP|HOP|VOP <#channel> ADD | DEL | LIST\x02\nTiered shortcuts over ACCESS: SOP grants op plus access-list management, AOP grants op, HOP grants halfop, VOP grants voice." }, HelpEntry { cmd: "STATUS", summary: "show a user's access", detail: "Syntax: \x02STATUS <#channel> [nick]\x02\nShows a user's access level on a channel." }, HelpEntry { cmd: "OP/DEOP/VOICE/DEVOICE", summary: "give or take op/voice", detail: "Syntax: \x02OP|DEOP|VOICE|DEVOICE <#channel> [nick]\x02\nGives or takes channel op or voice." }, HelpEntry { cmd: "UP/DOWN", summary: "apply or drop your status", detail: "Syntax: \x02UP|DOWN <#channel>\x02\nUP re-applies the op/voice your access entitles you to; DOWN removes your status." }, @@ -298,8 +298,9 @@ impl Service for ChanServ { // the mode lock and akick list, so SYNC is the same handler. Some("ENFORCE") | Some("SYNC") => enforce::handle(me, from, args, ctx, net, db), Some("CLONE") => clone::handle(me, from, args, ctx, db), + Some("SOP") => xop::handle(me, from, "SOP", "sop", args, ctx, db), Some("AOP") => xop::handle(me, from, "AOP", "op", args, ctx, db), - Some("SOP") => xop::handle(me, from, "SOP", "op", args, ctx, db), + Some("HOP") => xop::handle(me, from, "HOP", "halfop", args, ctx, db), Some("VOP") => xop::handle(me, from, "VOP", "voice", args, ctx, db), Some("HELP") => echo_api::help(me, from, ctx, BLURB, TOPICS, args.get(1).copied()), Some(other) => ctx.notice(me, from.uid, format!("I don't know the command \x02{other}\x02. Try \x02HELP\x02.")), diff --git a/modules/chanserv/src/status.rs b/modules/chanserv/src/status.rs index 0467ffd..9b4612c 100644 --- a/modules/chanserv/src/status.rs +++ b/modules/chanserv/src/status.rs @@ -1,5 +1,5 @@ use echo_api::Store; -use echo_api::{Sender, ServiceCtx}; +use echo_api::{access_role, Sender, ServiceCtx}; use echo_api::NetView; // STATUS <#channel> [nick]: show a user's access level (self if no nick). @@ -19,10 +19,11 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: let level = match account.as_deref() { None => "none (not logged in)", Some(a) if info.founder.eq_ignore_ascii_case(a) => "founder", - Some(a) => match info.join_mode(a) { - Some("+o") => "op", - Some("+v") => "voice", - _ => "none", + // Classify by resolved tier so halfop and sop read distinctly, matching + // ALIST and the XOP lists. + Some(a) => match info.access.iter().find(|e| e.account.eq_ignore_ascii_case(a)) { + Some(e) => access_role(&e.level).label(), + None => "none", }, }; ctx.notice(me, from.uid, format!("\x02{label}\x02 access on \x02{chan}\x02: \x02{level}\x02")); diff --git a/modules/chanserv/src/xop.rs b/modules/chanserv/src/xop.rs index d3ec348..53579df 100644 --- a/modules/chanserv/src/xop.rs +++ b/modules/chanserv/src/xop.rs @@ -1,8 +1,8 @@ -use echo_api::{level_caps, Sender, ServiceCtx, Store}; +use echo_api::{access_role, Sender, ServiceCtx, Store}; -// AOP/SOP/VOP <#channel> ADD | DEL | LIST — tiered -// shortcuts over the access list. `level` is the access level they map to -// ("op" for AOP/SOP, "voice" for VOP); `word` is what the user typed. +// SOP/AOP/HOP/VOP <#channel> ADD | DEL | LIST — tiered +// shortcuts over the access list. `level` is the tier they map to ("sop", "op", +// "halfop", "voice"); `word` is the tier the user typed ("SOP".."VOP"). pub fn handle(me: &str, from: &Sender, word: &str, level: &str, args: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) { let Some(&chan) = args.get(1) else { ctx.notice(me, from.uid, format!("Syntax: {word} <#channel> ADD | DEL | LIST")); @@ -13,14 +13,9 @@ pub fn handle(me: &str, from: &Sender, word: &str, level: &str, args: &[&str], c None => ctx.notice(me, from.uid, format!("\x02{chan}\x02 isn't registered.")), Some(info) => { ctx.notice(me, from.uid, format!("{word} list for \x02{}\x02:", info.name)); - // Match by capability, not the raw level string, so entries set via - // granular FLAGS still list under the right tier (VOP = voice-only, - // AOP/SOP = op). - let want_op = level_caps(level).op; - for a in info.access.iter().filter(|a| { - let c = level_caps(&a.level); - if want_op { c.op } else { c.auto == Some("+v") && !c.op } - }) { + // List by resolved tier, not the raw level string, so an entry set + // via granular FLAGS appears under the same tier a XOP ADD would. + for a in info.access.iter().filter(|a| access_role(&a.level).xop_word() == Some(word)) { ctx.notice(me, from.uid, format!(" \x02{}\x02", a.account)); } } diff --git a/modules/nickserv/src/alist.rs b/modules/nickserv/src/alist.rs index 92ee040..fb3c6d2 100644 --- a/modules/nickserv/src/alist.rs +++ b/modules/nickserv/src/alist.rs @@ -1,4 +1,4 @@ -use echo_api::{level_caps, Sender, ServiceCtx, Store}; +use echo_api::{access_role, Sender, ServiceCtx, Store}; // ALIST: list the channels the sender's account founds or has access on. pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &dyn Store) { @@ -11,20 +11,9 @@ pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &dyn Store) { if c.founder.eq_ignore_ascii_case(account) { rows.push((c.name.clone(), "founder")); } else if let Some(a) = c.access.iter().find(|a| a.account.eq_ignore_ascii_case(account)) { - // Resolve the entry's capabilities rather than string-matching the - // level, so a role set via granular FLAGS (e.g. "v") reads correctly - // and isn't mislabelled "op". - let caps = level_caps(&a.level); - let role = if caps.op { - "op" - } else if caps.auto == Some("+v") { - "voice" - } else if caps.auto == Some("+h") { - "halfop" - } else { - "access" - }; - rows.push((c.name.clone(), role)); + // Classify by resolved capability, not the raw level string, so a role + // set via granular FLAGS reads under the same tier XOP would show. + rows.push((c.name.clone(), access_role(&a.level).label())); } } if rows.is_empty() {