-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
609 lines (522 loc) · 16.3 KB
/
client.go
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2014-2015 Edmund Huber
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import (
"errors"
"fmt"
"log"
"net"
"runtime/debug"
"strconv"
"strings"
"time"
"github.com/DanielOaks/girc-go/ircmsg"
"github.com/DanielOaks/go-ident"
)
const (
IDLE_TIMEOUT = time.Minute + time.Second*30 // how long before a client is considered idle
QUIT_TIMEOUT = time.Minute // how long after idle before a client is kicked
IdentTimeoutSeconds = 5
)
var (
TIMEOUT_STATED_SECONDS = strconv.Itoa(int((IDLE_TIMEOUT + QUIT_TIMEOUT).Seconds()))
ErrNickAlreadySet = errors.New("Nickname is already set")
)
// Client is an IRC client.
type Client struct {
account *ClientAccount
atime time.Time
authorized bool
awayMessage string
capabilities CapabilitySet
capState CapState
capVersion CapVersion
certfp string
channels ChannelSet
class *OperClass
ctime time.Time
flags map[Mode]bool
isDestroyed bool
isQuitting bool
hasQuit bool
hops int
hostname string
rawHostname string
vhost string
idleTimer *time.Timer
monitoring map[string]bool
nick string
nickCasefolded string
nickMaskString string // cache for nickmask string since it's used with lots of replies
nickMaskCasefolded string
operName string
quitTimer *time.Timer
quitMessageSent bool
realname string
registered bool
saslInProgress bool
saslMechanism string
saslValue string
server *Server
socket *Socket
username string
whoisLine string
}
// NewClient returns a client with all the appropriate info setup.
func NewClient(server *Server, conn net.Conn, isTLS bool) *Client {
now := time.Now()
socket := NewSocket(conn, server.MaxSendQBytes)
go socket.RunSocketWriter()
client := &Client{
atime: now,
authorized: server.password == nil,
capabilities: make(CapabilitySet),
capState: CapNone,
capVersion: Cap301,
channels: make(ChannelSet),
ctime: now,
flags: make(map[Mode]bool),
monitoring: make(map[string]bool),
server: server,
socket: &socket,
account: &NoAccount,
nick: "*", // * is used until actual nick is given
nickCasefolded: "*",
nickMaskString: "*", // * is used until actual nick is given
}
if isTLS {
client.flags[TLS] = true
// error is not useful to us here anyways so we can ignore it
client.certfp, _ = client.socket.CertFP()
}
if server.checkIdent {
_, serverPortString, err := net.SplitHostPort(conn.LocalAddr().String())
serverPort, _ := strconv.Atoi(serverPortString)
if err != nil {
log.Fatal(err)
}
clientHost, clientPortString, err := net.SplitHostPort(conn.RemoteAddr().String())
clientPort, _ := strconv.Atoi(clientPortString)
if err != nil {
log.Fatal(err)
}
client.Notice("*** Looking up your username")
resp, err := ident.Query(clientHost, serverPort, clientPort, IdentTimeoutSeconds)
if err == nil {
username := resp.Identifier
_, err := CasefoldName(username) // ensure it's a valid username
if err == nil {
client.Notice("*** Found your username")
client.username = username
// we don't need to updateNickMask here since nickMask is not used for anything yet
} else {
client.Notice("*** Got a malformed username, ignoring")
}
} else {
client.Notice("*** Could not find your username")
}
}
client.Touch()
go client.run()
return client
}
//
// command goroutine
//
func (client *Client) maxlens() (int, int) {
maxlenTags := 512
maxlenRest := 512
if client.capabilities[MessageTags] {
maxlenTags = 4096
}
if client.capabilities[MaxLine] {
if client.server.limits.LineLen.Tags > maxlenTags {
maxlenTags = client.server.limits.LineLen.Tags
}
maxlenRest = client.server.limits.LineLen.Rest
}
return maxlenTags, maxlenRest
}
func (client *Client) run() {
var err error
var isExiting bool
var line string
var msg ircmsg.IrcMessage
// Set the hostname for this client
client.rawHostname = AddrLookupHostname(client.socket.conn.RemoteAddr())
//TODO(dan): Make this a socketreactor from ircbnc
for {
line, err = client.socket.Read()
if err != nil {
client.Quit("connection closed")
break
}
maxlenTags, maxlenRest := client.maxlens()
client.server.logger.Debug("userinput ", client.nick, "<- ", line)
msg, err = ircmsg.ParseLineMaxLen(line, maxlenTags, maxlenRest)
if err == ircmsg.ErrorLineIsEmpty {
continue
} else if err != nil {
client.Quit("received malformed line")
break
}
cmd, exists := Commands[msg.Command]
if !exists {
if len(msg.Command) > 0 {
client.Send(nil, client.server.name, ERR_UNKNOWNCOMMAND, client.nick, msg.Command, "Unknown command")
} else {
client.Send(nil, client.server.name, ERR_UNKNOWNCOMMAND, client.nick, "lastcmd", "No command given")
}
continue
}
isExiting = cmd.Run(client.server, client, msg)
if isExiting || client.isQuitting {
break
}
}
// ensure client connection gets closed
client.destroy()
}
//
// quit timer goroutine
//
func (client *Client) connectionTimeout() {
client.Quit(fmt.Sprintf("Ping timeout: %s seconds", TIMEOUT_STATED_SECONDS))
client.isQuitting = true
}
//
// idle timer goroutine
//
func (client *Client) connectionIdle() {
client.server.idle <- client
}
//
// server goroutine
//
// Active marks the client as 'active' (i.e. the user should be there).
func (client *Client) Active() {
client.atime = time.Now()
}
// Touch marks the client as alive.
func (client *Client) Touch() {
if client.quitTimer != nil {
client.quitTimer.Stop()
}
if client.idleTimer == nil {
client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.connectionIdle)
} else {
client.idleTimer.Reset(IDLE_TIMEOUT)
}
}
// Idle resets the timeout handlers and sends the client a PING.
func (client *Client) Idle() {
client.Send(nil, "", "PING", client.nick)
if client.quitTimer == nil {
client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.connectionTimeout)
} else {
client.quitTimer.Reset(QUIT_TIMEOUT)
}
}
// Register sets the client details as appropriate when entering the network.
func (client *Client) Register() {
if client.registered {
return
}
client.registered = true
client.Touch()
client.updateNickMask()
client.alertMonitors()
}
// IdleTime returns how long this client's been idle.
func (client *Client) IdleTime() time.Duration {
return time.Since(client.atime)
}
// SignonTime returns this client's signon time as a unix timestamp.
func (client *Client) SignonTime() int64 {
return client.ctime.Unix()
}
// IdleSeconds returns the number of seconds this client's been idle.
func (client *Client) IdleSeconds() uint64 {
return uint64(client.IdleTime().Seconds())
}
// HasNick returns true if the client's nickname is set (used in registration).
func (client *Client) HasNick() bool {
return client.nick != "" && client.nick != "*"
}
// HasUsername returns true if the client's username is set (used in registration).
func (client *Client) HasUsername() bool {
return client.username != "" && client.username != "*"
}
// HasCapabs returns true if client has the given (role) capabilities.
func (client *Client) HasCapabs(capabs ...string) bool {
if client.class == nil {
return false
}
for _, capab := range capabs {
if !client.class.Capabilities[capab] {
return false
}
}
return true
}
// ModeString returns the mode string for this client.
func (client *Client) ModeString() (str string) {
str = "+"
for flag := range client.flags {
str += flag.String()
}
return
}
// Friends refers to clients that share a channel with this client.
func (client *Client) Friends(Capabilities ...Capability) ClientSet {
friends := make(ClientSet)
// make sure that I have the right caps
hasCaps := true
for _, Cap := range Capabilities {
if !client.capabilities[Cap] {
hasCaps = false
break
}
}
if hasCaps {
friends.Add(client)
}
for channel := range client.channels {
channel.membersMutex.RLock()
for member := range channel.members {
// make sure they have all the required caps
for _, Cap := range Capabilities {
if !member.capabilities[Cap] {
continue
}
}
friends.Add(member)
}
channel.membersMutex.RUnlock()
}
return friends
}
// updateNick updates the casefolded nickname.
func (client *Client) updateNick() {
casefoldedName, err := CasefoldName(client.nick)
if err != nil {
log.Println(fmt.Sprintf("ERROR: Nick [%s] couldn't be casefolded... this should never happen. Printing stacktrace.", client.nick))
debug.PrintStack()
}
client.nickCasefolded = casefoldedName
}
// updateNickMask updates the casefolded nickname and nickmask.
func (client *Client) updateNickMask() {
client.updateNick()
if len(client.vhost) > 0 {
client.hostname = client.vhost
} else {
client.hostname = client.rawHostname
}
client.nickMaskString = fmt.Sprintf("%s!%s@%s", client.nick, client.username, client.hostname)
nickMaskCasefolded, err := Casefold(client.nickMaskString)
if err != nil {
log.Println(fmt.Sprintf("ERROR: Nickmask [%s] couldn't be casefolded... this should never happen. Printing stacktrace.", client.nickMaskString))
debug.PrintStack()
}
client.nickMaskCasefolded = nickMaskCasefolded
}
// AllNickmasks returns all the possible nickmasks for the client.
func (client *Client) AllNickmasks() []string {
var masks []string
var mask string
var err error
if len(client.vhost) > 0 {
mask, err = Casefold(fmt.Sprintf("%s!%s@%s", client.nick, client.username, client.vhost))
if err == nil {
masks = append(masks, mask)
}
}
mask, err = Casefold(fmt.Sprintf("%s!%s@%s", client.nick, client.username, client.rawHostname))
if err == nil {
masks = append(masks, mask)
}
mask2, err := Casefold(fmt.Sprintf("%s!%s@%s", client.nick, client.username, IPString(client.socket.conn.RemoteAddr())))
if err == nil && mask2 != mask {
masks = append(masks, mask2)
}
return masks
}
// SetNickname sets the very first nickname for the client.
func (client *Client) SetNickname(nickname string) error {
if client.HasNick() {
client.server.logger.Error("nick", fmt.Sprintf("%s nickname already set, something is wrong with server consistency", client.nickMaskString))
return ErrNickAlreadySet
}
err := client.server.clients.Add(client, nickname)
if err == nil {
client.nick = nickname
client.updateNick()
}
return err
}
// ChangeNickname changes the existing nickname of the client.
func (client *Client) ChangeNickname(nickname string) error {
origNickMask := client.nickMaskString
err := client.server.clients.Replace(client.nick, nickname, client)
if err == nil {
client.server.logger.Debug("nick", fmt.Sprintf("%s changed nickname to %s", client.nick, nickname))
client.server.whoWas.Append(client)
client.nick = nickname
client.updateNickMask()
for friend := range client.Friends() {
friend.Send(nil, origNickMask, "NICK", nickname)
}
}
return err
}
// Quit sends the given quit message to the client (but does not destroy them).
func (client *Client) Quit(message string) {
if !client.quitMessageSent {
client.Send(nil, client.nickMaskString, "QUIT", message)
client.Send(nil, "", "ERROR", message)
client.quitMessageSent = true
}
}
// destroy gets rid of a client, removes them from server lists etc.
func (client *Client) destroy() {
if client.isDestroyed {
return
}
client.server.logger.Debug("quit", fmt.Sprintf("%s is no longer on the server", client.nick))
// send quit/error message to client if they haven't been sent already
client.Quit("Connection closed")
client.isDestroyed = true
client.server.whoWas.Append(client)
friends := client.Friends()
friends.Remove(client)
// remove from connection limits
ipaddr := net.ParseIP(IPString(client.socket.conn.RemoteAddr()))
// this check shouldn't be required but eh
if ipaddr != nil {
client.server.connectionLimitsMutex.Lock()
client.server.connectionLimits.RemoveClient(ipaddr)
client.server.connectionLimitsMutex.Unlock()
}
// remove from opers list
_, exists := client.server.currentOpers[client]
if exists {
delete(client.server.currentOpers, client)
}
// alert monitors
for _, mClient := range client.server.monitoring[client.nickCasefolded] {
mClient.Send(nil, client.server.name, RPL_MONOFFLINE, mClient.nick, client.nick)
}
// remove my monitors
client.clearMonitorList()
// clean up channels
for channel := range client.channels {
channel.Quit(client, &friends)
}
// clean up server
client.server.clients.Remove(client)
// clean up self
if client.idleTimer != nil {
client.idleTimer.Stop()
}
if client.quitTimer != nil {
client.quitTimer.Stop()
}
client.socket.Close()
// send quit messages to friends
for friend := range friends {
//TODO(dan): store quit message in user, if exists use that instead here
friend.Send(nil, client.nickMaskString, "QUIT", "Exited")
}
}
// SendSplitMsgFromClient sends an IRC PRIVMSG/NOTICE coming from a specific client.
// Adds account-tag to the line as well.
func (client *Client) SendSplitMsgFromClient(msgid string, from *Client, tags *map[string]ircmsg.TagValue, command, target string, message SplitMessage) {
if client.capabilities[MaxLine] {
client.SendFromClient(msgid, from, tags, command, target, message.ForMaxLine)
} else {
for _, str := range message.For512 {
client.SendFromClient(msgid, from, tags, command, target, str)
}
}
}
// SendFromClient sends an IRC line coming from a specific client.
// Adds account-tag to the line as well.
func (client *Client) SendFromClient(msgid string, from *Client, tags *map[string]ircmsg.TagValue, command string, params ...string) error {
// attach account-tag
if client.capabilities[AccountTag] && from.account != &NoAccount {
if tags == nil {
tags = ircmsg.MakeTags("account", from.account.Name)
} else {
(*tags)["account"] = ircmsg.MakeTagValue(from.account.Name)
}
}
// attach message-id
if len(msgid) > 0 && client.capabilities[MessageIDs] {
if tags == nil {
tags = ircmsg.MakeTags("draft/msgid", msgid)
} else {
(*tags)["draft/msgid"] = ircmsg.MakeTagValue(msgid)
}
}
return client.Send(tags, from.nickMaskString, command, params...)
}
var (
// these are all the output commands that MUST have their last param be a trailing.
// this is needed because silly clients like to treat trailing as separate from the
// other params in messages.
commandsThatMustUseTrailing = map[string]bool{
"PRIVMSG": true,
"NOTICE": true,
RPL_WHOISCHANNELS: true,
RPL_USERHOST: true,
}
)
// Send sends an IRC line to the client.
func (client *Client) Send(tags *map[string]ircmsg.TagValue, prefix string, command string, params ...string) error {
// attach server-time
if client.capabilities[ServerTime] {
t := time.Now().UTC().Format("2006-01-02T15:04:05.999Z")
if tags == nil {
tags = ircmsg.MakeTags("time", t)
} else {
(*tags)["time"] = ircmsg.MakeTagValue(t)
}
}
// force trailing
var usedSpaceHack bool
if commandsThatMustUseTrailing[strings.ToUpper(command)] && len(params) > 0 {
lastParam := params[len(params)-1]
if !strings.Contains(lastParam, " ") {
params[len(params)-1] = lastParam + " "
usedSpaceHack = true
}
}
// send out the message
message := ircmsg.MakeMessage(tags, prefix, command, params...)
maxlenTags, maxlenRest := client.maxlens()
line, err := message.LineMaxLen(maxlenTags, maxlenRest)
if err != nil {
// try not to fail quietly - especially useful when running tests, as a note to dig deeper
// log.Println("Error assembling message:")
// spew.Dump(message)
// debug.PrintStack()
message = ircmsg.MakeMessage(nil, client.server.name, ERR_UNKNOWNERROR, "*", "Error assembling message for sending")
line, _ := message.Line()
client.socket.Write(line)
return err
}
// strip space hack if we used it
if usedSpaceHack {
line = line[:len(line)-3] + "\r\n"
}
client.server.logger.Debug("useroutput", client.nick, " ->", strings.TrimRight(line, "\r\n"))
client.socket.Write(line)
return nil
}
// Notice sends the client a notice from the server.
func (client *Client) Notice(text string) {
client.Send(nil, client.server.name, "NOTICE", client.nick, text)
}