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-06-19 02:01:30 +02:00
|
|
|
import "github.com/DanielOaks/girc-go/ircmsg"
|
|
|
|
|
|
|
|
// 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-06-20 02:04:53 +02:00
|
|
|
nickname := NewName(msg.Params[0])
|
2014-03-17 20:11:35 +01:00
|
|
|
|
2016-06-20 02:04:53 +02:00
|
|
|
if len(nickname) < 1 {
|
|
|
|
client.Send(nil, server.nameString, ERR_NONICKNAMEGIVEN, client.nickString, "No nickname given")
|
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 02:04:53 +02:00
|
|
|
if !nickname.IsNickname() {
|
|
|
|
client.Send(nil, server.nameString, ERR_ERRONEUSNICKNAME, client.nickString, msg.Params[0], "Erroneous nickname")
|
|
|
|
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 {
|
|
|
|
client.Send(nil, server.nameString, ERR_NICKNAMEINUSE, client.nickString, msg.Params[0], "Nickname is already in use")
|
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2016-08-13 12:54:15 +02:00
|
|
|
if client.registered {
|
|
|
|
client.ChangeNickname(nickname)
|
|
|
|
} else {
|
|
|
|
client.SetNickname(nickname)
|
|
|
|
}
|
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-06-20 02:04:53 +02:00
|
|
|
oldnick := NewName(msg.Params[0])
|
|
|
|
nickname := NewName(msg.Params[1])
|
2014-03-17 20:11:35 +01:00
|
|
|
|
2016-06-20 02:04:53 +02:00
|
|
|
if len(nickname) < 1 {
|
|
|
|
client.Send(nil, server.nameString, ERR_NONICKNAMEGIVEN, client.nickString, "No nickname given")
|
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 02:04:53 +02:00
|
|
|
if !nickname.IsNickname() {
|
|
|
|
client.Send(nil, server.nameString, ERR_ERRONEUSNICKNAME, client.nickString, msg.Params[0], "Erroneous nickname")
|
|
|
|
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-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-06-20 02:04:53 +02:00
|
|
|
client.Send(nil, server.nameString, ERR_NOSUCHNICK, msg.Params[0], "No such nick")
|
|
|
|
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
|
|
|
|
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
|
2014-03-28 21:47:49 +01:00
|
|
|
}
|
|
|
|
|
2016-08-13 12:54:15 +02:00
|
|
|
target.ChangeNickname(nickname)
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|