Answer what we may do in one place: Permissions
The rules were spread over ServerSession's can* methods, ServerModel's permits/mayHavePower/canDelete*, TeamspeakConnection and AvatarAdmin, and Swing dialogs and Android's ServerController passed raw permission names. Permissions now holds the server's permission table and the rules (offered until the server says, unreported powers unknown until asked) and answers one named question per capability. ServerSession.permissions() is the gate for frontends: Permissions.NONE, allowing nothing, while disconnected. ServerController's forwarding properties go the same way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -21,8 +21,6 @@ import java.util.concurrent.TimeUnit;
|
||||
*/
|
||||
final class AvatarAdmin {
|
||||
|
||||
/** The server's own default for {@code i_client_max_avatar_filesize}. */
|
||||
static final int DEFAULT_MAX_AVATAR_BYTES = 200_000;
|
||||
/** The largest avatar we are willing to download, whatever a server allows. */
|
||||
static final int MAX_AVATAR_DOWNLOAD_BYTES = 8 * 1024 * 1024;
|
||||
|
||||
@@ -34,15 +32,6 @@ final class AvatarAdmin {
|
||||
this.conn = conn;
|
||||
}
|
||||
|
||||
/**
|
||||
* How large an avatar this server lets us upload, from
|
||||
* {@code i_client_max_avatar_filesize}, or the server default while unknown.
|
||||
*/
|
||||
int maxUploadBytes() {
|
||||
int limit = conn.getModel().selfPermissionValue("i_client_max_avatar_filesize");
|
||||
return limit > 0 ? limit : DEFAULT_MAX_AVATAR_BYTES;
|
||||
}
|
||||
|
||||
/** Uploads {@code source} as our avatar and announces it; returns its flag. */
|
||||
String upload(File source) throws Exception {
|
||||
byte[] data = Files.readAllBytes(source.toPath());
|
||||
@@ -50,7 +39,7 @@ final class AvatarAdmin {
|
||||
if (ImageFormat.detect(data) == null) {
|
||||
throw new IllegalArgumentException("Avatars must be PNG, JPEG, GIF or BMP images");
|
||||
}
|
||||
int limit = maxUploadBytes();
|
||||
int limit = conn.getModel().permissions().maxAvatarBytes();
|
||||
if (data.length > limit) {
|
||||
throw new IllegalArgumentException("This server limits avatars to " + (limit / 1024) + " KiB");
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ final class ChannelAdmin {
|
||||
* carries — anything inherited from a group is absent
|
||||
*/
|
||||
Map<String, Integer> readPermissions(int channelId) throws Exception {
|
||||
if (!conn.getModel().hasPermissionNames()) {
|
||||
if (!conn.getModel().permissions().hasNames()) {
|
||||
// channelpermlist reports numeric ids, which only the permissionlist response
|
||||
// gives names to; without it the values could not be told apart.
|
||||
throw new IllegalStateException("The server did not send its permission list");
|
||||
@@ -133,7 +133,7 @@ final class ChannelAdmin {
|
||||
|
||||
Map<String, Integer> byName = new LinkedHashMap<>();
|
||||
for (Map.Entry<Integer, Integer> entry : request.values.entrySet()) {
|
||||
String name = conn.getModel().permissionName(entry.getKey());
|
||||
String name = conn.getModel().permissions().name(entry.getKey());
|
||||
if (name != null) byName.put(name, entry.getValue());
|
||||
}
|
||||
return byName;
|
||||
|
||||
@@ -633,12 +633,12 @@ final class ConnectionEventHandler implements TS3Listener {
|
||||
|
||||
@Override
|
||||
public void onPermissionList(PermissionListEvent e) {
|
||||
conn.getModel().putPermissionName(e.get("permname"));
|
||||
conn.getModel().permissions().putName(e.get("permname"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClientNeededPermissions(ClientNeededPermissionsEvent e) {
|
||||
conn.getModel().putSelfPermissionValue(safeInt(e, "permid"), safeInt(e, "permvalue"));
|
||||
conn.getModel().permissions().putValue(safeInt(e, "permid"), safeInt(e, "permvalue"));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+3
-3
@@ -89,9 +89,9 @@ final class ConnectionStatsCollector {
|
||||
s.monthBytesUploaded = entry.monthBytesUploaded;
|
||||
}
|
||||
if (s.self) {
|
||||
ServerModel model = conn.getModel();
|
||||
s.downloadQuotaMb = model.selfPermissionValue("i_ft_quota_mb_download_per_client", -1);
|
||||
s.uploadQuotaMb = model.selfPermissionValue("i_ft_quota_mb_upload_per_client", -1);
|
||||
Permissions permissions = conn.getModel().permissions();
|
||||
s.downloadQuotaMb = permissions.downloadQuotaMb();
|
||||
s.uploadQuotaMb = permissions.uploadQuotaMb();
|
||||
}
|
||||
VoiceOutput playback = conn.getPlayback();
|
||||
if (playback != null) {
|
||||
|
||||
@@ -0,0 +1,298 @@
|
||||
package eu.lixko.ts3j.client.net;
|
||||
|
||||
import eu.lixko.ts3j.client.net.ban.BanEntry;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* What the local client may do on a server, worked out from the permissions the server
|
||||
* reports for it. Frontends ask the named questions; the permission names stay in here.
|
||||
*
|
||||
* <p>Until the server has said, an action is offered and the server is left to refuse, as
|
||||
* the official client does. {@link #NONE} allows nothing, for while there is no connection.
|
||||
* Thread-safe: filled from the event thread, asked from the UI.
|
||||
*/
|
||||
public final class Permissions {
|
||||
|
||||
/** Allows nothing. */
|
||||
public static final Permissions NONE = new Permissions(false);
|
||||
|
||||
/**
|
||||
* The permissions {@code notifyclientneededpermissions} never carries, not even for an admin;
|
||||
* they have to be asked for with {@code permget}.
|
||||
*/
|
||||
public static final List<String> UNREPORTED = List.of("i_client_move_power",
|
||||
"i_client_kick_from_channel_power", "i_client_kick_from_server_power", "i_client_ban_power",
|
||||
"b_client_complain_delete", "b_client_complain_delete_own");
|
||||
|
||||
/** The server's own default for {@code i_client_max_avatar_filesize}. */
|
||||
private static final int DEFAULT_MAX_AVATAR_BYTES = 200_000;
|
||||
private static final int FIRST_PERMISSION_ID = 1;
|
||||
|
||||
private final boolean live;
|
||||
/**
|
||||
* Permission id -> name, learned from the server's {@code permissionlist} response.
|
||||
* Entries carry no id of their own: TS3 numbers them by position in the response,
|
||||
* starting at 1, and the empty "group_id_end" separator records marking category
|
||||
* boundaries don't count toward that position, so only {@link #putName} advances it.
|
||||
*/
|
||||
private final Map<Integer, String> names = new LinkedHashMap<>();
|
||||
private final Map<String, Integer> ids = new HashMap<>();
|
||||
private int nextId = FIRST_PERMISSION_ID;
|
||||
/** Permission id -> the local client's resolved value, from {@code notifyclientneededpermissions}. */
|
||||
private final Map<Integer, Integer> values = new LinkedHashMap<>();
|
||||
/** The local client's {@link #UNREPORTED} permissions; absent until known. */
|
||||
private final Map<String, Integer> unreported = new HashMap<>();
|
||||
/** Whether the server answered for the {@link #UNREPORTED} permissions at all. */
|
||||
private boolean unreportedKnown;
|
||||
|
||||
Permissions() {
|
||||
this(true);
|
||||
}
|
||||
|
||||
private Permissions(boolean live) {
|
||||
this.live = live;
|
||||
}
|
||||
|
||||
synchronized void clear() {
|
||||
names.clear();
|
||||
ids.clear();
|
||||
nextId = FIRST_PERMISSION_ID;
|
||||
values.clear();
|
||||
unreported.clear();
|
||||
unreportedKnown = false;
|
||||
}
|
||||
|
||||
// ---- what the server reports ----
|
||||
|
||||
/** Records the next named entry of a {@code permissionlist} response; ignores separators. */
|
||||
synchronized void putName(String name) {
|
||||
if (name == null || name.isEmpty()) return;
|
||||
names.put(nextId, name);
|
||||
ids.putIfAbsent(name, nextId);
|
||||
nextId++;
|
||||
}
|
||||
|
||||
synchronized void putValue(int permissionId, int value) {
|
||||
values.put(permissionId, value);
|
||||
}
|
||||
|
||||
/** Takes on the answer to asking for the {@link #UNREPORTED} permissions; those it leaves out count as 0. */
|
||||
synchronized void setUnreported(Map<String, Integer> values) {
|
||||
unreported.clear();
|
||||
unreported.putAll(values);
|
||||
unreportedKnown = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The server would not say: asking needs {@code b_client_permissionoverview_own}, which
|
||||
* moderator groups often lack although they hold the powers.
|
||||
*/
|
||||
synchronized void unreportedRefused() {
|
||||
unreported.clear();
|
||||
unreportedKnown = false;
|
||||
}
|
||||
|
||||
/** Whether the server's {@code permissionlist} has arrived, so ids can be named at all. */
|
||||
synchronized boolean hasNames() {
|
||||
return !names.isEmpty();
|
||||
}
|
||||
|
||||
/** The name the server gave permission id {@code id}, or {@code null} if unknown. */
|
||||
synchronized String name(int id) {
|
||||
return names.get(id);
|
||||
}
|
||||
|
||||
// ---- what we may do ----
|
||||
|
||||
public boolean canViewBanList() {
|
||||
return permits("b_client_ban_list");
|
||||
}
|
||||
|
||||
public boolean canCreateBans() {
|
||||
return permits("b_client_ban_create");
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we may delete all of {@code bans}: any ban with {@code b_client_ban_delete},
|
||||
* only our own with {@code b_client_ban_delete_own}.
|
||||
*
|
||||
* @param selfUid our unique identifier, which is how a ban names its creator
|
||||
*/
|
||||
public synchronized boolean canDeleteBans(Collection<BanEntry> bans, String selfUid) {
|
||||
if (permits("b_client_ban_delete")) return true;
|
||||
if (!permits("b_client_ban_delete_own")) return false;
|
||||
return bans.stream().allMatch(b -> b.createdBy(selfUid));
|
||||
}
|
||||
|
||||
/** Whether we may rewrite {@code ban}: that is deleting it and creating its replacement. */
|
||||
public synchronized boolean canEditBan(BanEntry ban, String selfUid) {
|
||||
return canCreateBans() && canDeleteBans(List.of(ban), selfUid);
|
||||
}
|
||||
|
||||
/** {@code i_client_ban_max_bantime}: the longest ban we may hand out in seconds; 0 or -1 for no limit. */
|
||||
public synchronized long maxBanSeconds() {
|
||||
return value("i_client_ban_max_bantime", 0);
|
||||
}
|
||||
|
||||
public boolean canViewComplaints() {
|
||||
return permits("b_client_complain_list");
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we may remove all of {@code complaints}: any with {@code b_client_complain_delete},
|
||||
* only those we filed with {@code b_client_complain_delete_own}.
|
||||
*/
|
||||
public synchronized boolean canDeleteComplaints(Collection<Complaint> complaints, int selfDbId) {
|
||||
if (power("b_client_complain_delete") > 0) return true;
|
||||
if (power("b_client_complain_delete_own") <= 0) return false;
|
||||
return complaints.stream().allMatch(c -> c.fromDbId() == selfDbId);
|
||||
}
|
||||
|
||||
public boolean canViewServerLog() {
|
||||
return permits("b_virtualserver_log_view");
|
||||
}
|
||||
|
||||
public boolean canListPrivilegeKeys() {
|
||||
return permits("b_virtualserver_token_list");
|
||||
}
|
||||
|
||||
public boolean canAddPrivilegeKeys() {
|
||||
return permits("b_virtualserver_token_add");
|
||||
}
|
||||
|
||||
public boolean canDeletePrivilegeKeys() {
|
||||
return permits("b_virtualserver_token_delete");
|
||||
}
|
||||
|
||||
public boolean canSendOfflineMessages() {
|
||||
return permits("b_client_offline_textmessage_send");
|
||||
}
|
||||
|
||||
/** Whether we may list channel files at all; a channel may still need more power than we have. */
|
||||
public boolean canBrowseFiles() {
|
||||
return permits("i_ft_file_browse_power");
|
||||
}
|
||||
|
||||
/** {@code i_ft_quota_mb_download_per_client}, or -1 while unknown. */
|
||||
public int downloadQuotaMb() {
|
||||
return value("i_ft_quota_mb_download_per_client", -1);
|
||||
}
|
||||
|
||||
/** {@code i_ft_quota_mb_upload_per_client}, or -1 while unknown. */
|
||||
public int uploadQuotaMb() {
|
||||
return value("i_ft_quota_mb_upload_per_client", -1);
|
||||
}
|
||||
|
||||
public boolean canSetAvatar() {
|
||||
return permits("i_client_max_avatar_filesize");
|
||||
}
|
||||
|
||||
/** How large an avatar we may upload, or the server default while unknown. */
|
||||
public int maxAvatarBytes() {
|
||||
int limit = value("i_client_max_avatar_filesize", 0);
|
||||
return limit > 0 ? limit : DEFAULT_MAX_AVATAR_BYTES;
|
||||
}
|
||||
|
||||
public boolean canDeleteOtherAvatars() {
|
||||
return permits("b_client_avatar_delete_other");
|
||||
}
|
||||
|
||||
/** Whether the server lets us ask for talk power; whether we need it is another matter. */
|
||||
public boolean canRequestTalkPower() {
|
||||
return permits("b_client_request_talker");
|
||||
}
|
||||
|
||||
public boolean canGrantTalkPower() {
|
||||
return permits("b_client_set_flag_talker");
|
||||
}
|
||||
|
||||
/*
|
||||
* Whether we have any of a power; whether it beats the client's needed power only the
|
||||
* server knows, so it is left to refuse.
|
||||
*/
|
||||
|
||||
public boolean canMoveClients() {
|
||||
return mayHavePower("i_client_move_power");
|
||||
}
|
||||
|
||||
public boolean canKickFromChannel() {
|
||||
return mayHavePower("i_client_kick_from_channel_power");
|
||||
}
|
||||
|
||||
public boolean canKickFromServer() {
|
||||
return mayHavePower("i_client_kick_from_server_power");
|
||||
}
|
||||
|
||||
public boolean canBanClients() {
|
||||
return mayHavePower("i_client_ban_power");
|
||||
}
|
||||
|
||||
public boolean canCreateChannels() {
|
||||
return permitsAny("b_channel_create_temporary", "b_channel_create_semi_permanent",
|
||||
"b_channel_create_permanent");
|
||||
}
|
||||
|
||||
public boolean canEditChannels() {
|
||||
return permitsAny("b_channel_modify_name", "b_channel_modify_topic", "b_channel_modify_description",
|
||||
"b_channel_modify_password", "b_channel_modify_codec", "b_channel_modify_maxclients");
|
||||
}
|
||||
|
||||
public boolean canDeleteChannels() {
|
||||
return permitsAny("b_channel_delete_temporary", "b_channel_delete_semi_permanent",
|
||||
"b_channel_delete_permanent");
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we have enough power to assign {@code group}, or to take it away when
|
||||
* {@code assigned}: the add power always gates it, and the remove power additionally
|
||||
* gates a group already held. {@code -1} is TS3's "unlimited" sentinel on either side:
|
||||
* an unlimited own power always passes, and a group that needs unlimited power can only
|
||||
* be touched by a client that has it.
|
||||
*/
|
||||
public synchronized boolean canToggleGroup(Group group, boolean assigned) {
|
||||
if (!live) return false;
|
||||
return beats(value("i_group_member_add_power", 0), group.neededMemberAddPower)
|
||||
&& (!assigned || beats(value("i_group_member_remove_power", 0), group.neededMemberRemovePower));
|
||||
}
|
||||
|
||||
// ---- rules ----
|
||||
|
||||
/** Whether we hold a permission; offered until the server has named its permissions. */
|
||||
private synchronized boolean permits(String permission) {
|
||||
return live && (!hasNames() || value(permission, 0) > 0);
|
||||
}
|
||||
|
||||
private boolean permitsAny(String... permissions) {
|
||||
for (String p : permissions) {
|
||||
if (permits(p)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** One of the {@link #UNREPORTED} powers: offered unless the server said we lack it. */
|
||||
private synchronized boolean mayHavePower(String name) {
|
||||
return live && (!unreportedKnown || unreported.getOrDefault(name, 0) != 0);
|
||||
}
|
||||
|
||||
/** One of the {@link #UNREPORTED} powers; 0 until known, or when the server would not tell. */
|
||||
private synchronized int power(String name) {
|
||||
return live ? unreported.getOrDefault(name, 0) : 0;
|
||||
}
|
||||
|
||||
private synchronized int value(String name, int absent) {
|
||||
Integer id = ids.get(name);
|
||||
Integer value = id == null ? null : values.get(id);
|
||||
return value == null ? absent : value;
|
||||
}
|
||||
|
||||
private static boolean beats(int own, int needed) {
|
||||
if (own == -1) return true;
|
||||
if (needed == -1) return false;
|
||||
return own >= needed;
|
||||
}
|
||||
}
|
||||
@@ -22,23 +22,8 @@ public final class ServerModel {
|
||||
private final Map<Integer, ClientEntry> clients = new LinkedHashMap<>();
|
||||
private final Map<Integer, Group> serverGroups = new LinkedHashMap<>();
|
||||
private final Map<Integer, Group> channelGroups = new LinkedHashMap<>();
|
||||
/**
|
||||
* Permission id -> name, learned from the server's {@code permissionlist} response.
|
||||
* Entries carry no id of their own: TS3 numbers them by position in the response,
|
||||
* starting at 1, and the empty "group_id_end" separator records marking category
|
||||
* boundaries don't count toward that position, so only {@link #putPermissionName}
|
||||
* advances it.
|
||||
*/
|
||||
private final Map<Integer, String> permissionNames = new LinkedHashMap<>();
|
||||
private final Map<String, Integer> permissionIds = new HashMap<>();
|
||||
private static final int FIRST_PERMISSION_ID = 1;
|
||||
private int nextPermissionId = FIRST_PERMISSION_ID;
|
||||
/** Permission id -> the local client's resolved value, from {@code notifyclientneededpermissions}. */
|
||||
private final Map<Integer, Integer> selfPermissionValues = new LinkedHashMap<>();
|
||||
/** The local client's {@link #UNREPORTED_PERMISSIONS}, which the needed permissions leave out; absent until known. */
|
||||
private final Map<String, Integer> unreportedValues = new HashMap<>();
|
||||
/** Whether the server answered for the {@link #UNREPORTED_PERMISSIONS} at all. */
|
||||
private boolean unreportedKnown;
|
||||
/** What we may do here; cleared with the rest. */
|
||||
private final Permissions permissions = new Permissions();
|
||||
private String serverName = "TeamSpeak Server";
|
||||
/** The {@code virtualserver_*} properties from {@code initserver}, kept up to date by server edits. */
|
||||
private final Map<String, String> serverProperties = new HashMap<>();
|
||||
@@ -52,12 +37,7 @@ public final class ServerModel {
|
||||
clients.clear();
|
||||
serverGroups.clear();
|
||||
channelGroups.clear();
|
||||
permissionNames.clear();
|
||||
permissionIds.clear();
|
||||
nextPermissionId = FIRST_PERMISSION_ID;
|
||||
selfPermissionValues.clear();
|
||||
unreportedValues.clear();
|
||||
unreportedKnown = false;
|
||||
permissions.clear();
|
||||
serverProperties.clear();
|
||||
defaultChannelGroupId = 0;
|
||||
defaultChannelId = 0;
|
||||
@@ -130,137 +110,8 @@ public final class ServerModel {
|
||||
return list;
|
||||
}
|
||||
|
||||
// ---- permissions ----
|
||||
|
||||
/** Records the next named entry of a {@code permissionlist} response; ignores separators. */
|
||||
public synchronized void putPermissionName(String name) {
|
||||
if (name == null || name.isEmpty()) return;
|
||||
permissionNames.put(nextPermissionId, name);
|
||||
permissionIds.putIfAbsent(name, nextPermissionId);
|
||||
nextPermissionId++;
|
||||
}
|
||||
|
||||
public synchronized void putSelfPermissionValue(int permId, int value) {
|
||||
selfPermissionValues.put(permId, value);
|
||||
}
|
||||
|
||||
/** Whether the server's {@code permissionlist} has arrived, so ids can be named at all. */
|
||||
public synchronized boolean hasPermissionNames() {
|
||||
return !permissionNames.isEmpty();
|
||||
}
|
||||
|
||||
/** The name the server gave permission id {@code id}, or {@code null} if unknown. */
|
||||
public synchronized String permissionName(int id) {
|
||||
return permissionNames.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* The local client's resolved value for a named permission (e.g.
|
||||
* {@code "i_group_member_add_power"}), or {@code 0} if it isn't known
|
||||
* yet (the {@code permissionlist} request is still in flight, or the server
|
||||
* never reported a non-default value for it).
|
||||
*/
|
||||
public synchronized int selfPermissionValue(String name) {
|
||||
return selfPermissionValue(name, 0);
|
||||
}
|
||||
|
||||
/** As {@link #selfPermissionValue(String)}, with {@code absent} for a permission not (yet) reported. */
|
||||
public synchronized int selfPermissionValue(String name, int absent) {
|
||||
Integer id = permissionIds.get(name);
|
||||
Integer value = id == null ? null : selfPermissionValues.get(id);
|
||||
return value == null ? absent : value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The permissions {@code notifyclientneededpermissions} never carries, not even for an admin;
|
||||
* they have to be asked for with {@code permget}.
|
||||
*/
|
||||
public static final List<String> UNREPORTED_PERMISSIONS = List.of("i_client_move_power",
|
||||
"i_client_kick_from_channel_power", "i_client_kick_from_server_power", "i_client_ban_power",
|
||||
"b_client_complain_delete", "b_client_complain_delete_own");
|
||||
|
||||
/** Takes on the answer to asking for {@link #UNREPORTED_PERMISSIONS}; those it leaves out count as 0. */
|
||||
public synchronized void setUnreportedPermissions(Map<String, Integer> values) {
|
||||
unreportedValues.clear();
|
||||
unreportedValues.putAll(values);
|
||||
unreportedKnown = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The server would not say: asking needs {@code b_client_permissionoverview_own}, which
|
||||
* moderator groups often lack although they hold the powers.
|
||||
*/
|
||||
public synchronized void unreportedPermissionsRefused() {
|
||||
unreportedValues.clear();
|
||||
unreportedKnown = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we may hold one of {@link #UNREPORTED_PERMISSIONS}: unless the server said we
|
||||
* do not, the action is offered, as the official client always does, and the server judges.
|
||||
*/
|
||||
public synchronized boolean mayHavePower(String name) {
|
||||
return !unreportedKnown || unreportedValues.getOrDefault(name, 0) != 0;
|
||||
}
|
||||
|
||||
/** One of {@link #UNREPORTED_PERMISSIONS}; 0 until known, or when the server would not tell. */
|
||||
public synchronized int selfPower(String name) {
|
||||
return unreportedValues.getOrDefault(name, 0);
|
||||
}
|
||||
|
||||
// ---- what the local client may do ----
|
||||
|
||||
/**
|
||||
* Whether we hold a permission. Until the server has named its permissions nothing can
|
||||
* be resolved, so everything is offered and the server is left to refuse.
|
||||
*/
|
||||
public synchronized boolean permits(String permission) {
|
||||
return !hasPermissionNames() || selfPermissionValue(permission) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we may delete all of {@code bans}: any ban with {@code b_client_ban_delete},
|
||||
* only our own with {@code b_client_ban_delete_own}.
|
||||
*
|
||||
* @param selfUid our unique identifier, which is how a ban names its creator
|
||||
*/
|
||||
public synchronized boolean canDeleteBans(Collection<BanEntry> bans, String selfUid) {
|
||||
if (permits("b_client_ban_delete")) return true;
|
||||
if (!permits("b_client_ban_delete_own")) return false;
|
||||
return bans.stream().allMatch(b -> b.createdBy(selfUid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we may remove all of {@code complaints}: any with {@code b_client_complain_delete},
|
||||
* only those we filed with {@code b_client_complain_delete_own}.
|
||||
*/
|
||||
public synchronized boolean canDeleteComplaints(Collection<Complaint> complaints, int selfDbId) {
|
||||
if (selfPower("b_client_complain_delete") > 0) return true;
|
||||
if (selfPower("b_client_complain_delete_own") <= 0) return false;
|
||||
return complaints.stream().allMatch(c -> c.fromDbId() == selfDbId);
|
||||
}
|
||||
|
||||
/** Whether we may rewrite {@code ban}: that is deleting it and creating its replacement. */
|
||||
public synchronized boolean canEditBan(BanEntry ban, String selfUid) {
|
||||
return permits("b_client_ban_create") && canDeleteBans(List.of(ban), selfUid);
|
||||
}
|
||||
|
||||
/** {@code i_client_ban_max_bantime}: the longest ban we may hand out in seconds; 0 or -1 for no limit. */
|
||||
public synchronized long maxBanSeconds() {
|
||||
return selfPermissionValue("i_client_ban_max_bantime");
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the local client has enough power to assign {@code group}, or to take it
|
||||
* away when {@code assigned}: the add power always gates it, and the remove power
|
||||
* additionally gates a group already held. {@code -1} is TS3's "unlimited" sentinel on
|
||||
* either side: an unlimited local power always passes, and a group that needs
|
||||
* unlimited power can only be touched by a local client that has it.
|
||||
*/
|
||||
public synchronized boolean canToggleGroup(Group group, boolean assigned) {
|
||||
return hasPower(group.neededMemberAddPower, selfPermissionValue("i_group_member_add_power"))
|
||||
&& (!assigned || hasPower(group.neededMemberRemovePower,
|
||||
selfPermissionValue("i_group_member_remove_power")));
|
||||
public Permissions permissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
/** Every regular server group, with whether {@code client} is in it and whether we may change that. */
|
||||
@@ -268,7 +119,7 @@ public final class ServerModel {
|
||||
List<GroupChoice> out = new ArrayList<>();
|
||||
for (Group g : allServerGroups()) {
|
||||
boolean assigned = client.inServerGroup(g.id);
|
||||
out.add(new GroupChoice(g, assigned, canToggleGroup(g, assigned)));
|
||||
out.add(new GroupChoice(g, assigned, permissions.canToggleGroup(g, assigned)));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -278,7 +129,7 @@ public final class ServerModel {
|
||||
List<GroupChoice> out = new ArrayList<>();
|
||||
for (Group g : allChannelGroups()) {
|
||||
boolean assigned = client.channelGroupId == g.id;
|
||||
out.add(new GroupChoice(g, assigned, canToggleGroup(g, assigned)));
|
||||
out.add(new GroupChoice(g, assigned, permissions.canToggleGroup(g, assigned)));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -287,16 +138,11 @@ public final class ServerModel {
|
||||
public synchronized List<Group> privilegeKeyGroups(boolean channelGroups) {
|
||||
List<Group> out = new ArrayList<>();
|
||||
for (Group g : channelGroups ? allChannelGroups() : allServerGroups()) {
|
||||
if (canToggleGroup(g, false)) out.add(g);
|
||||
if (permissions.canToggleGroup(g, false)) out.add(g);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private static boolean hasPower(int needed, int own) {
|
||||
if (own == -1) return true;
|
||||
if (needed == -1) return false;
|
||||
return own >= needed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a channel shows in the tree at all — it doesn't while we are not subscribed
|
||||
|
||||
@@ -871,8 +871,8 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
* Asks the server to (re-)send its permission definitions, which arrive as a burst
|
||||
* of {@code notifypermissionlist} events — the only way to learn permission names,
|
||||
* since {@code notifyclientneededpermissions} (which resolves our own power for
|
||||
* them) only ever reports numeric ids. Needed for {@link ServerModel#selfPermissionValue}
|
||||
* to work; harmless if it's slow or fails, since group-assignment eligibility just
|
||||
* them) only ever reports numeric ids. Needed for {@link Permissions} to resolve
|
||||
* anything; harmless if it's slow or fails, since group-assignment eligibility just
|
||||
* won't be known yet.
|
||||
*/
|
||||
private void requestPermissionNames() {
|
||||
@@ -881,13 +881,13 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks for our own {@link ServerModel#UNREPORTED_PERMISSIONS}, which {@code notifyclientneededpermissions}
|
||||
* never carries. Asking needs {@code b_client_permissionoverview_own}; refused, we take them
|
||||
* as none, which is what the groups that lack the one usually have of the others.
|
||||
* Asks for our own {@link Permissions#UNREPORTED} ones, which {@code notifyclientneededpermissions}
|
||||
* never carries. Asking needs {@code b_client_permissionoverview_own}; refused, they stay
|
||||
* unknown and the actions they gate stay offered.
|
||||
*/
|
||||
void requestSelfPowers() {
|
||||
List<SingleCommand> rows = new ArrayList<>();
|
||||
for (String name : ServerModel.UNREPORTED_PERMISSIONS) {
|
||||
for (String name : Permissions.UNREPORTED) {
|
||||
rows.add(new SingleCommand("permget", ProtocolRole.CLIENT, new CommandSingleParameter("permsid", name)));
|
||||
}
|
||||
submit(sock -> {
|
||||
@@ -898,8 +898,8 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
String value = fields.get("permvalue");
|
||||
if (name != null && value != null) powers.put(name, Integer.parseInt(value));
|
||||
}
|
||||
model.setUnreportedPermissions(powers);
|
||||
}, e -> model.unreportedPermissionsRefused());
|
||||
model.permissions().setUnreported(powers);
|
||||
}, e -> model.permissions().unreportedRefused());
|
||||
}
|
||||
|
||||
/** Assigns or removes a server group for a client (by database id, as {@code servergroupaddclient} needs). */
|
||||
@@ -1288,11 +1288,6 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
return self == null ? 0 : self.unreadMessages;
|
||||
}
|
||||
|
||||
/** Whether the server lets us send offline messages. */
|
||||
public boolean canSendOfflineMessages() {
|
||||
return model.permits("b_client_offline_textmessage_send");
|
||||
}
|
||||
|
||||
/** Our unread count changed from {@code before}; more unread means new mail, which the log announces. */
|
||||
void unreadMessagesChanged(int before) {
|
||||
int unread = unreadOfflineMessages();
|
||||
@@ -1694,7 +1689,7 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
|
||||
/** How large an avatar this server lets us upload, in bytes. */
|
||||
public int maxAvatarBytes() {
|
||||
return avatarAdmin.maxUploadBytes();
|
||||
return model.permissions().maxAvatarBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,6 +12,7 @@ import eu.lixko.ts3j.client.contacts.ContactStore;
|
||||
import eu.lixko.ts3j.client.net.ChannelNode;
|
||||
import eu.lixko.ts3j.client.net.ClientEntry;
|
||||
import eu.lixko.ts3j.client.net.ConnectionListener;
|
||||
import eu.lixko.ts3j.client.net.Permissions;
|
||||
import eu.lixko.ts3j.client.net.Replies;
|
||||
import eu.lixko.ts3j.client.net.TeamspeakConnection;
|
||||
import eu.lixko.ts3j.client.net.Whisper;
|
||||
@@ -675,46 +676,9 @@ public final class ServerSession {
|
||||
|
||||
// ---- permissions ----
|
||||
|
||||
/** Whether the server grants us {@code b_client_ban_list}; offered while unknown. */
|
||||
public boolean canViewBanList() {
|
||||
return permits("b_client_ban_list");
|
||||
}
|
||||
|
||||
/** Whether the server grants us {@code b_client_complain_list}; offered while unknown. */
|
||||
public boolean canViewComplaints() {
|
||||
return permits("b_client_complain_list");
|
||||
}
|
||||
|
||||
/** Whether the server grants us {@code b_virtualserver_log_view}; offered while unknown. */
|
||||
public boolean canViewServerLog() {
|
||||
return permits("b_virtualserver_log_view");
|
||||
}
|
||||
|
||||
/** Whether the server grants us {@code b_virtualserver_token_list}; offered while unknown. */
|
||||
public boolean canListPrivilegeKeys() {
|
||||
return permits("b_virtualserver_token_list");
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the server lets us list channel files ({@code i_ft_file_browse_power}); offered while
|
||||
* unknown. A channel may still need more power than we have, which only the server knows.
|
||||
*/
|
||||
public boolean canBrowseFiles() {
|
||||
return permits("i_ft_file_browse_power");
|
||||
}
|
||||
|
||||
/** Whether the server lets us upload an avatar ({@code i_client_max_avatar_filesize}); offered while unknown. */
|
||||
public boolean canSetAvatar() {
|
||||
return permits("i_client_max_avatar_filesize");
|
||||
}
|
||||
|
||||
/** Whether the server lets us delete other clients' avatars; offered while unknown. */
|
||||
public boolean canDeleteOtherAvatars() {
|
||||
return permits("b_client_avatar_delete_other");
|
||||
}
|
||||
|
||||
private boolean permits(String permission) {
|
||||
return conn.isConnected() && conn.getModel().permits(permission);
|
||||
/** What we may do on this server; nothing while not connected. */
|
||||
public Permissions permissions() {
|
||||
return conn.isConnected() ? conn.getModel().permissions() : Permissions.NONE;
|
||||
}
|
||||
|
||||
/** Whether we publish an avatar on this server. */
|
||||
@@ -724,65 +688,10 @@ public final class ServerSession {
|
||||
return self != null && !self.avatarFlag.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we may ask for talk power: the server lets us ({@code b_client_request_talker}, offered
|
||||
* while unknown), and we lack it in our channel, as it refuses the request otherwise.
|
||||
*/
|
||||
/** Whether we may ask for talk power: the server lets us, and we lack it in our channel, as it refuses otherwise. */
|
||||
public boolean canRequestTalkPower() {
|
||||
ClientEntry self = self();
|
||||
return permits("b_client_request_talker") && self != null && conn.getModel().lacksTalkPower(self);
|
||||
}
|
||||
|
||||
/** Whether the server lets us grant and revoke talk power ({@code b_client_set_flag_talker}); offered while unknown. */
|
||||
public boolean canGrantTalkPower() {
|
||||
return permits("b_client_set_flag_talker");
|
||||
}
|
||||
|
||||
public boolean canMoveClients() {
|
||||
return hasPower("i_client_move_power");
|
||||
}
|
||||
|
||||
public boolean canKickFromChannel() {
|
||||
return hasPower("i_client_kick_from_channel_power");
|
||||
}
|
||||
|
||||
public boolean canKickFromServer() {
|
||||
return hasPower("i_client_kick_from_server_power");
|
||||
}
|
||||
|
||||
public boolean canBanClients() {
|
||||
return hasPower("i_client_ban_power");
|
||||
}
|
||||
|
||||
public boolean canCreateChannels() {
|
||||
return permitsAny("b_channel_create_temporary", "b_channel_create_semi_permanent",
|
||||
"b_channel_create_permanent");
|
||||
}
|
||||
|
||||
public boolean canEditChannels() {
|
||||
return permitsAny("b_channel_modify_name", "b_channel_modify_topic", "b_channel_modify_description",
|
||||
"b_channel_modify_password", "b_channel_modify_codec", "b_channel_modify_maxclients");
|
||||
}
|
||||
|
||||
public boolean canDeleteChannels() {
|
||||
return permitsAny("b_channel_delete_temporary", "b_channel_delete_semi_permanent",
|
||||
"b_channel_delete_permanent");
|
||||
}
|
||||
|
||||
private boolean permitsAny(String... permissions) {
|
||||
if (!conn.isConnected()) return false;
|
||||
for (String p : permissions) {
|
||||
if (conn.getModel().permits(p)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we have any of a power; whether it beats the client's needed power only the
|
||||
* server knows, so it is left to refuse.
|
||||
*/
|
||||
private boolean hasPower(String power) {
|
||||
return conn.isConnected() && conn.getModel().mayHavePower(power);
|
||||
return permissions().canRequestTalkPower() && self != null && conn.getModel().lacksTalkPower(self);
|
||||
}
|
||||
|
||||
// ---- connection events ----
|
||||
|
||||
Reference in New Issue
Block a user