diff --git a/packages/core/tests/msg-push-handler.test.ts b/packages/core/tests/msg-push-handler.test.ts index 4aeac485..e3f0e19f 100644 --- a/packages/core/tests/msg-push-handler.test.ts +++ b/packages/core/tests/msg-push-handler.test.ts @@ -14,7 +14,7 @@ import type { GroupMemberInfo, QQGroupInfo } from '@snowluma/protocol/qq-info'; // inline-extract the kind. import type { GroupChange, NewFriend, FriendRecall, OperatorInfo, SelfJoinInGroup, GroupAdmin, - InputStatusNotify, + InputStatusNotify, NotifyMessageBody, GroupNameChange, } from '@snowluma/proto-defs/notify'; import type { PushMsg } from '@snowluma/proto-defs/message'; import type { @@ -302,6 +302,55 @@ describe('parseMsgPush Event0x210 subType=277 (input status → 对方正在输 }); }); +describe('parseMsgPush Event0x2DC subType=16 field13=12 (group name change)', () => { + // RE cross-ref: Lagrange Event0x2DCSubType16Field13.GroupNameChangeNotice = 12, + // EventParam(f5) → GroupNameChange{f2=name}, operator = NotifyMessageBody.f21. + // The 0x2DC subType16 body carries a 7-byte prefix (groupUin+byte+len) before + // the NotifyMessageBody proto — the decoder strips it via subarray(7). + type GroupNameChangeEvent = Extract; + + function makeGroupNamePacket(groupUin: number, operatorUid: string, name: string): PacketInfo { + const notify = protobuf_encode({ + groupUin, + field13: 12, + operatorUid, + eventParam: protobuf_encode({ name }), + }); + const content = new Uint8Array(7 + notify.length); // 7-byte prefix + proto + content.set(notify, 7); + const body = protobuf_encode({ + message: { + responseHead: { fromUin: 0, type: 0, sigMap: 0 }, + contentHead: { msgType: 732, subType: 16, timestamp: 1710000000 }, + body: { msgContent: content }, + }, + status: 0, + }); + return { pid: 1, uin: SELF_UIN, serviceCmd: MSG_PUSH_CMD, seqId: 1, retCode: 0, fromClient: false, body }; + } + + it('emits group_name_change with the new name + operator', () => { + const member = makeGroupMember(54321, 'u_operator'); + const [event] = parseMsgPush( + makeGroupNamePacket(GROUP_ID, member.uid, '新的群名'), + makeIdentity([member]), + ) as GroupNameChangeEvent[]; + + expect(event.kind).toBe('group_name_change'); + expect(event.groupId).toBe(GROUP_ID); + expect(event.name).toBe('新的群名'); + expect(event.operatorUin).toBe(54321); + }); + + it('drops a name-change body with an empty name', () => { + const events = parseMsgPush( + makeGroupNamePacket(GROUP_ID, 'u_operator', ''), + makeIdentity(), + ); + expect(events).toEqual([]); + }); +}); + describe('parseMsgPush Event0x210 subType=139 (self-recall direction)', () => { // 138 = friend recalled their own message sent to bot // (peer in friend_recall event = fromUid) diff --git a/packages/onebot/src/event-converter/index.ts b/packages/onebot/src/event-converter/index.ts index 1e15ac83..eb8b7e08 100644 --- a/packages/onebot/src/event-converter/index.ts +++ b/packages/onebot/src/event-converter/index.ts @@ -17,6 +17,7 @@ import { convertGroupMemberLeave, convertGroupMsgEmojiLike, convertGroupMute, + convertGroupNameChange, convertGroupPoke, convertGroupRecall, } from './to-notice'; @@ -80,6 +81,7 @@ const CONVERTERS: ConverterRegistry = { group_file_upload: convertGroupFileUpload, friend_add: convertFriendAdd, friend_input_status: convertFriendInputStatus, + group_name_change: convertGroupNameChange, group_msg_emoji_like: convertGroupMsgEmojiLike, // Requests. friend_request: convertFriendRequest, diff --git a/packages/onebot/src/event-converter/to-notice.ts b/packages/onebot/src/event-converter/to-notice.ts index cec7659f..c878e85b 100644 --- a/packages/onebot/src/event-converter/to-notice.ts +++ b/packages/onebot/src/event-converter/to-notice.ts @@ -18,6 +18,7 @@ type GroupFileUpload = Extract; type FriendAdd = Extract; type GroupMsgEmojiLike = Extract; type FriendInputStatus = Extract; +type GroupNameChange = Extract; export function convertGroupMemberJoin(ctx: ConverterContext, event: GroupMemberJoin): JsonObject { return notice(ctx, event, { @@ -157,6 +158,18 @@ export function convertFriendAdd(ctx: ConverterContext, event: FriendAdd): JsonO }); } +export function convertGroupNameChange(ctx: ConverterContext, event: GroupNameChange): JsonObject { + // Mirrors NapCat's OB11GroupNameEvent: notify/group_name with the operator's + // uin as user_id and the new name in `name_new`. + return notice(ctx, event, { + notice_type: 'notify', + sub_type: 'group_name', + group_id: event.groupId, + user_id: event.operatorUin, + name_new: event.name, + }); +} + export function convertFriendInputStatus(ctx: ConverterContext, event: FriendInputStatus): JsonObject { // Mirrors NapCat's OB11InputStatusEvent: notify/input_status with the peer's // uin, the raw event_type (1 = typing, 3 = recording voice) and status_text. diff --git a/packages/onebot/src/event-pipeline.ts b/packages/onebot/src/event-pipeline.ts index a96a8eb7..f9615b9c 100644 --- a/packages/onebot/src/event-pipeline.ts +++ b/packages/onebot/src/event-pipeline.ts @@ -71,6 +71,7 @@ const NOTICE_KINDS = [ 'group_file_upload', 'friend_add', 'friend_input_status', + 'group_name_change', 'group_msg_emoji_like', ] as const satisfies readonly QQEventVariant['kind'][]; diff --git a/packages/proto-defs/src/notify.ts b/packages/proto-defs/src/notify.ts index b32292e6..408d1947 100644 --- a/packages/proto-defs/src/notify.ts +++ b/packages/proto-defs/src/notify.ts @@ -263,6 +263,14 @@ export interface GroupReactNotify { groupReactionData?: pb<44, GroupReactionData>; } +// 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 +// name at field 2. The operator uid is `NotifyMessageBody.operatorUid` (f21). +export interface GroupNameChange { + name?: pb<2, string>; +} + // C2C input-status notify — the "对方正在输入…" push. Delivered as a system // message (msgType 0x210 / subMsgType 0x115) whose `MsgBody.msgContent` carries // this body. Field layout RE'd from `wrapper.linux.node` diff --git a/packages/protocol/src/events.ts b/packages/protocol/src/events.ts index a06cb16f..54b74321 100644 --- a/packages/protocol/src/events.ts +++ b/packages/protocol/src/events.ts @@ -294,6 +294,17 @@ export interface PttTransResultEvent extends QQEvent { text: string; } +/** + * Group name changed (Event 0x2DC subType 16, field13 == 12). Mirrors NapCat's + * OB11GroupNameEvent → `notice/notify` `sub_type:'group_name'`. + */ +export interface GroupNameChangeEvent extends QQEvent { + kind: 'group_name_change'; + groupId: number; + operatorUin: number; // who renamed the group + name: string; // the new group name +} + /** * C2C "对方正在输入…" input-status push (Event 0x210 subType 0x115 / 277). * `eventType` 1 = typing, 3 = recording a voice message. Mirrors NapCat's @@ -326,4 +337,5 @@ export type QQEventVariant = | FriendAddEvent | GroupMsgEmojiLikeEvent | PttTransResultEvent - | FriendInputStatusEvent; + | FriendInputStatusEvent + | GroupNameChangeEvent; diff --git a/packages/protocol/src/msg-push/decoders/event-0x2dc.ts b/packages/protocol/src/msg-push/decoders/event-0x2dc.ts index ca0b4dbe..116251e3 100644 --- a/packages/protocol/src/msg-push/decoders/event-0x2dc.ts +++ b/packages/protocol/src/msg-push/decoders/event-0x2dc.ts @@ -4,12 +4,14 @@ import type { GroupEssenceEvent, GroupMsgEmojiLikeEvent, GroupMuteEvent, + GroupNameChangeEvent, GroupPokeEvent, GroupRecallEvent, QQEventVariant, } from '../../events'; import type { GroupMute, + GroupNameChange, GroupReactNotify, NotifyMessageBody, } from '@snowluma/proto-defs/notify'; @@ -28,7 +30,7 @@ const unknownLog = createLogger('MsgPush.Unknown'); export const decodeEvent0x2DC: MsgPushDecoder = (ctx) => { switch (ctx.head.subType as Event0x2DCSubType) { case Event0x2DCSubType.GroupMuteNotice: return decodeGroupMute(ctx); - case Event0x2DCSubType.GroupMsgEmojiLikeNotice: return decodeGroupMsgEmojiLike(ctx); + case Event0x2DCSubType.GroupMsgEmojiLikeNotice: return decodeSubType16(ctx); case Event0x2DCSubType.GroupRecallNotice: return decodeGroupRecall(ctx); case Event0x2DCSubType.GroupGreyTipNotice: return decodeGroupGreyTip(ctx); case Event0x2DCSubType.GroupEssenceNotice: return decodeGroupEssence(ctx); @@ -123,6 +125,44 @@ const GROUP_REACT_PREFIX_BYTES = 7; // for other notify variants, only 35 means "emoji react". Anything // else falls through to MsgPush.Unknown for protocol-drift visibility. const GROUP_REACT_DISCRIMINATOR = 35; +// Event 0x2DC subType 16 multiplexes several group notices, discriminated by +// `NotifyMessageBody.field13`. RE cross-ref (Lagrange Event0x2DCSubType16Field13, +// itself RE'd from wrapper.linux.node): 6=special title, 12=name change, 23=todo, +// 35=emoji reaction. We peek field13 off the shared NotifyMessageBody, then branch. +const SUBTYPE16_GROUP_NAME_CHANGE = 12; + +function decodeSubType16(ctx: MsgPushContext): QQEventVariant[] { + if (ctx.content.length <= GROUP_REACT_PREFIX_BYTES) return []; + const field13 = protobuf_decode(ctx.content.subarray(GROUP_REACT_PREFIX_BYTES))?.field13 ?? 0; + switch (field13) { + case SUBTYPE16_GROUP_NAME_CHANGE: return decodeGroupName(ctx); + case GROUP_REACT_DISCRIMINATOR: return decodeGroupMsgEmojiLike(ctx); + default: + unknownLog.debug('Event0x2DC subType=16 unhandled field13=%d', field13); + return []; + } +} + +// field13 == 12: group renamed. New name is nested in `eventParam` as a +// `GroupNameChange` (field 2); operator is `NotifyMessageBody.operatorUid`. +function decodeGroupName(ctx: MsgPushContext): QQEventVariant[] { + const notify = protobuf_decode(ctx.content.subarray(GROUP_REACT_PREFIX_BYTES)); + const name = notify?.eventParam ? (protobuf_decode(notify.eventParam)?.name ?? '') : ''; + if (!name) return []; + const groupId = notify?.groupUin ?? 0; + const ev: GroupNameChangeEvent = { + kind: 'group_name_change', + time: ctx.head.timestamp, + selfUin: ctx.selfUin, + groupId, + // Operator is a group member → resolves from the roster. Fall back to 0 + // (not ctx.fromUin, which on a 0x2DC push is the group id) so an + // unresolved operator never surfaces the group id as user_id. + operatorUin: resolveUidToUin(ctx.identity, groupId, notify?.operatorUid ?? '', 0), + name, + }; + return [ev]; +} function decodeGroupMsgEmojiLike(ctx: MsgPushContext): QQEventVariant[] { if (ctx.content.length <= GROUP_REACT_PREFIX_BYTES) return [];