diff --git a/connector/Cargo.lock b/connector/Cargo.lock index a200e61..011a9ee 100644 --- a/connector/Cargo.lock +++ b/connector/Cargo.lock @@ -2641,6 +2641,7 @@ dependencies = [ "futures", "serde", "serde_json", + "time", "tokio", "tracing-subscriber", "tsclientlib", diff --git a/connector/Cargo.toml b/connector/Cargo.toml index d9f07d5..9b3cb80 100644 --- a/connector/Cargo.toml +++ b/connector/Cargo.toml @@ -15,3 +15,4 @@ clap = { version = "4", features = ["derive"] } anyhow = "1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } base64 = "0.22" +time = "0.3" diff --git a/connector/src/main.rs b/connector/src/main.rs index 3cc069f..bdc817b 100644 --- a/connector/src/main.rs +++ b/connector/src/main.rs @@ -7,7 +7,7 @@ use audiopus::coder::Encoder as OpusEncoder; use base64::Engine; use clap::Parser; use futures::prelude::*; -use serde::Serialize; +use serde::{Deserialize, Serialize}; use tokio::io::{AsyncBufReadExt, BufReader}; use tsclientlib::audio::AudioHandler; @@ -15,8 +15,9 @@ use tsclientlib::events::{Event as BookEvent, PropertyId, PropertyValue}; use tsclientlib::messages::c2s::{OutClientPokeRequestPart, OutSendTextMessagePart}; use tsclientlib::prelude::*; use tsclientlib::{ - data, ChannelId, ClientId, Connection, DisconnectOptions, Identity, MaxClients, MessageHandle, - MessageTarget, Reason, StreamItem, TextMessageTargetMode, + data, ChannelId, ClientId, CodecEncryptionMode, Connection, DisconnectOptions, HostBannerMode, + HostMessageMode, Identity, MaxClients, MessageHandle, MessageTarget, Reason, StreamItem, + TextMessageTargetMode, }; use tsproto_packets::packets::{AudioData, CodecType, Direction, Flags, OutAudio, OutCommand, PacketType}; @@ -78,6 +79,64 @@ struct ClientInfo { uid: String, } +/// Payload for the "serveredit " stdin command - every field is optional so +/// the frontend only needs to send what actually changed. Covers the fields +/// tsclientlib's `Server::edit()` builder exposes named setters for; a few +/// exotic ones (antiflood tuning, quotas, log toggles) have no typed setter +/// upstream and aren't included here. +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase")] +struct ServerEditPayload { + name: Option, + welcome_message: Option, + /// `Some("")` clears the password; `None` leaves it unchanged. + password: Option, + max_clients: Option, + hostmessage: Option, + /// "none" | "log" | "modal" | "modalquit" + hostmessage_mode: Option, + hostbanner_url: Option, + hostbanner_gfx_url: Option, + hostbanner_gfx_interval_secs: Option, + /// "noadjust" | "adjustignoreaspect" | "adjustkeepaspect" + hostbanner_mode: Option, + hostbutton_tooltip: Option, + hostbutton_url: Option, + hostbutton_gfx_url: Option, + nickname: Option, + phonetic_name: Option, + /// "perchannel" | "forcedoff" | "forcedon" + codec_encryption_mode: Option, +} + +fn parse_host_message_mode(s: &str) -> Option { + match s.to_ascii_lowercase().as_str() { + "none" => Some(HostMessageMode::None), + "log" => Some(HostMessageMode::Log), + "modal" => Some(HostMessageMode::Modal), + "modalquit" => Some(HostMessageMode::Modalquit), + _ => None, + } +} + +fn parse_host_banner_mode(s: &str) -> Option { + match s.to_ascii_lowercase().as_str() { + "noadjust" => Some(HostBannerMode::NoAdjust), + "adjustignoreaspect" => Some(HostBannerMode::AdjustIgnoreAspect), + "adjustkeepaspect" => Some(HostBannerMode::AdjustKeepAspect), + _ => None, + } +} + +fn parse_codec_encryption_mode(s: &str) -> Option { + match s.to_ascii_lowercase().as_str() { + "perchannel" => Some(CodecEncryptionMode::PerChannel), + "forcedoff" => Some(CodecEncryptionMode::ForcedOff), + "forcedon" => Some(CodecEncryptionMode::ForcedOn), + _ => None, + } +} + /// Human-facing entries for the Server tab's log-style notifications /// (client join/leave/switch, channel group assignment, channel/server /// changes, own permission errors) - mirrors what the native TS3 client @@ -741,6 +800,73 @@ async fn run(args: Args) -> Result<()> { } Err(_) => emit(&Event::Error { message: format!("Invalid client id: {id_str}") }), } + } else if let Some(rest) = l.strip_prefix("serveredit ") { + match serde_json::from_str::(rest) { + Ok(payload) => { + let mut part = con.get_state()?.server.edit(); + if let Some(v) = &payload.name { + part = part.set_name(v); + } + if let Some(v) = &payload.welcome_message { + part = part.set_welcome_message(v); + } + if let Some(v) = &payload.password { + part = part.set_password(if v.is_empty() { None } else { Some(v) }); + } + if let Some(v) = payload.max_clients { + part = part.set_max_clients(v); + } + if let Some(v) = &payload.hostmessage { + part = part.set_hostmessage(v); + } + if let Some(mode) = + payload.hostmessage_mode.as_deref().and_then(parse_host_message_mode) + { + part = part.set_hostmessage_mode(mode); + } + if let Some(v) = &payload.hostbanner_url { + part = part.set_hostbanner_url(v); + } + if let Some(v) = &payload.hostbanner_gfx_url { + part = part.set_hostbanner_gfx_url(v); + } + if let Some(secs) = payload.hostbanner_gfx_interval_secs { + part = part.set_hostbanner_gfx_interval(time::Duration::seconds(secs)); + } + if let Some(mode) = + payload.hostbanner_mode.as_deref().and_then(parse_host_banner_mode) + { + part = part.set_hostbanner_mode(mode); + } + if let Some(v) = &payload.hostbutton_tooltip { + part = part.set_hostbutton_tooltip(v); + } + if let Some(v) = &payload.hostbutton_url { + part = part.set_hostbutton_url(v); + } + if let Some(v) = &payload.hostbutton_gfx_url { + part = part.set_hostbutton_gfx_url(v); + } + if let Some(v) = &payload.nickname { + part = part.set_nickname(v); + } + if let Some(v) = &payload.phonetic_name { + part = part.set_phonetic_name(v); + } + if let Some(mode) = + payload.codec_encryption_mode.as_deref().and_then(parse_codec_encryption_mode) + { + part = part.set_codec_encryption_mode(mode); + } + match part.send_with_result(&mut con) { + Ok(handle) => { + pending_messages.insert(handle, "Server edit".into()); + } + Err(e) => emit(&Event::Error { message: e.to_string() }), + } + } + Err(e) => emit(&Event::Error { message: format!("Invalid server edit payload: {e}") }), + } } else if l == "serverconninfo" { let part = con.get_state()?.server.connection_info(); match part.send_with_result(&mut con) { diff --git a/gateway/src/index.ts b/gateway/src/index.ts index ce37761..07e2d78 100644 --- a/gateway/src/index.ts +++ b/gateway/src/index.ts @@ -142,6 +142,10 @@ wss.on("connection", (socket: WebSocket) => { await connection?.banClient(msg.clientId, msg.seconds ?? 0, msg.reason ?? ""); break; } + case "editServer": { + await connection?.editServer(msg.payload ?? {}); + break; + } case "disconnect": { await connection?.disconnect(msg.message ?? ""); break; diff --git a/gateway/src/ts3/connection.ts b/gateway/src/ts3/connection.ts index 3827f75..caba0c7 100644 --- a/gateway/src/ts3/connection.ts +++ b/gateway/src/ts3/connection.ts @@ -327,6 +327,30 @@ export class Ts3Connection { this.child?.stdin.write(`banclient ${clientId} ${Math.max(0, Math.floor(seconds))} ${sanitized}\n`); } + /** Every field is optional - only send what actually changed. JSON.stringify + * already escapes embedded newlines, so this is safe as a single stdin line + * without extra sanitization. */ + async editServer(payload: { + name?: string; + welcomeMessage?: string; + password?: string; + maxClients?: number; + hostmessage?: string; + hostmessageMode?: string; + hostbannerUrl?: string; + hostbannerGfxUrl?: string; + hostbannerGfxIntervalSecs?: number; + hostbannerMode?: string; + hostbuttonTooltip?: string; + hostbuttonUrl?: string; + hostbuttonGfxUrl?: string; + nickname?: string; + phoneticName?: string; + codecEncryptionMode?: string; + }): Promise { + this.child?.stdin.write(`serveredit ${JSON.stringify(payload)}\n`); + } + async sendChatMessage(message: string): Promise { const sanitized = message.replace(/[\r\n]+/g, " ").trim(); if (sanitized) this.child?.stdin.write(`chat ${sanitized}\n`); diff --git a/web/src/App.tsx b/web/src/App.tsx index 22ded0a..e697caf 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -492,6 +492,7 @@ function InfoPanel({ channels, clients, onShowServerConnectionInfo, + onEditServer, }: { selected: SelectedItem | null; host: string; @@ -503,6 +504,7 @@ function InfoPanel({ channels: ChannelInfo[]; clients: ClientInfo[]; onShowServerConnectionInfo: () => void; + onEditServer: () => void; }) { const t = useT(); if (!selected || selected.type === "server") { @@ -534,6 +536,9 @@ function InfoPanel({ + ); } @@ -2354,6 +2359,242 @@ function OptionsDialog({ ); } +const SERVER_EDIT_TABS = ["host", "misc", "transmission", "antiflood", "security", "log"] as const; + +interface ServerEditPayload { + name?: string; + welcomeMessage?: string; + password?: string; + maxClients?: number; + hostmessage?: string; + hostmessageMode?: string; + hostbannerUrl?: string; + hostbannerGfxUrl?: string; + hostbannerGfxIntervalSecs?: number; + hostbannerMode?: string; + hostbuttonTooltip?: string; + hostbuttonUrl?: string; + hostbuttonGfxUrl?: string; + nickname?: string; + phoneticName?: string; + codecEncryptionMode?: string; +} + +function ServerEditDialog({ + serverName, + welcomeMessage, + hostbannerGfxUrl, + onSave, + onClose, +}: { + serverName: string; + welcomeMessage: string; + hostbannerGfxUrl: string; + onSave: (payload: ServerEditPayload) => void; + onClose: () => void; +}) { + const t = useT(); + const backdrop = useBackdropDismiss(onClose); + const [tab, setTab] = useState<(typeof SERVER_EDIT_TABS)[number]>("host"); + + const [name, setName] = useState(serverName); + const [welcome, setWelcome] = useState(welcomeMessage); + const [clearPassword, setClearPassword] = useState(false); + const [password, setPassword] = useState(""); + const [hostmessage, setHostmessage] = useState(""); + const [hostmessageMode, setHostmessageMode] = useState(""); + const [hostbannerUrl, setHostbannerUrl] = useState(""); + const [bannerGfxUrl, setBannerGfxUrl] = useState(hostbannerGfxUrl); + const [hostbannerGfxInterval, setHostbannerGfxInterval] = useState(""); + const [hostbannerMode, setHostbannerMode] = useState(""); + const [hostbuttonTooltip, setHostbuttonTooltip] = useState(""); + const [hostbuttonUrl, setHostbuttonUrl] = useState(""); + const [hostbuttonGfxUrl, setHostbuttonGfxUrl] = useState(""); + const [maxClients, setMaxClients] = useState(""); + const [nickname, setNickname] = useState(""); + const [phoneticName, setPhoneticName] = useState(""); + const [codecEncryptionMode, setCodecEncryptionMode] = useState(""); + + const handleSave = () => { + const payload: ServerEditPayload = {}; + if (name.trim() && name !== serverName) payload.name = name.trim(); + if (welcome !== welcomeMessage) payload.welcomeMessage = welcome; + if (clearPassword) payload.password = ""; + else if (password) payload.password = password; + if (hostmessage) payload.hostmessage = hostmessage; + if (hostmessageMode) payload.hostmessageMode = hostmessageMode; + if (hostbannerUrl) payload.hostbannerUrl = hostbannerUrl; + if (bannerGfxUrl !== hostbannerGfxUrl) payload.hostbannerGfxUrl = bannerGfxUrl; + if (hostbannerGfxInterval) payload.hostbannerGfxIntervalSecs = Number(hostbannerGfxInterval) || 0; + if (hostbannerMode) payload.hostbannerMode = hostbannerMode; + if (hostbuttonTooltip) payload.hostbuttonTooltip = hostbuttonTooltip; + if (hostbuttonUrl) payload.hostbuttonUrl = hostbuttonUrl; + if (hostbuttonGfxUrl) payload.hostbuttonGfxUrl = hostbuttonGfxUrl; + if (maxClients) payload.maxClients = Number(maxClients) || 0; + if (nickname) payload.nickname = nickname; + if (phoneticName) payload.phoneticName = phoneticName; + if (codecEncryptionMode) payload.codecEncryptionMode = codecEncryptionMode; + onSave(payload); + onClose(); + }; + + return ( +
+
e.stopPropagation()}> +
+ {t("serverEdit.title")} + +
+
+
+ {SERVER_EDIT_TABS.map((id) => ( + + ))} +
+
+ {tab === "host" && ( + <> + +