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

91 lines
2.6 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-11-22 10:41:11 +01:00
"github.com/goshuirc/irc-go/ircfmt"
2017-06-15 18:14:19 +02:00
"github.com/goshuirc/irc-go/ircmsg"
2017-11-22 10:41:11 +01:00
"github.com/oragono/oragono/irc/sno"
)
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
}
2017-11-22 10:41:11 +01:00
return performNickChange(server, client, client, msg.Params[0])
}
func performNickChange(server *Server, client *Client, target *Client, newnick string) bool {
nickname := strings.TrimSpace(newnick)
cfnick, err := CasefoldName(nickname)
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 err != nil || len(nickname) > server.Limits().NickLen || restrictedNicknames[cfnick] {
client.Send(nil, server.name, ERR_ERRONEUSNICKNAME, client.nick, nickname, "Erroneous nickname")
2016-06-20 02:04:53 +02:00
return false
}
if target.Nick() == nickname {
2016-06-20 02:04:53 +02:00
return false
}
2017-11-22 10:41:11 +01:00
hadNick := target.HasNick()
origNick := target.Nick()
origNickMask := target.NickMaskString()
err = client.server.clients.SetNick(target, nickname)
2016-11-16 03:02:22 +01:00
if err == ErrNicknameInUse {
client.Send(nil, server.name, ERR_NICKNAMEINUSE, client.nick, nickname, "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
}
2017-11-22 10:41:11 +01:00
client.server.logger.Debug("nick", fmt.Sprintf("%s changed nickname to %s [%s]", origNickMask, nickname, cfnick))
2017-11-22 10:41:11 +01:00
if hadNick {
target.server.snomasks.Send(sno.LocalNicks, fmt.Sprintf(ircfmt.Unescape("$%s$r changed nickname to %s"), origNick, nickname))
2017-11-22 10:41:11 +01:00
target.server.whoWas.Append(client)
for friend := range target.Friends() {
friend.Send(nil, origNickMask, "NICK", nickname)
2017-11-22 10:41:11 +01:00
}
}
if target.registered {
client.server.monitorManager.AlertAbout(target, true)
} else {
server.tryRegister(target)
}
2016-06-20 02:04:53 +02:00
return false
}
2016-06-20 02:04:53 +02:00
// SANICK <oldnick> <nickname>
func sanickHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
2017-11-22 10:41:11 +01:00
targetNick := strings.TrimSpace(msg.Params[0])
target := server.clients.Get(targetNick)
if target == nil {
client.Send(nil, server.name, ERR_NOSUCHNICK, client.nick, msg.Params[0], "No such nick")
2016-06-20 02:04:53 +02:00
return false
}
2017-11-22 10:41:11 +01:00
return performNickChange(server, client, target, msg.Params[1])
}