|
@@ -14,17 +14,25 @@ type BaseCommand struct {
|
14
|
14
|
client *Client
|
15
|
15
|
}
|
16
|
16
|
|
17
|
|
-func (base *BaseCommand) Client() *Client {
|
|
17
|
+func (base BaseCommand) Client() *Client {
|
18
|
18
|
return base.client
|
19
|
19
|
}
|
20
|
20
|
|
21
|
21
|
func (base *BaseCommand) SetClient(c *Client) {
|
|
22
|
+ if base.client != nil {
|
|
23
|
+ panic("SetClient called twice!")
|
|
24
|
+ }
|
22
|
25
|
base.client = c
|
23
|
26
|
}
|
24
|
27
|
|
|
28
|
+type EditableCommand interface {
|
|
29
|
+ Command
|
|
30
|
+ SetClient(*Client)
|
|
31
|
+}
|
|
32
|
+
|
25
|
33
|
var (
|
26
|
34
|
ErrParseCommand = errors.New("failed to parse message")
|
27
|
|
- parseCommandFuncs = map[string]func([]string) (Command, error){
|
|
35
|
+ parseCommandFuncs = map[string]func([]string) (EditableCommand, error){
|
28
|
36
|
"JOIN": NewJoinCommand,
|
29
|
37
|
"MODE": NewModeCommand,
|
30
|
38
|
"NICK": NewNickCommand,
|
|
@@ -39,7 +47,7 @@ var (
|
39
|
47
|
}
|
40
|
48
|
)
|
41
|
49
|
|
42
|
|
-func ParseCommand(line string) (Command, error) {
|
|
50
|
+func ParseCommand(line string) (EditableCommand, error) {
|
43
|
51
|
command, args := parseLine(line)
|
44
|
52
|
constructor := parseCommandFuncs[command]
|
45
|
53
|
if constructor == nil {
|
|
@@ -77,14 +85,14 @@ func parseLine(line string) (command string, args []string) {
|
77
|
85
|
// <command> [args...]
|
78
|
86
|
|
79
|
87
|
type UnknownCommand struct {
|
80
|
|
- *BaseCommand
|
|
88
|
+ BaseCommand
|
81
|
89
|
command string
|
82
|
90
|
args []string
|
83
|
91
|
}
|
84
|
92
|
|
85
|
93
|
func NewUnknownCommand(command string, args []string) *UnknownCommand {
|
86
|
94
|
return &UnknownCommand{
|
87
|
|
- BaseCommand: &BaseCommand{},
|
|
95
|
+ BaseCommand: BaseCommand{},
|
88
|
96
|
command: command,
|
89
|
97
|
args: args,
|
90
|
98
|
}
|
|
@@ -93,17 +101,17 @@ func NewUnknownCommand(command string, args []string) *UnknownCommand {
|
93
|
101
|
// PING <server1> [ <server2> ]
|
94
|
102
|
|
95
|
103
|
type PingCommand struct {
|
96
|
|
- *BaseCommand
|
|
104
|
+ BaseCommand
|
97
|
105
|
server string
|
98
|
106
|
server2 string
|
99
|
107
|
}
|
100
|
108
|
|
101
|
|
-func NewPingCommand(args []string) (Command, error) {
|
|
109
|
+func NewPingCommand(args []string) (EditableCommand, error) {
|
102
|
110
|
if len(args) < 1 {
|
103
|
111
|
return nil, NotEnoughArgsError
|
104
|
112
|
}
|
105
|
113
|
msg := &PingCommand{
|
106
|
|
- BaseCommand: &BaseCommand{},
|
|
114
|
+ BaseCommand: BaseCommand{},
|
107
|
115
|
server: args[0],
|
108
|
116
|
}
|
109
|
117
|
if len(args) > 1 {
|
|
@@ -115,17 +123,17 @@ func NewPingCommand(args []string) (Command, error) {
|
115
|
123
|
// PONG <server> [ <server2> ]
|
116
|
124
|
|
117
|
125
|
type PongCommand struct {
|
118
|
|
- *BaseCommand
|
|
126
|
+ BaseCommand
|
119
|
127
|
server1 string
|
120
|
128
|
server2 string
|
121
|
129
|
}
|
122
|
130
|
|
123
|
|
-func NewPongCommand(args []string) (Command, error) {
|
|
131
|
+func NewPongCommand(args []string) (EditableCommand, error) {
|
124
|
132
|
if len(args) < 1 {
|
125
|
133
|
return nil, NotEnoughArgsError
|
126
|
134
|
}
|
127
|
135
|
message := &PongCommand{
|
128
|
|
- BaseCommand: &BaseCommand{},
|
|
136
|
+ BaseCommand: BaseCommand{},
|
129
|
137
|
server1: args[0],
|
130
|
138
|
}
|
131
|
139
|
if len(args) > 1 {
|
|
@@ -137,16 +145,16 @@ func NewPongCommand(args []string) (Command, error) {
|
137
|
145
|
// PASS <password>
|
138
|
146
|
|
139
|
147
|
type PassCommand struct {
|
140
|
|
- *BaseCommand
|
|
148
|
+ BaseCommand
|
141
|
149
|
password string
|
142
|
150
|
}
|
143
|
151
|
|
144
|
|
-func NewPassCommand(args []string) (Command, error) {
|
|
152
|
+func NewPassCommand(args []string) (EditableCommand, error) {
|
145
|
153
|
if len(args) < 1 {
|
146
|
154
|
return nil, NotEnoughArgsError
|
147
|
155
|
}
|
148
|
156
|
return &PassCommand{
|
149
|
|
- BaseCommand: &BaseCommand{},
|
|
157
|
+ BaseCommand: BaseCommand{},
|
150
|
158
|
password: args[0],
|
151
|
159
|
}, nil
|
152
|
160
|
}
|
|
@@ -154,16 +162,16 @@ func NewPassCommand(args []string) (Command, error) {
|
154
|
162
|
// NICK <nickname>
|
155
|
163
|
|
156
|
164
|
type NickCommand struct {
|
157
|
|
- *BaseCommand
|
|
165
|
+ BaseCommand
|
158
|
166
|
nickname string
|
159
|
167
|
}
|
160
|
168
|
|
161
|
|
-func NewNickCommand(args []string) (Command, error) {
|
|
169
|
+func NewNickCommand(args []string) (EditableCommand, error) {
|
162
|
170
|
if len(args) != 1 {
|
163
|
171
|
return nil, NotEnoughArgsError
|
164
|
172
|
}
|
165
|
173
|
return &NickCommand{
|
166
|
|
- BaseCommand: &BaseCommand{},
|
|
174
|
+ BaseCommand: BaseCommand{},
|
167
|
175
|
nickname: args[0],
|
168
|
176
|
}, nil
|
169
|
177
|
}
|
|
@@ -171,19 +179,19 @@ func NewNickCommand(args []string) (Command, error) {
|
171
|
179
|
// USER <user> <mode> <unused> <realname>
|
172
|
180
|
|
173
|
181
|
type UserMsgCommand struct {
|
174
|
|
- *BaseCommand
|
|
182
|
+ BaseCommand
|
175
|
183
|
user string
|
176
|
184
|
mode uint8
|
177
|
185
|
unused string
|
178
|
186
|
realname string
|
179
|
187
|
}
|
180
|
188
|
|
181
|
|
-func NewUserMsgCommand(args []string) (Command, error) {
|
|
189
|
+func NewUserMsgCommand(args []string) (EditableCommand, error) {
|
182
|
190
|
if len(args) != 4 {
|
183
|
191
|
return nil, NotEnoughArgsError
|
184
|
192
|
}
|
185
|
193
|
msg := &UserMsgCommand{
|
186
|
|
- BaseCommand: &BaseCommand{},
|
|
194
|
+ BaseCommand: BaseCommand{},
|
187
|
195
|
user: args[0],
|
188
|
196
|
unused: args[2],
|
189
|
197
|
realname: args[3],
|
|
@@ -198,13 +206,13 @@ func NewUserMsgCommand(args []string) (Command, error) {
|
198
|
206
|
// QUIT [ <Quit Command> ]
|
199
|
207
|
|
200
|
208
|
type QuitCommand struct {
|
201
|
|
- *BaseCommand
|
|
209
|
+ BaseCommand
|
202
|
210
|
message string
|
203
|
211
|
}
|
204
|
212
|
|
205
|
|
-func NewQuitCommand(args []string) (Command, error) {
|
|
213
|
+func NewQuitCommand(args []string) (EditableCommand, error) {
|
206
|
214
|
msg := &QuitCommand{
|
207
|
|
- BaseCommand: &BaseCommand{},
|
|
215
|
+ BaseCommand: BaseCommand{},
|
208
|
216
|
}
|
209
|
217
|
if len(args) > 0 {
|
210
|
218
|
msg.message = args[0]
|
|
@@ -215,14 +223,14 @@ func NewQuitCommand(args []string) (Command, error) {
|
215
|
223
|
// JOIN ( <channel> *( "," <channel> ) [ <key> *( "," <key> ) ] ) / "0"
|
216
|
224
|
|
217
|
225
|
type JoinCommand struct {
|
218
|
|
- *BaseCommand
|
|
226
|
+ BaseCommand
|
219
|
227
|
channels map[string]string
|
220
|
228
|
zero bool
|
221
|
229
|
}
|
222
|
230
|
|
223
|
|
-func NewJoinCommand(args []string) (Command, error) {
|
|
231
|
+func NewJoinCommand(args []string) (EditableCommand, error) {
|
224
|
232
|
msg := &JoinCommand{
|
225
|
|
- BaseCommand: &BaseCommand{},
|
|
233
|
+ BaseCommand: BaseCommand{},
|
226
|
234
|
channels: make(map[string]string),
|
227
|
235
|
}
|
228
|
236
|
|
|
@@ -252,17 +260,17 @@ func NewJoinCommand(args []string) (Command, error) {
|
252
|
260
|
// PART <channel> *( "," <channel> ) [ <Part Command> ]
|
253
|
261
|
|
254
|
262
|
type PartCommand struct {
|
255
|
|
- *BaseCommand
|
|
263
|
+ BaseCommand
|
256
|
264
|
channels []string
|
257
|
265
|
message string
|
258
|
266
|
}
|
259
|
267
|
|
260
|
|
-func NewPartCommand(args []string) (Command, error) {
|
|
268
|
+func NewPartCommand(args []string) (EditableCommand, error) {
|
261
|
269
|
if len(args) < 1 {
|
262
|
270
|
return nil, NotEnoughArgsError
|
263
|
271
|
}
|
264
|
272
|
msg := &PartCommand{
|
265
|
|
- BaseCommand: &BaseCommand{},
|
|
273
|
+ BaseCommand: BaseCommand{},
|
266
|
274
|
channels: strings.Split(args[0], ","),
|
267
|
275
|
}
|
268
|
276
|
if len(args) > 1 {
|
|
@@ -274,17 +282,17 @@ func NewPartCommand(args []string) (Command, error) {
|
274
|
282
|
// PRIVMSG <target> <message>
|
275
|
283
|
|
276
|
284
|
type PrivMsgCommand struct {
|
277
|
|
- *BaseCommand
|
|
285
|
+ BaseCommand
|
278
|
286
|
target string
|
279
|
287
|
message string
|
280
|
288
|
}
|
281
|
289
|
|
282
|
|
-func NewPrivMsgCommand(args []string) (Command, error) {
|
|
290
|
+func NewPrivMsgCommand(args []string) (EditableCommand, error) {
|
283
|
291
|
if len(args) < 2 {
|
284
|
292
|
return nil, NotEnoughArgsError
|
285
|
293
|
}
|
286
|
294
|
return &PrivMsgCommand{
|
287
|
|
- BaseCommand: &BaseCommand{},
|
|
295
|
+ BaseCommand: BaseCommand{},
|
288
|
296
|
target: args[0],
|
289
|
297
|
message: args[1],
|
290
|
298
|
}, nil
|
|
@@ -301,17 +309,17 @@ func (m *PrivMsgCommand) TargetIsChannel() bool {
|
301
|
309
|
// TOPIC [newtopic]
|
302
|
310
|
|
303
|
311
|
type TopicCommand struct {
|
304
|
|
- *BaseCommand
|
|
312
|
+ BaseCommand
|
305
|
313
|
channel string
|
306
|
314
|
topic string
|
307
|
315
|
}
|
308
|
316
|
|
309
|
|
-func NewTopicCommand(args []string) (Command, error) {
|
|
317
|
+func NewTopicCommand(args []string) (EditableCommand, error) {
|
310
|
318
|
if len(args) < 1 {
|
311
|
319
|
return nil, NotEnoughArgsError
|
312
|
320
|
}
|
313
|
321
|
msg := &TopicCommand{
|
314
|
|
- BaseCommand: &BaseCommand{},
|
|
322
|
+ BaseCommand: BaseCommand{},
|
315
|
323
|
channel: args[0],
|
316
|
324
|
}
|
317
|
325
|
if len(args) > 1 {
|
|
@@ -321,18 +329,18 @@ func NewTopicCommand(args []string) (Command, error) {
|
321
|
329
|
}
|
322
|
330
|
|
323
|
331
|
type ModeCommand struct {
|
324
|
|
- *BaseCommand
|
|
332
|
+ BaseCommand
|
325
|
333
|
nickname string
|
326
|
334
|
modes string
|
327
|
335
|
}
|
328
|
336
|
|
329
|
|
-func NewModeCommand(args []string) (Command, error) {
|
|
337
|
+func NewModeCommand(args []string) (EditableCommand, error) {
|
330
|
338
|
if len(args) == 0 {
|
331
|
339
|
return nil, NotEnoughArgsError
|
332
|
340
|
}
|
333
|
341
|
|
334
|
342
|
cmd := &ModeCommand{
|
335
|
|
- BaseCommand: &BaseCommand{},
|
|
343
|
+ BaseCommand: BaseCommand{},
|
336
|
344
|
nickname: args[0],
|
337
|
345
|
}
|
338
|
346
|
|