Frontends reach a server only through ServerSession
session.connection() is gone: the session hands out the model, our ids, the microphone and speakers, ping and connection info, the server's unique id, icon and avatar stores, myTeamSpeak, the connect error and a blocking disconnectNow. The connection's remaining actions move into helpers the session hands out instead of forwarding each method: ClientAdmin (move, kick, ban, groups, talk power, poke, client details), ChannelFiles (listing, mkdir, rename, delete, transfers and their sounds) and ClientContacts (display names, volume, auto-mute, whisper policy, sightings); ChannelAdmin also takes join, move, subscribe and description requests. Dialogs take the session instead of the connection. Android's network handover now uses disconnectNow, which also cancels a pending auto-reconnect. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -23,10 +23,12 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.CRC32;
|
||||
|
||||
/**
|
||||
* Everything the channel editor needs of the server: reading and writing a channel's
|
||||
* properties and its needed-power permissions, plus the virtual server's icon store.
|
||||
* What we do with channels: joining, moving and subscribing to them, and what the channel
|
||||
* editor needs of the server: reading and writing a channel's properties and its
|
||||
* needed-power permissions, plus the virtual server's icon store.
|
||||
*
|
||||
* <p>Requests run in the background and fail with a {@link RequestException}.
|
||||
* <p>Requests run in the background and fail with a {@link RequestException}; the actions
|
||||
* that return nothing report a failure in the log.
|
||||
*/
|
||||
public final class ChannelAdmin {
|
||||
|
||||
@@ -38,6 +40,7 @@ public final class ChannelAdmin {
|
||||
|
||||
private final Uplink uplink;
|
||||
private final ServerModel model;
|
||||
private final Announcer announce;
|
||||
|
||||
/**
|
||||
* The {@code channelpermlist} reply in progress. TeamSpeak answers the command with a
|
||||
@@ -47,9 +50,79 @@ public final class ChannelAdmin {
|
||||
*/
|
||||
private volatile PermRequest permRequest;
|
||||
|
||||
ChannelAdmin(Uplink uplink, ServerModel model) {
|
||||
ChannelAdmin(Uplink uplink, ServerModel model, Announcer announce) {
|
||||
this.uplink = uplink;
|
||||
this.model = model;
|
||||
this.announce = announce;
|
||||
}
|
||||
|
||||
// ---- channel actions ----
|
||||
|
||||
/** @param password the channel's password, or null or empty when it has none */
|
||||
public void join(int channelId, String password) {
|
||||
act("Could not join channel: ", sock ->
|
||||
sock.joinChannel(channelId, (password == null || password.isEmpty()) ? null : password));
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-parents and repositions a channel.
|
||||
*
|
||||
* @param orderPredecessorId the channel this one should sit below among its new
|
||||
* siblings, or 0 to place it first
|
||||
*/
|
||||
public void move(int channelId, int newParentId, int orderPredecessorId) {
|
||||
ChannelNode channel = model.getChannel(channelId);
|
||||
// The server refuses channelmove within the same parent ("already member of
|
||||
// channel"); sorting among the same siblings is an edit of channel_order.
|
||||
boolean reorder = channel != null && channel.parentId == newParentId;
|
||||
act("Could not move channel: ", sock -> {
|
||||
SingleCommand cmd;
|
||||
if (reorder) {
|
||||
cmd = new SingleCommand("channeledit", ProtocolRole.CLIENT);
|
||||
cmd.add(new CommandSingleParameter("cid", Integer.toString(channelId)));
|
||||
cmd.add(new CommandSingleParameter("channel_order", Integer.toString(orderPredecessorId)));
|
||||
} else {
|
||||
cmd = new SingleCommand("channelmove", ProtocolRole.CLIENT);
|
||||
cmd.add(new CommandSingleParameter("cid", Integer.toString(channelId)));
|
||||
cmd.add(new CommandSingleParameter("cpid", Integer.toString(newParentId)));
|
||||
cmd.add(new CommandSingleParameter("order", Integer.toString(orderPredecessorId)));
|
||||
}
|
||||
sock.executeCommand(cmd).complete();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribes to (or unsubscribes from) a set of channels in one command. The model
|
||||
* is left alone: the server answers with the subscription events that update it.
|
||||
*/
|
||||
public void setSubscribed(Collection<Integer> channelIds, boolean subscribed) {
|
||||
if (channelIds.isEmpty()) return;
|
||||
List<Integer> ids = List.copyOf(channelIds);
|
||||
String name = subscribed ? "channelsubscribe" : "channelunsubscribe";
|
||||
act(subscribed ? "Could not subscribe: " : "Could not unsubscribe: ", sock -> {
|
||||
List<SingleCommand> parts = new ArrayList<>(ids.size());
|
||||
for (int id : ids) {
|
||||
parts.add(new SingleCommand(name, ProtocolRole.CLIENT,
|
||||
new CommandSingleParameter("cid", Integer.toString(id))));
|
||||
}
|
||||
sock.executeCommand(new MultiCommand(name, ProtocolRole.CLIENT, parts)).complete();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks for a channel's description. The answer arrives as a channel edit, see
|
||||
* {@link ConnectionEventHandler#onChannelEdit}. Uses {@code channelgetdescription}, not
|
||||
* {@code channelinfo}, which counts heavily against the server's flood protection. May
|
||||
* be refused; the description then stays unknown.
|
||||
*/
|
||||
public void requestInfo(int channelId) {
|
||||
uplink.submit(sock -> sock.executeCommand(new SingleCommand("channelgetdescription", ProtocolRole.CLIENT,
|
||||
new CommandSingleParameter("cid", Integer.toString(channelId)))).complete(), e -> {
|
||||
});
|
||||
}
|
||||
|
||||
private void act(String errorPrefix, Uplink.Action action) {
|
||||
uplink.submit(action, e -> announce.error(errorPrefix, e));
|
||||
}
|
||||
|
||||
// ---- channel properties ----
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package eu.lixko.ts3j.client.net;
|
||||
|
||||
import eu.lixko.ts3j.client.net.filetransfer.FileTransfer;
|
||||
import eu.lixko.ts3j.client.net.filetransfer.RemoteFile;
|
||||
import eu.lixko.ts3j.client.sound.SoundEvent;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* The channels' file repositories: browsing, managing and transferring files. The listing
|
||||
* and management calls block; call them off the UI thread. They throw when not connected
|
||||
* or when the server refuses.
|
||||
*/
|
||||
public final class ChannelFiles {
|
||||
|
||||
private final Uplink uplink;
|
||||
private final Announcer announce;
|
||||
|
||||
ChannelFiles(Uplink uplink, Announcer announce) {
|
||||
this.uplink = uplink;
|
||||
this.announce = announce;
|
||||
}
|
||||
|
||||
/** Whether a live connection capable of file transfers is available. */
|
||||
public boolean isAvailable() {
|
||||
return uplink.canTransferFiles();
|
||||
}
|
||||
|
||||
/** Lists the files under {@code path} in a channel's repository. */
|
||||
public List<RemoteFile> list(int channelId, String channelPassword, String path) throws Exception {
|
||||
return uplink.files().list(channelId, channelPassword, path);
|
||||
}
|
||||
|
||||
public void createDirectory(int channelId, String channelPassword, String dirPath) throws Exception {
|
||||
uplink.files().createDirectory(channelId, channelPassword, dirPath);
|
||||
}
|
||||
|
||||
public void delete(int channelId, String channelPassword, String fullPath) throws Exception {
|
||||
uplink.files().delete(channelId, channelPassword, fullPath);
|
||||
}
|
||||
|
||||
public void rename(int channelId, String channelPassword, String oldFullPath, String newFullPath)
|
||||
throws Exception {
|
||||
uplink.files().rename(channelId, channelPassword, oldFullPath, newFullPath);
|
||||
}
|
||||
|
||||
/** Begins downloading a channel file to {@code target}; progress arrives via {@code listener}. */
|
||||
public FileTransfer download(int channelId, String channelPassword, String remoteFullPath,
|
||||
File target, FileTransfer.Listener listener) {
|
||||
return uplink.files().download(channelId, channelPassword, remoteFullPath, target, announcing(listener));
|
||||
}
|
||||
|
||||
/** Begins uploading {@code source} into a channel; progress arrives via {@code listener}. */
|
||||
public FileTransfer upload(int channelId, String channelPassword, String remoteFullPath,
|
||||
File source, boolean overwrite, FileTransfer.Listener listener) {
|
||||
return uplink.files().upload(channelId, channelPassword, remoteFullPath, source, overwrite, announcing(listener));
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a transfer listener so the completion and failure sounds play once the
|
||||
* transfer finishes, whoever is watching it.
|
||||
*/
|
||||
private FileTransfer.Listener announcing(FileTransfer.Listener listener) {
|
||||
AtomicBoolean announced = new AtomicBoolean();
|
||||
return transfer -> {
|
||||
FileTransfer.State state = transfer.getState();
|
||||
if (state != FileTransfer.State.CANCELLED && transfer.isDone()
|
||||
&& announced.compareAndSet(false, true)) {
|
||||
announce.sound(state == FileTransfer.State.COMPLETED
|
||||
? SoundEvent.OTHER_FILETRANSFER_COMPLETE
|
||||
: SoundEvent.OTHER_FILETRANSFER_FAILED, announce.serverVars());
|
||||
}
|
||||
if (listener != null) listener.onTransferChanged(transfer);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
package eu.lixko.ts3j.client.net;
|
||||
|
||||
import com.github.manevolent.ts3j.api.Client;
|
||||
import com.github.manevolent.ts3j.command.MultiCommand;
|
||||
import com.github.manevolent.ts3j.command.SingleCommand;
|
||||
import com.github.manevolent.ts3j.command.parameter.CommandSingleParameter;
|
||||
import com.github.manevolent.ts3j.protocol.ProtocolRole;
|
||||
import com.github.manevolent.ts3j.protocol.socket.client.LocalTeamspeakClientSocket;
|
||||
import eu.lixko.ts3j.client.sound.SoundEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.IntSupplier;
|
||||
|
||||
/**
|
||||
* What we do to other clients (or ourselves among them): moving, kicking, banning, groups,
|
||||
* talk power, pokes, and reading or editing their details.
|
||||
*
|
||||
* <p>The actions run in the background and report a failure in the log; the requests that
|
||||
* return a future fail it with a {@link RequestException} instead.
|
||||
*/
|
||||
public final class ClientAdmin {
|
||||
|
||||
/** The longest reason the protocol carries with a kick. */
|
||||
public static final int KICK_REASON_LIMIT = 40;
|
||||
|
||||
/** The protocol's reason ids for the two flavours of <b>clientkick</b>. */
|
||||
private static final int REASON_KICK_CHANNEL = 4;
|
||||
private static final int REASON_KICK_SERVER = 5;
|
||||
|
||||
private final Uplink uplink;
|
||||
private final ServerModel model;
|
||||
private final Announcer announce;
|
||||
private final IntSupplier selfClientId;
|
||||
/** Tells the frontend a client's details changed where the server will not. */
|
||||
private final Runnable infoUpdated;
|
||||
|
||||
ClientAdmin(Uplink uplink, ServerModel model, Announcer announce, IntSupplier selfClientId, Runnable infoUpdated) {
|
||||
this.uplink = uplink;
|
||||
this.model = model;
|
||||
this.announce = announce;
|
||||
this.selfClientId = selfClientId;
|
||||
this.infoUpdated = infoUpdated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves several clients (ourselves among them, if wanted) into a channel in one piped
|
||||
* command. Clients already in it are left out, which the server would refuse.
|
||||
*/
|
||||
public void move(Collection<Integer> clientIds, int channelId) {
|
||||
List<SingleCommand> rows = new ArrayList<>(clientIds.size());
|
||||
for (int id : clientIds) {
|
||||
ClientEntry client = model.getClient(id);
|
||||
if (client != null && client.channelId == channelId) continue;
|
||||
rows.add(new SingleCommand("clientmove", ProtocolRole.CLIENT,
|
||||
new CommandSingleParameter("clid", Integer.toString(id))));
|
||||
}
|
||||
if (rows.isEmpty()) return;
|
||||
act(rows.size() == 1 ? "Could not move client: " : "Could not move clients: ", sock -> {
|
||||
MultiCommand cmd = new MultiCommand("clientmove", ProtocolRole.CLIENT, rows);
|
||||
cmd.add(new CommandSingleParameter("cid", Integer.toString(channelId)));
|
||||
sock.executeCommand(cmd).complete();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Kicks clients out of their channels in one command; we are never among them, nor are
|
||||
* clients already in the default channel, as the server would refuse the whole command.
|
||||
*/
|
||||
public void kickFromChannel(Collection<Integer> clientIds, String reason) {
|
||||
int defaultChannel = model.defaultChannelId();
|
||||
List<Integer> ids = new ArrayList<>(clientIds.size());
|
||||
for (int id : clientIds) {
|
||||
ClientEntry client = model.getClient(id);
|
||||
if (client == null || client.channelId != defaultChannel) ids.add(id);
|
||||
}
|
||||
kick(ids, REASON_KICK_CHANNEL, reason, "channel");
|
||||
}
|
||||
|
||||
/** Kicks clients off the server in one command; we are never among them. */
|
||||
public void kickFromServer(Collection<Integer> clientIds, String reason) {
|
||||
kick(clientIds, REASON_KICK_SERVER, reason, "server");
|
||||
}
|
||||
|
||||
private void kick(Collection<Integer> clientIds, int reasonId, String reason, String from) {
|
||||
int self = selfClientId.getAsInt();
|
||||
List<SingleCommand> rows = new ArrayList<>(clientIds.size());
|
||||
for (int id : clientIds) {
|
||||
if (id == self) continue;
|
||||
rows.add(new SingleCommand("clientkick", ProtocolRole.CLIENT,
|
||||
new CommandSingleParameter("clid", Integer.toString(id))));
|
||||
}
|
||||
if (rows.isEmpty()) return;
|
||||
String errorPrefix = "Could not kick " + (rows.size() == 1 ? "client" : "clients") + " from the " + from + ": ";
|
||||
act(errorPrefix, sock -> {
|
||||
MultiCommand cmd = new MultiCommand("clientkick", ProtocolRole.CLIENT, rows);
|
||||
cmd.add(new CommandSingleParameter("reasonid", Integer.toString(reasonId)));
|
||||
if (reason != null && !reason.isEmpty()) {
|
||||
cmd.add(new CommandSingleParameter("reasonmsg", reason));
|
||||
}
|
||||
sock.executeCommand(cmd).complete();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Bans a client from the server.
|
||||
*
|
||||
* @param seconds how long the ban lasts, or 0 for a permanent one
|
||||
*/
|
||||
public void ban(int clientId, long seconds, String reason) {
|
||||
act("Could not ban client: ", sock -> sock.banClient(clientId,
|
||||
seconds <= 0 ? null : (int) Math.min(seconds, Integer.MAX_VALUE),
|
||||
reason == null || reason.isEmpty() ? null : reason));
|
||||
}
|
||||
|
||||
/** Assigns or removes a server group for a client (by database id, as {@code servergroupaddclient} needs). */
|
||||
public void setServerGroup(int clientDatabaseId, int groupId, boolean assign) {
|
||||
act("Could not " + (assign ? "assign" : "remove") + " server group: ", sock -> {
|
||||
if (assign) sock.serverGroupAddClient(groupId, clientDatabaseId);
|
||||
else sock.serverGroupRemoveClient(groupId, clientDatabaseId);
|
||||
});
|
||||
}
|
||||
|
||||
/** Assigns a channel group for a client in the channel it currently sits in. */
|
||||
public void setChannelGroup(int clientDatabaseId, int channelId, int groupId) {
|
||||
act("Could not set channel group: ", sock -> {
|
||||
SingleCommand cmd = new SingleCommand("setclientchannelgroup", ProtocolRole.CLIENT);
|
||||
cmd.add(new CommandSingleParameter("cgid", Integer.toString(groupId)));
|
||||
cmd.add(new CommandSingleParameter("cid", Integer.toString(channelId)));
|
||||
cmd.add(new CommandSingleParameter("cldbid", Integer.toString(clientDatabaseId)));
|
||||
sock.executeCommand(cmd).complete();
|
||||
});
|
||||
}
|
||||
|
||||
public void poke(int clientId, String message) {
|
||||
act("Poke failed: ", sock -> {
|
||||
sock.clientPoke(clientId, message);
|
||||
announce.sound(SoundEvent.OTHER_SENT_POKE, announce.clientVars(clientId, null));
|
||||
});
|
||||
}
|
||||
|
||||
/** Grants or revokes clients' talk power, which lets them talk whatever their channel needs. */
|
||||
public void setTalker(Collection<Integer> clientIds, boolean talker) {
|
||||
if (clientIds.isEmpty()) return;
|
||||
act(talker ? "Error granting talk power: " : "Error revoking talk power: ", sock -> {
|
||||
for (int id : clientIds) setTalker(sock, id, talker);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Grants talk power to whoever in a channel has waited longest for it, first revoking
|
||||
* it from everyone who has it there when {@code revokeOthers}.
|
||||
*/
|
||||
public void grantNextTalkPower(int channelId, boolean revokeOthers) {
|
||||
ClientEntry next = model.nextTalkRequest(channelId);
|
||||
List<ClientEntry> talkers = revokeOthers ? model.talkersIn(channelId) : List.of();
|
||||
if (next == null && talkers.isEmpty()) return;
|
||||
act("Error granting talk power: ", sock -> {
|
||||
for (ClientEntry c : talkers) {
|
||||
if (c != next) setTalker(sock, c.id, false);
|
||||
}
|
||||
if (next != null) setTalker(sock, next.id, true);
|
||||
});
|
||||
}
|
||||
|
||||
private static void setTalker(LocalTeamspeakClientSocket sock, int clientId, boolean talker) throws Exception {
|
||||
sock.executeCommand(new SingleCommand("clientedit", ProtocolRole.CLIENT,
|
||||
new CommandSingleParameter("clid", Integer.toString(clientId)),
|
||||
new CommandSingleParameter("client_is_talker", talker ? "1" : "0"))).complete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks for a client's version and platform, the only details the enter-view event
|
||||
* lacks. The answer arrives as a client update, see {@link ConnectionEventHandler#onClientChanged}.
|
||||
* Uses {@code clientgetvariables}, not {@code clientinfo}, which counts heavily against the
|
||||
* server's flood protection. May be refused; the details then stay as they are.
|
||||
*/
|
||||
public void requestInfo(int clientId) {
|
||||
uplink.submit(sock -> sock.executeCommand(new SingleCommand("clientgetvariables", ProtocolRole.CLIENT,
|
||||
new CommandSingleParameter("clid", Integer.toString(clientId)))).complete(), e -> {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a client's description straight from the server, so an editor opens on the
|
||||
* live text instead of whatever an earlier info request cached.
|
||||
*/
|
||||
public CompletableFuture<String> description(int clientId) {
|
||||
return uplink.request(() -> {
|
||||
Client c = uplink.socket().getClientInfo(clientId);
|
||||
String description = c == null ? "" : TeamspeakConnection.orEmpty(c.get("client_description"));
|
||||
ClientEntry e = model.getClient(clientId);
|
||||
if (e != null) e.description = description;
|
||||
return description;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes a client's description. Unlike the nickname or the away state, this is not
|
||||
* a {@code clientupdate} property even for ourselves: the server only takes it
|
||||
* through {@code clientedit}, gated by {@code b_client_modify_own_description}.
|
||||
*/
|
||||
public CompletableFuture<Void> setDescription(int clientId, String description) {
|
||||
boolean self = clientId == selfClientId.getAsInt();
|
||||
return uplink.requestVoid(() -> {
|
||||
SingleCommand cmd = new SingleCommand("clientedit", ProtocolRole.CLIENT);
|
||||
cmd.add(new CommandSingleParameter("clid", Integer.toString(clientId)));
|
||||
cmd.add(new CommandSingleParameter("client_description", description));
|
||||
uplink.socket().executeCommand(cmd).complete();
|
||||
// Only the other clients are told about our own update, so the local entry
|
||||
// (and the info panel showing it) has to be caught up here.
|
||||
if (self) {
|
||||
ClientEntry e = model.getClient(clientId);
|
||||
if (e != null) e.description = description;
|
||||
infoUpdated.run();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void act(String errorPrefix, Uplink.Action action) {
|
||||
uplink.submit(action, e -> announce.error(errorPrefix, e));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package eu.lixko.ts3j.client.net;
|
||||
|
||||
import eu.lixko.ts3j.client.audio.VoiceOutput;
|
||||
import eu.lixko.ts3j.client.config.Settings;
|
||||
import eu.lixko.ts3j.client.contacts.Contact;
|
||||
import eu.lixko.ts3j.client.contacts.ContactStore;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* The contact list as it applies to the clients on this server: the names they are shown
|
||||
* under, their automatic mute and volume, whose whispers we hear, and where they were seen.
|
||||
*/
|
||||
public final class ClientContacts {
|
||||
|
||||
private final ContactStore store;
|
||||
private final Settings settings;
|
||||
private final ServerModel model;
|
||||
private final OwnClient own;
|
||||
private final Uplink uplink;
|
||||
/** The server address we are on, recorded with a sighting. */
|
||||
private final Supplier<String> address;
|
||||
|
||||
ClientContacts(ContactStore store, Settings settings, ServerModel model, OwnClient own, Uplink uplink,
|
||||
Supplier<String> address) {
|
||||
this.store = store;
|
||||
this.settings = settings;
|
||||
this.model = model;
|
||||
this.own = own;
|
||||
this.uplink = uplink;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
/** Friends, blocked clients and their per-client filters, shared by every server. */
|
||||
public ContactStore store() {
|
||||
return store;
|
||||
}
|
||||
|
||||
/** The name a client is shown under: its contact's custom name where it has one. */
|
||||
public String displayName(ClientEntry client) {
|
||||
return store.displayName(client.uniqueId, client.nickname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a whisper from a client is played, per the global whisper policy and the
|
||||
* client's contact entry. A client that has already left is judged by the policy alone.
|
||||
*/
|
||||
boolean isWhisperAllowed(int clientId) {
|
||||
Settings.WhisperPolicy policy = settings.whisperPolicy;
|
||||
if (policy == Settings.WhisperPolicy.NOBODY) return false;
|
||||
ClientEntry c = model.getClient(clientId);
|
||||
Contact contact = c == null ? null : store.find(c.uniqueId);
|
||||
if (contact != null) return contact.whisperAllowed;
|
||||
return policy == Settings.WhisperPolicy.ALLOW_UNLESS_DENIED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a client's contact entry to its playback — the automatic mute and the
|
||||
* volume modifier — and records the sighting. Called when the client appears, which
|
||||
* is also when its myTeamSpeak ID can first match a contact.
|
||||
*/
|
||||
void apply(ClientEntry client) {
|
||||
store.link(client.uniqueId, client.myTeamSpeakId);
|
||||
Contact contact = applyPlayback(client);
|
||||
if (contact != null) store.markSeen(client.uniqueId, model.getServerName(), address.get());
|
||||
}
|
||||
|
||||
private Contact applyPlayback(ClientEntry client) {
|
||||
if (client == null || client.self) return null;
|
||||
Contact contact = store.find(client.uniqueId);
|
||||
VoiceOutput out = own.playback();
|
||||
if (contact != null && out != null) {
|
||||
if (contact.automute) out.setClientMuted(client.id, true);
|
||||
out.setClientVolume(client.id, contact.volumeGain());
|
||||
}
|
||||
return contact;
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-applies every contact entry to the clients on the server, after the list changed.
|
||||
* A client that just became a contact is also marked as seen. One seen before keeps
|
||||
* where it was seen: with it on two servers, each re-application would claim it for its
|
||||
* own server, which is a change that triggers the other's re-application, and so on.
|
||||
*/
|
||||
public void applyAll() {
|
||||
if (!uplink.isUp()) return;
|
||||
for (ClientEntry c : model.allClients()) {
|
||||
Contact contact = applyPlayback(c);
|
||||
if (contact != null && contact.lastSeen == null) {
|
||||
store.markSeen(c.uniqueId, model.getServerName(), address.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Remembers a per-client volume, in the contact entry when the client has one. */
|
||||
public void setVolumeModifier(ClientEntry client, double decibels) {
|
||||
VoiceOutput out = own.playback();
|
||||
if (out != null) out.setClientVolume(client.id, Math.pow(10.0, decibels / 20.0));
|
||||
Contact contact = store.find(client.uniqueId);
|
||||
if (contact == null) return;
|
||||
contact.hasVolumeModifier = decibels != 0;
|
||||
contact.volumeModifier = decibels;
|
||||
store.put(contact);
|
||||
}
|
||||
|
||||
public double volumeModifier(ClientEntry client) {
|
||||
VoiceOutput out = own.playback();
|
||||
if (out == null) return 0;
|
||||
double gain = out.getClientVolume(client.id);
|
||||
return gain <= 0 ? -60 : 20.0 * Math.log10(gain);
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,7 @@ final class ConnectionEventHandler implements TS3Listener {
|
||||
c.self = (e.getClientId() == conn.getSelfClientId());
|
||||
// Query clients stay out of sight, as they do in the tree and in TS3's log.
|
||||
if (!c.self && !c.isQuery()) {
|
||||
conn.applyContact(c);
|
||||
conn.contacts.apply(c);
|
||||
announceClientEntered(e);
|
||||
logClientEntered(e);
|
||||
}
|
||||
@@ -308,7 +308,7 @@ final class ConnectionEventHandler implements TS3Listener {
|
||||
if (reported(e, "client_description") || has(e, "client_version") || has(e, "client_platform")) {
|
||||
conn.ui.onInfoUpdated();
|
||||
}
|
||||
if (reported(e, "client_myteamspeak_id") && !c.self && !c.isQuery()) conn.applyContact(c);
|
||||
if (reported(e, "client_myteamspeak_id") && !c.self && !c.isQuery()) conn.contacts.apply(c);
|
||||
if (has(e, "client_unread_messages") && c.id == conn.getSelfClientId()) conn.unreadMessagesChanged(unreadBefore);
|
||||
announceClientUpdate(e, c, renamed, oldName);
|
||||
conn.ui.onModelChanged();
|
||||
@@ -479,7 +479,7 @@ final class ConnectionEventHandler implements TS3Listener {
|
||||
ChannelNode ch = conn.getModel().getChannel(safeInt(e, "cid"));
|
||||
if (ch == null || !ch.descriptionLoaded) return;
|
||||
ch.descriptionLoaded = false;
|
||||
conn.requestChannelInfo(ch.id);
|
||||
conn.channels.requestInfo(ch.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -646,7 +646,7 @@ final class ConnectionEventHandler implements TS3Listener {
|
||||
@Override
|
||||
public void onTextMessage(TextMessageEvent e) {
|
||||
if (e.getInvokerId() == conn.getSelfClientId()) return; // don't echo our own
|
||||
Contact contact = conn.contacts.find(e.getInvokerUniqueId());
|
||||
Contact contact = conn.contacts.store().find(e.getInvokerUniqueId());
|
||||
if (contact != null) {
|
||||
boolean isPrivate = e.getTargetMode() == TextMessageTargetMode.CLIENT;
|
||||
if (isPrivate ? contact.ignorePrivateMessages : contact.ignorePublicMessages) return;
|
||||
@@ -674,10 +674,10 @@ final class ConnectionEventHandler implements TS3Listener {
|
||||
|
||||
@Override
|
||||
public void onClientPoke(ClientPokeEvent e) {
|
||||
Contact contact = conn.contacts.find(e.getInvokerUniqueId());
|
||||
Contact contact = conn.contacts.store().find(e.getInvokerUniqueId());
|
||||
if (contact != null && contact.ignorePokes) return;
|
||||
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()));
|
||||
String from = conn.contacts.store().displayName(e.getInvokerUniqueId(), TeamspeakConnection.orEmpty(e.getInvokerName()));
|
||||
conn.ui.onPoke(from, TeamspeakConnection.orEmpty(e.get("msg")));
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -125,7 +125,7 @@ final class ConnectionStatsCollector {
|
||||
Long last = variablesAskedAt.get(clientId);
|
||||
if (last != null && now - last < VARIABLES_REFRESH_MS) return;
|
||||
variablesAskedAt.put(clientId, now);
|
||||
conn.requestClientInfo(clientId);
|
||||
conn.clients.requestInfo(clientId);
|
||||
}
|
||||
|
||||
/** Our own traffic, straight from the socket. */
|
||||
|
||||
@@ -15,31 +15,22 @@ import com.github.manevolent.ts3j.util.Ts3Crypt;
|
||||
import eu.lixko.ts3j.client.audio.AudioBackend;
|
||||
import eu.lixko.ts3j.client.audio.VoiceOutput;
|
||||
import eu.lixko.ts3j.client.config.Settings;
|
||||
import eu.lixko.ts3j.client.contacts.Contact;
|
||||
import eu.lixko.ts3j.client.contacts.ContactStore;
|
||||
import eu.lixko.ts3j.client.myts.MyTeamSpeakIdentity;
|
||||
import eu.lixko.ts3j.client.myts.MyTeamSpeakLogin;
|
||||
import eu.lixko.ts3j.client.net.avatar.Avatar;
|
||||
import eu.lixko.ts3j.client.net.avatar.AvatarNaming;
|
||||
import eu.lixko.ts3j.client.net.avatar.AvatarRepository;
|
||||
import eu.lixko.ts3j.client.net.filetransfer.FileTransfer;
|
||||
import eu.lixko.ts3j.client.net.filetransfer.FileTransferManager;
|
||||
import eu.lixko.ts3j.client.net.ban.BanEntry;
|
||||
import eu.lixko.ts3j.client.net.ban.BanRule;
|
||||
import eu.lixko.ts3j.client.net.filetransfer.RemoteFile;
|
||||
import eu.lixko.ts3j.client.sound.SoundEvent;
|
||||
import eu.lixko.ts3j.client.sound.SoundNotifier;
|
||||
import eu.lixko.ts3j.client.text.TsLink;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
@@ -52,12 +43,6 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
/** Upper bound for a group icon; anything larger is not an icon. Also used by {@link ChannelAdmin}. */
|
||||
static final int MAX_ICON_BYTES = 1024 * 1024;
|
||||
|
||||
/** The longest reason the protocol carries with a kick. */
|
||||
public static final int KICK_REASON_LIMIT = 40;
|
||||
|
||||
/** The protocol's reason ids for the two flavours of <b>clientkick</b>. */
|
||||
private static final int REASON_KICK_CHANNEL = 4;
|
||||
private static final int REASON_KICK_SERVER = 5;
|
||||
/** The reason ids of our own leaving that nobody chose: timing out, the server going down. */
|
||||
private static final int REASON_CONNECTION_LOST = 3;
|
||||
private static final int REASON_SERVER_SHUTDOWN = 11;
|
||||
@@ -78,7 +63,7 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
private final IconRepository icons;
|
||||
private final AvatarRepository avatars;
|
||||
/** Friends, blocked clients and their per-client filters; also read by {@link ConnectionEventHandler}. */
|
||||
final ContactStore contacts;
|
||||
final ClientContacts 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}. */
|
||||
@@ -89,6 +74,9 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
final ConnectionStatsCollector stats = new ConnectionStatsCollector(this);
|
||||
/** The server administration helpers; package-private for {@link ConnectionEventHandler}, which feeds them replies. */
|
||||
final ChannelAdmin channels;
|
||||
/** Package-private for {@link ConnectionStatsCollector}, which asks for client details. */
|
||||
final ClientAdmin clients;
|
||||
private final ChannelFiles files;
|
||||
final BanAdmin bans = new BanAdmin(uplink);
|
||||
final PrivilegeKeyAdmin privilegeKeys = new PrivilegeKeyAdmin(uplink);
|
||||
final ComplaintAdmin complaints;
|
||||
@@ -164,19 +152,21 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
SoundNotifier sounds, ContactStore contacts) {
|
||||
this.settings = settings;
|
||||
this.ui = ui;
|
||||
this.contacts = contacts;
|
||||
this.chatLog = new ChatLog(settings, ui);
|
||||
this.announce = new Announcer(model, sounds, ui, chatLog, () -> ownClient().isDeafened(), uplink::isUp);
|
||||
this.events = new ConnectionEventHandler(this);
|
||||
this.icons = new IconRepository(this::fetchIcon, ui::onIconsUpdated);
|
||||
this.avatars = new AvatarRepository(this::fetchAvatar, ui::onInfoUpdated);
|
||||
this.channels = new ChannelAdmin(uplink, model);
|
||||
this.channels = new ChannelAdmin(uplink, model, announce);
|
||||
this.clients = new ClientAdmin(uplink, model, announce, () -> selfClientId, ui::onInfoUpdated);
|
||||
this.files = new ChannelFiles(uplink, announce);
|
||||
this.complaints = new ComplaintAdmin(uplink, announce);
|
||||
this.offlineMessages = new OfflineMessageAdmin(uplink, announce);
|
||||
this.avatarAdmin = new AvatarAdmin(uplink, model.permissions(), avatars, this::getSelfUniqueId,
|
||||
flag -> ownClient().updateEntry(self -> self.avatarFlag = flag));
|
||||
this.own = new OwnClient(settings, audio, uplink, model, ui, announce, () -> selfClientId);
|
||||
this.recording = new Recording(audio, uplink, model, own, this::getSelfUniqueId);
|
||||
this.contacts = new ClientContacts(contacts, settings, model, own, uplink, () -> serverHost + ":" + serverPort);
|
||||
}
|
||||
|
||||
public ServerModel getModel() {
|
||||
@@ -286,7 +276,7 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
client.addListener(events);
|
||||
client.setVoiceHandler(playback::handleVoice);
|
||||
client.setWhisperHandler(whisper -> {
|
||||
if (!isWhisperAllowed(whisper.getClientId())) return;
|
||||
if (!contacts.isWhisperAllowed(whisper.getClientId())) return;
|
||||
lastWhisperer = whisper.getClientId();
|
||||
playback.handleWhisper(whisper);
|
||||
});
|
||||
@@ -531,123 +521,6 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
|
||||
// ---- self actions ----
|
||||
|
||||
public void joinChannel(int channelId, String password) {
|
||||
act("Could not join channel: ", sock ->
|
||||
sock.joinChannel(channelId, (password == null || password.isEmpty()) ? null : password));
|
||||
}
|
||||
|
||||
/** Moves another client (or ourselves) into a channel. */
|
||||
public void moveClient(int clientId, int channelId, String password) {
|
||||
act("Could not move client: ", sock -> sock.clientMove(clientId, channelId,
|
||||
(password == null || password.isEmpty()) ? null : password));
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves several clients (ourselves among them, if wanted) into a channel in one piped
|
||||
* command. Clients already in it are left out, which the server would refuse.
|
||||
*/
|
||||
public void moveClients(Collection<Integer> clientIds, int channelId) {
|
||||
List<SingleCommand> rows = new ArrayList<>(clientIds.size());
|
||||
for (int id : clientIds) {
|
||||
ClientEntry client = model.getClient(id);
|
||||
if (client != null && client.channelId == channelId) continue;
|
||||
rows.add(new SingleCommand("clientmove", ProtocolRole.CLIENT,
|
||||
new CommandSingleParameter("clid", Integer.toString(id))));
|
||||
}
|
||||
if (rows.isEmpty()) return;
|
||||
act(rows.size() == 1 ? "Could not move client: " : "Could not move clients: ", sock -> {
|
||||
MultiCommand cmd = new MultiCommand("clientmove", ProtocolRole.CLIENT, rows);
|
||||
cmd.add(new CommandSingleParameter("cid", Integer.toString(channelId)));
|
||||
sock.executeCommand(cmd).complete();
|
||||
});
|
||||
}
|
||||
|
||||
/** Kicks a client out of its channel, back into the server's default one. */
|
||||
public void kickFromChannel(int clientId, String reason) {
|
||||
kickFromChannel(List.of(clientId), reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* Kicks clients out of their channels in one command; we are never among them, nor are
|
||||
* clients already in the default channel, as the server would refuse the whole command.
|
||||
*/
|
||||
public void kickFromChannel(Collection<Integer> clientIds, String reason) {
|
||||
int defaultChannel = model.defaultChannelId();
|
||||
List<Integer> ids = new ArrayList<>(clientIds.size());
|
||||
for (int id : clientIds) {
|
||||
ClientEntry client = model.getClient(id);
|
||||
if (client == null || client.channelId != defaultChannel) ids.add(id);
|
||||
}
|
||||
kick(ids, REASON_KICK_CHANNEL, reason, "channel");
|
||||
}
|
||||
|
||||
/** Kicks a client off the server entirely. */
|
||||
public void kickFromServer(int clientId, String reason) {
|
||||
kickFromServer(List.of(clientId), reason);
|
||||
}
|
||||
|
||||
/** Kicks clients off the server in one command; we are never among them. */
|
||||
public void kickFromServer(Collection<Integer> clientIds, String reason) {
|
||||
kick(clientIds, REASON_KICK_SERVER, reason, "server");
|
||||
}
|
||||
|
||||
private void kick(Collection<Integer> clientIds, int reasonId, String reason, String from) {
|
||||
List<SingleCommand> rows = new ArrayList<>(clientIds.size());
|
||||
for (int id : clientIds) {
|
||||
if (id == selfClientId) continue;
|
||||
rows.add(new SingleCommand("clientkick", ProtocolRole.CLIENT,
|
||||
new CommandSingleParameter("clid", Integer.toString(id))));
|
||||
}
|
||||
if (rows.isEmpty()) return;
|
||||
String errorPrefix = "Could not kick " + (rows.size() == 1 ? "client" : "clients") + " from the " + from + ": ";
|
||||
act(errorPrefix, sock -> {
|
||||
MultiCommand cmd = new MultiCommand("clientkick", ProtocolRole.CLIENT, rows);
|
||||
cmd.add(new CommandSingleParameter("reasonid", Integer.toString(reasonId)));
|
||||
if (reason != null && !reason.isEmpty()) {
|
||||
cmd.add(new CommandSingleParameter("reasonmsg", reason));
|
||||
}
|
||||
sock.executeCommand(cmd).complete();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Bans a client from the server.
|
||||
*
|
||||
* @param seconds how long the ban lasts, or 0 for a permanent one
|
||||
*/
|
||||
public void banClient(int clientId, long seconds, String reason) {
|
||||
act("Could not ban client: ", sock -> sock.banClient(clientId,
|
||||
seconds <= 0 ? null : (int) Math.min(seconds, Integer.MAX_VALUE),
|
||||
reason == null || reason.isEmpty() ? null : reason));
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-parents and repositions a channel.
|
||||
*
|
||||
* @param orderPredecessorId the channel this one should sit below among its new
|
||||
* siblings, or 0 to place it first
|
||||
*/
|
||||
public void moveChannel(int channelId, int newParentId, int orderPredecessorId) {
|
||||
ChannelNode channel = model.getChannel(channelId);
|
||||
// The server refuses channelmove within the same parent ("already member of
|
||||
// channel"); sorting among the same siblings is an edit of channel_order.
|
||||
boolean reorder = channel != null && channel.parentId == newParentId;
|
||||
act("Could not move channel: ", sock -> {
|
||||
SingleCommand cmd;
|
||||
if (reorder) {
|
||||
cmd = new SingleCommand("channeledit", ProtocolRole.CLIENT);
|
||||
cmd.add(new CommandSingleParameter("cid", Integer.toString(channelId)));
|
||||
cmd.add(new CommandSingleParameter("channel_order", Integer.toString(orderPredecessorId)));
|
||||
} else {
|
||||
cmd = new SingleCommand("channelmove", ProtocolRole.CLIENT);
|
||||
cmd.add(new CommandSingleParameter("cid", Integer.toString(channelId)));
|
||||
cmd.add(new CommandSingleParameter("cpid", Integer.toString(newParentId)));
|
||||
cmd.add(new CommandSingleParameter("order", Integer.toString(orderPredecessorId)));
|
||||
}
|
||||
sock.executeCommand(cmd).complete();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks the server to (re-)send its permission definitions, which arrive as a burst
|
||||
* of {@code notifypermissionlist} events — the only way to learn permission names,
|
||||
@@ -683,43 +556,6 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
}, e -> model.permissions().unreportedRefused());
|
||||
}
|
||||
|
||||
/** Assigns or removes a server group for a client (by database id, as {@code servergroupaddclient} needs). */
|
||||
public void setClientServerGroup(int clientDatabaseId, int groupId, boolean assign) {
|
||||
act("Could not " + (assign ? "assign" : "remove") + " server group: ", sock -> {
|
||||
if (assign) sock.serverGroupAddClient(groupId, clientDatabaseId);
|
||||
else sock.serverGroupRemoveClient(groupId, clientDatabaseId);
|
||||
});
|
||||
}
|
||||
|
||||
/** Assigns a channel group for a client in the channel it currently sits in. */
|
||||
public void setClientChannelGroup(int clientDatabaseId, int channelId, int groupId) {
|
||||
act("Could not set channel group: ", sock -> {
|
||||
SingleCommand cmd = new SingleCommand("setclientchannelgroup", ProtocolRole.CLIENT);
|
||||
cmd.add(new CommandSingleParameter("cgid", Integer.toString(groupId)));
|
||||
cmd.add(new CommandSingleParameter("cid", Integer.toString(channelId)));
|
||||
cmd.add(new CommandSingleParameter("cldbid", Integer.toString(clientDatabaseId)));
|
||||
sock.executeCommand(cmd).complete();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribes to (or unsubscribes from) a set of channels in one command. The model
|
||||
* is left alone: the server answers with the subscription events that update it.
|
||||
*/
|
||||
public void setChannelsSubscribed(Collection<Integer> channelIds, boolean subscribed) {
|
||||
if (channelIds.isEmpty()) return;
|
||||
List<Integer> ids = List.copyOf(channelIds);
|
||||
String name = subscribed ? "channelsubscribe" : "channelunsubscribe";
|
||||
act(subscribed ? "Could not subscribe: " : "Could not unsubscribe: ", sock -> {
|
||||
List<SingleCommand> parts = new ArrayList<>(ids.size());
|
||||
for (int id : ids) {
|
||||
parts.add(new SingleCommand(name, ProtocolRole.CLIENT,
|
||||
new CommandSingleParameter("cid", Integer.toString(id))));
|
||||
}
|
||||
sock.executeCommand(new MultiCommand(name, ProtocolRole.CLIENT, parts)).complete();
|
||||
});
|
||||
}
|
||||
|
||||
public void sendChannelMessage(String text) {
|
||||
act("Message failed: ", sock -> {
|
||||
ClientEntry self = own.entry();
|
||||
@@ -755,45 +591,6 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
});
|
||||
}
|
||||
|
||||
public void poke(int clientId, String message) {
|
||||
act("Poke failed: ", sock -> {
|
||||
sock.clientPoke(clientId, message);
|
||||
announce.sound(SoundEvent.OTHER_SENT_POKE, announce.clientVars(clientId, null));
|
||||
});
|
||||
}
|
||||
|
||||
/** Grants or revokes clients' talk power, which lets them talk whatever their channel needs. */
|
||||
public void setTalker(Collection<Integer> clientIds, boolean talker) {
|
||||
if (clientIds.isEmpty()) return;
|
||||
act(talker ? "Error granting talk power: " : "Error revoking talk power: ", sock -> {
|
||||
for (int id : clientIds) setTalker(sock, id, talker);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Grants talk power to whoever in our channel has waited longest for it, first revoking
|
||||
* it from everyone who has it there when {@code revokeOthers}.
|
||||
*/
|
||||
public void grantNextTalkPower(boolean revokeOthers) {
|
||||
ClientEntry self = own.entry();
|
||||
if (self == null) return;
|
||||
ClientEntry next = model.nextTalkRequest(self.channelId);
|
||||
List<ClientEntry> talkers = revokeOthers ? model.talkersIn(self.channelId) : List.of();
|
||||
if (next == null && talkers.isEmpty()) return;
|
||||
act("Error granting talk power: ", sock -> {
|
||||
for (ClientEntry c : talkers) {
|
||||
if (c != next) setTalker(sock, c.id, false);
|
||||
}
|
||||
if (next != null) setTalker(sock, next.id, true);
|
||||
});
|
||||
}
|
||||
|
||||
private static void setTalker(LocalTeamspeakClientSocket sock, int clientId, boolean talker) throws Exception {
|
||||
sock.executeCommand(new SingleCommand("clientedit", ProtocolRole.CLIENT,
|
||||
new CommandSingleParameter("clid", Integer.toString(clientId)),
|
||||
new CommandSingleParameter("client_is_talker", talker ? "1" : "0"))).complete();
|
||||
}
|
||||
|
||||
public double getPingMillis() {
|
||||
try {
|
||||
LocalTeamspeakClientSocket sock = uplink.currentSocket();
|
||||
@@ -807,77 +604,23 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
act("Rename failed: ", sock -> sock.setNickname(nickname));
|
||||
}
|
||||
|
||||
/*
|
||||
* Info lookups use the commands the official client does. channelinfo/clientinfo would
|
||||
* answer directly, but they are query commands that count heavily against the server's
|
||||
* flood protection: clicking through the tree gets every command refused within seconds.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Asks for a channel's description. The answer arrives as a channel edit, see
|
||||
* {@link ConnectionEventHandler#onChannelEdit}.
|
||||
*/
|
||||
public void requestChannelInfo(int channelId) {
|
||||
requestInfo("channelgetdescription", "cid", channelId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks for a client's version and platform, the only details the enter-view event
|
||||
* lacks. The answer arrives as a client update, see {@link ConnectionEventHandler#onClientChanged}.
|
||||
*/
|
||||
public void requestClientInfo(int clientId) {
|
||||
requestInfo("clientgetvariables", "clid", clientId);
|
||||
}
|
||||
|
||||
/** May be refused for lack of permission; the details then stay as they are. */
|
||||
private void requestInfo(String command, String idParameter, int id) {
|
||||
attempt(sock -> sock.executeCommand(new SingleCommand(command, ProtocolRole.CLIENT,
|
||||
new CommandSingleParameter(idParameter, Integer.toString(id)))).complete());
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a client's description straight from the server, so an editor opens on the
|
||||
* live text instead of whatever an earlier info request cached.
|
||||
*/
|
||||
public CompletableFuture<String> requestClientDescription(int clientId) {
|
||||
return uplink.request(() -> {
|
||||
Client c = uplink.socket().getClientInfo(clientId);
|
||||
String description = c == null ? "" : orEmpty(c.get("client_description"));
|
||||
ClientEntry e = model.getClient(clientId);
|
||||
if (e != null) e.description = description;
|
||||
return description;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes a client's description. Unlike the nickname or the away state, this is not
|
||||
* a {@code clientupdate} property even for ourselves: the server only takes it
|
||||
* through {@code clientedit}, gated by {@code b_client_modify_own_description}.
|
||||
*/
|
||||
public CompletableFuture<Void> setClientDescription(int clientId, String description) {
|
||||
boolean self = clientId == selfClientId;
|
||||
return uplink.requestVoid(() -> {
|
||||
SingleCommand cmd = new SingleCommand("clientedit", ProtocolRole.CLIENT);
|
||||
cmd.add(new CommandSingleParameter("clid", Integer.toString(clientId)));
|
||||
cmd.add(new CommandSingleParameter("client_description", description));
|
||||
uplink.socket().executeCommand(cmd).complete();
|
||||
// Only the other clients are told about our own update, so the local entry
|
||||
// (and the info panel showing it) has to be caught up here.
|
||||
if (self) {
|
||||
ClientEntry e = model.getClient(clientId);
|
||||
if (e != null) e.description = description;
|
||||
ui.onInfoUpdated();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ---- server administration ----
|
||||
|
||||
/** Channel editing and the server's icon store. */
|
||||
/** Joining, moving and editing channels, and the server's icon store. */
|
||||
public ChannelAdmin channels() {
|
||||
return channels;
|
||||
}
|
||||
|
||||
/** Moving, kicking and banning clients, their groups, talk power and details. */
|
||||
public ClientAdmin clients() {
|
||||
return clients;
|
||||
}
|
||||
|
||||
/** The channels' file repositories. */
|
||||
public ChannelFiles files() {
|
||||
return files;
|
||||
}
|
||||
|
||||
public BanAdmin bans() {
|
||||
return bans;
|
||||
}
|
||||
@@ -924,84 +667,11 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
|
||||
// ---- contacts ----
|
||||
|
||||
public ContactStore getContacts() {
|
||||
/** The contact list as it applies to the clients on this server. */
|
||||
public ClientContacts contacts() {
|
||||
return contacts;
|
||||
}
|
||||
|
||||
/** The name a client is shown under: its contact's custom name where it has one. */
|
||||
public String displayName(ClientEntry client) {
|
||||
return contacts.displayName(client.uniqueId, client.nickname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a whisper from a client is played, per the global whisper policy and the
|
||||
* client's contact entry. A client that has already left is judged by the policy alone.
|
||||
*/
|
||||
boolean isWhisperAllowed(int clientId) {
|
||||
Settings.WhisperPolicy policy = settings.whisperPolicy;
|
||||
if (policy == Settings.WhisperPolicy.NOBODY) return false;
|
||||
ClientEntry c = model.getClient(clientId);
|
||||
Contact contact = c == null ? null : contacts.find(c.uniqueId);
|
||||
if (contact != null) return contact.whisperAllowed;
|
||||
return policy == Settings.WhisperPolicy.ALLOW_UNLESS_DENIED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a client's contact entry to its playback — the automatic mute and the
|
||||
* volume modifier — and records the sighting. Called when the client appears, which
|
||||
* is also when its myTeamSpeak ID can first match a contact.
|
||||
*/
|
||||
void applyContact(ClientEntry client) {
|
||||
contacts.link(client.uniqueId, client.myTeamSpeakId);
|
||||
Contact contact = applyContactPlayback(client);
|
||||
if (contact != null) contacts.markSeen(client.uniqueId, model.getServerName(), serverHost + ":" + serverPort);
|
||||
}
|
||||
|
||||
private Contact applyContactPlayback(ClientEntry client) {
|
||||
if (client == null || client.self) return null;
|
||||
Contact contact = contacts.find(client.uniqueId);
|
||||
VoiceOutput out = own.playback();
|
||||
if (contact != null && out != null) {
|
||||
if (contact.automute) out.setClientMuted(client.id, true);
|
||||
out.setClientVolume(client.id, contact.volumeGain());
|
||||
}
|
||||
return contact;
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-applies every contact entry to the clients on the server, after the list changed.
|
||||
* A client that just became a contact is also marked as seen. One seen before keeps
|
||||
* where it was seen: with it on two servers, each re-application would claim it for its
|
||||
* own server, which is a change that triggers the other's re-application, and so on.
|
||||
*/
|
||||
public void applyContacts() {
|
||||
if (!uplink.isUp()) return;
|
||||
for (ClientEntry c : model.allClients()) {
|
||||
Contact contact = applyContactPlayback(c);
|
||||
if (contact != null && contact.lastSeen == null) {
|
||||
contacts.markSeen(c.uniqueId, model.getServerName(), serverHost + ":" + serverPort);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Remembers a per-client volume, in the contact entry when the client has one. */
|
||||
public void setClientVolumeModifier(ClientEntry client, double decibels) {
|
||||
VoiceOutput out = own.playback();
|
||||
if (out != null) out.setClientVolume(client.id, Math.pow(10.0, decibels / 20.0));
|
||||
Contact contact = contacts.find(client.uniqueId);
|
||||
if (contact == null) return;
|
||||
contact.hasVolumeModifier = decibels != 0;
|
||||
contact.volumeModifier = decibels;
|
||||
contacts.put(contact);
|
||||
}
|
||||
|
||||
public double getClientVolumeModifier(ClientEntry client) {
|
||||
VoiceOutput out = own.playback();
|
||||
if (out == null) return 0;
|
||||
double gain = out.getClientVolume(client.id);
|
||||
return gain <= 0 ? -60 : 20.0 * Math.log10(gain);
|
||||
}
|
||||
|
||||
public String getSelfUniqueId() {
|
||||
ClientEntry self = own.entry();
|
||||
if (self != null && !self.uniqueId.isEmpty()) return self.uniqueId;
|
||||
@@ -1075,65 +745,6 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
return id == 0 ? null : model.getClient(id);
|
||||
}
|
||||
|
||||
// ---- file transfers ----
|
||||
|
||||
/** Whether a live connection capable of file transfers is available. */
|
||||
public boolean canTransferFiles() {
|
||||
return uplink.canTransferFiles();
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists the files under {@code path} in a channel's repository. Blocking; call
|
||||
* off the UI thread. Throws if not connected or the server denies the request.
|
||||
*/
|
||||
public java.util.List<RemoteFile> listFiles(int channelId, String channelPassword, String path)
|
||||
throws Exception {
|
||||
return uplink.files().list(channelId, channelPassword, path);
|
||||
}
|
||||
|
||||
public void createDirectory(int channelId, String channelPassword, String dirPath) throws Exception {
|
||||
uplink.files().createDirectory(channelId, channelPassword, dirPath);
|
||||
}
|
||||
|
||||
public void deleteFile(int channelId, String channelPassword, String fullPath) throws Exception {
|
||||
uplink.files().delete(channelId, channelPassword, fullPath);
|
||||
}
|
||||
|
||||
public void renameFile(int channelId, String channelPassword, String oldFullPath, String newFullPath)
|
||||
throws Exception {
|
||||
uplink.files().rename(channelId, channelPassword, oldFullPath, newFullPath);
|
||||
}
|
||||
|
||||
/** Begins downloading a channel file to {@code target}; progress arrives via {@code listener}. */
|
||||
public FileTransfer downloadFile(int channelId, String channelPassword, String remoteFullPath,
|
||||
File target, FileTransfer.Listener listener) {
|
||||
return uplink.files().download(channelId, channelPassword, remoteFullPath, target, announcing(listener));
|
||||
}
|
||||
|
||||
/** Begins uploading {@code source} into a channel; progress arrives via {@code listener}. */
|
||||
public FileTransfer uploadFile(int channelId, String channelPassword, String remoteFullPath,
|
||||
File source, boolean overwrite, FileTransfer.Listener listener) {
|
||||
return uplink.files().upload(channelId, channelPassword, remoteFullPath, source, overwrite, announcing(listener));
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a transfer listener so the completion and failure sounds play once the
|
||||
* transfer finishes, whoever is watching it.
|
||||
*/
|
||||
private FileTransfer.Listener announcing(FileTransfer.Listener listener) {
|
||||
java.util.concurrent.atomic.AtomicBoolean announced = new java.util.concurrent.atomic.AtomicBoolean();
|
||||
return transfer -> {
|
||||
FileTransfer.State state = transfer.getState();
|
||||
if (state != FileTransfer.State.CANCELLED && transfer.isDone()
|
||||
&& announced.compareAndSet(false, true)) {
|
||||
announce.sound(state == FileTransfer.State.COMPLETED
|
||||
? SoundEvent.OTHER_FILETRANSFER_COMPLETE
|
||||
: SoundEvent.OTHER_FILETRANSFER_FAILED, announce.serverVars());
|
||||
}
|
||||
if (listener != null) listener.onTransferChanged(transfer);
|
||||
};
|
||||
}
|
||||
|
||||
// ---- connection info ----
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,7 +11,18 @@ import eu.lixko.ts3j.client.myts.MyTsException;
|
||||
import eu.lixko.ts3j.client.contacts.ContactStore;
|
||||
import eu.lixko.ts3j.client.net.AvatarAdmin;
|
||||
import eu.lixko.ts3j.client.net.BanAdmin;
|
||||
import eu.lixko.ts3j.client.audio.VoiceInput;
|
||||
import eu.lixko.ts3j.client.audio.VoiceOutput;
|
||||
import eu.lixko.ts3j.client.myts.MyTeamSpeakLogin;
|
||||
import eu.lixko.ts3j.client.net.ChannelAdmin;
|
||||
import eu.lixko.ts3j.client.net.ChannelFiles;
|
||||
import eu.lixko.ts3j.client.net.ClientAdmin;
|
||||
import eu.lixko.ts3j.client.net.ClientContacts;
|
||||
import eu.lixko.ts3j.client.net.ConnectionStats;
|
||||
import eu.lixko.ts3j.client.net.IconRepository;
|
||||
import eu.lixko.ts3j.client.net.ServerModel;
|
||||
import eu.lixko.ts3j.client.net.avatar.Avatar;
|
||||
import eu.lixko.ts3j.client.net.avatar.AvatarRepository;
|
||||
import eu.lixko.ts3j.client.net.ChannelNode;
|
||||
import eu.lixko.ts3j.client.net.ClientEntry;
|
||||
import eu.lixko.ts3j.client.net.ComplaintAdmin;
|
||||
@@ -37,6 +48,7 @@ import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -44,6 +56,10 @@ import java.util.stream.Collectors;
|
||||
* connection lifecycle, the microphone/speaker/away flags, chat sending and channel
|
||||
* lookups. A frontend keeps one per server tab and hears from it through {@link Listener}.
|
||||
*
|
||||
* <p>It is the frontends' way in to the server: what they read lives in its {@link #model()},
|
||||
* and what they do beyond their own state goes through the helpers it hands out
|
||||
* ({@link #clients()}, {@link #channels()}, {@link #files()} and the other admin helpers).
|
||||
*
|
||||
* <p>Listener callbacks arrive on connection threads, so frontends marshal them onto their
|
||||
* UI thread. The getters may be called from any thread.
|
||||
*/
|
||||
@@ -117,10 +133,6 @@ public final class ServerSession {
|
||||
this.recording = conn.recording();
|
||||
}
|
||||
|
||||
public TeamspeakConnection connection() {
|
||||
return conn;
|
||||
}
|
||||
|
||||
/** The server's name once known, the address before that. */
|
||||
public String title() {
|
||||
return title;
|
||||
@@ -149,6 +161,38 @@ public final class ServerSession {
|
||||
return conn.getServerPort();
|
||||
}
|
||||
|
||||
/** The server's unique id, worked out from its key; null before it has introduced itself. */
|
||||
public String serverUniqueId() {
|
||||
return conn.serverUniqueId();
|
||||
}
|
||||
|
||||
/** Channels, clients, groups and server properties as last heard; empty while not connected. */
|
||||
public ServerModel model() {
|
||||
return conn.getModel();
|
||||
}
|
||||
|
||||
/** Our client id on the server, or -1 while not connected. */
|
||||
public int selfClientId() {
|
||||
return conn.getSelfClientId();
|
||||
}
|
||||
|
||||
public String selfUniqueId() {
|
||||
return conn.getSelfUniqueId();
|
||||
}
|
||||
|
||||
/** Round-trip time to the server, or a negative value while unknown. */
|
||||
public double pingMillis() {
|
||||
return conn.getPingMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a client's connection statistics, ourselves included, and delivers them on a
|
||||
* connection thread. May be called repeatedly to poll; does nothing while not connected.
|
||||
*/
|
||||
public void requestConnectionInfo(int clientId, Consumer<ConnectionStats> callback) {
|
||||
conn.requestConnectionInfo(clientId, callback);
|
||||
}
|
||||
|
||||
public String identityId() {
|
||||
return identityId;
|
||||
}
|
||||
@@ -167,6 +211,23 @@ public final class ServerSession {
|
||||
return b;
|
||||
}
|
||||
|
||||
// ---- myTeamSpeak ----
|
||||
|
||||
/** The account whose identity to show servers; null when signed out. */
|
||||
public void setMyTeamSpeakLogin(MyTeamSpeakLogin login) {
|
||||
conn.setMyTeamSpeakLogin(login);
|
||||
}
|
||||
|
||||
/** Whether to show this server our myTeamSpeak account, as its bookmark says. */
|
||||
public void setMyTeamSpeakFeatures(boolean enabled) {
|
||||
conn.setMyTeamSpeakFeatures(enabled);
|
||||
}
|
||||
|
||||
/** Tells the server about a sign-in or sign-out while connected. */
|
||||
public void updateMyTeamSpeak() {
|
||||
conn.updateMyTeamSpeak();
|
||||
}
|
||||
|
||||
// ---- connection lifecycle ----
|
||||
|
||||
/**
|
||||
@@ -225,7 +286,7 @@ public final class ServerSession {
|
||||
ChannelNode target = link.channel.isEmpty() ? null : conn.getModel().findChannelByPath(link.channel);
|
||||
ChannelNode current = currentChannel();
|
||||
if (target != null && (current == null || target.id != current.id)) {
|
||||
conn.joinChannel(target.id, link.channelPassword.isEmpty() ? null : link.channelPassword);
|
||||
conn.channels().join(target.id, link.channelPassword);
|
||||
}
|
||||
tokenLink = link.token.isEmpty() ? null : link;
|
||||
useConnectToken();
|
||||
@@ -318,6 +379,20 @@ public final class ServerSession {
|
||||
if (isBusy()) conn.disconnect();
|
||||
}
|
||||
|
||||
/** {@link #disconnect()} on the calling thread, returning once the server is left. */
|
||||
public void disconnectNow(String reason) {
|
||||
reconnects.cancel();
|
||||
if (isBusy()) conn.disconnectBlocking(reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* The error id the server refused the last connect with ({@code 3329} for a ban, for
|
||||
* one), or 0 when it was not refused or did not get that far.
|
||||
*/
|
||||
public int connectErrorId() {
|
||||
return conn.getConnectErrorId();
|
||||
}
|
||||
|
||||
/** Synchronous teardown for shutdown paths, so the server sees us leave and recordings are written. */
|
||||
public void shutdown() {
|
||||
reconnects.cancel();
|
||||
@@ -415,6 +490,16 @@ public final class ServerSession {
|
||||
listener.onSessionChanged();
|
||||
}
|
||||
|
||||
/** The capture device while connected, null otherwise. */
|
||||
public VoiceInput microphone() {
|
||||
return own.microphone();
|
||||
}
|
||||
|
||||
/** The speakers while connected, null otherwise. */
|
||||
public VoiceOutput playback() {
|
||||
return own.playback();
|
||||
}
|
||||
|
||||
/** Hands the capture device to (or takes it from) this connection. */
|
||||
public void setMicrophoneActive(boolean active) {
|
||||
own.setMicrophoneActive(active);
|
||||
@@ -439,7 +524,8 @@ public final class ServerSession {
|
||||
|
||||
/** Grants talk power to whoever in our channel asked first, after revoking everyone else's when {@code revokeOthers}. */
|
||||
public void grantNextTalkPower(boolean revokeOthers) {
|
||||
if (conn.isConnected()) conn.grantNextTalkPower(revokeOthers);
|
||||
ClientEntry self = own.entry();
|
||||
if (conn.isConnected() && self != null) conn.clients().grantNextTalkPower(self.channelId, revokeOthers);
|
||||
}
|
||||
|
||||
public String phoneticNickname() {
|
||||
@@ -619,7 +705,12 @@ public final class ServerSession {
|
||||
|
||||
private String peerName(int clientId) {
|
||||
ClientEntry c = conn.getModel().getClient(clientId);
|
||||
return c != null ? conn.displayName(c) : "Client " + clientId;
|
||||
return c != null ? conn.contacts().displayName(c) : "Client " + clientId;
|
||||
}
|
||||
|
||||
/** Hands {@link Listener#onPrivateChatHistory} what was said with a client before. */
|
||||
public void loadPrivateChatHistory(int clientId, String uniqueId) {
|
||||
conn.loadPrivateChatHistory(clientId, uniqueId);
|
||||
}
|
||||
|
||||
// ---- channels ----
|
||||
@@ -642,7 +733,7 @@ public final class ServerSession {
|
||||
public void joinChannelPath(String path) {
|
||||
if (!conn.isConnected() || path == null || path.isBlank()) return;
|
||||
ChannelNode target = conn.getModel().findChannelByPath(path);
|
||||
if (target != null) conn.joinChannel(target.id, null);
|
||||
if (target != null) conn.channels().join(target.id, null);
|
||||
}
|
||||
|
||||
/** Every channel of this server as a "/"-separated path, in the tree's own order. */
|
||||
@@ -665,6 +756,11 @@ public final class ServerSession {
|
||||
return conn.isConnected() ? conn.getModel().permissions() : Permissions.NONE;
|
||||
}
|
||||
|
||||
/** Unread messages in our offline message inbox. */
|
||||
public int unreadOfflineMessages() {
|
||||
return conn.unreadOfflineMessages();
|
||||
}
|
||||
|
||||
/** Whether we publish an avatar on this server. */
|
||||
public boolean hasAvatar() {
|
||||
if (!conn.isConnected()) return false;
|
||||
@@ -678,12 +774,42 @@ public final class ServerSession {
|
||||
return permissions().canRequestTalkPower() && self != null && conn.getModel().lacksTalkPower(self);
|
||||
}
|
||||
|
||||
// ---- icons, avatars and contacts ----
|
||||
|
||||
/** Group icons for this server: bundled defaults plus the server's own uploads. */
|
||||
public IconRepository icons() {
|
||||
return conn.getIcons();
|
||||
}
|
||||
|
||||
/** The avatar a client currently shows, or null if none or not yet downloaded. */
|
||||
public Avatar avatarOf(ClientEntry client) {
|
||||
return conn.avatarOf(client);
|
||||
}
|
||||
|
||||
/** Client avatars for this server, downloaded on demand. */
|
||||
public AvatarRepository avatars() {
|
||||
return conn.getAvatars();
|
||||
}
|
||||
|
||||
/** The contact list as it applies to the clients on this server. */
|
||||
public ClientContacts contacts() {
|
||||
return conn.contacts();
|
||||
}
|
||||
|
||||
// ---- server administration ----
|
||||
|
||||
public ClientAdmin clients() {
|
||||
return conn.clients();
|
||||
}
|
||||
|
||||
public ChannelAdmin channels() {
|
||||
return conn.channels();
|
||||
}
|
||||
|
||||
public ChannelFiles files() {
|
||||
return conn.files();
|
||||
}
|
||||
|
||||
public BanAdmin bans() {
|
||||
return conn.bans();
|
||||
}
|
||||
@@ -802,7 +928,7 @@ public final class ServerSession {
|
||||
/** Passes the sender on under their contact display name. */
|
||||
@Override
|
||||
public void onChat(ChatScope scope, int fromClientId, String fromUniqueId, String fromName, String message) {
|
||||
String name = conn.getContacts().displayName(fromUniqueId, fromName);
|
||||
String name = conn.contacts().store().displayName(fromUniqueId, fromName);
|
||||
listener.onChat(scope, fromClientId, fromUniqueId, name, message);
|
||||
}
|
||||
|
||||
|
||||
@@ -131,7 +131,7 @@ public final class Sessions {
|
||||
}
|
||||
|
||||
private static VoiceInput microphoneOf(ServerSession session) {
|
||||
return session == null || !session.isConnected() ? null : session.connection().ownClient().microphone();
|
||||
return session == null || !session.isConnected() ? null : session.microphone();
|
||||
}
|
||||
|
||||
// ---- the user's own state ----
|
||||
|
||||
Reference in New Issue
Block a user