Open ts3server:// links on both apps

A link connects with its nickname, passwords, channel (path or cid) and
privilege key, the key used once on; addbookmark saves it as a bookmark.
A link to a server we are on already goes to its channel instead.

Android takes the links from browsers and other apps through an intent
filter, and from chat. The desktop opens them from chat and descriptions,
and its launcher entry claims the scheme when run from the jar; a second
launch hands its link to the running instance over a Unix socket in the
user's runtime directory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-09 22:19:52 +00:00
co-authored by Claude Opus 5.5
parent 45d6fcda3d
commit e76804fb2d
15 changed files with 474 additions and 14 deletions
@@ -0,0 +1,145 @@
package com.ts3client.session;
import com.ts3client.config.Bookmark;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Locale;
/**
* A {@code ts3server://} link, as web pages and chats hand them out:
* {@code ts3server://host[:port]?port=…&nickname=…&password=…&channel=A/B&cid=…
* &channelpassword=…&token=…&addbookmark=…}. Every parameter is optional.
*/
public final class ServerLink {
public static final String SCHEME = "ts3server";
private static final String PREFIX = SCHEME + "://";
private static final int DEFAULT_PORT = 9987;
public final String address;
public final int port;
/** Empty for the identity's or the global nickname. */
public final String nickname;
public final String password;
/** A "/"-separated path or {@code "/<channelId>"}, as {@link Bookmark#channel}; empty for the default channel. */
public final String channel;
public final String channelPassword;
/** A privilege key to use once connected; empty for none. */
public final String token;
/** A name to bookmark the server under, or empty when the link does not ask for a bookmark. */
public final String bookmarkLabel;
private ServerLink(String address, int port, String nickname, String password, String channel,
String channelPassword, String token, String bookmarkLabel) {
this.address = address;
this.port = port;
this.nickname = nickname;
this.password = password;
this.channel = channel;
this.channelPassword = channelPassword;
this.token = token;
this.bookmarkLabel = bookmarkLabel;
}
/** Where a bookmark leads, as a link without a privilege key would. */
public static ServerLink of(Bookmark b) {
return new ServerLink(b.address.trim(), b.port, orEmpty(b.nickname), orEmpty(b.password), orEmpty(b.channel),
orEmpty(b.channelPassword), "", "");
}
private static String orEmpty(String s) {
return s == null ? "" : s;
}
public static boolean isLink(String href) {
return href != null && href.regionMatches(true, 0, PREFIX, 0, PREFIX.length());
}
/** Parses a link, or returns {@code null} if it is not one or names no server. */
public static ServerLink parse(String href) {
if (!isLink(href)) return null;
String rest = href.substring(PREFIX.length()).trim();
int query = rest.indexOf('?');
String authority = query < 0 ? rest : rest.substring(0, query);
String params = query < 0 ? "" : rest.substring(query + 1);
int slash = authority.indexOf('/');
if (slash >= 0) authority = authority.substring(0, slash);
String host = authority;
int port = DEFAULT_PORT;
if (authority.startsWith("[")) {
int close = authority.indexOf(']');
if (close < 0) return null;
host = authority.substring(1, close);
port = portAfter(authority.substring(close + 1), port);
} else if (authority.indexOf(':') == authority.lastIndexOf(':') && authority.indexOf(':') >= 0) {
host = authority.substring(0, authority.indexOf(':'));
port = portAfter(authority.substring(authority.indexOf(':')), port);
}
host = decode(host).trim();
if (host.isEmpty()) return null;
String nickname = "", password = "", channel = "", channelId = "", channelPassword = "", token = "", label = "";
for (String pair : params.split("&")) {
int eq = pair.indexOf('=');
String key = (eq < 0 ? pair : pair.substring(0, eq)).toLowerCase(Locale.ROOT);
String value = eq < 0 ? "" : decode(pair.substring(eq + 1));
switch (key) {
case "port" -> port = parsePort(value, port);
case "nickname" -> nickname = value.trim();
case "password" -> password = value;
case "channel" -> channel = value.trim();
case "cid" -> channelId = value.trim();
case "channelpassword" -> channelPassword = value;
case "token" -> token = value.trim();
case "addbookmark" -> label = value.trim();
default -> {
}
}
}
// A channel id is exact, where a path may have been renamed since the link was made.
if (isChannelId(channelId)) channel = "/" + channelId;
return new ServerLink(host, port, nickname, password, channel, channelPassword, token, label);
}
/** Whether this link leads to the server at {@code address}:{@code port}. */
public boolean pointsAt(String address, int port) {
return this.port == port && address != null && this.address.equalsIgnoreCase(address.trim());
}
/** A bookmark of the server the link leads to, under the name it asks for, else its address. */
public Bookmark toBookmark() {
Bookmark b = new Bookmark(bookmarkLabel.isEmpty() ? address : bookmarkLabel, address, port, nickname, password);
b.channel = channel;
b.channelPassword = channelPassword;
return b;
}
/** A positive number; 0 names no channel. */
private static boolean isChannelId(String s) {
return !s.isEmpty() && s.chars().allMatch(Character::isDigit) && s.chars().anyMatch(c -> c != '0');
}
private static int portAfter(String colonPort, int fallback) {
return colonPort.startsWith(":") ? parsePort(colonPort.substring(1), fallback) : fallback;
}
private static int parsePort(String value, int fallback) {
try {
int port = Integer.parseInt(value.trim());
return port > 0 && port <= 65535 ? port : fallback;
} catch (NumberFormatException e) {
return fallback;
}
}
/** Percent-decodes; a '+' stays one, as TeamSpeak reads its links. */
private static String decode(String s) {
try {
return URLDecoder.decode(s.replace("+", "%2B"), "UTF-8");
} catch (UnsupportedEncodingException | IllegalArgumentException e) {
return s;
}
}
}
@@ -82,6 +82,8 @@ public final class ServerSession {
private volatile String lastChannelPath = "";
/** The server icon last reported through {@link Listener#onServerIconChanged}; -1 for none yet. */
private volatile long serverIconId = -1;
/** The link whose privilege key to use once connected to its server; null for none. */
private volatile ServerLink tokenLink;
private record Target(String address, int port, String nickname, String password, String channelPassword) {
}
@@ -188,6 +190,29 @@ public final class ServerSession {
}, "identity-resolve").start();
}
/** Whether we are on the server a {@code ts3server://} link leads to. */
public boolean isOn(ServerLink link) {
return conn.isConnected() && link.pointsAt(address(), port());
}
/** Does what a link to the server we are on asks: goes to its channel, uses its privilege key. */
public void follow(ServerLink link) {
if (!isOn(link)) return;
ChannelNode target = link.channel.isEmpty() ? null : conn.getModel().findChannelByPath(link.channel);
ChannelNode current = currentChannel();
if (target != null && (current == null || target.id != current.id)) {
conn.joinChannel(target.id, link.channelPassword.isEmpty() ? null : link.channelPassword);
}
tokenLink = link.token.isEmpty() ? null : link;
useConnectToken();
}
/** Connects where a {@code ts3server://} link leads, using its privilege key once on. */
public void open(ServerLink link, String identityId) {
tokenLink = link.token.isEmpty() ? null : link;
switchTo(link.address, link.port, link.nickname, link.password, identityId, link.channel, link.channelPassword);
}
private String nicknameFor(String serverNickname, IdentityEntry identity) {
if (serverNickname != null && !serverNickname.isBlank()) return serverNickname.trim();
if (!identity.getNickname().isBlank()) return identity.getNickname();
@@ -627,6 +652,15 @@ public final class ServerSession {
setStatus(message);
}
private void useConnectToken() {
ServerLink link = tokenLink;
tokenLink = null;
Target target = lastTarget;
if (link == null || target == null || !link.pointsAt(target.address(), target.port())) return;
conn.usePrivilegeKey(link.token, error -> listener.onSystemMessage(error == null
? "Privilege key used." : "Could not use the privilege key: " + error));
}
private void checkServerIcon() {
if (!conn.isConnected()) return;
long id = conn.getModel().serverIconId();
@@ -652,6 +686,7 @@ public final class ServerSession {
listener.onSystemMessage("Connected.");
listener.onSessionChanged();
checkServerIcon();
useConnectToken();
}
@Override