-
Notifications
You must be signed in to change notification settings - Fork 1
/
process.go
125 lines (121 loc) · 3.36 KB
/
process.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
package wwdk
import (
"encoding/xml"
"errors"
"fmt"
"html"
"runtime/debug"
"strings"
"github.com/ikuiki/wwdk/datastruct"
"github.com/ikuiki/wwdk/datastruct/appmsg"
)
func (wxwb *WechatWeb) messageProcesser(msg *datastruct.Message) (err error) {
defer func() {
// 防止外部方法导致的崩溃
if err := recover(); err != nil {
debug.PrintStack()
fmt.Println("messageProcesser panic: ", err)
fmt.Println("message data: ", msg)
}
}()
context := Context{App: wxwb, hasStop: false}
switch msg.MsgType {
case datastruct.TextMsg:
wxwb.runInfo.MessageRecivedCount++
for _, v := range wxwb.messageHook[datastruct.TextMsg] {
if f, ok := v.(TextMessageHook); ok {
f(&context, *msg)
}
if context.hasStop {
break
}
}
case datastruct.ImageMsg:
wxwb.runInfo.MessageRecivedCount++
msg.Content = strings.Replace(html.UnescapeString(msg.Content), "<br/>", "", -1)
for _, v := range wxwb.messageHook[datastruct.ImageMsg] {
if f, ok := v.(ImageMessageHook); ok {
f(&context, *msg)
}
if context.hasStop {
break
}
}
case datastruct.AnimationEmotionsMsg:
wxwb.runInfo.MessageRecivedCount++
msg.Content = strings.Replace(html.UnescapeString(msg.Content), "<br/>", "", -1)
var emojiContent appmsg.EmotionMsgContent
err := xml.Unmarshal([]byte(msg.Content), &emojiContent)
if err != nil {
return errors.New("Unmarshal message content to struct: " + err.Error())
}
for _, v := range wxwb.messageHook[datastruct.AnimationEmotionsMsg] {
if f, ok := v.(EmotionMessageHook); ok {
f(&context, *msg, emojiContent)
}
if context.hasStop {
break
}
}
case datastruct.RevokeMsg:
wxwb.runInfo.MessageRevokeRecivedCount++
msg.Content = strings.Replace(html.UnescapeString(msg.Content), "<br/>", "", -1)
var revokeContent appmsg.RevokeMsgContent
err := xml.Unmarshal([]byte(msg.Content), &revokeContent)
if err != nil {
return errors.New("Unmarshal message content to struct: " + err.Error())
}
for _, v := range wxwb.messageHook[datastruct.RevokeMsg] {
if f, ok := v.(RevokeMessageHook); ok {
f(&context, *msg, revokeContent)
}
if context.hasStop {
break
}
}
case datastruct.LittleVideoMsg:
wxwb.runInfo.MessageRecivedCount++
msg.Content = strings.Replace(html.UnescapeString(msg.Content), "<br/>", "", -1)
for _, v := range wxwb.messageHook[datastruct.LittleVideoMsg] {
if f, ok := v.(VideoMessageHook); ok {
f(&context, *msg)
}
if context.hasStop {
break
}
}
case datastruct.VoiceMsg:
wxwb.runInfo.MessageRecivedCount++
msg.Content = strings.Replace(html.UnescapeString(msg.Content), "<br/>", "", -1)
for _, v := range wxwb.messageHook[datastruct.VoiceMsg] {
if f, ok := v.(VoiceMessageHook); ok {
f(&context, *msg)
}
if context.hasStop {
break
}
}
default:
return fmt.Errorf("Unknown MsgType %v: %#v", msg.MsgType, msg)
}
return nil
}
func (wxwb *WechatWeb) contactProcesser(oldContact, newContact *datastruct.Contact) (err error) {
defer func() {
// 防止外部方法导致的崩溃
if err := recover(); err != nil {
fmt.Println("contactProcesser panic: ", err)
fmt.Println("contact data: ", newContact)
}
}()
context := Context{App: wxwb, hasStop: false}
for _, v := range wxwb.modContactHook {
if f, ok := v.(ModContactHook); ok {
f(&context, oldContact, newContact)
}
if context.hasStop {
break
}
}
return
}