fix: show password protected channels

This commit is contained in:
illusia committed 2026-10-12 10:34:06 +08:00
1 parent ff508a8eb9
commit cec2a81549
23 files changed
+189 -27

No files matched your search

+28 -1
View File
@@ -1,6 +1,11 @@
import assert from "node:assert/strict";
import test from "node:test";
import { applyChannelCapacityNotification, parseChannelCapacities } from "./channel-capacity.js";
import {
applyChannelCapacityNotification,
applyChannelPasswordProtectionNotification,
parseChannelCapacities,
parseChannelPasswordProtections,
} from "./channel-capacity.js";
test("channel capacity rows keep finite limits and distinguish unlimited channels", () => {
const capacities = parseChannelCapacities([
@@ -35,3 +40,25 @@ test("channel capacity notifications merge edits and remove deleted channels", (
applyChannelCapacityNotification("notifychanneldeleted", { cid: "2" }, capacities);
assert.equal(capacities.has(2n), false);
});
test("channel password protection is parsed as a public boolean and tracks channel edits", () => {
const protections = parseChannelPasswordProtections([
{ cid: "1", channel_flag_password: "1" },
{ cid: "2", channel_flag_password: "0" },
{ cid: "3", channel_flag_password: "unknown" },
{ cid: "18446744073709551616", channel_flag_password: "1" },
]);
assert.deepEqual([...protections], [[1n, true], [2n, false]]);
assert.deepEqual(
applyChannelPasswordProtectionNotification("notifychanneledited", { cid: "1", channel_flag_password: "0" }, protections),
{ channelId: 1n, passwordProtected: false },
);
assert.deepEqual(
applyChannelPasswordProtectionNotification("notifychannelcreated", { cid: "4", channel_flag_password: "1" }, protections),
{ channelId: 4n, passwordProtected: true },
);
assert.equal(applyChannelPasswordProtectionNotification("notifychanneldeleted", { cid: "4" }, protections), null);
assert.equal(protections.has(4n), false);
assert.equal(protections.has(3n), false, "unknown flag values are not interpreted as unlocked");
});
+42
View File
@@ -3,6 +3,10 @@ export interface ChannelCapacity {
maxClientsLimited: boolean;
}
export interface ChannelPasswordProtection {
passwordProtected: boolean;
}
const UINT64_MAX = 18_446_744_073_709_551_615n;
function parseChannelId(value: string | undefined): bigint | null {
@@ -34,6 +38,44 @@ export function parseChannelCapacities(rows: readonly Record<string, string>[]):
return capacities;
}
/** The password flag reveals whether a password is required, never the password itself. */
export function parseChannelPasswordProtection(row: Record<string, string>): boolean | null {
if (row.channel_flag_password === "1") return true;
if (row.channel_flag_password === "0") return false;
return null;
}
export function parseChannelPasswordProtections(rows: readonly Record<string, string>[]): Map<bigint, boolean> {
const protections = new Map<bigint, boolean>();
for (const row of rows) {
const id = parseChannelId(row.cid);
const passwordProtected = parseChannelPasswordProtection(row);
if (id !== null && passwordProtected !== null) protections.set(id, passwordProtected);
}
return protections;
}
/** Keep the password-required flag current as channels are created or edited. */
export function applyChannelPasswordProtectionNotification(
name: string,
params: Record<string, string>,
protections: Map<bigint, boolean>,
): { channelId: bigint; passwordProtected: boolean } | null {
const id = parseChannelId(params.cid);
if (id === null) return null;
if (name === "notifychanneldeleted") {
protections.delete(id);
return null;
}
if (name !== "notifychannelcreated" && name !== "notifychanneledited") return null;
const passwordProtected = parseChannelPasswordProtection(params);
if (passwordProtected === null) return null;
protections.set(id, passwordProtected);
return { channelId: id, passwordProtected };
}
/** Keep capacity metadata in step with TeamSpeak's incremental channel events. */
export function applyChannelCapacityNotification(
name: string,
+2 -1
View File
@@ -5,7 +5,7 @@ import { mapChannelTree, normalizeDirectorySnapshot } from "./directory-view.js"
import { parseServerMessage } from "../shared/server-messages.js";
const directory = (): TSDirectorySnapshot => ({
channels: [{ id: 1n, parentID: 0n, order: 0n, name: "Lobby", description: "" }, { id: 18446744073709551615n, parentID: 1n, order: 1n, name: "Room", description: "", maxClients: 10, maxClientsLimited: true }],
channels: [{ id: 1n, parentID: 0n, order: 0n, name: "Lobby", description: "" }, { id: 18446744073709551615n, parentID: 1n, order: 1n, name: "Room", description: "", maxClients: 10, maxClientsLimited: true, passwordProtected: true }],
clients: [{ id: 2, channelID: 1n, nickname: "Member", uid: "uid-2", type: 0, serverGroups: [], away: true, inputMuted: true }],
});
@@ -38,5 +38,6 @@ test("directory presentation survives JSON transport with large IDs, member flag
assert.equal(decoded.channels[0]?.members?.[0]?.avatar, "data:image/png;base64,test");
assert.equal(decoded.channels[1]?.maxClients, 10);
assert.equal(decoded.channels[1]?.maxClientsLimited, true);
assert.equal(decoded.channels[1]?.passwordProtected, true);
assert.deepEqual(decoded.channels[1]?.members, []);
});
+1
View File
@@ -11,6 +11,7 @@ export function mapChannelTree(snapshot: TSDirectorySnapshot, avatarCache = new
...(typeof channel.maxClients === "number" && typeof channel.maxClientsLimited === "boolean"
? { maxClients: channel.maxClients, maxClientsLimited: channel.maxClientsLimited }
: {}),
...(typeof channel.passwordProtected === "boolean" ? { passwordProtected: channel.passwordProtected } : {}),
members: snapshot.clients
.filter((client) => client.channelID === channel.id)
.map((client) => {
+18
View File
@@ -98,6 +98,24 @@ test("channel changes preserve large identifiers and publish only actual create,
assert.equal(f.events.length, 0);
});
test("live channel password changes update and publish the lock state without a full tree refresh", t => {
const f = fixture(t);
f.client.emit("directorySnapshot", snapshot(member(1)));
f.coordinator.syncSelf(); f.phase.published = true;
f.client.emit("channelPasswordProtectionChanged", { channelId: 2n, passwordProtected: true });
assert.equal(f.state.channelTree.find(item => item.id === "2")?.passwordProtected, true);
let published = f.messages.at(-1);
assert.equal(published?.type, "channelList");
if (published?.type === "channelList") assert.equal(published.channels.find(item => item.id === "2")?.passwordProtected, true);
f.client.emit("channelPasswordProtectionChanged", { channelId: 2n, passwordProtected: false });
assert.equal(f.state.channelTree.find(item => item.id === "2")?.passwordProtected, false);
published = f.messages.at(-1);
assert.equal(published?.type, "channelList");
if (published?.type === "channelList") assert.equal(published.channels.find(item => item.id === "2")?.passwordProtected, false);
});
test("reset rejects backoff events and close detaches only owned listeners including captured callbacks", t => {
const f = fixture(t);
f.client.emit("directorySnapshot", snapshot(member(1), member(2)));
+9
View File
@@ -23,6 +23,7 @@ interface SdkEvents {
clientMoved: { id: number; targetChannelID?: bigint };
clientUpdated: TSDirectoryClient;
directoryClientsSnapshot: TSDirectoryClient[];
channelPasswordProtectionChanged: { channelId: bigint; passwordProtected: boolean };
rawNotification: TSRawNotification;
voiceData: TSVoiceData;
textMessage: TSChatMessage;
@@ -135,6 +136,14 @@ export class SessionEventCoordinator {
this.refresh();
this.publishDirectory();
});
this.listenDirectory("channelPasswordProtectionChanged", change => {
const channelId = String(change.channelId);
if (!state.channelTree.some(channel => channel.id === channelId)) return;
state.channelTree = state.channelTree.map(channel => channel.id === channelId
? { ...channel, passwordProtected: change.passwordProtected }
: channel);
this.publishDirectory();
});
this.listen("rawNotification", options.onNotification);
this.listen("voiceData", options.onVoice);
this.listen("textMessage", message => {
+13 -3
View File
@@ -313,14 +313,14 @@ test("a current avatar download preserves its bytes and rejects a mismatched ide
assert.equal(f.sdk.downloaded, 1);
});
test("public channel capacity survives directory snapshots and follows channel edits", () => {
test("public channel metadata survives directory snapshots and follows channel edits", () => {
const f = fixture();
const snapshots: Array<{ channels: Array<{ id: bigint; maxClients?: number; maxClientsLimited?: boolean }>; clients: unknown[] }> = [];
const snapshots: Array<{ channels: Array<{ id: bigint; maxClients?: number; maxClientsLimited?: boolean; passwordProtected?: boolean }>; clients: unknown[] }> = [];
f.client.on("directorySnapshot", snapshot => snapshots.push(snapshot));
f.sdk.emit("rawNotification", {
name: "notifychannelcreated",
params: { cid: "1", channel_maxclients: "10", channel_flag_maxclients_unlimited: "0" },
params: { cid: "1", channel_maxclients: "10", channel_flag_maxclients_unlimited: "0", channel_flag_password: "1" },
});
f.sdk.emit("directorySnapshot", {
channels: [{ id: 1n, parentID: 0n, order: 0n, name: "Room", description: "" }],
@@ -328,6 +328,7 @@ test("public channel capacity survives directory snapshots and follows channel e
});
assert.equal(snapshots.at(-1)?.channels[0]?.maxClients, 10);
assert.equal(snapshots.at(-1)?.channels[0]?.maxClientsLimited, true);
assert.equal(snapshots.at(-1)?.channels[0]?.passwordProtected, true);
f.sdk.emit("rawNotification", { name: "notifychanneledited", params: { cid: "1", channel_maxclients: "12" } });
f.sdk.emit("directorySnapshot", {
@@ -336,6 +337,14 @@ test("public channel capacity survives directory snapshots and follows channel e
});
assert.equal(snapshots.at(-1)?.channels[0]?.maxClients, 12);
assert.equal(snapshots.at(-1)?.channels[0]?.maxClientsLimited, true);
assert.equal(snapshots.at(-1)?.channels[0]?.passwordProtected, true, "partial edits retain the previously reported password flag");
f.sdk.emit("rawNotification", { name: "notifychanneledited", params: { cid: "1", channel_flag_password: "0" } });
f.sdk.emit("directorySnapshot", {
channels: [{ id: 1n, parentID: 0n, order: 0n, name: "Room", description: "" }],
clients: [],
});
assert.equal(snapshots.at(-1)?.channels[0]?.passwordProtected, false);
f.sdk.emit("rawNotification", { name: "notifychanneldeleted", params: { cid: "1" } });
f.sdk.emit("directorySnapshot", {
@@ -343,6 +352,7 @@ test("public channel capacity survives directory snapshots and follows channel e
clients: [],
});
assert.equal(snapshots.at(-1)?.channels[0]?.maxClients, undefined);
assert.equal(snapshots.at(-1)?.channels[0]?.passwordProtected, undefined);
});
test("a retired SDK client's disconnect cannot invalidate a current avatar request", async () => {
+36 -5
View File
@@ -20,7 +20,14 @@ import { describeTeamSpeakError, normalizeTeamSpeakError, normalizeTeamSpeakKick
import { TeamSpeakAdapter, type TeamSpeakProtocol } from "./teamspeak-adapter.js";
import type { TeamSpeakTarget } from "../domain/teamspeak-target.js";
import { createAccelerationRelayClient, type AccelerationRelayClient, type AccelerationRelayOptions } from "./acceleration-relay.js";
import { applyChannelCapacityNotification, parseChannelCapacities, type ChannelCapacity } from "./channel-capacity.js";
import {
applyChannelCapacityNotification,
applyChannelPasswordProtectionNotification,
parseChannelCapacities,
parseChannelPasswordProtections,
type ChannelCapacity,
type ChannelPasswordProtection,
} from "./channel-capacity.js";
export interface TSClientOptions {
target: TeamSpeakTarget;
@@ -62,7 +69,7 @@ const MAX_SERVER_ICON_BYTES = 128 * 1024;
const MAX_SERVER_BANNER_URL_LENGTH = 2_048;
const MAX_SERVER_WELCOME_MESSAGE_LENGTH = 4_096;
export type TSDirectoryChannel = DirectorySnapshot["channels"][number] & Partial<ChannelCapacity>;
export type TSDirectoryChannel = DirectorySnapshot["channels"][number] & Partial<ChannelCapacity> & Partial<ChannelPasswordProtection>;
export type TSDirectorySnapshot = Omit<DirectorySnapshot, "channels"> & { channels: TSDirectoryChannel[] };
export type TSDirectoryClient = DirectoryClientInfo;
export type TSRawNotification = RawNotification;
@@ -108,6 +115,7 @@ export class TSClient extends EventEmitter {
private preferredChannelId = 0n;
private accelerationClient: AccelerationRelayClient | null = null;
private readonly channelCapacities = new Map<bigint, ChannelCapacity>();
private readonly channelPasswordProtection = new Map<bigint, boolean>();
// Reason id of the most recent self leave, kept so the SDK `kicked` event can
// tell a plain kick (4) from a kick with ban (5). The SDK only forwards the
// reason message to its `kicked` handler, not the reason id.
@@ -126,6 +134,7 @@ export class TSClient extends EventEmitter {
this.inputStateQueue = Promise.resolve();
this.selfLeaveReasonId = null;
this.channelCapacities.clear();
this.channelPasswordProtection.clear();
if (!this.adapter || !this.client) {
let transportTarget = this.options.target;
if (this.options.acceleration && !this.accelerationClient) {
@@ -184,14 +193,19 @@ export class TSClient extends EventEmitter {
// The high-level SDK directory type intentionally omits channel limit
// metadata. Its raw client-protocol rows still include the public
// maximum-client properties, so retain those without another SDK API.
const rows = await client.execCommandWithResponse("channellist", 5_000);
// TeamSpeak only includes channel flag fields (including the public
// password-required bit) when the client asks for flags explicitly.
const rows = await client.execCommandWithResponse("channellist -flags", 5_000);
const capacities = parseChannelCapacities(rows);
for (const [id, capacity] of capacities) this.channelCapacities.set(id, capacity);
for (const [id, passwordProtected] of parseChannelPasswordProtections(rows)) {
this.channelPasswordProtection.set(id, passwordProtected);
}
} catch (error: unknown) {
this.logger.warn({ err: error instanceof Error ? error.message : String(error) }, "Could not read TeamSpeak channel limits");
}
this.emit("directorySnapshot", {
channels: channels.map((channel) => ({ ...channel, ...this.channelCapacities.get(channel.id) })),
channels: this.applyChannelMetadata(channels),
clients,
});
} catch (error: unknown) {
@@ -356,7 +370,7 @@ export class TSClient extends EventEmitter {
client.on("directorySnapshot", (snapshot) => {
if (this.client !== client) return;
this.emit("directorySnapshot", {
channels: snapshot.channels.map((channel) => ({ ...channel, ...this.channelCapacities.get(channel.id) })),
channels: this.applyChannelMetadata(snapshot.channels),
clients: snapshot.clients.slice(),
});
});
@@ -364,6 +378,12 @@ export class TSClient extends EventEmitter {
client.on("rawNotification", (notification: RawNotification) => {
if (this.client !== client) return;
applyChannelCapacityNotification(notification.name, notification.params, this.channelCapacities);
const protectionChange = applyChannelPasswordProtectionNotification(
notification.name,
notification.params,
this.channelPasswordProtection,
);
if (protectionChange) this.emit("channelPasswordProtectionChanged", protectionChange);
this.emit("rawNotification", {
name: notification.name,
params: { ...notification.params },
@@ -395,6 +415,7 @@ export class TSClient extends EventEmitter {
this.connected = false;
this.clientId = 0;
this.channelCapacities.clear();
this.channelPasswordProtection.clear();
this.emit("disconnected", err);
});
@@ -590,6 +611,16 @@ export class TSClient extends EventEmitter {
this.clientId = 0;
}
}
private applyChannelMetadata(channels: readonly TSDirectoryChannel[]): TSDirectoryChannel[] {
return channels.map((channel) => ({
...channel,
...this.channelCapacities.get(channel.id),
...(this.channelPasswordProtection.has(channel.id)
? { passwordProtected: this.channelPasswordProtection.get(channel.id)! }
: {}),
}));
}
}
function normalizeTeamSpeakBannerUrl(value: unknown): string | null {