/
c2c_processor.go
292 lines (271 loc) · 9.09 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
package client
//go:generate go run github.com/scxiangqilin/MiraiGo/internal/generator/c2c_switcher
import (
"fmt"
"time"
"github.com/pkg/errors"
"github.com/scxiangqilin/MiraiGo/binary"
"github.com/scxiangqilin/MiraiGo/client/internal/network"
"github.com/scxiangqilin/MiraiGo/client/pb"
"github.com/scxiangqilin/MiraiGo/client/pb/msg"
"github.com/scxiangqilin/MiraiGo/internal/proto"
)
type (
TempSessionInfo struct {
Source TempSessionSource
GroupCode int64
Sender int64
sig []byte
client *QQClient
}
TempSessionSource int
)
const (
GroupSource TempSessionSource = iota // 来自群聊
ConsultingSource // 来自QQ咨询
SearchSource // 来自查找
MovieSource // 来自QQ电影
HotChatSource // 来自热聊
SystemMessageSource // 来自验证消息
MultiChatSource // 来自多人聊天
DateSource // 来自约会
AddressBookSource // 来自通讯录
)
func (c *QQClient) c2cMessageSyncProcessor(rsp *msg.GetMessageResponse, info network.RequestParams) {
c.sig.SyncCookie = rsp.SyncCookie
c.sig.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.FromUin.Unwrap(),
ToUin: pMsg.Head.ToUin.Unwrap(),
MsgType: pMsg.Head.MsgType.Unwrap(),
MsgSeq: pMsg.Head.MsgSeq.Unwrap(),
MsgUid: pMsg.Head.MsgUid.Unwrap(),
}
delItems = append(delItems, delItem)
if pMsg.Head.ToUin.Unwrap() != c.Uin {
continue
}
if (int64(pairMsg.LastReadTime.Unwrap()) & 4294967295) > int64(pMsg.Head.MsgTime.Unwrap()) {
continue
}
c.commMsgProcessor(pMsg, info)
}
}
if delItems != nil {
_, _ = c.sendAndWait(c.buildDeleteMessageRequestPacket(delItems))
}
if rsp.SyncFlag.Unwrap() != msg.SyncFlag_STOP {
c.debug("continue sync with flag: %v", rsp.SyncFlag)
seq, pkt := c.buildGetMessageRequestPacket(rsp.SyncFlag.Unwrap(), time.Now().Unix())
_, _ = c.sendAndWait(seq, pkt, info)
}
}
func (c *QQClient) commMsgProcessor(pMsg *msg.Message, info network.RequestParams) {
strKey := fmt.Sprintf("%d%d%d%d", pMsg.Head.FromUin.Unwrap(), pMsg.Head.ToUin.Unwrap(), pMsg.Head.MsgSeq.Unwrap(), pMsg.Head.MsgUid.Unwrap())
if _, ok := c.msgSvcCache.GetAndUpdate(strKey, time.Hour); ok {
c.debug("c2c msg %v already exists in cache. skip.", pMsg.Head.MsgUid.Unwrap())
return
}
c.msgSvcCache.Add(strKey, unit{}, time.Hour)
if c.lastC2CMsgTime > int64(pMsg.Head.MsgTime.Unwrap()) && (c.lastC2CMsgTime-int64(pMsg.Head.MsgTime.Unwrap())) > 60*10 {
c.debug("c2c msg filtered by time. lastMsgTime: %v msgTime: %v", c.lastC2CMsgTime, pMsg.Head.MsgTime.Unwrap())
return
}
c.lastC2CMsgTime = int64(pMsg.Head.MsgTime.Unwrap())
if info.Bool("init") {
return
}
if decoder, _ := peekC2CDecoder(pMsg.Head.MsgType.Unwrap()); decoder != nil {
decoder(c, pMsg, info)
} else {
c.debug("unknown msg type on c2c processor: %v - %v", pMsg.Head.MsgType.Unwrap(), pMsg.Head.C2CCmd.Unwrap())
}
}
func privateMessageDecoder(c *QQClient, pMsg *msg.Message, _ network.RequestParams) {
switch pMsg.Head.C2CCmd.Unwrap() {
case 11, 175: // friend msg
if pMsg.Head.FromUin.Unwrap() == c.Uin {
for {
frdSeq := c.friendSeq.Load()
if frdSeq < pMsg.Head.MsgSeq.Unwrap() {
if c.friendSeq.CompareAndSwap(frdSeq, pMsg.Head.MsgSeq.Unwrap()) {
break
}
} else {
break
}
}
}
if pMsg.Body.RichText == nil || pMsg.Body.RichText.Elems == nil {
return
}
// handle fragmented message
if pMsg.Content != nil && pMsg.Content.PkgNum.Unwrap() > 1 {
seq := pMsg.Content.DivSeq.Unwrap()
builder := c.messageBuilder(seq)
builder.append(pMsg)
if builder.len() < pMsg.Content.PkgNum.Unwrap() {
// continue to receive other fragments
return
}
c.msgBuilders.Delete(seq)
pMsg = builder.build()
}
if pMsg.Head.FromUin.Unwrap() == c.Uin {
c.SelfPrivateMessageEvent.dispatch(c, c.parsePrivateMessage(pMsg))
return
}
c.PrivateMessageEvent.dispatch(c, c.parsePrivateMessage(pMsg))
default:
c.debug("unknown c2c cmd on private msg decoder: %v", pMsg.Head.C2CCmd.Unwrap())
}
}
func privatePttDecoder(c *QQClient, pMsg *msg.Message, _ network.RequestParams) {
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.PrivateMessageEvent.dispatch(c, c.parsePrivateMessage(pMsg))
}
func tempSessionDecoder(c *QQClient, pMsg *msg.Message, _ network.RequestParams) {
if pMsg.Head.C2CTmpMsgHead == nil || pMsg.Body == nil {
return
}
if (pMsg.Head.MsgType.Unwrap() == 529 && pMsg.Head.C2CCmd.Unwrap() == 6) || pMsg.Body.RichText != nil {
genTempSessionInfo := func() *TempSessionInfo {
if pMsg.Head.C2CTmpMsgHead.ServiceType.Unwrap() == 0 {
group := c.FindGroup(pMsg.Head.C2CTmpMsgHead.GroupCode.Unwrap())
if group == nil {
return nil
}
return &TempSessionInfo{
Source: GroupSource,
GroupCode: group.Code,
Sender: pMsg.Head.FromUin.Unwrap(),
client: c,
}
}
info := &TempSessionInfo{
Source: 0,
Sender: pMsg.Head.FromUin.Unwrap(),
sig: pMsg.Head.C2CTmpMsgHead.Sig,
client: c,
}
switch pMsg.Head.C2CTmpMsgHead.ServiceType.Unwrap() {
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.GroupCode.Unwrap())
if group == nil {
return
}
*/
if pMsg.Head.FromUin.Unwrap() == c.Uin {
return
}
c.TempMessageEvent.dispatch(c, &TempMessageEvent{
Message: c.parseTempMessage(pMsg),
Session: session,
})
}
}
func troopAddMemberBroadcastDecoder(c *QQClient, pMsg *msg.Message, _ network.RequestParams) {
groupJoinLock.Lock()
defer groupJoinLock.Unlock()
group := c.FindGroupByUin(pMsg.Head.FromUin.Unwrap())
if pMsg.Head.AuthUin.Unwrap() == c.Uin {
if group == nil && c.ReloadGroupList() == nil {
c.GroupJoinEvent.dispatch(c, c.FindGroupByUin(pMsg.Head.FromUin.Unwrap()))
}
} else {
if group != nil && group.FindMember(pMsg.Head.AuthUin.Unwrap()) == nil {
// 将 m 转换为 JSON 格式字符串,便于日志输出
jsonString, _ := json.MarshalIndent(pMsg, "", " ")
// 打印 m 的所有内容
c.info("Received group messageB: %v\n", string(pMsg))
c.info("parseGroupMessage infoB %+v", pMsg)
mem, err := c.GetMemberInfo(group.Code, pMsg.Head.AuthUin.Unwrap())
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.GroupMemberJoinEvent.dispatch(c, &MemberJoinGroupEvent{
Group: group,
Member: mem,
})
}
}
}
func systemMessageDecoder(c *QQClient, _ *msg.Message, _ network.RequestParams) {
_, pkt := c.buildSystemMsgNewFriendPacket()
_ = c.sendPacket(pkt)
}
func troopSystemMessageDecoder(c *QQClient, pMsg *msg.Message, info network.RequestParams) {
if !info.Bool("used_reg_proxy") && pMsg.Head.MsgType.Unwrap() != 85 && pMsg.Head.MsgType.Unwrap() != 36 {
c.exceptAndDispatchGroupSysMsg()
}
if len(pMsg.Body.MsgContent) == 0 {
return
}
reader := binary.NewReader(pMsg.Body.MsgContent)
groupCode := uint32(reader.ReadInt32())
if info := c.FindGroup(int64(groupCode)); info != nil && pMsg.Head.GroupName.Unwrap() != "" && info.Name != pMsg.Head.GroupName.Unwrap() {
c.debug("group %v name updated. %v -> %v", groupCode, info.Name, pMsg.Head.GroupName.Unwrap())
info.Name = pMsg.Head.GroupName.Unwrap()
}
}
func msgType0x211Decoder(c *QQClient, pMsg *msg.Message, info network.RequestParams) {
if pMsg.Head.C2CCmd.Unwrap() == 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.Subcmd.Unwrap() == 1 { // subcmd: 1 -> sendPacket, 2-> recv
rsp, err := c.sendAndWait(c.buildOfflineFileDownloadRequestPacket(sub4.NotOnlineFile.FileUuid)) // offline_file.go
if err != nil {
return
}
c.OfflineFileEvent.dispatch(c, &OfflineFileEvent{
FileName: string(sub4.NotOnlineFile.FileName),
FileSize: sub4.NotOnlineFile.FileSize.Unwrap(),
Sender: pMsg.Head.FromUin.Unwrap(),
DownloadUrl: rsp.(string),
})
}
}