chanserv: op, deop, voice, devoice, kick, ban, unban

Adds channel moderation commands sourced from the ChanServ pseudoclient.
Tracks connected users' nicks and hosts so targets can be resolved by
nick and banned by *!*@host. All commands require operator access.
This commit is contained in:
Jean Chevronnet 2026-07-12 11:21:35 +00:00
parent f0ce2d9d1b
commit acd0e60946
No known key found for this signature in database
13 changed files with 233 additions and 26 deletions

22
modules/chanserv/ban.rs Normal file
View file

@ -0,0 +1,22 @@
use crate::engine::db::Db;
use crate::engine::service::{Sender, ServiceCtx};
use crate::engine::state::Network;
// BAN <#channel> <nick> [reason]: ban *!*@host and kick the user.
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: &Network, db: &Db) {
let (Some(&chan), Some(&nick)) = (args.get(1), args.get(2)) else {
ctx.notice(me, from.uid, "Syntax: BAN <#channel> <nick> [reason]");
return;
};
if !super::require_op(me, from, chan, ctx, db) {
return;
}
let Some(target) = net.uid_by_nick(nick).map(str::to_string) else {
ctx.notice(me, from.uid, format!("\x02{nick}\x02 isn't here."));
return;
};
let host = net.host_of(&target).unwrap_or("*");
ctx.channel_mode(me, chan, &format!("+b *!*@{host}"));
let reason = if args.len() > 3 { args[3..].join(" ") } else { "Banned".to_string() };
ctx.kick(me, chan, &target, &reason);
}

View file

