/
commandrouter.go
184 lines (157 loc) · 4.27 KB
/
commandrouter.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
package commandrouter
import (
"errors"
"regexp"
"strings"
"github.com/dimfeld/promulgator/model"
)
// Command defines a single command, how it should be detected, and what value
// should be used to notify the client.
type Command struct {
// Tag is the value sent to the destination when a command matches.
Tag int
// The command to match on. If the command is not a regexp (see below), the
// command must be a single word and will only be matched if it occurs at
// the beginning of the message.
Command string
// IsRegExp is true if Command should be compiled as a regex.
IsRegexp bool
// MatchAll is true if the command should match on all messages. If false,
// the command will match only on messages directed to the bot user.
MatchAll bool
}
// cmd is internal data needed to route a command match
type cmd struct {
Tag int
destination destination
MatchAll bool
}
type commandList []cmd
type regexpCommand struct {
regexp *regexp.Regexp
cmd cmd
}
type destination struct {
Channel chan Match
}
type destinationList []destination
// Match contains all the information needed for a client to process an incoming
// message.
type Match struct {
// The tag supplied when the command was added.
Tag int
Message *model.ChatMessage
// RegexpMatch contains the matching subgroups, if any, when a Regexp
// command matches.
RegexpMatch []string
}
// Router is the command router itself.
type Router struct {
destinations []destination
commands map[string]commandList
regexpCommands []regexpCommand
done chan struct{}
}
// New creates a new Router
func New() *Router {
return &Router{
destinations: destinationList{},
commands: map[string]commandList{},
regexpCommands: []regexpCommand{},
done: make(chan struct{}),
}
}
// Close shuts down the router and closes all it outgoing channels.
func (r *Router) Close() {
for _, d := range r.destinations {
close(d.Channel)
}
close(r.done)
r.done = nil
}
func (r *Router) addCommand(d destination, c Command) error {
cmd := cmd{Tag: c.Tag, destination: d, MatchAll: c.MatchAll}
if c.IsRegexp {
re, err := regexp.Compile(c.Command)
if err != nil {
return err
}
rc := regexpCommand{
regexp: re,
cmd: cmd,
}
r.regexpCommands = append(r.regexpCommands, rc)
} else {
if strings.IndexAny(c.Command, " \r\n") != -1 {
return errors.New("Commands must not have whitespace. Use a regex instead: " + c.Command)
}
cmdList := r.commands[c.Command]
if cmdList == nil {
cmdList = commandList{cmd}
} else {
cmdList = append(cmdList, cmd)
}
r.commands[c.Command] = cmdList
}
return nil
}
// AddDestination adds a new destination and associated commands to the router.
func (r *Router) AddDestination(name string, commands []Command) (chan Match, error) {
ci := make(chan Match, 1)
dest := destination{ci}
r.destinations = append(r.destinations, dest)
for _, c := range commands {
if err := r.addCommand(dest, c); err != nil {
close(ci)
return nil, err
}
}
// Start a new buffering goroutine to handle sending to this destination.
co := MatchBuffer(ci, r.done)
return co, nil
}
// Route processes a ChatMessage and routes it to the correct destination, if any.
func (r *Router) Route(msg *model.ChatMessage) (bool, error) {
if r.done == nil {
return false, errors.New("Router is closed")
}
found := false
firstSpace := strings.Index(msg.Text, " ")
var firstWord string
if firstSpace == -1 {
// Just one word, but this might be ok
firstWord = msg.Text
} else {
firstWord = msg.Text[:firstSpace]
}
if cmdList, ok := r.commands[firstWord]; ok {
// There is a command for this word. Send it to all the destinations.
for _, c := range cmdList {
if c.MatchAll || msg.ToBot {
match := Match{
Tag: c.Tag,
Message: msg,
}
c.destination.Channel <- match
found = true
}
}
}
for _, rc := range r.regexpCommands {
if !rc.cmd.MatchAll && !msg.ToBot {
// Don't even bother matching this regexp if it only applies to
// messages addressed to us, and this message is not.
continue
}
reMatch := rc.regexp.FindStringSubmatch(msg.Text)
if reMatch != nil {
match := Match{
Tag: rc.cmd.Tag,
Message: msg,
}
rc.cmd.destination.Channel <- match
found = true
}
}
return found, nil
}