diff --git a/src/common/message-unique.ts b/src/common/message-unique.ts index 03d136ef..87e2ca58 100644 --- a/src/common/message-unique.ts +++ b/src/common/message-unique.ts @@ -23,7 +23,9 @@ export class LimitedHashTable { } while (this.keyToValue.size > this.maxSize || this.valueToKey.size > this.maxSize) { const oldestKey = this.keyToValue.keys().next().value; + // @ts-ignore this.valueToKey.delete(this.keyToValue.get(oldestKey)!); + // @ts-ignore this.keyToValue.delete(oldestKey); } } diff --git a/src/core/apis/packet.ts b/src/core/apis/packet.ts index dd3e816a..2b2d58f0 100644 --- a/src/core/apis/packet.ts +++ b/src/core/apis/packet.ts @@ -8,9 +8,9 @@ import { NapProtoMsg } from '@/core/packet/proto/NapProto'; import { OidbSvcTrpcTcp0X9067_202_Rsp_Body } from '@/core/packet/proto/oidb/Oidb.0x9067_202'; import { OidbSvcTrpcTcpBase, OidbSvcTrpcTcpBaseRsp } from '@/core/packet/proto/oidb/OidbBase'; import { OidbSvcTrpcTcp0XFE1_2RSP } from '@/core/packet/proto/oidb/Oidb.fe1_2'; -import { PacketForwardNode } from "@/core/packet/msg/entity/forward"; import {LogWrapper} from "@/common/log"; import {SendLongMsgResp} from "@/core/packet/proto/message/action"; +import {PacketMsg} from "@/core/packet/msg/message"; interface OffsetType { [key: string]: { @@ -106,7 +106,7 @@ export class NTQQPacketApi { await this.sendPacket('OidbSvcTrpcTcp.0x8fc_2', data, true); } - async sendUploadForwardMsg(msg: PacketForwardNode[], groupUin: number = 0) { + async sendUploadForwardMsg(msg: PacketMsg[], groupUin: number = 0) { const data = this.packetPacker.packUploadForwardMsg(this.core.selfInfo.uid, msg, groupUin); const ret = await this.sendPacket('trpc.group.long_msg_interface.MsgService.SsoSendLongMsg', data, true); const resp = new NapProtoMsg(SendLongMsgResp).decode(Buffer.from(ret.hex_data, 'hex')); diff --git a/src/core/packet/highway/frame.ts b/src/core/packet/highway/frame.ts index e0f87c4b..3c3f4039 100644 --- a/src/core/packet/highway/frame.ts +++ b/src/core/packet/highway/frame.ts @@ -1,4 +1,4 @@ -import assert from "node:assert"; +import * as assert from "node:assert"; export class Frame{ static pack(head: Buffer, body: Buffer): Buffer { diff --git a/src/core/packet/msg/builder.ts b/src/core/packet/msg/builder.ts index a3aff4f8..a169cb3f 100644 --- a/src/core/packet/msg/builder.ts +++ b/src/core/packet/msg/builder.ts @@ -1,8 +1,8 @@ import * as crypto from "crypto"; import {PushMsgBody} from "@/core/packet/proto/message/message"; import {NapProtoEncodeStructType} from "@/core/packet/proto/NapProto"; -import {PacketForwardNode} from "@/core/packet/msg/entity/forward"; import {LogWrapper} from "@/common/log"; +import {PacketMsg} from "@/core/packet/msg/message"; export class PacketMsgBuilder { private logger: LogWrapper; @@ -11,14 +11,14 @@ export class PacketMsgBuilder { this.logger = logger; } - buildFakeMsg(selfUid: string, element: PacketForwardNode[]): NapProtoEncodeStructType[] { + buildFakeMsg(selfUid: string, element: PacketMsg[]): NapProtoEncodeStructType[] { return element.map((node): NapProtoEncodeStructType => { - const avatar = `https://q.qlogo.cn/headimg_dl?dst_uin=${node.senderId}&spec=640&img_type=jpg`; + const avatar = `https://q.qlogo.cn/headimg_dl?dst_uin=${node.senderUin}&spec=640&img_type=jpg`; const msgElement = node.msg.flatMap(msg => msg.buildElement() ?? []); return { responseHead: { fromUid: "", - fromUin: node.senderId, + fromUin: node.senderUin, toUid: node.groupId ? undefined : selfUid, forward: node.groupId ? undefined : { friendName: node.senderName, diff --git a/src/core/packet/msg/element.ts b/src/core/packet/msg/element.ts index 0135edb0..91602a87 100644 --- a/src/core/packet/msg/element.ts +++ b/src/core/packet/msg/element.ts @@ -1,5 +1,6 @@ -import assert from "node:assert"; +import * as assert from "node:assert"; import * as zlib from "node:zlib"; +import * as crypto from "node:crypto"; import {NapProtoEncodeStructType, NapProtoMsg} from "@/core/packet/proto/NapProto"; import { CustomFace, @@ -26,6 +27,7 @@ import { SendVideoElement } from "@/core"; import {MsgInfo} from "@/core/packet/proto/oidb/common/Ntv2.RichMediaReq"; +import {PacketMsg} from "@/core/packet/msg/message"; // raw <-> packet // TODO: check ob11 -> raw impl! @@ -42,6 +44,10 @@ export abstract class IPacketMsgElement[] | undefined { return undefined; } + + toPreview(): string { + return '[nya~]'; + } } export class PacketMsgTextElement extends IPacketMsgElement { @@ -59,6 +65,10 @@ export class PacketMsgTextElement extends IPacketMsgElement { } }]; } + + toPreview(): string { + return this.text; + } } export class PacketMsgAtElement extends PacketMsgTextElement { @@ -85,6 +95,10 @@ export class PacketMsgAtElement extends PacketMsgTextElement { } }]; } + + toPreview(): string { + return `@${this.targetUid} ${this.text}`; + } } export class PacketMsgPicElement extends IPacketMsgElement { @@ -121,6 +135,10 @@ export class PacketMsgPicElement extends IPacketMsgElement { } }] } + + toPreview(): string { + return "[图片]"; + } } export class PacketMsgReplyElement extends IPacketMsgElement { @@ -169,6 +187,10 @@ export class PacketMsgReplyElement extends IPacketMsgElement { } : undefined, }] } + + toPreview(): string { + return "[回复]"; + } } export class PacketMsgFaceElement extends IPacketMsgElement { @@ -218,6 +240,10 @@ export class PacketMsgFaceElement extends IPacketMsgElement { }] } } + + toPreview(): string { + return "[表情]"; + } } export class PacketMsgVideoElement extends IPacketMsgElement { @@ -256,6 +282,10 @@ export class PacketMsgLightAppElement extends IPacketMsgElement } }] } + + toPreview(): string { + return "[小程序]"; + } } export class PacketMsgMarkDownElement extends IPacketMsgElement { @@ -277,23 +307,67 @@ export class PacketMsgMarkDownElement extends IPacketMsgElement { -// resid: string; -// -// constructor(element: SendStructLongMsgElement) { -// super(element); -// this.resid = element.structLongMsgElement.resId; -// } -// -// buildElement(): NapProtoEncodeStructType[] { -// return [{ -// generalFlags: { -// longTextResId: this.resid, -// longTextFlag: 1 -// } -// }] -// } -// } +export class PacketMultiMsgElement extends IPacketMsgElement { + resid: string; + message: PacketMsg[]; + + constructor(rawElement: SendStructLongMsgElement) + constructor(rawElement: SendStructLongMsgElement, message?: PacketMsg[]) { + super(rawElement); + this.resid = rawElement.structLongMsgElement.resId; + this.message = message ?? []; + } + + buildElement(): NapProtoEncodeStructType[] { + const id = crypto.randomUUID(); + const elementJson = { + app: "com.tencent.multimsg", + config: { + autosize: 1, + forward: 1, + round: 1, + type: "normal", + width: 300 + }, + desc: "[聊天记录]", + extra: { + filename: id, + tsum: this.message.length, + }, + meta: { + detail: { + news: this.message.length === 0 ? [{ + text: "[Nya~ This message is send from NapCat.Packet!]", + }] : this.message.map(packetMsg => ({ + text: `${packetMsg.senderName}: ${packetMsg.msg.map(msg => msg.toPreview()).join('')}`, + })), + resid: this.resid, + source: "聊天记录", + summary: `查看${this.message.length}条转发消息`, + uniseq: id, + } + }, + prompt: "[聊天记录]", + ver: "0.0.0.5", + view: "contact" + } + return [{ + lightAppElem: { + data: Buffer.concat([ + Buffer.from([0x01]), + zlib.deflateSync(Buffer.from(JSON.stringify(elementJson), 'utf-8')) + ]) + } + }] + } + + toPreview(): string { + return "[聊天记录]"; + } +} diff --git a/src/core/packet/msg/entity/forward.ts b/src/core/packet/msg/entity/forward.ts deleted file mode 100644 index f4184880..00000000 --- a/src/core/packet/msg/entity/forward.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { IPacketMsgElement } from "@/core/packet/msg/element"; -import { SendMessageElement } from "@/core"; - -export interface PacketForwardNode { - groupId?: number - senderId: number - senderName: string - time: number - msg: IPacketMsgElement[] -} diff --git a/src/core/packet/msg/message.ts b/src/core/packet/msg/message.ts new file mode 100644 index 00000000..6c8b9f29 --- /dev/null +++ b/src/core/packet/msg/message.ts @@ -0,0 +1,13 @@ +import {IPacketMsgElement} from "@/core/packet/msg/element"; +import {SendMessageElement} from "@/core"; + +export interface PacketMsg { + seq?: number; + clientSeq?: number; + groupId?: number; + senderUid: string; + senderUin: number; + senderName: string; + time: number; + msg: IPacketMsgElement[] +} diff --git a/src/core/packet/packer.ts b/src/core/packet/packer.ts index eb9a59f2..b0569bdc 100644 --- a/src/core/packet/packer.ts +++ b/src/core/packet/packer.ts @@ -11,9 +11,9 @@ import {NTV2RichMediaReq} from "@/core/packet/proto/oidb/common/Ntv2.RichMediaRe import {HttpConn0x6ff_501} from "@/core/packet/proto/action/action"; import {LongMsgResult, SendLongMsgReq} from "@/core/packet/proto/message/action"; import {PacketMsgBuilder} from "@/core/packet/msg/builder"; -import {PacketForwardNode} from "@/core/packet/msg/entity/forward"; import {PacketMsgPicElement} from "@/core/packet/msg/element"; import {LogWrapper} from "@/common/log"; +import {PacketMsg} from "@/core/packet/msg/message"; export type PacketHexStr = string & { readonly hexNya: unique symbol }; @@ -94,7 +94,7 @@ export class PacketPacker { return this.toHexStr(this.packOidbPacket(0xfe1, 2, oidb_0xfe1_2)); } - packUploadForwardMsg(selfUid: string, msg: PacketForwardNode[], groupUin: number = 0): PacketHexStr { + packUploadForwardMsg(selfUid: string, msg: PacketMsg[], groupUin: number = 0): PacketHexStr { // this.logger.logDebug("packUploadForwardMsg START!!!", selfUid, msg, groupUin); const msgBody = this.packetBuilder.buildFakeMsg(selfUid, msg); const longMsgResultData = new NapProtoMsg(LongMsgResult).encode( diff --git a/src/onebot/action/extends/SetQQAvatar.ts b/src/onebot/action/extends/SetQQAvatar.ts index c75d82f6..07fb9bae 100644 --- a/src/onebot/action/extends/SetQQAvatar.ts +++ b/src/onebot/action/extends/SetQQAvatar.ts @@ -38,6 +38,7 @@ export default class SetAvatar extends BaseAction { throw `头像${payload.file}设置失败,api无返回`; } // log(`头像设置返回:${JSON.stringify(ret)}`) + // @ts-ignore if (ret['result'] == 1004022) { throw `头像${payload.file}设置失败,文件可能不是图片格式`; } else if (ret['result'] != 0) { diff --git a/src/onebot/network/passive-http.ts b/src/onebot/network/passive-http.ts index 0a7b8367..44af0843 100644 --- a/src/onebot/network/passive-http.ts +++ b/src/onebot/network/passive-http.ts @@ -64,6 +64,7 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter { }); this.app.use((req, res, next) => this.authorize(this.token, req, res, next)); + // @ts-ignore this.app.use((req, res) => this.handleRequest(req, res)); this.server.listen(this.port, () => {