-
Notifications
You must be signed in to change notification settings - Fork 73
/
engine.go
274 lines (232 loc) · 7.88 KB
/
engine.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
package zero
// New 生成空引擎
func New() *Engine {
return &Engine{
preHandler: []Rule{},
midHandler: []Rule{},
postHandler: []Handler{},
}
}
var defaultEngine = New()
// Engine is the pre_handler, post_handler manager
type Engine struct {
preHandler []Rule
midHandler []Rule
postHandler []Handler
block bool
matchers []*Matcher
}
// Delete 移除该 Engine 注册的所有 Matchers
func (e *Engine) Delete() {
for _, m := range e.matchers {
m.Delete()
}
}
func (e *Engine) SetBlock(block bool) *Engine {
e.block = block
return e
}
// UsePreHandler 向该 Engine 添加新 PreHandler(Rule),
// 会在 Rule 判断前触发,如果 preHandler
// 没有通过,则 Rule, Matcher 不会触发
//
// 可用于分群组管理插件等
func (e *Engine) UsePreHandler(rules ...Rule) {
e.preHandler = append(e.preHandler, rules...)
}
// UseMidHandler 向该 Engine 添加新 MidHandler(Rule),
// 会在 Rule 判断后, Matcher 触发前触发,如果 midHandler
// 没有通过,则 Matcher 不会触发
//
// 可用于速率限制等
func (e *Engine) UseMidHandler(rules ...Rule) {
e.midHandler = append(e.midHandler, rules...)
}
// UsePostHandler 向该 Engine 添加新 PostHandler(Rule),
// 会在 Matcher 触发后触发,如果 PostHandler 返回 false,
// 则后续的 post handler 不会触发
//
// 可用于反并发等
func (e *Engine) UsePostHandler(handler ...Handler) {
e.postHandler = append(e.postHandler, handler...)
}
// On 添加新的指定消息类型的匹配器(默认Engine)
func On(typ string, rules ...Rule) *Matcher { return defaultEngine.On(typ, rules...) }
// On 添加新的指定消息类型的匹配器
func (e *Engine) On(typ string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: Type(typ),
Rules: rules,
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnMessage 消息触发器
func OnMessage(rules ...Rule) *Matcher { return On("message", rules...) }
// OnMessage 消息触发器
func (e *Engine) OnMessage(rules ...Rule) *Matcher { return e.On("message", rules...) }
// OnNotice 系统提示触发器
func OnNotice(rules ...Rule) *Matcher { return On("notice", rules...) }
// OnNotice 系统提示触发器
func (e *Engine) OnNotice(rules ...Rule) *Matcher { return e.On("notice", rules...) }
// OnRequest 请求消息触发器
func OnRequest(rules ...Rule) *Matcher { return On("request", rules...) }
// OnRequest 请求消息触发器
func (e *Engine) OnRequest(rules ...Rule) *Matcher { return On("request", rules...) }
// OnMetaEvent 元事件触发器
func OnMetaEvent(rules ...Rule) *Matcher { return On("meta_event", rules...) }
// OnMetaEvent 元事件触发器
func (e *Engine) OnMetaEvent(rules ...Rule) *Matcher { return On("meta_event", rules...) }
// OnPrefix 前缀触发器
func OnPrefix(prefix string, rules ...Rule) *Matcher { return defaultEngine.OnPrefix(prefix, rules...) }
// OnPrefix 前缀触发器
func (e *Engine) OnPrefix(prefix string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: Type("message"),
Rules: append([]Rule{PrefixRule(prefix)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnSuffix 后缀触发器
func OnSuffix(suffix string, rules ...Rule) *Matcher { return defaultEngine.OnSuffix(suffix, rules...) }
// OnSuffix 后缀触发器
func (e *Engine) OnSuffix(suffix string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: Type("message"),
Rules: append([]Rule{SuffixRule(suffix)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnCommand 命令触发器
func OnCommand(commands string, rules ...Rule) *Matcher {
return defaultEngine.OnCommand(commands, rules...)
}
// OnCommand 命令触发器
func (e *Engine) OnCommand(commands string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: Type("message"),
Rules: append([]Rule{CommandRule(commands)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnRegex 正则触发器
func OnRegex(regexPattern string, rules ...Rule) *Matcher {
return OnMessage(append([]Rule{RegexRule(regexPattern)}, rules...)...)
}
// OnRegex 正则触发器
func (e *Engine) OnRegex(regexPattern string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: Type("message"),
Rules: append([]Rule{RegexRule(regexPattern)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnKeyword 关键词触发器
func OnKeyword(keyword string, rules ...Rule) *Matcher {
return defaultEngine.OnKeyword(keyword, rules...)
}
// OnKeyword 关键词触发器
func (e *Engine) OnKeyword(keyword string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: Type("message"),
Rules: append([]Rule{KeywordRule(keyword)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnFullMatch 完全匹配触发器
func OnFullMatch(src string, rules ...Rule) *Matcher {
return defaultEngine.OnFullMatch(src, rules...)
}
// OnFullMatch 完全匹配触发器
func (e *Engine) OnFullMatch(src string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: Type("message"),
Rules: append([]Rule{FullMatchRule(src)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnFullMatchGroup 完全匹配触发器组
func OnFullMatchGroup(src []string, rules ...Rule) *Matcher {
return defaultEngine.OnFullMatchGroup(src, rules...)
}
// OnFullMatchGroup 完全匹配触发器组
func (e *Engine) OnFullMatchGroup(src []string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: Type("message"),
Rules: append([]Rule{FullMatchRule(src...)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnKeywordGroup 关键词触发器组
func OnKeywordGroup(keywords []string, rules ...Rule) *Matcher {
return defaultEngine.OnKeywordGroup(keywords, rules...)
}
// OnKeywordGroup 关键词触发器组
func (e *Engine) OnKeywordGroup(keywords []string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: Type("message"),
Rules: append([]Rule{KeywordRule(keywords...)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnCommandGroup 命令触发器组
func OnCommandGroup(commands []string, rules ...Rule) *Matcher {
return defaultEngine.OnCommandGroup(commands, rules...)
}
// OnCommandGroup 命令触发器组
func (e *Engine) OnCommandGroup(commands []string, rules ...Rule) *Matcher {
return e.On("message", append([]Rule{CommandRule(commands...)}, rules...)...)
}
// OnPrefixGroup 前缀触发器组
func OnPrefixGroup(prefix []string, rules ...Rule) *Matcher {
return defaultEngine.OnPrefixGroup(prefix, rules...)
}
// OnPrefixGroup 前缀触发器组
func (e *Engine) OnPrefixGroup(prefix []string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: Type("message"),
Rules: append([]Rule{PrefixRule(prefix...)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnSuffixGroup 后缀触发器组
func OnSuffixGroup(suffix []string, rules ...Rule) *Matcher {
return defaultEngine.OnSuffixGroup(suffix, rules...)
}
// OnSuffixGroup 后缀触发器组
func (e *Engine) OnSuffixGroup(suffix []string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: Type("message"),
Rules: append([]Rule{SuffixRule(suffix...)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnShell shell命令触发器
func OnShell(command string, model interface{}, rules ...Rule) *Matcher {
return defaultEngine.OnShell(command, model, rules...)
}
// OnShell shell命令触发器
func (e *Engine) OnShell(command string, model interface{}, rules ...Rule) *Matcher {
return e.On("message", append([]Rule{ShellRule(command, model)}, rules...)...)
}