diff --git a/src/main/java/com/github/manevolent/ts3j/api/Permission.java b/src/main/java/com/github/manevolent/ts3j/api/Permission.java index 7f79a4b..e726cb3 100644 --- a/src/main/java/com/github/manevolent/ts3j/api/Permission.java +++ b/src/main/java/com/github/manevolent/ts3j/api/Permission.java @@ -26,6 +26,8 @@ package com.github.manevolent.ts3j.api; * #L% */ +import java.util.Collections; +import java.util.LinkedHashMap; import java.util.Map; /** @@ -43,6 +45,14 @@ public class Permission extends Wrapper { super(map); } + public Permission(String id, int value, boolean negated) { + super(createPermissionMap(id, value, negated)); + } + + public Permission(String id, int value) { + this(id, value, false); + } + /** * Gets the name of this permission. *

@@ -107,4 +117,11 @@ public class Permission extends Wrapper { return getBoolean("permskip"); } + private static Map createPermissionMap(String id, int value, boolean negated) { + Map map = new LinkedHashMap<>(); + map.put("permsid", id); + map.put("permvalue", Integer.toString(value)); + map.put("permnegated", Integer.toString(negated ? 1 : 0)); + return Collections.unmodifiableMap(map); + } } diff --git a/src/main/java/com/github/manevolent/ts3j/protocol/socket/client/LocalTeamspeakClientSocket.java b/src/main/java/com/github/manevolent/ts3j/protocol/socket/client/LocalTeamspeakClientSocket.java index 6b48bc2..c6a0dac 100644 --- a/src/main/java/com/github/manevolent/ts3j/protocol/socket/client/LocalTeamspeakClientSocket.java +++ b/src/main/java/com/github/manevolent/ts3j/protocol/socket/client/LocalTeamspeakClientSocket.java @@ -6,6 +6,7 @@ import com.github.manevolent.ts3j.api.Client; import com.github.manevolent.ts3j.api.Permission; import com.github.manevolent.ts3j.audio.Microphone; import com.github.manevolent.ts3j.command.*; +import com.github.manevolent.ts3j.command.parameter.CommandOption; import com.github.manevolent.ts3j.command.parameter.CommandSingleParameter; import com.github.manevolent.ts3j.command.response.AbstractCommandResponse; import com.github.manevolent.ts3j.command.response.CommandResponse; @@ -454,7 +455,14 @@ public class LocalTeamspeakClientSocket ); } - private CommandResponse> executeCommand(Command command) + /** + * Executes a command. + * @param command Command to execute. + * @return Future, command response object. + * @throws IOException if there is a connectivity-level exception. + * @throws TimeoutException if command send times out. + */ + public CommandResponse> executeCommand(Command command) throws IOException, TimeoutException { return executeCommand(command, Function.identity()); } @@ -898,6 +906,58 @@ public class LocalTeamspeakClientSocket deleteBan(ban.getId()); } + /* + public void clientListPermission(int clientDatabaseId) + throws IOException, TimeoutException, ExecutionException, InterruptedException { + MultiCommand command = new MultiCommand( + "clientpermlist", + ProtocolRole.CLIENT, + new SingleCommand("clientlistperm", ProtocolRole.CLIENT) + ); + + command.add(new CommandSingleParameter("cldbid", Integer.toString(clientDatabaseId))); + command.add(new CommandOption("permsid")); + + executeCommand(command).complete(); + } + */ + + public void clientAddPermission(int clientDatabaseId, Permission... permission) + throws IOException, TimeoutException, ExecutionException, InterruptedException { + MultiCommand command = new MultiCommand( + "clientaddperm", + ProtocolRole.CLIENT, + Arrays.stream(permission).map(x -> + new SingleCommand("clientaddperm", ProtocolRole.CLIENT, // name is redundant here + new CommandSingleParameter("permsid", x.getName()), + new CommandSingleParameter("permvalue", Integer.toString(x.getValue())), + new CommandSingleParameter("permskip", Integer.toString(x.isNegated() ? 1 : 0)) + )).collect(Collectors.toList() + )); + + command.add(new CommandSingleParameter("cldbid", Integer.toString(clientDatabaseId))); + + executeCommand(command).complete(); + } + + public void clientDeletePermission(int clientDatabaseId, Permission... permission) + throws IOException, TimeoutException, ExecutionException, InterruptedException { + MultiCommand command = new MultiCommand( + "clientdelperm", + ProtocolRole.CLIENT, + Arrays.stream(permission).map(x -> + new SingleCommand("clientdelperm", ProtocolRole.CLIENT, // name is redundant here + new CommandSingleParameter("permsid", x.getName()), + new CommandSingleParameter("permvalue", Integer.toString(x.getValue())), + new CommandSingleParameter("permskip", Integer.toString(x.isNegated() ? 1 : 0)) + )).collect(Collectors.toList() + )); + + command.add(new CommandSingleParameter("cldbid", Integer.toString(clientDatabaseId))); + + executeCommand(command).complete(); + } + public void channelAddPermission(int channelId, Permission... permission) throws IOException, TimeoutException, ExecutionException, InterruptedException { MultiCommand command = new MultiCommand( @@ -906,7 +966,8 @@ public class LocalTeamspeakClientSocket Arrays.stream(permission).map(x -> new SingleCommand("channeladdperm", ProtocolRole.CLIENT, // name is redundant here new CommandSingleParameter("permsid", x.getName()), - new CommandSingleParameter("permvalue", Integer.toString(x.getValue())) + new CommandSingleParameter("permvalue", Integer.toString(x.getValue())), + new CommandSingleParameter("permskip", Integer.toString(x.isNegated() ? 1 : 0)) )).collect(Collectors.toList() )); @@ -923,7 +984,8 @@ public class LocalTeamspeakClientSocket Arrays.stream(permission).map(x -> new SingleCommand("channelclientaddperm", ProtocolRole.CLIENT, // name is redundant here new CommandSingleParameter("permsid", x.getName()), - new CommandSingleParameter("permvalue", Integer.toString(x.getValue())) + new CommandSingleParameter("permvalue", Integer.toString(x.getValue())), + new CommandSingleParameter("permskip", Integer.toString(x.isNegated() ? 1 : 0)) )).collect(Collectors.toList() )); @@ -941,7 +1003,8 @@ public class LocalTeamspeakClientSocket Arrays.stream(permission).map(x -> new SingleCommand("channeldelperm", ProtocolRole.CLIENT, // name is redundant here new CommandSingleParameter("permsid", x.getName()), - new CommandSingleParameter("permvalue", Integer.toString(x.getValue())) + new CommandSingleParameter("permvalue", Integer.toString(x.getValue())), + new CommandSingleParameter("permnegated", Integer.toString(x.isNegated() ? 1 : 0)) )).collect(Collectors.toList() )); diff --git a/src/test/java/com/github/manevolent/ts3j/ServerConnectionTest.java b/src/test/java/com/github/manevolent/ts3j/ServerConnectionTest.java index d00d377..44e3f13 100644 --- a/src/test/java/com/github/manevolent/ts3j/ServerConnectionTest.java +++ b/src/test/java/com/github/manevolent/ts3j/ServerConnectionTest.java @@ -1,17 +1,19 @@ package com.github.manevolent.ts3j; -; + import com.github.manevolent.ts3j.identity.LocalIdentity; import com.github.manevolent.ts3j.protocol.client.ClientConnectionState; import com.github.manevolent.ts3j.protocol.socket.client.LocalTeamspeakClientSocket; import com.github.manevolent.ts3j.util.Ts3Debugging; +import java.io.File; + import static org.junit.Assert.assertEquals; public class ServerConnectionTest { public static void main(String[] args) throws Exception { Ts3Debugging.setEnabled(true); - LocalIdentity identity = LocalIdentity.generateNew(10); + LocalIdentity identity = LocalIdentity.read(new File("ident.ini")); LocalTeamspeakClientSocket client = new LocalTeamspeakClientSocket(); client.setIdentity(identity); @@ -23,6 +25,7 @@ public class ServerConnectionTest { "teamlixo.net", 10000L ); + client.getChannels().get().forEach(x -> System.err.println(x.getTopic())); assertEquals(client.getState(), ClientConnectionState.CONNECTED);