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
|
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) bool {
if len(args) != n {
a.cmdWindow.err(
"%s: expected %d arguments, was %d", command, n, len(args),
)
return false
}
return true
}
switch command {
case "nick":
a.setNick(text)
case "join":
if a.authenticated {
a.join(text)
}
case "leave":
if !argN(0) {
break
}
win := a.windowCache.Get(a.currentWindow)
switch win.(type) {
case *channelWindow:
win.(*channelWindow).leaveChannel()
}
a.currentWindow = cmdWindowLocation {}
case "rename":
win := a.windowCache.Get(a.currentWindow)
switch win.(type) {
case *channelWindow:
win.(*channelWindow).renameChannel(text)
}
case "list":
if !argN(0) {
break
}
win := a.windowCache.Get(a.currentWindow)
switch win.(type) {
case *channelWindow:
win.(*channelWindow).userList(func(msg userListMsg) {
a.cmdWindow.buf.Add(msg)
})
}
case "create":
if a.authenticated {
a.createChannel(text)
}
case "help":
var (
hm helpMsg
ok bool
cmd string
)
if len(args) == 0 {
hm, ok = getHelp("")
} else {
if !argN(1) {
break
}
cmd = args[0]
if cmd[0] == '/' {
if len(cmd) > 1 {
cmd = cmd[1:]
} else {
cmd = ""
}
}
hm, ok = getHelp(cmd)
}
if !ok {
a.cmdWindow.err("unknown command: /" + cmd)
} else {
a.cmdWindow.buf.Add(hm)
}
case "quit":
a.quit = true
default:
a.cmdWindow.err("unknown command: /" + command)
}
}
|