nickserv: add INFO and ALIST
INFO shows an account's registration date and (to its owner) email; ALIST lists the channels an account founds or has access on. The UTC time formatter moves to the db module so both services share it.
This commit is contained in:
parent
9d6cf3bb08
commit
0427819f86
7 changed files with 115 additions and 32 deletions
|
|
@ -100,7 +100,7 @@ impl Service for ChanServ {
|
||||||
if !info.desc.is_empty() {
|
if !info.desc.is_empty() {
|
||||||
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 : {}", human_time(info.ts)));
|
ctx.notice(me, from.uid, format!(" Registered : {}", crate::engine::db::human_time(info.ts)));
|
||||||
}
|
}
|
||||||
None => ctx.notice(me, from.uid, format!("\x02{chan}\x02 isn't registered.")),
|
None => ctx.notice(me, from.uid, format!("\x02{chan}\x02 isn't registered.")),
|
||||||
}
|
}
|
||||||
|
|
@ -268,32 +268,3 @@ fn show_mlock(info: &ChannelInfo) -> String {
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format a Unix timestamp (seconds) as "YYYY-MM-DD HH:MM:SS UTC", using Howard
|
|
||||||
// Hinnant's civil-from-days algorithm so no date crate is needed.
|
|
||||||
fn human_time(ts: u64) -> String {
|
|
||||||
let days = (ts / 86400) as i64;
|
|
||||||
let rem = ts % 86400;
|
|
||||||
let (hh, mm, ss) = (rem / 3600, (rem % 3600) / 60, rem % 60);
|
|
||||||
let z = days + 719468;
|
|
||||||
let era = (if z >= 0 { z } else { z - 146096 }) / 146097;
|
|
||||||
let doe = z - era * 146097;
|
|
||||||
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
|
|
||||||
let y = yoe + era * 400;
|
|
||||||
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
|
|
||||||
let mp = (5 * doy + 2) / 153;
|
|
||||||
let day = doy - (153 * mp + 2) / 5 + 1;
|
|
||||||
let month = if mp < 10 { mp + 3 } else { mp - 9 };
|
|
||||||
let year = y + i64::from(month <= 2);
|
|
||||||
format!("{year:04}-{month:02}-{day:02} {hh:02}:{mm:02}:{ss:02} UTC")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::human_time;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn formats_unix_time_as_utc() {
|
|
||||||
assert_eq!(human_time(0), "1970-01-01 00:00:00 UTC");
|
|
||||||
assert_eq!(human_time(1783844590), "2026-07-12 08:23:10 UTC");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
match net.last_seen(nick) {
|
match net.last_seen(nick) {
|
||||||
Some(s) => ctx.notice(me, from.uid, format!("\x02{}\x02 was last seen {} ({}).", s.nick, super::human_time(s.ts), s.what)),
|
Some(s) => ctx.notice(me, from.uid, format!("\x02{}\x02 was last seen {} ({}).", s.nick, crate::engine::db::human_time(s.ts), s.what)),
|
||||||
None => ctx.notice(me, from.uid, format!("I have no record of \x02{nick}\x02.")),
|
None => ctx.notice(me, from.uid, format!("I have no record of \x02{nick}\x02.")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
27
modules/nickserv/alist.rs
Normal file
27
modules/nickserv/alist.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
use crate::engine::db::Db;
|
||||||
|
use crate::engine::service::{Sender, ServiceCtx};
|
||||||
|
|
||||||
|
// ALIST: list the channels the sender's account founds or has access on.
|
||||||
|
pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &Db) {
|
||||||
|
let Some(account) = from.account else {
|
||||||
|
ctx.notice(me, from.uid, "You need to be logged in. Identify to NickServ first.");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let mut rows: Vec<(String, &str)> = Vec::new();
|
||||||
|
for c in db.channels() {
|
||||||
|
if c.founder.eq_ignore_ascii_case(account) {
|
||||||
|
rows.push((c.name.clone(), "founder"));
|
||||||
|
} else if let Some(a) = c.access.iter().find(|a| a.account.eq_ignore_ascii_case(account)) {
|
||||||
|
rows.push((c.name.clone(), if a.level == "voice" { "voice" } else { "op" }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if rows.is_empty() {
|
||||||
|
ctx.notice(me, from.uid, "You have access on no channels.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
rows.sort_by(|a, b| a.0.cmp(&b.0));
|
||||||
|
ctx.notice(me, from.uid, format!("Channels you have access on ({}):", rows.len()));
|
||||||
|
for (chan, role) in rows {
|
||||||
|
ctx.notice(me, from.uid, format!(" \x02{chan}\x02 ({role})"));
|
||||||
|
}
|
||||||
|
}
|
||||||
20
modules/nickserv/info.rs
Normal file
20
modules/nickserv/info.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
use crate::engine::db::{human_time, Db};
|
||||||
|
use crate::engine::service::{Sender, ServiceCtx};
|
||||||
|
|
||||||
|
// INFO [account]: show an account's registration details. The email is shown
|
||||||
|
// only to the account's own owner.
|
||||||
|
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &Db) {
|
||||||
|
let name = args.get(1).copied().unwrap_or(from.nick);
|
||||||
|
let Some(acct) = db.account(name) else {
|
||||||
|
ctx.notice(me, from.uid, format!("\x02{name}\x02 isn't registered."));
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
ctx.notice(me, from.uid, format!("Information for \x02{}\x02:", acct.name));
|
||||||
|
ctx.notice(me, from.uid, format!(" Registered : {}", human_time(acct.ts)));
|
||||||
|
if from.account == Some(acct.name.as_str()) {
|
||||||
|
match &acct.email {
|
||||||
|
Some(email) => ctx.notice(me, from.uid, format!(" Email : {email}")),
|
||||||
|
None => ctx.notice(me, from.uid, " Email : (none set)"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,6 +10,10 @@ mod identify;
|
||||||
mod logout;
|
mod logout;
|
||||||
#[path = "cert.rs"]
|
#[path = "cert.rs"]
|
||||||
mod cert;
|
mod cert;
|
||||||
|
#[path = "info.rs"]
|
||||||
|
mod info;
|
||||||
|
#[path = "alist.rs"]
|
||||||
|
mod alist;
|
||||||
|
|
||||||
pub struct NickServ {
|
pub struct NickServ {
|
||||||
pub uid: String,
|
pub uid: String,
|
||||||
|
|
@ -40,7 +44,9 @@ impl Service for NickServ {
|
||||||
Some("IDENTIFY") | Some("ID") => identify::handle(me, from, args, ctx, db),
|
Some("IDENTIFY") | Some("ID") => identify::handle(me, from, args, ctx, db),
|
||||||
Some("LOGOUT") | Some("LOGOFF") => logout::handle(me, &self.guest_nick, &mut self.guest_seq, from, ctx),
|
Some("LOGOUT") | Some("LOGOFF") => logout::handle(me, &self.guest_nick, &mut self.guest_seq, from, ctx),
|
||||||
Some("CERT") => cert::handle(me, from, args, ctx, db),
|
Some("CERT") => cert::handle(me, from, args, ctx, db),
|
||||||
Some("HELP") => ctx.notice(me, from.uid, "NickServ looks after your nickname. Commands: \x02REGISTER\x02 <password> [email], \x02IDENTIFY\x02 [account] <password>, \x02LOGOUT\x02, \x02CERT\x02 ADD|DEL|LIST <password> [fingerprint]."),
|
Some("INFO") => info::handle(me, from, args, ctx, db),
|
||||||
|
Some("ALIST") => alist::handle(me, from, ctx, db),
|
||||||
|
Some("HELP") => ctx.notice(me, from.uid, "NickServ looks after your nickname. Commands: \x02REGISTER\x02 <password> [email], \x02IDENTIFY\x02 [account] <password>, \x02INFO\x02 [account], \x02ALIST\x02, \x02LOGOUT\x02, \x02CERT\x02 ADD|DEL|LIST <password> [fingerprint]."),
|
||||||
Some(other) => ctx.notice(me, from.uid, format!("I don't know the command \x02{other}\x02. Try \x02HELP\x02.")),
|
Some(other) => ctx.notice(me, from.uid, format!("I don't know the command \x02{other}\x02. Try \x02HELP\x02.")),
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -433,6 +433,25 @@ fn now() -> 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Format a Unix timestamp (seconds) as "YYYY-MM-DD HH:MM:SS UTC", using Howard
|
||||||
|
// Hinnant's civil-from-days algorithm so no date crate is needed.
|
||||||
|
pub(crate) fn human_time(ts: u64) -> String {
|
||||||
|
let days = (ts / 86400) as i64;
|
||||||
|
let rem = ts % 86400;
|
||||||
|
let (hh, mm, ss) = (rem / 3600, (rem % 3600) / 60, rem % 60);
|
||||||
|
let z = days + 719468;
|
||||||
|
let era = (if z >= 0 { z } else { z - 146096 }) / 146097;
|
||||||
|
let doe = z - era * 146097;
|
||||||
|
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
|
||||||
|
let y = yoe + era * 400;
|
||||||
|
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
|
||||||
|
let mp = (5 * doy + 2) / 153;
|
||||||
|
let day = doy - (153 * mp + 2) / 5 + 1;
|
||||||
|
let month = if mp < 10 { mp + 3 } else { mp - 9 };
|
||||||
|
let year = y + i64::from(month <= 2);
|
||||||
|
format!("{year:04}-{month:02}-{day:02} {hh:02}:{mm:02}:{ss:02} UTC")
|
||||||
|
}
|
||||||
|
|
||||||
impl Db {
|
impl Db {
|
||||||
pub fn open(path: impl Into<PathBuf>, origin: impl Into<String>) -> Self {
|
pub fn open(path: impl Into<PathBuf>, origin: impl Into<String>) -> Self {
|
||||||
let (log, events) = EventLog::open(path.into(), origin.into());
|
let (log, events) = EventLog::open(path.into(), origin.into());
|
||||||
|
|
@ -643,6 +662,11 @@ impl Db {
|
||||||
self.channels.get(&key(name))
|
self.channels.get(&key(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The account record for `name` (its canonical casing), if registered.
|
||||||
|
pub fn account(&self, name: &str) -> Option<&Account> {
|
||||||
|
self.accounts.get(&key(name))
|
||||||
|
}
|
||||||
|
|
||||||
/// All registered channels, for listing.
|
/// All registered channels, for listing.
|
||||||
pub fn channels(&self) -> impl Iterator<Item = &ChannelInfo> {
|
pub fn channels(&self) -> impl Iterator<Item = &ChannelInfo> {
|
||||||
self.channels.values()
|
self.channels.values()
|
||||||
|
|
@ -929,6 +953,12 @@ mod tests {
|
||||||
Event::CertAdded { account: account.into(), fp: fp.into() }
|
Event::CertAdded { account: account.into(), fp: fp.into() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn formats_unix_time_as_utc() {
|
||||||
|
assert_eq!(human_time(0), "1970-01-01 00:00:00 UTC");
|
||||||
|
assert_eq!(human_time(1783844590), "2026-07-12 08:23:10 UTC");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn glob_matches_hostmasks() {
|
fn glob_matches_hostmasks() {
|
||||||
assert!(glob_match("*!*@host.example", "bob!~b@host.example"));
|
assert!(glob_match("*!*@host.example", "bob!~b@host.example"));
|
||||||
|
|
|
||||||
|
|
@ -1103,6 +1103,35 @@ mod tests {
|
||||||
assert!(released, "the user must be told which channels were released");
|
assert!(released, "the user must be told which channels were released");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NickServ INFO shows registration details (email only to the owner); ALIST
|
||||||
|
// lists the channels the account founds or has access on.
|
||||||
|
#[test]
|
||||||
|
fn nickserv_info_and_alist() {
|
||||||
|
use crate::chanserv::ChanServ;
|
||||||
|
let path = std::env::temp_dir().join("fedserv-nsinfo.jsonl");
|
||||||
|
let _ = std::fs::remove_file(&path);
|
||||||
|
let mut db = Db::open(&path, "test");
|
||||||
|
db.scram_iterations = 4096;
|
||||||
|
db.register("alice", "sesame", None).unwrap();
|
||||||
|
db.register_channel("#a", "alice").unwrap();
|
||||||
|
let ns = NickServ { uid: "42SAAAAAA".into(), guest_nick: "Guest".into(), guest_seq: 0 };
|
||||||
|
let cs = ChanServ { uid: "42SAAAAAB".into() };
|
||||||
|
let mut e = Engine::new(vec![Box::new(ns), Box::new(cs)], db);
|
||||||
|
let to_ns = |e: &mut Engine, text: &str| {
|
||||||
|
e.handle(NetEvent::Privmsg { from: "000AAAAAB".into(), to: "42SAAAAAA".into(), text: text.into() })
|
||||||
|
};
|
||||||
|
let notice = |out: &[NetAction], needle: &str| out.iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains(needle)));
|
||||||
|
e.handle(NetEvent::UserConnect { uid: "000AAAAAB".into(), nick: "alice".into(), host: "h".into() });
|
||||||
|
e.handle(NetEvent::Privmsg { from: "000AAAAAB".into(), to: "42SAAAAAA".into(), text: "IDENTIFY sesame".into() });
|
||||||
|
|
||||||
|
let info = to_ns(&mut e, "INFO");
|
||||||
|
assert!(notice(&info, "Information for \x02alice\x02"));
|
||||||
|
assert!(notice(&info, "Registered"));
|
||||||
|
assert!(notice(&info, "Email"), "owner sees their email line: {info:?}");
|
||||||
|
assert!(notice(&to_ns(&mut e, "INFO ghost"), "isn't registered"));
|
||||||
|
assert!(notice(&to_ns(&mut e, "ALIST"), "#a"));
|
||||||
|
}
|
||||||
|
|
||||||
// IDENTIFY accepts the two-arg account+password form: log into a named
|
// IDENTIFY accepts the two-arg account+password form: log into a named
|
||||||
// account even when the current nick differs; a wrong password is refused.
|
// account even when the current nick differs; a wrong password is refused.
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue