-
Notifications
You must be signed in to change notification settings - Fork 5
/
helper.go
165 lines (148 loc) · 4.02 KB
/
helper.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
package nano
import (
"encoding/base64"
"encoding/hex"
"hash/crc64"
"net/url"
"regexp"
"runtime"
"strings"
"unsafe"
)
func getFuncAndFileNameWithSkip(n int) (string, string) {
pc, fn, _, ok := runtime.Caller(n)
if !ok {
return "", ""
}
i := strings.LastIndex(fn, "/") + 1
if i > 0 {
fn = strings.TrimSuffix(fn[i:], ".go")
}
fullname := runtime.FuncForPC(pc).Name()
i = strings.LastIndex(fullname, ".") + 1
if i <= 0 || i >= len(fullname) {
return fullname, fn
}
return fullname[i:], fn
}
// getThisFuncName 获取正在执行的函数名
func getThisFuncName() string {
x, _ := getFuncAndFileNameWithSkip(2)
return x
}
// getCallerFuncName 获取调用者函数名
func getCallerFuncName() string {
x, _ := getFuncAndFileNameWithSkip(3)
return x
}
// getLogHeader [文件名.函数名]
func getLogHeader() string {
funcname, filename := getFuncAndFileNameWithSkip(2)
return "[" + filename + "." + funcname + "]"
}
// MessageEscape 消息转义
//
// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_format.html
func MessageEscape(text string) string {
text = strings.ReplaceAll(text, "&", "&")
text = strings.ReplaceAll(text, "<", "<")
text = strings.ReplaceAll(text, ">", ">")
return text
}
// MessageUnescape 消息解转义
//
// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_format.html
func MessageUnescape(text string) string {
text = strings.ReplaceAll(text, "&", "&")
text = strings.ReplaceAll(text, "<", "<")
text = strings.ReplaceAll(text, ">", ">")
return text
}
// HideURL 转义 URL 以避免审核
func HideURL(s string) string {
s = strings.ReplaceAll(s, ".", "…")
s = strings.ReplaceAll(s, "http://", "🔗📄:")
s = strings.ReplaceAll(s, "https://", "🔗🔒:")
return s
}
// UnderlineToCamel convert abc_def to AbcDef
func UnderlineToCamel(s string) string {
sb := strings.Builder{}
isnextupper := true
for _, c := range []byte(strings.ToLower(s)) {
if c == '_' {
isnextupper = true
continue
}
if isnextupper {
sb.WriteString(strings.ToUpper(string(c)))
isnextupper = false
continue
}
sb.WriteByte(c)
}
return sb.String()
}
// resolveURI github.com/wdvxdr1123/ZeroBot/driver/uri.go
func resolveURI(addr string) (network, address string) {
network, address = "tcp", addr
uri, err := url.Parse(addr)
if err == nil && uri.Scheme != "" {
scheme, ext, _ := strings.Cut(uri.Scheme, "+")
if ext != "" {
network = ext
uri.Scheme = scheme // remove `+unix`/`+tcp4`
if ext == "unix" {
uri.Host, uri.Path, _ = strings.Cut(uri.Path, ":")
uri.Host = base64.StdEncoding.EncodeToString(StringToBytes(uri.Host)) // special handle for unix
}
address = uri.String()
}
}
return
}
// slice is the runtime representation of a slice.
// It cannot be used safely or portably and its representation may
// change in a later release.
//
// Unlike reflect.SliceHeader, its Data field is sufficient to guarantee the
// data it references will not be garbage collected.
type slice struct {
data unsafe.Pointer
len int
cap int
}
// BytesToString 没有内存开销的转换
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// StringToBytes 没有内存开销的转换
func StringToBytes(s string) (b []byte) {
bh := (*slice)(unsafe.Pointer(&b))
sh := (*slice)(unsafe.Pointer(&s))
bh.data = sh.data
bh.len = sh.len
bh.cap = sh.len
return b
}
// DigestID 归一化 id 为 uint64
func DigestID(id string) uint64 {
b, err := hex.DecodeString(id)
if err != nil || len(b) < 8 {
return 0
}
return crc64.Checksum(b, crc64.MakeTable(crc64.ECMA))
}
const mediafilebed = `https://multimedia.nt.qq.com.cn`
var mediafileinfourlre = regexp.MustCompile(`/download\?appid=\d+&fileid=[0-9A-Za-z-_]+&rkey=[0-9A-Za-z-_]+`)
// mediaURL 从 fileinfo 得到 URL
func mediaURL(fileinfo string) (string, error) {
sb := strings.Builder{}
data, err := base64.StdEncoding.DecodeString(fileinfo)
if err != nil {
return "", err
}
sb.WriteString(mediafilebed)
sb.Write(mediafileinfourlre.Find(data))
return sb.String(), nil
}