diff --git a/connector/src/main.rs b/connector/src/main.rs index bc75b69..095a0dc 100644 --- a/connector/src/main.rs +++ b/connector/src/main.rs @@ -11,7 +11,7 @@ use serde::Serialize; use tokio::io::{AsyncBufReadExt, BufReader}; use tsclientlib::audio::AudioHandler; -use tsclientlib::events::Event as BookEvent; +use tsclientlib::events::{Event as BookEvent, PropertyId, PropertyValue}; use tsclientlib::messages::c2s::{OutClientPokeRequestPart, OutSendTextMessagePart}; use tsclientlib::prelude::*; use tsclientlib::{ @@ -70,6 +70,26 @@ struct ClientInfo { is_channel_commander: bool, } +/// 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 +/// shows inline in the server chat. `rename_all = "camelCase"` covers both +/// the `kind` tag values and field names, so this needs no remapping on the +/// gateway side (unlike the snake_case variants below). +#[derive(Serialize)] +#[serde(tag = "kind", rename_all = "camelCase")] +enum ServerLogEntry { + ClientJoin { client: String, channel: String }, + ClientLeave { client: String }, + ClientChannelSwitch { client: String, from_channel: String, to_channel: String }, + ClientChannelGroupAssigned { client: String, group: String }, + ChannelCreated { channel: String }, + ChannelDeleted { channel: String }, + ChannelEdited { channel: String }, + ServerEdited, + PermissionError { action: String }, +} + #[derive(Serialize)] #[serde(tag = "type")] enum Event { @@ -108,12 +128,118 @@ enum Event { Disconnected { reason: String }, #[serde(rename = "error")] Error { message: String }, + /// Server-tab log notification (client join/leave/switch, channel group + /// assignment, channel/server changes, own permission errors). + #[serde(rename = "serverLog")] + ServerLog { + #[serde(flatten)] + entry: ServerLogEntry, + }, } fn emit(event: &Event) { println!("{}", serde_json::to_string(event).unwrap()); } +fn emit_server_log(entry: ServerLogEntry) { + emit(&Event::ServerLog { entry }); +} + +/// Translates a single book-state diff into a Server-tab log entry, when it's +/// one of the changes worth surfacing there (client join/leave/switch, +/// channel group assignment, channel/server changes). Most diffs (e.g. +/// per-field client updates like mute state) are intentionally not logged - +/// only the categories the native client's server log shows for these. +fn log_book_event(con: &data::Connection, event: &BookEvent) { + match event { + BookEvent::PropertyAdded { id: PropertyId::Client(client_id), .. } => { + if let Some(client) = con.clients.get(client_id) { + let channel = con.channels.get(&client.channel).map(|c| c.name.clone()).unwrap_or_default(); + emit_server_log(ServerLogEntry::ClientJoin { client: client.name.clone(), channel }); + } + } + BookEvent::PropertyRemoved { id: PropertyId::Client(_), old: PropertyValue::Client(client), .. } => { + emit_server_log(ServerLogEntry::ClientLeave { client: client.name.clone() }); + } + BookEvent::PropertyAdded { id: PropertyId::Channel(channel_id), .. } => { + if let Some(channel) = con.channels.get(channel_id) { + emit_server_log(ServerLogEntry::ChannelCreated { channel: channel.name.clone() }); + } + } + BookEvent::PropertyRemoved { id: PropertyId::Channel(_), old: PropertyValue::Channel(channel), .. } => { + emit_server_log(ServerLogEntry::ChannelDeleted { channel: channel.name.clone() }); + } + BookEvent::PropertyChanged { id: PropertyId::ClientChannel(client_id), old: PropertyValue::ChannelId(old_channel), .. } => { + if let Some(client) = con.clients.get(client_id) { + let from_channel = con.channels.get(old_channel).map(|c| c.name.clone()).unwrap_or_default(); + let to_channel = con.channels.get(&client.channel).map(|c| c.name.clone()).unwrap_or_default(); + emit_server_log(ServerLogEntry::ClientChannelSwitch { + client: client.name.clone(), + from_channel, + to_channel, + }); + } + } + BookEvent::PropertyChanged { id: PropertyId::ClientChannelGroup(client_id), .. } => { + if let Some(client) = con.clients.get(client_id) { + let group = + con.channel_groups.get(&client.channel_group).map(|g| g.name.clone()).unwrap_or_default(); + emit_server_log(ServerLogEntry::ClientChannelGroupAssigned { client: client.name.clone(), group }); + } + } + BookEvent::PropertyChanged { + id: + PropertyId::ChannelName(channel_id) + | PropertyId::ChannelTopic(channel_id) + | PropertyId::ChannelMaxClients(channel_id) + | PropertyId::ChannelMaxFamilyClients(channel_id) + | PropertyId::ChannelHasPassword(channel_id) + | PropertyId::ChannelNeededTalkPower(channel_id) + | PropertyId::ChannelCodec(channel_id) + | PropertyId::ChannelCodecQuality(channel_id) + | PropertyId::ChannelIsDefault(channel_id) + | PropertyId::ChannelDeleteDelay(channel_id) + | PropertyId::ChannelPhoneticName(channel_id) + | PropertyId::ChannelIsPrivate(channel_id) + | PropertyId::ChannelForcedSilence(channel_id) + | PropertyId::ChannelOrder(channel_id) + | PropertyId::ChannelParent(channel_id), + .. + } => { + if let Some(channel) = con.channels.get(channel_id) { + emit_server_log(ServerLogEntry::ChannelEdited { channel: channel.name.clone() }); + } + } + BookEvent::PropertyChanged { + id: + PropertyId::ServerName + | PropertyId::ServerWelcomeMessage + | PropertyId::ServerHostmessage + | PropertyId::ServerHostmessageMode + | PropertyId::ServerHostbannerUrl + | PropertyId::ServerHostbannerGfxUrl + | PropertyId::ServerHostbannerGfxInterval + | PropertyId::ServerHostbannerMode + | PropertyId::ServerHostbuttonTooltip + | PropertyId::ServerHostbuttonUrl + | PropertyId::ServerHostbuttonGfxUrl + | PropertyId::ServerPhoneticName + | PropertyId::ServerIcon + | PropertyId::ServerMaxClients + | PropertyId::ServerAskForPrivilegekey + | PropertyId::ServerDefaultServerGroup + | PropertyId::ServerDefaultChannelGroup + | PropertyId::ServerCodecEncryptionMode + | PropertyId::ServerTempChannelDefaultDeleteDelay + | PropertyId::ServerPrioritySpeakerDimmModificator, + .. + } => { + emit_server_log(ServerLogEntry::ServerEdited); + } + _ => {} + } +} + /// `Channel::order` is not a plain position, it's the id of the preceding /// sibling channel (0 = first child of its parent) - effectively a linked /// list per parent. This resolves that into a plain 0-based sequence index @@ -340,8 +466,11 @@ async fn run(args: Args) -> Result<()> { let own = &state.clients[&state.own_client]; own.client_move(ChannelId(id)) }; - if let Err(e) = part.send(&mut con) { - emit(&Event::Error { message: e.to_string() }); + match part.send_with_result(&mut con) { + Ok(handle) => { + pending_messages.insert(handle, "Channel switch".into()); + } + Err(e) => emit(&Event::Error { message: e.to_string() }), } } Err(_) => emit(&Event::Error { message: format!("Invalid channel id: {rest}") }), @@ -518,6 +647,8 @@ async fn run(args: Args) -> Result<()> { emit(&Event::Poke { from: invoker.name.clone(), message: message.clone() }); } } + } else { + log_book_event(con.get_state()?, event); } } emit(&snapshot(con.get_state()?)); @@ -539,7 +670,13 @@ async fn run(args: Args) -> Result<()> { Some(Ok(StreamItem::MessageResult(handle, result))) => { if let Some(label) = pending_messages.remove(&handle) { if let Err(e) = result { - emit(&Event::Error { message: format!("{label} failed: {e}") }); + if e.missing_permission.is_some() { + emit(&Event::ServerLog { + entry: ServerLogEntry::PermissionError { action: label }, + }); + } else { + emit(&Event::Error { message: format!("{label} failed: {e}") }); + } } } } diff --git a/gateway/src/ts3/connection.ts b/gateway/src/ts3/connection.ts index bffb0dc..70d58f9 100644 --- a/gateway/src/ts3/connection.ts +++ b/gateway/src/ts3/connection.ts @@ -33,6 +33,17 @@ export interface ClientInfo { isChannelCommander: boolean; } +export type ServerLogEntry = + | { kind: "clientJoin"; client: string; channel: string } + | { kind: "clientLeave"; client: string } + | { kind: "clientChannelSwitch"; client: string; fromChannel: string; toChannel: string } + | { kind: "clientChannelGroupAssigned"; client: string; group: string } + | { kind: "channelCreated"; channel: string } + | { kind: "channelDeleted"; channel: string } + | { kind: "channelEdited"; channel: string } + | { kind: "serverEdited" } + | { kind: "permissionError"; action: string }; + export type Ts3ConnectionEvent = | { type: "connected"; @@ -51,7 +62,8 @@ export type Ts3ConnectionEvent = | { type: "audioOut"; pcm: string } | { type: "talkers"; clients: number[] } | { type: "disconnected"; reason: string } - | { type: "error"; message: string }; + | { type: "error"; message: string } + | ({ type: "serverLog" } & ServerLogEntry); export interface Ts3ConnectOptions { host: string; @@ -134,7 +146,8 @@ export class Ts3Connection { | { type: "audioOut"; pcm: string } | { type: "talkers"; clients: number[] } | { type: "disconnected"; reason: string } - | { type: "error"; message: string }; + | { type: "error"; message: string } + | ({ type: "serverLog" } & ServerLogEntry); if (event.type === "connected") { this.emit({ diff --git a/web/src/App.css b/web/src/App.css index 349bd43..71dd4d3 100644 --- a/web/src/App.css +++ b/web/src/App.css @@ -600,6 +600,11 @@ color: var(--accent); } +.ts-chat-line-log { + color: var(--text-muted); + font-style: italic; +} + .ts-chat-input-row { flex-shrink: 0; display: flex; diff --git a/web/src/App.tsx b/web/src/App.tsx index 5603657..afb813e 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -371,6 +371,9 @@ function InfoPanel({ interface ChatEntry { from: string; message: string; + /** Set for server-log notifications (join/leave/switch/etc.) so they render + * without a "from:" prefix, like the native client's server tab. */ + isLog?: boolean; } interface PmMessage { @@ -1709,6 +1712,44 @@ function AppInner() { void playSound("poke"); break; } + case "serverLog": { + let message: string; + switch (data.kind) { + case "clientJoin": + message = t("serverLog.clientJoin", { client: data.client, channel: data.channel }); + break; + case "clientLeave": + message = t("serverLog.clientLeave", { client: data.client }); + break; + case "clientChannelSwitch": + message = t("serverLog.clientChannelSwitch", { + client: data.client, + fromChannel: data.fromChannel, + toChannel: data.toChannel, + }); + break; + case "clientChannelGroupAssigned": + message = t("serverLog.clientChannelGroupAssigned", { client: data.client, group: data.group }); + break; + case "channelCreated": + message = t("serverLog.channelCreated", { channel: data.channel }); + break; + case "channelDeleted": + message = t("serverLog.channelDeleted", { channel: data.channel }); + break; + case "channelEdited": + message = t("serverLog.channelEdited", { channel: data.channel }); + break; + case "serverEdited": + message = t("serverLog.serverEdited"); + break; + case "permissionError": + message = t("serverLog.permissionError", { action: data.action }); + break; + } + setServerChat((prev) => [...prev, { from: "", message, isLog: true }]); + break; + } case "disconnected": { const wasConnected = hasConnectedRef.current; hasConnectedRef.current = false; @@ -2575,11 +2616,17 @@ function AppInner() { )) : activeTab === "server" - ? serverChat.map((entry, i) => ( -