Skip to content

Commit bafbb1e

Browse files
authored
refactor: 重构消息解析部分 (#137)
* code ... * code ... * code ... * code ...
1 parent d8933a0 commit bafbb1e

File tree

5 files changed

+62
-68
lines changed

5 files changed

+62
-68
lines changed

client/message.go

+15-18
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,11 @@ func (c *QQClient) SendGroupMessage(groupUin uint32, elements []message2.IMessag
6868
GroupUin: groupUin,
6969
GroupName: group.GroupName,
7070
Sender: &message2.Sender{
71-
Uin: c.Uin,
72-
UID: c.GetUID(c.Uin),
73-
Nickname: c.NickName(),
74-
CardName: minfo.MemberCard,
75-
AnonymousInfo: nil,
76-
IsFriend: true,
71+
Uin: c.Uin,
72+
UID: c.GetUID(c.Uin),
73+
Nickname: c.NickName(),
74+
CardName: minfo.MemberCard,
75+
IsFriend: true,
7776
},
7877
Time: ret.Timestamp1,
7978
Elements: elements,
@@ -106,11 +105,10 @@ func (c *QQClient) SendPrivateMessage(uin uint32, elements []message2.IMessageEl
106105
Target: uin,
107106
Time: ret.Timestamp1,
108107
Sender: &message2.Sender{
109-
Uin: c.Uin,
110-
UID: c.GetUID(c.Uin),
111-
Nickname: c.NickName(),
112-
AnonymousInfo: nil,
113-
IsFriend: true,
108+
Uin: c.Uin,
109+
UID: c.GetUID(c.Uin),
110+
Nickname: c.NickName(),
111+
IsFriend: true,
114112
},
115113
Elements: elements,
116114
}
@@ -137,11 +135,10 @@ func (c *QQClient) SendTempMessage(groupUin uint32, uin uint32, elements []messa
137135
GroupName: group.GroupName,
138136
Self: c.Uin,
139137
Sender: &message2.Sender{
140-
Uin: c.Uin,
141-
UID: c.GetUID(c.Uin),
142-
Nickname: c.NickName(),
143-
AnonymousInfo: nil,
144-
IsFriend: true,
138+
Uin: c.Uin,
139+
UID: c.GetUID(c.Uin),
140+
Nickname: c.NickName(),
141+
IsFriend: true,
145142
},
146143
Elements: elements,
147144
}
@@ -159,7 +156,7 @@ func (c *QQClient) BuildFakeMessage(msgElems []*message2.ForwardNode) []*message
159156
FromUin: elem.SenderID,
160157
},
161158
ContentHead: &message.ContentHead{
162-
Type: uint32(utils.Ternary(elem.GroupID != 0, 82, 9)),
159+
Type: utils.Ternary[uint32](elem.GroupID != 0, 82, 9),
163160
MsgId: proto.Uint32(crypto.RandU32()),
164161
Sequence: proto.Uint32(crypto.RandU32()),
165162
TimeStamp: proto.Uint32(uint32(utils.TimeStamp())),
@@ -169,7 +166,7 @@ func (c *QQClient) BuildFakeMessage(msgElems []*message2.ForwardNode) []*message
169166
Foward: &message.ForwardHead{
170167
Field1: proto.Uint32(0),
171168
Field2: proto.Uint32(0),
172-
Field3: utils.Ternary(elem.GroupID != 0, proto.Uint32(0), proto.Uint32(2)),
169+
Field3: proto.Uint32(utils.Ternary[uint32](elem.GroupID != 0, 0, 2)),
173170
UnknownBase64: proto.String(avatar),
174171
Avatar: proto.String(avatar),
175172
},

client/operation.go

+13
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,19 @@ func (c *QQClient) GetGroupRecordURL(groupUin uint32, node *oidb.IndexNode) (str
477477
return oidb2.ParseGroupRecordDownloadResp(resp)
478478
}
479479

480+
func (c *QQClient) GenFileNode(name, md5, sha1, uuid string, size uint32, isnt bool) *oidb.IndexNode {
481+
return &oidb.IndexNode{
482+
Info: &oidb.FileInfo{
483+
FileName: name,
484+
FileSize: size,
485+
FileSha1: sha1,
486+
FileHash: md5,
487+
},
488+
FileUuid: uuid,
489+
StoreId: utils.Ternary[uint32](isnt, 1, 0), // 0旧服务器 1为nt服务器
490+
}
491+
}
492+
480493
// GetPrivateVideoURL 获取私聊视频下载链接
481494
func (c *QQClient) GetPrivateVideoURL(node *oidb.IndexNode) (string, error) {
482495
pkt, err := oidb2.BuildPrivateVideoDownloadReq(c.Sig().UID, node)

client/packets/message/multi_msg_upload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func BuildMultiMsgUploadReq(selfUID string, groupUin uint32, msg []*message.Push
2424
Info: &message.SendLongMsgInfo{
2525
Type: utils.Ternary[uint32](groupUin == 0, 1, 3),
2626
Uid: &message.LongMsgUid{
27-
Uid: utils.Ternary(groupUin == 0, proto.String(selfUID), proto.String(strconv.Itoa(int(groupUin)))),
27+
Uid: proto.String(utils.Ternary(groupUin == 0, selfUID, strconv.Itoa(int(groupUin)))),
2828
},
2929
GroupUin: proto.Uint32(groupUin),
3030
Payload: payload,

message/build.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (e *TextElement) BuildElement() []*message.Elem {
2121

2222
func (e *AtElement) BuildElement() []*message.Elem {
2323
reserveData, _ := proto.Marshal(&message.MentionExtra{
24-
Type: proto.Some(int32(utils.Ternary(e.TargetUin == 0, 1, 2))), // atAll
24+
Type: proto.Some(utils.Ternary[int32](e.TargetUin == 0, 1, 2)), // atAll
2525
Uin: proto.Some(uint32(0)),
2626
Field5: proto.Some(int32(0)),
2727
Uid: proto.Some(e.TargetUID),
@@ -75,7 +75,7 @@ func (e *ImageElement) BuildElement() []*message.Elem {
7575
CommonElem: &message.CommonElem{
7676
ServiceType: 48,
7777
PbElem: common,
78-
BusinessType: utils.Ternary(e.IsGroup, uint32(20), uint32(10)),
78+
BusinessType: utils.Ternary[uint32](e.IsGroup, 20, 10),
7979
},
8080
}}
8181
if e.CompatFace != nil {

message/message.go

+31-47
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,11 @@ type (
7777
}
7878

7979
Sender struct {
80-
Uin uint32
81-
UID string
82-
Nickname string
83-
CardName string
84-
AnonymousInfo *AnonymousInfo
85-
IsFriend bool
86-
}
87-
88-
AnonymousInfo struct {
89-
AnonymousID string
90-
AnonymousNick string
80+
Uin uint32
81+
UID string
82+
Nickname string
83+
CardName string
84+
IsFriend bool
9185
}
9286

9387
IMessageElement interface {
@@ -97,10 +91,6 @@ type (
9791
ElementType int
9892
)
9993

100-
func (s *Sender) IsAnonymous() bool {
101-
return s.Uin == 80000000
102-
}
103-
10494
func ParsePrivateMessage(msg *message.PushMsgBody) *PrivateMessage {
10595
prvMsg := &PrivateMessage{
10696
ID: msg.ContentHead.Sequence.Unwrap(),
@@ -161,26 +151,24 @@ func ParseMessageElements(msg []*message.Elem) []IMessageElement {
161151
continue
162152
}
163153
if elem.SrcMsg != nil && len(elem.SrcMsg.OrigSeqs) != 0 {
164-
r := &ReplyElement{
154+
res = append(res, &ReplyElement{
165155
ReplySeq: elem.SrcMsg.OrigSeqs[0],
166156
Time: uint32(elem.SrcMsg.Time.Unwrap()),
167157
SenderUin: uint32(elem.SrcMsg.SenderUin),
168158
GroupUin: uint32(elem.SrcMsg.ToUin.Unwrap()),
169159
Elements: ParseMessageElements(elem.SrcMsg.Elems),
170-
}
171-
res = append(res, r)
160+
})
172161
}
173162

174163
if elem.Text != nil {
175-
switch {
176-
case len(elem.Text.Attr6Buf) > 0:
164+
if len(elem.Text.Attr6Buf) > 0 {
177165
att6 := binary.NewReader(elem.Text.Attr6Buf)
178166
att6.SkipBytes(7)
179167
target := att6.ReadU32()
180168
at := NewAt(target, elem.Text.Str.Unwrap())
181169
at.SubType = AtTypeGroupMember
182170
res = append(res, at)
183-
default:
171+
} else {
184172
res = append(res, NewText(func() string {
185173
if strings.Contains(elem.Text.Str.Unwrap(), "\r") && !strings.Contains(elem.Text.Str.Unwrap(), "\r\n") {
186174
return strings.ReplaceAll(elem.Text.Str.Unwrap(), "\r", "\r\n")
@@ -191,24 +179,25 @@ func ParseMessageElements(msg []*message.Elem) []IMessageElement {
191179
}
192180

193181
if elem.Face != nil {
194-
switch {
195-
case len(elem.Face.Old) > 0:
182+
if len(elem.Face.Old) > 0 {
196183
faceID := elem.Face.Index
197184
if faceID.IsSome() {
198185
res = append(res, &FaceElement{FaceID: uint32(faceID.Unwrap())})
199186
}
200-
case elem.CommonElem != nil && elem.CommonElem.ServiceType == 37 && elem.CommonElem.PbElem != nil:
201-
qFace := message.QFaceExtra{}
202-
if err := proto.Unmarshal(elem.CommonElem.PbElem, &qFace); err == nil {
203-
if qFace.Qsid.IsSome() {
204-
res = append(res, &FaceElement{FaceID: uint32(qFace.Qsid.Unwrap()), isLargeFace: true})
187+
} else if elem.CommonElem != nil && elem.CommonElem.PbElem != nil {
188+
if elem.CommonElem.ServiceType == 37 {
189+
qFace := message.QFaceExtra{}
190+
if err := proto.Unmarshal(elem.CommonElem.PbElem, &qFace); err == nil {
191+
if qFace.Qsid.IsSome() {
192+
res = append(res, &FaceElement{FaceID: uint32(qFace.Qsid.Unwrap()), isLargeFace: true})
193+
}
194+
}
195+
} else if elem.CommonElem.ServiceType == 33 {
196+
qFace := message.QSmallFaceExtra{}
197+
err := proto.Unmarshal(elem.CommonElem.PbElem, &qFace)
198+
if err == nil {
199+
res = append(res, &FaceElement{FaceID: qFace.FaceId, isLargeFace: false})
205200
}
206-
}
207-
case elem.CommonElem != nil && elem.CommonElem.ServiceType == 33 && elem.CommonElem.PbElem != nil:
208-
qFace := message.QSmallFaceExtra{}
209-
err := proto.Unmarshal(elem.CommonElem.PbElem, &qFace)
210-
if err == nil {
211-
res = append(res, &FaceElement{FaceID: qFace.FaceId, isLargeFace: false})
212201
}
213202
}
214203
}
@@ -328,7 +317,7 @@ func ParseMessageElements(msg []*message.Elem) []IMessageElement {
328317
})
329318
case 11, 21: // video
330319
var thumb = new(VideoThumb)
331-
if !(len(extra.MsgInfoBody) < 2) {
320+
if len(extra.MsgInfoBody) > 1 {
332321
info := extra.MsgInfoBody[1].Index
333322
thumb.Size = info.Info.FileSize
334323
thumb.Width = info.Info.Width
@@ -408,7 +397,7 @@ func ParseMessageElements(msg []*message.Elem) []IMessageElement {
408397
} else {
409398
res = append(res, &XMLElement{
410399
ServiceID: 35,
411-
Content: utils.B2S(binary.ZlibUncompress(elem.RichMsg.Template1[1:])),
400+
Content: utils.B2S(xmlData),
412401
})
413402
}
414403
}
@@ -450,8 +439,7 @@ func ParseMessageBody(body *message.MessageBody, isGroup bool) []IMessageElement
450439
if body != nil {
451440
if body.RichText != nil && body.RichText.Ptt != nil {
452441
ptt := body.RichText.Ptt
453-
switch {
454-
case isGroup && ptt.FileId != 0:
442+
if isGroup && ptt.FileId != 0 {
455443
res = append(res, &VoiceElement{
456444
Name: ptt.FileName,
457445
UUID: ptt.FileUuid,
@@ -460,7 +448,7 @@ func ParseMessageBody(body *message.MessageBody, isGroup bool) []IMessageElement
460448
FileUuid: ptt.GroupFileKey,
461449
},
462450
})
463-
case !isGroup:
451+
} else if !isGroup {
464452
res = append(res, &VoiceElement{
465453
Name: ptt.FileName,
466454
UUID: ptt.FileUuid,
@@ -654,11 +642,9 @@ func PackElementsToBody(msgElems []IMessageElement) (msgBody *message.MessageBod
654642
RichText: &message.RichText{Elems: PackElements(msgElems)},
655643
}
656644
for _, elem := range msgElems {
657-
bd, ok := elem.(MsgContentBuilder)
658-
if !ok {
659-
continue
645+
if bd, ok := elem.(MsgContentBuilder); ok {
646+
msgBody.MsgContent = bd.BuildContent()
660647
}
661-
msgBody.MsgContent = bd.BuildContent()
662648
}
663649
return
664650
}
@@ -669,11 +655,9 @@ func PackElements(msgElems []IMessageElement) []*message.Elem {
669655
}
670656
elems := make([]*message.Elem, 0, len(msgElems))
671657
for _, elem := range msgElems {
672-
bd, ok := elem.(ElementBuilder)
673-
if !ok {
674-
continue
658+
if bd, ok := elem.(ElementBuilder); ok {
659+
elems = append(elems, bd.BuildElement()...)
675660
}
676-
elems = append(elems, bd.BuildElement()...)
677661
}
678662
return elems
679663
}

0 commit comments

Comments
 (0)