Skip to content

Commit 35e1da6

Browse files
committed
Add TS3Api#banClients to ban multiple clients at once
1 parent 0a233e0 commit 35e1da6

3 files changed

Lines changed: 114 additions & 32 deletions

File tree

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

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import java.io.InputStream;
3838
import java.io.OutputStream;
39+
import java.util.Collection;
3940
import java.util.List;
4041
import java.util.Map;
4142
import java.util.regex.Pattern;
@@ -499,13 +500,12 @@ public void addTS3Listeners(TS3Listener... listeners) {
499500
/**
500501
* Bans a client with a given client ID for a given time.
501502
* <p>
502-
* Please note that this will create 2 or 3 separate ban rules,
503+
* Please note that this will create up to three separate ban rules,
503504
* one for the targeted client's IP address, one for their unique identifier,
504-
* and potentially one more for their "myTeamSpeak" ID.
505+
* and potentially one more entry for their "myTeamSpeak" ID, if available.
505506
* </p><p>
506507
* <i>Exception:</i> If the banned client connects via a loopback address
507-
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created
508-
* and the returned array will only have 1 entry.
508+
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created.
509509
* </p>
510510
*
511511
* @param clientId
@@ -528,12 +528,12 @@ public int[] banClient(int clientId, long timeInSeconds) {
528528
/**
529529
* Bans a client with a given client ID for a given time for the specified reason.
530530
* <p>
531-
* Please note that this will create two separate ban rules,
532-
* one for the targeted client's IP address and their unique identifier.
531+
* Please note that this will create up to three separate ban rules,
532+
* one for the targeted client's IP address, one for their unique identifier,
533+
* and potentially one more entry for their "myTeamSpeak" ID, if available.
533534
* </p><p>
534535
* <i>Exception:</i> If the banned client connects via a loopback address
535-
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created
536-
* and the returned array will only have 1 entry.
536+
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created.
537537
* </p>
538538
*
539539
* @param clientId
@@ -543,7 +543,7 @@ public int[] banClient(int clientId, long timeInSeconds) {
543543
* @param reason
544544
* the reason for the ban, can be null
545545
*
546-
* @return an array containing the IDs of the first and the second ban entry
546+
* @return an array containing the IDs of the created ban entries
547547
*
548548
* @throws TS3CommandFailedException
549549
* if the execution of a command fails
@@ -558,20 +558,20 @@ public int[] banClient(int clientId, long timeInSeconds, String reason) {
558558
/**
559559
* Bans a client with a given client ID permanently for the specified reason.
560560
* <p>
561-
* Please note that this will create two separate ban rules,
562-
* one for the targeted client's IP address and their unique identifier.
561+
* Please note that this will create up to three separate ban rules,
562+
* one for the targeted client's IP address, one for their unique identifier,
563+
* and potentially one more entry for their "myTeamSpeak" ID, if available.
563564
* </p><p>
564565
* <i>Exception:</i> If the banned client connects via a loopback address
565-
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created
566-
* and the returned array will only have 1 entry.
566+
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created.
567567
* </p>
568568
*
569569
* @param clientId
570570
* the ID of the client
571571
* @param reason
572572
* the reason for the ban, can be null
573573
*
574-
* @return an array containing the IDs of the first and the second ban entry
574+
* @return an array containing the IDs of the created ban entries
575575
*
576576
* @throws TS3CommandFailedException
577577
* if the execution of a command fails
@@ -583,6 +583,41 @@ public int[] banClient(int clientId, String reason) {
583583
return asyncApi.banClient(clientId, reason).getUninterruptibly();
584584
}
585585

586+
/**
587+
* Bans multiple clients by their client ID for a given time for the specified reason.
588+
* <p>
589+
* Please note that this will create up to three separate ban rules for each client,
590+
* one for the targeted client's IP address, one for their unique identifier,
591+
* and potentially one more entry for their "myTeamSpeak" ID, if available.
592+
* </p><p>
593+
* <i>Exception:</i> If the banned client connects via a loopback address
594+
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created.
595+
* </p><p>
596+
* <i>Exception:</i> If two or more clients are connecting from the
597+
* same IP address, only one IP ban entry for that IP will be created.
598+
* </p>
599+
*
600+
* @param clientIds
601+
* the IDs of the clients to be banned
602+
* @param timeInSeconds
603+
* the duration of the ban in seconds. 0 equals a permanent ban
604+
* @param reason
605+
* the reason for the ban, can be null
606+
* @param continueOnError
607+
* if true, continue to the next client if banning one client fails, else do not create any bans on error
608+
*
609+
* @return an array containing the IDs of the created ban entries
610+
*
611+
* @throws TS3CommandFailedException
612+
* if the execution of a command fails
613+
* @querycommands 1
614+
* @see Client#getId()
615+
* @see #addBan(String, String, String, long, String)
616+
*/
617+
public int[] banClients(Collection<Integer> clientIds, long timeInSeconds, String reason, boolean continueOnError) {
618+
return asyncApi.banClients(clientIds, timeInSeconds, reason, continueOnError).getUninterruptibly();
619+
}
620+
586621
/**
587622
* Sends a text message to all clients on all virtual servers.
588623
* These messages will appear to clients in the tab for server messages.

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

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,12 @@ public void addTS3Listeners(TS3Listener... listeners) {
548548
/**
549549
* Bans a client with a given client ID for a given time.
550550
* <p>
551-
* Please note that this will create 2 or 3 separate ban rules,
551+
* Please note that this will create up to three separate ban rules,
552552
* one for the targeted client's IP address, one for their unique identifier,
553-
* and potentially one more for their "myTeamSpeak" ID.
553+
* and potentially one more entry for their "myTeamSpeak" ID, if available.
554554
* </p><p>
555555
* <i>Exception:</i> If the banned client connects via a loopback address
556-
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created
557-
* and the returned array will only have 1 entry.
556+
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created.
558557
* </p>
559558
*
560559
* @param clientId
@@ -577,12 +576,12 @@ public CommandFuture<int[]> banClient(int clientId, long timeInSeconds) {
577576
/**
578577
* Bans a client with a given client ID for a given time for the specified reason.
579578
* <p>
580-
* Please note that this will create two separate ban rules,
581-
* one for the targeted client's IP address and their unique identifier.
579+
* Please note that this will create up to three separate ban rules,
580+
* one for the targeted client's IP address, one for their unique identifier,
581+
* and potentially one more entry for their "myTeamSpeak" ID, if available.
582582
* </p><p>
583583
* <i>Exception:</i> If the banned client connects via a loopback address
584-
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created
585-
* and the returned array will only have 1 entry.
584+
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created.
586585
* </p>
587586
*
588587
* @param clientId
@@ -592,7 +591,7 @@ public CommandFuture<int[]> banClient(int clientId, long timeInSeconds) {
592591
* @param reason
593592
* the reason for the ban, can be null
594593
*
595-
* @return an array containing the IDs of the first and the second ban entry
594+
* @return an array containing the IDs of the created ban entries
596595
*
597596
* @throws TS3CommandFailedException
598597
* if the execution of a command fails
@@ -601,27 +600,27 @@ public CommandFuture<int[]> banClient(int clientId, long timeInSeconds) {
601600
* @see #addBan(String, String, String, long, String)
602601
*/
603602
public CommandFuture<int[]> banClient(int clientId, long timeInSeconds, String reason) {
604-
Command cmd = BanCommands.banClient(clientId, timeInSeconds, reason);
603+
Command cmd = BanCommands.banClient(Collections.singletonList(clientId), timeInSeconds, reason, false);
605604
return executeAndReturnIntArray(cmd, "banid");
606605
}
607606

608607
/**
609608
* Bans a client with a given client ID permanently for the specified reason.
610609
* <p>
611-
* Please note that this will create two separate ban rules,
612-
* one for the targeted client's IP address and their unique identifier.
610+
* Please note that this will create up to three separate ban rules,
611+
* one for the targeted client's IP address, one for their unique identifier,
612+
* and potentially one more entry for their "myTeamSpeak" ID, if available.
613613
* </p><p>
614614
* <i>Exception:</i> If the banned client connects via a loopback address
615-
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created
616-
* and the returned array will only have 1 entry.
615+
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created.
617616
* </p>
618617
*
619618
* @param clientId
620619
* the ID of the client
621620
* @param reason
622621
* the reason for the ban, can be null
623622
*
624-
* @return an array containing the IDs of the first and the second ban entry
623+
* @return an array containing the IDs of the created ban entries
625624
*
626625
* @throws TS3CommandFailedException
627626
* if the execution of a command fails
@@ -633,6 +632,42 @@ public CommandFuture<int[]> banClient(int clientId, String reason) {
633632
return banClient(clientId, 0, reason);
634633
}
635634

635+
/**
636+
* Bans multiple clients by their client ID for a given time for the specified reason.
637+
* <p>
638+
* Please note that this will create up to three separate ban rules for each client,
639+
* one for the targeted client's IP address, one for their unique identifier,
640+
* and potentially one more entry for their "myTeamSpeak" ID, if available.
641+
* </p><p>
642+
* <i>Exception:</i> If the banned client connects via a loopback address
643+
* (i.e. {@code 127.0.0.1} or {@code localhost}), no IP ban is created.
644+
* </p><p>
645+
* <i>Exception:</i> If two or more clients are connecting from the
646+
* same IP address, only one IP ban entry for that IP will be created.
647+
* </p>
648+
*
649+
* @param clientIds
650+
* the IDs of the clients to be banned
651+
* @param timeInSeconds
652+
* the duration of the ban in seconds. 0 equals a permanent ban
653+
* @param reason
654+
* the reason for the ban, can be null
655+
* @param continueOnError
656+
* if true, continue to the next client if banning one client fails, else do not create any bans on error
657+
*
658+
* @return an array containing the IDs of the created ban entries
659+
*
660+
* @throws TS3CommandFailedException
661+
* if the execution of a command fails
662+
* @querycommands 1
663+
* @see Client#getId()
664+
* @see #addBan(String, String, String, long, String)
665+
*/
666+
public CommandFuture<int[]> banClients(Collection<Integer> clientIds, long timeInSeconds, String reason, boolean continueOnError) {
667+
Command cmd = BanCommands.banClient(clientIds, timeInSeconds, reason, continueOnError);
668+
return executeAndReturnIntArray(cmd, "banid");
669+
}
670+
636671
/**
637672
* Sends a text message to all clients on all virtual servers.
638673
* These messages will appear to clients in the tab for server messages.

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
* #L%
2727
*/
2828

29+
import com.github.theholywaffle.teamspeak3.commands.parameter.ArrayParameter;
2930
import com.github.theholywaffle.teamspeak3.commands.parameter.KeyValueParam;
31+
import com.github.theholywaffle.teamspeak3.commands.parameter.OptionParam;
32+
33+
import java.util.Collection;
3034

3135
public final class BanCommands {
3236

@@ -49,11 +53,19 @@ public static Command banAdd(String ip, String name, String uid, String myTSId,
4953
return builder.build();
5054
}
5155

52-
public static Command banClient(int clientId, long timeInSeconds, String reason) {
53-
CommandBuilder builder = new CommandBuilder("banclient", 3);
54-
builder.add(new KeyValueParam("clid", clientId));
56+
public static Command banClient(Collection<Integer> clientIds, long timeInSeconds,
57+
String reason, boolean continueOnError) {
58+
CommandBuilder builder = new CommandBuilder("banclient", 4);
5559
builder.addIf(timeInSeconds > 0, new KeyValueParam("time", timeInSeconds));
5660
builder.addIf(reason != null, new KeyValueParam("banreason", reason));
61+
builder.addIf(continueOnError, new OptionParam("continueonerror"));
62+
63+
ArrayParameter clients = new ArrayParameter(clientIds.size());
64+
for (int clientId : clientIds) {
65+
clients.add(new KeyValueParam("clid", clientId));
66+
}
67+
builder.add(clients);
68+
5769
return builder.build();
5870
}
5971

0 commit comments

Comments
 (0)