diff --git a/packages/core/src/bridge/bridge.ts b/packages/core/src/bridge/bridge.ts index aeca9979..f9d99eb1 100644 --- a/packages/core/src/bridge/bridge.ts +++ b/packages/core/src/bridge/bridge.ts @@ -4,6 +4,7 @@ import type { PacketInfo } from '@snowluma/common/protocol-types'; import { BridgeEventBus } from '@snowluma/protocol/event-bus'; import { IdentityService } from '@snowluma/protocol/identity-service'; import { MSG_PUSH_CMD, parseMsgPush, SysMsgDedup } from '@snowluma/protocol/msg-push'; +import { KICK_NT_CMD, parseKickNT } from '@snowluma/protocol/kick-nt'; import { IncomingPacketPipeline, type CmdParser } from '@snowluma/protocol/packet-pipeline'; import { buildApiHub, type ApiHub } from './apis'; import { @@ -65,6 +66,7 @@ export class Bridge implements BridgeInterface { }, }); this.pipeline.registerCmd(MSG_PUSH_CMD, (pkt, identity) => parseMsgPush(pkt, identity, this.sysMsgDedup_)); + this.pipeline.registerCmd(KICK_NT_CMD, parseKickNT); } dispose(): void { diff --git a/packages/onebot/src/event-converter/index.ts b/packages/onebot/src/event-converter/index.ts index 3034ca8c..028c16f9 100644 --- a/packages/onebot/src/event-converter/index.ts +++ b/packages/onebot/src/event-converter/index.ts @@ -6,6 +6,7 @@ import { convertTempMessage, } from './to-message'; import { + convertBotOffline, convertFriendAdd, convertFriendInputStatus, convertFriendProfileLike, @@ -85,6 +86,7 @@ const CONVERTERS: ConverterRegistry = { friend_add: convertFriendAdd, friend_input_status: convertFriendInputStatus, friend_profile_like: convertFriendProfileLike, + bot_offline: convertBotOffline, group_name_change: convertGroupNameChange, group_title_change: convertGroupTitleChange, group_card_change: convertGroupCardChange, diff --git a/packages/onebot/src/event-converter/to-notice.ts b/packages/onebot/src/event-converter/to-notice.ts index 94d76e6b..2770aa57 100644 --- a/packages/onebot/src/event-converter/to-notice.ts +++ b/packages/onebot/src/event-converter/to-notice.ts @@ -22,6 +22,7 @@ type GroupNameChange = Extract; type GroupCardChange = Extract; type GroupTitleChange = Extract; type FriendProfileLike = Extract; +type BotOffline = Extract; export function convertGroupMemberJoin(ctx: ConverterContext, event: GroupMemberJoin): JsonObject { return notice(ctx, event, { @@ -173,6 +174,17 @@ export function convertGroupCardChange(ctx: ConverterContext, event: GroupCardCh }); } +export function convertBotOffline(ctx: ConverterContext, event: BotOffline): JsonObject { + // Mirrors NapCat's OB11BotOfflineEvent: notice_type bot_offline with the bot's + // own uin plus the tag/message describing why it went offline. + return notice(ctx, event, { + notice_type: 'bot_offline', + user_id: event.selfUin, + tag: event.tag, + message: event.message, + }); +} + export function convertFriendProfileLike(ctx: ConverterContext, event: FriendProfileLike): JsonObject { // Mirrors NapCat's OB11ProfileLikeEvent: notify/profile_like with the liker's // uin, nickname and like count. diff --git a/packages/onebot/src/event-pipeline.ts b/packages/onebot/src/event-pipeline.ts index 5079c481..1c278809 100644 --- a/packages/onebot/src/event-pipeline.ts +++ b/packages/onebot/src/event-pipeline.ts @@ -72,6 +72,7 @@ const NOTICE_KINDS = [ 'friend_add', 'friend_input_status', 'friend_profile_like', + 'bot_offline', 'group_name_change', 'group_title_change', 'group_card_change', diff --git a/packages/proto-defs/src/notify.ts b/packages/proto-defs/src/notify.ts index f106a46b..16d722ba 100644 --- a/packages/proto-defs/src/notify.ts +++ b/packages/proto-defs/src/notify.ts @@ -263,6 +263,18 @@ export interface GroupReactNotify { groupReactionData?: pb<44, GroupReactionData>; } +// Forced-offline ("被迫下线") push — SSO cmd +// `trpc.qq_new_tech.status_svc.StatusService.KickNT`. QQ NT's unified offline +// notification (kick / login-elsewhere / risk-control-triggered offline all +// arrive here; the reason is in the strings). Field layout RE'd from +// wrapper.linux.node (log fmt "tips_title:{} tips_content:{}") + Lagrange's +// ServiceKickNTResponse: f4 = tips_title (short title), f3 = tips_content (desc). +export interface KickNTResponse { + uin?: pb<1, uint_32>; + tips?: pb<3, string>; // tips_content — the longer description + title?: pb<4, string>; // tips_title — the short title +} + // Group-name change (Event 0x2DC subType 16, NotifyMessageBody.field13 == 12). // Rides in `NotifyMessageBody.eventParam` (field 5). Field layout matches // Lagrange's `GroupNameChange` (RE'd from wrapper.linux.node): only the new diff --git a/packages/protocol/package.json b/packages/protocol/package.json index 987ff78e..430f3c59 100644 --- a/packages/protocol/package.json +++ b/packages/protocol/package.json @@ -28,6 +28,10 @@ "types": "./src/packet-pipeline.ts", "default": "./src/packet-pipeline.ts" }, + "./kick-nt": { + "types": "./src/kick-nt.ts", + "default": "./src/kick-nt.ts" + }, "./element-builder": { "types": "./src/element-builder.ts", "default": "./src/element-builder.ts" diff --git a/packages/protocol/src/events.ts b/packages/protocol/src/events.ts index a015af0b..dade5237 100644 --- a/packages/protocol/src/events.ts +++ b/packages/protocol/src/events.ts @@ -294,6 +294,17 @@ export interface PttTransResultEvent extends QQEvent { text: string; } +/** + * The bot account was forced offline (kicked / logged in elsewhere / risk-control) + * — SSO push `StatusService.KickNT`. Mirrors NapCat's OB11BotOfflineEvent → + * `notice_type:'bot_offline'`. `tag` = short title, `message` = description. + */ +export interface BotOfflineEvent extends QQEvent { + kind: 'bot_offline'; + tag: string; + message: string; +} + /** * A group member's card (群名片) changed — detected from message traffic when * the sender's live card differs from the cached one (same mechanism as NapCat's @@ -377,4 +388,5 @@ export type QQEventVariant = | GroupNameChangeEvent | GroupCardChangeEvent | GroupTitleChangeEvent - | FriendProfileLikeEvent; + | FriendProfileLikeEvent + | BotOfflineEvent; diff --git a/packages/protocol/src/kick-nt.ts b/packages/protocol/src/kick-nt.ts new file mode 100644 index 00000000..c5f4483b --- /dev/null +++ b/packages/protocol/src/kick-nt.ts @@ -0,0 +1,33 @@ +// Forced-offline push. QQ NT tells a client it was kicked / logged in elsewhere / +// dropped for risk-control via the SSO command +// `trpc.qq_new_tech.status_svc.StatusService.KickNT`, whose body is a +// `KickNTResponse`. We surface it as a `bot_offline` event (NapCat parity). +// +// The push arrives on the same StatusService channel SnowLuma already receives +// (e.g. StatusService.SsoHeartBeat), so it just needs its own cmd handler. +// Command + field layout RE-confirmed against wrapper.linux.node + Lagrange's +// KickNTService/ServiceKickNTResponse. + +import { protobuf_decode } from '@snowluma/proton'; +import type { KickNTResponse } from '@snowluma/proto-defs/notify'; +import type { PacketInfo } from '@snowluma/common/protocol-types'; +import type { IdentityService } from './identity-service'; +import type { QQEventVariant } from './events'; + +export const KICK_NT_CMD = 'trpc.qq_new_tech.status_svc.StatusService.KickNT'; + +type BotOfflineEvent = Extract; + +export function parseKickNT(pkt: PacketInfo, identity: IdentityService): QQEventVariant[] { + const resp = protobuf_decode(pkt.body); + if (!resp) return []; + const ev: BotOfflineEvent = { + kind: 'bot_offline', + // KickNT carries no timestamp — stamp with receive time. + time: Math.floor(Date.now() / 1000), + selfUin: Number(pkt.uin) || Number(identity.uin) || 0, + tag: resp.title ?? '', // tips_title — the short title + message: resp.tips ?? '', // tips_content — the description / reason + }; + return [ev]; +} diff --git a/packages/protocol/tests/kick-nt.test.ts b/packages/protocol/tests/kick-nt.test.ts new file mode 100644 index 00000000..5501e203 --- /dev/null +++ b/packages/protocol/tests/kick-nt.test.ts @@ -0,0 +1,41 @@ +// StatusService.KickNT (forced-offline push) → bot_offline event. +// Command + field layout RE-confirmed against wrapper.linux.node + Lagrange's +// KickNTService/ServiceKickNTResponse (f4 = tips_title, f3 = tips_content). + +import { describe, expect, it } from 'vitest'; +import { protobuf_encode } from '@snowluma/proton'; +import { parseKickNT, KICK_NT_CMD } from '@snowluma/protocol/kick-nt'; +import { IdentityService } from '@snowluma/protocol/identity-service'; +import type { KickNTResponse } from '@snowluma/proto-defs/notify'; +import type { PacketInfo } from '@snowluma/common/protocol-types'; +import type { QQEventVariant } from '@snowluma/protocol/events'; + +type BotOfflineEvent = Extract; + +describe('parseKickNT (StatusService.KickNT → bot_offline)', () => { + const identity = IdentityService.memory('3433035623'); + + function pkt(resp: KickNTResponse, uin = '3433035623'): PacketInfo { + return { + pid: 1, uin, serviceCmd: KICK_NT_CMD, seqId: 1, retCode: 0, + fromClient: false, body: protobuf_encode(resp), + }; + } + + it('emits bot_offline with tag=title (f4) and message=tips (f3)', () => { + const [ev] = parseKickNT( + pkt({ uin: 3433035623, title: '你已离线', tips: '你的账号在其他设备登录' }), + identity, + ) as BotOfflineEvent[]; + + expect(ev.kind).toBe('bot_offline'); + expect(ev.selfUin).toBe(3433035623); + expect(ev.tag).toBe('你已离线'); + expect(ev.message).toBe('你的账号在其他设备登录'); + }); + + it('falls back to the identity uin when the packet uin is missing', () => { + const [ev] = parseKickNT(pkt({ title: 't', tips: 'm' }, ''), identity) as BotOfflineEvent[]; + expect(ev.selfUin).toBe(3433035623); + }); +});