-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.go
120 lines (95 loc) · 2.27 KB
/
parse.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
// This file is subject to a 1-clause BSD license.
// Its contents can be found in the enclosed LICENSE file.
package cmd
import (
"github.com/chimeracoder/gopherbot/proto"
"strings"
)
// Parse reads incoming message data and tries to parse it into
// a command structure and then execute it.
func Parse(prefix string, c *proto.Client, m *proto.Message) bool {
prefixlen := len(prefix)
if prefixlen == 0 || !strings.HasPrefix(m.Data, prefix) {
return false
}
// Split the data into a name and list of parameters.
name, params := parseCommand(m.Data[prefixlen:])
if len(name) == 0 {
return false
}
// Ensure the given command exists.
cmd := findCommand(name)
if cmd == nil {
return false
}
cmd.Data = strings.TrimSpace(m.Data[prefixlen+len(name):])
// Ensure the current user us allowed to execute the command.
if cmd.Restricted && !isWhitelisted(m.SenderMask) {
c.PrivMsg(m.SenderName, "Access to %q denied.", name)
return false
}
// Make sure we received enough parameters.
pc := cmd.RequiredParamCount()
lp := len(params)
if pc > lp {
c.PrivMsg(m.SenderName, "Missing parameters for command %q", name)
return false
}
// Copy over parameter values and ensure they are of the right format.
for i := 0; i < lp && i < len(cmd.Params); i++ {
cmd.Params[i].Value = params[i]
if !cmd.Params[i].Valid() {
c.PrivMsg(m.SenderName, "Invalid parameter value %q for command %q",
params[i], name)
return false
}
}
// Execute the command.
if cmd.Execute != nil {
go cmd.Execute(cmd, c, m)
}
return true
}
// parseCommand reads command name and arguments from the given input.
func parseCommand(data string) (string, []string) {
var list []string
var quoted bool
loop:
for i := 0; i < len(data); i++ {
switch data[i] {
case '"':
quoted = !quoted
fallthrough
case ' ', '\t':
if quoted {
break
}
v := strings.TrimSpace(data[:i])
if len(v) == 0 {
break
}
if v[0] == '"' {
v = v[1:]
if len(v) == 0 {
break
}
}
list = append(list, v)
data = data[i+1:]
goto loop
}
}
v := strings.TrimSpace(data)
if len(v) > 0 {
if v[0] == '"' {
v = v[1:]
}
if len(v) > 0 {
list = append(list, v)
}
}
if len(list) == 0 {
return "", nil
}
return strings.ToLower(list[0]), list[1:]
}