
com.github.theholywaffle.teamspeak3.TS3ApiAsync Maven / Gradle / Ivy
Show all versions of teamspeak3-api Show documentation
package com.github.theholywaffle.teamspeak3;
/*
* #%L
* TeamSpeak 3 Java API
* %%
* Copyright (C) 2014 Bert De Geyter
* %%
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* #L%
*/
import com.github.theholywaffle.teamspeak3.api.*;
import com.github.theholywaffle.teamspeak3.api.event.TS3EventType;
import com.github.theholywaffle.teamspeak3.api.event.TS3Listener;
import com.github.theholywaffle.teamspeak3.api.exception.TS3CommandFailedException;
import com.github.theholywaffle.teamspeak3.api.exception.TS3Exception;
import com.github.theholywaffle.teamspeak3.api.exception.TS3FileTransferFailedException;
import com.github.theholywaffle.teamspeak3.api.wrapper.*;
import com.github.theholywaffle.teamspeak3.commands.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* Asynchronous version of {@link TS3Api} to interact with the {@link TS3Query}.
*
* This class is used to easily interact with a {@link TS3Query}. It constructs commands,
* sends them to the TeamSpeak3 server, processes the response and returns the result.
*
* All methods in this class are asynchronous (so they won't block) and
* will return a {@link CommandFuture} of the corresponding return type in {@link TS3Api}.
* If a command fails, no exception will be thrown directly. It will however be rethrown in
* {@link CommandFuture#get()} and {@link CommandFuture#get(long, TimeUnit)}.
* Usually, the thrown exception is a {@link TS3CommandFailedException}, which will get you
* access to the {@link QueryError} from which more information about the error can be obtained.
*
* Also note that while these methods are asynchronous, the commands will still be sent through a
* synchronous command pipeline. That means if an asynchronous method is called immediately
* followed by a synchronous method, the synchronous method will first have to wait until the
* asynchronous method completed until it its command is sent.
*
* You won't be able to execute most commands while you're not logged in due to missing permissions.
* Make sure to either pass your login credentials to the {@link TS3Config} object when
* creating the {@code TS3Query} or to call {@link #login(String, String)} to log in.
*
* After that, most commands also require you to select a {@linkplain VirtualServer virtual server}.
* To do so, call either {@link #selectVirtualServerByPort(int)} or {@link #selectVirtualServerById(int)}.
*
*
* @see TS3Api The synchronous version of the API
*/
public class TS3ApiAsync {
/**
* The TS3 query that holds the event manager and the file transfer helper.
*/
private final TS3Query query;
/**
* The queue that this TS3ApiAsync sends its commands to.
*/
private final CommandQueue commandQueue;
/**
* Creates a new asynchronous API object for the given {@code TS3Query}.
*
* Usually, this constructor should not be called. Use {@link TS3Query#getAsyncApi()} instead.
*
*
* @param query
* the TS3Query to use
* @param commandQueue
* the queue to send commands to
*/
TS3ApiAsync(TS3Query query, CommandQueue commandQueue) {
this.query = query;
this.commandQueue = commandQueue;
}
/**
* Adds a new ban entry. At least one of the parameters {@code ip}, {@code name} or {@code uid} needs to be non-null.
* Returns the ID of the newly created ban entry.
*
* @param ip
* a RegEx pattern to match a client's IP against, can be {@code null}
* @param name
* a RegEx pattern to match a client's name against, can be {@code null}
* @param uid
* the unique identifier of a client, can be {@code null}
* @param timeInSeconds
* the duration of the ban in seconds. 0 equals a permanent ban
* @param reason
* the reason for the ban, can be {@code null}
*
* @return the ID of the newly created ban entry
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Pattern RegEx Pattern
* @see #addBan(String, String, String, String, long, String)
* @see Client#getId()
* @see Client#getUniqueIdentifier()
* @see ClientInfo#getIp()
*/
public CommandFuture addBan(String ip, String name, String uid, long timeInSeconds, String reason) {
return addBan(ip, name, uid, null, timeInSeconds, reason);
}
/**
* Adds a new ban entry. At least one of the parameters {@code ip}, {@code name}, {@code uid}, or
* {@code myTSId} needs to be non-null. Returns the ID of the newly created ban entry.
*
* Note that creating a ban entry for the {@code "empty"} "myTeamSpeak" ID will ban all clients who
* don't have a linked "myTeamSpeak" account.
*
*
* @param ip
* a RegEx pattern to match a client's IP against, can be {@code null}
* @param name
* a RegEx pattern to match a client's name against, can be {@code null}
* @param uid
* the unique identifier of a client, can be {@code null}
* @param myTSId
* the "myTeamSpeak" ID of a client, the string {@code "empty"}, or {@code null}
* @param timeInSeconds
* the duration of the ban in seconds. 0 equals a permanent ban
* @param reason
* the reason for the ban, can be {@code null}
*
* @return the ID of the newly created ban entry
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Pattern RegEx Pattern
* @see Client#getId()
* @see Client#getUniqueIdentifier()
* @see ClientInfo#getIp()
*/
public CommandFuture addBan(String ip, String name, String uid, String myTSId, long timeInSeconds, String reason) {
Command cmd = BanCommands.banAdd(ip, name, uid, myTSId, timeInSeconds, reason);
return executeAndReturnIntProperty(cmd, "banid");
}
/**
* Adds a specified permission to a client in a specific channel.
*
* @param channelId
* the ID of the channel wherein the permission should be granted
* @param clientDBId
* the database ID of the client to add a permission to
* @param permName
* the name of the permission to grant
* @param permValue
* the numeric value of the permission (or for boolean permissions: 1 = true, 0 = false)
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel#getId()
* @see Client#getDatabaseId()
* @see Permission
*/
public CommandFuture addChannelClientPermission(int channelId, int clientDBId, String permName, int permValue) {
Command cmd = PermissionCommands.channelClientAddPerm(channelId, clientDBId, permName, permValue);
return executeAndReturnError(cmd);
}
/**
* Creates a new channel group for clients using a given name and returns its ID.
*
* To create channel group templates or ones for server queries,
* use {@link #addChannelGroup(String, PermissionGroupDatabaseType)}.
*
*
* @param name
* the name of the new channel group
*
* @return the ID of the newly created channel group
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ChannelGroup
*/
public CommandFuture addChannelGroup(String name) {
return addChannelGroup(name, null);
}
/**
* Creates a new channel group using a given name and returns its ID.
*
* @param name
* the name of the new channel group
* @param type
* the desired type of channel group
*
* @return the ID of the newly created channel group
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ChannelGroup
*/
public CommandFuture addChannelGroup(String name, PermissionGroupDatabaseType type) {
Command cmd = ChannelGroupCommands.channelGroupAdd(name, type);
return executeAndReturnIntProperty(cmd, "cgid");
}
/**
* Adds a specified permission to a channel group.
*
* @param groupId
* the ID of the channel group to grant the permission
* @param permName
* the name of the permission to be granted
* @param permValue
* the numeric value of the permission (or for boolean permissions: 1 = true, 0 = false)
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ChannelGroup#getId()
* @see Permission
*/
public CommandFuture addChannelGroupPermission(int groupId, String permName, int permValue) {
Command cmd = PermissionCommands.channelGroupAddPerm(groupId, permName, permValue);
return executeAndReturnError(cmd);
}
/**
* Adds a specified permission to a channel.
*
* @param channelId
* the ID of the channel wherein the permission should be granted
* @param permName
* the name of the permission to grant
* @param permValue
* the numeric value of the permission (or for boolean permissions: 1 = true, 0 = false)
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel#getId()
* @see Permission
*/
public CommandFuture addChannelPermission(int channelId, String permName, int permValue) {
Command cmd = PermissionCommands.channelAddPerm(channelId, permName, permValue);
return executeAndReturnError(cmd);
}
/**
* Adds a specified permission to a channel.
*
* @deprecated
* This method is no longer preferred for adding permissions to a client.
*
* Use {@link TS3ApiAsync#addClientPermission(int, IPermissionType, int, boolean)}
* or {@link TS3ApiAsync#addClientPermission(int, BPermissionType, boolean, boolean)} instead.
*
*
* @param clientDBId
* the database ID of the client to grant the permission
* @param permName
* the name of the permission to grant
* @param value
* the numeric value of the permission (or for boolean permissions: 1 = true, 0 = false)
* @param skipped
* if set to {@code true}, the permission will not be overridden by channel group permissions
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getDatabaseId()
* @see Permission
*/
@Deprecated
public CommandFuture addClientPermission(int clientDBId, String permName, int value, boolean skipped) {
Command cmd = PermissionCommands.clientAddPerm(clientDBId, permName, value, skipped);
return executeAndReturnError(cmd);
}
/**
* Adds a specified permission to a client.
*
* @param clientDBId
* the database ID of the client to grant the permission
* @param permName
* the enum of the permission to grant
* @see IPermissionType
* @param value
* the numeric value of the permission
* @param skipped
* if set to {@code true}, the permission will not be overridden by channel group permissions
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getDatabaseId()
* @see Permission
*/
public CommandFuture addClientPermission(int clientDBId, IPermissionType permName, int value, boolean skipped) {
Command cmd = PermissionCommands.clientAddPerm(clientDBId, permName.getName(), value, skipped);
return executeAndReturnError(cmd);
}
/**
* Adds a specified permission to a client.
*
* @param clientDBId
* the database ID of the client to grant the permission
* @param permName
* the enum of the permission to grant
* @see BPermissionType
* @param value
* the boolean value of the permission
* @param skipped
* if set to {@code true}, the permission will not be overridden by channel group permissions
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getDatabaseId()
* @see Permission
*/
public CommandFuture addClientPermission(int clientDBId, BPermissionType permName, boolean value, boolean skipped) {
Command cmd = PermissionCommands.clientAddPerm(clientDBId, permName.getName(), value, skipped);
return executeAndReturnError(cmd);
}
/**
* Adds a client to the specified server group.
*
* Please note that a client cannot be added to default groups or template groups.
*
*
* @param groupId
* the ID of the server group to add the client to
* @param clientDatabaseId
* the database ID of the client to add
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ServerGroup#getId()
* @see Client#getDatabaseId()
*/
public CommandFuture addClientToServerGroup(int groupId, int clientDatabaseId) {
Command cmd = ServerGroupCommands.serverGroupAddClient(groupId, clientDatabaseId);
return executeAndReturnError(cmd);
}
/**
* Submits a complaint about the specified client.
* The length of the message is limited to 200 UTF-8 bytes and BB codes in it will be ignored.
*
* @param clientDBId
* the database ID of the client
* @param message
* the message of the complaint, may not contain BB codes
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getDatabaseId()
* @see Complaint#getMessage()
*/
public CommandFuture addComplaint(int clientDBId, String message) {
Command cmd = ComplaintCommands.complainAdd(clientDBId, message);
return executeAndReturnError(cmd);
}
/**
* Adds a specified permission to all server groups of the type specified by {@code type} on all virtual servers.
*
* @param type
* the kind of server group this permission should be added to
* @param permName
* the name of the permission to be granted
* @param value
* the numeric value of the permission (or for boolean permissions: 1 = true, 0 = false)
* @param negated
* if set to true, the lowest permission value will be selected instead of the highest
* @param skipped
* if set to true, this permission will not be overridden by client or channel group permissions
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ServerGroupType
* @see Permission
*/
public CommandFuture addPermissionToAllServerGroups(ServerGroupType type, String permName, int value, boolean negated, boolean skipped) {
Command cmd = PermissionCommands.serverGroupAutoAddPerm(type, permName, value, negated, skipped);
return executeAndReturnError(cmd);
}
/**
* Create a new privilege key that allows one client to join a server or channel group.
*
* - If {@code type} is set to {@linkplain PrivilegeKeyType#SERVER_GROUP SERVER_GROUP},
* {@code groupId} is used as a server group ID and {@code channelId} is ignored.
* - If {@code type} is set to {@linkplain PrivilegeKeyType#CHANNEL_GROUP CHANNEL_GROUP},
* {@code groupId} is used as a channel group ID and {@code channelId} is used as the channel in which the group should be set.
*
*
* @param type
* the type of token that should be created
* @param groupId
* the ID of the server or channel group
* @param channelId
* the ID of the channel, in case the token is channel group token
* @param description
* the description for the token, can be null
*
* @return the created token for a client to use
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see PrivilegeKeyType
* @see #addPrivilegeKeyServerGroup(int, String)
* @see #addPrivilegeKeyChannelGroup(int, int, String)
*/
public CommandFuture addPrivilegeKey(PrivilegeKeyType type, int groupId, int channelId, String description) {
Command cmd = PrivilegeKeyCommands.privilegeKeyAdd(type, groupId, channelId, description);
return executeAndReturnStringProperty(cmd, "token");
}
/**
* Creates a new privilege key for a channel group.
*
* @param channelGroupId
* the ID of the channel group
* @param channelId
* the ID of the channel in which the channel group should be set
* @param description
* the description for the token, can be null
*
* @return the created token for a client to use
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ChannelGroup#getId()
* @see Channel#getId()
* @see #addPrivilegeKey(PrivilegeKeyType, int, int, String)
* @see #addPrivilegeKeyServerGroup(int, String)
*/
public CommandFuture addPrivilegeKeyChannelGroup(int channelGroupId, int channelId, String description) {
return addPrivilegeKey(PrivilegeKeyType.CHANNEL_GROUP, channelGroupId, channelId, description);
}
/**
* Creates a new privilege key for a server group.
*
* @param serverGroupId
* the ID of the server group
* @param description
* the description for the token, can be null
*
* @return the created token for a client to use
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ServerGroup#getId()
* @see #addPrivilegeKey(PrivilegeKeyType, int, int, String)
* @see #addPrivilegeKeyChannelGroup(int, int, String)
*/
public CommandFuture addPrivilegeKeyServerGroup(int serverGroupId, String description) {
return addPrivilegeKey(PrivilegeKeyType.SERVER_GROUP, serverGroupId, 0, description);
}
/**
* Creates a new server group for clients using a given name and returns its ID.
*
* To create server group templates or ones for server queries,
* use {@link #addServerGroup(String, PermissionGroupDatabaseType)}.
*
*
* @param name
* the name of the new server group
*
* @return the ID of the newly created server group
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ServerGroup
*/
public CommandFuture addServerGroup(String name) {
return addServerGroup(name, PermissionGroupDatabaseType.REGULAR);
}
/**
* Creates a new server group using a given name and returns its ID.
*
* @param name
* the name of the new server group
* @param type
* the desired type of server group
*
* @return the ID of the newly created server group
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ServerGroup
* @see PermissionGroupDatabaseType
*/
public CommandFuture addServerGroup(String name, PermissionGroupDatabaseType type) {
Command cmd = ServerGroupCommands.serverGroupAdd(name, type);
return executeAndReturnIntProperty(cmd, "sgid");
}
/**
* Adds a specified permission to a server group.
*
* @param groupId
* the ID of the channel group to which the permission should be added
* @param permName
* the name of the permission to add
* @param value
* the numeric value of the permission (or for boolean permissions: 1 = true, 0 = false)
* @param negated
* if set to true, the lowest permission value will be selected instead of the highest
* @param skipped
* if set to true, this permission will not be overridden by client or channel group permissions
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ServerGroup#getId()
* @see Permission
*/
public CommandFuture addServerGroupPermission(int groupId, String permName, int value, boolean negated, boolean skipped) {
Command cmd = PermissionCommands.serverGroupAddPerm(groupId, permName, value, negated, skipped);
return executeAndReturnError(cmd);
}
/**
* Creates a server query login with name {@code loginName} for the client specified by {@code clientDBId}
* on the currently selected virtual server and returns the password of the created login.
* If the client already had a server query login, the existing login will be deleted and replaced.
*
* Moreover, this method can be used to create new global server query logins that are not tied to any
* particular virtual server or client. To create such a server query login, make sure no virtual server is
* selected (e.g. use {@code selectVirtualServerById(0)}) and call this method with {@code clientDBId = 0}.
*
*
* @param loginName
* the name of the server query login to add
* @param clientDBId
* the database ID of the client for which a server query login should be created
*
* @return an object containing the password of the new server query login
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see #deleteServerQueryLogin(int)
* @see #getServerQueryLogins()
* @see #updateServerQueryLogin(String)
*/
public CommandFuture addServerQueryLogin(String loginName, int clientDBId) {
Command cmd = QueryLoginCommands.queryLoginAdd(loginName, clientDBId);
return executeAndTransformFirst(cmd, CreatedQueryLogin::new);
}
/**
* Adds one or more {@link TS3Listener}s to the event manager of the query.
* These listeners will be notified when the TS3 server fires an event.
*
* Note that for the TS3 server to fire events, you must first also register
* the event types you want to listen to.
*
*
* @param listeners
* one or more listeners to register
*
* @see #registerAllEvents()
* @see #registerEvent(TS3EventType, int)
* @see TS3Listener
* @see TS3EventType
*/
public void addTS3Listeners(TS3Listener... listeners) {
query.getEventManager().addListeners(listeners);
}
/**
* Bans a client with a given client ID for a given time.
*
* Please note that this will create up to three separate ban rules,
* one for the targeted client's IP address, one for their unique identifier,
* and potentially one more entry for their "myTeamSpeak" ID, if available.
*
* Exception: If the banned client connects via a loopback address
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created.
*
*
* @param clientId
* the ID of the client
* @param timeInSeconds
* the duration of the ban in seconds. 0 equals a permanent ban
*
* @return an array containing the IDs of the created ban entries
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getId()
* @see #addBan(String, String, String, long, String)
*/
public CommandFuture banClient(int clientId, long timeInSeconds) {
return banClient(clientId, timeInSeconds, null);
}
/**
* Bans a client with a given client ID for a given time for the specified reason.
*
* Please note that this will create up to three separate ban rules,
* one for the targeted client's IP address, one for their unique identifier,
* and potentially one more entry for their "myTeamSpeak" ID, if available.
*
* Exception: If the banned client connects via a loopback address
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created.
*
*
* @param clientId
* the ID of the client
* @param timeInSeconds
* the duration of the ban in seconds. 0 equals a permanent ban
* @param reason
* the reason for the ban, can be null
*
* @return an array containing the IDs of the created ban entries
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getId()
* @see #addBan(String, String, String, long, String)
*/
public CommandFuture banClient(int clientId, long timeInSeconds, String reason) {
Command cmd = BanCommands.banClient(new int[] {clientId}, timeInSeconds, reason, false);
return executeAndReturnIntArray(cmd, "banid");
}
/**
* Bans a client with a given client ID permanently for the specified reason.
*
* Please note that this will create up to three separate ban rules,
* one for the targeted client's IP address, one for their unique identifier,
* and potentially one more entry for their "myTeamSpeak" ID, if available.
*
* Exception: If the banned client connects via a loopback address
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created.
*
*
* @param clientId
* the ID of the client
* @param reason
* the reason for the ban, can be null
*
* @return an array containing the IDs of the created ban entries
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getId()
* @see #addBan(String, String, String, long, String)
*/
public CommandFuture banClient(int clientId, String reason) {
return banClient(clientId, 0, reason);
}
/**
* Bans multiple clients by their client ID for a given time for the specified reason.
*
* Please note that this will create up to three separate ban rules for each client,
* one for the targeted client's IP address, one for their unique identifier,
* and potentially one more entry for their "myTeamSpeak" ID, if available.
*
* Exception: If the banned client connects via a loopback address
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created.
*
* Exception: If two or more clients are connecting from the
* same IP address, only one IP ban entry for that IP will be created.
*
*
* @param clientIds
* the IDs of the clients to be banned
* @param timeInSeconds
* the duration of the ban in seconds. 0 equals a permanent ban
* @param reason
* the reason for the ban, can be null
* @param continueOnError
* if true, continue to the next client if banning one client fails, else do not create any bans on error
*
* @return an array containing the IDs of the created ban entries
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getId()
* @see #addBan(String, String, String, long, String)
*/
public CommandFuture banClients(int[] clientIds, long timeInSeconds, String reason, boolean continueOnError) {
if (clientIds == null) throw new IllegalArgumentException("Client ID array was null");
if (clientIds.length == 0) return CommandFuture.immediate(new int[0]); // Success
Command cmd = BanCommands.banClient(clientIds, timeInSeconds, reason, continueOnError);
return executeAndReturnIntArray(cmd, "banid");
}
/**
* Sends a text message to all clients on all virtual servers.
* These messages will appear to clients in the tab for server messages.
*
* @param message
* the message to be sent
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
*/
public CommandFuture broadcast(String message) {
Command cmd = ServerCommands.gm(message);
return executeAndReturnError(cmd);
}
/**
* Creates a copy of the channel group specified by {@code sourceGroupId},
* overwriting any other channel group specified by {@code targetGroupId}.
*
* The parameter {@code type} can be used to create server query and template groups.
*
*
* @param sourceGroupId
* the ID of the channel group to copy
* @param targetGroupId
* the ID of another channel group to overwrite
* @param type
* the desired type of channel group
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ChannelGroup#getId()
*/
public CommandFuture copyChannelGroup(int sourceGroupId, int targetGroupId, PermissionGroupDatabaseType type) {
if (targetGroupId <= 0) {
throw new IllegalArgumentException("To create a new channel group, use the method with a String argument");
}
Command cmd = ChannelGroupCommands.channelGroupCopy(sourceGroupId, targetGroupId, type);
return executeAndReturnError(cmd);
}
/**
* Creates a copy of the channel group specified by {@code sourceGroupId} with a given name
* and returns the ID of the newly created channel group.
*
* @param sourceGroupId
* the ID of the channel group to copy
* @param targetName
* the name for the copy of the channel group
* @param type
* the desired type of channel group
*
* @return the ID of the newly created channel group
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ChannelGroup#getId()
*/
public CommandFuture copyChannelGroup(int sourceGroupId, String targetName, PermissionGroupDatabaseType type) {
Command cmd = ChannelGroupCommands.channelGroupCopy(sourceGroupId, targetName, type);
return executeAndReturnIntProperty(cmd, "cgid");
}
/**
* Creates a copy of the server group specified by {@code sourceGroupId},
* overwriting another server group specified by {@code targetGroupId}.
*
* The parameter {@code type} can be used to create server query and template groups.
*
*
* @param sourceGroupId
* the ID of the server group to copy
* @param targetGroupId
* the ID of another server group to overwrite
* @param type
* the desired type of server group
*
* @return the ID of the newly created server group
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ServerGroup#getId()
*/
public CommandFuture copyServerGroup(int sourceGroupId, int targetGroupId, PermissionGroupDatabaseType type) {
if (targetGroupId <= 0) {
throw new IllegalArgumentException("To create a new server group, use the method with a String argument");
}
Command cmd = ServerGroupCommands.serverGroupCopy(sourceGroupId, targetGroupId, type);
return executeAndReturnIntProperty(cmd, "sgid");
}
/**
* Creates a copy of the server group specified by {@code sourceGroupId} with a given name
* and returns the ID of the newly created server group.
*
* @param sourceGroupId
* the ID of the server group to copy
* @param targetName
* the name for the copy of the server group
* @param type
* the desired type of server group
*
* @return the ID of the newly created server group
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ServerGroup#getId()
*/
public CommandFuture copyServerGroup(int sourceGroupId, String targetName, PermissionGroupDatabaseType type) {
Command cmd = ServerGroupCommands.serverGroupCopy(sourceGroupId, targetName, type);
return executeAndReturnIntProperty(cmd, "sgid");
}
/**
* Creates a new channel with a given name using the given properties and returns its ID.
*
* @param name
* the name for the new channel
* @param options
* a map of options that should be set for the channel
*
* @return the ID of the newly created channel
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel
*/
public CommandFuture createChannel(String name, Map options) {
Command cmd = ChannelCommands.channelCreate(name, options);
return executeAndReturnIntProperty(cmd, "cid");
}
/**
* Creates a new directory on the file repository in the specified channel.
*
* @param directoryPath
* the path to the directory that should be created
* @param channelId
* the ID of the channel the directory should be created in
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see FileInfo#getPath()
* @see Channel#getId()
*/
public CommandFuture createFileDirectory(String directoryPath, int channelId) {
return createFileDirectory(directoryPath, channelId, null);
}
/**
* Creates a new directory on the file repository in the specified channel.
*
* @param directoryPath
* the path to the directory that should be created
* @param channelId
* the ID of the channel the directory should be created in
* @param channelPassword
* the password of that channel
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see FileInfo#getPath()
* @see Channel#getId()
*/
public CommandFuture createFileDirectory(String directoryPath, int channelId, String channelPassword) {
Command cmd = FileCommands.ftCreateDir(directoryPath, channelId, channelPassword);
return executeAndReturnError(cmd);
}
/**
* Creates a new virtual server with the given name and returns an object containing the ID of the newly
* created virtual server, the default server admin token and the virtual server's voice port. Usually,
* the virtual server is also automatically started. This can be turned off on the TS3 server, though.
*
* If {@link VirtualServerProperty#VIRTUALSERVER_PORT} is not specified in the virtual server properties,
* the server will test for the first unused UDP port.
*
* Please also note that creating virtual servers usually requires the server query admin account
* and that there is a limit to how many virtual servers can be created, which is dependent on your license.
* Unlicensed TS3 server instances are limited to 1 virtual server with up to 32 client slots.
*
*
* @param name
* the name for the new virtual server
* @param options
* a map of options that should be set for the virtual server
*
* @return information about the newly created virtual server
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see VirtualServer
*/
public CommandFuture createServer(String name, Map options) {
Command cmd = VirtualServerCommands.serverCreate(name, options);
return executeAndTransformFirst(cmd, CreatedVirtualServer::new);
}
/**
* Creates a {@link Snapshot} of the selected virtual server containing all settings,
* groups and known client identities. The data from a server snapshot can be
* used to restore a virtual servers configuration.
*
* @return a snapshot of the virtual server
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see #deployServerSnapshot(Snapshot)
*/
public CommandFuture createServerSnapshot() {
Command cmd = VirtualServerCommands.serverSnapshotCreate();
CommandFuture future = cmd.getFuture()
.map(result -> new Snapshot(result.getRawResponse()));
commandQueue.enqueueCommand(cmd);
return future;
}
/**
* Deletes all active ban rules from the server. Use with caution.
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
*/
public CommandFuture deleteAllBans() {
Command cmd = BanCommands.banDelAll();
return executeAndReturnError(cmd);
}
/**
* Deletes all complaints about the client with specified database ID from the server.
*
* @param clientDBId
* the database ID of the client
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getDatabaseId()
* @see Complaint
*/
public CommandFuture deleteAllComplaints(int clientDBId) {
Command cmd = ComplaintCommands.complainDelAll(clientDBId);
return executeAndReturnError(cmd);
}
/**
* Deletes the ban rule with the specified ID from the server.
*
* @param banId
* the ID of the ban to delete
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Ban#getId()
*/
public CommandFuture deleteBan(int banId) {
Command cmd = BanCommands.banDel(banId);
return executeAndReturnError(cmd);
}
/**
* Deletes an existing channel specified by its ID, kicking all clients out of the channel.
*
* @param channelId
* the ID of the channel to delete
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel#getId()
* @see #deleteChannel(int, boolean)
* @see #kickClientFromChannel(String, int...)
*/
public CommandFuture deleteChannel(int channelId) {
return deleteChannel(channelId, true);
}
/**
* Deletes an existing channel with a given ID.
* If {@code force} is true, the channel will be deleted even if there are clients within,
* else the command will fail in this situation.
*
* @param channelId
* the ID of the channel to delete
* @param force
* whether clients should be kicked out of the channel
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel#getId()
* @see #kickClientFromChannel(String, int...)
*/
public CommandFuture deleteChannel(int channelId, boolean force) {
Command cmd = ChannelCommands.channelDelete(channelId, force);
return executeAndReturnError(cmd);
}
/**
* Removes a specified permission from a client in a specific channel.
*
* @param channelId
* the ID of the channel wherein the permission should be removed
* @param clientDBId
* the database ID of the client
* @param permName
* the name of the permission to revoke
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel#getId()
* @see Client#getDatabaseId()
* @see Permission#getName()
*/
public CommandFuture deleteChannelClientPermission(int channelId, int clientDBId, String permName) {
Command cmd = PermissionCommands.channelClientDelPerm(channelId, clientDBId, permName);
return executeAndReturnError(cmd);
}
/**
* Removes the channel group with the given ID.
*
* @param groupId
* the ID of the channel group
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ChannelGroup#getId()
*/
public CommandFuture deleteChannelGroup(int groupId) {
return deleteChannelGroup(groupId, true);
}
/**
* Removes the channel group with the given ID.
* If {@code force} is true, the channel group will be deleted even if it still contains clients,
* else the command will fail in this situation.
*
* @param groupId
* the ID of the channel group
* @param force
* whether the channel group should be deleted even if it still contains clients
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ChannelGroup#getId()
*/
public CommandFuture deleteChannelGroup(int groupId, boolean force) {
Command cmd = ChannelGroupCommands.channelGroupDel(groupId, force);
return executeAndReturnError(cmd);
}
/**
* Removes a permission from the channel group with the given ID.
*
* @param groupId
* the ID of the channel group
* @param permName
* the name of the permission to revoke
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ChannelGroup#getId()
* @see Permission#getName()
*/
public CommandFuture deleteChannelGroupPermission(int groupId, String permName) {
Command cmd = PermissionCommands.channelGroupDelPerm(groupId, permName);
return executeAndReturnError(cmd);
}
/**
* Removes a permission from the channel with the given ID.
*
* @param channelId
* the ID of the channel
* @param permName
* the name of the permission to revoke
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel#getId()
* @see Permission#getName()
*/
public CommandFuture deleteChannelPermission(int channelId, String permName) {
Command cmd = PermissionCommands.channelDelPerm(channelId, permName);
return executeAndReturnError(cmd);
}
/**
* Removes a permission from a client.
*
* @deprecated
* This method is no longer preferred for removing permissions from a client.
*
* Use {@link TS3ApiAsync#deleteClientPermission(int, IPermissionType)}
* or {@link TS3ApiAsync#deleteClientPermission(int, BPermissionType)} instead.
*
*
* @param clientDBId
* the database ID of the client
* @param permName
* the name of the permission to revoke
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getDatabaseId()
* @see Permission#getName()
*/
@Deprecated
public CommandFuture deleteClientPermission(int clientDBId, String permName) {
Command cmd = PermissionCommands.clientDelPerm(clientDBId, permName);
return executeAndReturnError(cmd);
}
/**
* Removes a permission from a client.
*
* @param clientDBId
* the database ID of the client
* @param permName
* the enum of the permission to revoke
* @see IPermissionType
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getDatabaseId()
* @see Permission#getName()
*/
public CommandFuture deleteClientPermission(int clientDBId, IPermissionType permName) {
Command cmd = PermissionCommands.clientDelPerm(clientDBId, permName.getName());
return executeAndReturnError(cmd);
}
/**
* Removes a permission from a client.
*
* @param clientDBId
* the database ID of the client
* @param permName
* the enum of the permission to revoke
* @see BPermissionType
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getDatabaseId()
* @see Permission#getName()
*/
public CommandFuture deleteClientPermission(int clientDBId, BPermissionType permName) {
Command cmd = PermissionCommands.clientDelPerm(clientDBId, permName.getName());
return executeAndReturnError(cmd);
}
/**
* Deletes the complaint about the client with database ID {@code targetClientDBId} submitted by
* the client with database ID {@code fromClientDBId} from the server.
*
* @param targetClientDBId
* the database ID of the client the complaint is about
* @param fromClientDBId
* the database ID of the client who added the complaint
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Complaint
* @see Client#getDatabaseId()
*/
public CommandFuture deleteComplaint(int targetClientDBId, int fromClientDBId) {
Command cmd = ComplaintCommands.complainDel(targetClientDBId, fromClientDBId);
return executeAndReturnError(cmd);
}
/**
* Removes the {@code key} custom client property from a client.
*
* @param clientDBId
* the database ID of the target client
* @param key
* the key of the custom property to delete, cannot be {@code null}
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getDatabaseId()
*/
public CommandFuture deleteCustomClientProperty(int clientDBId, String key) {
if (key == null) throw new IllegalArgumentException("Key cannot be null");
Command cmd = CustomPropertyCommands.customDelete(clientDBId, key);
return executeAndReturnError(cmd);
}
/**
* Removes all stored database information about the specified client.
* Please note that this data is also automatically removed after a configured time (usually 90 days).
*
* See {@link DatabaseClientInfo} for a list of stored information about a client.
*
*
* @param clientDBId
* the database ID of the client
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getDatabaseId()
* @see #getDatabaseClientInfo(int)
* @see DatabaseClientInfo
*/
public CommandFuture deleteDatabaseClientProperties(int clientDBId) {
Command cmd = DatabaseClientCommands.clientDBDelete(clientDBId);
return executeAndReturnError(cmd);
}
/**
* Deletes a file or directory from the file repository in the specified channel.
*
* @param filePath
* the path to the file or directory
* @param channelId
* the ID of the channel the file or directory resides in
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see FileInfo#getPath()
* @see Channel#getId()
*/
public CommandFuture deleteFile(String filePath, int channelId) {
return deleteFile(filePath, channelId, null);
}
/**
* Deletes a file or directory from the file repository in the specified channel.
*
* @param filePath
* the path to the file or directory
* @param channelId
* the ID of the channel the file or directory resides in
* @param channelPassword
* the password of that channel
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see FileInfo#getPath()
* @see Channel#getId()
*/
public CommandFuture deleteFile(String filePath, int channelId, String channelPassword) {
Command cmd = FileCommands.ftDeleteFile(channelId, channelPassword, filePath);
return executeAndReturnError(cmd);
}
/**
* Deletes multiple files or directories from the file repository in the specified channel.
*
* @param filePaths
* the paths to the files or directories
* @param channelId
* the ID of the channel the file or directory resides in
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see FileInfo#getPath()
* @see Channel#getId()
*/
public CommandFuture deleteFiles(String[] filePaths, int channelId) {
return deleteFiles(filePaths, channelId, null);
}
/**
* Deletes multiple files or directories from the file repository in the specified channel.
*
* @param filePaths
* the paths to the files or directories
* @param channelId
* the ID of the channel the file or directory resides in
* @param channelPassword
* the password of that channel
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see FileInfo#getPath()
* @see Channel#getId()
*/
public CommandFuture deleteFiles(String[] filePaths, int channelId, String channelPassword) {
Command cmd = FileCommands.ftDeleteFile(channelId, channelPassword, filePaths);
return executeAndReturnError(cmd);
}
/**
* Deletes an icon from the icon directory in the file repository.
*
* @param iconId
* the ID of the icon to delete
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see IconFile#getIconId()
*/
public CommandFuture deleteIcon(long iconId) {
String iconPath = "/icon_" + iconId;
return deleteFile(iconPath, 0);
}
/**
* Deletes multiple icons from the icon directory in the file repository.
*
* @param iconIds
* the IDs of the icons to delete
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see IconFile#getIconId()
*/
public CommandFuture deleteIcons(long... iconIds) {
String[] iconPaths = new String[iconIds.length];
for (int i = 0; i < iconIds.length; ++i) {
iconPaths[i] = "/icon_" + iconIds[i];
}
return deleteFiles(iconPaths, 0);
}
/**
* Deletes the offline message with the specified ID.
*
* @param messageId
* the ID of the offline message to delete
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Message#getId()
*/
public CommandFuture deleteOfflineMessage(int messageId) {
Command cmd = MessageCommands.messageDel(messageId);
return executeAndReturnError(cmd);
}
/**
* Removes a specified permission from all server groups of the type specified by {@code type} on all virtual servers.
*
* @param type
* the kind of server group this permission should be removed from
* @param permName
* the name of the permission to remove
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ServerGroupType
* @see Permission#getName()
*/
public CommandFuture deletePermissionFromAllServerGroups(ServerGroupType type, String permName) {
Command cmd = PermissionCommands.serverGroupAutoDelPerm(type, permName);
return executeAndReturnError(cmd);
}
/**
* Deletes the privilege key with the given token.
*
* @param token
* the token of the privilege key
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see PrivilegeKey
*/
public CommandFuture deletePrivilegeKey(String token) {
Command cmd = PrivilegeKeyCommands.privilegeKeyDelete(token);
return executeAndReturnError(cmd);
}
/**
* Deletes the virtual server with the specified ID.
*
* Only stopped virtual servers can be deleted.
*
*
* @param serverId
* the ID of the virtual server
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see VirtualServer#getId()
* @see #stopServer(int)
*/
public CommandFuture deleteServer(int serverId) {
Command cmd = VirtualServerCommands.serverDelete(serverId);
return executeAndReturnError(cmd);
}
/**
* Deletes the server group with the specified ID, even if the server group still contains clients.
*
* @param groupId
* the ID of the server group
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ServerGroup#getId()
*/
public CommandFuture deleteServerGroup(int groupId) {
return deleteServerGroup(groupId, true);
}
/**
* Deletes a server group with the specified ID.
*
* If {@code force} is true, the server group will be deleted even if it contains clients,
* else the command will fail in this situation.
*
*
* @param groupId
* the ID of the server group
* @param force
* whether the server group should be deleted if it still contains clients
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ServerGroup#getId()
*/
public CommandFuture deleteServerGroup(int groupId, boolean force) {
Command cmd = ServerGroupCommands.serverGroupDel(groupId, force);
return executeAndReturnError(cmd);
}
/**
* Removes a permission from the server group with the given ID.
*
* @param groupId
* the ID of the server group
* @param permName
* the name of the permission to revoke
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ServerGroup#getId()
* @see Permission#getName()
*/
public CommandFuture deleteServerGroupPermission(int groupId, String permName) {
Command cmd = PermissionCommands.serverGroupDelPerm(groupId, permName);
return executeAndReturnError(cmd);
}
/**
* Deletes the server query login with the specified client database ID.
*
* If you only know the name of the server query login, use {@link #getServerQueryLoginsByName(String)} first.
*
*
* @param clientDBId
* the client database ID of the server query login (usually the ID of the associated client)
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see #addServerQueryLogin(String, int)
* @see #getServerQueryLogins()
* @see #updateServerQueryLogin(String)
*/
public CommandFuture deleteServerQueryLogin(int clientDBId) {
Command cmd = QueryLoginCommands.queryLoginDel(clientDBId);
return executeAndReturnError(cmd);
}
/**
* Restores the selected virtual servers configuration using the data from a
* previously created server snapshot.
*
* @param snapshot
* the snapshot to restore
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see #createServerSnapshot()
*/
public CommandFuture deployServerSnapshot(Snapshot snapshot) {
return deployServerSnapshot(snapshot.get());
}
/**
* Restores the configuration of the selected virtual server using the data from a
* previously created server snapshot.
*
* @param snapshot
* the snapshot to restore
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see #createServerSnapshot()
*/
public CommandFuture deployServerSnapshot(String snapshot) {
Command cmd = VirtualServerCommands.serverSnapshotDeploy(snapshot);
return executeAndReturnError(cmd);
}
/**
* Downloads a file from the file repository at a given path and channel
* and writes the file's bytes to an open {@link OutputStream}.
*
* It is the user's responsibility to ensure that the given {@code OutputStream} is
* open and to close the stream again once the download has finished.
*
* Note that this method will not read the entire file to memory and can thus
* download arbitrarily sized files from the file repository.
*
*
* @param dataOut
* a stream that the downloaded data should be written to
* @param filePath
* the path of the file on the file repository
* @param channelId
* the ID of the channel to download the file from
*
* @return how many bytes were downloaded
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @throws TS3FileTransferFailedException
* if the file transfer fails for any reason
* @querycommands 1
* @see FileInfo#getPath()
* @see Channel#getId()
* @see #downloadFileDirect(String, int)
*/
public CommandFuture downloadFile(OutputStream dataOut, String filePath, int channelId) {
return downloadFile(dataOut, filePath, channelId, null);
}
/**
* Downloads a file from the file repository at a given path and channel
* and writes the file's bytes to an open {@link OutputStream}.
*
* It is the user's responsibility to ensure that the given {@code OutputStream} is
* open and to close the stream again once the download has finished.
*
* Note that this method will not read the entire file to memory and can thus
* download arbitrarily sized files from the file repository.
*
*
* @param dataOut
* a stream that the downloaded data should be written to
* @param filePath
* the path of the file on the file repository
* @param channelId
* the ID of the channel to download the file from
* @param channelPassword
* that channel's password
*
* @return how many bytes were downloaded
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @throws TS3FileTransferFailedException
* if the file transfer fails for any reason
* @querycommands 1
* @see FileInfo#getPath()
* @see Channel#getId()
* @see #downloadFileDirect(String, int, String)
*/
public CommandFuture downloadFile(OutputStream dataOut, String filePath, int channelId, String channelPassword) {
FileTransferHelper helper = query.getFileTransferHelper();
int transferId = helper.getClientTransferId();
Command cmd = FileCommands.ftInitDownload(transferId, filePath, channelId, channelPassword);
CommandFuture future = new CommandFuture<>();
executeAndTransformFirst(cmd, FileTransferParameters::new).onSuccess(params -> {
QueryError error = params.getQueryError();
if (!error.isSuccessful()) {
future.fail(new TS3CommandFailedException(error, cmd.getName()));
return;
}
try {
query.getFileTransferHelper().downloadFile(dataOut, params);
} catch (IOException e) {
future.fail(new TS3FileTransferFailedException("Download failed", e));
return;
}
future.set(params.getFileSize());
}).forwardFailure(future);
return future;
}
/**
* Downloads a file from the file repository at a given path and channel
* and returns the file's bytes as a byte array.
*
* Note that this method will read the entire file to memory.
* That means that if a file is larger than 231-1 bytes in size,
* the download will fail.
*
*
* @param filePath
* the path of the file on the file repository
* @param channelId
* the ID of the channel to download the file from
*
* @return a byte array containing the file's data
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @throws TS3FileTransferFailedException
* if the file transfer fails for any reason
* @querycommands 1
* @see FileInfo#getPath()
* @see Channel#getId()
* @see #downloadFile(OutputStream, String, int)
*/
public CommandFuture downloadFileDirect(String filePath, int channelId) {
return downloadFileDirect(filePath, channelId, null);
}
/**
* Downloads a file from the file repository at a given path and channel
* and returns the file's bytes as a byte array.
*
* Note that this method will read the entire file to memory.
* That means that if a file is larger than 231-1 bytes in size,
* the download will fail.
*
*
* @param filePath
* the path of the file on the file repository
* @param channelId
* the ID of the channel to download the file from
* @param channelPassword
* that channel's password
*
* @return a byte array containing the file's data
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @throws TS3FileTransferFailedException
* if the file transfer fails for any reason
* @querycommands 1
* @see FileInfo#getPath()
* @see Channel#getId()
* @see #downloadFile(OutputStream, String, int, String)
*/
public CommandFuture downloadFileDirect(String filePath, int channelId, String channelPassword) {
FileTransferHelper helper = query.getFileTransferHelper();
int transferId = helper.getClientTransferId();
Command cmd = FileCommands.ftInitDownload(transferId, filePath, channelId, channelPassword);
CommandFuture future = new CommandFuture<>();
executeAndTransformFirst(cmd, FileTransferParameters::new).onSuccess(params -> {
QueryError error = params.getQueryError();
if (!error.isSuccessful()) {
future.fail(new TS3CommandFailedException(error, cmd.getName()));
return;
}
long fileSize = params.getFileSize();
if (fileSize > Integer.MAX_VALUE) {
future.fail(new TS3FileTransferFailedException("File too big for byte array"));
return;
}
ByteArrayOutputStream dataOut = new ByteArrayOutputStream((int) fileSize);
try {
query.getFileTransferHelper().downloadFile(dataOut, params);
} catch (IOException e) {
future.fail(new TS3FileTransferFailedException("Download failed", e));
return;
}
future.set(dataOut.toByteArray());
}).forwardFailure(future);
return future;
}
/**
* Downloads an icon from the icon directory in the file repository
* and writes the file's bytes to an open {@link OutputStream}.
*
* It is the user's responsibility to ensure that the given {@code OutputStream} is
* open and to close the stream again once the download has finished.
*
*
* @param dataOut
* a stream that the downloaded data should be written to
* @param iconId
* the ID of the icon that should be downloaded
*
* @return a byte array containing the icon file's data
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @throws TS3FileTransferFailedException
* if the file transfer fails for any reason
* @querycommands 1
* @see IconFile#getIconId()
* @see #downloadIconDirect(long)
* @see #uploadIcon(InputStream, long)
*/
public CommandFuture downloadIcon(OutputStream dataOut, long iconId) {
String iconPath = "/icon_" + iconId;
return downloadFile(dataOut, iconPath, 0);
}
/**
* Downloads an icon from the icon directory in the file repository
* and returns the file's bytes as a byte array.
*
* Note that this method will read the entire file to memory.
*
*
* @param iconId
* the ID of the icon that should be downloaded
*
* @return a byte array containing the icon file's data
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @throws TS3FileTransferFailedException
* if the file transfer fails for any reason
* @querycommands 1
* @see IconFile#getIconId()
* @see #downloadIcon(OutputStream, long)
* @see #uploadIconDirect(byte[])
*/
public CommandFuture downloadIconDirect(long iconId) {
String iconPath = "/icon_" + iconId;
return downloadFileDirect(iconPath, 0);
}
/**
* Changes a channel's configuration using the given properties.
*
* @param channelId
* the ID of the channel to edit
* @param options
* the map of properties to modify
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel#getId()
*/
public CommandFuture editChannel(int channelId, Map options) {
Command cmd = ChannelCommands.channelEdit(channelId, options);
return executeAndReturnError(cmd);
}
/**
* Changes a single property of the given channel.
*
* Note that one can set many properties at once with the overloaded method that
* takes a map of channel properties and strings.
*
*
* @param channelId
* the ID of the channel to edit
* @param property
* the channel property to modify, make sure it is editable
* @param value
* the new value of the property
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel#getId()
* @see #editChannel(int, Map)
*/
public CommandFuture editChannel(int channelId, ChannelProperty property, String value) {
return editChannel(channelId, Collections.singletonMap(property, value));
}
/**
* Changes a client's configuration using given properties.
*
* Only {@link ClientProperty#CLIENT_DESCRIPTION} can be changed for other clients.
* To update the current client's properties, use {@link #updateClient(Map)}
* or {@link #updateClient(ClientProperty, String)}.
*
*
* @param clientId
* the ID of the client to edit
* @param options
* the map of properties to modify
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getId()
* @see #updateClient(Map)
*/
public CommandFuture editClient(int clientId, Map options) {
Command cmd = ClientCommands.clientEdit(clientId, options);
return executeAndReturnError(cmd);
}
/**
* Changes a single property of the given client.
*
* Only {@link ClientProperty#CLIENT_DESCRIPTION} can be changed for other clients.
* To update the current client's properties, use {@link #updateClient(Map)}
* or {@link #updateClient(ClientProperty, String)}.
*
*
* @param clientId
* the ID of the client to edit
* @param property
* the client property to modify, make sure it is editable
* @param value
* the new value of the property
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getId()
* @see #editClient(int, Map)
* @see #updateClient(Map)
*/
public CommandFuture editClient(int clientId, ClientProperty property, String value) {
return editClient(clientId, Collections.singletonMap(property, value));
}
/**
* Changes a client's database settings using given properties.
*
* @param clientDBId
* the database ID of the client to edit
* @param options
* the map of properties to modify
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see DatabaseClientInfo
* @see Client#getDatabaseId()
*/
public CommandFuture editDatabaseClient(int clientDBId, Map options) {
Command cmd = DatabaseClientCommands.clientDBEdit(clientDBId, options);
return executeAndReturnError(cmd);
}
/**
* Changes the server instance configuration using given properties.
* If the given property is not changeable, {@code IllegalArgumentException} will be thrown.
*
* @param property
* the property to edit, must be changeable
* @param value
* the new value for the edit
*
* @return a future to track the progress of this command
*
* @throws IllegalArgumentException
* if {@code property} is not changeable
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ServerInstanceProperty#isChangeable()
*/
public CommandFuture editInstance(ServerInstanceProperty property, String value) {
Command cmd = ServerCommands.instanceEdit(Collections.singletonMap(property, value));
return executeAndReturnError(cmd);
}
/**
* Changes the configuration of the selected virtual server using given properties.
*
* @param options
* the map of properties to edit
*
* @return a future to track the progress of this command
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see VirtualServerProperty
*/
public CommandFuture editServer(Map options) {
Command cmd = VirtualServerCommands.serverEdit(options);
return executeAndReturnError(cmd);
}
/**
* Gets a list of all bans on the selected virtual server.
*
* @return a list of all bans on the virtual server
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Ban
*/
public CommandFuture> getBans() {
Command cmd = BanCommands.banList();
return executeAndTransform(cmd, Ban::new);
}
/**
* Gets a list of IP addresses used by the server instance.
*
* @return the list of bound IP addresses
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Binding
*/
public CommandFuture> getBindings() {
Command cmd = ServerCommands.bindingList();
return executeAndTransform(cmd, Binding::new);
}
/**
* Finds and returns the channel matching the given name exactly.
*
* @param name
* the name of the channel
* @param ignoreCase
* whether the case of the name should be ignored
*
* @return the found channel or {@code null} if no channel was found
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel
* @see #getChannelsByName(String)
*/
public CommandFuture getChannelByNameExact(String name, boolean ignoreCase) {
String caseName = ignoreCase ? name.toLowerCase(Locale.ROOT) : name;
return getChannels().map(allChannels -> {
for (Channel c : allChannels) {
String channelName = ignoreCase ? c.getName().toLowerCase(Locale.ROOT) : c.getName();
if (caseName.equals(channelName)) return c;
}
return null; // Not found
});
}
/**
* Gets a list of channels whose names contain the given search string.
*
* @param name
* the name to search
*
* @return a list of all channels with names matching the search pattern
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 2
* @see Channel
* @see #getChannelByNameExact(String, boolean)
*/
public CommandFuture> getChannelsByName(String name) {
Command cmd = ChannelCommands.channelFind(name);
CommandFuture> future = new CommandFuture<>();
CommandFuture> channelIds = executeAndMap(cmd, response -> response.getInt("cid"));
CommandFuture> allChannels = getChannels();
findByKey(channelIds, allChannels, Channel::getId)
.forwardSuccess(future)
.onFailure(transformError(future, 768, Collections.emptyList()));
return future;
}
/**
* Displays a list of permissions defined for a client in a specific channel.
*
* @param channelId
* the ID of the channel
* @param clientDBId
* the database ID of the client
*
* @return a list of permissions for the user in the specified channel
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel#getId()
* @see Client#getDatabaseId()
* @see Permission
*/
public CommandFuture> getChannelClientPermissions(int channelId, int clientDBId) {
Command cmd = PermissionCommands.channelClientPermList(channelId, clientDBId);
return executeAndTransform(cmd, Permission::new);
}
/**
* Gets all client / channel ID combinations currently assigned to channel groups.
* All three parameters are optional and can be turned off by setting it to {@code -1}.
*
* @param channelId
* restricts the search to the channel with a specified ID. Set to {@code -1} to ignore.
* @param clientDBId
* restricts the search to the client with a specified database ID. Set to {@code -1} to ignore.
* @param groupId
* restricts the search to the channel group with the specified ID. Set to {@code -1} to ignore.
*
* @return a list of combinations of channel ID, client database ID and channel group ID
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel#getId()
* @see Client#getDatabaseId()
* @see ChannelGroup#getId()
* @see ChannelGroupClient
*/
public CommandFuture> getChannelGroupClients(int channelId, int clientDBId, int groupId) {
Command cmd = ChannelGroupCommands.channelGroupClientList(channelId, clientDBId, groupId);
return executeAndTransform(cmd, ChannelGroupClient::new);
}
/**
* Gets all client / channel ID combinations currently assigned to the specified channel group.
*
* @param groupId
* the ID of the channel group whose client / channel assignments should be returned.
*
* @return a list of combinations of channel ID, client database ID and channel group ID
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ChannelGroup#getId()
* @see ChannelGroupClient
* @see #getChannelGroupClients(int, int, int)
*/
public CommandFuture> getChannelGroupClientsByChannelGroupId(int groupId) {
return getChannelGroupClients(-1, -1, groupId);
}
/**
* Gets all channel group assignments in the specified channel.
*
* @param channelId
* the ID of the channel whose channel group assignments should be returned.
*
* @return a list of combinations of channel ID, client database ID and channel group ID
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel#getId()
* @see ChannelGroupClient
* @see #getChannelGroupClients(int, int, int)
*/
public CommandFuture> getChannelGroupClientsByChannelId(int channelId) {
return getChannelGroupClients(channelId, -1, -1);
}
/**
* Gets all channel group assignments for the specified client.
*
* @param clientDBId
* the database ID of the client whose channel group
*
* @return a list of combinations of channel ID, client database ID and channel group ID
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getDatabaseId()
* @see ChannelGroupClient
* @see #getChannelGroupClients(int, int, int)
*/
public CommandFuture> getChannelGroupClientsByClientDBId(int clientDBId) {
return getChannelGroupClients(-1, clientDBId, -1);
}
/**
* Gets a list of all permissions assigned to the specified channel group.
*
* @param groupId
* the ID of the channel group.
*
* @return a list of permissions assigned to the channel group
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ChannelGroup#getId()
* @see Permission
*/
public CommandFuture> getChannelGroupPermissions(int groupId) {
Command cmd = PermissionCommands.channelGroupPermList(groupId);
return executeAndTransform(cmd, Permission::new);
}
/**
* Gets a list of all channel groups on the selected virtual server.
*
* @return a list of all channel groups on the virtual server
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ChannelGroup
*/
public CommandFuture> getChannelGroups() {
Command cmd = ChannelGroupCommands.channelGroupList();
return executeAndTransform(cmd, ChannelGroup::new);
}
/**
* Gets detailed configuration information about the channel specified channel.
*
* @param channelId
* the ID of the channel
*
* @return information about the channel
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel#getId()
* @see ChannelInfo
*/
public CommandFuture getChannelInfo(int channelId) {
Command cmd = ChannelCommands.channelInfo(channelId);
return executeAndTransformFirst(cmd, map -> new ChannelInfo(channelId, map));
}
/**
* Gets a list of all permissions assigned to the specified channel.
*
* @param channelId
* the ID of the channel
*
* @return a list of all permissions assigned to the channel
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel#getId()
* @see Permission
*/
public CommandFuture> getChannelPermissions(int channelId) {
Command cmd = PermissionCommands.channelPermList(channelId);
return executeAndTransform(cmd, Permission::new);
}
/**
* Gets a list of all channels on the selected virtual server.
*
* @return a list of all channels on the virtual server
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Channel
*/
public CommandFuture> getChannels() {
Command cmd = ChannelCommands.channelList();
return executeAndTransform(cmd, Channel::new);
}
/**
* Finds and returns the client whose nickname matches the given name exactly.
*
* @param name
* the name of the client
* @param ignoreCase
* whether the case of the name should be ignored
*
* @return the found client or {@code null} if no client was found
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client
* @see #getClientsByName(String)
*/
public CommandFuture getClientByNameExact(String name, boolean ignoreCase) {
String caseName = ignoreCase ? name.toLowerCase(Locale.ROOT) : name;
return getClients().map(allClients -> {
for (Client c : allClients) {
String clientName = ignoreCase ? c.getNickname().toLowerCase(Locale.ROOT) : c.getNickname();
if (caseName.equals(clientName)) return c;
}
return null; // Not found
});
}
/**
* Gets a list of clients whose nicknames contain the given search string.
*
* @param name
* the name to search
*
* @return a list of all clients with nicknames matching the search pattern
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 2
* @see Client
* @see #getClientByNameExact(String, boolean)
*/
public CommandFuture> getClientsByName(String name) {
Command cmd = ClientCommands.clientFind(name);
CommandFuture> future = new CommandFuture<>();
CommandFuture> clientIds = executeAndMap(cmd, response -> response.getInt("clid"));
CommandFuture> allClients = getClients();
findByKey(clientIds, allClients, Client::getId)
.forwardSuccess(future)
.onFailure(transformError(future, 512, Collections.emptyList()));
return future;
}
/**
* Gets information about the client with the specified unique identifier.
*
* @param clientUId
* the unique identifier of the client
*
* @return information about the client
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 2
* @see Client#getUniqueIdentifier()
* @see ClientInfo
*/
public CommandFuture getClientByUId(String clientUId) {
Command cmd = ClientCommands.clientGetIds(clientUId);
return executeAndReturnIntProperty(cmd, "clid")
.then(this::getClientInfo);
}
/**
* Gets information about the client with the specified client ID.
*
* @param clientId
* the client ID of the client
*
* @return information about the client
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getId()
* @see ClientInfo
*/
public CommandFuture getClientInfo(int clientId) {
Command cmd = ClientCommands.clientInfo(clientId);
return executeAndTransformFirst(cmd, map -> new ClientInfo(clientId, map));
}
/**
* Gets a list of all permissions assigned to the specified client.
*
* @param clientDBId
* the database ID of the client
*
* @return a list of all permissions assigned to the client
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getDatabaseId()
* @see Permission
*/
public CommandFuture> getClientPermissions(int clientDBId) {
Command cmd = PermissionCommands.clientPermList(clientDBId);
return executeAndTransform(cmd, Permission::new);
}
/**
* Gets a list of all clients on the selected virtual server.
*
* @return a list of all clients on the virtual server
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client
*/
public CommandFuture> getClients() {
Command cmd = ClientCommands.clientList();
return executeAndTransform(cmd, Client::new);
}
/**
* Gets a list of all complaints on the selected virtual server.
*
* @return a list of all complaints on the virtual server
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Complaint
* @see #getComplaints(int)
*/
public CommandFuture> getComplaints() {
return getComplaints(-1);
}
/**
* Gets a list of all complaints about the specified client.
*
* @param clientDBId
* the database ID of the client
*
* @return a list of all complaints about the specified client
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getDatabaseId()
* @see Complaint
*/
public CommandFuture> getComplaints(int clientDBId) {
Command cmd = ComplaintCommands.complainList(clientDBId);
return executeAndTransform(cmd, Complaint::new);
}
/**
* Gets detailed connection information about the selected virtual server.
*
* @return connection information about the selected virtual server
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see ConnectionInfo
* @see #getServerInfo()
*/
public CommandFuture getConnectionInfo() {
Command cmd = VirtualServerCommands.serverRequestConnectionInfo();
return executeAndTransformFirst(cmd, ConnectionInfo::new);
}
/**
* Gets a map of all custom client properties and their values
* assigned to the client with database ID {@code clientDBId}.
*
* @param clientDBId
* the database ID of the target client
*
* @return a map of the client's custom client property assignments
*
* @throws TS3CommandFailedException
* if the execution of a command fails
* @querycommands 1
* @see Client#getDatabaseId()
* @see #searchCustomClientProperty(String)
* @see #searchCustomClientProperty(String, String)
*/
public CommandFuture