Skip to content

Commit a8d89fb

Browse files
committed
Add support for banning clients by their myTeamSpeak ID
1 parent b8ef6f1 commit a8d89fb

File tree

3 files changed

+96
-21
lines changed

3 files changed

+96
-21
lines changed

src/main/java/com/github/theholywaffle/teamspeak3/TS3Api.java

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,27 @@ public TS3Api(TS3ApiAsync asyncApi) {
8282
}
8383

8484
/**
85-
* Adds a new ban entry. At least one of the parameters {@code ip}, {@code name} or {@code uid} needs to be not null.
86-
* Returns the ID of the newly created ban.
85+
* Adds a new ban entry. At least one of the parameters {@code ip}, {@code name} or {@code uid} needs to be non-null.
86+
* Returns the ID of the newly created ban entry.
8787
*
8888
* @param ip
89-
* a RegEx pattern to match a client's IP against, can be null
89+
* a RegEx pattern to match a client's IP against, can be {@code null}
9090
* @param name
91-
* a RegEx pattern to match a client's name against, can be null
91+
* a RegEx pattern to match a client's name against, can be {@code null}
9292
* @param uid
93-
* the unique identifier of a client, can be null
93+
* the unique identifier of a client, can be {@code null}
9494
* @param timeInSeconds
9595
* the duration of the ban in seconds. 0 equals a permanent ban
9696
* @param reason
97-
* the reason for the ban, can be null
97+
* the reason for the ban, can be {@code null}
9898
*
9999
* @return the ID of the newly created ban entry
100100
*
101101
* @throws TS3CommandFailedException
102102
* if the execution of a command fails
103103
* @querycommands 1
104104
* @see Pattern RegEx Pattern
105+
* @see #addBan(String, String, String, String, long, String)
105106
* @see Client#getId()
106107
* @see Client#getUniqueIdentifier()
107108
* @see ClientInfo#getIp()
@@ -110,6 +111,41 @@ public int addBan(String ip, String name, String uid, long timeInSeconds, String
110111
return asyncApi.addBan(ip, name, uid, timeInSeconds, reason).getUninterruptibly();
111112
}
112113

114+
/**
115+
* Adds a new ban entry. At least one of the parameters {@code ip}, {@code name}, {@code uid}, or
116+
* {@code myTSId} needs to be non-null. Returns the ID of the newly created ban entry.
117+
* <p>
118+
* Note that creating a ban entry for the {@code "empty"} "myTeamSpeak" ID will ban all clients who
119+
* don't have a linked "myTeamSpeak" account.
120+
* </p>
121+
*
122+
* @param ip
123+
* a RegEx pattern to match a client's IP against, can be {@code null}
124+
* @param name
125+
* a RegEx pattern to match a client's name against, can be {@code null}
126+
* @param uid
127+
* the unique identifier of a client, can be {@code null}
128+
* @param myTSId
129+
* the "myTeamSpeak" ID of a client, the string {@code "empty"}, or {@code null}
130+
* @param timeInSeconds
131+
* the duration of the ban in seconds. 0 equals a permanent ban
132+
* @param reason
133+
* the reason for the ban, can be {@code null}
134+
*
135+
* @return the ID of the newly created ban entry
136+
*
137+
* @throws TS3CommandFailedException
138+
* if the execution of a command fails
139+
* @querycommands 1
140+
* @see Pattern RegEx Pattern
141+
* @see Client#getId()
142+
* @see Client#getUniqueIdentifier()
143+
* @see ClientInfo#getIp()
144+
*/
145+
public int addBan(String ip, String name, String uid, String myTSId, long timeInSeconds, String reason) {
146+
return asyncApi.addBan(ip, name, uid, myTSId, timeInSeconds, reason).getUninterruptibly();
147+
}
148+
113149
/**
114150
* Adds a specified permission to a client in a specific channel.
115151
*
@@ -463,8 +499,9 @@ public void addTS3Listeners(TS3Listener... listeners) {
463499
/**
464500
* Bans a client with a given client ID for a given time.
465501
* <p>
466-
* Please note that this will create two separate ban rules,
467-
* one for the targeted client's IP address and their unique identifier.
502+
* Please note that this will create 2 or 3 separate ban rules,
503+
* one for the targeted client's IP address, one for their unique identifier,
504+
* and potentially one more for their "myTeamSpeak" ID.
468505
* </p><p>
469506
* <i>Exception:</i> If the banned client connects via a loopback address
470507
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created
@@ -476,7 +513,7 @@ public void addTS3Listeners(TS3Listener... listeners) {
476513
* @param timeInSeconds
477514
* the duration of the ban in seconds. 0 equals a permanent ban
478515
*
479-
* @return an array containing the IDs of the first and the second ban entry
516+
* @return an array containing the IDs of the created ban entries
480517
*
481518
* @throws TS3CommandFailedException
482519
* if the execution of a command fails

src/main/java/com/github/theholywaffle/teamspeak3/TS3ApiAsync.java

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,32 +95,68 @@ public TS3ApiAsync(TS3Query query) {
9595
}
9696

9797
/**
98-
* Adds a new ban entry. At least one of the parameters {@code ip}, {@code name} or {@code uid} needs to be not null.
99-
* Returns the ID of the newly created ban.
98+
* Adds a new ban entry. At least one of the parameters {@code ip}, {@code name} or {@code uid} needs to be non-null.
99+
* Returns the ID of the newly created ban entry.
100100
*
101101
* @param ip
102-
* a RegEx pattern to match a client's IP against, can be null
102+
* a RegEx pattern to match a client's IP against, can be {@code null}
103103
* @param name
104-
* a RegEx pattern to match a client's name against, can be null
104+
* a RegEx pattern to match a client's name against, can be {@code null}
105105
* @param uid
106-
* the unique identifier of a client, can be null
106+
* the unique identifier of a client, can be {@code null}
107107
* @param timeInSeconds
108108
* the duration of the ban in seconds. 0 equals a permanent ban
109109
* @param reason
110-
* the reason for the ban, can be null
110+
* the reason for the ban, can be {@code null}
111111
*
112112
* @return the ID of the newly created ban entry
113113
*
114114
* @throws TS3CommandFailedException
115115
* if the execution of a command fails
116116
* @querycommands 1
117117
* @see Pattern RegEx Pattern
118+
* @see #addBan(String, String, String, String, long, String)
118119
* @see Client#getId()
119120
* @see Client#getUniqueIdentifier()
120121
* @see ClientInfo#getIp()
121122
*/
122123
public CommandFuture<Integer> addBan(String ip, String name, String uid, long timeInSeconds, String reason) {
123-
Command cmd = BanCommands.banAdd(ip, name, uid, timeInSeconds, reason);
124+
return addBan(ip, name, uid, null, timeInSeconds, reason);
125+
}
126+
127+
/**
128+
* Adds a new ban entry. At least one of the parameters {@code ip}, {@code name}, {@code uid}, or
129+
* {@code myTSId} needs to be non-null. Returns the ID of the newly created ban entry.
130+
* <p>
131+
* Note that creating a ban entry for the {@code "empty"} "myTeamSpeak" ID will ban all clients who
132+
* don't have a linked "myTeamSpeak" account.
133+
* </p>
134+
*
135+
* @param ip
136+
* a RegEx pattern to match a client's IP against, can be {@code null}
137+
* @param name
138+
* a RegEx pattern to match a client's name against, can be {@code null}
139+
* @param uid
140+
* the unique identifier of a client, can be {@code null}
141+
* @param myTSId
142+
* the "myTeamSpeak" ID of a client, the string {@code "empty"}, or {@code null}
143+
* @param timeInSeconds
144+
* the duration of the ban in seconds. 0 equals a permanent ban
145+
* @param reason
146+
* the reason for the ban, can be {@code null}
147+
*
148+
* @return the ID of the newly created ban entry
149+
*
150+
* @throws TS3CommandFailedException
151+
* if the execution of a command fails
152+
* @querycommands 1
153+
* @see Pattern RegEx Pattern
154+
* @see Client#getId()
155+
* @see Client#getUniqueIdentifier()
156+
* @see ClientInfo#getIp()
157+
*/
158+
public CommandFuture<Integer> addBan(String ip, String name, String uid, String myTSId, long timeInSeconds, String reason) {
159+
Command cmd = BanCommands.banAdd(ip, name, uid, myTSId, timeInSeconds, reason);
124160
return executeAndReturnIntProperty(cmd, "banid");
125161
}
126162

@@ -504,8 +540,9 @@ public void addTS3Listeners(TS3Listener... listeners) {
504540
/**
505541
* Bans a client with a given client ID for a given time.
506542
* <p>
507-
* Please note that this will create two separate ban rules,
508-
* one for the targeted client's IP address and their unique identifier.
543+
* Please note that this will create 2 or 3 separate ban rules,
544+
* one for the targeted client's IP address, one for their unique identifier,
545+
* and potentially one more for their "myTeamSpeak" ID.
509546
* </p><p>
510547
* <i>Exception:</i> If the banned client connects via a loopback address
511548
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created
@@ -517,7 +554,7 @@ public void addTS3Listeners(TS3Listener... listeners) {
517554
* @param timeInSeconds
518555
* the duration of the ban in seconds. 0 equals a permanent ban
519556
*
520-
* @return an array containing the IDs of the first and the second ban entry
557+
* @return an array containing the IDs of the created ban entries
521558
*
522559
* @throws TS3CommandFailedException
523560
* if the execution of a command fails

src/main/java/com/github/theholywaffle/teamspeak3/commands/BanCommands.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ private BanCommands() {
3434
throw new Error("No instances");
3535
}
3636

37-
public static Command banAdd(String ip, String name, String uid, long timeInSeconds, String reason) {
37+
public static Command banAdd(String ip, String name, String uid, String myTSId, long timeInSeconds, String reason) {
3838
if (ip == null && name == null && uid == null) {
3939
throw new IllegalArgumentException("Either IP, name or UId must be non-null");
4040
}
4141

42-
CommandBuilder builder = new CommandBuilder("banadd", 5);
42+
CommandBuilder builder = new CommandBuilder("banadd", 6);
4343
builder.addIf(ip != null, new KeyValueParam("ip", ip));
4444
builder.addIf(name != null, new KeyValueParam("name", name));
4545
builder.addIf(uid != null, new KeyValueParam("uid", uid));
46+
builder.addIf(myTSId != null, new KeyValueParam("mytsid", myTSId));
4647
builder.addIf(timeInSeconds > 0, new KeyValueParam("time", timeInSeconds));
4748
builder.addIf(reason != null, new KeyValueParam("banreason", reason));
4849
return builder.build();

0 commit comments

Comments
 (0)