forked from LagrangeDev/LagrangeGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
176 lines (168 loc) · 5.49 KB
/
listener.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package client
import (
"errors"
"runtime/debug"
"github.com/RomiChan/protobuf/proto"
eventConverter "github.com/2mf8/LagrangeGo/client/event"
"github.com/2mf8/LagrangeGo/client/internal/network"
"github.com/2mf8/LagrangeGo/client/packets/pb/message"
msgConverter "github.com/2mf8/LagrangeGo/message"
"github.com/2mf8/LagrangeGo/utils"
"github.com/2mf8/LagrangeGo/utils/binary"
)
// decoders https://github.com/Mrs4s/MiraiGo/blob/54bdd873e3fed9fe1c944918924674dacec5ac76/client/client.go#L150
var decoders = map[string]func(*QQClient, *network.Packet) (any, error){
"trpc.msg.olpush.OlPushService.MsgPush": decodeOlPushServicePacket,
"trpc.qq_new_tech.status_svc.StatusService.KickNT": decodeKickNTPacket,
}
func decodeOlPushServicePacket(c *QQClient, pkt *network.Packet) (any, error) {
msg := message.PushMsg{}
err := proto.Unmarshal(pkt.Payload, &msg)
if err != nil {
return nil, err
}
pkg := msg.Message
typ := pkg.ContentHead.Type
defer func() {
if r := recover(); r != nil {
networkLogger.Errorf("recovered from panic: %v\n%s", r, debug.Stack())
networkLogger.Errorf("protobuf data: %x", pkt.Payload)
}
}()
if pkg.Body == nil {
return nil, errors.New("message body is empty")
}
switch typ {
case 166: // private msg
c.PrivateMessageEvent.dispatch(c, msgConverter.ParsePrivateMessage(&msg))
return nil, nil
case 82: // group msg
c.GroupMessageEvent.dispatch(c, msgConverter.ParseGroupMessage(&msg))
return nil, nil
case 141: // temp msg
c.TempMessageEvent.dispatch(c, msgConverter.ParseTempMessage(&msg))
return nil, nil
case 33: // member increase
pb := message.GroupChange{}
err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
if err != nil {
return nil, err
}
c.GroupMemberJoinEvent.dispatch(c, eventConverter.ParseMemberIncreaseEvent(&pb))
return nil, nil
case 34: // member decrease
pb := message.GroupChange{}
err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
if err != nil {
return nil, err
}
// 3 是bot自身被踢出,Operator字段会是一个protobuf
if pb.DecreaseType == 3 && pb.Operator != nil {
Operator := message.OperatorInfo{}
err = proto.Unmarshal(pb.Operator, &Operator)
if err != nil {
return nil, err
}
pb.Operator = utils.S2B(Operator.OperatorField1.OperatorUid)
}
c.GroupMemberLeaveEvent.dispatch(c, eventConverter.ParseMemberDecreaseEvent(&pb))
return nil, nil
case 84: // group request join notice
pb := message.GroupJoin{}
err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
if err != nil {
return nil, err
}
c.GroupMemberJoinRequestEvent.dispatch(c, eventConverter.ParseRequestJoinNotice(&pb))
return nil, nil
case 525: // group request invitation notice
pb := message.GroupInvitation{}
err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
if err != nil {
return nil, err
}
c.GroupMemberJoinRequestEvent.dispatch(c, eventConverter.ParseRequestInvitationNotice(&pb))
return nil, nil
case 87: // group invite notice
pb := message.GroupInvite{}
err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
if err != nil {
return nil, err
}
c.GroupInvitedEvent.dispatch(c, eventConverter.ParseInviteNotice(&pb))
return nil, nil
case 0x210: // friend event, 528
subType := pkg.ContentHead.SubType.Unwrap()
switch subType {
case 35: // friend request notice
pb := message.FriendRequest{}
err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
if err != nil {
return nil, err
}
c.FriendRequestEvent.dispatch(c, eventConverter.ParseFriendRequestNotice(&pb, &msg))
return nil, nil
case 138: // friend recall
pb := message.FriendRecall{}
err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
if err != nil {
return nil, err
}
c.FriendRecallEvent.dispatch(c, eventConverter.ParseFriendRecallEvent(&pb))
return nil, nil
case 39: // friend rename
networkLogger.Debugln("friend rename")
pb := message.FriendRenameMsg{}
err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
if err != nil {
return nil, err
}
c.RenameEvent.dispatch(c, eventConverter.ParseFriendRenameEvent(&pb, c.cache.GetUin(pb.Body.Data.Uid)))
return nil, nil
case 29:
networkLogger.Debugln("self rename")
pb := message.SelfRenameMsg{}
err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
if err != nil {
return nil, err
}
c.RenameEvent.dispatch(c, eventConverter.ParseSelfRenameEvent(&pb, &c.transport.Sig))
return nil, nil
default:
networkLogger.Warningf("unknown subtype %d of type 0x210, proto data: %x", subType, pkg.Body.MsgContent)
}
case 0x2DC: // grp event, 732
subType := pkg.ContentHead.SubType.Unwrap()
switch subType {
case 20: // nudget(grp_id only)
return nil, nil
case 17: // recall
reader := binary.NewReader(pkg.Body.MsgContent)
_ = reader.ReadU32() // group uin
_ = reader.ReadU8() // reserve
pb := message.NotifyMessageBody{}
err = proto.Unmarshal(reader.ReadBytesWithLength("u16", false), &pb)
if err != nil {
return nil, err
}
c.GroupRecallEvent.dispatch(c, eventConverter.ParseGroupRecallEvent(&pb))
return nil, nil
case 12: // mute
pb := message.GroupMute{}
err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
if err != nil {
return nil, err
}
c.GroupMuteEvent.dispatch(c, eventConverter.ParseGroupMuteEvent(&pb))
return nil, nil
default:
networkLogger.Warningf("Unsupported group event, subType: %v", subType)
}
default:
networkLogger.Warningf("Unsupported message type: %v", typ)
}
return nil, nil
}
func decodeKickNTPacket(c *QQClient, pkt *network.Packet) (any, error) {
return nil, nil
}