diff --git a/packages/proto-defs/src/action.ts b/packages/proto-defs/src/action.ts index b97be067..9e58823c 100644 --- a/packages/proto-defs/src/action.ts +++ b/packages/proto-defs/src/action.ts @@ -93,7 +93,17 @@ export interface MentionExtraSend { uid?: pb<9, string>; } -// MarkdownData +// Markdown commonElem (svc=45). Official DecodeMarkdownElement reads +// content / process_msg / summary / extType / mdExtInfo. +// extType=1 dispatches to DecodeMdExtInfoFileTransfer. +export interface MarkdownFlashTransferExt { + filesetId?: pb<1, string>; + name?: pb<2, string>; + fileSize?: pb<3, uint_32>; + thumbnail?: pb<4, bytes>; + expireTime?: pb<6, uint_32>; +} + export interface MarkdownData { content?: pb<1, string>; field2?: pb<2, string>; @@ -101,5 +111,5 @@ export interface MarkdownData { processMsg?: pb<4, string>; summary?: pb<5, string>; extType?: pb<6, uint_32>; - field7?: pb<7, bytes>; + extInfo?: pb<7, MarkdownFlashTransferExt>; } diff --git a/packages/protocol/src/element-manifest.ts b/packages/protocol/src/element-manifest.ts index bf026eda..0cdef7af 100644 --- a/packages/protocol/src/element-manifest.ts +++ b/packages/protocol/src/element-manifest.ts @@ -215,7 +215,7 @@ export const ELEMENT_MANIFEST = { directions: { D: 'yes', S: 'yes', P: 'by-design-no', W: 'by-design-no' }, fields: fieldsFor<'flash_file'>()(['filesetId', 'sceneType', 'fileName']), requiredFields: ['filesetId'], - note: '闪传文件(#199/#200):旧客户端(≤9.9.30)的 richui markdown 卡片才有;仅收侧解码上报,发送走 send_flash_msg 动作,故 P/W 按设计不支持', + note: '闪传文件:收侧解码 markdown commonElem(旧卡 JSON data.fileSetId,现网 extType=1/extInfo + open_fileset scheme,#199/#200/#358);发送走 send_flash_msg,故 P/W 按设计不支持', }, } as const satisfies ManifestShape; diff --git a/packages/protocol/src/msg-push/rich-body-decoder.ts b/packages/protocol/src/msg-push/rich-body-decoder.ts index 554b81c8..ed6cb695 100644 --- a/packages/protocol/src/msg-push/rich-body-decoder.ts +++ b/packages/protocol/src/msg-push/rich-body-decoder.ts @@ -228,15 +228,14 @@ function decodeMarkdownCommonElement(pbElem: Uint8Array, businessType: number): () => protobuf_decode(pbElem), ); const content = md?.content ?? ''; - if (!content) return null; - - // Business type 3 is also used by the old flash-transfer richui card. Keep - // that semantic element instead of exposing the transport markdown. If a - // payload identifies itself as FlashTransfer but is malformed, fail open to - // the sibling compatibility text rather than misreporting it as markdown. - if (businessType === 3 && content.includes('FlashTransfer')) { - return decodeFlashTransferMarkdown(content); + // Business type 3 carries the 闪传 card. Current NT puts fileset identity + // on extType=1 / extInfo (DecodeMdExtInfoFileTransfer); older cards only + // have it inside the richui JSON. If the payload looks like FlashTransfer + // but has no fileset, fail open to the sibling compatibility text. + if (md && businessType === 3 && isFlashTransferMarkdown(md, content)) { + return decodeFlashTransfer(md); } + if (!content) return null; return { type: 'markdown', text: content }; } @@ -487,7 +486,9 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] { const decodedInlineKeyboards = decodeInlineKeyboardsOnce(elems); const hasRichContent = [...decodedCards.values()].some((cards) => ( Boolean(cards.rich?.element || cards.light?.element) - )) || [...decodedMarkdown.values()].some((element) => element?.type === 'markdown'); + )) || [...decodedMarkdown.values()].some((element) => ( + element?.type === 'markdown' || element?.type === 'flash_file' + )); // QQ NT serializes a service-37 big face as the CommonElem followed by a // compatibility TextElem. The latter repeats QFaceExtra.text in `str`, while // field 12 contains a nested TextElem whose `str` is @@ -940,14 +941,14 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] { } /** - * Decode the content of a 闪传 richui markdown commonElem (svc=45) into a - * `flash_file` element. The `MarkdownData.content` is a markdown link - * `[闪传](mqqapi://markdown/node?nodeType=richui&json=)`; the - * JSON's `busId` is `FlashTransfer`. Field NAMES (fileSetId / sceneType / - * title) were confirmed against QQ NT's flash-transfer manager in - * wrapper.node.i64; the exact nesting is unknown (the card is built by the - * sender's client), so we search recursively rather than pin a path. See - * #199 / #200. + * Decode a 闪传 markdown commonElem (svc=45, biz=3) into `flash_file`. + * + * Current NT (DecodeMarkdownElement + DecodeMdExtInfoFileTransfer, #358): + * extType=1, extInfo.filesetId / name; click scheme also carries + * `mqqrouter://flash_transfer/open_fileset?fileset_id=`. + * Older cards (#199/#200): only the richui JSON `data.fileSetId`. + * Field names on the JSON card are searched recursively because the + * sender builds that blob; extInfo tags are fixed. */ function deepFindValue(obj: unknown, keys: readonly string[], depth = 0): unknown { if (depth > 8 || obj === null || typeof obj !== 'object') return undefined; @@ -963,28 +964,117 @@ function deepFindValue(obj: unknown, keys: readonly string[], depth = 0): unknow return undefined; } -function decodeFlashTransferMarkdown(content: string): MessageElement | null { - const m = content.match(/[?&]json=([^)\s]+)/); - if (!m) return null; - let obj: unknown; +function isFlashTransferMarkdown(md: MarkdownData | null | undefined, content: string): boolean { + if (md?.extType === 1 && md.extInfo?.filesetId) return true; + return content.includes('FlashTransfer') || content.includes('flash_transfer'); +} + +function parseQueryValue(text: string, key: string): string { + const m = text.match(new RegExp(`[?&]${key}=([^&\\s"']+)`)); + if (!m) return ''; try { - obj = JSON.parse(decodeURIComponent(m[1])); + return decodeURIComponent(m[1]).trim(); } catch { + return m[1].trim(); + } +} + +function collectSchemeFields(obj: unknown): { filesetId: string; sceneType: number | undefined } { + const schemes: string[] = []; + const walk = (value: unknown, depth: number): void => { + if (depth > 8 || value == null) return; + if (typeof value === 'string') { + if (value.includes('fileset_id=') || value.includes('open_fileset')) schemes.push(value); + return; + } + if (typeof value === 'object') { + for (const child of Object.values(value as Record)) walk(child, depth + 1); + } + }; + walk(obj, 0); + for (const scheme of schemes) { + const filesetId = parseQueryValue(scheme, 'fileset_id'); + if (!filesetId) continue; + const rawScene = parseQueryValue(scheme, 'scene_type'); + const n = Number(rawScene); + return { + filesetId, + sceneType: rawScene && Number.isSafeInteger(n) && n >= 0 ? n : undefined, + }; + } + return { filesetId: '', sceneType: undefined }; +} + +function parseFlashTransferCard(content: string): { + filesetId: string; + fileName: string; + sceneType: number | undefined; +} | null { + const m = content.match(/[?&]json=([^)\s]+)/); + let obj: unknown; + let decodedJson = ''; + if (m) { + try { + decodedJson = decodeURIComponent(m[1]); + } catch { + decodedJson = m[1]; + } + try { + obj = JSON.parse(decodedJson); + } catch { + obj = undefined; + } + } + + const fromKeys = obj + ? String(deepFindValue(obj, ['fileSetId', 'filesetId', 'fileset_id', 'file_set_id']) ?? '').trim() + : ''; + const fromScheme = obj ? collectSchemeFields(obj) : { filesetId: '', sceneType: undefined }; + const fromRaw = { + filesetId: parseQueryValue(decodedJson, 'fileset_id'), + sceneType: Number(parseQueryValue(decodedJson, 'scene_type')), + }; + const filesetId = fromKeys || fromScheme.filesetId || fromRaw.filesetId; + if (!filesetId && obj && deepFindValue(obj, ['busId']) !== 'FlashTransfer' && !content.includes('flash_transfer')) { return null; } - if (deepFindValue(obj, ['busId']) !== 'FlashTransfer') return null; - const filesetId = deepFindValue(obj, ['fileSetId', 'filesetId', 'fileset_id', 'file_set_id']); - const title = deepFindValue(obj, ['title', 'fileName', 'name']); - const sceneType = deepFindValue(obj, ['sceneType', 'scene_type']); - const normalizedFilesetId = filesetId != null ? String(filesetId).trim() : ''; - if (!normalizedFilesetId) return null; - const normalizedSceneType = sceneType == null ? 0 : Number(sceneType); - if (!Number.isSafeInteger(normalizedSceneType) || normalizedSceneType < 0) return null; + + const title = obj ? deepFindValue(obj, ['title', 'fileName', 'name']) : undefined; + const rawScene = obj ? deepFindValue(obj, ['sceneType', 'scene_type']) : undefined; + let sceneType: number | undefined; + if (rawScene != null && rawScene !== '') { + const n = Number(rawScene); + if (Number.isSafeInteger(n) && n >= 0) sceneType = n; + } + if (sceneType == null) sceneType = fromScheme.sceneType; + if (sceneType == null && Number.isSafeInteger(fromRaw.sceneType) && fromRaw.sceneType >= 0 && parseQueryValue(decodedJson, 'scene_type')) { + sceneType = fromRaw.sceneType; + } + + return { + filesetId, + fileName: title != null ? String(title) : '', + sceneType, + }; +} + +function decodeFlashTransfer(md: MarkdownData): MessageElement | null { + const ext = md.extType === 1 ? md.extInfo : undefined; + const card = parseFlashTransferCard(md.content ?? ''); + const filesetId = (ext?.filesetId ?? card?.filesetId ?? '').trim(); + if (!filesetId) return null; + + let fileName = (ext?.name ?? '').trim(); + if (!fileName) fileName = (card?.fileName ?? '').trim(); + if (!fileName && md.summary) { + fileName = md.summary.replace(/^\[QQ闪传\]\s*/, '').trim(); + } + return { type: 'flash_file', - filesetId: normalizedFilesetId, - fileName: title != null ? String(title) : '', - sceneType: normalizedSceneType, + filesetId, + fileName, + sceneType: card?.sceneType ?? 0, }; } diff --git a/packages/protocol/tests/msg-push/fixtures/issue-358-flash-push.hex b/packages/protocol/tests/msg-push/fixtures/issue-358-flash-push.hex new file mode 100644 index 00000000..88765f88 --- /dev/null +++ b/packages/protocol/tests/msg-push/fixtures/issue-358-flash-push.hex @@ -0,0 +1 @@ +0ad2190a43088dc2daaf0d1218755f5f4b593230365877546767686e5857376954796a4d6718aa1928bec6acea0c3218755f74676a704f715241555444556f784775433953375551123508a601180b2080c4e2e40d28d32230efe2f5d30638015883016080c4e2e48d80808001708201e001d3228002cfb398af8beee6cb711ad3180ad0180a21080010efe2f5d3061880c4e2e40d2000280a300038860140024a06e5ae8be4bd13129916aa039516082d128e160ac5145be997aae4bca05d286d71716170693a2f2f6d61726b646f776e2f6e6f64653f6e6f6465547970653d726963687569266a736f6e3d2537422532326275734964253232253341253232466c6173685472616e7366657225323225324325323274656d706c6174654964253232253341253232666c61736825323225324325323276657273696f6e253232253341322532432532326c61796f7574253232253341253742253232766965774964253232253341253232666c6173685f66696c6525323225324325323277696474682532322533412d322532432532326865696768742532322533412d32253243253232646972656374696f6e253232253341253232686f72697a6f6e74616c2532322532432532326c61796f757425323225334125354225374225323276696577496425323225334125323270726f67726573734c656674253232253243253232766965775479706525323225334125323263697263756c617250726f67726573732532322532432532326865696768742532322533413238253243253232776964746825323225334132382532432532326d617267696e5269676874253232253341382532432532326772617669747925323225334125323263656e746572566572746963616c25323225374425324325374225323276696577496425323225334125323266696c65253232253243253232646972656374696f6e253232253341253232766572746963616c2532322532432532326865696768742532322533412d3225324325323277696474682532322533413236332532432532326c61796f7574253232253341253542253742253232766965774964253232253341253232696d6167652532322532432532327669657754797065253232253341253232696d61676525323225324325323277696474682532322533412d312532432532326865696768742532322533413138302537442532432537422532327669657749642532322533412532327469746c6525323225324325323276696577547970652532322533412532327465787425323225324325323277696474682532322533412d312532432532326865696768742532322533412d322532432532326d617267696e546f7025323225334131322532432532326d617267696e4c65667425323225334131322532432532326d617267696e526967687425323225334131322537442532432537422532327669657749642532322533412532326465736325323225324325323276696577547970652532322533412532327465787425323225324325323277696474682532322533412d312532432532326865696768742532322533412d322532432532326d617267696e4c65667425323225334131322532432532326d617267696e546f70253232253341342532432532326d617267696e526967687425323225334131322537442532432537422532327669657749642532322533412532326469766964657225323225324325323277696474682532322533412d31253243253232686569676874253232253341302e352532432532326d617267696e546f7025323225334131332537442532432537422532327669657749642532322533412532327461696c253232253243253232646972656374696f6e253232253341253232686f72697a6f6e74616c25323225324325323277696474682532322533412d3125324325323268656967687425323225334132322532432532326d617267696e4c65667425323225334131322532432532326c61796f75742532322533412535422537422532327669657749642532322533412532327461696c49636f6e2532322532432532327669657754797065253232253341253232696d6167652532322532432532327769647468253232253341313225324325323268656967687425323225334131322532432532326772617669747925323225334125323263656e746572566572746963616c2532322537442532432537422532327669657749642532322533412532327461696c5465787425323225324325323276696577547970652532322533412532327465787425323225324325323277696474682532322533412d322532432532326865696768742532322533412d322532432532326772617669747925323225334125323263656e746572566572746963616c2532322532432532326d617267696e4c6566742532322533413425374425354425374425354425374425324325374225323276696577496425323225334125323270726f67726573735269676874253232253243253232766965775479706525323225334125323263697263756c617250726f67726573732532322532432532326865696768742532322533413238253243253232776964746825323225334132382532432532326d617267696e4c656674253232253341382532432532326772617669747925323225334125323263656e746572566572746963616c25323225374425354425374425324325323261747472696275746573253232253341253742253232766965774964253232253341253232666c6173685f66696c652532322532432532326174747269627574657325323225334125354225374225323276696577496425323225334125323270726f67726573734c65667425323225324325323270726f67726573732532322533413025324325323273746174652532322533412532326e6f6e652532322532432532326576656e74253232253341253742253232696e697425323225334125374225323276697369626c6543747225323225334125374225323276697369626c652532322533412532322532342532346c65667450726f677265737356697369626c6525323225324325323276697369626c65426568617665253232253341253232676f6e652532322532432532327372632532322533412532323125323225374425374425374425374425324325374225323276696577496425323225334125323266696c652532322532432532327261646975732532322533413130253243253232736368656d612532322533412532326d7171726f75746572253341253246253246666c6173685f7472616e736665722532466f70656e5f66696c6573657425334666696c657365745f69642533445b555549445d253543753030323676657273696f6e2533443125354375303032366368616e6e656c5f69642533443125354375303032367372635f74797065253344696e7465726e616c25354375303032367363656e655f74797065253344312532322532432532326261636b67726f756e64436f6c6f72253232253341253232627562626c655f67756573742532322532432532326576656e74253232253341253742253232696e6974253232253341253742253232726573657457696474682532322533412537422532327769647468253232253341253232253234253234776964746825323225374425374425374425324325323261747472696275746573253232253341253542253742253232766965774964253232253341253232696d6167652532322532432532327372632532322533412532325b454e434f4445445f46494c455f55524c5d122d080110efe2f5d3061a205b905a688f9df43a63efad2259e57cf8a3434019631eabc33000a893aad0e9d220aa191a0218012a195b5151e997aae4bca05d2063707764e688aae59bbe2e7a697030013a740a065b555549445d120e63707764e688aae59bbe2e7a69701897a4b104224f0a325b544f4b454e5d5f743651455768445a4e624577596c764a35726e7035587062616c723265674d7362762d4341514a6e6567120e0802120a5b46494c455f55524c5d1a065b484153485d20aa6c30edccbfd406180312b6010ab30162b0010aad015b20e5afb9e696b9e9809ae8bf875151e997aae4bca0e58f91e98081e69687e4bbb6e7bb99e4bda0efbc8ce8afb7e68993e5bc80e993bee68ea5e69fa5e79c8befbc9a5b46494c455f55524c5d200a20e69bb4e696b05151e7ab8be4baabe5858de8b4b9e69e81e9809fe4b88be8bdbdefbc8ce58d87e7baa7e993bee68ea5efbc9a5b51515f4c494e4b5d20efbc884c696e7578e38081e9b8bfe89299e69a82e4b88de694afe68c81efbc895d124eaa024b080030023a075b544f4b454e5d8801009a01387800c80100f00100f80100900200d80285da8018ca0400d20506088301108201b2060a0a080800100018002000a007cfb398af8beee6cb7112064a0408006000 \ No newline at end of file diff --git a/packages/protocol/tests/msg-push/flash-transfer-decode.test.ts b/packages/protocol/tests/msg-push/flash-transfer-decode.test.ts index 5f8e5914..eeb94489 100644 --- a/packages/protocol/tests/msg-push/flash-transfer-decode.test.ts +++ b/packages/protocol/tests/msg-push/flash-transfer-decode.test.ts @@ -1,7 +1,9 @@ +import { readFileSync } from 'node:fs'; +import { fileURLToPath } from 'node:url'; import { describe, expect, it } from 'vitest'; -import { protobuf_encode } from '@snowluma/proton'; +import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; import { decodeRichBody } from '../../src/msg-push/rich-body-decoder'; -import type { MessageBody } from '@snowluma/proto-defs/message'; +import type { MessageBody, PushMsg } from '@snowluma/proto-defs/message'; import type { MarkdownData } from '@snowluma/proto-defs/action'; // Build a svc=45 richui markdown commonElem carrying a FlashTransfer card, the @@ -46,3 +48,119 @@ describe('decodeRichBody / 闪传 flash_file (#199/#200)', () => { expect(out[0]?.type).toBe('markdown'); }); }); + +// Current NT cards (#358) put fileset identity on MarkdownData.extInfo +// (extType=1 / DecodeMdExtInfoFileTransfer) and in the click scheme. The +// richui JSON no longer has `data.fileSetId`. +function currentNtFlashBody(json: unknown, ext?: MarkdownData['extInfo'], summary?: string): MessageBody { + const markdown = `[闪传](mqqapi://markdown/node?nodeType=richui&json=${encodeURIComponent(JSON.stringify(json))})`; + const pbElem = protobuf_encode({ + content: markdown, + summary, + extType: ext ? 1 : undefined, + extInfo: ext, + }); + return { + richText: { elems: [{ commonElem: { serviceType: 45, businessType: 3, pbElem } } as never] }, + }; +} + +const currentNtCardJson = { + busId: 'FlashTransfer', + templateId: 'flash', + version: 2, + layout: { viewId: 'flash_file', width: -2, height: -2, direction: 'horizontal' }, + attributes: { + viewId: 'flash_file', + attributes: [{ + viewId: 'file', + scheme: 'mqqrouter://flash_transfer/open_fileset?fileset_id=fs-nt-358&version=1&channel_id=1&src_type=internal&scene_type=1', + }], + }, +}; + +describe('decodeRichBody / 闪传 flash_file (#358 current NT card)', () => { + it('reads filesetId and name from markdown extInfo when the JSON has no data.fileSetId', () => { + const out = decodeRichBody(currentNtFlashBody(currentNtCardJson, { + filesetId: 'fs-nt-358', + name: 'cpwd截图.zip', + fileSize: 9196055, + expireTime: 1787815533, + }, '[QQ闪传] cpwd截图.zip'), true); + expect(out).toEqual([{ + type: 'flash_file', + filesetId: 'fs-nt-358', + fileName: 'cpwd截图.zip', + sceneType: 1, + }]); + }); + + it('falls back to the open_fileset scheme when extInfo is absent', () => { + const out = decodeRichBody(currentNtFlashBody(currentNtCardJson), true); + expect(out).toEqual([{ + type: 'flash_file', + filesetId: 'fs-nt-358', + fileName: '', + sceneType: 1, + }]); + }); + + it('still decodes from extInfo when the richui JSON is truncated', () => { + const markdown = '[闪传](mqqapi://markdown/node?nodeType=richui&json=%7B%22busId%22%3A%22FlashTransfer%22%2C%22src%22%3A%22'; + const pbElem = protobuf_encode({ + content: markdown, + summary: '[QQ闪传] report.pdf', + extType: 1, + extInfo: { filesetId: 'fs-from-ext', name: 'report.pdf' }, + }); + const out = decodeRichBody({ + richText: { elems: [{ commonElem: { serviceType: 45, businessType: 3, pbElem } } as never] }, + }, true); + expect(out).toEqual([{ + type: 'flash_file', + filesetId: 'fs-from-ext', + fileName: 'report.pdf', + sceneType: 0, + }]); + }); + + it('drops the compatibility text sibling after a successful flash_file decode', () => { + const markdown = `[闪传](mqqapi://markdown/node?nodeType=richui&json=${encodeURIComponent(JSON.stringify(currentNtCardJson))})`; + const pbElem = protobuf_encode({ + content: markdown, + extType: 1, + extInfo: { filesetId: 'fs-nt-358', name: 'a.zip' }, + }); + const out = decodeRichBody({ + richText: { + elems: [ + { commonElem: { serviceType: 45, businessType: 3, pbElem } } as never, + { text: { str: '[闪传]' } } as never, + ], + }, + }, true); + expect(out).toEqual([{ + type: 'flash_file', + filesetId: 'fs-nt-358', + fileName: 'a.zip', + sceneType: 1, + }]); + }); + + it('decodes the sanitized #358 C2C push (extInfo + open_fileset scheme)', () => { + const hex = readFileSync( + fileURLToPath(new URL('./fixtures/issue-358-flash-push.hex', import.meta.url)), + 'utf8', + ).trim(); + const push = protobuf_decode(Buffer.from(hex, 'hex')); + const out = decodeRichBody(push.message?.body, false); + // Reporter redacted the richui JSON (unterminated src), so scene_type + // cannot be recovered from the scheme. extInfo still supplies fileset + name. + expect(out).toEqual([{ + type: 'flash_file', + filesetId: '[UUID]', + fileName: 'cpwd截图.zip', + sceneType: 0, + }]); + }); +});