3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-25 05:19:25 +01:00
ergo/irc/nickname.go

105 lines
2.8 KiB
Go
Raw Normal View History

// Copyright (c) 2012-2014 Jeremy Latt
2017-03-27 14:15:02 +02:00
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import (
2016-11-16 03:02:22 +01:00
"fmt"
"strings"
2017-06-15 18:14:19 +02:00
"github.com/goshuirc/irc-go/ircmsg"
)
var (
restrictedNicknames = map[string]bool{
"=scene=": true, // used for rp commands
"chanserv": true,
"nickserv": true,
}
)
// NICK <nickname>
func nickHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
if !client.authorized {
2016-06-20 02:04:53 +02:00
client.Quit("Bad password")
return true
}
nicknameRaw := strings.TrimSpace(msg.Params[0])
nickname, err := CasefoldName(nicknameRaw)
if len(nicknameRaw) < 1 {
client.Send(nil, server.name, ERR_NONICKNAMEGIVEN, client.nick, "No nickname given")
2016-06-20 02:04:53 +02:00
return false
}
if err != nil || len(nicknameRaw) > server.limits.NickLen || restrictedNicknames[nickname] {
client.Send(nil, server.name, ERR_ERRONEUSNICKNAME, client.nick, nicknameRaw, "Erroneous nickname")
2016-06-20 02:04:53 +02:00
return false
}
2016-06-20 02:04:53 +02:00
if client.nick == nickname {
return false
}
// bleh, this will be replaced and done below
if client.registered {
err = client.ChangeNickname(nicknameRaw)
} else {
err = client.SetNickname(nicknameRaw)
}
2016-11-16 03:02:22 +01:00
if err == ErrNicknameInUse {
client.Send(nil, server.name, ERR_NICKNAMEINUSE, client.nick, nicknameRaw, "Nickname is already in use")
return false
2016-11-16 03:02:22 +01:00
} else if err != nil {
client.Send(nil, server.name, ERR_UNKNOWNERROR, client.nick, "NICK", fmt.Sprintf("Could not set or change nickname: %s", err.Error()))
return false
}
if client.registered {
client.alertMonitors()
}
2016-06-20 02:04:53 +02:00
server.tryRegister(client)
return false
}
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
}
oldnick, oerr := CasefoldName(msg.Params[0])
2016-11-01 14:56:21 +01:00
nickname, err := CasefoldName(msg.Params[1])
2016-11-01 14:56:21 +01:00
if len(nickname) < 1 {
client.Send(nil, server.name, ERR_NONICKNAMEGIVEN, client.nick, "No nickname given")
2016-06-20 02:04:53 +02:00
return false
}
if oerr != nil || err != nil || len(strings.TrimSpace(msg.Params[1])) > server.limits.NickLen || restrictedNicknames[nickname] {
client.Send(nil, server.name, ERR_ERRONEUSNICKNAME, client.nick, msg.Params[0], "Erroneous nickname")
2016-06-20 02:04:53 +02:00
return false
}
if client.nick == msg.Params[1] {
2016-06-20 02:04:53 +02:00
return false
}
2016-06-20 02:04:53 +02:00
target := server.clients.Get(oldnick)
if target == nil {
client.Send(nil, server.name, ERR_NOSUCHNICK, msg.Params[0], "No such nick")
2016-06-20 02:04:53 +02:00
return false
}
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
2017-06-18 16:23:51 +02:00
if server.clients.Get(nickname) != nil && server.clients.Get(nickname) != 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
}
target.ChangeNickname(msg.Params[1])
2016-06-20 02:04:53 +02:00
return false
}