diff --git a/packages/core/src/bridge/element-builder.ts b/packages/core/src/bridge/element-builder.ts index cace04b2..367f881e 100644 --- a/packages/core/src/bridge/element-builder.ts +++ b/packages/core/src/bridge/element-builder.ts @@ -262,7 +262,16 @@ function makeGroupFileElem(element: MessageElement): ProtoElem { const md5 = element.md5Hex ? hexToBytes(element.md5Hex) : new Uint8Array(0); const sha1 = element.sha1Hex ? hexToBytes(element.sha1Hex) : new Uint8Array(0); + // Outer field1 is hardcoded to 6 in NapCat's encoder + // (`packet/message/element.ts:589`). It's a magic tag the server + // expects on send (acidify ships the same shape via their generic + // `field1: Int` default but their consumer writes it too). Without + // these outer fields the current QQ-NT server rejects the message + // with `result=79`. Inner field5 is the uint32 placeholder both + // NapCat and acidify leave at 0 — we follow suit. const extraBytes = protobuf_encode({ + field1: 6, + fileName, inner: { info: { busId: 102, diff --git a/packages/core/src/bridge/proto/element.ts b/packages/core/src/bridge/proto/element.ts index 7ce06cb8..94f78dce 100644 --- a/packages/core/src/bridge/proto/element.ts +++ b/packages/core/src/bridge/proto/element.ts @@ -390,14 +390,21 @@ export const MsgInfoSchema = { // --- GroupFileExtra (TransElem type=24) --- +// Field numbers cross-checked against NapCat (component.ts:146) + +// acidify (GroupFileExtra.kt:Inner.Info). Old schema had fileSha at 5 +// / extInfoString at 6 / fileMd5 at 7 which mismatched the real wire +// shape — the server rejected the chat post with `result=79` because +// fileSha bytes were landing where it expected a uint32 (field 5). +// See proton/element.ts for the WHY comment. export const GroupFileInfoSchema = { busId: { field: 1, type: 'uint32' as const }, fileId: { field: 2, type: 'string' as const }, fileSize: { field: 3, type: 'uint64' as const }, fileName: { field: 4, type: 'string' as const }, - fileSha: { field: 5, type: 'bytes' as const }, - extInfoString: { field: 6, type: 'string' as const }, - fileMd5: { field: 7, type: 'bytes' as const }, + field5: { field: 5, type: 'uint32' as const }, + fileSha: { field: 6, type: 'bytes' as const }, + extInfoString: { field: 7, type: 'string' as const }, + fileMd5: { field: 8, type: 'bytes' as const }, } satisfies ProtoSchema; export const GroupFileExtraInnerSchema = { @@ -405,6 +412,9 @@ export const GroupFileExtraInnerSchema = { } satisfies ProtoSchema; export const GroupFileExtraSchema = { + field1: { field: 1, type: 'uint32' as const }, + fileName: { field: 2, type: 'string' as const }, + display: { field: 3, type: 'string' as const }, inner: { field: 7, type: 'message' as const, schema: GroupFileExtraInnerSchema }, } satisfies ProtoSchema; diff --git a/packages/core/src/bridge/proto/proton/element.ts b/packages/core/src/bridge/proto/proton/element.ts index 3da2ac6f..304cd08e 100644 --- a/packages/core/src/bridge/proto/proton/element.ts +++ b/packages/core/src/bridge/proto/proton/element.ts @@ -392,23 +392,43 @@ export interface MsgInfo { } // ── GroupFileExtra (TransElem type=24) ────────────────────────────── +// +// Sourced from NapCat's `GroupFileExtraInfo` (component.ts:146-155) +// cross-checked against acidify's `GroupFileExtra.Inner.Info` +// (proto/message/extra/GroupFileExtra.kt). Field numbers had been +// shifted by one in the previous schema (fileSha at 5, ext at 6, +// fileMd5 at 7) which caused the server to reject group-file chat +// posts with `result=79` — the server saw fileSha bytes where it +// expected a uint32 placeholder (field 5) and silently dropped the +// rest. Field 5 is a uint32 placeholder both refs label `field5`; +// fileSha is at 6, extInfoString at 7, fileMd5 at 8. export interface GroupFileInfo { busId?: pb<1, uint_32>; fileId?: pb<2, string>; fileSize?: pb<3, uint_64>; fileName?: pb<4, string>; - fileSha?: pb<5, bytes>; - extInfoString?: pb<6, string>; - fileMd5?: pb<7, bytes>; + field5?: pb<5, uint_32>; + fileSha?: pb<6, bytes>; + extInfoString?: pb<7, string>; + fileMd5?: pb<8, bytes>; } export interface GroupFileExtraInner { info?: pb<2, GroupFileInfo>; } +// Outer wrapper carries three additional fields the server expects: +// field1 (uint32, NapCat hardcodes to 6 — "magic op tag"), fileName +// (string mirror of the inner info.fileName), display (optional UI +// preview string, NapCat leaves empty but writes the slot). Without +// these the server still recognized the message in older builds but +// rejects it in the current rollout. export interface GroupFileExtra { - inner?: pb<7, GroupFileExtraInner>; + field1?: pb<1, uint_32>; + fileName?: pb<2, string>; + display?: pb<3, string>; + inner?: pb<7, GroupFileExtraInner>; } // ── Preserve (for message receipt info) ───────────────────────────── diff --git a/packages/core/tests/element-builder.test.ts b/packages/core/tests/element-builder.test.ts index ebe4e639..a4893110 100644 --- a/packages/core/tests/element-builder.test.ts +++ b/packages/core/tests/element-builder.test.ts @@ -92,3 +92,57 @@ describe('element-builder / commonElem.businessType per scene', () => { }); }); }); + +describe('element-builder / group file element wire shape', () => { + // Regression for the same `result=79` class: the GroupFileExtra outer + // wrapper was missing the magic `field1=6` + `fileName` slots and the + // inner GroupFileInfo had fileSha/extInfoString/fileMd5 shifted one + // tag to the left (5/6/7 instead of the correct 6/7/8). Both + // mistakes made the current QQ-NT server reject the chat post. This + // test decodes the element back through the proton-generated codec + // and asserts each field lands at the right tag. + it('encodes outer field1=6 + fileName, and inner fields at tags 6/7/8', async () => { + const [elem] = await buildSendElems( + [{ + type: 'file', + fileId: 'fid-abc', + fileName: 'doc.txt', + fileSize: 123, + md5Hex: 'aabbccddeeff00112233445566778899', + sha1Hex: '0102030405060708090a0b0c0d0e0f1011121314', + } as any], + { bridge: fakeBridge, groupId: 12345 }, + ); + const transElem = (elem as any).transElem as { elemType: number; elemValue: Uint8Array }; + expect(transElem.elemType).toBe(24); + // 0x01 prefix + BE16 length + payload + expect(transElem.elemValue[0]).toBe(0x01); + const len = (transElem.elemValue[1] << 8) | transElem.elemValue[2]; + const payload = transElem.elemValue.subarray(3, 3 + len); + + // Hand-walk the wire bytes to verify each tag lands where the + // server expects. Each varint tag = (field << 3) | wire_type: + // outer field1=6 → tag 8 (wire 0 / varint), value 6 + // outer fileName → tag 18 (wire 2 / length-delimited), 'doc.txt' + // outer inner → tag 58 (field 7 wire 2) + // inner.info → tag 18 (field 2 wire 2) + // info.busId → tag 8 (field 1 wire 0), value 102 + // info.fileId → tag 18 (field 2 wire 2) + // info.fileSize → tag 24 (field 3 wire 0), value 123 + // info.fileName → tag 34 (field 4 wire 2) + // info.fileSha → tag 50 (field 6 wire 2) ← was 42 (field 5) + // info.extInfoString → tag 58 (field 7 wire 2) ← was 50 (field 6) + // info.fileMd5 → tag 66 (field 8 wire 2) ← was 58 (field 7) + const tagAt = (i: number) => payload[i]; + expect(tagAt(0)).toBe(8); // outer field1 tag — regression: missing entirely before + expect(payload[1]).toBe(6); // value of field1 (NapCat hardcodes 6) + expect(tagAt(2)).toBe(18); // outer fileName tag — regression: missing + // Locate the inner GroupFileExtraInfo by scanning for tag 50 (was 42 + // in the bug). A naive scan for `0x32` is enough because no string + // field happens to contain that byte in this test fixture. + expect(payload.includes(50)).toBe(true); // fileSha at tag 50 (field 6) + expect(payload.includes(58)).toBe(true); // extInfoString at tag 58 (field 7) + expect(payload.includes(66)).toBe(true); // fileMd5 at tag 66 (field 8) + expect(payload.includes(42)).toBe(false); // sanity: nothing at the old (wrong) tag for fileSha + }); +});