-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmux_command.go
176 lines (142 loc) · 3.92 KB
/
mux_command.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
package seabird
import (
"sort"
"strings"
)
// CommandMux is a simple IRC event multiplexer, based on the BasicMux.
// HelpInfo is a collection of instructions for command usage that
// is formatted with <prefix>help.
type HelpInfo struct {
name string
Usage string
Description string
}
// The CommandMux is given a prefix string and matches all PRIVMSG
// events which start with it. The first word after the string is
// moved into the Event.Command.
type CommandMux struct {
private *BasicMux
public *BasicMux
prefix string
cmdHelp map[string]*HelpInfo
}
// NewCommandMux will create an initialized BasicMux with no handlers.
func NewCommandMux(prefix string) *CommandMux {
m := &CommandMux{
NewBasicMux(),
NewBasicMux(),
prefix,
make(map[string]*HelpInfo),
}
m.Event("help", m.help, &HelpInfo{
"help",
"<command>",
"Displays help messages for a given command",
})
return m
}
func (m *CommandMux) help(r *Request) {
cmd := r.Message.Trailing()
if cmd == "" {
// Get all keys
keys := make([]string, 0, len(m.cmdHelp))
for k := range m.cmdHelp {
keys = append(keys, k)
}
// Sort everything
sort.Strings(keys)
if r.FromChannel() {
// If they said "!help" in a channel, list all available commands
r.Replyf("Available commands: %s. Use %shelp [command] for more info.", strings.Join(keys, ", "), m.prefix)
} else {
for _, v := range keys {
h := m.cmdHelp[v]
if h.Usage != "" {
r.Replyf("%s %s: %s", v, h.Usage, h.Description)
} else {
r.Replyf("%s: %s", v, h.Description)
}
}
}
} else if help, ok := m.cmdHelp[cmd]; ok {
if help == nil {
r.Replyf("There is no help available for command %q", cmd)
} else {
lines := help.format(m.prefix, cmd)
for _, line := range lines {
r.Replyf("%s", line)
}
}
} else {
r.MentionReplyf("There is no help available for command %q", cmd)
}
}
func (h *HelpInfo) format(prefix, command string) []string {
if h.Usage == "" && h.Description == "" {
return []string{"There is no help available for command " + command}
}
ret := []string{}
if h.Usage != "" {
ret = append(ret, "Usage: "+prefix+h.name+" "+h.Usage)
}
if h.Description != "" {
ret = append(ret, h.Description)
}
return ret
}
// Event will register a Handler as both a private and public command.
func (m *CommandMux) Event(c string, h HandlerFunc, help *HelpInfo) {
if help != nil {
help.name = c
}
c = strings.ToLower(c)
m.private.Event(c, h)
m.public.Event(c, h)
m.cmdHelp[c] = help
}
// Channel will register a handler as a public command.
func (m *CommandMux) Channel(c string, h HandlerFunc, help *HelpInfo) {
if help != nil {
help.name = c
}
c = strings.ToLower(c)
m.public.Event(c, h)
m.cmdHelp[c] = help
}
// Private will register a handler as a private command.
func (m *CommandMux) Private(c string, h HandlerFunc, help *HelpInfo) {
if help != nil {
help.name = c
}
c = strings.ToLower(c)
m.private.Event(c, h)
m.cmdHelp[c] = help
}
// HandleEvent strips off the prefix, pulls the command out
// and runs HandleEvent on the internal BasicMux.
func (m *CommandMux) HandleEvent(r *Request) {
if r.Message.Command != "PRIVMSG" {
// TODO: Log this
return
}
// Get the last arg and see if it starts with the command prefix
lastArg := r.Message.Trailing()
if r.FromChannel() && !strings.HasPrefix(lastArg, m.prefix) {
return
}
// Copy it into a new Event
newRequest := r.Copy()
// Chop off the command itself
msgParts := strings.SplitN(lastArg, " ", 2)
newRequest.Message.Params[len(newRequest.Message.Params)-1] = ""
if len(msgParts) > 1 {
newRequest.Message.Params[len(newRequest.Message.Params)-1] = strings.TrimSpace(msgParts[1])
}
newRequest.Message.Command = strings.ToLower(msgParts[0])
newRequest.Message.Command = strings.TrimPrefix(newRequest.Message.Command, m.prefix)
if newRequest.FromChannel() {
m.public.HandleEvent(newRequest)
} else {
m.private.HandleEvent(newRequest)
}
}