BotServ: add SAY and ACT
The channel's assigned bot can now speak: SAY <#channel> <text> and ACT <#channel> <text> (CTCP ACTION), gated on channel-operator access. Bots are tracked in the network view so uid_by_nick resolves them, and the message is sourced from the bot's own uid via a new ServiceCtx::privmsg.
This commit is contained in:
parent
6a37d010b8
commit
c7d05a8b77
5 changed files with 129 additions and 4 deletions
|
|
@ -10,6 +10,8 @@ mod bot;
|
|||
mod assign;
|
||||
#[path = "info.rs"]
|
||||
mod info;
|
||||
#[path = "say.rs"]
|
||||
mod say;
|
||||
|
||||
pub struct BotServ {
|
||||
pub uid: String,
|
||||
|
|
@ -26,14 +28,16 @@ impl Service for BotServ {
|
|||
"Bot Services"
|
||||
}
|
||||
|
||||
fn on_command(&mut self, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, _net: &dyn NetView, db: &mut dyn Store) {
|
||||
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::handle(me, from, args, ctx, db),
|
||||
Some("ASSIGN") => assign::handle(me, from, args, ctx, db, true),
|
||||
Some("UNASSIGN") => assign::handle(me, from, args, ctx, db, false),
|
||||
Some("INFO") => info::handle(me, from, args, ctx, db),
|
||||
Some("HELP") | None => ctx.notice(me, from.uid, "BotServ keeps service bots for your channels: \x02ASSIGN\x02 <#channel> <bot> puts a bot in your channel, \x02UNASSIGN\x02 <#channel> removes it, \x02INFO\x02 <bot|#channel> shows details. Operators also have \x02BOT\x02 ADD|DEL|LIST."),
|
||||
Some("SAY") => say::handle(me, from, args, ctx, net, db, false),
|
||||
Some("ACT") => say::handle(me, from, args, ctx, net, db, true),
|
||||
Some("HELP") | None => ctx.notice(me, from.uid, "BotServ keeps service bots for your channels: \x02ASSIGN\x02 <#channel> <bot> puts a bot in your channel, \x02UNASSIGN\x02 <#channel> removes it, \x02INFO\x02 <bot|#channel> shows details, \x02SAY\x02/\x02ACT\x02 <#channel> <text> speaks through the bot. Operators also have \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.")),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
35
botserv/src/say.rs
Normal file
35
botserv/src/say.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
use fedserv_api::{NetView, Priv, Sender, ServiceCtx, Store};
|
||||
|
||||
// SAY <#channel> <text> — make the channel's assigned bot say something.
|
||||
// ACT <#channel> <text> — the same, as a CTCP ACTION (/me).
|
||||
// Requires channel-operator access (founder, op, or a services admin).
|
||||
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: &dyn NetView, db: &dyn Store, action: bool) {
|
||||
let verb = if action { "ACT" } else { "SAY" };
|
||||
if args.len() < 3 {
|
||||
ctx.notice(me, from.uid, format!("Syntax: {verb} <#channel> <text>"));
|
||||
return;
|
||||
}
|
||||
let chan = args[1];
|
||||
let text = args[2..].join(" ");
|
||||
|
||||
let Some(info) = db.channel(chan) else {
|
||||
ctx.notice(me, from.uid, format!("\x02{chan}\x02 isn't registered."));
|
||||
return;
|
||||
};
|
||||
let allowed = from.privs.has(Priv::Admin) || from.account.is_some_and(|a| info.is_op(a));
|
||||
if !allowed {
|
||||
ctx.notice(me, from.uid, format!("Access denied — you need operator access in \x02{chan}\x02."));
|
||||
return;
|
||||
}
|
||||
let Some(bot) = info.assigned_bot else {
|
||||
ctx.notice(me, from.uid, format!("\x02{chan}\x02 has no bot assigned. Assign one with \x02ASSIGN\x02 {chan} <bot>."));
|
||||
return;
|
||||
};
|
||||
let Some(botuid) = net.uid_by_nick(&bot) else {
|
||||
ctx.notice(me, from.uid, format!("Bot \x02{bot}\x02 isn't on the network right now."));
|
||||
return;
|
||||
};
|
||||
let botuid = botuid.to_string();
|
||||
let payload = if action { format!("\x01ACTION {text}\x01") } else { text };
|
||||
ctx.privmsg(&botuid, chan, payload);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue