From 1ed4e0c4dabf1ff7bf16ad1d44a8dd42a33c27f7 Mon Sep 17 00:00:00 2001 From: Manevolent Date: Sat, 4 Aug 2018 19:22:08 -0400 Subject: [PATCH] Channel list event is back, adding chat to client commands. --- .../ts3j/event/ChannelListEvent.java | 47 +++++++++++++ .../manevolent/ts3j/event/TS3Listener.java | 3 +- .../manevolent/ts3j/identity/Identity.java | 3 +- .../client/LocalTeamspeakClientSocket.java | 66 +++++++++++++++++-- .../manevolent/ts3j/GenerateIdentityTest.java | 23 +++++++ .../manevolent/ts3j/ServerConnectionTest.java | 12 ---- 6 files changed, 136 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/github/manevolent/ts3j/event/ChannelListEvent.java create mode 100644 src/test/java/com/github/manevolent/ts3j/GenerateIdentityTest.java diff --git a/src/main/java/com/github/manevolent/ts3j/event/ChannelListEvent.java b/src/main/java/com/github/manevolent/ts3j/event/ChannelListEvent.java new file mode 100644 index 0000000..c6f7bbc --- /dev/null +++ b/src/main/java/com/github/manevolent/ts3j/event/ChannelListEvent.java @@ -0,0 +1,47 @@ +package com.github.manevolent.ts3j.event; + +/* + * #%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.manevolent.ts3j.api.ChannelProperty; + +import java.util.Map; + +public class ChannelListEvent extends BaseEvent { + + public ChannelListEvent(Map map) { + super(map); + } + + public int getChannelId() { + return getInt(ChannelProperty.CID); + } + + @Override + public void fire(TS3Listener listener) { + listener.onChannelList(this); + } +} diff --git a/src/main/java/com/github/manevolent/ts3j/event/TS3Listener.java b/src/main/java/com/github/manevolent/ts3j/event/TS3Listener.java index 9f73d6e..27f7d8e 100644 --- a/src/main/java/com/github/manevolent/ts3j/event/TS3Listener.java +++ b/src/main/java/com/github/manevolent/ts3j/event/TS3Listener.java @@ -50,6 +50,8 @@ public interface TS3Listener { void onChannelPasswordChanged(ChannelPasswordChangedEvent e); + void onChannelList(ChannelListEvent e); + void onPrivilegeKeyUsed(PrivilegeKeyUsedEvent e); void onChannelGroupList(ChannelGroupListEvent e); @@ -61,5 +63,4 @@ public interface TS3Listener { void onClientChannelGroupChanged(ClientChannelGroupChangedEvent e); void onClientChanged(ClientUpdatedEvent e); - } diff --git a/src/main/java/com/github/manevolent/ts3j/identity/Identity.java b/src/main/java/com/github/manevolent/ts3j/identity/Identity.java index eca8218..d14913a 100644 --- a/src/main/java/com/github/manevolent/ts3j/identity/Identity.java +++ b/src/main/java/com/github/manevolent/ts3j/identity/Identity.java @@ -76,7 +76,8 @@ public class Identity { if (curr > best) { - Ts3Debugging.debug("Improved identity security level: from " + best + " to " + curr); + Ts3Debugging.debug("Improved identity security level: from " + best + " to " + curr + + " (" + keyOffset + ")"); keyOffset = lastCheckedKeyOffset; best = curr; } 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 40758aa..b8179b3 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 @@ -9,6 +9,7 @@ import com.github.manevolent.ts3j.command.*; import com.github.manevolent.ts3j.command.parameter.CommandSingleParameter; import com.github.manevolent.ts3j.command.response.AbstractCommandResponse; import com.github.manevolent.ts3j.command.response.CommandResponse; +import com.github.manevolent.ts3j.event.ChannelListEvent; import com.github.manevolent.ts3j.event.TS3Event; import com.github.manevolent.ts3j.event.TS3Listener; import com.github.manevolent.ts3j.identity.LocalIdentity; @@ -65,7 +66,14 @@ public class LocalTeamspeakClientSocket super(); namedProcessors.put("initserver", new InitServerHandler()); - namedProcessors.put("channellist", new NullRouteHandler()); + namedProcessors.put("channellist", new CommandProcessor() { + @Override + public void process(AbstractTeamspeakClientSocket client, SingleCommand singleCommand) + throws CommandProcessException { + ChannelListEvent channelListEvent = new ChannelListEvent(singleCommand.toMap()); + for (TS3Listener listener : listeners) listener.onChannelList(channelListEvent); + } + }); namedProcessors.put("channellistfinished", new ChannelListFinishedHandler()); } @@ -413,7 +421,15 @@ public class LocalTeamspeakClientSocket } private class ChannelListFinishedHandler implements CommandProcessor { - + @Override + public void process(AbstractTeamspeakClientSocket client, SingleCommand singleCommand) + throws CommandProcessException { + try { + setState(ClientConnectionState.CONNECTED); + } catch (Exception e) { + throw new CommandProcessException(e); + } + } } private class NullRouteHandler implements CommandProcessor { @@ -430,8 +446,6 @@ public class LocalTeamspeakClientSocket throws CommandProcessException { try { setClientId(Integer.parseInt(singleCommand.get("aclid").getValue())); - - setState(ClientConnectionState.CONNECTED); } catch (Exception e) { throw new CommandProcessException(e); } @@ -597,6 +611,50 @@ public class LocalTeamspeakClientSocket return executeCommand(command, x -> new Client(x.toMap())).get(); } + /** + * Sends a text message a specified target. The type of the target is determined by targetmode while target + specifies the ID of the recipient, whether it be a virtual server, a channel or a client. + */ + public void sendServerMessage(String message) + throws IOException, TimeoutException, ExecutionException, InterruptedException { + Command command = new SingleCommand("sendtextmessage", ProtocolRole.CLIENT); + + command.add(new CommandSingleParameter("targetmode", Integer.toString(1))); + command.add(new CommandSingleParameter("msg", message)); + + executeCommand(command).complete(); + } + + /** + * Sends a text message a specified target. The type of the target is determined by targetmode while target + specifies the ID of the recipient, whether it be a virtual server, a channel or a client. + */ + public void sendChannelMessage(int channelId, String message) + throws IOException, TimeoutException, ExecutionException, InterruptedException { + Command command = new SingleCommand("sendtextmessage", ProtocolRole.CLIENT); + + command.add(new CommandSingleParameter("targetmode", Integer.toString(2))); + command.add(new CommandSingleParameter("target", Integer.toString(channelId))); + command.add(new CommandSingleParameter("msg", message)); + + executeCommand(command).complete(); + } + + /** + * Sends a text message a specified target. The type of the target is determined by targetmode while target + specifies the ID of the recipient, whether it be a virtual server, a channel or a client. + */ + public void sendPrivateMessage(int clientId, String message) + throws IOException, TimeoutException, ExecutionException, InterruptedException { + Command command = new SingleCommand("sendtextmessage", ProtocolRole.CLIENT); + + command.add(new CommandSingleParameter("targetmode", Integer.toString(3))); + command.add(new CommandSingleParameter("target", Integer.toString(clientId))); + command.add(new CommandSingleParameter("msg", message)); + + executeCommand(command).complete(); + } + /** * Moves one or more clients specified with clid to the channel with ID cid. If the target channel has a password, it needs to be specified with cpw. If the diff --git a/src/test/java/com/github/manevolent/ts3j/GenerateIdentityTest.java b/src/test/java/com/github/manevolent/ts3j/GenerateIdentityTest.java new file mode 100644 index 0000000..8abc784 --- /dev/null +++ b/src/test/java/com/github/manevolent/ts3j/GenerateIdentityTest.java @@ -0,0 +1,23 @@ +package com.github.manevolent.ts3j; + +import com.github.manevolent.ts3j.identity.LocalIdentity; +import com.github.manevolent.ts3j.util.Ts3Debugging; +import java.util.Base64; + +public class GenerateIdentityTest { + public static void main(String[] args) throws Exception { + try { + Ts3Debugging.setEnabled(true); + + LocalIdentity identity = LocalIdentity.generateNew(0); + + Ts3Debugging.debug("Private key: " + + Base64.getEncoder().encodeToString(identity.getPrivateKey().toByteArray()) + ); + + identity.improveSecurity(1000); + } catch (Throwable ex) { + ex.printStackTrace(); + } + } +} diff --git a/src/test/java/com/github/manevolent/ts3j/ServerConnectionTest.java b/src/test/java/com/github/manevolent/ts3j/ServerConnectionTest.java index c01bb6d..85d4e00 100644 --- a/src/test/java/com/github/manevolent/ts3j/ServerConnectionTest.java +++ b/src/test/java/com/github/manevolent/ts3j/ServerConnectionTest.java @@ -1,26 +1,14 @@ package com.github.manevolent.ts3j; -import com.github.manevolent.ts3j.api.Ban; import com.github.manevolent.ts3j.api.Channel; -import com.github.manevolent.ts3j.api.Client; -import com.github.manevolent.ts3j.audio.Microphone; -import com.github.manevolent.ts3j.command.SingleCommand; -import com.github.manevolent.ts3j.enums.CodecType; import com.github.manevolent.ts3j.identity.LocalIdentity; -import com.github.manevolent.ts3j.identity.Uid; -import com.github.manevolent.ts3j.protocol.ProtocolRole; import com.github.manevolent.ts3j.protocol.client.ClientConnectionState; import com.github.manevolent.ts3j.protocol.socket.client.LocalTeamspeakClientSocket; -import com.github.manevolent.ts3j.util.Ts3Crypt; import com.github.manevolent.ts3j.util.Ts3Debugging; -import junit.framework.TestCase; import java.math.BigInteger; import java.net.InetSocketAddress; import java.util.Base64; -import java.util.Collections; -import java.util.Random; -import java.util.stream.Collectors; import static org.junit.Assert.assertEquals;