Add HostServ: virtual hosts
New HostServ pseudo-service assigns and applies vhosts. A vhost is a typed
Option<Vhost>{host,setter,ts} on the account (VhostSet/VhostDeleted events),
applied to the displayed host on identify and re-applied by ON; OFF restores
the normal host for the session. Operators SET/DEL (applied at once to
online sessions) and LIST; hosts are validated as dot-separated labels.
New SetHost NetAction -> InspIRCd ENCAP <target-sid> CHGHOST <uid> <host>.
This commit is contained in:
parent
540d2f39d1
commit
1227e26b95
17 changed files with 378 additions and 6 deletions
8
hostserv/Cargo.toml
Normal file
8
hostserv/Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "fedserv-hostserv"
|
||||
version = "0.0.1"
|
||||
edition = "2021"
|
||||
description = "HostServ: assign and apply virtual hosts (vhosts)."
|
||||
|
||||
[dependencies]
|
||||
fedserv-api = { path = "../api" }
|
||||
26
hostserv/src/del.rs
Normal file
26
hostserv/src/del.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
use fedserv_api::{NetView, Sender, ServiceCtx, Store};
|
||||
|
||||
// DEL <account>: remove an account's vhost, restoring the normal host on any
|
||||
// online sessions. Operators only.
|
||||
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: &dyn NetView, db: &mut dyn Store) {
|
||||
if !super::require_oper(me, from, ctx) {
|
||||
return;
|
||||
}
|
||||
let Some(&account) = args.get(1) else {
|
||||
ctx.notice(me, from.uid, "Syntax: DEL <account>");
|
||||
return;
|
||||
};
|
||||
match db.del_vhost(account) {
|
||||
Ok(true) => {
|
||||
for uid in net.uids_logged_into(account) {
|
||||
if let Some(host) = net.host_of(&uid) {
|
||||
let host = host.to_string();
|
||||
ctx.set_host(&uid, &host);
|
||||
}
|
||||
}
|
||||
ctx.notice(me, from.uid, format!("Vhost for \x02{account}\x02 removed."));
|
||||
}
|
||||
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.")),
|
||||
}
|
||||
}
|
||||
70
hostserv/src/lib.rs
Normal file
70
hostserv/src/lib.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
//! HostServ assigns virtual hosts (vhosts) to accounts and applies them to the
|
||||
//! displayed host. Members toggle their own with ON/OFF; operators assign them
|
||||
//! with SET/DEL and review with LIST. `lib.rs` holds the dispatcher; each
|
||||
//! command lives in its own file.
|
||||
|
||||
use fedserv_api::{NetView, Priv, Sender, Service, ServiceCtx, Store};
|
||||
|
||||
#[path = "on.rs"]
|
||||
mod on;
|
||||
#[path = "off.rs"]
|
||||
mod off;
|
||||
#[path = "set.rs"]
|
||||
mod set;
|
||||
#[path = "del.rs"]
|
||||
mod del;
|
||||
#[path = "list.rs"]
|
||||
mod list;
|
||||
|
||||
pub struct HostServ {
|
||||
pub uid: String,
|
||||
}
|
||||
|
||||
impl Service for HostServ {
|
||||
fn nick(&self) -> &str {
|
||||
"HostServ"
|
||||
}
|
||||
fn uid(&self) -> &str {
|
||||
&self.uid
|
||||
}
|
||||
fn gecos(&self) -> &str {
|
||||
"Host 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("ON") => on::handle(me, from, ctx, db),
|
||||
Some("OFF") => off::handle(me, from, ctx, net, db),
|
||||
Some("SET") => set::handle(me, from, args, ctx, net, db),
|
||||
Some("DEL") => del::handle(me, from, args, ctx, net, db),
|
||||
Some("LIST") => list::handle(me, from, ctx, db),
|
||||
Some("HELP") | None => ctx.notice(me, from.uid, "HostServ gives you a vhost: \x02ON\x02 activates your assigned vhost, \x02OFF\x02 restores your normal host. Operators use \x02SET\x02 <account> <host>, \x02DEL\x02 <account> and \x02LIST\x02."),
|
||||
Some(other) => ctx.notice(me, from.uid, format!("I don't know the command \x02{other}\x02. Try \x02HELP\x02.")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Operator gate for vhost administration.
|
||||
fn require_oper(me: &str, from: &Sender, ctx: &mut ServiceCtx) -> bool {
|
||||
if from.privs.has(Priv::Admin) {
|
||||
return true;
|
||||
}
|
||||
ctx.notice(me, from.uid, "Access denied — assigning vhosts is for services operators.");
|
||||
false
|
||||
}
|
||||
|
||||
// Whether `host` is a syntactically valid vhost: a hostname of dot-separated
|
||||
// labels using letters, digits and hyphens, not too long.
|
||||
pub(crate) fn valid_vhost(host: &str) -> bool {
|
||||
if host.is_empty() || host.len() > 64 || !host.contains('.') {
|
||||
return false;
|
||||
}
|
||||
host.split('.').all(|label| {
|
||||
!label.is_empty()
|
||||
&& label.len() <= 63
|
||||
&& !label.starts_with('-')
|
||||
&& !label.ends_with('-')
|
||||
&& label.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-')
|
||||
})
|
||||
}
|
||||
17
hostserv/src/list.rs
Normal file
17
hostserv/src/list.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
use fedserv_api::{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) {
|
||||
if !super::require_oper(me, from, ctx) {
|
||||
return;
|
||||
}
|
||||
let vhosts = db.vhosts();
|
||||
if vhosts.is_empty() {
|
||||
ctx.notice(me, from.uid, "No vhosts have been assigned.");
|
||||
return;
|
||||
}
|
||||
ctx.notice(me, from.uid, format!("Assigned vhosts ({}):", vhosts.len()));
|
||||
for v in &vhosts {
|
||||
ctx.notice(me, from.uid, format!(" \x02{}\x02 — {} (by {})", v.account, v.host, v.setter));
|
||||
}
|
||||
}
|
||||
22
hostserv/src/off.rs
Normal file
22
hostserv/src/off.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use fedserv_api::{NetView, Sender, ServiceCtx, Store};
|
||||
|
||||
// OFF: restore your normal host for this session (the vhost stays assigned and
|
||||
// re-applies next time you identify).
|
||||
pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, net: &dyn NetView, db: &dyn Store) {
|
||||
let Some(account) = from.account else {
|
||||
ctx.notice(me, from.uid, "You need to identify to NickServ first.");
|
||||
return;
|
||||
};
|
||||
if db.vhost(account).is_none() {
|
||||
ctx.notice(me, from.uid, "You have no vhost assigned.");
|
||||
return;
|
||||
}
|
||||
match net.host_of(from.uid) {
|
||||
Some(host) => {
|
||||
let host = host.to_string();
|
||||
ctx.set_host(from.uid, &host);
|
||||
ctx.notice(me, from.uid, "Your vhost is off; your normal host is restored.");
|
||||
}
|
||||
None => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
|
||||
}
|
||||
}
|
||||
16
hostserv/src/on.rs
Normal file
16
hostserv/src/on.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
use fedserv_api::{Sender, ServiceCtx, Store};
|
||||
|
||||
// ON: activate the vhost assigned to your account.
|
||||
pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &dyn Store) {
|
||||
let Some(account) = from.account else {
|
||||
ctx.notice(me, from.uid, "You need to identify to NickServ first.");
|
||||
return;
|
||||
};
|
||||
match db.vhost(account) {
|
||||
Some(v) => {
|
||||
ctx.set_host(from.uid, &v.host);
|
||||
ctx.notice(me, from.uid, format!("Your vhost \x02{}\x02 is now active.", v.host));
|
||||
}
|
||||
None => ctx.notice(me, from.uid, "You have no vhost assigned."),
|
||||
}
|
||||
}
|
||||
30
hostserv/src/set.rs
Normal file
30
hostserv/src/set.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
use fedserv_api::{NetView, Sender, ServiceCtx, Store};
|
||||
|
||||
// SET <account> <host>: assign a vhost to an account, applying it at once to any
|
||||
// online sessions. Operators only.
|
||||
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: &dyn NetView, db: &mut dyn Store) {
|
||||
if !super::require_oper(me, from, ctx) {
|
||||
return;
|
||||
}
|
||||
let (Some(&account), Some(&host)) = (args.get(1), args.get(2)) else {
|
||||
ctx.notice(me, from.uid, "Syntax: SET <account> <host>");
|
||||
return;
|
||||
};
|
||||
if !super::valid_vhost(host) {
|
||||
ctx.notice(me, from.uid, format!("\x02{host}\x02 isn't a valid host (letters, digits, hyphens and dots)."));
|
||||
return;
|
||||
}
|
||||
if db.account(account).is_none() {
|
||||
ctx.notice(me, from.uid, format!("\x02{account}\x02 isn't registered."));
|
||||
return;
|
||||
}
|
||||
match db.set_vhost(account, host, from.nick) {
|
||||
Ok(()) => {
|
||||
for uid in net.uids_logged_into(account) {
|
||||
ctx.set_host(&uid, host);
|
||||
}
|
||||
ctx.notice(me, from.uid, format!("Vhost \x02{host}\x02 assigned to \x02{account}\x02."));
|
||||
}
|
||||
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue