Skip to content

Commit b76cf1f

Browse files
committed
xml code
1 parent d79b474 commit b76cf1f

File tree

6 files changed

+90
-31
lines changed

6 files changed

+90
-31
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/Mrs4s/MiraiGo v0.0.0-20201017132316-00cefa75d99b
77
github.com/fanliao/go-promise v0.0.0-20141029170127-1890db352a72
88
github.com/gin-gonic/gin v1.6.3
9-
github.com/go-playground/validator/v10 v10.4.0 // indirect
9+
github.com/go-playground/validator/v10 v10.4.1 // indirect
1010
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e
1111
github.com/golang/protobuf v1.4.3
1212
github.com/gorilla/websocket v1.4.2
@@ -17,6 +17,6 @@ require (
1717
github.com/smartystreets/goconvey v1.6.4 // indirect
1818
github.com/ugorji/go v1.1.12 // indirect
1919
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
20-
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7 // indirect
20+
golang.org/x/sys v0.0.0-20201018121011-98379d014ca7 // indirect
2121
gopkg.in/yaml.v2 v2.3.0
2222
)

go.sum

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3-
github.com/Mrs4s/MiraiGo v0.0.0-20201015025426-9b7f533603e8 h1:uLi2czRLndmwYxdLgDl5PhrSXL2yjTaTugRH1Pyu4z8=
4-
github.com/Mrs4s/MiraiGo v0.0.0-20201015025426-9b7f533603e8/go.mod h1:cwYPI2uq6nxNbx0nA6YuAKF1V5szSs6FPlGVLQvRUlo=
5-
github.com/Mrs4s/MiraiGo v0.0.0-20201015123028-325bfd80e9e4 h1:5ZcH7lNF7Cf+1/ZF+5YO9ERisiJp+7wNOgRMhMKczS0=
6-
github.com/Mrs4s/MiraiGo v0.0.0-20201015123028-325bfd80e9e4/go.mod h1:cwYPI2uq6nxNbx0nA6YuAKF1V5szSs6FPlGVLQvRUlo=
73
github.com/Mrs4s/MiraiGo v0.0.0-20201017132316-00cefa75d99b h1:0zXEoZfQJzo9DlaEFuJFttSuv21d+5QCsyHHqziMQn0=
84
github.com/Mrs4s/MiraiGo v0.0.0-20201017132316-00cefa75d99b/go.mod h1:cwYPI2uq6nxNbx0nA6YuAKF1V5szSs6FPlGVLQvRUlo=
95
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@@ -27,8 +23,8 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87
2723
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
2824
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
2925
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
30-
github.com/go-playground/validator/v10 v10.4.0 h1:72qIR/m8ybvL8L5TIyfgrigqkrw7kVYAvjEvpT85l70=
31-
github.com/go-playground/validator/v10 v10.4.0/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
26+
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
27+
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
3228
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
3329
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY=
3430
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
@@ -107,8 +103,6 @@ github.com/ugorji/go/codec v1.1.12/go.mod h1:U/SFD954ms+MwaHihwfeIz/sGz5OFgHt81t
107103
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
108104
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
109105
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
110-
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee h1:4yd7jl+vXjalO5ztz6Vc1VADv+S/80LGJmyl1ROJ2AI=
111-
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
112106
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNmw2api+jEfxLoykJVice/E=
113107
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
114108
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@@ -119,6 +113,7 @@ golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73r
119113
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
120114
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
121115
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
116+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
122117
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
123118
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
124119
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -130,10 +125,10 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
130125
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
131126
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
132127
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
133-
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 h1:9UQO31fZ+0aKQOFldThf7BKPMJTiBfWycGh/u3UoO88=
134-
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
135128
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7 h1:XtNJkfEjb4zR3q20BBBcYUykVOEMgZeIUOpBPfNYgxg=
136129
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
130+
golang.org/x/sys v0.0.0-20201018121011-98379d014ca7 h1:CNOpL+H7PSxBI7dF/EIUsfOguRSzWp6CQ91yxZE6PG4=
131+
golang.org/x/sys v0.0.0-20201018121011-98379d014ca7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
137132
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
138133
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
139134
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

service/bot/api_handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func preProcessGroupSendingMessage(cli *client.QQClient, groupCode int64, m *mes
5353
}
5454

5555
func HandleSendPrivateMsg(cli *client.QQClient, req *onebot.SendPrivateMsgReq) *onebot.SendPrivateMsgResp {
56-
miraiMsg := ProtoMsgToMiraiMsg(req.Message)
56+
miraiMsg := ProtoMsgToMiraiMsg(req.Message, req.AutoEscape)
5757
sendingMessage := &message.SendingMessage{Elements: miraiMsg}
5858
preProcessPrivateSendingMessage(cli, req.UserId, sendingMessage)
5959
ret := cli.SendPrivateMessage(req.UserId, sendingMessage)
@@ -64,7 +64,7 @@ func HandleSendPrivateMsg(cli *client.QQClient, req *onebot.SendPrivateMsgReq) *
6464
}
6565

