-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
client_handlers.go
151 lines (134 loc) · 3.49 KB
/
client_handlers.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
package irc
import (
"fmt"
"strings"
)
type clientFilter func(*Client, *Message)
// clientFilters are pre-processing which happens for certain message
// types. These were moved from below to keep the complexity of each
// component down.
var clientFilters = map[string]clientFilter{
"001": handle001,
"433": handle433,
"437": handle437,
"PING": handlePing,
"PONG": handlePong,
"NICK": handleNick,
"CAP": handleCap,
}
// From rfc2812 section 5.1 (Command responses)
//
// 001 RPL_WELCOME
// "Welcome to the Internet Relay Network
// <nick>!<user>@<host>"
func handle001(c *Client, m *Message) {
c.currentNick = m.Params[0]
c.connected = true
}
// From rfc2812 section 5.2 (Error Replies)
//
// 433 ERR_NICKNAMEINUSE
// "<nick> :Nickname is already in use"
//
// - Returned when a NICK message is processed that results
// in an attempt to change to a currently existing
// nickname.
func handle433(c *Client, m *Message) {
// We only want to try and handle nick collisions during the initial
// handshake.
if c.connected {
return
}
c.currentNick += "_"
_ = c.Writef("NICK :%s", c.currentNick)
}
// From rfc2812 section 5.2 (Error Replies)
//
// 437 ERR_UNAVAILRESOURCE
// "<nick/channel> :Nick/channel is temporarily unavailable"
//
// - Returned by a server to a user trying to join a channel
// currently blocked by the channel delay mechanism.
//
// - Returned by a server to a user trying to change nickname
// when the desired nickname is blocked by the nick delay
// mechanism.
func handle437(c *Client, m *Message) {
// We only want to try and handle nick collisions during the initial
// handshake.
if c.connected {
return
}
c.currentNick += "_"
_ = c.Writef("NICK :%s", c.currentNick)
}
func handlePing(c *Client, m *Message) {
reply := m.Copy()
reply.Command = "PONG"
_ = c.WriteMessage(reply)
}
func handlePong(c *Client, m *Message) {
if c.incomingPongChan != nil {
select {
case c.incomingPongChan <- m.Trailing():
default:
// Note that this return isn't really needed, but it helps some code
// coverage tools actually see this line.
return
}
}
}
func handleNick(c *Client, m *Message) {
if m.Prefix.Name == c.currentNick && len(m.Params) > 0 {
c.currentNick = m.Params[0]
}
}
var capFilters = map[string]clientFilter{
"LS": handleCapLs,
"ACK": handleCapAck,
"NAK": handleCapNak,
}
func handleCap(c *Client, m *Message) {
if c.remainingCapResponses <= 0 || len(m.Params) <= 2 {
return
}
if filter, ok := capFilters[m.Params[1]]; ok {
filter(c, m)
}
if c.remainingCapResponses <= 0 {
for key, capStatus := range c.caps {
if capStatus.Required && !capStatus.Enabled {
c.sendError(fmt.Errorf("CAP %s requested but not accepted", key))
return
}
}
_ = c.Write("CAP END")
}
}
func handleCapLs(c *Client, m *Message) {
for _, key := range strings.Split(m.Trailing(), " ") {
capStatus := c.caps[key]
capStatus.Available = true
c.caps[key] = capStatus
}
c.remainingCapResponses--
}
func handleCapAck(c *Client, m *Message) {
for _, key := range strings.Split(m.Trailing(), " ") {
capStatus := c.caps[key]
capStatus.Enabled = true
c.caps[key] = capStatus
}
c.remainingCapResponses--
}
func handleCapNak(c *Client, m *Message) {
// If we got a NAK and this REQ was required, we need to bail
// with an error.
for _, key := range strings.Split(m.Trailing(), " ") {
if c.caps[key].Required {
c.sendError(fmt.Errorf("CAP %s requested but was rejected", key))
return
}
}
c.remainingCapResponses--
}