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
|
package main
func isCommand(text string) (bool, string) {
if text[0] == '/' {
if len(text) > 1 && text[1] == '/' {
text = text[1:]
return false, text
}
return true, text
}
return false, text
}
func (a *application) processCommand(text string) {
text = text[1:]
args := []string {""}
escaped := false
quoted := false
for _, c := range text {
if escaped {
args[len(args) - 1] += string(c)
continue
}
switch c {
case '\\':
escaped = true
case '"':
quoted = !quoted
case ' ':
if !quoted {
if args[len(args) - 1] != "" {
args = append(args, "")
}
break
}
fallthrough
default:
args[len(args) - 1] += string(c)
}
}
if len(text) > len(args[0]) {
text = text[len(args[0]) + 1:]
} else {
text = ""
}
a.doCommand(args[0], args[1:], text)
}
func (a *application) doCommand(command string, args []string, text string) {
if !a.connected {
return
}
argN := func(n int) {
if len(args) != n {
a.cmdWindow.err(
"%s: expected %d arguments, was %d", command, n, len(args),
)
}
}
switch command {
case "nick":
a.setNick(text)
case "join":
argN(1)
if a.authenticated {
a.join(args[0])
}
case "leave":
argN(0)
win := a.windowCache.Get(a.currentWindow)
switch win.(type) {
case *channelWindow:
win.(*channelWindow).leaveChannel()
}
a.currentWindow = cmdWindowLocation {}
case "create":
argN(1)
if a.authenticated {
a.createChannel(args[0])
}
case "quit":
a.quit = true
default:
a.cmdWindow.err("unknown command: /" + command)
}
}
|