fix(c2c-file): rewrite sendC2cFileMessage wire shape per Lagrange.Core V2

The c2c file send path was wrong on three independent axes — every
c2c file the bot tried to deliver was getting rejected by the QQ-NT
server. Aligned to `dev/Lagrange.Core` (V2 branch) which is the only
direct-protocol reference for c2c files; NapCat goes through QQ-NT
IPC and never builds this packet itself.

Three wire-shape fixes:

1. **Routing**: `routingHead.c2c { uin, uid }` → `routingHead.trans0x211
   { ccCmd: 4, uid }`. RoutingHead field 15 is the c2c-file scene's
   slot. `MessagePacker.BuildPacketBase` makes this conditional on
   `chain.HasTypeOf<FileEntity>()`: when a file is present, C2C goes
   nil and Trans0X211 carries the routing.

2. **File payload location**: `messageBody.richText.notOnlineFile {...}`
   → `messageBody.msgContent = serialize(FileExtra{file: NotOnlineFile})`.
   The server doesn't read c2c file metadata from `richText` at all —
   `MessageBody.cs:12` even has the comment "Offline file is now put
   here(?". Confirmed by `FileEntity.PackElement()` returning empty
   and `PackMessageContent()` returning the FileExtra wrapper.

3. **contentHead.c2cCmd**: `11` → `0` (default/omitted). `11` was a
   stale go-cqhttp value the QQ-NT server doesn't recognise on the
   c2c-file dispatcher. Lagrange leaves it at the C# default (0).

Schema fixes (proton + legacy, both directions):

- `RoutingHead` gains `trans0x211?: pb<15, RoutingTrans0x211>`. The
  new message has `toUin@1`, `ccCmd@2`, `uid@8`.
- `SendMessageBody` gains `msgContent?: pb<2, bytes>` (the receive-side
  `MessageBody` already had it).
- `FileExtraInfo` deleted — was wrong on every tag (fileSize=1,
  fileName=2, fileMd5=3, fileUuid=4, fileHash=5, none of which match
  the wire). `FileExtra { file: pb<1, NotOnlineFile> }` now wraps the
  same `NotOnlineFile` used on the receive path. This is why c2c file
  PUSHES were also silently failing the four-field truthy check in
  `extractMsgContent` — the schema couldn't decode anything.
- `NotOnlineFile` gains the three intake-validator fields the server
  requires on send: `subcmd@9 = 1`, `dangerEvel@50 = 0`, `expireTime@55`
  = now+7d.

Receive side (`rich-body-decoder.ts::extractMsgContent`): drop the
"all four fields must be truthy" guard (it was a workaround for the
broken schema) and accept any payload that decodes a fileUuid.

Tests:

- `bridge-private-routing.test.ts`: new regression that round-trips
  `sendC2cFileMessage` through `protoDecode` and asserts trans0x211
  routing, msgContent FileExtra wrapper, NotOnlineFile field tags,
  c2cCmd=0. Catches each of the three wire-shape regressions
  independently.
- `send-private-c2c-file.test.ts`: vi.fn arg signatures explicit for
  CI typecheck.

647/647 tests pass.
This commit is contained in:
DrSmoothl committed 2026-05-19 23:45:40 +08:00
1 parent f65fe959bb
commit 1db287d8cc
8 files changed
+248 -68

No files matched your search

+59 -21
View File
@@ -17,6 +17,7 @@ import type {
SendMessageRequest,
SendMessageResponse,
} from './proto/proton/action';
import type { FileExtra } from './proto/proton/message';
// Delegated modules
import {
@@ -401,13 +402,27 @@ export class Bridge implements BridgeInterface {
/**
* Send a c2c file as a chat message.
*
* Unlike images / videos / records that ride on `RichText.elems` as
* regular Elem entries, c2c files use `RichText.notOnlineFile`
* (parallel to `elems`, see `proto/proton/message.ts:75`). The
* receive-side decoder strips this back to a `{type:'file'}` segment
* in `rich-body-decoder.ts:425-433`. There's no Elem you can stuff
* into `buildSendElems` for this, so this helper short-circuits the
* normal send pipeline.
* The wire shape isn't the same as a regular c2c message — the c2c
* file path uses three slots that differ from a normal text/image
* send (verified against `dev/Lagrange.Core/.../MessagePacker.cs:
* BuildPacketBase` + `FileEntity.PackMessageContent`):
*
* 1. `routingHead.trans0x211 { ccCmd: 4, uid: peer }` instead of
* `routingHead.c2c { uin, uid }`. The server rejects c2c file
* messages routed through the regular c2c slot.
* 2. `messageBody.msgContent` carries the serialised
* `FileExtra { file: NotOnlineFile }` bytes. NOT
* `richText.notOnlineFile` — the receiver doesn't read that
* slot for file metadata.
* 3. `contentHead.c2cCmd` left at 0 (Lagrange's default). The
* previous `c2cCmd: 11` was a stale go-cqhttp value the QQ-NT
* server doesn't recognise.
*
* NotOnlineFile carries three required-on-send fields the receiver
* itself ignores but the server's intake validator checks:
* - `subcmd: 1` — c2c file send command code
* - `dangerEvel: 0` — virus-scan severity, always 0 client-side
* - `expireTime` — 7 days from now (Lagrange convention)
*/
async sendC2cFileMessage(
userUin: number,
@@ -417,38 +432,61 @@ export class Bridge implements BridgeInterface {
const random = this.nextMessageRandom();
const clientSeq = this.nextClientSequence();
const nowSec = Math.floor(Date.now() / 1000);
const sevenDaysSec = 7 * 24 * 60 * 60;
// Serialise `FileExtra { file: NotOnlineFile }` for `msgContent`.
// The NotOnlineFile field tags (1/3/4/5/6/9/50/55/57) are shared
// between send and receive — the schema is symmetric.
const fileExtraBytes = protobuf_encode<FileExtra>({
file: {
fileType: 0,
fileUuid: info.fileId,
fileMd5: info.fileMd5,
fileName: info.fileName,
fileSize: BigInt(info.fileSize),
subcmd: 1,
dangerEvel: 0,
expireTime: nowSec + sevenDaysSec,
fileHash: info.fileHash ?? '',
},
});
const request = protobuf_encode<SendMessageRequest>({
routingHead: {
c2c: { uin: userUin, uid: userUid },
// c2c-file scene: route through `trans0x211` (field 15) with
// ccCmd=4. The regular `c2c { uin, uid }` routing slot causes
// the server to reject this with a routing-mismatch error.
trans0x211: { ccCmd: 4, uid: userUid },
},
contentHead: {
type: 1,
subType: 0,
c2cCmd: 11,
// c2cCmd intentionally omitted (defaults to 0). Was `11`,
// which produced an unknown-command error in the QQ-NT
// server's c2c file handler. `userUin` is unused on this path
// (routing carries the uid only) but kept in the function
// signature for symmetry with the OneBot caller.
},
messageBody: {
richText: {
notOnlineFile: {
fileType: 0,
fileUuid: info.fileId,
fileMd5: info.fileMd5,
fileName: info.fileName,
fileSize: BigInt(info.fileSize),
fileHash: info.fileHash ?? '',
},
},
} as any,
// No elems — the file metadata lives in msgContent below.
msgContent: fileExtraBytes,
},
clientSequence: clientSeq,
random,
syncCookie: new Uint8Array(0),
via: 0,
dataStatist: 0,
ctrl: {
msgFlag: Math.floor(Date.now() / 1000),
msgFlag: nowSec,
},
multiSendSeq: 0,
});
// Silence the unused-parameter lint — `userUin` is part of our
// BridgeInterface contract (the OneBot layer threads it through)
// but the wire shape only needs the uid.
void userUin;
const result = await this.sendRawPacket(Bridge.SEND_MSG_CMD, request);
if (!result.success || !result.gotResponse || !result.responseData) {
throw new Error(`send c2c file message failed: ${result.errorMessage || 'no response'}`);