forked from kyokomi/slackbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
208 lines (167 loc) · 4.01 KB
/
plugin.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
package plugins
import (
"fmt"
"strings"
)
type PluginManager interface {
AddPlugin(key interface{}, val BotMessagePlugin)
ExecPlugins(botEvent BotEvent)
StopReply()
StartReply()
IsReply() bool
GetPlugins() []Plugin // deepCopy
}
type plugins struct {
plugins []Plugin
isReply bool
messageSender MessageSender
}
var _ PluginManager = (*plugins)(nil)
type MessageSender interface {
SendMessage(message string, channel string)
}
func NewPluginManager(sender MessageSender) PluginManager {
return &plugins{
plugins: []Plugin{},
isReply: true,
messageSender: sender,
}
}
func (ps *plugins) AddPlugin(key interface{}, val BotMessagePlugin) {
ps.plugins = append(ps.plugins, Plugin{key, val})
}
func (ps *plugins) StopReply() {
ps.isReply = false
}
func (ps *plugins) StartReply() {
ps.isReply = true
}
func (ps *plugins) IsReply() bool {
return ps.isReply
}
func (ps *plugins) ExecPlugins(botEvent BotEvent) {
for _, p := range ps.plugins {
ok, m := p.CheckMessage(botEvent, botEvent.text)
if !ok {
continue
}
next := p.DoAction(botEvent, m)
if !next {
break
}
}
}
func (ps *plugins) SendMessage(message string, channel string) {
ps.messageSender.SendMessage(message, channel)
}
func (ps *plugins) GetPlugins() []Plugin {
deepCopy := make([]Plugin, len(ps.plugins))
copy(deepCopy, ps.plugins)
return deepCopy
}
type BotMessagePlugin interface {
CheckMessage(event BotEvent, message string) (bool, string)
DoAction(event BotEvent, message string) bool
Help() string
}
type Plugin struct {
Key interface{}
BotMessagePlugin
}
func (p Plugin) Name() string {
return fmt.Sprintf("%s", p.Key)
}
type BotID string
func (b BotID) Equal(bot string) bool {
if string(b) == bot {
return true
}
if b.LinkID() == bot {
return true
}
return false
}
func (b BotID) LinkID() string {
return fmt.Sprintf("<@%s>", b)
}
type BotEvent struct {
messageSender MessageSender
botID BotID
botName string
senderID string
senderName string
text string
channelID string
channelName string
}
var _ MessageSender = (*BotEvent)(nil)
func NewBotEvent(sender MessageSender, botID, botName, senderID, senderName, text, channelID, channelName string) BotEvent {
return BotEvent{
messageSender: sender,
botID: BotID(botID),
botName: botName,
senderID: senderID,
senderName: senderName,
text: text,
channelID: channelID,
channelName: channelName,
}
}
func (b *BotEvent) Reply(message string) {
b.SendMessage(message, b.Channel())
}
func (b *BotEvent) SendMessage(message string, channel string) {
b.messageSender.SendMessage(message, channel)
}
func (b *BotEvent) BaseText() string {
return b.text
}
// Channel is Deprecated
func (b *BotEvent) Channel() string {
return b.ChannelID()
}
func (b *BotEvent) ChannelID() string {
return b.channelID
}
func (b *BotEvent) ChannelName() string {
return b.channelName
}
func (b *BotEvent) BotID() string {
return string(b.botID)
}
func (b *BotEvent) BotName() string {
return string(b.botName)
}
func (b *BotEvent) BotLinkID() string {
return b.botID.LinkID()
}
func (b *BotEvent) BotLinkIDForClient() string {
return b.botID.LinkID() + ":"
}
func (b *BotEvent) SenderID() string {
return b.senderID
}
func (b *BotEvent) SenderName() string {
return b.senderName
}
func (b *BotEvent) BotCmdArgs(message string) ([]string, bool) {
trimMessage, ok := b.BotCmdMessage(message)
return strings.Fields(trimMessage), ok
}
func (b *BotEvent) BotCmdMessage(message string) (string, bool) {
switch {
case strings.HasPrefix(message, b.BotLinkIDForClient()):
return message[len(b.BotLinkIDForClient()):], true
case strings.HasPrefix(message, b.BotLinkID()):
return message[len(b.BotLinkID()):], true
case strings.HasPrefix(message, b.BotName()):
return message[len(b.BotName()):], true
case strings.HasPrefix(message, b.BotID()):
return message[len(b.BotID()):], true
default:
return "", false
}
}
func (b *BotEvent) GetMessageSender() MessageSender {
return b.messageSender
}