-
Notifications
You must be signed in to change notification settings - Fork 4
/
command_args.go
408 lines (363 loc) · 10.4 KB
/
command_args.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package cmd
import (
"regexp"
"strings"
"github.com/aarondl/ultimateq/data"
"github.com/aarondl/ultimateq/irc"
"github.com/pkg/errors"
)
const (
errFmtInternal = "cmd: Internal Error Occurred: %v"
errFmtArgumentForm = `cmd: Arguments must look like: ` +
`#name OR [~|*]name OR [[~|*]name] OR [~|*]name... (given: %v)`
errFmtArgumentDupName = `cmd: Argument names must be unique ` +
`(given: %v)`
errFmtArgumentOrderChan = `cmd: The channel argument must come ` +
`first. (given: %v)`
errFmtArgumentDupChan = `cmd: Only one #channel argument is ` +
`allowed (given: %v)`
errFmtArgumentOrderReq = `cmd: Required arguments must come before ` +
`all [optional] and varargs... arguments. (given: %v)`
errFmtArgumentOrderOpt = `cmd: Optional arguments must come before ` +
`varargs... arguments. (given: %v)`
errFmtArgumentDupVargs = `cmd: Only one varargs... argument is ` +
`allowed (given: %v)`
errMsgStateDisabled = "Error: Cannot use nick or user parameter commands " +
"when state is disabled."
errMsgStoreDisabled = "Access Denied: Cannot use authenticated commands, " +
"nick or user parameters when store is disabled."
errFmtUserNotRegistered = "Error: User [%v] is not registered."
errFmtUserNotAuthed = "Error: User [%v] is not authenticated."
errFmtUserNotFound = "Error: User [%v] could not be found."
errMsgMissingUsername = "Error: Username must follow *, found nothing."
errMsgUnexpectedArgument = "Error: No arguments expected."
errFmtNArguments = "Error: Expected %v %v arguments. (%v)"
errFmtArgumentNotChannel = "Error: Expected a valid channel. (given: %v)"
errAtLeast = "at least"
errExactly = "exactly"
errAtMost = "at most"
)
type argType int
// These are for internal use of the command structure to deal with parsing
// and storing argument metadata.
const (
argTypeRequired argType = 1 << iota
argTypeOptional
argTypeVariadic
argTypeChannel
argTypeNick
argTypeUser
argTypeTypeMask = argTypeRequired | argTypeOptional | argTypeVariadic | argTypeChannel
argTypeUSERMASK = argTypeNick | argTypeUser
argStripChars = `#~*[].`
)
func (a argType) has(b argType) bool {
return a&b == b
}
var (
// rgxArgs checks a single argument to see if it matches the
// forms: arg #arg [arg] or arg...
rgxArgs = regexp.MustCompile(
`(?i)^(\[[~\*]?[a-z0-9]+\]|[~\*]?[a-z0-9]+(\.\.\.)?|#[a-z0-9]+)$`)
)
// args stores data about each argument after it's parsed.
type commandArgs struct {
args []argument
reqs int
opts int
}
// argument is a type to hold argument information.
type argument struct {
Original string
Name string
Type argType
}
// parseArgs parses and sets the arguments for a command.
func (c *Command) parseArgs() (err error) {
nArgs := len(c.Args)
if nArgs == 0 {
return nil
}
var cArgs commandArgs
cArgs.args = make([]argument, nArgs)
var chanArg, required, optional, variadic bool
for i := 0; i < nArgs; i++ {
arg := strings.ToLower(c.Args[i])
if !rgxArgs.MatchString(arg) {
return errors.Errorf(errFmtArgumentForm, arg)
}
argMeta := &cArgs.args[i]
argMeta.Original = arg
argMeta.Name = strings.Trim(c.Args[i], argStripChars)
for j := 0; j < i; j++ {
if cArgs.args[j].Name == argMeta.Name {
return errors.Errorf(errFmtArgumentDupName, argMeta.Name)
}
}
modifier := arg[0]
if modifier == '[' {
modifier = arg[1]
}
switch modifier {
case '#':
if chanArg {
return errors.Errorf(errFmtArgumentDupChan, arg)
} else if required || optional || variadic {
return errors.Errorf(errFmtArgumentOrderChan, arg)
}
argMeta.Type = argTypeChannel
chanArg = true
case '~':
argMeta.Type = argTypeNick
case '*':
argMeta.Type = argTypeUser
}
switch arg[len(arg)-1] {
case ']':
if variadic {
return errors.Errorf(errFmtArgumentOrderOpt, arg)
}
argMeta.Type |= argTypeOptional
optional = true
cArgs.opts++
case '.':
if variadic {
return errors.Errorf(errFmtArgumentDupVargs, arg)
}
argMeta.Type |= argTypeVariadic
variadic = true
default:
if optional {
return errors.Errorf(errFmtArgumentOrderReq, arg)
}
argMeta.Type |= argTypeRequired
required = true
cArgs.reqs++
}
}
c.parsedArgs = cArgs
return nil
}
// ProcessArgs parses all the arguments. It looks up channel and user arguments
// using the state and store, and generally populates the Event struct
// with argument information.
func ProcessArgs(server string, command *Command, channel string,
isChan bool, msgArgs []string, ev *Event, ircEvent *irc.Event,
state *data.State, store *data.Store) (err error) {
ev.Args = make(map[string]string)
i, j := 0, 0
for i = 0; i < len(command.Args); i, j = i+1, j+1 {
arg := command.parsedArgs.args[i]
switch {
case arg.Type.has(argTypeChannel):
if state == nil {
return errors.New(errMsgStateDisabled)
}
var consumed bool
if consumed, err = parseChanArg(command, ev, state, j,
msgArgs, channel, isChan); err != nil {
return
} else if !consumed {
j--
}
case arg.Type.has(argTypeRequired):
if j >= len(msgArgs) {
nReq := command.parsedArgs.reqs
if command.parsedArgs.args[0].Type.has(argTypeChannel) && isChan {
nReq--
}
return errors.Errorf(errFmtNArguments, errAtLeast, nReq,
strings.Join(command.Args, " "))
}
ev.Args[arg.Name] = msgArgs[j]
case arg.Type.has(argTypeOptional):
if j >= len(msgArgs) {
return
}
ev.Args[arg.Name] = msgArgs[j]
case arg.Type.has(argTypeVariadic):
if j >= len(msgArgs) {
return
}
ev.Args[arg.Name] = strings.Join(msgArgs[j:], " ")
}
if arg.Type.has(argTypeNick) || arg.Type.has(argTypeUser) {
if arg.Type.has(argTypeVariadic) {
err = parseUserArg(ev, state, store, server, arg.Name,
arg.Type, msgArgs[j:]...)
} else {
err = parseUserArg(ev, state, store, server, arg.Name,
arg.Type, msgArgs[j])
}
if err != nil {
return
}
}
if arg.Type.has(argTypeVariadic) {
j = len(msgArgs)
break
}
}
if j < len(msgArgs) {
if j == 0 {
return errors.New(errMsgUnexpectedArgument)
}
return errors.Errorf(errFmtNArguments, errAtMost,
command.parsedArgs.reqs+command.parsedArgs.opts,
strings.Join(command.Args, " "))
}
return nil
}
// parseChanArg checks the argument provided and ensures it's a valid situation
// for the channel arg to be in (isChan & validChan) | (isChan & missing) |
// (!isChan & validChan)
func parseChanArg(command *Command, ev *Event,
state *data.State,
index int, msgArgs []string, channel string, isChan bool) (bool, error) {
var isFirstChan bool
if index < len(msgArgs) {
isFirstChan = ev.Event.NetworkInfo.IsChannel(msgArgs[index])
} else if !isChan {
return false, errors.Errorf(errFmtNArguments, errAtLeast,
command.parsedArgs.reqs, strings.Join(command.Args, " "))
}
name := command.parsedArgs.args[index].Name
if isChan {
if !isFirstChan {
ev.Args[name] = channel
if ch, ok := state.Channel(channel); ok {
ev.Channel = &ch
ev.TargetChannel = &ch
}
return false, nil
}
ev.Args[name] = msgArgs[index]
if ch, ok := state.Channel(msgArgs[index]); ok {
ev.TargetChannel = &ch
}
return true, nil
} else if isFirstChan {
ev.Args[name] = msgArgs[index]
if ch, ok := state.Channel(msgArgs[index]); ok {
ev.TargetChannel = &ch
}
return true, nil
}
return false, errors.Errorf(errFmtArgumentNotChannel, msgArgs[index])
}
// parseUserArg takes user arguments and assigns them to the correct structures
// in a command data struct.
func parseUserArg(ev *Event, state *data.State,
store *data.Store, srv, name string, t argType, users ...string) error {
vargs := t.has(argTypeVariadic)
nUsers := len(users)
var access *data.StoredUser
var user *data.User
var err error
addData := func(index int) {
if access != nil {
if vargs {
ev.TargetVarStoredUsers[index] = access
} else {
ev.TargetStoredUsers[name] = access
}
}
if user != nil {
if vargs {
ev.TargetVarUsers[index] = user
} else {
ev.TargetUsers[name] = user
}
}
}
if vargs {
ev.TargetVarUsers = make([]*data.User, nUsers)
} else {
if ev.TargetUsers == nil {
ev.TargetUsers = make(map[string]*data.User)
}
}
switch t & argTypeUSERMASK {
case argTypeUser:
if vargs {
ev.TargetVarStoredUsers = make([]*data.StoredUser, nUsers)
} else {
if ev.TargetStoredUsers == nil {
ev.TargetStoredUsers = make(map[string]*data.StoredUser)
}
}
for i, u := range users {
access, user, err = findAccessByUser(state, store, ev, srv, u)
if err != nil {
return err
}
addData(i)
}
case argTypeNick:
for i, u := range users {
user, err = findUserByNick(state, ev, u)
if err != nil {
return err
}
addData(i)
}
}
return nil
}
// findUserByNick finds a user by their nickname. An error is returned if
// they were not found.
func findUserByNick(state *data.State, ev *Event, nick string) (*data.User, error) {
if state == nil {
return nil, errors.New(errMsgStateDisabled)
}
if user, ok := state.User(nick); ok {
return &user, nil
}
return nil, errors.Errorf(errFmtUserNotFound, nick)
}
// findAccessByUser locates a user's access based on their nick or
// username. To look up by username instead of nick use the * prefix before the
// username in the string. The user parameter is returned when a nickname lookup
// is done. An error occurs if the user is not found, the user is not authed,
// the username is not registered, etc.
func findAccessByUser(state *data.State, store *data.Store, ev *Event,
server, nickOrUser string) (
access *data.StoredUser, user *data.User, err error) {
if store == nil {
err = errors.New(errMsgStoreDisabled)
return access, user, err
}
switch nickOrUser[0] {
case '*':
if len(nickOrUser) == 1 {
err = errors.New(errMsgMissingUsername)
return access, user, err
}
uname := nickOrUser[1:]
access, err = store.FindUser(uname)
if access == nil {
err = errors.Errorf(errFmtUserNotRegistered, uname)
return access, user, err
}
default:
if state == nil {
err = errors.New(errMsgStateDisabled)
return access, user, err
}
u, ok := state.User(nickOrUser)
if !ok {
err = errors.Errorf(errFmtUserNotFound, nickOrUser)
return access, user, err
}
user = &u
access = store.AuthedUser(server, user.Host.String())
if access == nil {
err = errors.Errorf(errFmtUserNotAuthed, nickOrUser)
return
}
}
if err != nil {
err = errors.Errorf(errFmtInternal, err)
}
return access, user, err
}