// Admin / action operations extracted from Bridge. import type { Bridge } from './bridge'; import type { PacketInfo } from '../protocol/types'; import { protoDecode, protoEncode } from '../protobuf/decode'; import { sendOidbAndCheck, sendOidbAndDecode, resolveUserUid } from './bridge-oidb'; import { fetchHighwaySession, uploadHighwayHttp } from './highway/highway-client'; import { computeHashes, computeMd5, loadBinarySource } from './highway/utils'; import { buildSendElems } from './builders/element-builder'; import { parseMsgPush } from './handlers/msg-push-handler'; import type { ForwardNodePayload, MessageElement } from './events'; import { PushMsgSchema } from './proto/message'; import { RequestUtil } from './web/request-util'; import { getHonorListWebAPI, WebHonorType } from './web/group-honor'; import { OidbMuteMemberSchema, OidbMuteAllSchema, OidbKickMemberSchema, OidbLeaveGroupSchema, OidbFriendRequestActionSchema, OidbDeleteFriendSchema, OidbGroupRequestActionSchema, OidbPokeSchema, OidbEssenceSchema, OidbSetAdminSchema, OidbRenameMemberSchema, OidbRenameGroupSchema, OidbSpecialTitleSchema, OidbLikeSchema, OidbGroupReactionSchema, OidbSetFriendRemarkSchema, OidbGroupFileCountViewReqSchema, OidbGroupFileCountViewRespSchema, OidbGroupFileViewReqSchema, OidbGroupFileViewRespSchema, OidbGroupFileReqSchema, OidbGroupFileRespSchema, OidbGroupFileFolderReqSchema, OidbGroupFileFolderRespSchema, OidbPrivateFileDownloadReqSchema, OidbPrivateFileDownloadRespSchema, OidbPrivateFileUploadReqSchema, OidbPrivateFileUploadRespSchema, NTV2RichMediaReqSchema, NTV2RichMediaRespSchema, GroupRecallRequestSchema, C2CRecallRequestSchema, SsoReadedReportReqSchema, OidbClientKeyRespSchema, OidbClientKeyReqSchema, OidbGetPskeyReqSchema, OidbGetPskeyRespSchema } from './proto/oidb-action'; import { FileUploadExtSchema } from './proto/highway'; import { LongMsgResultSchema, RecvLongMsgReqSchema, RecvLongMsgRespSchema, SendLongMsgReqSchema, SendLongMsgRespSchema, } from './proto/longmsg'; import { gzipSync, gunzipSync } from 'zlib'; export interface GroupFileInfo { fileId: string; fileName: string; busId: number; fileSize: number; uploadTime: number; deadTime: number; modifyTime: number; downloadTimes: number; uploader: number; uploaderName: string; } export interface GroupFolderInfo { folderId: string; folderName: string; createTime: number; creator: number; creatorName: string; totalFileCount: number; } export interface GroupFilesResult { files: GroupFileInfo[]; folders: GroupFolderInfo[]; } export interface UploadFileResult { fileId: string | null; fileHash?: string | null; } export interface MediaIndexNode { info?: { fileSize?: number; fileHash?: string; fileSha1?: string; fileName?: string; width?: number; height?: number; time?: number; original?: number; type?: { type?: number; picFormat?: number; videoFormat?: number; voiceFormat?: number; }; }; fileUuid?: string; storeId?: number; uploadTime?: number; ttl?: number; subType?: number; } const forwardResCache = new Map(); function toInt(value: unknown): number { if (typeof value === 'number' && Number.isFinite(value)) return Math.trunc(value); if (typeof value === 'bigint') { const n = Number(value); return Number.isFinite(n) ? Math.trunc(n) : 0; } if (typeof value === 'string' && value.trim()) { const n = Number(value); return Number.isFinite(n) ? Math.trunc(n) : 0; } return 0; } function ensureRetCodeZero(operation: string, code: unknown, msg: unknown, wording: unknown): void { const retCode = toInt(code); if (retCode === 0) return; const text = (typeof wording === 'string' && wording) || (typeof msg === 'string' && msg) || 'unknown error'; throw new Error(`${operation} failed: code=${retCode} msg=${text}`); } function normalizeDirectory(dir?: string): string { if (!dir || !dir.trim()) return '/'; return dir; } function bytesToHexUpper(data: unknown): string { if (!(data instanceof Uint8Array) || data.length === 0) return ''; return Buffer.from(data).toString('hex').toUpperCase(); } function normalizeUploadFileName(name: string, fallback: string): string { const trimmed = name.trim(); if (trimmed) return trimmed; const safeFallback = fallback.trim(); return safeFallback || 'file.bin'; } function md5First10MB(bytes: Uint8Array): Uint8Array { const limit = Math.min(bytes.length, 10 * 1024 * 1024); return computeMd5(bytes.subarray(0, limit)); } function buildGroupFileUploadExt( senderUin: number, groupId: number, fileName: string, fileSize: number, md5: Uint8Array, fileId: string, uploadKey: Uint8Array, checkKey: Uint8Array, uploadHost: string, uploadPort: number, ): Uint8Array { return protoEncode({ unknown1: 100, unknown2: 1, entry: { busiBuff: { busId: 102, senderUin: BigInt(senderUin), receiverUin: BigInt(groupId), groupCode: BigInt(groupId), }, fileEntry: { fileSize: BigInt(Math.max(0, fileSize)), md5, md5S2: md5, checkKey, fileId, uploadKey, }, clientInfo: { clientType: 3, appId: '100', terminalType: 3, clientVer: '1.1.1', unknown: 4, }, fileNameInfo: { fileName, }, host: { hosts: [ { url: { host: uploadHost, unknown: 1, }, port: uploadPort, }, ], }, }, unknown200: 0, }, FileUploadExtSchema); } function buildPrivateFileUploadExt( senderUin: number, fileName: string, fileSize: number, md5: Uint8Array, sha1: Uint8Array, fileId: string, uploadKey: Uint8Array, uploadHost: string, uploadPort: number, ): Uint8Array { return protoEncode({ unknown1: 100, unknown2: 1, entry: { busiBuff: { busId: 102, senderUin: BigInt(senderUin), receiverUin: 0n, groupCode: 0n, }, fileEntry: { fileSize: BigInt(Math.max(0, fileSize)), md5, md5S2: md5, checkKey: sha1, fileId, uploadKey, }, clientInfo: { clientType: 3, appId: '100', terminalType: 3, clientVer: '1.1.1', unknown: 4, }, fileNameInfo: { fileName, }, host: { hosts: [ { url: { host: uploadHost, unknown: 1, }, port: uploadPort, }, ], }, }, unknown3: 0, unknown200: 1, }, FileUploadExtSchema); } async function resolveSelfUid(bridge: Bridge): Promise { let selfUid = bridge.qqInfo.selfUid; if (selfUid) return selfUid; const selfUin = toInt(bridge.qqInfo.uin); if (selfUin <= 0) { throw new Error('self uid is unavailable'); } selfUid = await resolveUserUid(bridge, selfUin); return selfUid; } function normalizeMediaNode(node: MediaIndexNode): Record { const fileUuid = typeof node.fileUuid === 'string' ? node.fileUuid : ''; if (!fileUuid) throw new Error('media node fileUuid is required'); const info = node.info ?? {}; const type = info.type ?? {}; return { info: { fileSize: toInt(info.fileSize), fileHash: typeof info.fileHash === 'string' ? info.fileHash : '', fileSha1: typeof info.fileSha1 === 'string' ? info.fileSha1 : '', fileName: typeof info.fileName === 'string' ? info.fileName : '', type: { type: toInt(type.type), picFormat: toInt(type.picFormat), videoFormat: toInt(type.videoFormat), voiceFormat: toInt(type.voiceFormat), }, width: toInt(info.width), height: toInt(info.height), time: toInt(info.time), original: toInt(info.original), }, fileUuid, storeId: toInt(node.storeId), uploadTime: toInt(node.uploadTime), ttl: toInt(node.ttl), subType: toInt(node.subType), }; } async function fetchNtv2DownloadUrl( bridge: Bridge, serviceCmd: string, oidbCmd: number, payload: Record, ): Promise { const resp = await sendOidbAndDecode( bridge, serviceCmd, oidbCmd, 200, payload, NTV2RichMediaReqSchema, NTV2RichMediaRespSchema, true, ); ensureRetCodeZero('ntv2 download', resp?.respHead?.retCode, resp?.respHead?.message, undefined); const domain = typeof resp?.download?.info?.domain === 'string' ? resp.download.info.domain : ''; const path = typeof resp?.download?.info?.urlPath === 'string' ? resp.download.info.urlPath : ''; const rKeyParam = typeof resp?.download?.rKeyParam === 'string' ? resp.download.rKeyParam : ''; if (!domain || !path) { throw new Error('ntv2 download response invalid'); } return `https://${domain}${path}${rKeyParam}`; } async function buildForwardPushBody( bridge: Bridge, node: ForwardNodePayload, ): Promise> { const fromUin = node.userUin > 0 ? node.userUin : toInt(bridge.qqInfo.uin); if (fromUin <= 0) throw new Error('forward node user uin is invalid'); const nickname = node.nickname.trim() || String(fromUin); const elems = await buildSendElems(node.elements); const now = Math.floor(Date.now() / 1000); const random = Math.floor(Math.random() * 0x7fffffff) >>> 0; const seq = Math.floor(Math.random() * 9000000) + 1000000; return { responseHead: { fromUin, toUid: bridge.qqInfo.selfUid ?? '', forward: { friendName: nickname, }, }, contentHead: { msgType: 9, subType: 4, msgId: random, sequence: seq, timestamp: now, divSeq: 0, }, body: { richText: { elems, }, }, }; } // --------------------------------------------------------------------------- // Friend actions // --------------------------------------------------------------------------- export async function setFriendRemark(bridge: Bridge, userId: number, remark: string): Promise { const uid = await resolveUserUid(bridge, userId); await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0xb6e_2', 0xB6E, 2, { targetUid: uid, remark }, OidbSetFriendRemarkSchema); } // --------------------------------------------------------------------------- // Group file count // --------------------------------------------------------------------------- export async function fetchGroupFileCount(bridge: Bridge, groupId: number): Promise<{ fileCount: number; maxCount: number }> { const resp = await sendOidbAndDecode(bridge, 'OidbSvcTrpcTcp.0x6d8_3', 0x6D8, 3, { count: { groupUin: groupId, appId: 7, busId: 0 } }, OidbGroupFileCountViewReqSchema, OidbGroupFileCountViewRespSchema); return { fileCount: toInt(resp?.count?.fileCount ?? 0), maxCount: toInt(resp?.count?.maxCount ?? 10000), }; } // --------------------------------------------------------------------------- // Group admin actions // --------------------------------------------------------------------------- export async function muteGroupMember(bridge: Bridge, groupId: number, userId: number, duration: number): Promise { const uid = await resolveUserUid(bridge, userId, groupId); await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x1253_1', 0x1253, 1, { groupUin: groupId, type: 1, body: { targetUid: uid, duration } }, OidbMuteMemberSchema); } export async function muteGroupAll(bridge: Bridge, groupId: number, enable: boolean): Promise { await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x89a_0', 0x89A, 0, { groupUin: groupId, muteState: { state: enable ? 0xFFFFFFFF : 0 } }, OidbMuteAllSchema); } export async function kickGroupMember(bridge: Bridge, groupId: number, userId: number, reject: boolean, reason = ''): Promise { const uid = await resolveUserUid(bridge, userId, groupId); await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x8a0_1', 0x8A0, 1, { groupUin: groupId, targetUid: uid, rejectAddRequest: reject, reason }, OidbKickMemberSchema); } export async function leaveGroup(bridge: Bridge, groupId: number): Promise { await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x1097_1', 0x1097, 1, { groupUin: groupId }, OidbLeaveGroupSchema); } export async function setGroupAdmin(bridge: Bridge, groupId: number, userId: number, enable: boolean): Promise { const uid = await resolveUserUid(bridge, userId, groupId); await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x1096_1', 0x1096, 1, { groupUin: groupId, uid, isAdmin: enable }, OidbSetAdminSchema); } export async function setGroupCard(bridge: Bridge, groupId: number, userId: number, card: string): Promise { const uid = await resolveUserUid(bridge, userId, groupId); await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x8fc_3', 0x8FC, 3, { groupUin: groupId, body: { targetUid: uid, targetName: card } }, OidbRenameMemberSchema); } export async function setGroupName(bridge: Bridge, groupId: number, name: string): Promise { await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x89a_15', 0x89A, 15, { groupUin: groupId, body: { targetName: name } }, OidbRenameGroupSchema); } export async function setGroupSpecialTitle(bridge: Bridge, groupId: number, userId: number, title: string): Promise { const uid = await resolveUserUid(bridge, userId, groupId); await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x8fc_2', 0x8FC, 2, { groupUin: groupId, body: { targetUid: uid, specialTitle: title, expireTime: -1 } }, OidbSpecialTitleSchema); } // --------------------------------------------------------------------------- // Friend / group add requests // --------------------------------------------------------------------------- export async function setFriendAddRequest(bridge: Bridge, uidOrFlag: string, approve: boolean): Promise { let targetUid = uidOrFlag; if (/^\d+$/.test(uidOrFlag)) { targetUid = await resolveUserUid(bridge, parseInt(uidOrFlag, 10)); } await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0xb5d_44', 0xB5D, 44, { accept: approve ? 3 : 5, targetUid }, OidbFriendRequestActionSchema); } export async function deleteFriend(bridge: Bridge, userId: number, block = false): Promise { const targetUid = await resolveUserUid(bridge, userId); await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x126b_0', 0x126B, 0, { field1: { targetUid, field2: { field1: 130, field2: 109, field3: { field1: 8, field2: 8, field3: 50, }, }, block, field4: false, }, }, OidbDeleteFriendSchema); // Refresh friend cache after deletion. try { await bridge.fetchFriendList(); } catch { /* ignore */ } } // --------------------------------------------------------------------------- // Group / private file operations // --------------------------------------------------------------------------- export async function uploadGroupFile( bridge: Bridge, groupId: number, source: string, name = '', folderId = '/', uploadFile = true, ): Promise { const loaded = await loadBinarySource(source, 'file'); if (!loaded.bytes.length) throw new Error('group file is empty'); const fileName = normalizeUploadFileName(name, loaded.fileName); const hashes = computeHashes(loaded.bytes); const resp = await sendOidbAndDecode( bridge, 'OidbSvcTrpcTcp.0x6d6_0', 0x6D6, 0, { file: { groupUin: groupId, appId: 4, busId: 102, entrance: 6, targetDirectory: normalizeDirectory(folderId), fileName, localDirectory: `/${fileName}`, fileSize: BigInt(loaded.bytes.length), fileSha1: hashes.sha1, fileSha3: new Uint8Array(0), fileMd5: hashes.md5, field15: true, }, }, OidbGroupFileReqSchema, OidbGroupFileRespSchema, true, ); const upload = resp?.upload; if (!upload) throw new Error('group file upload response missing'); ensureRetCodeZero('group file upload', upload.retCode, upload.retMsg, upload.clientWording); const fileId = typeof upload.fileId === 'string' && upload.fileId ? upload.fileId : null; if (!fileId) throw new Error('group file upload response missing file_id'); if (!upload.boolFileExist && uploadFile) { const senderUin = toInt(bridge.qqInfo.uin); if (senderUin <= 0) throw new Error('invalid self uin for group file upload'); const uploadHost = (typeof upload.uploadIp === 'string' && upload.uploadIp) || (typeof upload.serverDns === 'string' && upload.serverDns) || ''; const uploadPort = toInt(upload.uploadPort); if (!uploadHost || uploadPort <= 0) { throw new Error('group file upload host is invalid'); } const ext = buildGroupFileUploadExt( senderUin, groupId, fileName, loaded.bytes.length, hashes.md5, fileId, upload.fileKey instanceof Uint8Array ? upload.fileKey : new Uint8Array(0), upload.checkKey instanceof Uint8Array ? upload.checkKey : new Uint8Array(0), uploadHost, uploadPort, ); const session = await fetchHighwaySession(bridge); await uploadHighwayHttp(bridge, session, 71, loaded.bytes, hashes.md5, ext); } return { fileId }; } export async function uploadPrivateFile( bridge: Bridge, userId: number, source: string, name = '', uploadFile = true, ): Promise { const loaded = await loadBinarySource(source, 'file'); if (!loaded.bytes.length) throw new Error('private file is empty'); const targetUid = await resolveUserUid(bridge, userId); let selfUid = bridge.qqInfo.selfUid; if (!selfUid) { const selfUin = toInt(bridge.qqInfo.uin); if (selfUin > 0) { selfUid = await resolveUserUid(bridge, selfUin); } } if (!selfUid) throw new Error('self uid is unavailable'); const senderUin = toInt(bridge.qqInfo.uin); if (senderUin <= 0) throw new Error('invalid self uin for private file upload'); const fileName = normalizeUploadFileName(name, loaded.fileName); const hashes = computeHashes(loaded.bytes); const resp = await sendOidbAndDecode( bridge, 'OidbSvcTrpcTcp.0xe37_1700', 0xE37, 1700, { command: 1700, seq: 0, upload: { senderUid: selfUid, receiverUid: targetUid, fileSize: loaded.bytes.length, fileName, md510MCheckSum: md5First10MB(loaded.bytes), sha1CheckSum: hashes.sha1, localPath: '/', md5CheckSum: hashes.md5, sha3CheckSum: new Uint8Array(0), }, businessId: 3, clientType: 1, flagSupportMediaPlatform: 1, }, OidbPrivateFileUploadReqSchema, OidbPrivateFileUploadRespSchema, ); const upload = resp?.upload; if (!upload) throw new Error('private file upload response missing'); ensureRetCodeZero('private file upload', upload.retCode, upload.retMsg, undefined); const fileId = typeof upload.uuid === 'string' && upload.uuid ? upload.uuid : null; const fileHash = typeof upload.fileAddon === 'string' && upload.fileAddon ? upload.fileAddon : null; if (!fileId) throw new Error('private file upload response missing file_id'); if (!upload.boolFileExist && uploadFile) { const uploadHost = (typeof upload.uploadIp === 'string' && upload.uploadIp) || ''; const uploadPort = toInt(upload.uploadPort); if (!uploadHost || uploadPort <= 0) { throw new Error('private file upload host is invalid'); } const ext = buildPrivateFileUploadExt( senderUin, fileName, loaded.bytes.length, hashes.md5, hashes.sha1, fileId, upload.mediaPlatformUploadKey instanceof Uint8Array ? upload.mediaPlatformUploadKey : (upload.uploadKey instanceof Uint8Array ? upload.uploadKey : new Uint8Array(0)), uploadHost, uploadPort, ); const session = await fetchHighwaySession(bridge); await uploadHighwayHttp(bridge, session, 95, loaded.bytes, hashes.md5, ext); } return { fileId, fileHash }; } export async function fetchGroupFiles(bridge: Bridge, groupId: number, folderId = '/'): Promise { const targetDirectory = normalizeDirectory(folderId); const files: GroupFileInfo[] = []; const folders: GroupFolderInfo[] = []; const pageSize = 20; let startIndex = 0; for (let page = 0; page < 200; page++) { const resp = await sendOidbAndDecode( bridge, 'OidbSvcTrpcTcp.0x6d8_1', 0x6D8, 1, { list: { groupUin: groupId, appId: 7, targetDirectory, fileCount: pageSize, sortBy: 1, startIndex, field17: 2, field18: 0, }, }, OidbGroupFileViewReqSchema, OidbGroupFileViewRespSchema, true, ); const list = resp?.list; if (!list) break; ensureRetCodeZero('group file list', list.retCode, list.retMsg, list.clientWording); for (const item of list.items ?? []) { const type = toInt(item?.type); if (type === 1 && item?.fileInfo) { const file = item.fileInfo; const uploader = toInt(file.uploaderUin); const cached = bridge.qqInfo.findGroupMember(groupId, uploader); files.push({ fileId: typeof file.fileId === 'string' ? file.fileId : '', fileName: typeof file.fileName === 'string' ? file.fileName : '', busId: toInt(file.busId), fileSize: toInt(file.fileSize), uploadTime: toInt(file.uploadedTime), deadTime: toInt(file.expireTime), modifyTime: toInt(file.modifiedTime), downloadTimes: toInt(file.downloadedTimes), uploader, uploaderName: (typeof file.uploaderName === 'string' && file.uploaderName) || cached?.card || cached?.nickname || '', }); } else if (type === 2 && item?.folderInfo) { const folder = item.folderInfo; const creator = toInt(folder.creatorUin); const cached = bridge.qqInfo.findGroupMember(groupId, creator); folders.push({ folderId: typeof folder.folderId === 'string' ? folder.folderId : '', folderName: typeof folder.folderName === 'string' ? folder.folderName : '', createTime: toInt(folder.createTime), creator, creatorName: (typeof folder.creatorName === 'string' && folder.creatorName) || cached?.card || cached?.nickname || '', totalFileCount: toInt(folder.totalFileCount), }); } } if (list.isEnd) break; startIndex += pageSize; } return { files, folders }; } export async function fetchGroupFileUrl(bridge: Bridge, groupId: number, fileId: string, busId = 102): Promise { const resp = await sendOidbAndDecode( bridge, 'OidbSvcTrpcTcp.0x6d6_2', 0x6D6, 2, { download: { groupUin: groupId, appId: 7, busId, fileId, }, }, OidbGroupFileReqSchema, OidbGroupFileRespSchema, true, ); const download = resp?.download; if (!download) throw new Error('group file url response missing'); ensureRetCodeZero('group file url', download.retCode, download.retMsg, download.clientWording); const dns = (typeof download.downloadDns === 'string' && download.downloadDns) || (typeof download.downloadIp === 'string' && download.downloadIp) || ''; const hexUrl = bytesToHexUpper(download.downloadUrl); if (!dns || !hexUrl) { throw new Error('group file url response invalid'); } // Keep the same behavior as Lagrange: append file_id after ?fname= return `https://${dns}/ftn_handler/${hexUrl}/?fname=${fileId}`; } export async function deleteGroupFile(bridge: Bridge, groupId: number, fileId: string): Promise { const resp = await sendOidbAndDecode( bridge, 'OidbSvcTrpcTcp.0x6d6_3', 0x6D6, 3, { delete: { groupUin: groupId, busId: 102, fileId, }, }, OidbGroupFileReqSchema, OidbGroupFileRespSchema, true, ); const result = resp?.delete; if (!result) throw new Error('group file delete response missing'); ensureRetCodeZero('group file delete', result.retCode, result.retMsg, result.clientWording); } export async function moveGroupFile( bridge: Bridge, groupId: number, fileId: string, parentDirectory: string, targetDirectory: string, ): Promise { const resp = await sendOidbAndDecode( bridge, 'OidbSvcTrpcTcp.0x6d6_5', 0x6D6, 5, { move: { groupUin: groupId, appId: 7, busId: 102, fileId, parentDirectory, targetDirectory, }, }, OidbGroupFileReqSchema, OidbGroupFileRespSchema, true, ); const result = resp?.move; if (!result) throw new Error('group file move response missing'); ensureRetCodeZero('group file move', result.retCode, result.retMsg, result.clientWording); } export async function createGroupFileFolder(bridge: Bridge, groupId: number, name: string, parentId = '/'): Promise { const resp = await sendOidbAndDecode( bridge, 'OidbSvcTrpcTcp.0x6d7_0', 0x6D7, 0, { create: { groupUin: groupId, rootDirectory: normalizeDirectory(parentId), folderName: name, }, }, OidbGroupFileFolderReqSchema, OidbGroupFileFolderRespSchema, true, ); const result = resp?.create; if (!result) throw new Error('group folder create response missing'); ensureRetCodeZero('group folder create', result.retcode, result.retMsg, result.clientWording); } export async function deleteGroupFileFolder(bridge: Bridge, groupId: number, folderId: string): Promise { const resp = await sendOidbAndDecode( bridge, 'OidbSvcTrpcTcp.0x6d7_1', 0x6D7, 1, { delete: { groupUin: groupId, folderId, }, }, OidbGroupFileFolderReqSchema, OidbGroupFileFolderRespSchema, true, ); const result = resp?.delete; if (!result) throw new Error('group folder delete response missing'); ensureRetCodeZero('group folder delete', result.retcode, result.retMsg, result.clientWording); } export async function renameGroupFileFolder(bridge: Bridge, groupId: number, folderId: string, newFolderName: string): Promise { const resp = await sendOidbAndDecode( bridge, 'OidbSvcTrpcTcp.0x6d7_2', 0x6D7, 2, { rename: { groupUin: groupId, folderId, newFolderName, }, }, OidbGroupFileFolderReqSchema, OidbGroupFileFolderRespSchema, true, ); const result = resp?.rename; if (!result) throw new Error('group folder rename response missing'); ensureRetCodeZero('group folder rename', result.retcode, result.retMsg, result.clientWording); } export async function fetchPrivateFileUrl(bridge: Bridge, userId: number, fileId: string, fileHash: string): Promise { const uid = await resolveUserUid(bridge, userId); const resp = await sendOidbAndDecode( bridge, 'OidbSvcTrpcTcp.0xe37_1200', 0xE37, 1200, { subCommand: 1200, field2: 1, body: { receiverUid: uid, fileUuid: fileId, type: 2, fileHash, t2: 0, }, field101: 3, field102: 103, field200: 1, field99999: new Uint8Array([0xC0, 0x85, 0x2C, 0x01]), }, OidbPrivateFileDownloadReqSchema, OidbPrivateFileDownloadRespSchema, ); const result = resp?.body?.result; const server = typeof result?.server === 'string' ? result.server : ''; const port = toInt(result?.port); const url = typeof result?.url === 'string' ? result.url : ''; if (!server || !port || !url) { throw new Error('private file url response invalid'); } return `http://${server}:${port}${url}&isthumb=0`; } export async function fetchGroupPttUrlByNode(bridge: Bridge, groupId: number, node: MediaIndexNode): Promise { const normalizedNode = normalizeMediaNode(node); return fetchNtv2DownloadUrl(bridge, 'OidbSvcTrpcTcp.0x126e_200', 0x126E, { reqHead: { common: { requestId: 4, command: 200 }, scene: { requestType: 1, businessType: 3, sceneType: 2, group: { groupUin: groupId }, }, client: { agentType: 2 }, }, download: { node: normalizedNode, download: { video: { busiType: 0, sceneType: 0 } }, }, }); } export async function fetchPrivatePttUrlByNode(bridge: Bridge, node: MediaIndexNode): Promise { const selfUid = await resolveSelfUid(bridge); const normalizedNode = normalizeMediaNode(node); return fetchNtv2DownloadUrl(bridge, 'OidbSvcTrpcTcp.0x126d_200', 0x126D, { reqHead: { common: { requestId: 1, command: 200 }, scene: { requestType: 1, businessType: 3, sceneType: 1, c2c: { accountType: 2, targetUid: selfUid, }, }, client: { agentType: 2 }, }, download: { node: normalizedNode, download: { video: { busiType: 0, sceneType: 0 } }, }, }); } export async function fetchGroupVideoUrlByNode(bridge: Bridge, groupId: number, node: MediaIndexNode): Promise { const normalizedNode = normalizeMediaNode(node); return fetchNtv2DownloadUrl(bridge, 'OidbSvcTrpcTcp.0x11ea_200', 0x11EA, { reqHead: { common: { requestId: 1, command: 200 }, scene: { requestType: 2, businessType: 2, sceneType: 2, group: { groupUin: groupId }, }, client: { agentType: 2 }, }, download: { node: normalizedNode, download: { video: { busiType: 0, sceneType: 0 } }, }, }); } export async function fetchPrivateVideoUrlByNode(bridge: Bridge, node: MediaIndexNode): Promise { const selfUid = await resolveSelfUid(bridge); const normalizedNode = normalizeMediaNode(node); return fetchNtv2DownloadUrl(bridge, 'OidbSvcTrpcTcp.0x11e9_200', 0x11E9, { reqHead: { common: { requestId: 1, command: 200 }, scene: { requestType: 2, businessType: 2, sceneType: 1, c2c: { accountType: 2, targetUid: selfUid, }, }, client: { agentType: 2 }, }, download: { node: normalizedNode, download: { video: { busiType: 0, sceneType: 0 } }, }, }); } export async function uploadForwardNodes(bridge: Bridge, nodes: ForwardNodePayload[], groupId?: number): Promise { if (!Array.isArray(nodes) || nodes.length === 0) { throw new Error('forward nodes are required'); } const msgBody = await Promise.all(nodes.map(node => buildForwardPushBody(bridge, node))); const longMsgResult = protoEncode({ action: [ { actionCommand: 'MultiMsg', actionData: { msgBody: msgBody as any }, }, ], }, LongMsgResultSchema); const selfUid = await resolveSelfUid(bridge); const info: any = { type: groupId ? 3 : 1, uid: { uid: groupId ? String(groupId) : selfUid }, payload: gzipSync(Buffer.from(longMsgResult)), }; if (groupId) info.groupUin = groupId; const request = protoEncode({ info, settings: { field1: 4, field2: 1, field3: 7, field4: 0, }, }, SendLongMsgReqSchema); const result = await bridge.sendRawPacket('trpc.group.long_msg_interface.MsgService.SsoSendLongMsg', request); if (!result.success || !result.gotResponse || !result.responseData) { throw new Error(result.errorMessage || 'upload forward message failed'); } const resp = protoDecode(result.responseData, SendLongMsgRespSchema); const resId = typeof resp?.result?.resId === 'string' ? resp.result.resId : ''; if (!resId) { throw new Error('upload forward message response missing res_id'); } forwardResCache.set(resId, nodes.map(node => ({ userUin: node.userUin, nickname: node.nickname, elements: [...node.elements], }))); return resId; } export async function fetchForwardNodes(bridge: Bridge, resId: string): Promise { const cached = forwardResCache.get(resId); if (cached) { return cached.map(node => ({ userUin: node.userUin, nickname: node.nickname, elements: [...node.elements] })); } const selfUid = await resolveSelfUid(bridge); const request = protoEncode({ info: { uid: { uid: selfUid }, resId, acquire: true, }, settings: { field1: 2, field2: 0, field3: 0, field4: 0, }, }, RecvLongMsgReqSchema); const result = await bridge.sendRawPacket('trpc.group.long_msg_interface.MsgService.SsoRecvLongMsg', request); if (!result.success || !result.gotResponse || !result.responseData) { throw new Error(result.errorMessage || 'download forward message failed'); } const resp = protoDecode(result.responseData, RecvLongMsgRespSchema); const payload = resp?.result?.payload; if (!(payload instanceof Uint8Array) || payload.length === 0) { throw new Error('download forward message payload is empty'); } const inflate = gunzipSync(Buffer.from(payload)); const longMsg = protoDecode(inflate, LongMsgResultSchema); const action = longMsg?.action?.find((item: any) => item?.actionCommand === 'MultiMsg'); const msgBodyList = Array.isArray(action?.actionData?.msgBody) ? action.actionData.msgBody : []; const nodes: ForwardNodePayload[] = []; for (const msgBody of msgBodyList) { const wrapped = protoEncode({ message: msgBody }, PushMsgSchema); const pkt: PacketInfo = { pid: 0, uin: bridge.qqInfo.uin, serviceCmd: 'trpc.msg.olpush.OlPushService.MsgPush', seqId: 0, retCode: 0, fromClient: false, body: wrapped, }; const events = parseMsgPush(pkt, bridge.qqInfo); const event = events.find(e => e.kind === 'friend_message' || e.kind === 'group_message' || e.kind === 'temp_message'); if (!event) continue; if (event.kind === 'group_message') { nodes.push({ userUin: event.senderUin, nickname: event.senderCard || event.senderNick, elements: event.elements, }); } else if (event.kind === 'friend_message') { nodes.push({ userUin: event.senderUin, nickname: event.senderNick, elements: event.elements, }); } else { nodes.push({ userUin: event.senderUin, nickname: event.senderNick, elements: event.elements, }); } } if (nodes.length > 0) { forwardResCache.set(resId, nodes.map(node => ({ userUin: node.userUin, nickname: node.nickname, elements: [...node.elements] }))); } return nodes; } export async function setGroupAddRequest( bridge: Bridge, groupId: number, sequence: number, eventType: number, approve: boolean, reason = '', filtered = false, ): Promise { const subCmd = filtered ? 2 : 1; const cmd = filtered ? 'OidbSvcTrpcTcp.0x10c8_2' : 'OidbSvcTrpcTcp.0x10c8_1'; await sendOidbAndCheck(bridge, cmd, 0x10C8, subCmd, { accept: approve ? 1 : 2, body: { sequence: BigInt(sequence), eventType, groupUin: groupId, message: reason } }, OidbGroupRequestActionSchema, true); } // --------------------------------------------------------------------------- // Poke / Like // --------------------------------------------------------------------------- export async function sendPoke(bridge: Bridge, isGroup: boolean, peerUin: number, targetUin?: number): Promise { await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0xed3_1', 0xED3, 1, { uin: targetUin ?? peerUin, groupUin: isGroup ? peerUin : 0, friendUin: isGroup ? 0 : peerUin, ext: 0, }, OidbPokeSchema); } export async function sendLike(bridge: Bridge, userId: number, count: number): Promise { await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x7e5_104', 0x7E5, 104, { targetUin: userId, count }, OidbLikeSchema); } // --------------------------------------------------------------------------- // Essence / Reaction // --------------------------------------------------------------------------- export async function setGroupEssence(bridge: Bridge, groupId: number, sequence: number, random: number, enable: boolean): Promise { const subCmd = enable ? 1 : 2; const cmd = enable ? 'OidbSvcTrpcTcp.0xeac_1' : 'OidbSvcTrpcTcp.0xeac_2'; await sendOidbAndCheck(bridge, cmd, 0xEAC, subCmd, { groupUin: groupId, sequence, random }, OidbEssenceSchema); } export async function setGroupReaction(bridge: Bridge, groupId: number, sequence: number, code: string, isSet: boolean): Promise { const subCmd = isSet ? 1 : 2; const cmd = isSet ? 'OidbSvcTrpcTcp.0x9082_1' : 'OidbSvcTrpcTcp.0x9082_2'; await sendOidbAndCheck(bridge, cmd, 0x9082, subCmd, { groupUin: groupId, sequence, code }, OidbGroupReactionSchema); } // --------------------------------------------------------------------------- // Recall messages // --------------------------------------------------------------------------- export async function recallGroupMessage(bridge: Bridge, groupId: number, sequence: number): Promise { const request = protoEncode({ type: 1, groupUin: groupId, info: { sequence, random: 0, field3: 0 }, settings: { field1: 0 }, }, GroupRecallRequestSchema); const result = await bridge.sendRawPacket('trpc.msg.msg_svc.MsgService.SsoGroupRecallMsg', request); if (!result.success) throw new Error(result.errorMessage || 'recall group message failed'); } export async function recallPrivateMessage( bridge: Bridge, userUin: number, clientSeq: number, msgSeq: number, random: number, timestamp: number, ): Promise { const targetUid = await resolveUserUid(bridge, userUin); const request = protoEncode({ type: 1, targetUid, info: { clientSequence: clientSeq, random, messageId: BigInt((0x01000000 * 0x100000000) + random), timestamp, field5: 0, messageSequence: msgSeq, }, settings: { field1: false, field2: false }, field6: false, }, C2CRecallRequestSchema); const result = await bridge.sendRawPacket('trpc.msg.msg_svc.MsgService.SsoC2CRecallMsg', request); if (!result.success) throw new Error(result.errorMessage || 'recall private message failed'); } // --- Mark message as read --- export async function markPrivateMessageRead( bridge: Bridge, userId: number, msgSeq: number, timestamp: number = Math.floor(Date.now() / 1000) ): Promise { const uid = await resolveUserUid(bridge, userId); const request = protoEncode({ c2cList: [ { uid, lastReadTime: BigInt(timestamp), lastReadSeq: BigInt(msgSeq), } ] }, SsoReadedReportReqSchema); const result = await bridge.sendRawPacket('trpc.msg.msg_svc.MsgService.SsoReadedReport', request); if (!result.success) { throw new Error(result.errorMessage || 'mark private message read failed'); } } export async function markGroupMessageRead( bridge: Bridge, groupId: number, msgSeq: number ): Promise { const request = protoEncode({ groupList: [ { groupUin: BigInt(groupId), lastReadSeq: BigInt(msgSeq), } ] }, SsoReadedReportReqSchema); const result = await bridge.sendRawPacket('trpc.msg.msg_svc.MsgService.SsoReadedReport', request); if (!result.success) { throw new Error(result.errorMessage || 'mark group message read failed'); } } export async function forceFetchClientKey(bridge: Bridge) { const resp = await sendOidbAndDecode( bridge, 'OidbSvcTrpcTcp.0x102a_1', 0x102A, 1, {}, OidbClientKeyReqSchema, OidbClientKeyRespSchema ); const clientKey = resp?.clientKey || ''; const keyIndex = String(resp?.keyIndex || '19'); // 不知道是什么 return { clientKey, keyIndex, expireTime: String(resp?.expireTime || '1800') }; } export async function getPSkey(bridge: Bridge, domainList: string[]) { const resp = await sendOidbAndDecode( bridge, 'OidbSvcTrpcTcp.0x102a_0', 0x102A, 0, { domainList }, OidbGetPskeyReqSchema, OidbGetPskeyRespSchema ); const domainPskeyMap = new Map(); if (resp?.pskeyItems && Array.isArray(resp.pskeyItems)) { for (const item of resp.pskeyItems) { if (item.domain && item.pskey) { domainPskeyMap.set(item.domain, item.pskey); } } } return { domainPskeyMap, }; } export async function getCookies(bridge: Bridge, domain: string) { const ClientKeyData = await forceFetchClientKey(bridge); // 构造 ptlogin2 跳转 URL const requestUrl = 'https://ssl.ptlogin2.qq.com/jump?ptlang=1033&clientuin=' + bridge.qqInfo.uin + '&clientkey=' + ClientKeyData.clientKey + '&u1=https%3A%2F%2F' + domain + '%2F' + bridge.qqInfo.uin + '%2Finfocenter&keyindex=' + ClientKeyData.keyIndex; const data = await RequestUtil.HttpsGetCookies(requestUrl); if (!data['p_skey'] || data['p_skey'].length === 0) { try { const pskeyData = await getPSkey(bridge, [domain]); const pskey = pskeyData.domainPskeyMap.get(domain); if (pskey) { data['p_skey'] = pskey; } } catch { return data; } } return data; } export async function getGroupHonorInfo(bridge: Bridge, groupId: number, type: WebHonorType | string) { const groupCode = groupId.toString(); const cookieObject = await getCookies(bridge, 'qun.qq.com'); const honorInfo: any = { group_id: groupId, current_talkative: null, talkative_list: [], performer_list: [], legend_list: [], emotion_list: [], strong_newbie_list: [], }; if (type === WebHonorType.TALKATIVE || type === WebHonorType.ALL) { const talkativeList = await getHonorListWebAPI(cookieObject, groupCode, 1); if (talkativeList.length > 0) { honorInfo.current_talkative = talkativeList[0]; honorInfo.talkative_list = talkativeList; } } if (type === WebHonorType.PERFORMER || type === WebHonorType.ALL) { honorInfo.performer_list = await getHonorListWebAPI(cookieObject, groupCode, 2); } if (type === WebHonorType.LEGEND || type === WebHonorType.ALL) { honorInfo.legend_list = await getHonorListWebAPI(cookieObject, groupCode, 3); } if (type === WebHonorType.EMOTION || type === WebHonorType.ALL) { honorInfo.emotion_list = await getHonorListWebAPI(cookieObject, groupCode, 6); } return honorInfo; }