/
client.go
292 lines (241 loc) · 5.87 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
package irc
import (
"fmt"
"net"
"time"
)
const (
IDLE_TIMEOUT = time.Minute // how long before a client is considered idle
QUIT_TIMEOUT = time.Minute // how long after idle before a client is kicked
)
type Client struct {
atime time.Time
authorized bool
awayMessage Text
capabilities CapabilitySet
capState CapState
channels ChannelSet
ctime time.Time
flags map[UserMode]bool
hasQuit bool
hops uint
hostname Name
idleTimer *time.Timer
nick Name
quitTimer *time.Timer
realname Text
registered bool
server *Server
socket *Socket
username Name
}
func NewClient(server *Server, conn net.Conn) *Client {
now := time.Now()
client := &Client{
atime: now,
authorized: server.password == nil,
capState: CapNone,
capabilities: make(CapabilitySet),
channels: make(ChannelSet),
ctime: now,
flags: make(map[UserMode]bool),
server: server,
socket: NewSocket(conn),
}
client.Touch()
go client.run()
return client
}
//
// command goroutine
//
func (client *Client) run() {
var command Command
var err error
var line string
// Set the hostname for this client. The client may later send a PROXY
// command from stunnel that sets the hostname to something more accurate.
client.send(NewProxyCommand(AddrLookupHostname(
client.socket.conn.RemoteAddr())))
for err == nil {
if line, err = client.socket.Read(); err != nil {
command = NewQuitCommand("connection closed")
} else if command, err = ParseCommand(line); err != nil {
switch err {
case ErrParseCommand:
//TODO(dan): use the real failed numeric for this (400)
client.Reply(RplNotice(client.server, client,
NewText("failed to parse command")))
case NotEnoughArgsError:
// TODO
}
// so the read loop will continue
err = nil
continue
} else if checkPass, ok := command.(checkPasswordCommand); ok {
checkPass.LoadPassword(client.server)
// Block the client thread while handling a potentially expensive
// password bcrypt operation. Since the server is single-threaded
// for commands, we don't want the server to perform the bcrypt,
// blocking anyone else from sending commands until it
// completes. This could be a form of DoS if handled naively.
checkPass.CheckPassword()
}
client.send(command)
}
}
func (client *Client) send(command Command) {
command.SetClient(client)
client.server.commands <- command
}
// quit timer goroutine
func (client *Client) connectionTimeout() {
client.send(NewQuitCommand("connection timeout"))
}
//
// idle timer goroutine
//
func (client *Client) connectionIdle() {
client.server.idle <- client
}
//
// server goroutine
//
func (client *Client) Active() {
client.atime = time.Now()
}
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)
}
}
func (client *Client) Idle() {
client.Reply(RplPing(client.server))
if client.quitTimer == nil {
client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.connectionTimeout)
} else {
client.quitTimer.Reset(QUIT_TIMEOUT)
}
}
func (client *Client) Register() {
if client.registered {
return
}
client.registered = true
client.Touch()
}
func (client *Client) destroy() {
// clean up channels
for channel := range client.channels {
channel.Quit(client)
}
// 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()
Log.debug.Printf("%s: destroyed", client)
}
func (client *Client) IdleTime() time.Duration {
return time.Since(client.atime)
}
func (client *Client) SignonTime() int64 {
return client.ctime.Unix()
}
func (client *Client) IdleSeconds() uint64 {
return uint64(client.IdleTime().Seconds())
}
func (client *Client) HasNick() bool {
return client.nick != ""
}
func (client *Client) HasUsername() bool {
return client.username != ""
}
// <mode>
func (c *Client) ModeString() (str string) {
for flag := range c.flags {
str += flag.String()
}
if len(str) > 0 {
str = "+" + str
}
return
}
func (c *Client) UserHost() Name {
username := "*"
if c.HasUsername() {
username = c.username.String()
}
return Name(fmt.Sprintf("%s!%s@%s", c.Nick(), username, c.hostname))
}
func (c *Client) Nick() Name {
if c.HasNick() {
return c.nick
}
return Name("*")
}
func (c *Client) Id() Name {
return c.UserHost()
}
func (c *Client) String() string {
return c.Id().String()
}
func (client *Client) Friends() ClientSet {
friends := make(ClientSet)
friends.Add(client)
for channel := range client.channels {
for member := range channel.members {
friends.Add(member)
}
}
return friends
}
func (client *Client) SetNickname(nickname Name) {
if client.HasNick() {
Log.error.Printf("%s nickname already set!", client)
return
}
client.nick = nickname
client.server.clients.Add(client)
}
func (client *Client) ChangeNickname(nickname Name) {
// Make reply before changing nick to capture original source id.
reply := RplNick(client, nickname)
client.server.clients.Remove(client)
client.server.whoWas.Append(client)
client.nick = nickname
client.server.clients.Add(client)
for friend := range client.Friends() {
friend.Reply(reply)
}
}
func (client *Client) Reply(reply string) error {
return client.socket.Write(reply)
}
func (client *Client) Quit(message Text) {
if client.hasQuit {
return
}
client.hasQuit = true
client.Reply(RplError("quit"))
client.server.whoWas.Append(client)
friends := client.Friends()
friends.Remove(client)
client.destroy()
if len(friends) > 0 {
reply := RplQuit(client, message)
for friend := range friends {
friend.Reply(reply)
}
}
}