BotServ GREET + NickServ SET GREET
Members set a personal greet with NickServ SET GREET; a channel founder enables display with BotServ SET <#channel> GREET ON. On join, if greets are enabled and the member has channel access and a non-empty greet, the assigned bot shows [account] greet in the channel. Per-account greet is a typed field on Account (AccountGreetSet event, rides the account snapshot); the per-channel toggle is a new ChanSetting::BotGreet bool. Greet is public in NickServ INFO.
This commit is contained in:
parent
801bfc5c96
commit
ad8041bd9d
8 changed files with 172 additions and 5 deletions
|
|
@ -307,6 +307,9 @@ pub struct AccountView {
|
|||
pub email: Option<String>,
|
||||
pub ts: u64,
|
||||
pub verified: bool,
|
||||
// Personal greet shown by a bot when this account joins a greet-enabled
|
||||
// channel (empty = none).
|
||||
pub greet: String,
|
||||
}
|
||||
|
||||
// One channel access-list entry (account -> level, e.g. "op" / "voice").
|
||||
|
|
@ -383,6 +386,8 @@ pub struct ChannelView {
|
|||
pub topic: String,
|
||||
// BotServ bot assigned to this channel, if any.
|
||||
pub assigned_bot: Option<String>,
|
||||
// BotServ: whether members' personal greets are shown on join.
|
||||
pub bot_greet: bool,
|
||||
}
|
||||
|
||||
// A single ChanServ SET option, named for the typed `set_channel_setting` call.
|
||||
|
|
@ -394,6 +399,8 @@ pub enum ChanSetting {
|
|||
SecureOps,
|
||||
KeepTopic,
|
||||
TopicLock,
|
||||
// BotServ: show members' personal greets on join.
|
||||
BotGreet,
|
||||
}
|
||||
|
||||
impl ChannelView {
|
||||
|
|
@ -507,6 +514,7 @@ pub trait Store {
|
|||
fn note_auth(&mut self, account: &str, success: bool);
|
||||
fn verify_account(&mut self, account: &str) -> Result<(), RegError>;
|
||||
fn set_email(&mut self, account: &str, email: Option<String>) -> Result<(), RegError>;
|
||||
fn set_greet(&mut self, account: &str, greet: &str) -> Result<(), RegError>;
|
||||
fn group_nick(&mut self, nick: &str, account: &str) -> Result<(), RegError>;
|
||||
fn ungroup_nick(&mut self, nick: &str) -> Result<bool, RegError>;
|
||||
fn drop_account(&mut self, account: &str) -> Result<bool, RegError>;
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ mod assign;
|
|||
mod info;
|
||||
#[path = "say.rs"]
|
||||
mod say;
|
||||
#[path = "set.rs"]
|
||||
mod set;
|
||||
|
||||
pub struct BotServ {
|
||||
pub uid: String,
|
||||
|
|
@ -37,7 +39,8 @@ impl Service for BotServ {
|
|||
Some("INFO") => info::handle(me, from, args, ctx, db),
|
||||
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("SET") => set::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, \x02SAY\x02/\x02ACT\x02 <#channel> <text> speaks through the bot, \x02SET\x02 <#channel> GREET <on|off> toggles greets. 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.")),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
34
botserv/src/set.rs
Normal file
34
botserv/src/set.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use fedserv_api::{ChanSetting, Priv, Sender, ServiceCtx, Store};
|
||||
|
||||
// SET <#channel> <option> <on|off>: per-channel bot options. Founder-or-admin.
|
||||
// Currently: GREET (show members' personal greets when they join).
|
||||
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) {
|
||||
let (Some(&chan), Some(option)) = (args.get(1), args.get(2)) else {
|
||||
ctx.notice(me, from.uid, "Syntax: SET <#channel> GREET <ON|OFF>");
|
||||
return;
|
||||
};
|
||||
let Some(founder) = db.channel(chan).map(|c| c.founder) else {
|
||||
ctx.notice(me, from.uid, format!("\x02{chan}\x02 isn't registered."));
|
||||
return;
|
||||
};
|
||||
if from.account != Some(founder.as_str()) && !from.privs.has(Priv::Admin) {
|
||||
ctx.notice(me, from.uid, format!("Only \x02{chan}\x02's founder can change its bot options."));
|
||||
return;
|
||||
}
|
||||
let on = match args.get(3).map(|s| s.to_ascii_uppercase()).as_deref() {
|
||||
Some("ON") | Some("TRUE") => true,
|
||||
Some("OFF") | Some("FALSE") => false,
|
||||
_ => {
|
||||
ctx.notice(me, from.uid, "Syntax: SET <#channel> GREET <ON|OFF>");
|
||||
return;
|
||||
}
|
||||
};
|
||||
match option.to_ascii_uppercase().as_str() {
|
||||
"GREET" => match db.set_channel_setting(chan, ChanSetting::BotGreet, on) {
|
||||
Ok(()) if on => ctx.notice(me, from.uid, format!("Greet messages are now \x02on\x02 in \x02{chan}\x02.")),
|
||||
Ok(()) => ctx.notice(me, from.uid, format!("Greet messages are now \x02off\x02 in \x02{chan}\x02.")),
|
||||
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
|
||||
},
|
||||
other => ctx.notice(me, from.uid, format!("Unknown option \x02{other}\x02. Available: \x02GREET\x02.")),
|
||||
}
|
||||
}
|
||||
|
|
@ -12,6 +12,10 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
|
|||
};
|
||||
ctx.notice(me, from.uid, format!("Information for \x02{}\x02:", acct.name));
|
||||
ctx.notice(me, from.uid, format!(" Registered : {}", human_time(acct.ts)));
|
||||
// A greet is public — the bot shows it in-channel to everyone anyway.
|
||||
if !acct.greet.is_empty() {
|
||||
ctx.notice(me, from.uid, format!(" Greet : {}", acct.greet));
|
||||
}
|
||||
let is_owner = from.account == Some(acct.name.as_str());
|
||||
if is_owner || from.privs.has(Priv::Auspex) {
|
||||
if let Some(s) = db.suspension(&acct.name) {
|
||||
|
|
|
|||
|
|
@ -29,6 +29,16 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
|
|||
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
|
||||
}
|
||||
}
|
||||
_ => ctx.notice(me, from.uid, "Syntax: SET PASSWORD <newpassword> | SET EMAIL [address]"),
|
||||
Some("GREET") => {
|
||||
// A bot shows this when you join a greet-enabled channel; no arg clears it.
|
||||
let greet = if args.len() > 2 { args[2..].join(" ") } else { String::new() };
|
||||
let cleared = greet.is_empty();
|
||||
match db.set_greet(account, &greet) {
|
||||
Ok(()) if cleared => ctx.notice(me, from.uid, "Your greet has been cleared."),
|
||||
Ok(()) => ctx.notice(me, from.uid, format!("Your greet is now: {greet}")),
|
||||
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
|
||||
}
|
||||
}
|
||||
_ => ctx.notice(me, from.uid, "Syntax: SET PASSWORD <newpassword> | SET EMAIL [address] | SET GREET [message]"),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,6 +55,9 @@ pub struct Account {
|
|||
// Memos left for this account (MemoServ), oldest first.
|
||||
#[serde(default)]
|
||||
pub memos: Vec<Memo>,
|
||||
// Personal greet a bot shows when this account joins a greet-enabled channel.
|
||||
#[serde(default)]
|
||||
pub greet: String,
|
||||
}
|
||||
|
||||
fn verified_default() -> bool {
|
||||
|
|
@ -71,6 +74,7 @@ pub enum Event {
|
|||
CertAdded { account: String, fp: String },
|
||||
CertRemoved { account: String, fp: String },
|
||||
AccountEmailSet { account: String, email: Option<String> },
|
||||
AccountGreetSet { account: String, greet: String },
|
||||
AccountPasswordSet { account: String, password_hash: String, scram256: String, scram512: String },
|
||||
AccountDropped { account: String },
|
||||
AccountVerified { account: String },
|
||||
|
|
@ -120,6 +124,7 @@ impl Event {
|
|||
| Event::CertAdded { .. }
|
||||
| Event::CertRemoved { .. }
|
||||
| Event::AccountEmailSet { .. }
|
||||
| Event::AccountGreetSet { .. }
|
||||
| Event::AccountPasswordSet { .. }
|
||||
| Event::AccountDropped { .. }
|
||||
| Event::AccountVerified { .. }
|
||||
|
|
@ -229,6 +234,9 @@ pub struct ChanSettings {
|
|||
// Revert topic changes made by users without op-level access.
|
||||
#[serde(default)]
|
||||
pub topiclock: bool,
|
||||
// BotServ: show members' personal greets when they join.
|
||||
#[serde(default)]
|
||||
pub bot_greet: bool,
|
||||
}
|
||||
|
||||
// A registered channel and who owns it.
|
||||
|
|
@ -792,6 +800,7 @@ impl Db {
|
|||
ajoin: Vec::new(),
|
||||
suspension: None,
|
||||
memos: Vec::new(),
|
||||
greet: String::new(),
|
||||
};
|
||||
self.log.append(Event::AccountRegistered(account.clone())).map_err(|_| RegError::Internal)?;
|
||||
self.accounts.insert(key(name), account);
|
||||
|
|
@ -961,6 +970,17 @@ impl Db {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Set (or clear, when empty) `account`'s personal greet.
|
||||
pub fn set_greet(&mut self, account: &str, greet: &str) -> Result<(), RegError> {
|
||||
let k = key(account);
|
||||
if !self.accounts.contains_key(&k) {
|
||||
return Err(RegError::Internal);
|
||||
}
|
||||
self.log.append(Event::AccountGreetSet { account: account.to_string(), greet: greet.to_string() }).map_err(|_| RegError::Internal)?;
|
||||
self.accounts.get_mut(&k).unwrap().greet = greet.to_string();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Replace `account`'s password with freshly derived credentials.
|
||||
pub fn set_credentials(&mut self, account: &str, creds: Credentials) -> Result<(), RegError> {
|
||||
let k = key(account);
|
||||
|
|
@ -1260,6 +1280,7 @@ impl Db {
|
|||
ChanSetting::SecureOps => settings.secureops = on,
|
||||
ChanSetting::KeepTopic => settings.keeptopic = on,
|
||||
ChanSetting::TopicLock => settings.topiclock = on,
|
||||
ChanSetting::BotGreet => settings.bot_greet = on,
|
||||
}
|
||||
self.log
|
||||
.append(Event::ChannelSettingsSet { channel: channel.to_string(), settings })
|
||||
|
|
@ -1486,6 +1507,11 @@ fn apply(accounts: &mut HashMap<String, Account>, channels: &mut HashMap<String,
|
|||
a.email = email;
|
||||
}
|
||||
}
|
||||
Event::AccountGreetSet { account, greet } => {
|
||||
if let Some(a) = accounts.get_mut(&key(&account)) {
|
||||
a.greet = greet;
|
||||
}
|
||||
}
|
||||
Event::AccountPasswordSet { account, password_hash, scram256, scram512 } => {
|
||||
if let Some(a) = accounts.get_mut(&key(&account)) {
|
||||
a.password_hash = password_hash;
|
||||
|
|
@ -1703,6 +1729,7 @@ impl Store for Db {
|
|||
email: a.email.clone(),
|
||||
ts: a.ts,
|
||||
verified: a.verified,
|
||||
greet: a.greet.clone(),
|
||||
})
|
||||
}
|
||||
fn resolve_account(&self, name: &str) -> Option<&str> {
|
||||
|
|
@ -1759,6 +1786,9 @@ impl Store for Db {
|
|||
fn set_email(&mut self, account: &str, email: Option<String>) -> Result<(), RegError> {
|
||||
Db::set_email(self, account, email)
|
||||
}
|
||||
fn set_greet(&mut self, account: &str, greet: &str) -> Result<(), RegError> {
|
||||
Db::set_greet(self, account, greet)
|
||||
}
|
||||
fn group_nick(&mut self, nick: &str, account: &str) -> Result<(), RegError> {
|
||||
Db::group_nick(self, nick, account)
|
||||
}
|
||||
|
|
@ -1898,6 +1928,7 @@ fn channel_view(c: &ChannelInfo) -> ChannelView {
|
|||
topic: c.topic.clone(),
|
||||
suspended: c.suspension.as_ref().is_some_and(|s| s.expires.is_none_or(|e| e > now())),
|
||||
assigned_bot: c.assigned_bot.clone(),
|
||||
bot_greet: c.settings.bot_greet,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1935,7 +1966,7 @@ mod tests {
|
|||
fn account_conflict_resolves_deterministically() {
|
||||
let alice = |hash: &str, ts: u64, home: &str| Account {
|
||||
name: "alice".into(), password_hash: hash.into(), email: None,
|
||||
ts, home: home.into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![],
|
||||
ts, home: home.into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(),
|
||||
};
|
||||
let converge = |first: &Account, second: &Account| {
|
||||
let (mut acc, mut ch, mut gr, mut bo) = (HashMap::new(), HashMap::new(), HashMap::new(), HashMap::new());
|
||||
|
|
@ -2211,7 +2242,7 @@ mod tests {
|
|||
db.register("alice", "pw", None).unwrap();
|
||||
let bob = Account {
|
||||
name: "bob".into(), password_hash: "x".into(), email: None,
|
||||
ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![],
|
||||
ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(),
|
||||
};
|
||||
let entry = LogEntry { origin: "peer".into(), seq: 0, lamport: 1, event: Event::AccountRegistered(bob) };
|
||||
db.ingest(entry).unwrap();
|
||||
|
|
|
|||
|
|
@ -529,6 +529,8 @@ impl Engine {
|
|||
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 entrymsg = self.db.channel(&channel).map(|c| c.entrymsg.clone()).filter(|m| !m.is_empty());
|
||||
// Computed before the match below moves `channel` into its actions.
|
||||
let greet = self.greet_on_join(&channel, account.as_deref());
|
||||
let mut out = Vec::new();
|
||||
// SECUREOPS: a user who arrives opped (FJOIN prefix) but lacks op-level
|
||||
// access loses it, unless we're about to grant it to them anyway.
|
||||
|
|
@ -566,6 +568,11 @@ impl Engine {
|
|||
}
|
||||
}
|
||||
}
|
||||
// GREET: if the channel shows greets and this member has channel
|
||||
// access + a personal greet, the assigned bot displays it.
|
||||
if let Some(g) = greet {
|
||||
out.push(g);
|
||||
}
|
||||
out
|
||||
}
|
||||
NetEvent::Part { uid, channel } => {
|
||||
|
|
@ -872,6 +879,24 @@ impl Engine {
|
|||
out
|
||||
}
|
||||
|
||||
// The greet a channel's bot shows when a member logged into `account` joins:
|
||||
// only when greets are enabled for the channel, the member holds channel
|
||||
// access, and has set a non-empty personal greet.
|
||||
fn greet_on_join(&self, channel: &str, account: Option<&str>) -> Option<NetAction> {
|
||||
let acc = account?;
|
||||
let c = self.db.channel(channel)?;
|
||||
if !c.settings.bot_greet || c.join_mode(acc).is_none() {
|
||||
return None;
|
||||
}
|
||||
let bot = c.assigned_bot.clone()?;
|
||||
let greet = self.db.account(acc)?.greet.clone();
|
||||
if greet.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let botuid = self.network.uid_by_nick(&bot)?.to_string();
|
||||
Some(NetAction::Privmsg { from: botuid, to: channel.to_string(), text: format!("[{acc}] {greet}") })
|
||||
}
|
||||
|
||||
// Fantasy commands: `!op nick`, `!kick nick`, `!topic …` spoken in a channel
|
||||
// that has a bot assigned. We reuse ChanServ's real command handlers — the
|
||||
// fantasy word becomes the command and the channel is injected as its first
|
||||
|
|
@ -1442,7 +1467,7 @@ mod tests {
|
|||
// An earlier claim from another node wins and takes the name over.
|
||||
let winner = db::Account {
|
||||
name: "alice".into(), password_hash: "OTHER".into(), email: None,
|
||||
ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![],
|
||||
ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(),
|
||||
};
|
||||
let entry = LogEntry::for_test("peer", 0, 1, db::Event::AccountRegistered(winner));
|
||||
e.gossip_ingest(entry).unwrap();
|
||||
|
|
@ -1854,6 +1879,56 @@ mod tests {
|
|||
assert!(out.iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains("Access denied"))), "denied notice: {out:?}");
|
||||
}
|
||||
|
||||
// A member's personal greet is shown by the assigned bot on join, once the
|
||||
// channel enables greets and the member has access.
|
||||
#[test]
|
||||
fn botserv_greet_shown_on_join() {
|
||||
use fedserv_botserv::BotServ;
|
||||
use fedserv_chanserv::ChanServ;
|
||||
use fedserv_nickserv::NickServ;
|
||||
let path = std::env::temp_dir().join("fedserv-bsgreet.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_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() }),
|
||||
Box::new(BotServ { uid: "42SAAAAAD".into() }),
|
||||
],
|
||||
db,
|
||||
);
|
||||
e.set_sid("42S".into());
|
||||
let mut opers = std::collections::HashMap::new();
|
||||
opers.insert("boss".to_string(), Privs::default().with(fedserv_api::Priv::Admin));
|
||||
e.set_opers(opers);
|
||||
e.chan_service = Some("42SAAAAAB".into());
|
||||
let ns = |e: &mut Engine, uid: &str, t: &str| e.handle(NetEvent::Privmsg { from: uid.into(), to: "42SAAAAAA".into(), text: t.into() });
|
||||
let bs = |e: &mut Engine, uid: &str, t: &str| e.handle(NetEvent::Privmsg { from: uid.into(), to: "42SAAAAAD".into(), text: t.into() });
|
||||
|
||||
e.handle(NetEvent::UserConnect { uid: "000AAAAAB".into(), nick: "boss".into(), host: "h".into() });
|
||||
ns(&mut e, "000AAAAAB", "IDENTIFY password1");
|
||||
ns(&mut e, "000AAAAAB", "SET GREET hello all");
|
||||
bs(&mut e, "000AAAAAB", "BOT ADD Bendy bot serv.host Helper");
|
||||
bs(&mut e, "000AAAAAB", "ASSIGN #c Bendy");
|
||||
|
||||
// Greets off by default: joining is silent.
|
||||
assert!(
|
||||
!e.handle(NetEvent::Join { uid: "000AAAAAB".into(), channel: "#c".into(), op: true }).iter().any(|a| matches!(a, NetAction::Privmsg { .. })),
|
||||
"no greet before it is enabled"
|
||||
);
|
||||
e.handle(NetEvent::Part { uid: "000AAAAAB".into(), channel: "#c".into() });
|
||||
bs(&mut e, "000AAAAAB", "SET #c GREET ON");
|
||||
// Now the founder's greet is shown by the bot.
|
||||
let out = e.handle(NetEvent::Join { uid: "000AAAAAB".into(), channel: "#c".into(), op: true });
|
||||
assert!(
|
||||
out.iter().any(|a| matches!(a, NetAction::Privmsg { from, to, text } if from.starts_with("42SB") && to == "#c" && text == "[boss] hello all")),
|
||||
"greet shown: {out:?}"
|
||||
);
|
||||
}
|
||||
|
||||
// Fantasy commands typed in-channel route through ChanServ and are sourced
|
||||
// from the assigned bot.
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -122,6 +122,7 @@ fn to_wire(entry: &LogEntry) -> Option<ReplicationEvent> {
|
|||
Event::CertAdded { .. }
|
||||
| Event::CertRemoved { .. }
|
||||
| Event::AccountPasswordSet { .. }
|
||||
| Event::AccountGreetSet { .. }
|
||||
| Event::AjoinAdded { .. }
|
||||
| Event::AjoinRemoved { .. }
|
||||
| Event::AccountSuspended { .. }
|
||||
|
|
@ -369,6 +370,7 @@ mod tests {
|
|||
ajoin: vec![],
|
||||
suspension: None,
|
||||
memos: vec![],
|
||||
greet: String::new(),
|
||||
};
|
||||
let registered = LogEntry::for_test("A", 0, 1, Event::AccountRegistered(acct));
|
||||
let wire = to_wire(®istered).expect("account registration replicates");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue