diff --git a/ts3-client/AUDIT-STRUCTURE.md b/ts3-client/AUDIT-STRUCTURE.md index f058990..672332b 100644 --- a/ts3-client/AUDIT-STRUCTURE.md +++ b/ts3-client/AUDIT-STRUCTURE.md @@ -10,8 +10,12 @@ Items are worked through in the order below. Status: open, done (commit), or clo |---|---|---| | 1 | Request failures reach frontends as strings, so they guess the kind by substring (`BanListDialog`, `ServerLogDialog`) and core needs a sentinel message (`SEARCH_NOT_PERMITTED`); success is signalled by a `null` error, here and in Android's `ServerController` (`String?`) | done (`0552ed8`): requests return `CompletableFuture`, failing with `RequestException` (kind + message); `Replies`/`Edt` split success and failure; Android uses `Result` | | 2 | Client properties parsed twice: `ConnectionEventHandler.onClientJoin` sets every field, `onClientChanged` the same ones again; channels already share `applyChannelProperties` | done (`e255a46`): one `applyClientProperties`. It also fixed a joining channel commander shown as none, and picks up the description a client enters with | -| 3 | What we may do is answered in five places: `ServerSession.can*`, `ServerModel.can*/permits/mayHavePower`, `TeamspeakConnection.canSendOfflineMessages/canTransferFiles`, and raw permission names in Swing dialogs and `ServerController.canCreateBans` | done: `Permissions` owns the table, the rules and one named question per capability; `ServerSession.permissions()` is `NONE` while disconnected; no permission names outside core except channel-permission editing | -| 4 | `TeamspeakConnection` (2,157 lines) still does lifecycle, mic/speaker, recording, chat logs, log/sound text, contacts, avatars, icons, file transfers, myTeamSpeak and job plumbing. The `*Admin` helpers take the whole connection, which forwards ~90 methods to them | open | +| 3 | What we may do is answered in five places: `ServerSession.can*`, `ServerModel.can*/permits/mayHavePower`, `TeamspeakConnection.canSendOfflineMessages/canTransferFiles`, and raw permission names in Swing dialogs and `ServerController.canCreateBans` | done (`13e3a62`): `Permissions` owns the table, the rules and one named question per capability; `ServerSession.permissions()` is `NONE` while disconnected; no permission names outside core except channel-permission editing | +| 4 | `TeamspeakConnection` (2,157 lines) still does lifecycle, mic/speaker, recording, chat logs, log/sound text, contacts, avatars, icons, file transfers, myTeamSpeak and job plumbing. The `*Admin` helpers take the whole connection, which forwards ~90 methods to them | in progress, in sub-steps: | +| 4a | … chat log and what we tell the user (log lines, sounds, links and sound variables) | done (`c28c2ce`): `ChatLog`, `Announcer` | +| 4b | … request plumbing into its own class; `*Admin` helpers on narrow dependencies instead of the whole connection | open | +| 4c | … frontends reach the admin helpers directly instead of ~40 one-line forwarders | open | +| 4d | … our own state (mic, speakers, away, talk request, recording) out of the connection | open | | 5 | Two front doors: frontends use both `ServerSession` and `session.connection()` with no rule for which | open | | 6 | Menu bar, toolbar and hotkeys each implement the same commands: `MainMenuBar.Listener` (~50 methods, ~210 lines of forwarding in `MainFrame`), `MainToolbar.Listener`, `HotkeyActions` | open | | 7 | The admin list dialogs (bans, complaints, offline messages, privilege keys, server log) repeat the load/status/table-model scaffolding | open | diff --git a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/net/Announcer.java b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/net/Announcer.java new file mode 100644 index 0000000..a47c893 --- /dev/null +++ b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/net/Announcer.java @@ -0,0 +1,113 @@ +package eu.lixko.ts3j.client.net; + +import eu.lixko.ts3j.client.sound.SoundEvent; +import eu.lixko.ts3j.client.sound.SoundNotifier; +import eu.lixko.ts3j.client.text.TsLink; + +import java.util.Map; +import java.util.function.BooleanSupplier; + +/** + * Tells the user what happens on a connection the way TeamSpeak does: a line in the server + * tab's log, with the channels and clients in it linked, and the sound pack's entry, with + * the names it may speak. + */ +final class Announcer { + + private final ServerModel model; + private final SoundNotifier sounds; + private final ConnectionListener ui; + private final ChatLog chatLog; + /** Speakers muted: only actions marked important are still heard. */ + private final BooleanSupplier deafened; + private final BooleanSupplier connected; + + Announcer(ServerModel model, SoundNotifier sounds, ConnectionListener ui, ChatLog chatLog, + BooleanSupplier deafened, BooleanSupplier connected) { + this.model = model; + this.sounds = sounds; + this.ui = ui; + this.chatLog = chatLog; + this.deafened = deafened; + this.connected = connected; + } + + /** Plays the sound pack's entry for an action. */ + void sound(SoundEvent event) { + sounds.fire(event, deafened.getAsBoolean()); + } + + void sound(SoundEvent event, Map variables) { + sounds.fire(event, deafened.getAsBoolean(), variables); + } + + /** Records a line in the server tab's log; nothing once the connection is gone. */ + void log(String message) { + if (connected.getAsBoolean()) chatLog.serverEvent(message); + } + + /** Reports a failure to the UI and plays the matching server sound. */ + void error(String prefix, Exception e) { + sound(RequestException.isPermissionError(e) + ? SoundEvent.SERVER_INSUFFICIENT_PERMISSIONS + : SoundEvent.SERVER_ERROR, serverVars()); + ui.onError(prefix + RequestException.rootMessage(e)); + } + + String channelName(int channelId) { + ChannelNode ch = model.getChannel(channelId); + return ch != null ? ch.name : "channel #" + channelId; + } + + /** + * A channel's name as the log wants it: TeamSpeak's own {@code channel://} link, which + * the chat log turns into the same clickable entry a dragged-in channel produces. A + * channel we no longer know (a deleted one, say) is named in plain text instead. + */ + String channelLink(int channelId) { + ChannelNode ch = model.getChannel(channelId); + return ch != null ? TsLink.channelBBCode(channelId, ch.name) : channelName(channelId); + } + + /** The same for a client, named by the model. */ + String clientLink(int clientId) { + ClientEntry c = model.getClient(clientId); + return c != null ? clientLink(clientId, c.uniqueId, c.nickname) : "Client " + clientId; + } + + /** The same for a client an event describes, which may already be gone from the model. */ + String clientLink(int clientId, String uniqueId, String nickname) { + if (clientId <= 0 || nickname == null || nickname.isEmpty()) return orEmpty(nickname); + return TsLink.clientBBCode(clientId, orEmpty(uniqueId), nickname); + } + + /** The placeholder values a pack may reference for an action involving a client. */ + Map clientVars(int clientId, String fallbackName) { + ClientEntry c = model.getClient(clientId); + Map vars = SoundNotifier.vars( + "servername", model.getServerName(), + "clientname", c != null ? c.nickname : orEmpty(fallbackName), + "clientuid", c != null ? c.uniqueId : ""); + if (c != null) { + ChannelNode ch = model.getChannel(c.channelId); + if (ch != null) vars.put("channelname", ch.name); + } + return vars; + } + + Map channelVars(int channelId, String invokerName) { + ChannelNode ch = model.getChannel(channelId); + return SoundNotifier.vars( + "servername", model.getServerName(), + "channelname", ch != null ? ch.name : "", + "clientname", orEmpty(invokerName)); + } + + Map serverVars() { + return SoundNotifier.vars("servername", model.getServerName()); + } + + private static String orEmpty(String s) { + return s == null ? "" : s; + } +} diff --git a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/net/ChatLog.java b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/net/ChatLog.java new file mode 100644 index 0000000..6105881 --- /dev/null +++ b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/net/ChatLog.java @@ -0,0 +1,122 @@ +package eu.lixko.ts3j.client.net; + +import eu.lixko.ts3j.client.chatlog.ChatLogScope; +import eu.lixko.ts3j.client.chatlog.ChatLogStore; +import eu.lixko.ts3j.client.config.Settings; + +import java.io.IOException; +import java.nio.file.Paths; +import java.util.List; + +/** + * A connection's chat log on disk: opened once the server is known, it hands the frontend + * the history and keeps what is said, and the server tab's log lines, from then on. Without + * one (turned off, or unavailable) everything still reaches the frontend, only unkept. + */ +final class ChatLog { + + private final Settings settings; + private final ConnectionListener ui; + private volatile ChatLogStore store; + + ChatLog(Settings settings, ConnectionListener ui) { + this.settings = settings; + this.ui = ui; + } + + /** + * Opens the log for the server with {@code serverUniqueId} and hands the frontend its + * server and channel history. + */ + void open(String serverUniqueId) { + store = null; + if (!settings.chatLogs) return; + try { + if (serverUniqueId == null) { + ui.onStatus("Chat logs unavailable: the server's unique id is not known"); + return; + } + String dir = settings.chatLogDir == null ? "" : settings.chatLogDir.trim(); + // Android < API 34 lacks Path.of; original: + // ChatLogStore logs = new ChatLogStore(dir.isEmpty() ? ChatLogStore.defaultRoot() : Path.of(dir), uniqueId); + ChatLogStore logs = new ChatLogStore(dir.isEmpty() ? ChatLogStore.defaultRoot() : Paths.get(dir), serverUniqueId); + store = logs; + logs.begin(); + List serverHistory = logs.serverHtmlHistory(); + List channelHistory = logs.channelHtmlHistory(); + ui.onChatHistory(ConnectionListener.ChatScope.SERVER, serverHistory); + ui.onChatHistory(ConnectionListener.ChatScope.CHANNEL, channelHistory); + ui.onStatus("Loaded chat logs from " + logs.serverDir() + + " (" + serverHistory.size() + " server, " + channelHistory.size() + " channel)"); + } catch (Exception e) { + store = null; + ui.onStatus("Chat logs unavailable: " + RequestException.rootMessage(e)); + } + } + + void close() { + store = null; + } + + /** Hands the frontend what was said with the client with {@code uniqueId} before. */ + void loadPrivateHistory(int clientId, String name, String uniqueId) { + ChatLogStore logs = store; + if (logs == null || uniqueId == null || uniqueId.isBlank()) return; + try { + ui.onPrivateChatHistory(clientId, name, logs.privateHtmlHistory(uniqueId)); + } catch (IOException e) { + ui.onStatus("Private chat log unavailable: " + RequestException.rootMessage(e)); + } + } + + void received(ConnectionListener.ChatScope scope, int fromClientId, String fromUniqueId, String fromName, + String message) { + ChatLogStore logs = store; + if (logs == null) return; + try { + logs.appendMessage(scope(scope), false, fromClientId, fromUniqueId, fromName, message); + } catch (IOException e) { + ui.onStatus("Could not write chat log: " + RequestException.rootMessage(e)); + } + } + + /** What we said; a private message is kept with {@code peerUniqueId}'s log, and dropped without one. */ + void sent(ConnectionListener.ChatScope scope, int selfClientId, String selfUniqueId, String selfName, + String peerUniqueId, String message) { + ChatLogStore logs = store; + if (logs == null) return; + try { + if (scope == ConnectionListener.ChatScope.PRIVATE) { + if (peerUniqueId == null || peerUniqueId.isBlank()) return; + logs.appendPrivateMessage(peerUniqueId, true, selfClientId, selfUniqueId, selfName, message); + } else { + logs.appendMessage(scope(scope), true, selfClientId, selfUniqueId, selfName, message); + } + } catch (IOException e) { + ui.onStatus("Could not write chat log: " + RequestException.rootMessage(e)); + } + } + + /** Shows a line in the server tab's log, the way native TS3 reports server activity, and keeps it. */ + void serverEvent(String message) { + ChatLogStore logs = store; + if (logs == null) { + ui.onServerLog(message); + return; + } + try { + ui.onServerLogHtml(logs.appendServerEvent(message)); + } catch (IOException e) { + ui.onServerLog(message); + ui.onStatus("Could not write server log: " + RequestException.rootMessage(e)); + } + } + + private static ChatLogScope scope(ConnectionListener.ChatScope scope) { + return switch (scope) { + case SERVER -> ChatLogScope.SERVER; + case CHANNEL -> ChatLogScope.CHANNEL; + case PRIVATE -> ChatLogScope.PRIVATE; + }; + } +} diff --git a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/net/ConnectionEventHandler.java b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/net/ConnectionEventHandler.java index e86c9f9..04c9b8a 100644 --- a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/net/ConnectionEventHandler.java +++ b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/net/ConnectionEventHandler.java @@ -54,7 +54,7 @@ final class ConnectionEventHandler implements TS3Listener { if (e.getClientId() == conn.getSelfClientId()) { announceOwnRemoval(safeInt(e, "reasonid"), e); } else if (gone == null || !gone.isQuery()) { - String name = conn.clientLink(e.getClientId()); + String name = conn.announce.clientLink(e.getClientId()); announceClientLeft(e); logClientLeft(e, name); } @@ -75,7 +75,7 @@ final class ConnectionEventHandler implements TS3Listener { logOwnMove(e, from, e.getTargetChannelId()); } else if (!c.isQuery()) { announceClientMoved(safeInt(e, "reasonid"), e.getClientId(), from, e.getTargetChannelId()); - logClientMoved(e, conn.clientLink(c.id), from, e.getTargetChannelId()); + logClientMoved(e, conn.announce.clientLink(c.id), from, e.getTargetChannelId()); } conn.ui.onModelChanged(); } @@ -83,25 +83,25 @@ final class ConnectionEventHandler implements TS3Listener { /** A client became visible to us, logged the way native TS3's server tab does. */ private void logClientEntered(ClientJoinEvent e) { - String name = conn.clientLink(e.getClientId(), + String name = conn.announce.clientLink(e.getClientId(), TeamspeakConnection.orEmpty(e.getUniqueClientIdentifier()), e.getClientNickname()); // A client connecting also comes with "switched", only from no channel at all. int reason = safeInt(e, "reasonid"); if (reason == REASON_SWITCHED && e.getClientFromId() == 0) reason = -1; switch (reason) { case REASON_MOVED: - conn.log(name + " appears, coming from channel \"" + conn.channelLink(e.getClientFromId()) + "\""); + conn.announce.log(name + " appears, coming from channel \"" + conn.announce.channelLink(e.getClientFromId()) + "\""); break; case REASON_CHANNEL_KICK: - conn.log(name + " appears, was kicked from channel \"" + conn.channelLink(e.getClientFromId()) + conn.announce.log(name + " appears, was kicked from channel \"" + conn.announce.channelLink(e.getClientFromId()) + "\" by " + invokerName(e)); break; case REASON_SWITCHED: - conn.log(name + " switched to channel \"" + conn.channelLink(e.getClientTargetId()) - + "\", coming from channel \"" + conn.channelLink(e.getClientFromId()) + "\""); + conn.announce.log(name + " switched to channel \"" + conn.announce.channelLink(e.getClientTargetId()) + + "\", coming from channel \"" + conn.announce.channelLink(e.getClientFromId()) + "\""); break; default: - conn.log(name + " connected to channel \"" + conn.channelLink(e.getClientTargetId()) + "\""); + conn.announce.log(name + " connected to channel \"" + conn.announce.channelLink(e.getClientTargetId()) + "\""); } } @@ -114,26 +114,26 @@ final class ConnectionEventHandler implements TS3Listener { if (reason == REASON_SWITCHED && e.getClientTargetId() == 0) reason = -1; switch (reason) { case REASON_TIMEOUT: - conn.log(name + " dropped (ping timeout)"); + conn.announce.log(name + " dropped (ping timeout)"); break; case REASON_SERVER_KICK: - conn.log(name + " was kicked from the server by " + invokerName(e) + suffix); + conn.announce.log(name + " was kicked from the server by " + invokerName(e) + suffix); break; case REASON_BAN: - conn.log(name + " was banned from the server by " + invokerName(e) + suffix); + conn.announce.log(name + " was banned from the server by " + invokerName(e) + suffix); break; case REASON_CHANNEL_KICK: - conn.log(name + " left: was kicked to channel \"" + conn.channelLink(e.getClientTargetId()) + conn.announce.log(name + " left: was kicked to channel \"" + conn.announce.channelLink(e.getClientTargetId()) + "\" by " + invokerName(e) + suffix); break; case REASON_MOVED: - conn.log(name + " left, heading to channel \"" + conn.channelLink(e.getClientTargetId()) + "\""); + conn.announce.log(name + " left, heading to channel \"" + conn.announce.channelLink(e.getClientTargetId()) + "\""); break; case REASON_SWITCHED: - conn.log(name + " left, switched to channel \"" + conn.channelLink(e.getClientTargetId()) + "\""); + conn.announce.log(name + " left, switched to channel \"" + conn.announce.channelLink(e.getClientTargetId()) + "\""); break; default: - conn.log(name + " disconnected"); + conn.announce.log(name + " disconnected"); } } @@ -143,16 +143,16 @@ final class ConnectionEventHandler implements TS3Listener { String suffix = reasonMsg.isEmpty() ? "" : " (" + reasonMsg + ")"; switch (safeInt(e, "reasonid")) { case REASON_MOVED: - conn.log(name + " was moved from channel \"" + conn.channelLink(fromChannel) + "\" to \"" - + conn.channelLink(toChannel) + "\" by " + invokerName(e)); + conn.announce.log(name + " was moved from channel \"" + conn.announce.channelLink(fromChannel) + "\" to \"" + + conn.announce.channelLink(toChannel) + "\" by " + invokerName(e)); break; case REASON_CHANNEL_KICK: - conn.log(name + " was kicked from channel \"" + conn.channelLink(fromChannel) + "\" to \"" - + conn.channelLink(toChannel) + "\" by " + invokerName(e) + suffix); + conn.announce.log(name + " was kicked from channel \"" + conn.announce.channelLink(fromChannel) + "\" to \"" + + conn.announce.channelLink(toChannel) + "\" by " + invokerName(e) + suffix); break; default: - conn.log(name + " switched from channel \"" + conn.channelLink(fromChannel) + "\" to \"" - + conn.channelLink(toChannel) + "\""); + conn.announce.log(name + " switched from channel \"" + conn.announce.channelLink(fromChannel) + "\" to \"" + + conn.announce.channelLink(toChannel) + "\""); } } @@ -162,16 +162,16 @@ final class ConnectionEventHandler implements TS3Listener { String suffix = reasonMsg.isEmpty() ? "" : " (" + reasonMsg + ")"; switch (safeInt(e, "reasonid")) { case REASON_MOVED: - conn.log("You were moved from channel \"" + conn.channelLink(fromChannel) + "\" to \"" - + conn.channelLink(toChannel) + "\" by " + invokerName(e)); + conn.announce.log("You were moved from channel \"" + conn.announce.channelLink(fromChannel) + "\" to \"" + + conn.announce.channelLink(toChannel) + "\" by " + invokerName(e)); break; case REASON_CHANNEL_KICK: - conn.log("You were kicked from channel \"" + conn.channelLink(fromChannel) + "\" to \"" - + conn.channelLink(toChannel) + "\" by " + invokerName(e) + suffix); + conn.announce.log("You were kicked from channel \"" + conn.announce.channelLink(fromChannel) + "\" to \"" + + conn.announce.channelLink(toChannel) + "\" by " + invokerName(e) + suffix); break; default: - conn.log("You switched from channel \"" + conn.channelLink(fromChannel) + "\" to \"" - + conn.channelLink(toChannel) + "\""); + conn.announce.log("You switched from channel \"" + conn.announce.channelLink(fromChannel) + "\" to \"" + + conn.announce.channelLink(toChannel) + "\""); } } @@ -183,22 +183,22 @@ final class ConnectionEventHandler implements TS3Listener { if (!conn.isConnected()) return; int clientId = e.getClientId(); boolean current = conn.inOwnChannel(e.getClientTargetId()); - Map vars = conn.clientVars(clientId, e.getClientNickname()); + Map vars = conn.announce.clientVars(clientId, e.getClientNickname()); switch (safeInt(e, "reasonid")) { case REASON_MOVED: - conn.sound(current ? SoundEvent.CLIENT_MOVED_TO_CURRENT_CHANNEL_APPEARS + conn.announce.sound(current ? SoundEvent.CLIENT_MOVED_TO_CURRENT_CHANNEL_APPEARS : SoundEvent.CLIENT_MOVED_TO_OTHER_CHANNEL_APPEARS, vars); break; case REASON_CHANNEL_KICK: - conn.sound(current ? SoundEvent.CLIENT_WAS_KICKED_FROM_CHANNEL_TO_CURRENT_CHANNEL_APPEARS + conn.announce.sound(current ? SoundEvent.CLIENT_WAS_KICKED_FROM_CHANNEL_TO_CURRENT_CHANNEL_APPEARS : SoundEvent.CLIENT_WAS_KICKED_FROM_CHANNEL_TO_OTHER_CHANNEL_APPEARS, vars); break; case REASON_SWITCHED: - conn.sound(current ? SoundEvent.CLIENT_SWITCHED_TO_CURRENT_CHANNEL_APPEARS + conn.announce.sound(current ? SoundEvent.CLIENT_SWITCHED_TO_CURRENT_CHANNEL_APPEARS : SoundEvent.CLIENT_SWITCHED_TO_OTHER_CHANNEL_APPEARS, vars); break; default: - conn.sound(current ? SoundEvent.CLIENT_CONNECTION_CONNECTED_CURRENT_CHANNEL + conn.announce.sound(current ? SoundEvent.CLIENT_CONNECTION_CONNECTED_CURRENT_CHANNEL : SoundEvent.CLIENT_CONNECTION_CONNECTED_SERVER, vars); } } @@ -208,34 +208,34 @@ final class ConnectionEventHandler implements TS3Listener { if (!conn.isConnected()) return; int clientId = e.getClientId(); boolean current = conn.inOwnChannel(e.getClientFromId()); - Map vars = conn.clientVars(clientId, null); + Map vars = conn.announce.clientVars(clientId, null); switch (safeInt(e, "reasonid")) { case REASON_TIMEOUT: - conn.sound(current ? SoundEvent.CLIENT_CONNECTION_LOST_CONNECTION_CURRENT_CHANNEL + conn.announce.sound(current ? SoundEvent.CLIENT_CONNECTION_LOST_CONNECTION_CURRENT_CHANNEL : SoundEvent.CLIENT_CONNECTION_LOST_CONNECTION_SERVER, vars); break; case REASON_SERVER_KICK: - conn.sound(current ? SoundEvent.CLIENT_WAS_KICKED_FROM_SERVER_CURRENT_CHANNEL + conn.announce.sound(current ? SoundEvent.CLIENT_WAS_KICKED_FROM_SERVER_CURRENT_CHANNEL : SoundEvent.CLIENT_WAS_KICKED_FROM_SERVER_SERVER, vars); break; case REASON_BAN: - conn.sound(current ? SoundEvent.CLIENT_WAS_BANNED_CURRENT_CHANNEL + conn.announce.sound(current ? SoundEvent.CLIENT_WAS_BANNED_CURRENT_CHANNEL : SoundEvent.CLIENT_WAS_BANNED_SERVER, vars); break; case REASON_CHANNEL_KICK: - conn.sound(current ? SoundEvent.CLIENT_WAS_KICKED_FROM_CHANNEL_FROM_CURRENT_CHANNEL_DISAPPEARS + conn.announce.sound(current ? SoundEvent.CLIENT_WAS_KICKED_FROM_CHANNEL_FROM_CURRENT_CHANNEL_DISAPPEARS : SoundEvent.CLIENT_WAS_KICKED_FROM_CHANNEL_TO_OTHER_CHANNEL_DISAPPEARS, vars); break; case REASON_MOVED: - conn.sound(current ? SoundEvent.CLIENT_MOVED_FROM_CURRENT_CHANNEL_DISAPPEARS + conn.announce.sound(current ? SoundEvent.CLIENT_MOVED_FROM_CURRENT_CHANNEL_DISAPPEARS : SoundEvent.CLIENT_MOVED_TO_OTHER_CHANNEL_DISAPPEARS, vars); break; case REASON_SWITCHED: - conn.sound(current ? SoundEvent.CLIENT_SWITCHED_FROM_CURRENT_CHANNEL_DISAPPEARS + conn.announce.sound(current ? SoundEvent.CLIENT_SWITCHED_FROM_CURRENT_CHANNEL_DISAPPEARS : SoundEvent.CLIENT_SWITCHED_TO_OTHER_CHANNEL_DISAPPEARS, vars); break; default: - conn.sound(current ? SoundEvent.CLIENT_CONNECTION_DISCONNECTED_CURRENT_CHANNEL + conn.announce.sound(current ? SoundEvent.CLIENT_CONNECTION_DISCONNECTED_CURRENT_CHANNEL : SoundEvent.CLIENT_CONNECTION_DISCONNECTED_SERVER, vars); } } @@ -245,20 +245,20 @@ final class ConnectionEventHandler implements TS3Listener { if (!conn.isConnected()) return; boolean toCurrent = conn.inOwnChannel(toChannel); boolean fromCurrent = conn.inOwnChannel(fromChannel); - Map vars = conn.clientVars(clientId, null); + Map vars = conn.announce.clientVars(clientId, null); switch (reason) { case REASON_MOVED: - conn.sound(toCurrent ? SoundEvent.CLIENT_MOVED_TO_CURRENT_CHANNEL_STAYS + conn.announce.sound(toCurrent ? SoundEvent.CLIENT_MOVED_TO_CURRENT_CHANNEL_STAYS : fromCurrent ? SoundEvent.CLIENT_MOVED_FROM_CURRENT_CHANNEL_STAYS : SoundEvent.CLIENT_MOVED_TO_OTHER_CHANNEL_STAYS, vars); break; case REASON_CHANNEL_KICK: - conn.sound(toCurrent ? SoundEvent.CLIENT_WAS_KICKED_FROM_CHANNEL_TO_CURRENT_CHANNEL_STAYS + conn.announce.sound(toCurrent ? SoundEvent.CLIENT_WAS_KICKED_FROM_CHANNEL_TO_CURRENT_CHANNEL_STAYS : fromCurrent ? SoundEvent.CLIENT_WAS_KICKED_FROM_CHANNEL_FROM_CURRENT_CHANNEL_STAYS : SoundEvent.CLIENT_WAS_KICKED_FROM_CHANNEL_TO_OTHER_CHANNEL_STAYS, vars); break; default: - conn.sound(toCurrent ? SoundEvent.CLIENT_SWITCHED_TO_CURRENT_CHANNEL_STAYS + conn.announce.sound(toCurrent ? SoundEvent.CLIENT_SWITCHED_TO_CURRENT_CHANNEL_STAYS : fromCurrent ? SoundEvent.CLIENT_SWITCHED_FROM_CURRENT_CHANNEL_STAYS : SoundEvent.CLIENT_SWITCHED_TO_OTHER_CHANNEL_STAYS, vars); } @@ -267,16 +267,16 @@ final class ConnectionEventHandler implements TS3Listener { /** We changed channel ourselves, or somebody changed it for us. */ private void announceOwnMove(int reason, ClientMovedEvent e) { if (!conn.isConnected()) return; - Map vars = conn.channelVars(e.getTargetChannelId(), e.get("invokername")); + Map vars = conn.announce.channelVars(e.getTargetChannelId(), e.get("invokername")); switch (reason) { case REASON_MOVED: - conn.sound(SoundEvent.YOU_WERE_MOVED_TO_DIFFERENT_CHANNEL, vars); + conn.announce.sound(SoundEvent.YOU_WERE_MOVED_TO_DIFFERENT_CHANNEL, vars); break; case REASON_CHANNEL_KICK: - conn.sound(SoundEvent.YOU_WERE_KICKED_FROM_CHANNEL, vars); + conn.announce.sound(SoundEvent.YOU_WERE_KICKED_FROM_CHANNEL, vars); break; default: - conn.sound(SoundEvent.YOU_SWITCHED_CHANNEL, vars); + conn.announce.sound(SoundEvent.YOU_SWITCHED_CHANNEL, vars); } } @@ -288,9 +288,9 @@ final class ConnectionEventHandler implements TS3Listener { "clientname", TeamspeakConnection.orEmpty(e.get("invokername")), "reason", TeamspeakConnection.orEmpty(e.get("reasonmsg"))); if (reason == REASON_SERVER_KICK) { - conn.sound(SoundEvent.YOU_WERE_KICKED_FROM_SERVER, vars); + conn.announce.sound(SoundEvent.YOU_WERE_KICKED_FROM_SERVER, vars); } else if (reason == REASON_BAN) { - conn.sound(SoundEvent.YOU_WERE_BANNED, vars); + conn.announce.sound(SoundEvent.YOU_WERE_BANNED, vars); } } @@ -363,29 +363,29 @@ final class ConnectionEventHandler implements TS3Listener { private void announceClientUpdate(ClientUpdatedEvent e, ClientEntry c, boolean renamed, String oldName) { if (!conn.isConnected()) return; boolean self = c.id == conn.getSelfClientId(); - Map vars = conn.clientVars(c.id, null); + Map vars = conn.announce.clientVars(c.id, null); if (renamed) { - conn.sound(safeInt(e, "invokerid") == conn.getSelfClientId() + conn.announce.sound(safeInt(e, "invokerid") == conn.getSelfClientId() ? SoundEvent.CLIENT_RENAMED_BY_YOU : SoundEvent.CLIENT_RENAMED_BY_OTHER, vars); - conn.log(oldName + " is now known as " + conn.clientLink(c.id)); + conn.announce.log(oldName + " is now known as " + conn.announce.clientLink(c.id)); } if (has(e, "client_talk_request") && talkRequested(e) && !self) { - conn.sound(SoundEvent.CLIENT_REQUESTED_TALK_POWER, vars); - conn.log(conn.clientLink(c.id) + " requested talk power"); + conn.announce.sound(SoundEvent.CLIENT_REQUESTED_TALK_POWER, vars); + conn.announce.log(conn.announce.clientLink(c.id) + " requested talk power"); } // Leaving the channel drops the grant too, but without an invoker: that is no news. if (self && has(e, "client_is_talker") && has(e, "invokerid")) { boolean granted = e.getBoolean("client_is_talker"); - conn.sound(granted ? SoundEvent.YOU_WERE_GRANTED_TALK_POWER : SoundEvent.YOU_WERE_REVOKED_TALK_POWER, vars); - conn.log("Talk Power " + (granted ? "granted" : "revoked") + " by " + invokerName(e)); + conn.announce.sound(granted ? SoundEvent.YOU_WERE_GRANTED_TALK_POWER : SoundEvent.YOU_WERE_REVOKED_TALK_POWER, vars); + conn.announce.log("Talk Power " + (granted ? "granted" : "revoked") + " by " + invokerName(e)); } if (!self && has(e, "client_is_recording")) { boolean recording = e.getBoolean("client_is_recording"); if (!recording) { - conn.sound(SoundEvent.CLIENT_RECORDING_STOP, vars); + conn.announce.sound(SoundEvent.CLIENT_RECORDING_STOP, vars); } else { - conn.sound(conn.inOwnChannel(c.channelId) + conn.announce.sound(conn.inOwnChannel(c.channelId) ? SoundEvent.CLIENT_RECORDING_IN_CHANNEL : SoundEvent.CLIENT_RECORDING_START, vars); } } @@ -401,9 +401,9 @@ final class ConnectionEventHandler implements TS3Listener { applyChannelProperties(node, e); conn.getModel().relinkChannel(cid, pid, order); if (conn.isConnected()) { - conn.sound(byInvoker(e, SoundEvent.CHANNEL_CREATED_BY_YOU, SoundEvent.CHANNEL_CREATED_BY_OTHER, - SoundEvent.CHANNEL_CREATED_BY_OTHER), conn.channelVars(cid, e.get("invokername"))); - conn.log("Channel \"" + conn.channelLink(cid) + "\" was created by " + invokerName(e)); + conn.announce.sound(byInvoker(e, SoundEvent.CHANNEL_CREATED_BY_YOU, SoundEvent.CHANNEL_CREATED_BY_OTHER, + SoundEvent.CHANNEL_CREATED_BY_OTHER), conn.announce.channelVars(cid, e.get("invokername"))); + conn.announce.log("Channel \"" + conn.announce.channelLink(cid) + "\" was created by " + invokerName(e)); } conn.ui.onModelChanged(); } @@ -412,9 +412,9 @@ final class ConnectionEventHandler implements TS3Listener { public void onChannelDeleted(ChannelDeletedEvent e) { int cid = e.getChannelId(); if (conn.isConnected()) { - conn.sound(byInvoker(e, SoundEvent.CHANNEL_DELETED_BY_YOU, SoundEvent.CHANNEL_DELETED_BY_OTHER, - SoundEvent.CHANNEL_DELETED_BY_SERVER), conn.channelVars(cid, e.get("invokername"))); - conn.log("Channel \"" + conn.channelName(cid) + "\" was deleted by " + invokerName(e)); + conn.announce.sound(byInvoker(e, SoundEvent.CHANNEL_DELETED_BY_YOU, SoundEvent.CHANNEL_DELETED_BY_OTHER, + SoundEvent.CHANNEL_DELETED_BY_SERVER), conn.announce.channelVars(cid, e.get("invokername"))); + conn.announce.log("Channel \"" + conn.announce.channelName(cid) + "\" was deleted by " + invokerName(e)); } conn.getModel().removeChannel(cid); conn.ui.onModelChanged(); @@ -437,13 +437,13 @@ final class ConnectionEventHandler implements TS3Listener { if (has(e, "channel_order")) conn.getModel().relinkChannel(ch.id, ch.parentId, e.getInt("channel_order")); if (conn.isConnected()) { boolean current = conn.inOwnChannel(ch.id); - conn.sound(current + conn.announce.sound(current ? byInvoker(e, SoundEvent.CHANNEL_EDITED_CURRENT_BY_YOU, SoundEvent.CHANNEL_EDITED_CURRENT_BY_OTHER, SoundEvent.CHANNEL_EDITED_CURRENT_BY_OTHER) : byInvoker(e, SoundEvent.CHANNEL_EDITED_OTHER_BY_YOU, SoundEvent.CHANNEL_EDITED_OTHER_BY_OTHER, SoundEvent.CHANNEL_EDITED_OTHER_BY_SERVER), - conn.channelVars(ch.id, e.get("invokername"))); - conn.log("Channel \"" + conn.channelLink(ch.id) + "\" was edited by " + invokerName(e)); + conn.announce.channelVars(ch.id, e.get("invokername"))); + conn.announce.log("Channel \"" + conn.announce.channelLink(ch.id) + "\" was edited by " + invokerName(e)); } conn.ui.onModelChanged(); } @@ -488,10 +488,10 @@ final class ConnectionEventHandler implements TS3Listener { int order = has(e, "order") ? e.getInt("order") : ch.order; conn.getModel().relinkChannel(ch.id, parent, order); if (conn.isConnected()) { - conn.sound(byInvoker(e, SoundEvent.CHANNEL_MOVED_BY_YOU, SoundEvent.CHANNEL_MOVED_BY_OTHER, - SoundEvent.CHANNEL_MOVED_BY_OTHER), conn.channelVars(ch.id, e.get("invokername"))); - String newParent = parent == 0 ? conn.getModel().getServerName() : conn.channelLink(parent); - conn.log("Channel \"" + conn.channelLink(ch.id) + "\" was moved by " + invokerName(e) + conn.announce.sound(byInvoker(e, SoundEvent.CHANNEL_MOVED_BY_YOU, SoundEvent.CHANNEL_MOVED_BY_OTHER, + SoundEvent.CHANNEL_MOVED_BY_OTHER), conn.announce.channelVars(ch.id, e.get("invokername"))); + String newParent = parent == 0 ? conn.getModel().getServerName() : conn.announce.channelLink(parent); + conn.announce.log("Channel \"" + conn.announce.channelLink(ch.id) + "\" was moved by " + invokerName(e) + ", new parent channel is \"" + newParent + "\""); } conn.ui.onModelChanged(); @@ -519,8 +519,8 @@ final class ConnectionEventHandler implements TS3Listener { if (has(e, "virtualserver_name")) conn.getModel().setServerName(e.get("virtualserver_name")); conn.getModel().putServerProperties(e.getMap()); if (conn.isConnected()) { - conn.sound(byInvoker(e, SoundEvent.SERVER_EDITED_BY_YOU, SoundEvent.SERVER_EDITED_BY_OTHER, - SoundEvent.SERVER_EDITED_BY_OTHER), conn.serverVars()); + conn.announce.sound(byInvoker(e, SoundEvent.SERVER_EDITED_BY_YOU, SoundEvent.SERVER_EDITED_BY_OTHER, + SoundEvent.SERVER_EDITED_BY_OTHER), conn.announce.serverVars()); } conn.ui.onModelChanged(); } @@ -531,13 +531,13 @@ final class ConnectionEventHandler implements TS3Listener { if (c != null) c.addServerGroup(e.getServerGroupId()); boolean self = e.getClientId() == conn.getSelfClientId(); if (self) conn.requestSelfPowers(); - conn.sound(self + conn.announce.sound(self ? byInvoker(e, SoundEvent.YOU_SERVERGROUP_ADDED_BY_USER, SoundEvent.YOU_SERVERGROUP_ADDED_BY_USER, SoundEvent.YOU_SERVERGROUP_ADDED_BY_SERVER) : byInvoker(e, SoundEvent.CLIENT_SERVERGROUP_ADDED_BY_USER, SoundEvent.CLIENT_SERVERGROUP_ADDED_BY_USER, SoundEvent.CLIENT_SERVERGROUP_ADDED_BY_SERVER), groupVars(e.getClientId(), serverGroupName(e.getServerGroupId()))); - conn.log(clientLogName(e.getClientId()) + " was added to server group \"" + serverGroupName(e.getServerGroupId()) + conn.announce.log(clientLogName(e.getClientId()) + " was added to server group \"" + serverGroupName(e.getServerGroupId()) + "\" by " + invokerName(e) + "."); conn.ui.onModelChanged(); } @@ -549,13 +549,13 @@ final class ConnectionEventHandler implements TS3Listener { if (c != null) c.removeServerGroup(e.getServerGroupId()); boolean self = clientId == conn.getSelfClientId(); if (self) conn.requestSelfPowers(); - conn.sound(self + conn.announce.sound(self ? byInvoker(e, SoundEvent.YOU_SERVERGROUP_REMOVED_BY_USER, SoundEvent.YOU_SERVERGROUP_REMOVED_BY_USER, SoundEvent.YOU_SERVERGROUP_REMOVED_BY_SERVER) : byInvoker(e, SoundEvent.CLIENT_SERVERGROUP_REMOVED_BY_USER, SoundEvent.CLIENT_SERVERGROUP_REMOVED_BY_USER, SoundEvent.CLIENT_SERVERGROUP_REMOVED_BY_SERVER), groupVars(clientId, serverGroupName(e.getServerGroupId()))); - conn.log(clientLogName(clientId) + " was removed from server group \"" + serverGroupName(e.getServerGroupId()) + conn.announce.log(clientLogName(clientId) + " was removed from server group \"" + serverGroupName(e.getServerGroupId()) + "\" by " + invokerName(e) + "."); conn.ui.onModelChanged(); } @@ -567,13 +567,13 @@ final class ConnectionEventHandler implements TS3Listener { boolean self = e.getClientId() == conn.getSelfClientId(); if (self) conn.requestSelfPowers(); String groupName = conn.getModel().channelGroupName(e.getChannelGroupId()); - conn.sound(self + conn.announce.sound(self ? byInvoker(e, SoundEvent.YOU_CHANNELGROUP_CHANGED_BY_USER, SoundEvent.YOU_CHANNELGROUP_CHANGED_BY_USER, SoundEvent.YOU_CHANNELGROUP_CHANGED_BY_SERVER) : byInvoker(e, SoundEvent.CLIENT_CHANNELGROUP_CHANGED_BY_USER, SoundEvent.CLIENT_CHANNELGROUP_CHANGED_BY_USER, SoundEvent.CLIENT_CHANNELGROUP_CHANGED_BY_SERVER), groupVars(e.getClientId(), groupName)); - conn.log("Channel group \"" + TeamspeakConnection.orEmpty(groupName) + "\" was assigned to " + conn.announce.log("Channel group \"" + TeamspeakConnection.orEmpty(groupName) + "\" was assigned to " + clientLogName(e.getClientId()) + " by " + invokerName(e) + "."); conn.ui.onModelChanged(); } @@ -589,7 +589,7 @@ final class ConnectionEventHandler implements TS3Listener { /** A client's name for a log line, as a link, falling back to its id once it has left. */ private String clientLogName(int clientId) { - return conn.clientLink(clientId); + return conn.announce.clientLink(clientId); } /** Picks the event variant matching who caused the change: us, another client, or the server. */ @@ -600,7 +600,7 @@ final class ConnectionEventHandler implements TS3Listener { } private Map groupVars(int clientId, String groupName) { - Map vars = conn.clientVars(clientId, null); + Map vars = conn.announce.clientVars(clientId, null); vars.put("groupname", TeamspeakConnection.orEmpty(groupName)); return vars; } @@ -665,8 +665,8 @@ final class ConnectionEventHandler implements TS3Listener { notification = SoundEvent.CHAT_RECEIVED_MESSAGE_SERVER; break; } - conn.persistIncomingChat(scope, e.getInvokerId(), e.getInvokerUniqueId(), e.getInvokerName(), e.getMessage()); - conn.sound(notification, conn.clientVars(e.getInvokerId(), e.getInvokerName())); + conn.chatLog.received(scope, e.getInvokerId(), e.getInvokerUniqueId(), e.getInvokerName(), e.getMessage()); + conn.announce.sound(notification, conn.announce.clientVars(e.getInvokerId(), e.getInvokerName())); conn.ui.onChat(scope, e.getInvokerId(), e.getInvokerUniqueId(), e.getInvokerName(), e.getMessage()); } @@ -674,7 +674,7 @@ final class ConnectionEventHandler implements TS3Listener { public void onClientPoke(ClientPokeEvent e) { Contact contact = conn.contacts.find(e.getInvokerUniqueId()); if (contact != null && contact.ignorePokes) return; - conn.sound(SoundEvent.OTHER_RECEIVED_POKE, conn.clientVars(e.getInvokerId(), e.getInvokerName())); + conn.announce.sound(SoundEvent.OTHER_RECEIVED_POKE, conn.announce.clientVars(e.getInvokerId(), e.getInvokerName())); String from = conn.contacts.displayName(e.getInvokerUniqueId(), TeamspeakConnection.orEmpty(e.getInvokerName())); conn.ui.onPoke(from, TeamspeakConnection.orEmpty(e.get("msg"))); } @@ -697,7 +697,7 @@ final class ConnectionEventHandler implements TS3Listener { private String invokerName(BaseEvent e) { String name = TeamspeakConnection.orEmpty(e.get("invokername")); if (name.isEmpty()) return "the server"; - return conn.clientLink(safeInt(e, "invokerid"), + return conn.announce.clientLink(safeInt(e, "invokerid"), TeamspeakConnection.orEmpty(e.get("invokeruid")), name); } diff --git a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/net/TeamspeakConnection.java b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/net/TeamspeakConnection.java index 74c1e64..03bf59f 100644 --- a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/net/TeamspeakConnection.java +++ b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/net/TeamspeakConnection.java @@ -15,8 +15,6 @@ import com.github.manevolent.ts3j.util.Ts3Crypt; import eu.lixko.ts3j.client.audio.AudioBackend; import eu.lixko.ts3j.client.audio.VoiceInput; import eu.lixko.ts3j.client.audio.VoiceOutput; -import eu.lixko.ts3j.client.chatlog.ChatLogScope; -import eu.lixko.ts3j.client.chatlog.ChatLogStore; import eu.lixko.ts3j.client.config.Settings; import eu.lixko.ts3j.client.contacts.Contact; import eu.lixko.ts3j.client.contacts.ContactStore; @@ -39,7 +37,6 @@ import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -95,12 +92,14 @@ public final class TeamspeakConnection implements TS3Listener { final ConnectionListener ui; private final IconRepository icons; private final AvatarRepository avatars; - /** Package-private: read directly by {@link ConnectionEventHandler}. */ - final SoundNotifier sounds; /** Friends, blocked clients and their per-client filters; also read by {@link ConnectionEventHandler}. */ final ContactStore contacts; + /** The chat log on disk, opened once the server is known; package-private for {@link ConnectionEventHandler}. */ + final ChatLog chatLog; + /** Log lines and sounds for what happens; package-private for {@link ConnectionEventHandler}. */ + final Announcer announce; /** The {@link TS3Listener} side of this connection; see its class doc. */ - private final ConnectionEventHandler events = new ConnectionEventHandler(this); + private final ConnectionEventHandler events; /** Builds {@code getconnectioninfo} snapshots; also package-private for {@link ConnectionEventHandler}. */ final ConnectionStatsCollector stats = new ConnectionStatsCollector(this); /** Channel editing and the server's icon store; package-private for {@link ConnectionEventHandler}. */ @@ -126,7 +125,6 @@ public final class TeamspeakConnection implements TS3Listener { private volatile VoiceOutput playback; private volatile LocalIdentity identity; private volatile FileTransferManager fileTransfers; - private volatile ChatLogStore chatLogs; /** The recording in progress; guarded by this connection. */ private VoiceRecorder recorder; /** @@ -219,8 +217,10 @@ public final class TeamspeakConnection implements TS3Listener { this.settings = settings; this.audio = audio; this.ui = ui; - this.sounds = sounds; this.contacts = contacts; + this.chatLog = new ChatLog(settings, ui); + this.announce = new Announcer(model, sounds, ui, chatLog, () -> deafened, () -> connected); + this.events = new ConnectionEventHandler(this); this.icons = new IconRepository(this::fetchIcon, ui::onIconsUpdated); this.avatars = new AvatarRepository(this::fetchAvatar, ui::onInfoUpdated); } @@ -367,8 +367,8 @@ public final class TeamspeakConnection implements TS3Listener { ClientEntry c = model.getClient(clientId); if (c != null) c.talking = talking; if (c != null && inOwnChannel(c.channelId)) { - sound(talking ? SoundEvent.SPECIAL_MIC_CLICK_OTHER_ON - : SoundEvent.SPECIAL_MIC_CLICK_OTHER_OFF, clientVars(clientId, null)); + announce.sound(talking ? SoundEvent.SPECIAL_MIC_CLICK_OTHER_ON + : SoundEvent.SPECIAL_MIC_CLICK_OTHER_OFF, announce.clientVars(clientId, null)); } ui.onTalkStateChanged(clientId, talking); }); @@ -378,7 +378,7 @@ public final class TeamspeakConnection implements TS3Listener { if (selfClientId >= 0) { ClientEntry c = model.getClient(selfClientId); if (c != null) c.talking = talking; - sound(talking ? SoundEvent.SPECIAL_MIC_CLICK_SELF_ON + announce.sound(talking ? SoundEvent.SPECIAL_MIC_CLICK_SELF_ON : SoundEvent.SPECIAL_MIC_CLICK_SELF_OFF); ui.onTalkStateChanged(selfClientId, talking); } @@ -446,7 +446,7 @@ public final class TeamspeakConnection implements TS3Listener { disconnectAnnounced = false; deafened = false; offlineMessages.reset(); - loadChatLogs(); + chatLog.open(serverUniqueId()); ui.onConnected(); requestPermissionNames(); requestSelfPowers(); @@ -456,7 +456,7 @@ public final class TeamspeakConnection implements TS3Listener { ui.onModelChanged(); joinDefaultChannelIfNeeded(channel, channelPassword); ui.onStatus("Connected to " + model.getServerName()); - sound(SoundEvent.CONNECTION_CONNECTED, serverVars()); + announce.sound(SoundEvent.CONNECTION_CONNECTED, announce.serverVars()); unreadMessagesChanged(0); // Only captures if this connection holds the microphone. @@ -551,7 +551,7 @@ public final class TeamspeakConnection implements TS3Listener { ? null : channelPassword); } catch (Exception e) { if (!connected) return; - error("Could not join channel \"" + target.name + "\": ", e); + announce.error("Could not join channel \"" + target.name + "\": ", e); } } @@ -637,8 +637,8 @@ public final class TeamspeakConnection implements TS3Listener { private void announceDisconnect(boolean deliberate) { if (disconnectAnnounced) return; disconnectAnnounced = true; - sound(deliberate || leaving ? SoundEvent.CONNECTION_DISCONNECTED - : SoundEvent.CONNECTION_LOST_CONNECTION, serverVars()); + announce.sound(deliberate || leaving ? SoundEvent.CONNECTION_DISCONNECTED + : SoundEvent.CONNECTION_LOST_CONNECTION, announce.serverVars()); } /** Reminds the user, at most every few seconds, that the microphone is muted. */ @@ -647,7 +647,7 @@ public final class TeamspeakConnection implements TS3Listener { long now = System.nanoTime(); if (now - lastMutedTalkNanos < MUTED_TALK_COOLDOWN_NANOS) return; lastMutedTalkNanos = now; - sound(SoundEvent.SPECIAL_TALKING_WHILE_MUTED); + announce.sound(SoundEvent.SPECIAL_TALKING_WHILE_MUTED); } private synchronized void safeCleanup() { @@ -669,7 +669,7 @@ public final class TeamspeakConnection implements TS3Listener { playback = null; client = null; fileTransfers = null; - chatLogs = null; + chatLog.close(); selfClientId = -1; stats.clear(); @@ -707,7 +707,7 @@ public final class TeamspeakConnection implements TS3Listener { public void setMicMuted(boolean muted) { VoiceInput mic = microphone; if (mic != null) mic.setMuted(muted); - sound(muted ? SoundEvent.SOUND_CAPTURE_MUTED : SoundEvent.SOUND_CAPTURE_UNMUTED); + announce.sound(muted ? SoundEvent.SOUND_CAPTURE_MUTED : SoundEvent.SOUND_CAPTURE_UNMUTED); pushSelfFlags(); } @@ -722,11 +722,11 @@ public final class TeamspeakConnection implements TS3Listener { public void setDeafened(boolean deaf) { // Announce muting while we can still be heard, and unmuting once we can again. - if (deaf) sound(SoundEvent.SOUND_PLAYBACK_MUTED); + if (deaf) announce.sound(SoundEvent.SOUND_PLAYBACK_MUTED); deafened = deaf; VoiceOutput out = playback; if (out != null) out.setDeafened(deaf); - if (!deaf) sound(SoundEvent.SOUND_PLAYBACK_UNMUTED); + if (!deaf) announce.sound(SoundEvent.SOUND_PLAYBACK_UNMUTED); pushSelfFlags(); } @@ -944,16 +944,16 @@ public final class TeamspeakConnection implements TS3Listener { ClientEntry self = model.getClient(selfClientId); int cid = self != null ? self.channelId : 0; sock.sendChannelMessage(cid, text); - persistOutgoingChat(ConnectionListener.ChatScope.CHANNEL, 0, "", text); - sound(SoundEvent.CHAT_SENT_MESSAGE_CHANNEL, channelVars(cid, null)); + keepSentChat(ConnectionListener.ChatScope.CHANNEL, "", text); + announce.sound(SoundEvent.CHAT_SENT_MESSAGE_CHANNEL, announce.channelVars(cid, null)); }); } public void sendServerMessage(String text) { act("Message failed: ", sock -> { sock.sendServerMessage(text); - persistOutgoingChat(ConnectionListener.ChatScope.SERVER, 0, "", text); - sound(SoundEvent.CHAT_SENT_MESSAGE_SERVER, serverVars()); + keepSentChat(ConnectionListener.ChatScope.SERVER, "", text); + announce.sound(SoundEvent.CHAT_SENT_MESSAGE_SERVER, announce.serverVars()); }); } @@ -969,20 +969,20 @@ public final class TeamspeakConnection implements TS3Listener { } catch (Exception ignored) { } } - persistOutgoingChat(ConnectionListener.ChatScope.PRIVATE, clientId, peerUid, text); - sound(SoundEvent.CHAT_SENT_MESSAGE_CLIENT, clientVars(clientId, null)); + keepSentChat(ConnectionListener.ChatScope.PRIVATE, peerUid, text); + announce.sound(SoundEvent.CHAT_SENT_MESSAGE_CLIENT, announce.clientVars(clientId, null)); }); } public void poke(int clientId, String message) { act("Poke failed: ", sock -> { sock.clientPoke(clientId, message); - sound(SoundEvent.OTHER_SENT_POKE, clientVars(clientId, null)); + announce.sound(SoundEvent.OTHER_SENT_POKE, announce.clientVars(clientId, null)); }); } public void setAway(boolean away, String message) { - sound(away ? SoundEvent.STATUS_SET_AWAY : SoundEvent.STATUS_SET_PRESENT); + announce.sound(away ? SoundEvent.STATUS_SET_AWAY : SoundEvent.STATUS_SET_PRESENT); updateSelf(self -> { self.away = away; self.awayMessage = away && message != null ? message : ""; @@ -1260,7 +1260,7 @@ public final class TeamspeakConnection implements TS3Listener { public void complain(ClientEntry client, String reason) { act("Failed to send complaint: ", sock -> { complaints.add(client.databaseId, reason); - log("Complaint sent."); + announce.log("Complaint sent."); }); } @@ -1293,7 +1293,7 @@ public final class TeamspeakConnection implements TS3Listener { int unread = unreadOfflineMessages(); if (unread > before) { String count = unread == 1 ? "1 unread offline message" : unread + " unread offline messages"; - log("You have " + TsLink.offlineMessagesBBCode(count)); + announce.log("You have " + TsLink.offlineMessagesBBCode(count)); } ui.onUnreadMessagesChanged(unread); } @@ -1329,7 +1329,7 @@ public final class TeamspeakConnection implements TS3Listener { public CompletableFuture sendOfflineMessage(Collection recipientUids, String subject, String message) { return requestVoid(() -> { for (String uid : recipientUids) offlineMessages.send(uid, subject, message); - log("Offline message sent."); + announce.log("Offline message sent."); }); } @@ -1568,7 +1568,7 @@ public final class TeamspeakConnection implements TS3Listener { /** Sends a request in the background, reporting a failure as {@code errorPrefix} and the reason. */ private void act(String errorPrefix, Action action) { - submit(action, e -> error(errorPrefix, e)); + submit(action, e -> announce.error(errorPrefix, e)); } /** Sends a request in the background whose failure nobody needs to hear about. */ @@ -1809,9 +1809,9 @@ public final class TeamspeakConnection implements TS3Listener { FileTransfer.State state = transfer.getState(); if (state != FileTransfer.State.CANCELLED && transfer.isDone() && announced.compareAndSet(false, true)) { - sound(state == FileTransfer.State.COMPLETED + announce.sound(state == FileTransfer.State.COMPLETED ? SoundEvent.OTHER_FILETRANSFER_COMPLETE - : SoundEvent.OTHER_FILETRANSFER_FAILED, serverVars()); + : SoundEvent.OTHER_FILETRANSFER_FAILED, announce.serverVars()); } if (listener != null) listener.onTransferChanged(transfer); }; @@ -1853,46 +1853,6 @@ public final class TeamspeakConnection implements TS3Listener { // ---- sound notifications ---- - /** - * Plays the sound pack's entry for an action. Muting is passed along so the - * notifier can drop everything the user did not mark as important. Package-private: - * also called by {@link ConnectionEventHandler}. - */ - void sound(SoundEvent event) { - sounds.fire(event, deafened); - } - - void sound(SoundEvent event, Map variables) { - sounds.fire(event, deafened, variables); - } - - private void loadChatLogs() { - chatLogs = null; - if (!settings.chatLogs) return; - try { - String uniqueId = serverUniqueId(); - if (uniqueId == null) { - ui.onStatus("Chat logs unavailable: the server's unique id is not known"); - return; - } - String dir = settings.chatLogDir == null ? "" : settings.chatLogDir.trim(); - // Android < API 34 lacks Path.of; original: - // ChatLogStore logs = new ChatLogStore(dir.isEmpty() ? ChatLogStore.defaultRoot() : Path.of(dir), uniqueId); - ChatLogStore logs = new ChatLogStore(dir.isEmpty() ? ChatLogStore.defaultRoot() : Paths.get(dir), uniqueId); - chatLogs = logs; - logs.begin(); - List serverHistory = logs.serverHtmlHistory(); - List channelHistory = logs.channelHtmlHistory(); - ui.onChatHistory(ConnectionListener.ChatScope.SERVER, serverHistory); - ui.onChatHistory(ConnectionListener.ChatScope.CHANNEL, channelHistory); - ui.onStatus("Loaded chat logs from " + logs.serverDir() - + " (" + serverHistory.size() + " server, " + channelHistory.size() + " channel)"); - } catch (Exception e) { - chatLogs = null; - ui.onStatus("Chat logs unavailable: " + RequestException.rootMessage(e)); - } - } - /** * The server's unique identifier, worked out from its key as TeamSpeak does (a guest is * never told it); {@code null} before the server has introduced itself. @@ -1904,131 +1864,16 @@ public final class TeamspeakConnection implements TS3Listener { return uid == null ? null : uid.toBase64(); } + /** Hands the frontend what was said with a client before, from the chat log. */ public void loadPrivateChatHistory(int clientId, String uniqueId) { - ChatLogStore logs = chatLogs; - if (logs == null || uniqueId == null || uniqueId.isBlank()) return; - try { - ClientEntry c = model.getClient(clientId); - ui.onPrivateChatHistory(clientId, c != null ? c.nickname : "Client " + clientId, - logs.privateHtmlHistory(uniqueId)); - } catch (IOException e) { - ui.onStatus("Private chat log unavailable: " + RequestException.rootMessage(e)); - } - } - - void persistIncomingChat(ConnectionListener.ChatScope scope, int fromClientId, String fromUniqueId, - String fromName, String message) { - ChatLogStore logs = chatLogs; - if (logs == null) return; - try { - logs.appendMessage(toLogScope(scope), false, fromClientId, fromUniqueId, fromName, message); - } catch (IOException e) { - ui.onStatus("Could not write chat log: " + RequestException.rootMessage(e)); - } - } - - private void persistOutgoingChat(ConnectionListener.ChatScope scope, int peerClientId, String peerUniqueId, - String message) { - ChatLogStore logs = chatLogs; - if (logs == null) return; - try { - ClientEntry self = model.getClient(selfClientId); - String uid = identity != null ? identity.getUid().toBase64() : ""; - String name = self != null ? self.nickname : settings.nickname; - if (scope == ConnectionListener.ChatScope.PRIVATE) { - if (peerUniqueId == null || peerUniqueId.isBlank()) return; - logs.appendPrivateMessage(peerUniqueId, true, selfClientId, uid, name, message); - } else { - logs.appendMessage(toLogScope(scope), true, selfClientId, uid, name, message); - } - } catch (IOException e) { - ui.onStatus("Could not write chat log: " + RequestException.rootMessage(e)); - } - } - - private static ChatLogScope toLogScope(ConnectionListener.ChatScope scope) { - return switch (scope) { - case SERVER -> ChatLogScope.SERVER; - case CHANNEL -> ChatLogScope.CHANNEL; - case PRIVATE -> ChatLogScope.PRIVATE; - }; - } - - /** Records a line in the server tab's log, the way native TS3 reports server activity. */ - void log(String message) { - if (!connected) return; - ChatLogStore logs = chatLogs; - if (logs == null) { - ui.onServerLog(message); - return; - } - try { - ui.onServerLogHtml(logs.appendServerEvent(message)); - } catch (IOException e) { - ui.onServerLog(message); - ui.onStatus("Could not write server log: " + RequestException.rootMessage(e)); - } - } - - String channelName(int channelId) { - ChannelNode ch = model.getChannel(channelId); - return ch != null ? ch.name : "channel #" + channelId; - } - - /** - * A channel's name as the log wants it: TeamSpeak's own {@code channel://} link, which - * the chat log turns into the same clickable entry a dragged-in channel produces. A - * channel we no longer know (a deleted one, say) is named in plain text instead. - */ - String channelLink(int channelId) { - ChannelNode ch = model.getChannel(channelId); - return ch != null ? TsLink.channelBBCode(channelId, ch.name) : channelName(channelId); - } - - /** The same for a client, named by the model. */ - String clientLink(int clientId) { ClientEntry c = model.getClient(clientId); - return c != null ? clientLink(clientId, c.uniqueId, c.nickname) : "Client " + clientId; + chatLog.loadPrivateHistory(clientId, c != null ? c.nickname : "Client " + clientId, uniqueId); } - /** The same for a client an event describes, which may already be gone from the model. */ - String clientLink(int clientId, String uniqueId, String nickname) { - if (clientId <= 0 || nickname == null || nickname.isEmpty()) return orEmpty(nickname); - return TsLink.clientBBCode(clientId, orEmpty(uniqueId), nickname); - } - - /** The placeholder values a pack may reference for an action involving a client. */ - Map clientVars(int clientId, String fallbackName) { - ClientEntry c = model.getClient(clientId); - Map vars = SoundNotifier.vars( - "servername", model.getServerName(), - "clientname", c != null ? c.nickname : orEmpty(fallbackName), - "clientuid", c != null ? c.uniqueId : ""); - if (c != null) { - ChannelNode ch = model.getChannel(c.channelId); - if (ch != null) vars.put("channelname", ch.name); - } - return vars; - } - - Map channelVars(int channelId, String invokerName) { - ChannelNode ch = model.getChannel(channelId); - return SoundNotifier.vars( - "servername", model.getServerName(), - "channelname", ch != null ? ch.name : "", - "clientname", orEmpty(invokerName)); - } - - Map serverVars() { - return SoundNotifier.vars("servername", model.getServerName()); - } - - /** Reports a failure to the UI and plays the matching server sound. */ - private void error(String prefix, Exception e) { - sound(RequestException.isPermissionError(e) - ? SoundEvent.SERVER_INSUFFICIENT_PERMISSIONS - : SoundEvent.SERVER_ERROR, serverVars()); - ui.onError(prefix + RequestException.rootMessage(e)); + private void keepSentChat(ConnectionListener.ChatScope scope, String peerUniqueId, String message) { + ClientEntry self = model.getClient(selfClientId); + String uid = identity != null ? identity.getUid().toBase64() : ""; + chatLog.sent(scope, selfClientId, uid, self != null ? self.nickname : settings.nickname, peerUniqueId, message); } /** Whether the client is in the channel we are in ourselves. Package-private: also