@ -1,10 +1,19 @@
use crate::engine::db::{ChanError, ChannelInfo, Db};
use crate::engine::service::{Sender, Service, ServiceCtx};
use crate::engine::state::Network;
#[path = "mode.rs"]
mod mode;
#[path = "access.rs"]
mod access;
#[path = "op.rs"]
mod op;
#[path = "kick.rs"]
mod kick;
#[path = "ban.rs"]
mod ban;
#[path = "unban.rs"]
mod unban;
pub struct ChanServ {
pub uid: String,
@ -24,7 +33,7 @@ impl Service for ChanServ {
true
}
fn on_command(&mut self, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mut Db) {
fn on_command(&mut self, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: &Network, db: &mut Db) {
let me = self.uid.as_str();
match args.first().map(|s| s.to_ascii_uppercase()).as_deref() {
Some("REGISTER") => {
@ -130,13 +139,35 @@ impl Service for ChanServ {
}
Some("MODE") => mode::handle(me, from, args, ctx, db),
Some("ACCESS") => access::handle(me, from, args, ctx, db),
Some("HELP") => ctx.notice(me, from.uid, "ChanServ registers and looks after channels. Commands: \x02REGISTER\x02 <#channel>, \x02INFO\x02 <#channel>, \x02ACCESS\x02 <#channel>, \x02MODE\x02 <#channel> <modes>, \x02MLOCK\x02 <#channel> [modes], \x02DROP\x02 <#channel>."),
Some("OP") => op::handle(me, from, "+o", args, ctx, net, db),
Some("DEOP") => op::handle(me, from, "-o", args, ctx, net, db),
Some("VOICE") => op::handle(me, from, "+v", args, ctx, net, db),
Some("DEVOICE") => op::handle(me, from, "-v", args, ctx, net, db),
Some("KICK") => kick::handle(me, from, args, ctx, net, db),
Some("BAN") => ban::handle(me, from, args, ctx, net, db),
Some("UNBAN") => unban::handle(me, from, args, ctx, net, db),
Some("HELP") => ctx.notice(me, from.uid, "ChanServ registers and looks after channels. Commands: \x02REGISTER\x02, \x02INFO\x02, \x02ACCESS\x02, \x02OP\x02/\x02DEOP\x02, \x02VOICE\x02/\x02DEVOICE\x02, \x02KICK\x02, \x02BAN\x02/\x02UNBAN\x02, \x02MODE\x02, \x02MLOCK\x02, \x02DROP\x02 — each takes a <#channel>."),
Some(other) => ctx.notice(me, from.uid, format!("I don't know the command \x02{other}\x02. Try \x02HELP\x02.")),
None => {}
}
}
}
// True if `from` is the founder or an access-list op of `chan`; else notices why.
fn require_op(me: &str, from: &Sender, chan: &str, ctx: &mut ServiceCtx, db: &Db) -> bool {
match (from.account, db.channel(chan)) {
(_, None) => {
ctx.notice(me, from.uid, format!("\x02{chan}\x02 isn't registered."));
false
}
(Some(acc), Some(info)) if info.is_op(acc) => true,
_ => {
ctx.notice(me, from.uid, format!("You need operator access to \x02{chan}\x02."));
false
}
}
}
// Parse a mode spec like "+nt-s" into (locked-on, locked-off) mode chars. `r` is
// implicit for a registered channel and is ignored here.
fn parse_mlock(spec: &str) -> (String, String) {

20
modules/chanserv/kick.rs Normal file
View file

@ -0,0 +1,20 @@
use crate::engine::db::Db;
use crate::engine::service::{Sender, ServiceCtx};
use crate::engine::state::Network;
// KICK <#channel> <nick> [reason]: kick a user.
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: &Network, db: &Db) {
let (Some(&chan), Some(&nick)) = (args.get(1), args.get(2)) else {
ctx.notice(me, from.uid, "Syntax: KICK <#channel> <nick> [reason]");
return;
};
if !super::require_op(me, from, chan, ctx, db) {
return;
}
let Some(target) = net.uid_by_nick(nick) else {
ctx.notice(me, from.uid, format!("\x02{nick}\x02 isn't here."));
return;
};
let reason = if args.len() > 3 { args[3..].join(" ") } else { "Kicked".to_string() };
ctx.kick(me, chan, target, &reason);
}

26
modules/chanserv/op.rs Normal file
View file

@ -0,0 +1,26 @@
use crate::engine::db::Db;
use crate::engine::service::{Sender, ServiceCtx};
use crate::engine::state::Network;
// OP/DEOP/VOICE/DEVOICE <#channel> [nick]: set a status mode on a user (self if
// no nick given). `mode` is the mode to apply, e.g. "+o".
pub fn handle(me: &str, from: &Sender, mode: &str, args: &[&str], ctx: &mut ServiceCtx, net: &Network, db: &Db) {
let Some(&chan) = args.get(1) else {
ctx.notice(me, from.uid, "Syntax: OP/DEOP/VOICE/DEVOICE <#channel> [nick]");
return;
};
if !super::require_op(me, from, chan, ctx, db) {
return;
}
let target = match args.get(2) {
Some(&nick) => match net.uid_by_nick(nick) {
Some(u) => u.to_string(),
None => {
ctx.notice(me, from.uid, format!("\x02{nick}\x02 isn't here."));
return;
}
},
None => from.uid.to_string(),
};
ctx.channel_mode(me, chan, &format!("{mode} {target}"));
}

22
modules/chanserv/unban.rs Normal file
View file

@ -0,0 +1,22 @@
use crate::engine::db::Db;
use crate::engine::service::{Sender, ServiceCtx};
use crate::engine::state::Network;
// UNBAN <#channel> [nick]: remove the *!*@host ban of a user (self if no nick).
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: &Network, db: &Db) {
let Some(&chan) = args.get(1) else {
ctx.notice(me, from.uid, "Syntax: UNBAN <#channel> [nick]");
return;
};
if !super::require_op(me, from, chan, ctx, db) {
return;
}
let target = args
.get(2)
.and_then(|&n| net.uid_by_nick(n))
.map(str::to_string)
.unwrap_or_else(|| from.uid.to_string());
let host = net.host_of(&target).unwrap_or("*");
ctx.channel_mode(me, chan, &format!("-b *!*@{host}"));
ctx.notice(me, from.uid, format!("Cleared the *!*@{host} ban on \x02{chan}\x02."));
}