forked from ergochat/ergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nickname.go
86 lines (69 loc) · 2.28 KB
/
nickname.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
// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import "github.com/DanielOaks/girc-go/ircmsg"
// NICK <nickname>
func nickHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
if !client.authorized {
client.Quit("Bad password")
return true
}
nickname := NewName(msg.Params[0])
if len(nickname) < 1 {
client.Send(nil, server.nameString, ERR_NONICKNAMEGIVEN, client.nickString, "No nickname given")
return false
}
if !nickname.IsNickname() {
client.Send(nil, server.nameString, ERR_ERRONEUSNICKNAME, client.nickString, msg.Params[0], "Erroneous nickname")
return false
}
if client.nick == nickname {
return false
}
//TODO(dan): There's probably some races here, we should be changing this in the primary server thread
target := server.clients.Get(nickname)
if target != nil && target != client {
client.Send(nil, server.nameString, ERR_NICKNAMEINUSE, client.nickString, msg.Params[0], "Nickname is already in use")
return false
}
if client.registered {
client.ChangeNickname(nickname)
} else {
client.SetNickname(nickname)
}
server.tryRegister(client)
return false
}
// SANICK <oldnick> <nickname>
func sanickHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
if !client.authorized {
client.Quit("Bad password")
return true
}
oldnick := NewName(msg.Params[0])
nickname := NewName(msg.Params[1])
if len(nickname) < 1 {
client.Send(nil, server.nameString, ERR_NONICKNAMEGIVEN, client.nickString, "No nickname given")
return false
}
if !nickname.IsNickname() {
client.Send(nil, server.nameString, ERR_ERRONEUSNICKNAME, client.nickString, msg.Params[0], "Erroneous nickname")
return false
}
if client.nick == nickname {
return false
}
target := server.clients.Get(oldnick)
if target == nil {
client.Send(nil, server.nameString, ERR_NOSUCHNICK, msg.Params[0], "No such nick")
return false
}
//TODO(dan): There's probably some races here, we should be changing this in the primary server thread
if server.clients.Get(nickname) != nil {
client.Send(nil, server.nameString, ERR_NICKNAMEINUSE, client.nickString, msg.Params[0], "Nickname is already in use")
return false
}
target.ChangeNickname(nickname)
return false
}