chanserv: channel SUSPEND / UNSUSPEND (oper-gated)
Freezes a channel gated on Priv::Suspend: a typed Suspension on ChannelInfo (event-logged, snapshotted, lazy expiry — same shape as the account one, no Anope Extensible/Checker or expiry timer). While suspended, ChanServ refuses all management (guards in require_op/require_founder and the founder-inline SET/DROP/MLOCK), the engine skips Join enforcement (no auto-op/akick/entrymsg), and suspending kicks everyone out; INFO shows it. parse_duration moved into the SDK so both SUSPEND commands share it. Data + full-flow tests.
This commit is contained in:
parent
bbbe2c6cb8
commit
cb081a2e95
8 changed files with 241 additions and 18 deletions
|
|
@ -345,6 +345,7 @@ pub struct ChannelView {
|
||||||
pub secureops: bool,
|
pub secureops: bool,
|
||||||
pub keeptopic: bool,
|
pub keeptopic: bool,
|
||||||
pub topiclock: bool,
|
pub topiclock: bool,
|
||||||
|
pub suspended: bool,
|
||||||
// Last known topic (for KEEPTOPIC / TOPICLOCK).
|
// Last known topic (for KEEPTOPIC / TOPICLOCK).
|
||||||
pub topic: String,
|
pub topic: String,
|
||||||
}
|
}
|
||||||
|
|
@ -491,6 +492,10 @@ pub trait Store {
|
||||||
fn set_desc(&mut self, channel: &str, desc: &str) -> Result<(), ChanError>;
|
fn set_desc(&mut self, channel: &str, desc: &str) -> Result<(), ChanError>;
|
||||||
fn set_channel_setting(&mut self, channel: &str, setting: ChanSetting, on: bool) -> Result<(), ChanError>;
|
fn set_channel_setting(&mut self, channel: &str, setting: ChanSetting, on: bool) -> Result<(), ChanError>;
|
||||||
fn set_channel_topic(&mut self, channel: &str, topic: &str) -> Result<(), ChanError>;
|
fn set_channel_topic(&mut self, channel: &str, topic: &str) -> Result<(), ChanError>;
|
||||||
|
fn suspend_channel(&mut self, channel: &str, by: &str, reason: &str, expires: Option<u64>) -> Result<(), ChanError>;
|
||||||
|
fn unsuspend_channel(&mut self, channel: &str) -> Result<bool, ChanError>;
|
||||||
|
fn is_channel_suspended(&self, channel: &str) -> bool;
|
||||||
|
fn channel_suspension(&self, channel: &str) -> Option<SuspensionView>;
|
||||||
fn set_entrymsg(&mut self, channel: &str, msg: &str) -> Result<(), ChanError>;
|
fn set_entrymsg(&mut self, channel: &str, msg: &str) -> Result<(), ChanError>;
|
||||||
fn set_founder(&mut self, channel: &str, account: &str) -> Result<(), ChanError>;
|
fn set_founder(&mut self, channel: &str, account: &str) -> Result<(), ChanError>;
|
||||||
fn access_add(&mut self, channel: &str, account: &str, level: &str) -> Result<(), ChanError>;
|
fn access_add(&mut self, channel: &str, account: &str, level: &str) -> Result<(), ChanError>;
|
||||||
|
|
@ -535,6 +540,20 @@ pub trait Service: Send {
|
||||||
fn on_command(&mut self, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: &dyn NetView, store: &mut dyn Store);
|
fn on_command(&mut self, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: &dyn NetView, store: &mut dyn Store);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse a duration like "30d", "12h", "45m", "90s", or bare seconds. Shared by
|
||||||
|
// the SUSPEND commands.
|
||||||
|
pub fn parse_duration(s: &str) -> Option<u64> {
|
||||||
|
let (num, mult) = match s.chars().last()? {
|
||||||
|
'd' | 'D' => (&s[..s.len() - 1], 86_400),
|
||||||
|
'h' | 'H' => (&s[..s.len() - 1], 3_600),
|
||||||
|
'm' | 'M' => (&s[..s.len() - 1], 60),
|
||||||
|
's' | 'S' => (&s[..s.len() - 1], 1),
|
||||||
|
c if c.is_ascii_digit() => (s, 1),
|
||||||
|
_ => return None,
|
||||||
|
};
|
||||||
|
num.parse::<u64>().ok().map(|n| n * mult)
|
||||||
|
}
|
||||||
|
|
||||||
// Case-insensitive hostmask glob: `*` (any run) and `?` (one char).
|
// Case-insensitive hostmask glob: `*` (any run) and `?` (one char).
|
||||||
fn glob_match(pattern: &str, text: &str) -> bool {
|
fn glob_match(pattern: &str, text: &str) -> bool {
|
||||||
let (p, t): (Vec<char>, Vec<char>) = (
|
let (p, t): (Vec<char>, Vec<char>) = (
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,8 @@ mod enforce;
|
||||||
mod clone;
|
mod clone;
|
||||||
#[path = "xop.rs"]
|
#[path = "xop.rs"]
|
||||||
mod xop;
|
mod xop;
|
||||||
|
#[path = "suspend.rs"]
|
||||||
|
mod suspend;
|
||||||
|
|
||||||
pub struct ChanServ {
|
pub struct ChanServ {
|
||||||
pub uid: String,
|
pub uid: String,
|
||||||
|
|
@ -101,6 +103,9 @@ impl Service for ChanServ {
|
||||||
ctx.notice(me, from.uid, format!(" Description: {}", info.desc));
|
ctx.notice(me, from.uid, format!(" Description: {}", info.desc));
|
||||||
}
|
}
|
||||||
ctx.notice(me, from.uid, format!(" Registered : {}", fedserv_api::human_time(info.ts)));
|
ctx.notice(me, from.uid, format!(" Registered : {}", fedserv_api::human_time(info.ts)));
|
||||||
|
if let Some(s) = db.channel_suspension(chan) {
|
||||||
|
ctx.notice(me, from.uid, format!(" Suspended : by \x02{}\x02 — {}", s.by, s.reason));
|
||||||
|
}
|
||||||
let mut opts = Vec::new();
|
let mut opts = Vec::new();
|
||||||
if info.signkick { opts.push("SIGNKICK"); }
|
if info.signkick { opts.push("SIGNKICK"); }
|
||||||
if info.private { opts.push("PRIVATE"); }
|
if info.private { opts.push("PRIVATE"); }
|
||||||
|
|
@ -132,6 +137,9 @@ impl Service for ChanServ {
|
||||||
ctx.notice(me, from.uid, format!("Only \x02{chan}\x02's founder can drop it."));
|
ctx.notice(me, from.uid, format!("Only \x02{chan}\x02's founder can drop it."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if suspended_block(me, from, chan, ctx, db) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
match db.drop_channel(chan) {
|
match db.drop_channel(chan) {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
ctx.channel_mode(me, chan, "-r"); // no longer registered
|
ctx.channel_mode(me, chan, "-r"); // no longer registered
|
||||||
|
|
@ -168,6 +176,9 @@ impl Service for ChanServ {
|
||||||
ctx.notice(me, from.uid, format!("Only \x02{chan}\x02's founder can set its mode lock."));
|
ctx.notice(me, from.uid, format!("Only \x02{chan}\x02's founder can set its mode lock."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if suspended_block(me, from, chan, ctx, db) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
let (on, off) = parse_mlock(&args[2..].concat());
|
let (on, off) = parse_mlock(&args[2..].concat());
|
||||||
match db.set_mlock(chan, &on, &off) {
|
match db.set_mlock(chan, &on, &off) {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
|
|
@ -192,6 +203,8 @@ impl Service for ChanServ {
|
||||||
Some("INVITE") => invite::handle(me, from, args, ctx, net, db),
|
Some("INVITE") => invite::handle(me, from, args, ctx, net, db),
|
||||||
Some("AKICK") => akick::handle(me, from, args, ctx, db),
|
Some("AKICK") => akick::handle(me, from, args, ctx, db),
|
||||||
Some("STATUS") => status::handle(me, from, args, ctx, net, db),
|
Some("STATUS") => status::handle(me, from, args, ctx, net, db),
|
||||||
|
Some("SUSPEND") => suspend::handle(me, from, args, ctx, net, db, true),
|
||||||
|
Some("UNSUSPEND") => suspend::handle(me, from, args, ctx, net, db, false),
|
||||||
Some("LIST") => list::handle(me, from, args, ctx, db),
|
Some("LIST") => list::handle(me, from, args, ctx, db),
|
||||||
Some("SET") => set::handle(me, from, args, ctx, db),
|
Some("SET") => set::handle(me, from, args, ctx, db),
|
||||||
Some("ENTRYMSG") => entrymsg::handle(me, from, args, ctx, db),
|
Some("ENTRYMSG") => entrymsg::handle(me, from, args, ctx, db),
|
||||||
|
|
@ -211,6 +224,9 @@ impl Service for ChanServ {
|
||||||
|
|
||||||
// True if `from` is the channel's founder; otherwise notices why and returns false.
|
// True if `from` is the channel's founder; otherwise notices why and returns false.
|
||||||
fn require_founder(me: &str, from: &Sender, chan: &str, ctx: &mut ServiceCtx, db: &dyn Store) -> bool {
|
fn require_founder(me: &str, from: &Sender, chan: &str, ctx: &mut ServiceCtx, db: &dyn Store) -> bool {
|
||||||
|
if suspended_block(me, from, chan, ctx, db) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
match db.channel(chan) {
|
match db.channel(chan) {
|
||||||
None => {
|
None => {
|
||||||
ctx.notice(me, from.uid, format!("\x02{chan}\x02 isn't registered."));
|
ctx.notice(me, from.uid, format!("\x02{chan}\x02 isn't registered."));
|
||||||
|
|
@ -226,6 +242,9 @@ fn require_founder(me: &str, from: &Sender, chan: &str, ctx: &mut ServiceCtx, db
|
||||||
|
|
||||||
// True if `from` is the founder or an access-list op of `chan`; else notices why.
|
// 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: &dyn Store) -> bool {
|
fn require_op(me: &str, from: &Sender, chan: &str, ctx: &mut ServiceCtx, db: &dyn Store) -> bool {
|
||||||
|
if suspended_block(me, from, chan, ctx, db) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
match (from.account, db.channel(chan)) {
|
match (from.account, db.channel(chan)) {
|
||||||
(_, None) => {
|
(_, None) => {
|
||||||
ctx.notice(me, from.uid, format!("\x02{chan}\x02 isn't registered."));
|
ctx.notice(me, from.uid, format!("\x02{chan}\x02 isn't registered."));
|
||||||
|
|
@ -239,6 +258,15 @@ fn require_op(me: &str, from: &Sender, chan: &str, ctx: &mut ServiceCtx, db: &dy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A suspended channel is frozen: ChanServ won't manage it (returns true + notices).
|
||||||
|
fn suspended_block(me: &str, from: &Sender, chan: &str, ctx: &mut ServiceCtx, db: &dyn Store) -> bool {
|
||||||
|
if db.is_channel_suspended(chan) {
|
||||||
|
ctx.notice(me, from.uid, format!("\x02{chan}\x02 is suspended by network staff and can't be managed right now."));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
// PEACE: returns true (and notices) if `from` may not act against `target_uid`
|
// PEACE: returns true (and notices) if `from` may not act against `target_uid`
|
||||||
// because the channel has PEACE set and the target holds equal-or-higher access.
|
// because the channel has PEACE set and the target holds equal-or-higher access.
|
||||||
fn peace_blocks(me: &str, from: &Sender, chan: &str, target_uid: &str, ctx: &mut ServiceCtx, net: &dyn NetView, db: &dyn Store) -> bool {
|
fn peace_blocks(me: &str, from: &Sender, chan: &str, target_uid: &str, ctx: &mut ServiceCtx, net: &dyn NetView, db: &dyn Store) -> bool {
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,9 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
|
||||||
ctx.notice(me, from.uid, format!("Only \x02{chan}\x02's founder can change its settings."));
|
ctx.notice(me, from.uid, format!("Only \x02{chan}\x02's founder can change its settings."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if super::suspended_block(me, from, chan, ctx, db) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
match args.get(2).map(|s| s.to_ascii_uppercase()).as_deref() {
|
match args.get(2).map(|s| s.to_ascii_uppercase()).as_deref() {
|
||||||
Some("FOUNDER") => {
|
Some("FOUNDER") => {
|
||||||
let Some(&account) = args.get(3) else {
|
let Some(&account) = args.get(3) else {
|
||||||
|
|
|
||||||
53
chanserv/src/suspend.rs
Normal file
53
chanserv/src/suspend.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
use fedserv_api::{parse_duration, NetView, Priv, Sender, ServiceCtx, Store};
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
// SUSPEND <#channel> [+expiry] [reason] / UNSUSPEND <#channel>: freeze or unfreeze
|
||||||
|
// a channel. Its data is kept but it can't be managed while suspended. Oper-only
|
||||||
|
// (Priv::Suspend). `suspending` selects SUSPEND vs UNSUSPEND.
|
||||||
|
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: &dyn NetView, db: &mut dyn Store, suspending: bool) {
|
||||||
|
if !from.privs.has(Priv::Suspend) {
|
||||||
|
ctx.notice(me, from.uid, "Access denied — that command is for services operators.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let Some(&chan) = args.get(1) else {
|
||||||
|
let syntax = if suspending { "Syntax: SUSPEND <#channel> [+expiry] [reason]" } else { "Syntax: UNSUSPEND <#channel>" };
|
||||||
|
ctx.notice(me, from.uid, syntax);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
if db.channel(chan).is_none() {
|
||||||
|
ctx.notice(me, from.uid, format!("\x02{chan}\x02 isn't registered."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if !suspending {
|
||||||
|
match db.unsuspend_channel(chan) {
|
||||||
|
Ok(true) => ctx.notice(me, from.uid, format!("\x02{chan}\x02 is no longer suspended.")),
|
||||||
|
Ok(false) => ctx.notice(me, from.uid, format!("\x02{chan}\x02 isn't suspended.")),
|
||||||
|
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut rest = &args[2..];
|
||||||
|
let expires = rest.first().and_then(|t| t.strip_prefix('+')).and_then(parse_duration).map(|secs| now_unix() + secs);
|
||||||
|
if expires.is_some() {
|
||||||
|
rest = &rest[1..];
|
||||||
|
}
|
||||||
|
let reason = if rest.is_empty() { "This channel has been suspended.".to_string() } else { rest.join(" ") };
|
||||||
|
let by = from.account.unwrap_or(from.nick);
|
||||||
|
match db.suspend_channel(chan, by, &reason, expires) {
|
||||||
|
Ok(()) => {
|
||||||
|
// Clear the channel: kick everyone currently in it.
|
||||||
|
for uid in net.channel_members(chan) {
|
||||||
|
ctx.kick(me, chan, &uid, &reason);
|
||||||
|
}
|
||||||
|
let expiry = if expires.is_some() { " (with expiry)" } else { "" };
|
||||||
|
ctx.notice(me, from.uid, format!("\x02{chan}\x02 is now suspended{expiry}."));
|
||||||
|
}
|
||||||
|
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn now_unix() -> u64 {
|
||||||
|
SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0)
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use fedserv_api::{NetView, Priv, Sender, ServiceCtx, Store};
|
use fedserv_api::{parse_duration, NetView, Priv, Sender, ServiceCtx, Store};
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
// SUSPEND <account> [+expiry] [reason] / UNSUSPEND <account>: freeze or unfreeze
|
// SUSPEND <account> [+expiry] [reason] / UNSUSPEND <account>: freeze or unfreeze
|
||||||
|
|
@ -49,19 +49,6 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse a duration like "30d", "12h", "45m", "90s", or a bare number of seconds.
|
|
||||||
fn parse_duration(s: &str) -> Option<u64> {
|
|
||||||
let (num, mult) = match s.chars().last()? {
|
|
||||||
'd' | 'D' => (&s[..s.len() - 1], 86_400),
|
|
||||||
'h' | 'H' => (&s[..s.len() - 1], 3_600),
|
|
||||||
'm' | 'M' => (&s[..s.len() - 1], 60),
|
|
||||||
's' | 'S' => (&s[..s.len() - 1], 1),
|
|
||||||
c if c.is_ascii_digit() => (s, 1),
|
|
||||||
_ => return None,
|
|
||||||
};
|
|
||||||
num.parse::<u64>().ok().map(|n| n * mult)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn now_unix() -> u64 {
|
fn now_unix() -> u64 {
|
||||||
SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0)
|
SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,8 @@ pub enum Event {
|
||||||
ChannelEntryMsgSet { channel: String, msg: String },
|
ChannelEntryMsgSet { channel: String, msg: String },
|
||||||
ChannelSettingsSet { channel: String, settings: ChanSettings },
|
ChannelSettingsSet { channel: String, settings: ChanSettings },
|
||||||
ChannelTopicSet { channel: String, topic: String },
|
ChannelTopicSet { channel: String, topic: String },
|
||||||
|
ChannelSuspended { channel: String, by: String, reason: String, ts: u64, expires: Option<u64> },
|
||||||
|
ChannelUnsuspended { channel: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
// Whether an event replicates across the federation. Account identity is Global
|
// Whether an event replicates across the federation. Account identity is Global
|
||||||
|
|
@ -128,7 +130,9 @@ impl Event {
|
||||||
| Event::ChannelDescSet { .. }
|
| Event::ChannelDescSet { .. }
|
||||||
| Event::ChannelEntryMsgSet { .. }
|
| Event::ChannelEntryMsgSet { .. }
|
||||||
| Event::ChannelSettingsSet { .. }
|
| Event::ChannelSettingsSet { .. }
|
||||||
| Event::ChannelTopicSet { .. } => Scope::Local,
|
| Event::ChannelTopicSet { .. }
|
||||||
|
| Event::ChannelSuspended { .. }
|
||||||
|
| Event::ChannelUnsuspended { .. } => Scope::Local,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -218,6 +222,9 @@ pub struct ChannelInfo {
|
||||||
// Last known topic, kept for KEEPTOPIC / TOPICLOCK.
|
// Last known topic, kept for KEEPTOPIC / TOPICLOCK.
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub topic: String,
|
pub topic: String,
|
||||||
|
// Services suspension, if any (channel frozen while set and unexpired).
|
||||||
|
#[serde(default)]
|
||||||
|
pub suspension: Option<Suspension>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChannelInfo {
|
impl ChannelInfo {
|
||||||
|
|
@ -627,6 +634,9 @@ impl Db {
|
||||||
if !c.topic.is_empty() {
|
if !c.topic.is_empty() {
|
||||||
snapshot.push(Event::ChannelTopicSet { channel: c.name.clone(), topic: c.topic.clone() });
|
snapshot.push(Event::ChannelTopicSet { channel: c.name.clone(), topic: c.topic.clone() });
|
||||||
}
|
}
|
||||||
|
if let Some(s) = &c.suspension {
|
||||||
|
snapshot.push(Event::ChannelSuspended { channel: c.name.clone(), by: s.by.clone(), reason: s.reason.clone(), ts: s.ts, expires: s.expires });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (nick, account) in &self.grouped {
|
for (nick, account) in &self.grouped {
|
||||||
snapshot.push(Event::NickGrouped { nick: nick.clone(), account: account.clone() });
|
snapshot.push(Event::NickGrouped { nick: nick.clone(), account: account.clone() });
|
||||||
|
|
@ -1053,7 +1063,7 @@ impl Db {
|
||||||
self.log
|
self.log
|
||||||
.append(Event::ChannelRegistered { name: name.to_string(), founder: founder.to_string(), ts })
|
.append(Event::ChannelRegistered { name: name.to_string(), founder: founder.to_string(), ts })
|
||||||
.map_err(|_| ChanError::Internal)?;
|
.map_err(|_| ChanError::Internal)?;
|
||||||
self.channels.insert(k, ChannelInfo { name: name.to_string(), founder: founder.to_string(), ts, lock_on: String::new(), lock_off: String::new(), access: Vec::new(), akick: Vec::new(), desc: String::new(), entrymsg: String::new(), settings: ChanSettings::default(), topic: String::new() });
|
self.channels.insert(k, ChannelInfo { name: name.to_string(), founder: founder.to_string(), ts, lock_on: String::new(), lock_off: String::new(), access: Vec::new(), akick: Vec::new(), desc: String::new(), entrymsg: String::new(), settings: ChanSettings::default(), topic: String::new(), suspension: None });
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1222,6 +1232,41 @@ impl Db {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Suspend a channel. `expires` is absolute unix time (None = permanent).
|
||||||
|
pub fn suspend_channel(&mut self, channel: &str, by: &str, reason: &str, expires: Option<u64>) -> Result<(), ChanError> {
|
||||||
|
let k = key(channel);
|
||||||
|
if !self.channels.contains_key(&k) {
|
||||||
|
return Err(ChanError::NoChannel);
|
||||||
|
}
|
||||||
|
let ts = now();
|
||||||
|
self.log.append(Event::ChannelSuspended { channel: channel.to_string(), by: by.to_string(), reason: reason.to_string(), ts, expires }).map_err(|_| ChanError::Internal)?;
|
||||||
|
self.channels.get_mut(&k).unwrap().suspension = Some(Suspension { by: by.to_string(), reason: reason.to_string(), ts, expires });
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Lift a channel suspension. Returns whether one was set.
|
||||||
|
pub fn unsuspend_channel(&mut self, channel: &str) -> Result<bool, ChanError> {
|
||||||
|
let k = key(channel);
|
||||||
|
match self.channels.get(&k) {
|
||||||
|
None => return Err(ChanError::NoChannel),
|
||||||
|
Some(c) if c.suspension.is_none() => return Ok(false),
|
||||||
|
Some(_) => {}
|
||||||
|
}
|
||||||
|
self.log.append(Event::ChannelUnsuspended { channel: channel.to_string() }).map_err(|_| ChanError::Internal)?;
|
||||||
|
self.channels.get_mut(&k).unwrap().suspension = None;
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether a channel has a set, unexpired suspension (lazy — no timer).
|
||||||
|
pub fn is_channel_suspended(&self, channel: &str) -> bool {
|
||||||
|
self.channels.get(&key(channel)).and_then(|c| c.suspension.as_ref()).is_some_and(|s| s.expires.is_none_or(|e| e > now()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The channel's suspension record, if any.
|
||||||
|
pub fn channel_suspension(&self, channel: &str) -> Option<SuspensionView> {
|
||||||
|
self.channels.get(&key(channel)).and_then(|c| c.suspension.as_ref()).map(|s| SuspensionView { by: s.by.clone(), reason: s.reason.clone(), ts: s.ts, expires: s.expires })
|
||||||
|
}
|
||||||
|
|
||||||
/// Set `channel`'s entry message (empty clears it).
|
/// Set `channel`'s entry message (empty clears it).
|
||||||
pub fn set_entrymsg(&mut self, channel: &str, msg: &str) -> Result<(), ChanError> {
|
pub fn set_entrymsg(&mut self, channel: &str, msg: &str) -> Result<(), ChanError> {
|
||||||
let k = key(channel);
|
let k = key(channel);
|
||||||
|
|
@ -1333,7 +1378,7 @@ fn apply(accounts: &mut HashMap<String, Account>, channels: &mut HashMap<String,
|
||||||
grouped.remove(&key(&nick));
|
grouped.remove(&key(&nick));
|
||||||
}
|
}
|
||||||
Event::ChannelRegistered { name, founder, ts } => {
|
Event::ChannelRegistered { name, founder, ts } => {
|
||||||
channels.insert(key(&name), ChannelInfo { name, founder, ts, lock_on: String::new(), lock_off: String::new(), access: Vec::new(), akick: Vec::new(), desc: String::new(), entrymsg: String::new(), settings: ChanSettings::default(), topic: String::new() });
|
channels.insert(key(&name), ChannelInfo { name, founder, ts, lock_on: String::new(), lock_off: String::new(), access: Vec::new(), akick: Vec::new(), desc: String::new(), entrymsg: String::new(), settings: ChanSettings::default(), topic: String::new(), suspension: None });
|
||||||
}
|
}
|
||||||
Event::ChannelDropped { name } => {
|
Event::ChannelDropped { name } => {
|
||||||
channels.remove(&key(&name));
|
channels.remove(&key(&name));
|
||||||
|
|
@ -1386,6 +1431,16 @@ fn apply(accounts: &mut HashMap<String, Account>, channels: &mut HashMap<String,
|
||||||
c.topic = topic;
|
c.topic = topic;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Event::ChannelSuspended { channel, by, reason, ts, expires } => {
|
||||||
|
if let Some(c) = channels.get_mut(&key(&channel)) {
|
||||||
|
c.suspension = Some(Suspension { by, reason, ts, expires });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::ChannelUnsuspended { channel } => {
|
||||||
|
if let Some(c) = channels.get_mut(&key(&channel)) {
|
||||||
|
c.suspension = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
Event::ChannelEntryMsgSet { channel, msg } => {
|
Event::ChannelEntryMsgSet { channel, msg } => {
|
||||||
if let Some(c) = channels.get_mut(&key(&channel)) {
|
if let Some(c) = channels.get_mut(&key(&channel)) {
|
||||||
c.entrymsg = msg;
|
c.entrymsg = msg;
|
||||||
|
|
@ -1575,6 +1630,18 @@ impl Store for Db {
|
||||||
fn set_channel_topic(&mut self, channel: &str, topic: &str) -> Result<(), ChanError> {
|
fn set_channel_topic(&mut self, channel: &str, topic: &str) -> Result<(), ChanError> {
|
||||||
Db::set_channel_topic(self, channel, topic)
|
Db::set_channel_topic(self, channel, topic)
|
||||||
}
|
}
|
||||||
|
fn suspend_channel(&mut self, channel: &str, by: &str, reason: &str, expires: Option<u64>) -> Result<(), ChanError> {
|
||||||
|
Db::suspend_channel(self, channel, by, reason, expires)
|
||||||
|
}
|
||||||
|
fn unsuspend_channel(&mut self, channel: &str) -> Result<bool, ChanError> {
|
||||||
|
Db::unsuspend_channel(self, channel)
|
||||||
|
}
|
||||||
|
fn is_channel_suspended(&self, channel: &str) -> bool {
|
||||||
|
Db::is_channel_suspended(self, channel)
|
||||||
|
}
|
||||||
|
fn channel_suspension(&self, channel: &str) -> Option<SuspensionView> {
|
||||||
|
Db::channel_suspension(self, channel)
|
||||||
|
}
|
||||||
fn set_entrymsg(&mut self, channel: &str, msg: &str) -> Result<(), ChanError> {
|
fn set_entrymsg(&mut self, channel: &str, msg: &str) -> Result<(), ChanError> {
|
||||||
Db::set_entrymsg(self, channel, msg)
|
Db::set_entrymsg(self, channel, msg)
|
||||||
}
|
}
|
||||||
|
|
@ -1613,6 +1680,7 @@ fn channel_view(c: &ChannelInfo) -> ChannelView {
|
||||||
keeptopic: c.settings.keeptopic,
|
keeptopic: c.settings.keeptopic,
|
||||||
topiclock: c.settings.topiclock,
|
topiclock: c.settings.topiclock,
|
||||||
topic: c.topic.clone(),
|
topic: c.topic.clone(),
|
||||||
|
suspended: c.suspension.as_ref().is_some_and(|s| s.expires.is_none_or(|e| e > now())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1789,6 +1857,23 @@ mod tests {
|
||||||
assert!(Db::open(&p, "N1").is_suspended("alice"), "suspension replays from the log");
|
assert!(Db::open(&p, "N1").is_suspended("alice"), "suspension replays from the log");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A channel suspension lifts, expires lazily, and survives compaction + reopen.
|
||||||
|
#[test]
|
||||||
|
fn channel_suspend_lifts_persists_and_compacts() {
|
||||||
|
let p = tmp("chansuspend");
|
||||||
|
let mut db = Db::open(&p, "N1");
|
||||||
|
db.register_channel("#c", "boss").unwrap();
|
||||||
|
db.suspend_channel("#c", "oper", "raided", Some(1)).unwrap();
|
||||||
|
assert!(!db.is_channel_suspended("#c"), "a past expiry is inactive");
|
||||||
|
db.suspend_channel("#c", "oper", "raided", None).unwrap();
|
||||||
|
assert!(db.is_channel_suspended("#c"));
|
||||||
|
db.compact().unwrap(); // the snapshot must retain the suspension
|
||||||
|
drop(db);
|
||||||
|
let db = Db::open(&p, "N1");
|
||||||
|
assert!(db.is_channel_suspended("#c"), "survives compaction + reopen");
|
||||||
|
assert_eq!(db.channel_suspension("#c").unwrap().by, "oper");
|
||||||
|
}
|
||||||
|
|
||||||
// A wrong code is tolerated a few times, then the code is burned so it can't
|
// A wrong code is tolerated a few times, then the code is burned so it can't
|
||||||
// be ground down online even though it is short.
|
// be ground down online even though it is short.
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
|
|
@ -449,6 +449,10 @@ impl Engine {
|
||||||
// members their status mode, and send the entry message.
|
// members their status mode, and send the entry message.
|
||||||
NetEvent::Join { uid, channel, op } => {
|
NetEvent::Join { uid, channel, op } => {
|
||||||
self.network.channel_join(&channel, &uid, op);
|
self.network.channel_join(&channel, &uid, op);
|
||||||
|
// A suspended channel is frozen: no auto-op, akick, or entry message.
|
||||||
|
if self.db.is_channel_suspended(&channel) {
|
||||||
|
return Vec::new();
|
||||||
|
}
|
||||||
let account = self.network.account_of(&uid).map(str::to_string);
|
let account = self.network.account_of(&uid).map(str::to_string);
|
||||||
let from = self.chan_service.clone().unwrap_or_default();
|
let from = self.chan_service.clone().unwrap_or_default();
|
||||||
let mode = account.as_deref().and_then(|a| self.db.channel(&channel).and_then(|c| c.join_mode(a)));
|
let mode = account.as_deref().and_then(|a| self.db.channel(&channel).and_then(|c| c.join_mode(a)));
|
||||||
|
|
@ -1553,6 +1557,48 @@ mod tests {
|
||||||
assert!(out.is_empty(), "no enforcement when SECUREOPS is off: {out:?}");
|
assert!(out.is_empty(), "no enforcement when SECUREOPS is off: {out:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Channel SUSPEND is oper-gated and freezes founder management until lifted.
|
||||||
|
#[test]
|
||||||
|
fn channel_suspend_is_oper_gated_and_freezes_management() {
|
||||||
|
use fedserv_chanserv::ChanServ;
|
||||||
|
use fedserv_nickserv::NickServ;
|
||||||
|
let path = std::env::temp_dir().join("fedserv-chansuspendcmd.jsonl");
|
||||||
|
let _ = std::fs::remove_file(&path);
|
||||||
|
let mut db = Db::open(&path, "42S");
|
||||||
|
db.scram_iterations = 4096;
|
||||||
|
db.register("boss", "password1", None).unwrap();
|
||||||
|
db.register("staff", "password1", None).unwrap();
|
||||||
|
db.register_channel("#c", "boss").unwrap();
|
||||||
|
let mut e = Engine::new(
|
||||||
|
vec![
|
||||||
|
Box::new(NickServ { uid: "42SAAAAAA".into(), guest_nick: "Guest".into(), guest_seq: 0 }),
|
||||||
|
Box::new(ChanServ { uid: "42SAAAAAB".into() }),
|
||||||
|
],
|
||||||
|
db,
|
||||||
|
);
|
||||||
|
let mut opers = std::collections::HashMap::new();
|
||||||
|
opers.insert("staff".to_string(), Privs::default().with(fedserv_api::Priv::Suspend));
|
||||||
|
e.set_opers(opers);
|
||||||
|
let ns = |e: &mut Engine, uid: &str, t: &str| e.handle(NetEvent::Privmsg { from: uid.into(), to: "42SAAAAAA".into(), text: t.into() });
|
||||||
|
let cs = |e: &mut Engine, uid: &str, t: &str| e.handle(NetEvent::Privmsg { from: uid.into(), to: "42SAAAAAB".into(), text: t.into() });
|
||||||
|
let notice = |out: &[NetAction], n: &str| out.iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains(n)));
|
||||||
|
|
||||||
|
e.handle(NetEvent::UserConnect { uid: "000AAAAAB".into(), nick: "boss".into(), host: "h".into() });
|
||||||
|
e.handle(NetEvent::UserConnect { uid: "000AAAAAC".into(), nick: "staff".into(), host: "h".into() });
|
||||||
|
ns(&mut e, "000AAAAAB", "IDENTIFY password1");
|
||||||
|
ns(&mut e, "000AAAAAC", "IDENTIFY password1");
|
||||||
|
|
||||||
|
// A non-oper cannot suspend a channel.
|
||||||
|
assert!(notice(&cs(&mut e, "000AAAAAB", "SUSPEND #c"), "Access denied"));
|
||||||
|
// The oper suspends it.
|
||||||
|
assert!(notice(&cs(&mut e, "000AAAAAC", "SUSPEND #c raided"), "now suspended"));
|
||||||
|
// The founder can no longer manage it.
|
||||||
|
assert!(notice(&cs(&mut e, "000AAAAAB", "SET #c SIGNKICK ON"), "suspended"));
|
||||||
|
// UNSUSPEND restores management.
|
||||||
|
assert!(notice(&cs(&mut e, "000AAAAAC", "UNSUSPEND #c"), "no longer suspended"));
|
||||||
|
assert!(notice(&cs(&mut e, "000AAAAAB", "SET #c SIGNKICK ON"), "is now"));
|
||||||
|
}
|
||||||
|
|
||||||
// SUSPEND is oper-gated, blocks the victim's login, and UNSUSPEND restores it.
|
// SUSPEND is oper-gated, blocks the victim's login, and UNSUSPEND restores it.
|
||||||
#[test]
|
#[test]
|
||||||
fn suspend_blocks_login_and_needs_oper() {
|
fn suspend_blocks_login_and_needs_oper() {
|
||||||
|
|
|
||||||
|
|
@ -133,7 +133,9 @@ fn to_wire(entry: &LogEntry) -> Option<ReplicationEvent> {
|
||||||
| Event::ChannelAkickDel { .. }
|
| Event::ChannelAkickDel { .. }
|
||||||
| Event::ChannelEntryMsgSet { .. }
|
| Event::ChannelEntryMsgSet { .. }
|
||||||
| Event::ChannelSettingsSet { .. }
|
| Event::ChannelSettingsSet { .. }
|
||||||
| Event::ChannelTopicSet { .. } => return None,
|
| Event::ChannelTopicSet { .. }
|
||||||
|
| Event::ChannelSuspended { .. }
|
||||||
|
| Event::ChannelUnsuspended { .. } => return None,
|
||||||
};
|
};
|
||||||
Some(ReplicationEvent { origin: entry.origin().to_string(), seq: entry.seq(), lamport: entry.lamport(), kind: Some(kind) })
|
Some(ReplicationEvent { origin: entry.origin().to_string(), seq: entry.seq(), lamport: entry.lamport(), kind: Some(kind) })
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue