From 330ecccadbe1e6b0d3818c18baa1cb40e37b1b5b Mon Sep 17 00:00:00 2001 From: Moepchi Date: Wed, 5 Aug 2026 11:21:52 +0200 Subject: [PATCH] Add kick and ban to the client context menu Wires up "Aus dem Channel kicken", "Vom Server kicken", and "Bannen" (previously disabled placeholders). Kick uses tsclientlib's typed Client::kick() builder; ban has no typed wrapper so it's built as a raw "banclient" command (same OutCommand::write_arg approach used for the connection-info requests). Both use send_with_result() so a missing-permission rejection surfaces as a visible error. "Channel-Gruppe zuweisen"/"Server-Gruppe zuweisen" remain disabled - they need the server's group list threaded through first, left for a follow-up. Co-Authored-By: Claude Sonnet 5 --- connector/src/main.rs | 81 ++++++++++++++++++++- gateway/src/index.ts | 12 ++++ gateway/src/ts3/connection.ts | 15 ++++ web/src/App.css | 21 ++++++ web/src/App.tsx | 129 +++++++++++++++++++++++++++++++++- web/src/i18n.tsx | 32 +++++++++ 6 files changed, 287 insertions(+), 3 deletions(-) diff --git a/connector/src/main.rs b/connector/src/main.rs index 7c5540d..3cc069f 100644 --- a/connector/src/main.rs +++ b/connector/src/main.rs @@ -18,7 +18,7 @@ use tsclientlib::{ data, ChannelId, ClientId, Connection, DisconnectOptions, Identity, MaxClients, MessageHandle, MessageTarget, Reason, StreamItem, TextMessageTargetMode, }; -use tsproto_packets::packets::{AudioData, CodecType, OutAudio}; +use tsproto_packets::packets::{AudioData, CodecType, Direction, Flags, OutAudio, OutCommand, PacketType}; /// 20ms frames at 48kHz, which is what TeamSpeak's Opus voice codec expects. const FRAME_SAMPLES: usize = 960; @@ -662,6 +662,85 @@ async fn run(args: Args) -> Result<()> { } Err(_) => emit(&Event::Error { message: format!("Invalid client id: {rest}") }), } + } else if let Some(rest) = l.strip_prefix("kickchannel ") { + let mut parts = rest.splitn(2, ' '); + let id_str = parts.next().unwrap_or(""); + let reason = parts.next().unwrap_or("").trim(); + match id_str.trim().parse::() { + Ok(id) => { + let client_id = ClientId(id); + let part = con.get_state()?.clients.get(&client_id).map(|c| { + let kick = c.kick(Reason::KickChannel); + if reason.is_empty() { kick } else { kick.set_reason_message(reason) } + }); + match part { + Some(part) => match part.send_with_result(&mut con) { + Ok(handle) => { + pending_messages.insert(handle, "Kick from channel".into()); + } + Err(e) => emit(&Event::Error { message: e.to_string() }), + }, + None => { + emit(&Event::Error { message: format!("Unknown client id: {id}") }) + } + } + } + Err(_) => emit(&Event::Error { message: format!("Invalid client id: {id_str}") }), + } + } else if let Some(rest) = l.strip_prefix("kickserver ") { + let mut parts = rest.splitn(2, ' '); + let id_str = parts.next().unwrap_or(""); + let reason = parts.next().unwrap_or("").trim(); + match id_str.trim().parse::() { + Ok(id) => { + let client_id = ClientId(id); + let part = con.get_state()?.clients.get(&client_id).map(|c| { + let kick = c.kick(Reason::KickServer); + if reason.is_empty() { kick } else { kick.set_reason_message(reason) } + }); + match part { + Some(part) => match part.send_with_result(&mut con) { + Ok(handle) => { + pending_messages.insert(handle, "Kick from server".into()); + } + Err(e) => emit(&Event::Error { message: e.to_string() }), + }, + None => { + emit(&Event::Error { message: format!("Unknown client id: {id}") }) + } + } + } + Err(_) => emit(&Event::Error { message: format!("Invalid client id: {id_str}") }), + } + } else if let Some(rest) = l.strip_prefix("banclient ") { + // Format: "banclient " - seconds=0 means permanent. + // No typed builder exists for banclient, so this is built as a raw command + // (same approach used for the connection-info requests above). + let mut parts = rest.splitn(3, ' '); + let id_str = parts.next().unwrap_or(""); + let seconds_str = parts.next().unwrap_or("0"); + let reason = parts.next().unwrap_or("").trim(); + match id_str.trim().parse::() { + Ok(id) => { + let seconds: u64 = seconds_str.trim().parse().unwrap_or(0); + let mut packet = + OutCommand::new(Direction::C2S, Flags::empty(), PacketType::Command, "banclient"); + packet.write_arg("clid", &id); + if seconds > 0 { + packet.write_arg("time", &seconds); + } + if !reason.is_empty() { + packet.write_arg("banreason", &reason); + } + match packet.send_with_result(&mut con) { + Ok(handle) => { + pending_messages.insert(handle, "Ban client".into()); + } + Err(e) => emit(&Event::Error { message: e.to_string() }), + } + } + Err(_) => emit(&Event::Error { message: format!("Invalid client id: {id_str}") }), + } } 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 31defab..ce37761 100644 --- a/gateway/src/index.ts +++ b/gateway/src/index.ts @@ -130,6 +130,18 @@ wss.on("connection", (socket: WebSocket) => { await connection?.getServerConnectionInfo(); break; } + case "kickFromChannel": { + await connection?.kickFromChannel(msg.clientId, msg.reason ?? ""); + break; + } + case "kickFromServer": { + await connection?.kickFromServer(msg.clientId, msg.reason ?? ""); + break; + } + case "banClient": { + await connection?.banClient(msg.clientId, msg.seconds ?? 0, msg.reason ?? ""); + break; + } case "disconnect": { await connection?.disconnect(msg.message ?? ""); break; diff --git a/gateway/src/ts3/connection.ts b/gateway/src/ts3/connection.ts index 4f0cfb2..3827f75 100644 --- a/gateway/src/ts3/connection.ts +++ b/gateway/src/ts3/connection.ts @@ -312,6 +312,21 @@ export class Ts3Connection { this.child?.stdin.write(`serverconninfo\n`); } + async kickFromChannel(clientId: number, reason: string): Promise { + const sanitized = reason.replace(/[\r\n]+/g, " ").trim(); + this.child?.stdin.write(`kickchannel ${clientId} ${sanitized}\n`); + } + + async kickFromServer(clientId: number, reason: string): Promise { + const sanitized = reason.replace(/[\r\n]+/g, " ").trim(); + this.child?.stdin.write(`kickserver ${clientId} ${sanitized}\n`); + } + + async banClient(clientId: number, seconds: number, reason: string): Promise { + const sanitized = reason.replace(/[\r\n]+/g, " ").trim(); + this.child?.stdin.write(`banclient ${clientId} ${Math.max(0, Math.floor(seconds))} ${sanitized}\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.css b/web/src/App.css index 863526f..b565178 100644 --- a/web/src/App.css +++ b/web/src/App.css @@ -1632,6 +1632,27 @@ background: linear-gradient(var(--button-bg-hover-1), var(--button-bg-hover-2)); } +.ts-ban-compose { + flex-wrap: wrap; + max-width: 380px; +} + +.ts-ban-duration-field { + display: flex; + align-items: center; + gap: 0.4rem; + font-size: 12px; + color: var(--text-muted); +} + +.ts-ban-duration-field select { + background: var(--input-bg); + color: var(--input-text); + border: 1px solid var(--border); + border-radius: 3px; + padding: 0.2rem 0.4rem; +} + .ts-poke-notice { display: flex; align-items: center; diff --git a/web/src/App.tsx b/web/src/App.tsx index 5df602d..22ded0a 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -2387,6 +2387,15 @@ function AppInner() { const [pokeTarget, setPokeTarget] = useState<{ id: number; name: string } | null>(null); const [pokeMessage, setPokeMessage] = useState(""); const pokeBackdrop = useBackdropDismiss(() => setPokeTarget(null)); + const [kickTarget, setKickTarget] = useState<{ id: number; name: string; scope: "channel" | "server" } | null>( + null + ); + const [kickReason, setKickReason] = useState(""); + const kickBackdrop = useBackdropDismiss(() => setKickTarget(null)); + const [banTarget, setBanTarget] = useState<{ id: number; name: string } | null>(null); + const [banReason, setBanReason] = useState(""); + const [banSeconds, setBanSeconds] = useState("0"); + const banBackdrop = useBackdropDismiss(() => setBanTarget(null)); const [demoForceMobile, setDemoForceMobile] = useState(false); const [clientContextMenu, setClientContextMenu] = useState<{ x: number; @@ -3517,6 +3526,36 @@ function AppInner() { setPokeMessage(""); }; + const handleConfirmKick = () => { + if (!kickTarget) return; + socketRef.current?.send( + JSON.stringify({ + type: kickTarget.scope === "channel" ? "kickFromChannel" : "kickFromServer", + clientId: kickTarget.id, + reason: kickReason, + }) + ); + logClient("info", "Moderation", `Kicked ${kickTarget.name} from ${kickTarget.scope}`); + setKickTarget(null); + setKickReason(""); + }; + + const handleConfirmBan = () => { + if (!banTarget) return; + socketRef.current?.send( + JSON.stringify({ + type: "banClient", + clientId: banTarget.id, + seconds: Number(banSeconds) || 0, + reason: banReason, + }) + ); + logClient("info", "Moderation", `Banned ${banTarget.name}`); + setBanTarget(null); + setBanReason(""); + setBanSeconds("0"); + }; + const handleClosePrivateChat = (clientId: number) => { setPmThreads((prev) => { const next = { ...prev }; @@ -4284,6 +4323,58 @@ function AppInner() { )} + {kickTarget && ( +
+
e.stopPropagation()}> + + 👢 {t("kick.title")} {kickTarget.name} + + setKickReason(e.target.value)} + onKeyDown={(e) => { + if (e.key === "Enter") handleConfirmKick(); + if (e.key === "Escape") setKickTarget(null); + }} + placeholder={t("kick.reasonPlaceholder")} + /> + + +
+
+ )} + + {banTarget && ( +
+
e.stopPropagation()}> + + 🚫 {t("ban.title")} {banTarget.name} + + + setBanReason(e.target.value)} + onKeyDown={(e) => { + if (e.key === "Enter") handleConfirmBan(); + if (e.key === "Escape") setBanTarget(null); + }} + placeholder={t("ban.reasonPlaceholder")} + /> + + +
+
+ )} + {pokes.map((poke) => (
@@ -4372,11 +4463,45 @@ function AppInner() { {!clientContextMenu.isSelf && ( <>
- + - diff --git a/web/src/i18n.tsx b/web/src/i18n.tsx index d21d17e..fb1b8d8 100644 --- a/web/src/i18n.tsx +++ b/web/src/i18n.tsx @@ -303,6 +303,21 @@ const translations: Record> = { "poke.pokedYou": "hat dich angestupst", "poke.dismiss": "Schließen", + "kick.title": "Kicken:", + "kick.reasonPlaceholder": "Grund (optional)...", + "kick.confirm": "Kicken", + "kick.cancel": "Abbrechen", + + "ban.title": "Bannen:", + "ban.reasonPlaceholder": "Grund (optional)...", + "ban.duration": "Dauer:", + "ban.durationPermanent": "Permanent", + "ban.duration1h": "1 Stunde", + "ban.duration1d": "1 Tag", + "ban.duration1w": "1 Woche", + "ban.confirm": "Bannen", + "ban.cancel": "Abbrechen", + "clientContext.privateChat": "Privater Chat", "clientContext.poke": "Anstupsen", "clientContext.copyName": "Namen kopieren", @@ -311,6 +326,7 @@ const translations: Record> = { "clientContext.removeWhisperTarget": "Als Whisper-Ziel entfernen", "clientContext.assignChannelGroup": "Channel-Gruppe zuweisen", "clientContext.assignServerGroup": "Server-Gruppe zuweisen", + "clientContext.kickChannel": "Aus dem Channel kicken", "clientContext.kick": "Vom Server kicken", "clientContext.ban": "Bannen", "clientContext.notSupported": "Im Web-Client noch nicht unterstützt", @@ -642,6 +658,21 @@ const translations: Record> = { "poke.pokedYou": "poked you", "poke.dismiss": "Dismiss", + "kick.title": "Kick:", + "kick.reasonPlaceholder": "Reason (optional)...", + "kick.confirm": "Kick", + "kick.cancel": "Cancel", + + "ban.title": "Ban:", + "ban.reasonPlaceholder": "Reason (optional)...", + "ban.duration": "Duration:", + "ban.durationPermanent": "Permanent", + "ban.duration1h": "1 hour", + "ban.duration1d": "1 day", + "ban.duration1w": "1 week", + "ban.confirm": "Ban", + "ban.cancel": "Cancel", + "clientContext.privateChat": "Private chat", "clientContext.poke": "Poke", "clientContext.copyName": "Copy name", @@ -650,6 +681,7 @@ const translations: Record> = { "clientContext.removeWhisperTarget": "Remove as whisper target", "clientContext.assignChannelGroup": "Assign channel group", "clientContext.assignServerGroup": "Assign server group", + "clientContext.kickChannel": "Kick from channel", "clientContext.kick": "Kick from server", "clientContext.ban": "Ban", "clientContext.notSupported": "Not yet supported in the web client",