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
|
// Copyright (c) Liam Stanley <me@liamstanley.io>. All rights reserved. Use
// of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package girc
import (
"errors"
"fmt"
)
// Commands holds a large list of useful methods to interact with the server,
// and wrappers for common events.
type Commands struct {
c *Client
}
// Nick changes the client nickname.
func (cmd *Commands) Nick(name string) error {
if !IsValidNick(name) {
return &ErrInvalidTarget{Target: name}
}
cmd.c.Send(&Event{Command: NICK, Params: []string{name}})
return nil
}
// Join attempts to enter a list of IRC channels, at bulk if possible to
// prevent sending extensive JOIN commands.
func (cmd *Commands) Join(channels ...string) error {
// We can join multiple channels at once, however we need to ensure that
// we are not exceeding the line length. (see maxLength)
max := maxLength - len(JOIN) - 1
var buffer string
for i := 0; i < len(channels); i++ {
if !IsValidChannel(channels[i]) {
return &ErrInvalidTarget{Target: channels[i]}
}
if len(buffer+","+channels[i]) > max {
cmd.c.Send(&Event{Command: JOIN, Params: []string{buffer}})
buffer = ""
continue
}
if len(buffer) == 0 {
buffer = channels[i]
} else {
buffer += "," + channels[i]
}
if i == len(channels)-1 {
cmd.c.Send(&Event{Command: JOIN, Params: []string{buffer}})
return nil
}
}
return nil
}
// JoinKey attempts to enter an IRC channel with a password.
func (cmd *Commands) JoinKey(channel, password string) error {
if !IsValidChannel(channel) {
return &ErrInvalidTarget{Target: channel}
}
cmd.c.Send(&Event{Command: JOIN, Params: []string{channel, password}})
return nil
}
// Part leaves an IRC channel.
func (cmd *Commands) Part(channel, message string) error {
if !IsValidChannel(channel) {
return &ErrInvalidTarget{Target: channel}
}
cmd.c.Send(&Event{Command: JOIN, Params: []string{channel}})
return nil
}
// PartMessage leaves an IRC channel with a specified leave message.
func (cmd *Commands) PartMessage(channel, message string) error {
if !IsValidChannel(channel) {
return &ErrInvalidTarget{Target: channel}
}
cmd.c.Send(&Event{Command: JOIN, Params: []string{channel}, Trailing: message})
return nil
}
// SendCTCP sends a CTCP request to target. Note that this method uses
// PRIVMSG specifically.
func (cmd *Commands) SendCTCP(target, ctcpType, message string) error {
out := encodeCTCPRaw(ctcpType, message)
if out == "" {
return errors.New("invalid CTCP")
}
return cmd.Message(target, out)
}
// SendCTCPf sends a CTCP request to target using a specific format. Note that
// this method uses PRIVMSG specifically.
func (cmd *Commands) SendCTCPf(target, ctcpType, format string, a ...interface{}) error {
return cmd.SendCTCP(target, ctcpType, fmt.Sprintf(format, a...))
}
// SendCTCPReplyf sends a CTCP response to target using a specific format.
// Note that this method uses NOTICE specifically.
func (cmd *Commands) SendCTCPReplyf(target, ctcpType, format string, a ...interface{}) error {
return cmd.SendCTCPReply(target, ctcpType, fmt.Sprintf(format, a...))
}
// SendCTCPReply sends a CTCP response to target. Note that this method uses
// NOTICE specifically.
func (cmd *Commands) SendCTCPReply(target, ctcpType, message string) error {
out := encodeCTCPRaw(ctcpType, message)
if out == "" {
return errors.New("invalid CTCP")
}
return cmd.Notice(target, out)
}
// Message sends a PRIVMSG to target (either channel, service, or user).
func (cmd *Commands) Message(target, message string) error {
if !IsValidNick(target) && !IsValidChannel(target) {
return &ErrInvalidTarget{Target: target}
}
cmd.c.Send(&Event{Command: PRIVMSG, Params: []string{target}, Trailing: message})
return nil
}
// Messagef sends a formated PRIVMSG to target (either channel, service, or
// user).
func (cmd *Commands) Messagef(target, format string, a ...interface{}) error {
return cmd.Message(target, fmt.Sprintf(format, a...))
}
// ErrInvalidSource is returned when a method needs to know the origin of an
// event, however Event.Source is unknown (e.g. sent by the user, not the
// server.)
var ErrInvalidSource = errors.New("event has nil or invalid source address")
// Reply sends a reply to channel or user, based on where the supplied event
// originated from. See also ReplyTo().
func (cmd *Commands) Reply(event Event, message string) error {
if event.Source == nil {
return ErrInvalidSource
}
if len(event.Params) > 0 && IsValidChannel(event.Params[0]) {
return cmd.Message(event.Params[0], message)
}
return cmd.Message(event.Source.Name, message)
}
// Replyf sends a reply to channel or user with a format string, based on
// where the supplied event originated from. See also ReplyTof().
func (cmd *Commands) Replyf(event Event, format string, a ...interface{}) error {
return cmd.Reply(event, fmt.Sprintf(format, a...))
}
// ReplyTo sends a reply to a channel or user, based on where the supplied
// event originated from. ReplyTo(), when originating from a channel will
// default to replying with "<user>, <message>". See also Reply().
func (cmd *Commands) ReplyTo(event Event, message string) error {
if event.Source == nil {
return ErrInvalidSource
}
if len(event.Params) > 0 && IsValidChannel(event.Params[0]) {
return cmd.Message(event.Params[0], event.Source.Name+", "+message)
}
return cmd.Message(event.Source.Name, message)
}
// ReplyTof sends a reply to a channel or user with a format string, based
// on where the supplied event originated from. ReplyTo(), when originating
// from a channel will default to replying with "<user>, <message>". See
// also Replyf().
func (cmd *Commands) ReplyTof(event Event, format string, a ...interface{}) error {
return cmd.ReplyTo(event, fmt.Sprintf(format, a...))
}
// Action sends a PRIVMSG ACTION (/me) to target (either channel, service,
// or user).
func (cmd *Commands) Action(target, message string) error {
if !IsValidNick(target) && !IsValidChannel(target) {
return &ErrInvalidTarget{Target: target}
}
cmd.c.Send(&Event{
Command: PRIVMSG,
Params: []string{target},
Trailing: fmt.Sprintf("\001ACTION %s\001", message),
})
return nil
}
// Actionf sends a formated PRIVMSG ACTION (/me) to target (either channel,
// service, or user).
func (cmd *Commands) Actionf(target, format string, a ...interface{}) error {
return cmd.Action(target, fmt.Sprintf(format, a...))
}
// Notice sends a NOTICE to target (either channel, service, or user).
func (cmd *Commands) Notice(target, message string) error {
if !IsValidNick(target) && !IsValidChannel(target) {
return &ErrInvalidTarget{Target: target}
}
cmd.c.Send(&Event{Command: NOTICE, Params: []string{target}, Trailing: message})
return nil
}
// Noticef sends a formated NOTICE to target (either channel, service, or
// user).
func (cmd *Commands) Noticef(target, format string, a ...interface{}) error {
return cmd.Notice(target, fmt.Sprintf(format, a...))
}
// SendRaw sends a raw string back to the server, without carriage returns
// or newlines.
func (cmd *Commands) SendRaw(raw string) error {
e := ParseEvent(raw)
if e == nil {
return errors.New("invalid event: " + raw)
}
cmd.c.Send(e)
return nil
}
// SendRawf sends a formated string back to the server, without carriage
// returns or newlines.
func (cmd *Commands) SendRawf(format string, a ...interface{}) error {
return cmd.SendRaw(fmt.Sprintf(format, a...))
}
// Topic sets the topic of channel to message. Does not verify the length
// of the topic.
func (cmd *Commands) Topic(channel, message string) {
cmd.c.Send(&Event{Command: TOPIC, Params: []string{channel}, Trailing: message})
}
// Who sends a WHO query to the server, which will attempt WHOX by default.
// See http://faerion.sourceforge.net/doc/irc/whox.var for more details. This
// sends "%tcuhnr,2" per default. Do not use "1" as this will conflict with
// girc's builtin tracking functionality.
func (cmd *Commands) Who(target string) error {
if !IsValidNick(target) && !IsValidChannel(target) && !IsValidUser(target) {
return &ErrInvalidTarget{Target: target}
}
cmd.c.Send(&Event{Command: WHO, Params: []string{target, "%tcuhnr,2"}})
return nil
}
// Whois sends a WHOIS query to the server, targeted at a specific user.
// as WHOIS is a bit slower, you may want to use WHO for brief user info.
func (cmd *Commands) Whois(nick string) error {
if !IsValidNick(nick) {
return &ErrInvalidTarget{Target: nick}
}
cmd.c.Send(&Event{Command: WHOIS, Params: []string{nick}})
return nil
}
// Ping sends a PING query to the server, with a specific identifier that
// the server should respond with.
func (cmd *Commands) Ping(id string) {
cmd.c.write(&Event{Command: PING, Params: []string{id}})
}
// Pong sends a PONG query to the server, with an identifier which was
// received from a previous PING query received by the client.
func (cmd *Commands) Pong(id string) {
cmd.c.write(&Event{Command: PONG, Params: []string{id}})
}
// Oper sends a OPER authentication query to the server, with a username
// and password.
func (cmd *Commands) Oper(user, pass string) {
cmd.c.Send(&Event{Command: OPER, Params: []string{user, pass}, Sensitive: true})
}
// Kick sends a KICK query to the server, attempting to kick nick from
// channel, with reason. If reason is blank, one will not be sent to the
// server.
func (cmd *Commands) Kick(channel, nick, reason string) error {
if !IsValidChannel(channel) {
return &ErrInvalidTarget{Target: channel}
}
if !IsValidNick(nick) {
return &ErrInvalidTarget{Target: nick}
}
if reason != "" {
cmd.c.Send(&Event{Command: KICK, Params: []string{channel, nick}, Trailing: reason})
return nil
}
cmd.c.Send(&Event{Command: KICK, Params: []string{channel, nick}})
return nil
}
// Invite sends a INVITE query to the server, to invite nick to channel.
func (cmd *Commands) Invite(channel, nick string) error {
if !IsValidChannel(channel) {
return &ErrInvalidTarget{Target: channel}
}
if !IsValidNick(nick) {
return &ErrInvalidTarget{Target: nick}
}
cmd.c.Send(&Event{Command: INVITE, Params: []string{nick, channel}})
return nil
}
// Away sends a AWAY query to the server, suggesting that the client is no
// longer active. If reason is blank, Client.Back() is called. Also see
// Client.Back().
func (cmd *Commands) Away(reason string) {
if reason == "" {
cmd.Back()
return
}
cmd.c.Send(&Event{Command: AWAY, Params: []string{reason}})
}
// Back sends a AWAY query to the server, however the query is blank,
// suggesting that the client is active once again. Also see Client.Away().
func (cmd *Commands) Back() {
cmd.c.Send(&Event{Command: AWAY})
}
// List sends a LIST query to the server, which will list channels and topics.
// Supports multiple channels at once, in hopes it will reduce extensive
// LIST queries to the server. Supply no channels to run a list against the
// entire server (warning, that may mean LOTS of channels!)
func (cmd *Commands) List(channels ...string) error {
if len(channels) == 0 {
cmd.c.Send(&Event{Command: LIST})
return nil
}
// We can LIST multiple channels at once, however we need to ensure that
// we are not exceeding the line length. (see maxLength)
max := maxLength - len(JOIN) - 1
var buffer string
for i := 0; i < len(channels); i++ {
if !IsValidChannel(channels[i]) {
return &ErrInvalidTarget{Target: channels[i]}
}
if len(buffer+","+channels[i]) > max {
cmd.c.Send(&Event{Command: LIST, Params: []string{buffer}})
buffer = ""
continue
}
if len(buffer) == 0 {
buffer = channels[i]
} else {
buffer += "," + channels[i]
}
if i == len(channels)-1 {
cmd.c.Send(&Event{Command: LIST, Params: []string{buffer}})
return nil
}
}
return nil
}
// Whowas sends a WHOWAS query to the server. amount is the amount of results
// you want back.
func (cmd *Commands) Whowas(nick string, amount int) error {
if !IsValidNick(nick) {
return &ErrInvalidTarget{Target: nick}
}
cmd.c.Send(&Event{Command: WHOWAS, Params: []string{nick, string(amount)}})
return nil
}
|