Migrate interpolated service messages to the t! macro (8 modules)

This commit is contained in:
Jean Chevronnet 2026-07-19 23:22:56 +00:00
parent b462d37bd5
commit d207c3d60d
No known key found for this signature in database
133 changed files with 782 additions and 702 deletions

View file

@ -1,4 +1,4 @@
use echo_api::{NetView, Sender, ServiceCtx, Store};
use echo_api::{t, NetView, Sender, ServiceCtx, Store};
// ACTIVATE <account> / REJECT <account>: approve a pending vhost request (setting
// and applying it) or turn it down. Operators only.
@ -14,30 +14,30 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net:
let host = match db.take_vhost_request(account) {
Ok(Some(h)) => h,
Ok(None) => {
ctx.notice(me, from.uid, format!("\x02{account}\x02 has no pending vhost request."));
ctx.notice(me, from.uid, t!(ctx, "\x02{account}\x02 has no pending vhost request.", account = account));
return;
}
Err(_) => {
ctx.notice(me, from.uid, format!("\x02{account}\x02 isn't registered."));
ctx.notice(me, from.uid, t!(ctx, "\x02{account}\x02 isn't registered.", account = account));
return;
}
};
if !activate {
ctx.notice(me, from.uid, format!("Rejected \x02{account}\x02's vhost request."));
ctx.notice(me, from.uid, t!(ctx, "Rejected \x02{account}\x02's vhost request.", account = account));
return;
}
// Re-check the requested host now (another account may have taken it since).
let host = match super::prepare_vhost(&host, account, db) {
let host = match super::prepare_vhost(&host, account, db, ctx) {
Ok(h) => h,
Err(msg) => {
ctx.notice(me, from.uid, format!("Can't activate: {msg}"));
ctx.notice(me, from.uid, t!(ctx, "Can't activate: {msg}", msg = msg));
return;
}
};
// The forbidden list may have grown since the request was filed; a user's
// request must still obey it (an operator's own SET is a deliberate override).
if db.vhost_is_forbidden(&host) {
ctx.notice(me, from.uid, format!("Can't activate: \x02{host}\x02 is on the forbidden list."));
ctx.notice(me, from.uid, t!(ctx, "Can't activate: \x02{host}\x02 is on the forbidden list.", host = host));
return;
}
match db.set_vhost(account, &host, from.nick, None) {
@ -45,7 +45,7 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net:
for uid in net.uids_logged_into(account) {
ctx.apply_vhost(&uid, &host);
}
ctx.notice(me, from.uid, format!("Activated vhost \x02{host}\x02 for \x02{account}\x02."));
ctx.notice(me, from.uid, t!(ctx, "Activated vhost \x02{host}\x02 for \x02{account}\x02.", host = host, account = account));
}
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
}

View file

@ -1,4 +1,4 @@
use echo_api::{Sender, ServiceCtx, Store};
use echo_api::{t, Sender, ServiceCtx, Store};
// DEFAULT: give yourself the auto-vhost from the network template, with your
// account name substituted for $account.
@ -18,7 +18,7 @@ pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &mut dyn Store)
return;
}
let generated = template.replace("$account", &label);
let host = match super::prepare_vhost(&generated, account, db) {
let host = match super::prepare_vhost(&generated, account, db, ctx) {
Ok(h) if !db.vhost_is_forbidden(&h) => h,
_ => {
ctx.notice(me, from.uid, "Sorry, a vhost couldn't be generated for your account.");
@ -28,7 +28,7 @@ pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &mut dyn Store)
match db.set_vhost(account, &host, "template", None) {
Ok(()) => {
ctx.apply_vhost(from.uid, &host);
ctx.notice(me, from.uid, format!("You now have the vhost \x02{host}\x02."));
ctx.notice(me, from.uid, t!(ctx, "You now have the vhost \x02{host}\x02.", host = host));
}
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
}

View file

@ -1,4 +1,4 @@
use echo_api::{NetView, Sender, ServiceCtx, Store};
use echo_api::{t, NetView, Sender, ServiceCtx, Store};
// DEL <account>: remove an account's vhost, restoring the normal host on any
// online sessions. Operators only.
@ -27,9 +27,9 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net:
}
}
}
ctx.notice(me, from.uid, format!("Vhost for \x02{account}\x02 removed."));
ctx.notice(me, from.uid, t!(ctx, "Vhost for \x02{account}\x02 removed.", account = account));
}
Ok(false) => ctx.notice(me, from.uid, format!("\x02{account}\x02 has no vhost.")),
Err(_) => ctx.notice(me, from.uid, format!("\x02{account}\x02 isn't registered.")),
Ok(false) => ctx.notice(me, from.uid, t!(ctx, "\x02{account}\x02 has no vhost.", account = account)),
Err(_) => ctx.notice(me, from.uid, t!(ctx, "\x02{account}\x02 isn't registered.", account = account)),
}
}

