forked from TheHolyWaffle/TeamSpeak-3-Java-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TS3Config.java
354 lines (317 loc) · 10.7 KB
/
TS3Config.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
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.TS3Query.FloodRate;
import com.github.theholywaffle.teamspeak3.TS3Query.Protocol;
import com.github.theholywaffle.teamspeak3.api.reconnect.ConnectionHandler;
import com.github.theholywaffle.teamspeak3.api.reconnect.ReconnectStrategy;
/**
* Class used to configure the behavior of a {@link TS3Query}.
*/
public class TS3Config {
private boolean frozen = false;
private String host = null;
private int queryPort = -1;
private Protocol protocol = Protocol.RAW;
private String username = null;
private String password = null;
private FloodRate floodRate = FloodRate.DEFAULT;
private boolean enableCommunicationsLogging = false;
private int commandTimeout = 4000;
private ReconnectStrategy reconnectStrategy = ReconnectStrategy.disconnect();
private ConnectionHandler connectionHandler = null;
/**
* Sets the hostname or IP address of the TeamSpeak3 server to connect to.
* <p>
* Note that the query port <strong>is not</strong> part of the hostname -
* use {@link #setQueryPort(int)} for that purpose.
* </p><p>
* If the application is running on the same machine as the TS3 server, you can use
* {@code null} as the hostname. You can also use any other loopback address,
* such as {@code localhost} or {@code 127.0.0.1}.
* </p>
*
* @param host
* a valid hostname or IP address of a TeamSpeak3 server, or {@code null}
*
* @return this TS3Config object for chaining
*/
public TS3Config setHost(String host) {
checkFrozen();
this.host = host;
return this;
}
String getHost() {
return host;
}
/**
* Sets the query port to use when connecting to the TeamSpeak3 server.
* <p>
* Note that the query uses a different port to connect to a server than the regular
* TeamSpeak3 clients. Regular clients use "voice ports", the query uses the "query port".
* </p><p>
* If you don't set the query port by calling this method, the query will use the default
* query port:
* </p>
* <ul>
* <li>{@code 10011} when connecting using {@link Protocol#RAW}</li>
* <li>{@code 10022} when connecting using {@link Protocol#SSH}</li>
* </ul>
*
* @param queryPort
* the query port to use, must be between {@code 1} and {@code 65535}
*
* @return this TS3Config object for chaining
*
* @throws IllegalArgumentException
* if the port is out of range
*/
public TS3Config setQueryPort(int queryPort) {
checkFrozen();
if (queryPort <= 0 || queryPort > 65535) {
throw new IllegalArgumentException("Port out of range: " + queryPort);
}
this.queryPort = queryPort;
return this;
}
int getQueryPort() {
if (queryPort > 0) {
return queryPort;
} else {
// Query port not set by user, use default for chosen protocol
return protocol == Protocol.SSH ? 10022 : 10011;
}
}
/**
* Defines the protocol used to connect to the TeamSpeak3 server.
* By default, {@link Protocol#RAW} is used.
*
* @param protocol
* the connection protocol to use
*
* @return this TS3Config object for chaining
*
* @throws IllegalArgumentException
* if {@code protocol} is {@code null}
* @see Protocol Protocol
*/
public TS3Config setProtocol(Protocol protocol) {
checkFrozen();
if (protocol == null) throw new IllegalArgumentException("protocol cannot be null!");
this.protocol = protocol;
return this;
}
Protocol getProtocol() {
return protocol;
}
/**
* Authenticates the query with the TeamSpeak3 server using the given login credentials
* immediately after connecting.
* <p>
* Setting the login credentials is mandatory when using the {@link Protocol#SSH} protocol.
* </p><p>
* A server query login can be generated by heading over to the TeamSpeak3 Client, Tools,
* ServerQuery Login. Note that the server query will have the same permissions as the client who
* generated the credentials.
* </p>
*
* @param username
* the username used to authenticate the query
* @param password
* the password corresponding to {@code username}
*
* @return this TS3Config object for chaining
*/
public TS3Config setLoginCredentials(String username, String password) {
checkFrozen();
this.username = username;
this.password = password;
return this;
}
boolean hasLoginCredentials() {
return username != null && password != null;
}
String getUsername() {
return username;
}
String getPassword() {
return password;
}
/**
* Sets the delay between sending commands.
* <p>
* If the query's hostname / IP has not been added to the server's {@code query_ip_whitelist.txt},
* you need to use {@link FloodRate#DEFAULT} to prevent the query from being flood-banned.
* </p><p>
* Calling {@link FloodRate#custom} allows you to use a custom command delay if neither
* {@link FloodRate#UNLIMITED} nor {@link FloodRate#DEFAULT} fit your needs.
* </p>
*
* @param rate
* a {@link FloodRate} object that defines the delay between commands
*
* @return this TS3Config object for chaining
*
* @throws IllegalArgumentException
* if {@code rate} is {@code null}
* @see FloodRate FloodRate
*/
public TS3Config setFloodRate(FloodRate rate) {
checkFrozen();
if (rate == null) throw new IllegalArgumentException("rate cannot be null!");
this.floodRate = rate;
return this;
}
FloodRate getFloodRate() {
return floodRate;
}
/**
* Setting this value to {@code true} will log the communication between the
* query client and the TS3 server at the {@code DEBUG} level.
* <p>
* By default, this is turned off to prevent leaking IPs, tokens, passwords, etc.
* into the console and / or log files.
* </p>
*
* @param enable
* whether to log query commands
*
* @return this TS3Config object for chaining
*/
public TS3Config setEnableCommunicationsLogging(boolean enable) {
checkFrozen();
enableCommunicationsLogging = enable;
return this;
}
boolean getEnableCommunicationsLogging() {
return enableCommunicationsLogging;
}
/**
* Sets how long the query should wait for any response to a command before disconnecting.
* <p>
* If the query doesn't receive any data from the TeamSpeak server after
* having waited for at least {@code commandTimeout} milliseconds, the connection
* is considered to be interrupted, and the query will try to reconnect according to
* its {@linkplain TS3Config#setReconnectStrategy(ReconnectStrategy) reconnect strategy}.
* </p><p>
* By default, this timeout is 4000 milliseconds.
* </p>
*
* @param commandTimeout
* the minimum amount of time to wait for any response, in milliseconds
*
* @return this TS3Config object for chaining
*
* @throws IllegalArgumentException
* if the timeout value is less than or equal to {@code 0}
*/
public TS3Config setCommandTimeout(int commandTimeout) {
checkFrozen();
if (commandTimeout <= 0) {
throw new IllegalArgumentException("Timeout value must be greater than 0");
}
this.commandTimeout = commandTimeout;
return this;
}
int getCommandTimeout() {
return commandTimeout;
}
/**
* Sets what strategy the query uses to reconnect after having been disconnected.
* <p>
* The different reconnect strategies let you control whether and after which delay the
* query will try to reconnect. By default, {@link ReconnectStrategy#disconnect()} is used,
* which doesn't try to reconnect and simply stops the query.
* </p><p>
* Note that when using a reconnect strategy, you probably also want to set the
* {@link ConnectionHandler} using {@link TS3Config#setConnectionHandler(ConnectionHandler)}.
*
* @param reconnectStrategy
* the reconnect strategy used when the query loses connection
*
* @return this TS3Config object for chaining
*
* @see ReconnectStrategy The reconnect strategies
* @see ConnectionHandler The connection handler
*/
public TS3Config setReconnectStrategy(ReconnectStrategy reconnectStrategy) {
checkFrozen();
if (reconnectStrategy == null) throw new IllegalArgumentException("reconnectStrategy cannot be null!");
this.reconnectStrategy = reconnectStrategy;
return this;
}
ReconnectStrategy getReconnectStrategy() {
return reconnectStrategy;
}
/**
* Sets the {@link ConnectionHandler} that defines the query's behaviour
* when connecting or disconnecting.
* <p>
* The following sample code illustrates how a reconnect strategy and connection handler can be
* used to print a message to the console every time the query connects or disconnects:
* </p>
*
* <pre>
* config.setReconnectStrategy(ReconnectStrategy.exponentialBackoff());
* config.setConnectionHandler(new ConnectionHandler() {
* @Override
* public void onConnect(TS3Api api) {
* System.out.println("Successfully connected!");
* }
*
* @Override
* public void onDisconnect(TS3Query query) {
* System.out.println("The query was disconnected!");
* }
* });
* </pre>
*
* @param connectionHandler
* the {@link ConnectionHandler} object
*
* @return this TS3Config object for chaining
*
* @see TS3Config#setReconnectStrategy(ReconnectStrategy)
*/
public TS3Config setConnectionHandler(ConnectionHandler connectionHandler) {
checkFrozen();
this.connectionHandler = connectionHandler;
return this;
}
ConnectionHandler getConnectionHandler() {
return connectionHandler;
}
TS3Config freeze() {
frozen = true;
return this;
}
private void checkFrozen() {
if (frozen) {
throw new IllegalStateException("TS3Config cannot be modified after being used to create a TS3Query. " +
"Please make any changes to TS3Config *before* calling TS3Query's constructor.");
}
}
}