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
|
package main
import (
"citrons.xyz/talk/proto"
"citrons.xyz/talk/client/window"
)
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) {
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 "quit":
a.quit = true
return
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.Buffer().Add(hm)
}
return
}
if !a.connected {
return
}
switch command {
case "nick":
a.setNick(text)
return
case "join":
if a.authenticated {
a.join(text)
}
return
case "leave":
leave := func(w window.Location) {
win := a.windowCache.Open(w)
switch win.(type) {
case *channelWindow:
win.(*channelWindow).leaveChannel()
}
}
if text == "" {
leave(a.currentWindow)
a.currentWindow = cmdWindowLocation {}
} else {
a.lookup(text, "channel", func(ch *proto.Object, err *proto.Fail) {
if err != nil {
a.cmdWindow.fail(proto.Object(*err))
} else if ch.Id != "" {
leave(channelLocation {id: ch.Id})
}
})
}
return
case "rename":
win := a.windowCache.Get(a.currentWindow)
switch win.(type) {
case *channelWindow:
win.(*channelWindow).renameChannel(text)
}
return
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.Buffer().Add(msg)
})
}
return
case "status":
if !a.authenticated {
break
}
cb := func(response proto.Command) {
if response.Kind == "fail" && len(response.Args) > 0 {
a.cmdWindow.fail(response.Args[0])
}
}
a.sendUpdate(proto.Object {
"u", a.uid, map[string]string {"status": text},
}, cb)
return
case "who":
a.lookup(text, "u", func(u *proto.Object, fail *proto.Fail) {
if fail != nil {
a.cmdWindow.fail(proto.Object(*fail))
} else {
a.cmdWindow.who(u.Id)
}
})
return
case "msg":
a.lookup(text, "u", func(u *proto.Object, fail *proto.Fail) {
if fail != nil {
a.cmdWindow.fail(proto.Object(*fail))
} else {
a.joinDirect([]proto.Object {*u})
}
})
return
case "msgall":
a.lookupAll(args, "u", func(us []proto.Object, fail *proto.Fail) {
if fail != nil {
a.cmdWindow.fail(proto.Object(*fail))
} else {
a.joinDirect(us)
}
})
return
case "create":
a.createChannel(text)
return
case "password":
if text == "" {
a.cmdWindow.err("password cannot be empty")
return
}
a.pushPrompt(&passwordChangePrompt {password: text})
return
case "debug":
a.goTo(debugWindowLocation{})
return
}
a.cmdWindow.err("unknown command: /" + command)
}
|