View file

@ -1,4 +1,4 @@
use echo_api::{Sender, ServiceCtx, Store};
use echo_api::{t, Sender, ServiceCtx, Store};
// FORBID <pattern>: block user-requested vhosts matching this regex (operators),
// e.g. (?i)(oper|admin|staff|services) to stop impersonation.
@ -12,9 +12,9 @@ pub fn add(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mu
}
let pattern = args[1..].join(" ");
match db.vhost_forbid_add(&pattern) {
Ok(true) => ctx.notice(me, from.uid, format!("Forbidden vhost pattern added: {pattern}")),
Ok(true) => ctx.notice(me, from.uid, t!(ctx, "Forbidden vhost pattern added: {pattern}", pattern = pattern)),
Ok(false) => ctx.notice(me, from.uid, "That pattern is already forbidden."),
Err(_) => ctx.notice(me, from.uid, format!("\x02{pattern}\x02 isn't a valid regular expression.")),
Err(_) => ctx.notice(me, from.uid, t!(ctx, "\x02{pattern}\x02 isn't a valid regular expression.", pattern = pattern)),
}
}
@ -28,9 +28,9 @@ pub fn list(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &dyn Store) {
ctx.notice(me, from.uid, "No vhost patterns are forbidden.");
return;
}
ctx.notice(me, from.uid, format!("Forbidden vhost patterns ({}):", forbidden.len()));
ctx.notice(me, from.uid, t!(ctx, "Forbidden vhost patterns ({count}):", count = forbidden.len()));
for (i, p) in forbidden.iter().enumerate() {
ctx.notice(me, from.uid, format!(" {}. {p}", i + 1));
ctx.notice(me, from.uid, t!(ctx, " {n}. {p}", n = i + 1, p = p));
}
}
@ -44,8 +44,8 @@ pub fn del(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mu
return;
};
match db.vhost_forbid_del(n) {
Ok(Some(pattern)) => ctx.notice(me, from.uid, format!("Removed forbidden pattern: {pattern}")),
Ok(None) => ctx.notice(me, from.uid, format!("There's no forbidden pattern #\x02{n}\x02.")),
Ok(Some(pattern)) => ctx.notice(me, from.uid, t!(ctx, "Removed forbidden pattern: {pattern}", pattern = pattern)),
Ok(None) => ctx.notice(me, from.uid, t!(ctx, "There's no forbidden pattern #\x02{n}\x02.", n = n)),
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
}
}

View file

