forked from Mrs4s/MiraiGo
/
c2c_processor.go
306 lines (284 loc) · 9.55 KB
/
c2c_processor.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package client
import (
"fmt"
"sync/atomic"
"time"
"github.com/asjdf/MiraiGo/binary"
"github.com/asjdf/MiraiGo/client/pb"
"github.com/asjdf/MiraiGo/client/pb/msg"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)
var privateMsgDecoders = map[int32]func(*QQClient, *msg.Message, *incomingPacketInfo){
9: privateMessageDecoder, 10: privateMessageDecoder, 31: privateMessageDecoder,
79: privateMessageDecoder, 97: privateMessageDecoder, 120: privateMessageDecoder,
132: privateMessageDecoder, 133: privateMessageDecoder, 166: privateMessageDecoder,
167: privateMessageDecoder, 140: tempSessionDecoder, 141: tempSessionDecoder,
208: privatePttDecoder,
}
var troopSystemMsgDecoders = map[int32]func(*QQClient, *msg.Message, *incomingPacketInfo){
35: troopSystemMessageDecoder, 36: troopSystemMessageDecoder, 37: troopSystemMessageDecoder,
45: troopSystemMessageDecoder, 46: troopSystemMessageDecoder, 84: troopSystemMessageDecoder,
85: troopSystemMessageDecoder, 86: troopSystemMessageDecoder, 87: troopSystemMessageDecoder,
}
var sysMsgDecoders = map[int32]func(*QQClient, *msg.Message, *incomingPacketInfo){
187: systemMessageDecoder, 188: systemMessageDecoder, 189: systemMessageDecoder,
190: systemMessageDecoder, 191: systemMessageDecoder,
}
var otherDecoders = map[int32]func(*QQClient, *msg.Message, *incomingPacketInfo){
33: troopAddMemberBroadcastDecoder, 529: msgType0x211Decoder,
}
var c2cDecoders = map[int32]func(*QQClient, *msg.Message, *incomingPacketInfo){}
type (
TempSessionInfo struct {
Source TempSessionSource
GroupCode int64
Sender int64
sig []byte
client *QQClient
}
TempSessionSource int
)
const (
GroupSource TempSessionSource = 0 // 来自群聊
ConsultingSource TempSessionSource = 1 // 来自QQ咨询
SearchSource TempSessionSource = 2 // 来自查找
MovieSource TempSessionSource = 3 // 来自QQ电影
HotChatSource TempSessionSource = 4 // 来自热聊
SystemMessageSource TempSessionSource = 6 // 来自验证消息
MultiChatSource TempSessionSource = 7 // 来自多人聊天
DateSource TempSessionSource = 8 // 来自约会
AddressBookSource TempSessionSource = 9 // 来自通讯录
)
func init() {
merge := func(m map[int32]func(*QQClient, *msg.Message, *incomingPacketInfo)) {
for k, v := range m {
c2cDecoders[k] = v
}
}
merge(privateMsgDecoders)
merge(troopSystemMsgDecoders)
merge(sysMsgDecoders)
merge(otherDecoders)
}
func (c *QQClient) c2cMessageSyncProcessor(rsp *msg.GetMessageResponse, info *incomingPacketInfo) {
c.syncCookie = rsp.SyncCookie
c.pubAccountCookie = rsp.PubAccountCookie
c.msgCtrlBuf = rsp.MsgCtrlBuf
if rsp.UinPairMsgs == nil {
return
}
var delItems []*pb.MessageItem
for _, pairMsg := range rsp.UinPairMsgs {
for _, pMsg := range pairMsg.Messages {
// delete message
delItem := &pb.MessageItem{
FromUin: pMsg.Head.GetFromUin(),
ToUin: pMsg.Head.GetToUin(),
MsgType: pMsg.Head.GetMsgType(),
MsgSeq: pMsg.Head.GetMsgSeq(),
MsgUid: pMsg.Head.GetMsgUid(),
}
delItems = append(delItems, delItem)
if pMsg.Head.GetToUin() != c.Uin {
continue
}
if (int64(pairMsg.GetLastReadTime()) & 4294967295) > int64(pMsg.Head.GetMsgTime()) {
continue
}
c.commMsgProcessor(pMsg, info)
}
}
if delItems != nil {
_, _ = c.sendAndWait(c.buildDeleteMessageRequestPacket(delItems))
}
if rsp.GetSyncFlag() != msg.SyncFlag_STOP {
c.Debug("continue sync with flag: %v", rsp.SyncFlag.String())
seq, pkt := c.buildGetMessageRequestPacket(rsp.GetSyncFlag(), time.Now().Unix())
_, _ = c.sendAndWait(seq, pkt, info.Params)
}
}
func (c *QQClient) commMsgProcessor(pMsg *msg.Message, info *incomingPacketInfo) {
strKey := fmt.Sprintf("%d%d%d%d", pMsg.Head.GetFromUin(), pMsg.Head.GetToUin(), pMsg.Head.GetMsgSeq(), pMsg.Head.GetMsgUid())
if _, ok := c.msgSvcCache.GetAndUpdate(strKey, time.Hour); ok {
c.Debug("c2c msg %v already exists in cache. skip.", pMsg.Head.GetMsgUid())
return
}
c.msgSvcCache.Add(strKey, "", time.Hour)
if c.lastC2CMsgTime > int64(pMsg.Head.GetMsgTime()) && (c.lastC2CMsgTime-int64(pMsg.Head.GetMsgTime())) > 60*10 {
c.Debug("c2c msg filtered by time. lastMsgTime: %v msgTime: %v", c.lastC2CMsgTime, pMsg.Head.GetMsgTime())
return
}
c.lastC2CMsgTime = int64(pMsg.Head.GetMsgTime())
if info.Params.bool("init") {
return
}
if decoder, ok := c2cDecoders[pMsg.Head.GetMsgType()]; ok {
decoder(c, pMsg, info)
} else {
c.Debug("unknown msg type on c2c processor: %v - %v", pMsg.Head.GetMsgType(), pMsg.Head.GetC2CCmd())
}
}
func privateMessageDecoder(c *QQClient, pMsg *msg.Message, _ *incomingPacketInfo) {
switch pMsg.Head.GetC2CCmd() {
case 11, 175: // friend msg
if pMsg.Head.GetFromUin() == c.Uin {
for {
frdSeq := atomic.LoadInt32(&c.friendSeq)
if frdSeq < pMsg.Head.GetMsgSeq() {
if atomic.CompareAndSwapInt32(&c.friendSeq, frdSeq, pMsg.Head.GetMsgSeq()) {
break
}
} else {
break
}
}
}
if pMsg.Body.RichText == nil || pMsg.Body.RichText.Elems == nil {
return
}
if pMsg.Head.GetFromUin() == c.Uin {
c.dispatchPrivateMessageSelf(c.parsePrivateMessage(pMsg))
return
}
c.dispatchPrivateMessage(c.parsePrivateMessage(pMsg))
default:
c.Debug("unknown c2c cmd on private msg decoder: %v", pMsg.Head.GetC2CCmd())
}
}
func privatePttDecoder(c *QQClient, pMsg *msg.Message, _ *incomingPacketInfo) {
if pMsg.Body == nil || pMsg.Body.RichText == nil || pMsg.Body.RichText.Ptt == nil {
return
}
if len(pMsg.Body.RichText.Ptt.Reserve) != 0 {
// m := binary.NewReader(pMsg.Body.RichText.Ptt.Reserve[1:]).ReadTlvMap(1)
// T3 -> timestamp T8 -> voiceType T9 -> voiceLength T10 -> PbReserveStruct
}
c.dispatchPrivateMessage(c.parsePrivateMessage(pMsg))
}
func tempSessionDecoder(c *QQClient, pMsg *msg.Message, _ *incomingPacketInfo) {
if pMsg.Head.C2CTmpMsgHead == nil || pMsg.Body == nil {
return
}
if (pMsg.Head.GetMsgType() == 529 && pMsg.Head.GetC2CCmd() == 6) || pMsg.Body.RichText != nil {
genTempSessionInfo := func() *TempSessionInfo {
if pMsg.Head.C2CTmpMsgHead.GetServiceType() == 0 {
group := c.FindGroup(pMsg.Head.C2CTmpMsgHead.GetGroupCode())
if group == nil {
return nil
}
return &TempSessionInfo{
Source: GroupSource,
GroupCode: group.Code,
Sender: pMsg.Head.GetFromUin(),
client: c,
}
}
info := &TempSessionInfo{
Source: 0,
Sender: pMsg.Head.GetFromUin(),
sig: pMsg.Head.C2CTmpMsgHead.GetSig(),
client: c,
}
switch pMsg.Head.C2CTmpMsgHead.GetServiceType() {
case 1:
info.Source = MultiChatSource
case 130:
info.Source = AddressBookSource
case 132:
info.Source = HotChatSource
case 134:
info.Source = SystemMessageSource
case 201:
info.Source = ConsultingSource
default:
return nil
}
return info
}
session := genTempSessionInfo()
if session == nil {
return
}
/*
group := c.FindGroup(pMsg.Head.C2CTmpMsgHead.GetGroupCode())
if group == nil {
return
}
*/
if pMsg.Head.GetFromUin() == c.Uin {
return
}
c.dispatchTempMessage(&TempMessageEvent{
Message: c.parseTempMessage(pMsg),
Session: session,
})
}
}
func troopAddMemberBroadcastDecoder(c *QQClient, pMsg *msg.Message, _ *incomingPacketInfo) {
groupJoinLock.Lock()
defer groupJoinLock.Unlock()
group := c.FindGroupByUin(pMsg.Head.GetFromUin())
if pMsg.Head.GetAuthUin() == c.Uin {
if group == nil && c.ReloadGroupList() == nil {
c.dispatchJoinGroupEvent(c.FindGroupByUin(pMsg.Head.GetFromUin()))
}
} else {
if group != nil && group.FindMember(pMsg.Head.GetAuthUin()) == nil {
mem, err := c.GetMemberInfo(group.Code, pMsg.Head.GetAuthUin())
if err != nil {
c.Debug("error to fetch new member info: %v", err)
return
}
group.Update(func(info *GroupInfo) {
info.Members = append(info.Members, mem)
info.sort()
})
c.dispatchNewMemberEvent(&MemberJoinGroupEvent{
Group: group,
Member: mem,
})
}
}
}
func systemMessageDecoder(c *QQClient, _ *msg.Message, _ *incomingPacketInfo) {
_, pkt := c.buildSystemMsgNewFriendPacket()
_ = c.sendPacket(pkt)
}
func troopSystemMessageDecoder(c *QQClient, pMsg *msg.Message, info *incomingPacketInfo) {
if !info.Params.bool("used_reg_proxy") && pMsg.Head.GetMsgType() != 85 && pMsg.Head.GetMsgType() != 36 {
c.exceptAndDispatchGroupSysMsg()
}
if len(pMsg.Body.GetMsgContent()) == 0 {
return
}
reader := binary.NewReader(pMsg.GetBody().GetMsgContent())
groupCode := uint32(reader.ReadInt32())
if info := c.FindGroup(int64(groupCode)); info != nil && pMsg.Head.GetGroupName() != "" && info.Name != pMsg.Head.GetGroupName() {
c.Debug("group %v name updated. %v -> %v", groupCode, info.Name, pMsg.Head.GetGroupName())
info.Name = pMsg.Head.GetGroupName()
}
}
func msgType0x211Decoder(c *QQClient, pMsg *msg.Message, info *incomingPacketInfo) {
if pMsg.Head.GetC2CCmd() == 6 || pMsg.Head.C2CTmpMsgHead != nil {
tempSessionDecoder(c, pMsg, info)
}
sub4 := msg.SubMsgType0X4Body{}
if err := proto.Unmarshal(pMsg.Body.MsgContent, &sub4); err != nil {
err = errors.Wrap(err, "unmarshal sub msg 0x4 error")
c.Error("unmarshal sub msg 0x4 error: %v", err)
return
}
if sub4.NotOnlineFile != nil && sub4.NotOnlineFile.GetSubcmd() == 1 { // subcmd: 1 -> sendPacket, 2-> recv
rsp, err := c.sendAndWait(c.buildOfflineFileDownloadRequestPacket(sub4.NotOnlineFile.FileUuid)) // offline_file.go
if err != nil {
return
}
c.dispatchOfflineFileEvent(&OfflineFileEvent{
FileName: string(sub4.NotOnlineFile.FileName),
FileSize: sub4.NotOnlineFile.GetFileSize(),
Sender: pMsg.Head.GetFromUin(),
DownloadUrl: rsp.(string),
})
}
}