2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
|
|
|
// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
|
|
|
|
// released under the MIT license
|
|
|
|
|
2014-03-17 20:11:35 +01:00
|
|
|
package irc
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/DanielOaks/girc-go/ircmsg"
|
|
|
|
)
|
2016-06-19 02:01:30 +02:00
|
|
|
|
|
|
|
// NICK <nickname>
|
|
|
|
func nickHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
|
2014-03-17 20:11:35 +01:00
|
|
|
if !client.authorized {
|
2016-06-20 02:04:53 +02:00
|
|
|
client.Quit("Bad password")
|
|
|
|
return true
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2016-10-13 16:31:13 +02:00
|
|
|
nicknameRaw := strings.TrimSpace(msg.Params[0])
|
|
|
|
nickname, err := CasefoldName(nicknameRaw)
|
2014-03-17 20:11:35 +01:00
|
|
|
|
2016-10-13 16:31:13 +02:00
|
|
|
if len(nicknameRaw) < 1 {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, server.name, ERR_NONICKNAMEGIVEN, client.nick, "No nickname given")
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2016-10-13 16:31:13 +02:00
|
|
|
if err != nil || len(nicknameRaw) > server.limits.NickLen {
|
|
|
|
client.Send(nil, server.name, ERR_ERRONEUSNICKNAME, client.nick, nicknameRaw, "Erroneous nickname")
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 02:04:53 +02:00
|
|
|
if client.nick == nickname {
|
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 02:04:53 +02:00
|
|
|
//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 {
|
2016-10-13 16:31:13 +02:00
|
|
|
client.Send(nil, server.name, ERR_NICKNAMEINUSE, client.nick, nicknameRaw, "Nickname is already in use")
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2016-08-13 12:54:15 +02:00
|
|
|
if client.registered {
|
2016-10-13 16:31:13 +02:00
|
|
|
client.ChangeNickname(nicknameRaw)
|
2016-08-13 12:54:15 +02:00
|
|
|
} else {
|
2016-10-13 16:31:13 +02:00
|
|
|
client.SetNickname(nicknameRaw)
|
2016-08-13 12:54:15 +02:00
|
|
|
}
|
2016-06-20 02:04:53 +02:00
|
|
|
server.tryRegister(client)
|
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 02:04:53 +02:00
|
|
|
// SANICK <oldnick> <nickname>
|
|
|
|
func sanickHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
|
|
|
|
if !client.authorized {
|
|
|
|
client.Quit("Bad password")
|
|
|
|
return true
|
|
|
|
}
|
2014-03-17 20:11:35 +01:00
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
oldnick, oerr := CasefoldName(msg.Params[0])
|
|
|
|
casefoldedNickname, err := CasefoldName(msg.Params[1])
|
2014-03-17 20:11:35 +01:00
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
if len(casefoldedNickname) < 1 {
|
|
|
|
client.Send(nil, server.name, ERR_NONICKNAMEGIVEN, client.nick, "No nickname given")
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
if oerr != nil || err != nil || len(strings.TrimSpace(msg.Params[1])) > server.limits.NickLen {
|
|
|
|
client.Send(nil, server.name, ERR_ERRONEUSNICKNAME, client.nick, msg.Params[0], "Erroneous nickname")
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
if client.nick == msg.Params[1] {
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
2014-03-28 21:47:49 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 02:04:53 +02:00
|
|
|
target := server.clients.Get(oldnick)
|
2014-03-17 20:11:35 +01:00
|
|
|
if target == nil {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, server.name, ERR_NOSUCHNICK, msg.Params[0], "No such nick")
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 02:04:53 +02:00
|
|
|
//TODO(dan): There's probably some races here, we should be changing this in the primary server thread
|
2016-10-11 15:51:46 +02:00
|
|
|
if server.clients.Get(casefoldedNickname) != nil || server.clients.Get(casefoldedNickname) != target {
|
|
|
|
client.Send(nil, server.name, ERR_NICKNAMEINUSE, client.nick, msg.Params[0], "Nickname is already in use")
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
2014-03-28 21:47:49 +01:00
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
target.ChangeNickname(msg.Params[1])
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|