6666
func HandleSendGroupMsg(cli *client.QQClient, req *onebot.SendGroupMsgReq) *onebot.SendGroupMsgResp {
67-
miraiMsg := ProtoMsgToMiraiMsg(req.Message)
67+
miraiMsg := ProtoMsgToMiraiMsg(req.Message, req.AutoEscape)
6868
sendingMessage := &message.SendingMessage{Elements: miraiMsg}
6969
preProcessGroupSendingMessage(cli, req.GroupId, sendingMessage)
7070
ret := cli.SendGroupMessage(req.GroupId, sendingMessage)
@@ -75,7 +75,7 @@ func HandleSendGroupMsg(cli *client.QQClient, req *onebot.SendGroupMsgReq) *oneb
7575
}
7676

7777
func HandleSendMsg(cli *client.QQClient, req *onebot.SendMsgReq) *onebot.SendMsgResp {
78-
miraiMsg := ProtoMsgToMiraiMsg(req.Message)
78+
miraiMsg := ProtoMsgToMiraiMsg(req.Message, req.AutoEscape)
7979
sendingMessage := &message.SendingMessage{Elements: miraiMsg}
8080
if req.GroupId != 0 {
8181
preProcessGroupSendingMessage(cli, req.GroupId, sendingMessage)

service/bot/mirai2raw_test.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

service/bot/proto2mirai.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
package bot
22

33
import (
4+
"strconv"
5+
46
"github.com/Mrs4s/MiraiGo/message"
57
"github.com/ProtobufBot/Go-Mirai-Client/pkg/util"
68
"github.com/ProtobufBot/Go-Mirai-Client/proto_gen/onebot"
79
log "github.com/sirupsen/logrus"
8-
"strconv"
910
)
1011

1112
func EmptyText() *message.TextElement {
1213
return message.NewText("")
1314
}
1415

15-
func ProtoMsgToMiraiMsg(msgList []*onebot.Message) []message.IMessageElement {
16+
// 消息列表,不自动把code变成msg
17+
func ProtoMsgToMiraiMsg(msgList []*onebot.Message, notConvertText bool) []message.IMessageElement {
1618
messageChain := make([]message.IMessageElement, 0)
1719
for _, protoMsg := range msgList {
1820
switch protoMsg.Type {
21+
case "text":
22+
if notConvertText {
23+
messageChain = append(messageChain, ProtoTextToMiraiText(protoMsg.Data))
24+
} else {
25+
text, ok := protoMsg.Data["text"]
26+
if !ok {
27+
log.Warnf("text不存在")
28+
continue
29+
}
30+
messageChain = append(messageChain, RawMsgToMiraiMsg(text)...) // 转换xml码
31+
}
1932
case "at":
2033
messageChain = append(messageChain, ProtoAtToMiraiAt(protoMsg.Data))
21-
case "text":
22-
messageChain = append(messageChain, ProtoTextToMiraiText(protoMsg.Data))
2334
case "image":
2435
messageChain = append(messageChain, ProtoImageToMiraiImage(protoMsg.Data))
2536
case "record":

service/bot/raw2mirai.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package bot
2+
3+
import (
4+
"html"
5+
"regexp"
6+
"strings"
7+
8+
"encoding/xml"
9+
"github.com/Mrs4s/MiraiGo/message"
10+
log "github.com/sirupsen/logrus"
11+
)
12+
13+
type Node struct {
14+
XMLName xml.Name
15+
Attr []xml.Attr `xml:",any,attr"`
16+
}
17+
18+
var re = regexp.MustCompile("<[\\s\\S]+?/>")
19+
20+
func RawMsgToMiraiMsg(str string) []message.IMessageElement {
21+
var node Node
22+
textList := re.Split(str, -1)
23+
codeList := re.FindAllString(str, -1)
24+
elemList := make([]message.IMessageElement, 0)
25+
for len(textList) > 0 || len(codeList) > 0 {
26+
if len(textList) > 0 && strings.HasPrefix(str, textList[0]) {
27+
text := textList[0]
28+
textList = textList[1:]
29+
str = str[len(text):]
30+
elemList = append(elemList, message.NewText(text))
31+
}
32+
if len(codeList) > 0 && strings.HasPrefix(str, codeList[0]) {
33+
code := codeList[0]
34+
codeList = codeList[1:]
35+
str = str[len(code):]
36+
err := xml.Unmarshal([]byte(code), &node)
37+
if err != nil {
38+
elemList = append(elemList, message.NewText(code))
39+
continue
40+
}
41+
attrMap := make(map[string]string)
42+
for _, attr := range node.Attr {
43+
attrMap[attr.Name.Local] = html.UnescapeString(attr.Value)
44+
}
45+
switch node.XMLName.Local {
46+
case "at":
47+
elemList = append(elemList, ProtoAtToMiraiAt(attrMap))
48+
case "image":
49+
elemList = append(elemList, ProtoImageToMiraiImage(attrMap))
50+
case "face":
51+
elemList = append(elemList, ProtoFaceToMiraiFace(attrMap))
52+
case "share":
53+
elemList = append(elemList, ProtoShareToMiraiShare(attrMap))
54+
case "record":
55+
elemList = append(elemList, ProtoVoiceToMiraiVoice(attrMap))
56+
case "text":
57+
elemList = append(elemList, ProtoTextToMiraiText(attrMap))
58+
default:
59+
log.Warnf("不支持的类型 %s", code)
60+
elemList = append(elemList, message.NewText(code))
61+
}
62+
}
63+
}
64+
return elemList
65+
}

0 commit comments

Comments
 (0)