diff --git a/README.md b/README.md index f94b8743..01becdc3 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,5 @@ qq-android协议的golang实现 移植于Mirai | 好友消息发送 | 正在做| | 群消息接受 |完成| | 群消息发送 | 50% (仅支持 文本/图片/表情)| -| QQ各种事件 | 正在做, 已支持 群内禁言/群内消息撤回/加群 | -| MiraiGo文档 | 咕咕| -| QQ协议说明文档| 自用整理 有空了应该会做| \ No newline at end of file +| QQ各种事件 | 正在做, 已支持 群内禁言/群内消息撤回/加群/新成员进群 | +| MiraiGo文档 | 咕咕| \ No newline at end of file diff --git a/client/client.go b/client/client.go index 6da519a2..5c48c064 100644 --- a/client/client.go +++ b/client/client.go @@ -14,6 +14,7 @@ import ( "math/rand" "net" "strconv" + "sync" "sync/atomic" "time" ) @@ -57,6 +58,8 @@ type QQClient struct { messageSeq int32 groupDataTransSeq int32 eventHandlers *eventHandlers + + groupListLock *sync.Mutex } type loginSigInfo struct { @@ -109,6 +112,7 @@ func NewClientMd5(uin int64, passwordMd5 [16]byte) *QQClient { messageSeq: 22911, ksid: []byte("|454001228437590|A8.2.7.27f6ea96"), eventHandlers: &eventHandlers{}, + groupListLock: new(sync.Mutex), } rand.Read(cli.RandomKey) return cli @@ -267,6 +271,8 @@ func (c *QQClient) ReloadGroupList() error { } func (c *QQClient) GetGroupList() ([]*GroupInfo, error) { + c.groupListLock.Lock() + defer c.groupListLock.Unlock() rsp, err := c.sendAndWait(c.buildGroupListRequestPacket()) if err != nil { return nil, err diff --git a/client/decoders.go b/client/decoders.go index 06209b33..49ded81c 100644 --- a/client/decoders.go +++ b/client/decoders.go @@ -385,6 +385,24 @@ func decodeOnlinePushReqPacket(c *QQClient, seq uint16, payload []byte) (interfa } } } + if m.MsgType == 528 { + vr := jce.NewJceReader(m.VMsg) + subType := vr.ReadInt64(0) + probuf := vr.ReadAny(10).([]byte) + switch subType { + case 0xD4: + d4 := pb.SubD4{} + if err := proto.Unmarshal(probuf, &d4); err != nil { + return nil, err + } + if g := c.FindGroup(d4.Uin); g != nil { + if err := c.ReloadGroupList(); err != nil { + return nil, err + } + c.dispatchLeaveGroupEvent(&GroupLeaveEvent{Group: g}) + } + } + } } return nil, nil @@ -396,5 +414,26 @@ func decodeOnlinePushTransPacket(c *QQClient, seq uint16, payload []byte) (inter if err != nil { return nil, err } + data := binary.NewReader(info.MsgData) + if info.MsgType == 34 { + data.ReadInt32() + data.ReadByte() + _ = int64(uint32(data.ReadInt32())) + typ := int32(data.ReadByte()) + operator := int64(uint32(data.ReadInt32())) + switch typ { + case 0x03: + if g := c.FindGroup(info.FromUin); g != nil { + if err = c.ReloadGroupList(); err != nil { + return nil, err + } + c.dispatchLeaveGroupEvent(&GroupLeaveEvent{ + Group: g, + Operator: g.FindMember(operator), + }) + } + } + + } return nil, nil } diff --git a/client/entities.go b/client/entities.go index 0f132910..9a71e2ac 100644 --- a/client/entities.go +++ b/client/entities.go @@ -76,6 +76,11 @@ type ( Time int32 } + GroupLeaveEvent struct { + Group *GroupInfo + Operator *GroupMemberInfo + } + groupMemberListResponse struct { NextUin int64 list []*GroupMemberInfo diff --git a/client/events.go b/client/events.go index c202b6c4..f719a7a9 100644 --- a/client/events.go +++ b/client/events.go @@ -1,43 +1,21 @@ package client import ( - "errors" "github.com/Mrs4s/MiraiGo/message" "sync" ) -var ErrEventUndefined = errors.New("event undefined") - type eventHandlers struct { privateMessageHandlers []func(*QQClient, *message.PrivateMessage) groupMessageHandlers []func(*QQClient, *message.GroupMessage) groupMuteEventHandlers []func(*QQClient, *GroupMuteEvent) groupRecalledHandlers []func(*QQClient, *GroupMessageRecalledEvent) joinGroupHandlers []func(*QQClient, *GroupInfo) + leaveGroupHandlers []func(*QQClient, *GroupLeaveEvent) memberJoinedHandlers []func(*QQClient, *GroupInfo, *GroupMemberInfo) groupMessageReceiptHandlers sync.Map } -func (c *QQClient) OnEvent(i interface{}) error { - switch f := i.(type) { - case func(*QQClient, *message.PrivateMessage): - c.OnPrivateMessage(f) - case func(*QQClient, *message.GroupMessage): - c.OnGroupMessage(f) - case func(*QQClient, *GroupMuteEvent): - c.OnGroupMuted(f) - case func(*QQClient, *GroupMessageRecalledEvent): - c.OnGroupMessageRecalled(f) - case func(*QQClient, *GroupInfo): - c.OnJoinGroup(f) - case func(*QQClient, *GroupInfo, *GroupMemberInfo): - c.OnGroupMemberJoined(f) - default: - return ErrEventUndefined - } - return nil -} - func (c *QQClient) OnPrivateMessage(f func(*QQClient, *message.PrivateMessage)) { c.eventHandlers.privateMessageHandlers = append(c.eventHandlers.privateMessageHandlers, f) } @@ -62,6 +40,10 @@ func (c *QQClient) OnJoinGroup(f func(*QQClient, *GroupInfo)) { c.eventHandlers.joinGroupHandlers = append(c.eventHandlers.joinGroupHandlers, f) } +func (c *QQClient) OnLeaveGroup(f func(*QQClient, *GroupLeaveEvent)) { + c.eventHandlers.leaveGroupHandlers = append(c.eventHandlers.leaveGroupHandlers, f) +} + func (c *QQClient) OnGroupMemberJoined(f func(*QQClient, *GroupInfo, *GroupMemberInfo)) { c.eventHandlers.memberJoinedHandlers = append(c.eventHandlers.memberJoinedHandlers, f) } @@ -139,6 +121,17 @@ func (c *QQClient) dispatchJoinGroupEvent(group *GroupInfo) { } } +func (c *QQClient) dispatchLeaveGroupEvent(e *GroupLeaveEvent) { + if e == nil { + return + } + for _, f := range c.eventHandlers.leaveGroupHandlers { + cover(func() { + f(c, e) + }) + } +} + func (c *QQClient) dispatchNewMemberEvent(group *GroupInfo, mem *GroupMemberInfo) { if group == nil || mem == nil { return diff --git a/client/pb/data.pb.go b/client/pb/data.pb.go index e7b804e3..6da4ad88 100644 --- a/client/pb/data.pb.go +++ b/client/pb/data.pb.go @@ -1711,6 +1711,53 @@ func (x *RecalledMessageMeta) GetAuthorUin() int64 { return 0 } +type SubD4 struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Uin int64 `protobuf:"varint,1,opt,name=uin,proto3" json:"uin,omitempty"` +} + +func (x *SubD4) Reset() { + *x = SubD4{} + if protoimpl.UnsafeEnabled { + mi := &file_data_proto_msgTypes[18] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SubD4) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SubD4) ProtoMessage() {} + +func (x *SubD4) ProtoReflect() protoreflect.Message { + mi := &file_data_proto_msgTypes[18] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use SubD4.ProtoReflect.Descriptor instead. +func (*SubD4) Descriptor() ([]byte, []int) { + return file_data_proto_rawDescGZIP(), []int{18} +} + +func (x *SubD4) GetUin() int64 { + if x != nil { + return x.Uin + } + return 0 +} + var File_data_proto protoreflect.FileDescriptor var file_data_proto_rawDesc = []byte{ @@ -1971,8 +2018,9 @@ var file_data_proto_rawDesc = []byte{ 0x6d, 0x73, 0x67, 0x46, 0x6c, 0x61, 0x67, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x6d, 0x73, 0x67, 0x46, 0x6c, 0x61, 0x67, 0x12, 0x1c, 0x0a, 0x09, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x55, 0x69, 0x6e, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x61, 0x75, 0x74, 0x68, 0x6f, - 0x72, 0x55, 0x69, 0x6e, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x33, + 0x72, 0x55, 0x69, 0x6e, 0x22, 0x19, 0x0a, 0x05, 0x53, 0x75, 0x62, 0x44, 0x34, 0x12, 0x10, 0x0a, + 0x03, 0x75, 0x69, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x03, 0x75, 0x69, 0x6e, 0x42, + 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -1987,7 +2035,7 @@ func file_data_proto_rawDescGZIP() []byte { return file_data_proto_rawDescData } -var file_data_proto_msgTypes = make([]protoimpl.MessageInfo, 18) +var file_data_proto_msgTypes = make([]protoimpl.MessageInfo, 19) var file_data_proto_goTypes = []interface{}{ (*DeviceInfo)(nil), // 0: DeviceInfo (*RequestBody)(nil), // 1: RequestBody @@ -2007,6 +2055,7 @@ var file_data_proto_goTypes = []interface{}{ (*NotifyMsgBody)(nil), // 15: NotifyMsgBody (*MessageRecallReminder)(nil), // 16: MessageRecallReminder (*RecalledMessageMeta)(nil), // 17: RecalledMessageMeta + (*SubD4)(nil), // 18: SubD4 } var file_data_proto_depIdxs = []int32{ 2, // 0: RequestBody.rpt_config_list:type_name -> ConfigSeq @@ -2249,6 +2298,18 @@ func file_data_proto_init() { return nil } } + file_data_proto_msgTypes[18].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SubD4); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } type x struct{} out := protoimpl.TypeBuilder{ @@ -2256,7 +2317,7 @@ func file_data_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_data_proto_rawDesc, NumEnums: 0, - NumMessages: 18, + NumMessages: 19, NumExtensions: 0, NumServices: 0, }, diff --git a/client/pb/data.proto b/client/pb/data.proto index a9227630..0a59a4fb 100644 --- a/client/pb/data.proto +++ b/client/pb/data.proto @@ -179,4 +179,8 @@ message RecalledMessageMeta { int32 msgType = 4; int32 msgFlag = 5; int64 authorUin = 6; +} + +message SubD4 { + int64 uin = 1; } \ No newline at end of file