1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 19:17:37 +08:00

Merge branch 'dev' of github.com:/Mrs4s/go-cqhttp into dev

This commit is contained in:
Mrs4s 2021-08-01 06:29:37 +08:00
commit c951caba9f
No known key found for this signature in database
GPG Key ID: 3186E98FA19CE3A7
4 changed files with 68 additions and 14 deletions

View File

@ -155,7 +155,7 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
var m MSG var m MSG
switch o := elem.(type) { switch o := elem.(type) {
case *message.ReplyElement: case *message.ReplyElement:
if RemoveReplyAt && len(e) > i+1 { if RemoveReplyAt && i+1 < len(e) && e[i+1].Type() == message.At {
elem, ok := e[i+1].(*message.AtElement) elem, ok := e[i+1].(*message.AtElement)
if ok && elem.Target == o.Sender { if ok && elem.Target == o.Sender {
e[i+1] = nil e[i+1] = nil
@ -180,7 +180,7 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
} else { } else {
m = MSG{ m = MSG{
"type": "at", "type": "at",
"data": map[string]string{"qq": fmt.Sprint(o.Target)}, "data": map[string]string{"qq": strconv.FormatInt(o.Target, 10)},
} }
} }
case *message.RedBagElement: case *message.RedBagElement:
@ -234,12 +234,12 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
if isOk := strings.Contains(o.Content, "<?xml"); isOk { if isOk := strings.Contains(o.Content, "<?xml"); isOk {
m = MSG{ m = MSG{
"type": "xml", "type": "xml",
"data": map[string]string{"data": o.Content, "resid": fmt.Sprintf("%d", o.Id)}, "data": map[string]string{"data": o.Content, "resid": strconv.FormatInt(int64(o.Id), 10)},
} }
} else { } else {
m = MSG{ m = MSG{
"type": "json", "type": "json",
"data": map[string]string{"data": o.Content, "resid": fmt.Sprintf("%d", o.Id)}, "data": map[string]string{"data": o.Content, "resid": strconv.FormatInt(int64(o.Id), 10)},
} }
} }
default: default:
@ -947,12 +947,35 @@ func XMLEscape(c string) string {
] -> &#93; ] -> &#93;
*/ */
func CQCodeEscapeText(raw string) string { func CQCodeEscapeText(s string) string {
ret := raw count := strings.Count(s, "&")
ret = strings.ReplaceAll(ret, "&", "&amp;") count += strings.Count(s, "[")
ret = strings.ReplaceAll(ret, "[", "&#91;") count += strings.Count(s, "]")
ret = strings.ReplaceAll(ret, "]", "&#93;") if count == 0 {
return ret return s
}
// Apply replacements to buffer.
var b strings.Builder
b.Grow(len(s) + count*4)
start := 0
for i := 0; i < count; i++ {
j := start + strings.IndexFunc(s[start:], func(r rune) bool {
return r == '&' || r == '[' || r == ']'
})
b.WriteString(s[start:j])
switch s[j] {
case '&':
b.WriteString("&amp;")
case '[':
b.WriteString("&#91;")
case ']':
b.WriteString("&#93;")
}
start = j + 1
}
b.WriteString(s[start:])
return b.String()
} }
/*CQCodeEscapeValue 将字符串value中部分字符转义 /*CQCodeEscapeValue 将字符串value中部分字符转义

View File

@ -2,8 +2,11 @@ package coolq
import ( import (
"fmt" "fmt"
"strings"
"testing" "testing"
"github.com/Mrs4s/MiraiGo/utils"
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
@ -30,3 +33,32 @@ func BenchmarkCQBot_ConvertObjectMessage(b *testing.B) {
bot.ConvertObjectMessage(benchArray, false) bot.ConvertObjectMessage(benchArray, false)
} }
} }
const bText = `123456789[]&987654321[]&987654321[]&987654321[]&987654321[]&987654321[]&`
func BenchmarkCQCodeEscapeText(b *testing.B) {
for i := 0; i < b.N; i++ {
ret := bText
ret = CQCodeEscapeText(ret)
}
}
func BenchmarkCQCodeEscapeTextBefore(b *testing.B) {
for i := 0; i < b.N; i++ {
ret := bText
ret = strings.ReplaceAll(ret, "&", "&amp;")
ret = strings.ReplaceAll(ret, "[", "&#91;")
ret = strings.ReplaceAll(ret, "]", "&#93;")
}
}
func TestCQCodeEscapeText(t *testing.T) {
for i := 0; i < 200; i++ {
rs := utils.RandomStringRange(3000, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890[]&")
ret := rs
ret = strings.ReplaceAll(ret, "&", "&amp;")
ret = strings.ReplaceAll(ret, "[", "&#91;")
ret = strings.ReplaceAll(ret, "]", "&#93;")
assert.Equal(t, ret, CQCodeEscapeText(rs))
}
}

View File

@ -2,7 +2,7 @@ package global
import ( import (
"crypto/md5" "crypto/md5"
"fmt" "encoding/hex"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -19,7 +19,7 @@ func EncoderSilk(data []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, errors.Wrap(err, "calc md5 failed") return nil, errors.Wrap(err, "calc md5 failed")
} }
tempName := fmt.Sprintf("%x", h.Sum(nil)) tempName := hex.EncodeToString(h.Sum(nil))
if silkPath := path.Join("data/cache", tempName+".silk"); PathExists(silkPath) { if silkPath := path.Join("data/cache", tempName+".silk"); PathExists(silkPath) {
return os.ReadFile(silkPath) return os.ReadFile(silkPath)
} }

View File

@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"hash" "hash"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -42,7 +41,7 @@ func FromStream(updateWith io.Reader) (err error, errRecover error) {
// no patch to apply, go on through // no patch to apply, go on through
bufBytes := bufio.NewReader(updateWith) bufBytes := bufio.NewReader(updateWith)
updateWith = io.Reader(bufBytes) updateWith = io.Reader(bufBytes)
newBytes, err = ioutil.ReadAll(updateWith) newBytes, err = io.ReadAll(updateWith)
if err != nil { if err != nil {
return return
} }