feat(onebot): decode incoming 闪传 flash-file messages (#199/#200)

Older QQ clients (≤9.9.30) deliver a 闪传 (flash transfer) file as a richui
markdown commonElem (svc=45, busId=FlashTransfer); SnowLuma had no decoder for
it, so the message decoded to 0 elements and was reported empty. (Newer
clients ≥9.9.31 send a plain text+link message that already decodes fine.)

Decode svc=45 markdown → parse the FlashTransfer card JSON → emit a flash_file
element, surfaced as the segment the issue asked for:
  { type: "flash_file", data: { title, file_set_id, scene_type } }

- Field names (fileSetId / sceneType / title) confirmed against QQ NT's
  flash-transfer manager in wrapper.node.i64. The card is built by the
  sender's client so the exact JSON nesting is unknown — values are pulled by
  a bounded recursive search rather than a pinned path (handles either shape).
- Non-FlashTransfer markdown is ignored; missing fields default (never empty).
- Manifest: D/S = yes, P/W = by-design-no (receive-only; sending is the
  existing send_flash_msg action). Log preview shows [闪传文件:title].
- Tests: card decode (nested + alias + defaults + non-flash) and the
  element→segment mapping.

Note: not end-to-end verified on a live message — the svc=45 format only comes
from ≤9.9.30 senders and no such client was available (every test sender here
is ≥9.9.31 → text format). Logic + field names are unit-tested / IDA-confirmed.

typecheck 12 包; protocol 596 / onebot 529 green.
This commit is contained in:
DrSmoothl committed 2026-07-04 19:02:47 +08:00
1 parent 0d03b00643
commit 770b224d58
7 files changed
+154

No files matched your search

@@ -12,6 +12,7 @@ import type {
QFaceExtra,
QSmallFaceExtra,
} from '@snowluma/proto-defs/element';
import type { MarkdownData } from '@snowluma/proto-defs/action';
import type { FileExtra, MessageBody, PushMsgBody as PushMsgBodyFull, RichText } from '@snowluma/proto-defs/message';
import { decompressData, makeImageUrl } from './helpers';
@@ -480,6 +481,13 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
const extra = protobuf_decode<QFaceExtra>(ce.pbElem);
if (extra?.qsid !== undefined) result.push({ type: 'face', faceId: extra.qsid });
skipNext = true;
} else if (svcType === 45 && ce.pbElem && ce.pbElem.length > 0) {
// Markdown commonElem. Older QQ clients (≤9.9.30) deliver a 闪传 (flash
// transfer) file as a richui markdown card (busId=FlashTransfer); newer
// clients send a plain text+link message that decodes elsewhere. Pull
// the flash fields out so the message isn't dropped to empty (#199/#200).
const flash = decodeFlashTransferCard(ce.pbElem);
if (flash) result.push(flash);
}
}
}
@@ -487,6 +495,54 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
return result;
}
/**
* Decode a 闪传 richui markdown commonElem (svc=45) into a `flash_file`
* element. The pbElem is a `MarkdownData` whose `content` is a markdown link
* `[闪传](mqqapi://markdown/node?nodeType=richui&json=<url-encoded 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. Returns
* null for any non-flash markdown. See #199 / #200.
*/
function deepFindValue(obj: unknown, keys: readonly string[], depth = 0): unknown {
if (depth > 8 || obj === null || typeof obj !== 'object') return undefined;
const rec = obj as Record<string, unknown>;
for (const key of keys) {
const v = rec[key];
if (v !== undefined && v !== null && v !== '') return v;
}
for (const v of Object.values(rec)) {
const found = deepFindValue(v, keys, depth + 1);
if (found !== undefined) return found;
}
return undefined;
}
function decodeFlashTransferCard(pbElem: Uint8Array): MessageElement | null {
const md = protobuf_decode<MarkdownData>(pbElem);
const content = md?.content ?? '';
if (!content.includes('FlashTransfer')) return null;
const m = content.match(/[?&]json=([^)\s]+)/);
if (!m) return null;
let obj: unknown;
try {
obj = JSON.parse(decodeURIComponent(m[1]));
} catch {
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']);
return {
type: 'flash_file',
filesetId: filesetId != null ? String(filesetId) : '',
fileName: title != null ? String(title) : '',
sceneType: sceneType != null ? Number(sceneType) : 0,
};
}
function extractRichtextExtras(
rt: RichTextDecoded,
elements: MessageElement[],