botserv: bot registry + BOT ADD/DEL/LIST (first slice)
Adds BotServ as a new module crate (fedserv-botserv) and a persisted bot
registry: a typed Bot{nick,user,host,gecos} threaded through the event log
(new bots map folded in apply, snapshotted, Local scope). BOT ADD/DEL/LIST
gated on the typed Priv::Admin. BotServ is a default service (uid suffix
AAAAAD). Next slices: introduce bots on the network, ASSIGN to channels, and
in-channel fantasy commands. Data + oper-gated engine tests.
This commit is contained in:
parent
cb081a2e95
commit
651cb683de
10 changed files with 244 additions and 12 deletions
8
botserv/Cargo.toml
Normal file
8
botserv/Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "fedserv-botserv"
|
||||
version = "0.0.1"
|
||||
edition = "2021"
|
||||
description = "BotServ: registers and manages service bots that sit in channels."
|
||||
|
||||
[dependencies]
|
||||
fedserv-api = { path = "../api" }
|
||||
76
botserv/src/lib.rs
Normal file
76
botserv/src/lib.rs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
//! BotServ registers service bots — pseudo-clients that (in later slices) get
|
||||
//! assigned to channels and run fantasy commands. This first slice is the bot
|
||||
//! registry: BOT ADD/DEL/LIST, gated on the typed Priv::Admin.
|
||||
|
||||
use fedserv_api::{NetView, Priv, Sender, Service, ServiceCtx, Store};
|
||||
|
||||
pub struct BotServ {
|
||||
pub uid: String,
|
||||
}
|
||||
|
||||
impl Service for BotServ {
|
||||
fn nick(&self) -> &str {
|
||||
"BotServ"
|
||||
}
|
||||
fn uid(&self) -> &str {
|
||||
&self.uid
|
||||
}
|
||||
fn gecos(&self) -> &str {
|
||||
"Bot Services"
|
||||
}
|
||||
|
||||
fn on_command(&mut self, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, _net: &dyn NetView, db: &mut dyn Store) {
|
||||
let me = self.uid.as_str();
|
||||
match args.first().map(|s| s.to_ascii_uppercase()).as_deref() {
|
||||
Some("BOT") => bot(me, from, args, ctx, db),
|
||||
Some("HELP") => ctx.notice(me, from.uid, "BotServ keeps service bots for your channels. Operator commands: \x02BOT\x02 ADD|DEL|LIST."),
|
||||
Some(other) => ctx.notice(me, from.uid, format!("I don't know the command \x02{other}\x02. Try \x02HELP\x02.")),
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BOT ADD <nick> <user> <host> [gecos] | BOT DEL <nick> | BOT LIST — manage the
|
||||
// bot registry. Administering bots is oper-only (Priv::Admin).
|
||||
fn bot(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) {
|
||||
if !from.privs.has(Priv::Admin) {
|
||||
ctx.notice(me, from.uid, "Access denied — managing bots is for services operators.");
|
||||
return;
|
||||
}
|
||||
match args.get(1).map(|s| s.to_ascii_uppercase()).as_deref() {
|
||||
Some("ADD") => {
|
||||
let (Some(&nick), Some(&user), Some(&host)) = (args.get(2), args.get(3), args.get(4)) else {
|
||||
ctx.notice(me, from.uid, "Syntax: BOT ADD <nick> <user> <host> [gecos]");
|
||||
return;
|
||||
};
|
||||
let gecos = if args.len() > 5 { args[5..].join(" ") } else { "Service Bot".to_string() };
|
||||
match db.bot_add(nick, user, host, &gecos) {
|
||||
Ok(()) => ctx.notice(me, from.uid, format!("Bot \x02{nick}\x02 (\x02{user}@{host}\x02) added.")),
|
||||
Err(_) => ctx.notice(me, from.uid, format!("A bot named \x02{nick}\x02 already exists, or that didn't work.")),
|
||||
}
|
||||
}
|
||||
Some("DEL") => {
|
||||
let Some(&nick) = args.get(2) else {
|
||||
ctx.notice(me, from.uid, "Syntax: BOT DEL <nick>");
|
||||
return;
|
||||
};
|
||||
match db.bot_del(nick) {
|
||||
Ok(true) => ctx.notice(me, from.uid, format!("Bot \x02{nick}\x02 deleted.")),
|
||||
Ok(false) => ctx.notice(me, from.uid, format!("There's no bot named \x02{nick}\x02.")),
|
||||
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
|
||||
}
|
||||
}
|
||||
None | Some("LIST") => {
|
||||
let bots = db.bots();
|
||||
if bots.is_empty() {
|
||||
ctx.notice(me, from.uid, "No bots have been added yet. Add one with \x02BOT ADD\x02.");
|
||||
return;
|
||||
}
|
||||
ctx.notice(me, from.uid, format!("Bots ({}):", bots.len()));
|
||||
for b in &bots {
|
||||
ctx.notice(me, from.uid, format!(" \x02{}\x02 ({}@{}) — {}", b.nick, b.user, b.host, b.gecos));
|
||||
}
|
||||
}
|
||||
Some(other) => ctx.notice(me, from.uid, format!("Unknown BOT command \x02{other}\x02. Use \x02ADD\x02, \x02DEL\x02 or \x02LIST\x02.")),
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue