myTeamSpeak: import TeamSpeak 6 contacts; bookmark myTeamSpeak switch

TeamSpeak 6 syncs chat contacts as item class 8 (Item_Data field 25). They
are imported as friends keyed by myTeamSpeak ID, taken from the item or from
the partner's chat ID (@<base32 id>:<homeserver>), the same bytes servers
report as base64 client_myteamspeak_id. The contact list matches them to
clients through the ID each client carries.

Bookmarks gain TeamSpeak's "Enable myTeamSpeak features" switch
(send_mytsid_on_server), stored and imported but not yet sent; what is
still missing is noted in docs/myteamspeak/myteamspeak_server_signin.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-10 17:56:29 +00:00
co-authored by Claude Opus 5.5
parent f78e4fcd66
commit ca86c510b7
31 changed files with 594 additions and 128 deletions
@@ -24,6 +24,8 @@ public final class Bookmark {
public String channelPassword = "";
/** Connect to this server automatically when the client starts. */
public boolean connectOnStartup = false;
/** TeamSpeak's "Enable myTeamSpeak features" ({@code Bookmark_Data.send_mytsid_on_server}). */
public boolean myTeamSpeakFeatures = true;
/** The server's icon as last seen there, 0 for none or not yet known. */
public long iconId = 0;
@@ -46,6 +48,7 @@ public final class Bookmark {
b.channel = channel;
b.channelPassword = channelPassword;
b.connectOnStartup = connectOnStartup;
b.myTeamSpeakFeatures = myTeamSpeakFeatures;
b.iconId = iconId;
return b;
}
@@ -88,6 +88,7 @@ public final class Bookmarks {
bm.channel = p.getProperty(prefix + "channel", "");
bm.channelPassword = p.getProperty(prefix + "channelPassword", "");
bm.connectOnStartup = Boolean.parseBoolean(p.getProperty(prefix + "connectOnStartup", "false"));
bm.myTeamSpeakFeatures = Boolean.parseBoolean(p.getProperty(prefix + "myTeamSpeakFeatures", "true"));
bm.iconId = parseLong(p.getProperty(prefix + "iconId"));
if (bm.address != null && !bm.address.isBlank()) b.entries.add(bm);
}
@@ -110,6 +111,7 @@ public final class Bookmarks {
p.setProperty(prefix + "channel", nullToEmpty(bm.channel));
p.setProperty(prefix + "channelPassword", nullToEmpty(bm.channelPassword));
p.setProperty(prefix + "connectOnStartup", Boolean.toString(bm.connectOnStartup));
p.setProperty(prefix + "myTeamSpeakFeatures", Boolean.toString(bm.myTeamSpeakFeatures));
p.setProperty(prefix + "iconId", Long.toString(bm.iconId));
}
try {
@@ -8,19 +8,27 @@ import java.util.Locale;
import java.util.Map;
/**
* One entry of the contact list, keyed by the client's unique id.
* One entry of the contact list, keyed by the client's unique id — or, for a contact
* brought over from TeamSpeak 6, by a myTeamSpeak ID, which is the person rather
* than one of their identities.
*
* <p>The fields are exactly the ones the TeamSpeak 3 client keeps for a contact,
* and {@link #toRecord()} / {@link #parseRecord} use its own {@code Key=Value}
* serialisation (the {@code value} column of its {@code Contacts} table), so
* records can be exchanged with it as they are.
* records can be exchanged with it as they are. The myTeamSpeak ID is an extra
* {@code MyTsId} line, written only when set.
*/
public final class Contact {
/** TeamSpeak writes {@code LastSeen} as an ISO local date-time without zone. */
private static final DateTimeFormatter LAST_SEEN = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
/** Base64 never holds a colon, so these keys cannot pass for a unique id. */
private static final String MYTS_KEY_PREFIX = "myts:";
/** Empty for a contact known only by its {@link #myTeamSpeakId}. */
public final String uniqueId;
/** The {@code client_myteamspeak_id} the contact's clients carry, or empty. */
public final String myTeamSpeakId;
/** Custom name; TeamSpeak seeds it with the nickname the client had when added. */
public String nickname = "";
public String phoneticNickname = "";
@@ -42,7 +50,27 @@ public final class Contact {
public String lastSeenServerAddress = "";
public Contact(String uniqueId) {
this(uniqueId, "");
}
public Contact(String uniqueId, String myTeamSpeakId) {
this.uniqueId = uniqueId == null ? "" : uniqueId;
this.myTeamSpeakId = myTeamSpeakId == null ? "" : myTeamSpeakId;
}
/** What the contact list files the entry under: the unique id, else the myTeamSpeak ID. */
public String key() {
return uniqueId.isEmpty() ? MYTS_KEY_PREFIX + myTeamSpeakId : uniqueId;
}
/** The id shown for the contact: its unique id, else its myTeamSpeak ID. */
public String shownId() {
return uniqueId.isEmpty() ? myTeamSpeakId : uniqueId;
}
/** What the contact list calls the contact: its custom name, else its id. */
public String label() {
return nickname.isBlank() ? shownId() : nickname;
}
/** The name the contact is shown under, given the nickname it currently carries. */
@@ -56,12 +84,12 @@ public final class Contact {
}
public Contact copy() {
Contact c = new Contact(uniqueId);
Contact c = new Contact(uniqueId, myTeamSpeakId);
c.copyFrom(this);
return c;
}
/** Takes over every field but the unique id. */
/** Takes over every field but the ids. */
public void copyFrom(Contact other) {
nickname = other.nickname;
phoneticNickname = other.phoneticNickname;
@@ -115,10 +143,11 @@ public final class Contact {
line(sb, "LastSeen", lastSeen == null ? "" : LAST_SEEN.format(lastSeen));
line(sb, "LastSeenServerName", lastSeenServerName);
line(sb, "LastSeenServerAddress", lastSeenServerAddress);
if (!myTeamSpeakId.isEmpty()) line(sb, "MyTsId", myTeamSpeakId);
return sb.toString();
}
/** Parses a TeamSpeak contact record, or returns {@code null} when it carries no unique id. */
/** Parses a TeamSpeak contact record, or returns {@code null} when it carries no id at all. */
public static Contact parseRecord(String record) {
if (record == null) return null;
Map<String, String> fields = new LinkedHashMap<>();
@@ -127,9 +156,10 @@ public final class Contact {
if (eq > 0) fields.put(line.substring(0, eq).trim(), line.substring(eq + 1));
}
String uid = fields.getOrDefault("IDS", "").trim();
if (uid.isEmpty()) return null;
String myTsId = fields.getOrDefault("MyTsId", "").trim();
if (uid.isEmpty() && myTsId.isEmpty()) return null;
Contact c = new Contact(uid);
Contact c = new Contact(uid, myTsId);
c.nickname = fields.getOrDefault("Nickname", "");
c.type = ContactType.fromCode(parseInt(fields.get("Friend"), ContactType.NEUTRAL.code()));
c.automute = parseFlag(fields.get("Automute"));
@@ -15,6 +15,7 @@ import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@@ -31,6 +32,10 @@ import java.util.concurrent.atomic.AtomicBoolean;
* between the two verbatim. On first run the list is seeded from an installed
* TeamSpeak client's database, when one is found.
*
* <p>A contact known by its myTeamSpeak ID alone stands for every client carrying
* that ID, once the connection {@linkplain #link links} the client to it; lookups by
* unique id then find it like any other.
*
* <p>Safe to use from any thread: lookups are lock-free on a snapshot, and
* mutations are serialised and announced to listeners on the calling thread.
*/
@@ -49,7 +54,9 @@ public final class ContactStore {
}
private final File file;
private final Map<String, Contact> byUid = new LinkedHashMap<>();
private final Map<String, Contact> byKey = new LinkedHashMap<>();
/** Unique id → myTeamSpeak ID of the clients seen so far, which only lives as long as the client. */
private final Map<String, String> myTsIds = new ConcurrentHashMap<>();
/** Immutable snapshot for readers, replaced under the lock after every change. */
private volatile List<Contact> snapshot = List.of();
private final List<Listener> listeners = new CopyOnWriteArrayList<>();
@@ -96,9 +103,9 @@ public final class ContactStore {
if (c != null) parsed.add(c);
}
synchronized (this) {
byUid.clear();
for (Contact c : parsed) byUid.put(c.uniqueId, c);
snapshot = List.copyOf(byUid.values());
byKey.clear();
for (Contact c : parsed) byKey.put(c.key(), c);
snapshot = List.copyOf(byKey.values());
}
}
@@ -132,14 +139,33 @@ public final class ContactStore {
return snapshot;
}
/**
* The contact a client is filed under: by its unique id, else by the myTeamSpeak ID it was
* {@linkplain #link linked} with. A {@link Contact#key()} finds its contact too.
*/
public Contact find(String uniqueId) {
if (uniqueId == null || uniqueId.isEmpty()) return null;
for (Contact c : snapshot) {
if (c.uniqueId.equals(uniqueId)) return c;
if (c.uniqueId.equals(uniqueId) || c.key().equals(uniqueId)) return c;
}
return findByMyTeamSpeakId(myTsIds.get(uniqueId));
}
public Contact findByMyTeamSpeakId(String myTeamSpeakId) {
if (myTeamSpeakId == null || myTeamSpeakId.isEmpty()) return null;
for (Contact c : snapshot) {
if (c.myTeamSpeakId.equals(myTeamSpeakId)) return c;
}
return null;
}
/** Notes which myTeamSpeak ID a client carries (empty for none), so {@link #find} can match it. */
public void link(String uniqueId, String myTeamSpeakId) {
if (uniqueId == null || uniqueId.isEmpty()) return;
if (myTeamSpeakId == null || myTeamSpeakId.isEmpty()) myTsIds.remove(uniqueId);
else myTsIds.put(uniqueId, myTeamSpeakId);
}
/** The type a client is filed under, {@code null} when it is not a contact. */
public ContactType typeOf(String uniqueId) {
Contact c = find(uniqueId);
@@ -163,35 +189,38 @@ public final class ContactStore {
public Contact add(String uniqueId, String nickname, ContactType type, ContactDefaults defaults) {
Contact contact;
synchronized (this) {
contact = byUid.get(uniqueId);
contact = find(uniqueId);
if (contact == null) {
contact = new Contact(uniqueId);
contact.nickname = nickname == null ? "" : nickname;
contact.applyDefaults(defaults.of(type));
byUid.put(uniqueId, contact);
byKey.put(contact.key(), contact);
}
contact.type = type;
snapshot = List.copyOf(byUid.values());
snapshot = List.copyOf(byKey.values());
}
changed();
return contact;
}
/** Stores an edited contact, replacing any entry with the same unique id. */
/** Stores an edited contact, replacing any entry with the same {@link Contact#key()}. */
public void put(Contact contact) {
synchronized (this) {
Contact existing = byUid.get(contact.uniqueId);
Contact existing = byKey.get(contact.key());
if (existing != null && existing != contact) existing.copyFrom(contact);
else byUid.put(contact.uniqueId, contact);
snapshot = List.copyOf(byUid.values());
else byKey.put(contact.key(), contact);
snapshot = List.copyOf(byKey.values());
}
changed();
}
/** Removes the contact {@link #find} gives for the id. */
public void remove(String uniqueId) {
synchronized (this) {
if (byUid.remove(uniqueId) == null) return;
snapshot = List.copyOf(byUid.values());
Contact contact = find(uniqueId);
if (contact == null) return;
byKey.remove(contact.key());
snapshot = List.copyOf(byKey.values());
}
changed();
}
@@ -199,9 +228,9 @@ public final class ContactStore {
/** Takes over a whole list, e.g. one read from the TeamSpeak client. */
public void replaceAll(Collection<Contact> contacts) {
synchronized (this) {
byUid.clear();
for (Contact c : contacts) byUid.put(c.uniqueId, c);
snapshot = List.copyOf(byUid.values());
byKey.clear();
for (Contact c : contacts) byKey.put(c.key(), c);
snapshot = List.copyOf(byKey.values());
}
changed();
}
@@ -216,9 +245,9 @@ public final class ContactStore {
int added = 0;
synchronized (this) {
for (Contact c : contacts) {
if (byUid.put(c.uniqueId, c) == null) added++;
if (byKey.put(c.key(), c) == null) added++;
}
snapshot = List.copyOf(byUid.values());
snapshot = List.copyOf(byKey.values());
}
changed();
return added;
@@ -12,27 +12,8 @@ import java.time.Instant;
*/
public record AccountInfo(String username, String userId, String uuid, Instant created) {
private static final String BASE32 = "abcdefghijklmnopqrstuvwxyz234567";
static AccountInfo of(String username, String uuid, byte[] userId, long createdSeconds) {
return new AccountInfo(username, userId == null ? "" : base32(userId), uuid,
return new AccountInfo(username, userId == null ? "" : MyTsId.base32(userId), uuid,
createdSeconds > 0 ? Instant.ofEpochSecond(createdSeconds) : null);
}
/** RFC 4648 base32, lowercase and unpadded. */
static String base32(byte[] data) {
StringBuilder out = new StringBuilder((data.length * 8 + 4) / 5);
int buffer = 0, bits = 0;
for (byte b : data) {
buffer = (buffer << 8) | (b & 0xff);
bits += 8;
while (bits >= 5) {
out.append(BASE32.charAt((buffer >> (bits - 5)) & 31));
bits -= 5;
}
buffer &= (1 << bits) - 1;
}
if (bits > 0) out.append(BASE32.charAt((buffer << (5 - bits)) & 31));
return out.toString();
}
}
@@ -13,7 +13,7 @@ import java.util.ArrayList;
import java.util.List;
/**
* Reads the bookmarks and identities a myTeamSpeak account synchronises, the way
* Reads the bookmarks, identities and contacts a myTeamSpeak account synchronises, the way
* the official client pulls them on a fresh install: sign in, ask for every item
* of the wanted classes against an empty local state, decrypt them, and sign out.
* Nothing is ever written to the account. Staying signed in is {@link MyTeamSpeakLogin}'s.
@@ -41,11 +41,16 @@ public final class MyTeamSpeak {
public long bookmarks() {
return items.stream().filter(SyncItem.Bookmark.class::isInstance).count();
}
public long contacts() {
return items.stream().filter(SyncItem.Contact.class::isInstance).count();
}
}
private static final String NO_VERSION = "00000000-0000-0000-0000-000000000000";
private static final int SYNC_VERSION_1_1 = 1;
private static final int[] ITEM_CLASSES = {0 /* BOOKMARK */, 1 /* IDENTITY */, 6 /* ITEM_FOLDER */};
private static final int CONTACTS = 8;
private static final int[] ITEM_CLASSES = {0 /* BOOKMARK */, 1 /* IDENTITY */, 6 /* ITEM_FOLDER */, CONTACTS};
private static final int LOGIN_OK = 200;
private static final int LOGIN_OFFLINE = 201;
@@ -159,7 +164,7 @@ public final class MyTeamSpeak {
ProtobufReader reply = new ProtobufReader(
transport.call("synchronization", "requestServerItems", request.toByteArray()));
List<byte[]> frames = new ArrayList<>();
List<Frame> frames = new ArrayList<>();
int status = 0;
while (reply.next()) {
switch (reply.fieldNumber()) {
@@ -173,13 +178,8 @@ public final class MyTeamSpeak {
}
List<SyncItem> items = new ArrayList<>();
for (byte[] frame : frames) {
SyncItem item;
try {
item = SyncItemDecoder.decode(MyTsCrypto.decryptItem(frame, itemKey));
} catch (GeneralSecurityException e) {
throw new MyTsException("Could not decrypt the account's items", e);
}
for (Frame frame : frames) {
SyncItem item = decode(frame, itemKey);
if (item != null) items.add(item);
}
return SyncOrder.sorted(items);
@@ -191,7 +191,7 @@ public final class MyTeamSpeak {
* and not all of them are encrypted (add-on flags come in the clear), so only the
* wanted classes are kept.
*/
private static void collectFrames(ProtobufReader itemClass, List<byte[]> frames) throws IOException {
private static void collectFrames(ProtobufReader itemClass, List<Frame> frames) throws IOException {
int classId = 0;
List<byte[]> blobs = new ArrayList<>();
while (itemClass.next()) {
@@ -205,7 +205,30 @@ public final class MyTeamSpeak {
}
}
for (int wanted : ITEM_CLASSES) {
if (wanted == classId) frames.addAll(blobs);
if (wanted != classId) continue;
for (byte[] blob : blobs) frames.add(new Frame(classId, blob));
}
}
private record Frame(int itemClass, byte[] blob) {
}
/**
* Contacts are TeamSpeak 6's and were never seen in an account, so one that does not
* decrypt is taken for plaintext, like add-on flags, or else left out — not allowed to
* fail the bookmarks and identities.
*/
private static SyncItem decode(Frame frame, byte[] itemKey) throws IOException, MyTsException {
try {
return SyncItemDecoder.decode(MyTsCrypto.decryptItem(frame.blob(), itemKey));
} catch (GeneralSecurityException e) {
if (frame.itemClass() != CONTACTS) throw new MyTsException("Could not decrypt the account's items", e);
}
try {
SyncItem item = SyncItemDecoder.decode(frame.blob());
return item instanceof SyncItem.Contact ? item : null;
} catch (IOException | RuntimeException notPlaintext) {
return null;
}
}
@@ -0,0 +1,60 @@
package eu.lixko.ts3j.client.myts;
import java.util.Base64;
/**
* The two spellings of a myTeamSpeak ID. Servers carry it as {@code client_myteamspeak_id},
* standard base64; TeamSpeak chat puts the same bytes, lowercase base32, in the localpart of
* the account's Matrix user ID ({@code @<base32>:<homeserver>}).
*/
public final class MyTsId {
private static final String BASE32 = "abcdefghijklmnopqrstuvwxyz234567";
private MyTsId() {
}
/** The myTeamSpeak ID behind a TeamSpeak chat user ID, or empty when it is not one. */
public static String fromChatId(String chatId) {
if (chatId == null || !chatId.startsWith("@")) return "";
int colon = chatId.indexOf(':');
if (colon < 2) return "";
byte[] id = base32Decode(chatId.substring(1, colon));
return id == null ? "" : Base64.getEncoder().encodeToString(id);
}
/** RFC 4648 base32, lowercase and unpadded. */
static String base32(byte[] data) {
StringBuilder out = new StringBuilder((data.length * 8 + 4) / 5);
int buffer = 0, bits = 0;
for (byte b : data) {
buffer = (buffer << 8) | (b & 0xff);
bits += 8;
while (bits >= 5) {
out.append(BASE32.charAt((buffer >> (bits - 5)) & 31));
bits -= 5;
}
buffer &= (1 << bits) - 1;
}
if (bits > 0) out.append(BASE32.charAt((buffer << (5 - bits)) & 31));
return out.toString();
}
/** The inverse of {@link #base32}, either case; {@code null} for anything else. */
static byte[] base32Decode(String text) {
byte[] out = new byte[text.length() * 5 / 8];
int buffer = 0, bits = 0, n = 0;
for (int i = 0; i < text.length(); i++) {
int value = BASE32.indexOf(Character.toLowerCase(text.charAt(i)));
if (value < 0) return null;
buffer = (buffer << 5) | value;
bits += 5;
if (bits >= 8) {
out[n++] = (byte) (buffer >> (bits - 8));
bits -= 8;
buffer &= (1 << bits) - 1;
}
}
return out;
}
}
@@ -11,6 +11,8 @@ public final class ClientEntry {
/** How text-to-speech should say the nickname ({@code client_nickname_phonetic}). */
public volatile String phoneticNickname = "";
public volatile String uniqueId = "";
/** {@code client_myteamspeak_id}: the client's myTeamSpeak account, empty when it shares none. */
public volatile String myTeamSpeakId = "";
public volatile int databaseId;
public volatile int type; // 0 = normal voice client, 1 = server-query
public volatile int talkPower;
@@ -47,6 +47,7 @@ final class ConnectionEventHandler implements TS3Listener {
c.away = e.isClientAway();
c.awayMessage = TeamspeakConnection.orEmpty(e.get("client_away_message"));
c.uniqueId = TeamspeakConnection.orEmpty(e.getUniqueClientIdentifier());
c.myTeamSpeakId = TeamspeakConnection.orEmpty(e.get("client_myteamspeak_id"));
c.avatarFlag = TeamspeakConnection.orEmpty(e.get("client_flag_avatar"));
c.country = TeamspeakConnection.orEmpty(e.get("client_country"));
c.databaseId = e.getClientDatabaseId();
@@ -351,6 +352,10 @@ final class ConnectionEventHandler implements TS3Listener {
c.channelCommander = e.getBoolean("client_is_channel_commander");
if (reported(e, "client_flag_avatar")) c.avatarFlag = TeamspeakConnection.orEmpty(e.get("client_flag_avatar"));
if (reported(e, "client_country")) c.country = TeamspeakConnection.orEmpty(e.get("client_country"));
if (reported(e, "client_myteamspeak_id")) {
c.myTeamSpeakId = TeamspeakConnection.orEmpty(e.get("client_myteamspeak_id"));
if (!c.self && !c.isQuery()) conn.applyContact(c);
}
if (has(e, "client_unread_messages")) {
int before = c.unreadMessages;
c.unreadMessages = safeInt(e, "client_unread_messages");
@@ -1312,9 +1312,11 @@ public final class TeamspeakConnection implements TS3Listener {
/**
* Applies a client's contact entry to its playback — the automatic mute and the
* volume modifier — and records the sighting. Called when the client appears.
* volume modifier — and records the sighting. Called when the client appears, which
* is also when its myTeamSpeak ID can first match a contact.
*/
void applyContact(ClientEntry client) {
contacts.link(client.uniqueId, client.myTeamSpeakId);
Contact contact = applyContactPlayback(client);
if (contact != null) contacts.markSeen(client.uniqueId, model.getServerName(), serverHost + ":" + serverPort);
}
@@ -1,10 +1,13 @@
package eu.lixko.ts3j.client.teamspeak;
import eu.lixko.ts3j.client.myts.MyTsId;
/**
* One entry of the TeamSpeak 3 client's synchronised item store — the data that
* a myTeamSpeak account syncs between installations. Since client 3.1 the
* bookmarks and identities of every installation live there, signed in or not.
* Whisper lists, hotkey profiles and the other item classes are not modelled.
* TeamSpeak 6 adds contacts. Whisper lists, hotkey profiles and the other item
* classes are not modelled.
*/
public sealed interface SyncItem {
@@ -33,10 +36,28 @@ public sealed interface SyncItem {
* in some myTeamSpeak items); empty for the default
* @param defaultChannel channel path to join, "/"-separated; empty for the server default
* @param defaultChannelId the same channel by id, {@code 0} when unknown
* @param myTeamSpeakFeatures "Enable myTeamSpeak features" ({@code send_mytsid_on_server})
*/
record Bookmark(String uuid, String parentUuid, String previousUuid, String name, String address, int port,
String nickname, String phoneticNickname, String identityUuid,
String serverPassword, String defaultChannel, String defaultChannelPassword,
long defaultChannelId, boolean autoconnect) implements SyncItem {
long defaultChannelId, boolean autoconnect, boolean myTeamSpeakFeatures) implements SyncItem {
}
/**
* What TeamSpeak 6 keeps about a chat: a custom name and how it is filed. One exists only
* once something about the chat was customised, so it is not a whole contact list.
*
* @param myTsId the partner's myTeamSpeak ID as servers carry it; TeamSpeak 6 leaves it empty
* @param chatMemberId the partner's TeamSpeak chat (Matrix) user ID, for a direct chat
*/
record Contact(String uuid, String parentUuid, String previousUuid, String customName, String category,
boolean favourite, boolean directChat, String myTsId, String chatMemberId) implements SyncItem {
/** The partner's myTeamSpeak ID, read out of the chat ID when the item names none; empty for a group chat. */
public String myTeamSpeakId() {
if (!myTsId.isBlank()) return myTsId;
return directChat ? MyTsId.fromChatId(chatMemberId) : "";
}
}
}
@@ -38,7 +38,7 @@ public final class SyncItemDecoder {
case 4 -> deleted = r.readVarint() == ITEM_DELETED;
case 7 -> parent = r.readString();
case 8 -> previous = r.readString();
case 16, 17, 22 -> {
case 16, 17, 22, 25 -> {
contentField = r.fieldNumber();
content = r.readMessage();
}
@@ -50,6 +50,7 @@ public final class SyncItemDecoder {
return switch (contentField) {
case 16 -> bookmark(uuid, parent, previous, content);
case 17 -> identity(uuid, parent, previous, content);
case 25 -> contact(uuid, parent, previous, content);
default -> folder(uuid, parent, previous, content);
};
}
@@ -59,7 +60,7 @@ public final class SyncItemDecoder {
String serverPassword = "", channel = "", channelPassword = "";
int port = 9987;
long channelId = 0;
boolean autoconnect = false;
boolean autoconnect = false, myTeamSpeakFeatures = false;
while (r.next()) {
switch (r.fieldNumber()) {
case 1 -> name = r.readString();
@@ -73,11 +74,12 @@ public final class SyncItemDecoder {
case 9 -> channelPassword = r.readString();
case 10 -> channelId = r.readVarint();
case 14 -> autoconnect = r.readBool();
case 22 -> myTeamSpeakFeatures = r.readBool();
default -> r.skip();
}
}
return new SyncItem.Bookmark(uuid, parent, previous, name, address, port, nickname, phonetic, identity,
serverPassword, channel, channelPassword, channelId, autoconnect);
serverPassword, channel, channelPassword, channelId, autoconnect, myTeamSpeakFeatures);
}
private static SyncItem.Identity identity(String uuid, String parent, String previous, ProtobufReader r) throws IOException {
@@ -96,6 +98,23 @@ public final class SyncItemDecoder {
return new SyncItem.Identity(uuid, parent, previous, identity, name, nickname, phonetic, isDefault);
}
private static SyncItem.Contact contact(String uuid, String parent, String previous, ProtobufReader r) throws IOException {
String name = "", category = "", myTsId = "", memberId = "";
boolean favourite = false, direct = false;
while (r.next()) {
switch (r.fieldNumber()) {
case 2 -> name = r.readString();
case 3 -> category = r.readString();
case 5 -> favourite = r.readBool();
case 6 -> direct = r.readBool();
case 7 -> myTsId = r.readString();
case 8 -> memberId = r.readString();
default -> r.skip();
}
}
return new SyncItem.Contact(uuid, parent, previous, name, category, favourite, direct, myTsId, memberId);
}
private static SyncItem.Folder folder(String uuid, String parent, String previous, ProtobufReader r) throws IOException {
String name = "";
while (r.next()) {
@@ -5,6 +5,9 @@ import eu.lixko.ts3j.client.config.Bookmarks;
import eu.lixko.ts3j.client.config.IdentityEntry;
import eu.lixko.ts3j.client.config.IdentityStore;
import eu.lixko.ts3j.client.config.Settings;
import eu.lixko.ts3j.client.contacts.Contact;
import eu.lixko.ts3j.client.contacts.ContactStore;
import eu.lixko.ts3j.client.contacts.ContactType;
import com.github.manevolent.ts3j.identity.LocalIdentity;
import java.io.File;
@@ -21,15 +24,18 @@ import java.util.Set;
/**
* Carries the TeamSpeak 3 client's synchronised bookmarks and identities —
* from its local store or a myTeamSpeak account — over into this client's stores. Importing is additive and idempotent: an identity
* already present (same unique ID) or a bookmark already present (same label,
* address and port) is left alone, and bookmarks are wired to the identities
* they referenced in TeamSpeak.
* from its local store or a myTeamSpeak account — over into this client's stores,
* and TeamSpeak 6's contacts along with them when given a contact list. Importing
* is additive and idempotent: an identity already present (same unique ID), a
* bookmark already present (same label, address and port) or a contact already
* present (same myTeamSpeak ID) is left alone, and bookmarks are wired to the
* identities they referenced in TeamSpeak.
*/
public final class TeamSpeakImporter {
/** What an import did; {@code known} counts the items that were already there. */
public record Result(int identitiesAdded, int identitiesKnown, int bookmarksAdded, int bookmarksKnown) {
public record Result(int identitiesAdded, int identitiesKnown, int bookmarksAdded, int bookmarksKnown,
int contactsAdded, int contactsKnown) {
public int identities() {
return identitiesAdded + identitiesKnown;
}
@@ -42,11 +48,26 @@ public final class TeamSpeakImporter {
private final IdentityStore identities;
private final Bookmarks bookmarks;
private final Settings settings;
/** Where contacts go; {@code null} leaves them out. */
private final ContactStore contacts;
public TeamSpeakImporter(IdentityStore identities, Bookmarks bookmarks, Settings settings) {
this(identities, bookmarks, settings, null);
}
public TeamSpeakImporter(IdentityStore identities, Bookmarks bookmarks, Settings settings, ContactStore contacts) {
this.identities = identities;
this.bookmarks = bookmarks;
this.settings = settings;
this.contacts = contacts;
}
/**
* Whether a contact can be brought over: only a direct chat says whose it is, and only a
* myTeamSpeak ID can be matched against clients on a server.
*/
public static boolean isImportable(SyncItem.Contact contact) {
return !contact.myTeamSpeakId().isEmpty();
}
/** Imports the identities only. */
@@ -64,7 +85,7 @@ public final class TeamSpeakImporter {
return importAll(TeamSpeakSettingsDb.readSyncItems(db));
}
/** Imports every identity and bookmark among items read elsewhere, such as from a myTeamSpeak account. */
/** Imports every identity, bookmark and contact among items read elsewhere, such as from a myTeamSpeak account. */
public Result importAll(List<SyncItem> items) throws IOException {
return importItems(items, true, false);
}
@@ -84,9 +105,15 @@ public final class TeamSpeakImporter {
return importItems(new ArrayList<>(selection), true, false);
}
/** Whether the item is here already: an identity with the same key, or a bookmark with the same label and address. */
/**
* Whether the item is here already: an identity with the same key, a bookmark with the same
* label and address, or a contact with the same myTeamSpeak ID.
*/
public boolean isPresent(SyncItem item) {
if (item instanceof SyncItem.Bookmark bookmark) return find(bookmark) != null;
if (item instanceof SyncItem.Contact contact) {
return contacts != null && contacts.findByMyTeamSpeakId(contact.myTeamSpeakId()) != null;
}
if (!(item instanceof SyncItem.Identity identity)) return false;
try {
return identities.byUniqueId(IdentityStore.parseIdentityString(identity.identity()).getUid().toBase64()) != null;
@@ -157,7 +184,7 @@ public final class TeamSpeakImporter {
}
}
if (settingsChanged) settings.save();
if (!withBookmarks) return new Result(identitiesAdded, identitiesKnown, 0, 0);
if (!withBookmarks) return new Result(identitiesAdded, identitiesKnown, 0, 0, 0, 0);
for (SyncItem.Bookmark bookmark : syncBookmarks) {
if (find(bookmark) != null) {
@@ -172,7 +199,32 @@ public final class TeamSpeakImporter {
bookmarksAdded++;
}
if (bookmarksAdded > 0) bookmarks.save();
return new Result(identitiesAdded, identitiesKnown, bookmarksAdded, bookmarksKnown);
int contactsAdded = 0, contactsKnown = 0;
if (contacts != null) {
Map<String, Contact> added = new LinkedHashMap<>();
for (SyncItem item : items) {
if (!(item instanceof SyncItem.Contact sync) || !isImportable(sync)) continue;
if (isPresent(sync) || added.containsKey(sync.myTeamSpeakId())) {
contactsKnown++;
} else {
added.put(sync.myTeamSpeakId(), toContact(sync));
contactsAdded++;
}
}
if (!added.isEmpty()) contacts.merge(added.values());
}
return new Result(identitiesAdded, identitiesKnown, bookmarksAdded, bookmarksKnown,
contactsAdded, contactsKnown);
}
/** Someone chatted with in TeamSpeak 6 is a friend, as a contact there is. */
private Contact toContact(SyncItem.Contact sync) {
Contact contact = new Contact("", sync.myTeamSpeakId());
contact.nickname = sync.customName();
contact.type = ContactType.FRIEND;
contact.applyDefaults(settings.contactDefaults.of(ContactType.FRIEND));
return contact;
}
private Bookmark toBookmark(SyncItem.Bookmark sync, IdentityEntry identity) {
@@ -183,6 +235,7 @@ public final class TeamSpeakImporter {
if (b.channel.isBlank() && sync.defaultChannelId() != 0) b.channel = "/" + sync.defaultChannelId();
b.channelPassword = sync.defaultChannelPassword();
b.connectOnStartup = sync.autoconnect();
b.myTeamSpeakFeatures = sync.myTeamSpeakFeatures();
return b;
}