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:
Jean Chevronnet 2026-07-13 04:17:17 +00:00
parent bbbe2c6cb8
commit cb081a2e95
No known key found for this signature in database
8 changed files with 241 additions and 18 deletions

View file

@ -89,6 +89,8 @@ pub enum Event {
ChannelEntryMsgSet { channel: String, msg: String },
ChannelSettingsSet { channel: String, settings: ChanSettings },
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
@ -128,7 +130,9 @@ impl Event {
| Event::ChannelDescSet { .. }
| Event::ChannelEntryMsgSet { .. }
| 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.
#[serde(default)]
pub topic: String,
// Services suspension, if any (channel frozen while set and unexpired).
#[serde(default)]
pub suspension: Option<Suspension>,
}
impl ChannelInfo {
@ -627,6 +634,9 @@ impl Db {
if !c.topic.is_empty() {
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 {
snapshot.push(Event::NickGrouped { nick: nick.clone(), account: account.clone() });
@ -1053,7 +1063,7 @@ impl Db {
self.log
.append(Event::ChannelRegistered { name: name.to_string(), founder: founder.to_string(), ts })
.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(())
}
@ -1222,6 +1232,41 @@ impl Db {
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).
pub fn set_entrymsg(&mut self, channel: &str, msg: &str) -> Result<(), ChanError> {
let k = key(channel);
@ -1333,7 +1378,7 @@ fn apply(accounts: &mut HashMap<String, Account>, channels: &mut HashMap<String,
grouped.remove(&key(&nick));
}
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 } => {
channels.remove(&key(&name));
@ -1386,6 +1431,16 @@ fn apply(accounts: &mut HashMap<String, Account>, channels: &mut HashMap<String,
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 } => {
if let Some(c) = channels.get_mut(&key(&channel)) {
c.entrymsg = msg;
@ -1575,6 +1630,18 @@ impl Store for Db {
fn set_channel_topic(&mut self, channel: &str, topic: &str) -> Result<(), ChanError> {
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> {
Db::set_entrymsg(self, channel, msg)
}
@ -1613,6 +1680,7 @@ fn channel_view(c: &ChannelInfo) -> ChannelView {
keeptopic: c.settings.keeptopic,
topiclock: c.settings.topiclock,
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");
}
// 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
// be ground down online even though it is short.
#[test]

View file

@ -449,6 +449,10 @@ impl Engine {
// members their status mode, and send the entry message.
NetEvent::Join { uid, channel, 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 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)));
@ -1553,6 +1557,48 @@ mod tests {
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.
#[test]
fn suspend_blocks_login_and_needs_oper() {

View file

@ -133,7 +133,9 @@ fn to_wire(entry: &LogEntry) -> Option<ReplicationEvent> {
| Event::ChannelAkickDel { .. }
| Event::ChannelEntryMsgSet { .. }
| 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) })
}