@ -3,7 +3,7 @@
//! with SET/DEL and review with LIST. `lib.rs` holds the dispatcher; each
//! command lives in its own file.
use echo_api::{HelpEntry, NetView, Priv, Sender, Service, ServiceCtx, Store};
use echo_api::{t, HelpEntry, NetView, Priv, Sender, Service, ServiceCtx, Store};
#[path = "on.rs"]
mod on;
@ -96,7 +96,7 @@ impl Service for HostServ {
Some("TEMPLATE") => template::handle(me, from, args, ctx, db),
Some("DEFAULT") => default::handle(me, from, ctx, db),
Some("HELP") | None => echo_api::help(me, from, ctx, BLURB, TOPICS, args.get(1).copied()),
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, t!(ctx, "I don't know the command \x02{other}\x02. Try \x02HELP\x02.", other = other)),
}
}
}
@ -125,13 +125,13 @@ pub(crate) fn normalize_vhost(spec: &str) -> String {
// Normalise a requested vhost and check it's valid and not already another
// account's. Returns the canonical spec to store, or a message to show.
pub(crate) fn prepare_vhost(spec: &str, account: &str, db: &dyn Store) -> Result<String, String> {
pub(crate) fn prepare_vhost(spec: &str, account: &str, db: &dyn Store, ctx: &ServiceCtx) -> Result<String, String> {
let host = normalize_vhost(spec);
if !valid_vhost(&host) {
return Err(format!("\x02{host}\x02 isn't a valid host (letters, digits, hyphens and dots)."));
return Err(t!(ctx, "\x02{host}\x02 isn't a valid host (letters, digits, hyphens and dots).", host = host));
}
if db.vhost_owner(&host).is_some_and(|owner| !owner.eq_ignore_ascii_case(account)) {
return Err(format!("\x02{host}\x02 is already in use. Please choose another."));
return Err(t!(ctx, "\x02{host}\x02 is already in use. Please choose another.", host = host));
}
Ok(host)
}

View file

@ -1,4 +1,4 @@
use echo_api::{Sender, ServiceCtx, Store};
use echo_api::{t, Sender, ServiceCtx, Store};
// LIST: every account with an assigned vhost. Operators only.
pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &dyn Store) {
@ -10,9 +10,9 @@ pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &dyn Store) {
ctx.notice(me, from.uid, "No vhosts have been assigned.");
return;
}
ctx.notice(me, from.uid, format!("Assigned vhosts ({}):", vhosts.len()));
ctx.notice(me, from.uid, t!(ctx, "Assigned vhosts ({count}):", count = vhosts.len()));
for v in &vhosts {
let temp = if v.expires.is_some() { ", temporary" } else { "" };
ctx.notice(me, from.uid, format!(" \x02{}\x02{} (by {}{temp})", v.account, v.host, v.setter));
let temp = if v.expires.is_some() { t!(ctx, ", temporary") } else { String::new() };
ctx.notice(me, from.uid, t!(ctx, " \x02{account}\x02 — {host} (by {setter}{temp})", account = v.account, host = v.host, setter = v.setter, temp = temp));
}
}

View file

@ -1,4 +1,4 @@
use echo_api::{Sender, ServiceCtx, Store};
use echo_api::{t, Sender, ServiceCtx, Store};
// OFFER <host>: add a vhost to the self-serve menu (operators).
pub fn add(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) {
@ -10,11 +10,11 @@ pub fn add(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mu
return;
};
if !super::valid_vhost(host) {
ctx.notice(me, from.uid, format!("\x02{host}\x02 isn't a valid host."));
ctx.notice(me, from.uid, t!(ctx, "\x02{host}\x02 isn't a valid host.", host = host));
return;
}
match db.vhost_offer_add(host) {
Ok(true) => ctx.notice(me, from.uid, format!("\x02{host}\x02 is now on the vhost menu.")),
Ok(true) => ctx.notice(me, from.uid, t!(ctx, "\x02{host}\x02 is now on the vhost menu.", host = host)),
Ok(false) => ctx.notice(me, from.uid, "That vhost is already on the menu."),
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
}
@ -27,9 +27,9 @@ pub fn list(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &dyn Store) {
ctx.notice(me, from.uid, "No vhosts are on offer.");
return;
}
ctx.notice(me, from.uid, format!("Vhosts on offer ({}):", offers.len()));
ctx.notice(me, from.uid, t!(ctx, "Vhosts on offer ({count}):", count = offers.len()));
for (i, host) in offers.iter().enumerate() {
ctx.notice(me, from.uid, format!(" {}. {host}", i + 1));
ctx.notice(me, from.uid, t!(ctx, " {n}. {host}", n = i + 1, host = host));
}
ctx.notice(me, from.uid, "Take one with \x02TAKE\x02 <number>.");
}
@ -44,8 +44,8 @@ pub fn del(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mu
return;
};
match db.vhost_offer_del(n) {
Ok(Some(host)) => ctx.notice(me, from.uid, format!("Removed \x02{host}\x02 from the menu.")),
Ok(None) => ctx.notice(me, from.uid, format!("There's no offer #\x02{n}\x02.")),
Ok(Some(host)) => ctx.notice(me, from.uid, t!(ctx, "Removed \x02{host}\x02 from the menu.", host = host)),
Ok(None) => ctx.notice(me, from.uid, t!(ctx, "There's no offer #\x02{n}\x02.", n = n)),
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
}
}

View file

@ -1,4 +1,4 @@
use echo_api::{Sender, ServiceCtx, Store};
use echo_api::{t, Sender, ServiceCtx, Store};
// ON: activate the vhost assigned to your account.
pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &dyn Store) {
@ -9,7 +9,7 @@ pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &dyn Store) {
match db.vhost(account) {
Some(v) => {
ctx.apply_vhost(from.uid, &v.host);
ctx.notice(me, from.uid, format!("Your vhost \x02{}\x02 is now active.", v.host));
ctx.notice(me, from.uid, t!(ctx, "Your vhost \x02{host}\x02 is now active.", host = v.host));
}
None => ctx.notice(me, from.uid, "You have no vhost assigned."),
}

View file

@ -1,4 +1,4 @@
use echo_api::{Sender, ServiceCtx, Store};
use echo_api::{t, Sender, ServiceCtx, Store};
// REQUEST <host>: ask for a vhost, to be approved by an operator.
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) {
@ -10,7 +10,7 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
ctx.notice(me, from.uid, "Syntax: REQUEST <host>");
return;
};
let host = match super::prepare_vhost(host, account, db) {
let host = match super::prepare_vhost(host, account, db, ctx) {
Ok(h) => h,
Err(msg) => {
ctx.notice(me, from.uid, msg);
@ -18,16 +18,16 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
}
};
if db.vhost_is_forbidden(&host) {
ctx.notice(me, from.uid, format!("\x02{host}\x02 isn't allowed here. Please choose another."));
ctx.notice(me, from.uid, t!(ctx, "\x02{host}\x02 isn't allowed here. Please choose another.", host = host));
return;
}
let wait = db.vhost_request_wait(account);
if wait > 0 {
ctx.notice(me, from.uid, format!("Please wait \x02{wait}\x02s before requesting another vhost."));
ctx.notice(me, from.uid, t!(ctx, "Please wait \x02{wait}\x02s before requesting another vhost.", wait = wait));
return;
}
match db.request_vhost(account, &host) {
Ok(()) => ctx.notice(me, from.uid, format!("Requested vhost \x02{host}\x02 — an operator will review it.")),
Ok(()) => ctx.notice(me, from.uid, t!(ctx, "Requested vhost \x02{host}\x02 — an operator will review it.", host = host)),
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
}
}

View file

@ -1,4 +1,4 @@
use echo_api::{parse_duration, NetView, Sender, ServiceCtx, Store};
use echo_api::{parse_duration, t, NetView, Sender, ServiceCtx, Store};
// SET <account> <host> [duration]: assign a vhost to an account, applying it at
// once to online sessions. An optional duration (e.g. 30d) makes it temporary.
@ -12,10 +12,10 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net:
return;
};
if db.account(account).is_none() {
ctx.notice(me, from.uid, format!("\x02{account}\x02 isn't registered."));
ctx.notice(me, from.uid, t!(ctx, "\x02{account}\x02 isn't registered.", account = account));
return;
}
let host = match super::prepare_vhost(host, account, db) {
let host = match super::prepare_vhost(host, account, db, ctx) {
Ok(h) => h,
Err(msg) => {
ctx.notice(me, from.uid, msg);
@ -28,8 +28,8 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net:
for uid in net.uids_logged_into(account) {
ctx.apply_vhost(&uid, &host);
}
let when = args.get(3).filter(|_| ttl.is_some()).map(|d| format!(" (expires in {d})")).unwrap_or_default();
ctx.notice(me, from.uid, format!("Vhost \x02{host}\x02 assigned to \x02{account}\x02{when}."));
let when = args.get(3).filter(|_| ttl.is_some()).map(|d| t!(ctx, " (expires in {d})", d = d)).unwrap_or_default();
ctx.notice(me, from.uid, t!(ctx, "Vhost \x02{host}\x02 assigned to \x02{account}\x02{when}.", host = host, account = account, when = when));
}
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
}

View file

@ -1,4 +1,4 @@
use echo_api::{Sender, ServiceCtx, Store};
use echo_api::{t, Sender, ServiceCtx, Store};
// TAKE <number>: assign yourself the vhost offered at that position on the menu.
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) {
@ -11,10 +11,10 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
return;
};
let Some(offer) = n.checked_sub(1).and_then(|i| db.vhost_offers().into_iter().nth(i)) else {
ctx.notice(me, from.uid, format!("There's no offer #\x02{n}\x02. See \x02OFFERLIST\x02."));
ctx.notice(me, from.uid, t!(ctx, "There's no offer #\x02{n}\x02. See \x02OFFERLIST\x02.", n = n));
return;
};
let host = match super::prepare_vhost(&offer, account, db) {
let host = match super::prepare_vhost(&offer, account, db, ctx) {
Ok(h) => h,
Err(msg) => {
ctx.notice(me, from.uid, msg);
@ -24,13 +24,13 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
// A menu offer can outlive a later FORBID; re-check at grant time like the
// request/approve/default paths, or a stale offer bypasses the impersonation guard.
if db.vhost_is_forbidden(&host) {
ctx.notice(me, from.uid, format!("\x02{host}\x02 isn't allowed here. Please choose another."));
ctx.notice(me, from.uid, t!(ctx, "\x02{host}\x02 isn't allowed here. Please choose another.", host = host));
return;
}
match db.set_vhost(account, &host, "offer", None) {
Ok(()) => {
ctx.apply_vhost(from.uid, &host);
ctx.notice(me, from.uid, format!("You now have the vhost \x02{host}\x02."));
ctx.notice(me, from.uid, t!(ctx, "You now have the vhost \x02{host}\x02.", host = host));
}
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
}

View file

@ -1,4 +1,4 @@
use echo_api::{Sender, ServiceCtx, Store};
use echo_api::{t, Sender, ServiceCtx, Store};
// TEMPLATE [<pattern>]: show the auto-vhost template, or (operators) set it.
// Use $account for the requester's sanitised account name, e.g.
@ -6,7 +6,7 @@ use echo_api::{Sender, ServiceCtx, Store};
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) {
match args.get(1) {
None => match db.vhost_template() {
Some(t) => ctx.notice(me, from.uid, format!("Auto-vhost template: \x02{t}\x02. Users apply it with \x02DEFAULT\x02.")),
Some(tmpl) => ctx.notice(me, from.uid, t!(ctx, "Auto-vhost template: \x02{tmpl}\x02. Users apply it with \x02DEFAULT\x02.", tmpl = tmpl)),
None => ctx.notice(me, from.uid, "No auto-vhost template is set."),
},
Some(&arg) => {
@ -24,7 +24,7 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
return;
}
match db.set_vhost_template(Some(template.clone())) {
Ok(()) => ctx.notice(me, from.uid, format!("Auto-vhost template set to \x02{template}\x02.")),
Ok(()) => ctx.notice(me, from.uid, t!(ctx, "Auto-vhost template set to \x02{template}\x02.", template = template)),
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
}
}

View file

@ -1,4 +1,4 @@
use echo_api::{Sender, ServiceCtx, Store};
use echo_api::{t, Sender, ServiceCtx, Store};
// WAITING: pending vhost requests awaiting approval. Operators only.
pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &dyn Store) {
@ -10,9 +10,9 @@ pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &dyn Store) {
ctx.notice(me, from.uid, "No vhost requests are waiting.");
return;
}
ctx.notice(me, from.uid, format!("Pending vhost requests ({}):", requests.len()));
ctx.notice(me, from.uid, t!(ctx, "Pending vhost requests ({count}):", count = requests.len()));
for (account, host) in &requests {
ctx.notice(me, from.uid, format!(" \x02{account}\x02{host}"));
ctx.notice(me, from.uid, t!(ctx, " \x02{account}\x02 — {host}", account = account, host = host));
}
ctx.notice(me, from.uid, "Approve with \x02ACTIVATE\x02 <account> or turn down with \x02REJECT\x02 <account>.");
}