-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgRouter.go
308 lines (286 loc) · 8.64 KB
/
msgRouter.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
307
308
package main
import (
"context"
"io/ioutil"
kcard "local/khlcard"
"net/http"
qq "qqNode"
"regexp"
"strconv"
"strings"
"time"
"unicode"
"github.com/Mrs4s/MiraiGo/message"
"github.com/jinzhu/copier"
"github.com/lonelyevil/kook"
)
// TODO: 大量辣鸡代码待优化
// : 支持消息回复
// : 将kook回复标记转换成 qq @ uid
// TODO: 更多 kmarkdown tag 处理
// TODO: 处理KOOK服务器表情
// TODO: 移除对于机器人自身的at转发,QQ & KOOK
// 将(met)bot id(met)\s+ 变为 @ name
func kookMet2At(content string, channelId string) string {
replaceMap := make(map[string]string)
var c *kook.Channel = nil
r := regexp.MustCompile(`\(met\)(\d+)\(met\)`)
content = strings.ReplaceAll(content, "(met)"+botID+"(met)", "")
for {
matchs := r.FindStringSubmatch(content)
if len(matchs) > 0 {
if c == nil {
c, _ = localSession.ChannelView(channelId)
}
if _, ok := replaceMap["(met)"+matchs[1]+"(met)"]; !ok {
u, err := localSession.UserView(matchs[1], kook.UserViewWithGuildID(c.GuildID))
if err == nil {
content = strings.ReplaceAll(content, "(met)"+matchs[1]+"(met)", "@"+u.Nickname)
replaceMap["(met)"+matchs[1]+"(met)"] = "@" + u.Nickname
} else {
content = strings.ReplaceAll(content, "(met)"+matchs[1]+"(met)", "@某人")
replaceMap["(met)"+matchs[1]+"(met)"] = "@某人"
}
}
} else {
break
}
}
return content
}
// 将 [foo](bar) 变为 bar
func kookLink2Link(content string) string {
r := regexp.MustCompile(`\[.+?\]\((.+?)\)`)
content = r.ReplaceAllString(content, " $1 ")
return content
}
func kookMsgToVC(ctx *kook.KmarkdownMessageContext, guildId string, api string, gid string, secret string) {
name := ctx.Extra.Author.Nickname
content := ctx.Common.Content
gidInt, _ := strconv.Atoi(gid)
vocechatSend(api, gidInt, secret, "**`"+name+"`** 转发自 KOOK:\n"+content)
}
func kookMsgToQQGroup(ctx *kook.KmarkdownMessageContext, guildId string, groupId string) {
gLog.Info().Msgf("kmsg-log:%v", ctx)
if _, ok := kookLastCache[ctx.Common.TargetID]; ok {
kookLastCache[ctx.Common.TargetID] = kookLastMsgs{}
}
channel := ctx.Common.TargetID
name := ctx.Extra.Author.Nickname
content := ctx.Common.Content
content = kookMet2At(content, guildId)
content = kookLink2Link(content)
gLog.Info().Msgf("[KOOK Markdown]:[channel=%v][name=%v][content=%v]", channel, name, content)
id, _ := strconv.ParseInt(groupId, 10, 64)
var mid int32
var replyUid, replyName string
if ctx.Extra.Quote != nil {
replyUid, replyName = msgCache.WhomReply(guildId, ctx.Extra.Quote.RongID)
}
if len(replyUid) > 0 {
msgs := make([]message.IMessageElement, 0)
quoteUid, _ := strconv.ParseInt(replyUid, 10, 64)
msgs = append(msgs, message.NewText(name+" 转发自 KOOK:\n"))
if len(replyName) > 0 {
msgs = append(msgs, message.NewAt(quoteUid, "@"+replyName))
} else {
msgs = append(msgs, message.NewAt(quoteUid))
}
msgs = append(msgs, message.NewText(content))
mid = qq.SendToQQGroupEx(msgs, id)
} else {
mid = qq.SendToQQGroup(name+" 转发自 KOOK:\n"+content, id)
}
msgCache.GetMsg(groupId, strconv.FormatInt(int64(mid), 10), ctx.Extra.Author.ID, name)
gLog.Info().Msgf("[SEND QQ msg]:[ID=%d]", mid)
}
func imageHandler(ctx *kook.ImageMessageContext) {
if _, ok := kookLastCache[ctx.Common.TargetID]; ok {
kookLastCache[ctx.Common.TargetID] = kookLastMsgs{}
}
gLog.Info().Msgf("[KOOK Image]:[name=%v][url=%v]", ctx.Extra.Author.Nickname, ctx.Extra.Attachments.URL)
for k, v := range kook2qqRouteMap {
if ctx.Common.TargetID == k {
gidStr := v
go func() {
gid, _ := strconv.ParseInt(gidStr, 10, 64)
// 超时退出
context, cancel := context.WithCancel(context.Background())
defer cancel()
ch := make(chan bool, 1)
// 下载图片后发送至QQ群
go func() {
// mid = qq.SendToQQGroup(ctx.Extra.Author.Nickname+" 尝试转发图片自 KOOK:\n"+ctx.Extra.Attachments.URL, gid)
// msgCache.GetMsg(strconv.FormatInt(gid, 10), strconv.FormatInt(int64(mid), 10), ctx.Extra.Author.ID, ctx.Extra.Author.Nickname)
resp, err := http.Get(ctx.Extra.Attachments.URL)
if err != nil {
return
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
msgs := make([]message.IMessageElement, 0)
msgs = append(msgs, message.NewText(ctx.Extra.Author.Nickname+" 转发图片自 KOOK:"))
imgMsg, err := qq.UploadImgToQQGroup(data, gid)
if err == nil {
msgs = append(msgs, imgMsg)
} else {
msgs = append(msgs, message.NewText("Failed..."))
}
mid := qq.SendToQQGroupEx(msgs, gid)
msgCache.GetMsg(strconv.FormatInt(gid, 10), strconv.FormatInt(int64(mid), 10), ctx.Extra.Author.ID, ctx.Extra.Author.Nickname)
gLog.Info().Msgf("[SEND QQ msg]:[ID=%d]", mid)
select {
default:
ch <- true
case <-context.Done():
return
}
}()
select {
case <-ch:
return
case <-time.After(20 * time.Second):
gLog.Info().Msgf("[SEND QQ IMG]:TIMEOUT")
}
}()
}
}
}
func qqMsgHandler(msg *message.GroupMessage) {
gid := strconv.FormatInt(msg.GroupCode, 10)
name := msg.Sender.DisplayName()
if name == "" {
name = msg.Sender.Nickname
}
for k, v := range kook2qqRouteMap {
if gid == k {
go qqMsgToKook(gid, msg.Sender.Uin, v, name, qq.GroupMsgParse(msg))
}
}
for k, v := range qq2vcRouteMap {
if gid == k {
intgid, _ := strconv.Atoi(v.Gid)
go vocechatSend(v.Url, intgid, v.Secret, "**`"+name+"`** 转发自 QQ:\n"+qq.GroupMsg2Markdown(msg))
}
}
}
func escapeToCleanUnicode(raw string) (string, error) {
str, err := strconv.Unquote(strings.Replace(strconv.Quote(string(raw)), `\\u`, `\u`, -1))
if err != nil {
return "", err
}
clean := strings.Map(func(r rune) rune {
if unicode.IsGraphic(r) {
return r
}
return -1
}, str)
return clean, nil
}
func vcMsgToKook(channel string, name string, content string) {
card := kcard.KHLCard{}
card.Init()
card.Card.Theme = "success"
card.AddModule_markdown("**`" + name + "`** 转发自 vocechat:\n---")
card.AddModule_markdown(content)
sendKCard(channel, card.String())
}
// DONE: 相同用户短时间连续发言自动合并
func qqMsgToKook(gid string, uid int64, channel string, name string, msgs []qq.QQMsg) {
var card kcard.KHLCard
// 是否合并消息
var merge bool = false
var entry kookLastMsgs
var cleanName string
var err error
gLog.Info().Msgf("qmsg-log:%v", msgs)
if kmm, ok := kookLastCache[channel]; ok {
entry = kmm
if uid == entry.Uid && time.Now().Unix()-entry.MsgTime < 300 && entry.CardStack < 10 {
entry.CardStack += 1
card = entry.Card
merge = true
}
}
if !merge {
if _, ok := kookLastCache[channel]; !ok {
kookLastCache[channel] = kookLastMsgs{}
entry = kookLastCache[channel]
}
card = kcard.KHLCard{}
card.Init()
card.Card.Theme = "success"
cleanName, err = escapeToCleanUnicode(name)
if err != nil {
cleanName = "某姓名无法打印人士"
}
card.AddModule_markdown("**`" + cleanName + "`** 转发自 QQ:\n---")
}
var atCount int = 0
var cachedStr string = ""
cachedStrRelease := func() {
if len(cachedStr) > 0 {
card.AddModule_markdown(cachedStr)
cachedStr = ""
}
}
for _, v := range msgs {
switch v.Type {
case 0: // 可合并消息
if len(v.Content) > 0 && v.Content != " " {
// 忽略QQ回复消息自带的空白消息(一个0x20字符)
clean, _ := escapeToCleanUnicode(v.Content)
cachedStr += clean
}
case 1:
cachedStrRelease()
card.AddModule_image(v.Content)
case 2: // At
atCount += 1
if atCount == 1 {
clean, _ := escapeToCleanUnicode(v.Content)
cachedStr += clean
}
case 3: // Reply
var mid string
r := regexp.MustCompile(`\d+`)
matchs := r.FindStringSubmatch(v.Content)
if len(matchs) > 0 {
mid = matchs[0]
}
replyUid, _ := msgCache.WhomReply(gid, mid)
if len(replyUid) > 0 {
cachedStr += "(met)" + replyUid + "(met) "
} else {
clean, _ := escapeToCleanUnicode(v.Content)
cachedStr += clean
}
case 4: // Unknown
cachedStrRelease()
card.AddModule_markdown(v.Content)
}
}
cachedStrRelease()
if !merge {
resp, err := sendKCard(channel, card.String())
if err != nil {
gLog.Error().Msgf("[SEND KOOK MSG]:[json=%s]", card.String())
kookLog("消息转发失败")
entry.MsgId = ""
} else {
entry.CardStack = 1
entry.MsgId = resp.MsgID
msgCache.GetMsg(channel, entry.MsgId, strconv.FormatInt(uid, 10), cleanName)
gLog.Info().Msgf("[SEND KOOK MSG]:[ID=%s][json=%s]", entry.MsgId, card.String())
}
} else {
updateKMsg(entry.MsgId, card.String())
}
entry.MsgTime = time.Now().Unix()
entry.Uid = uid
copier.Copy(&entry.Card, &card)
kookLastCache[channel] = entry
}