2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
|
2016-06-15 13:50:56 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
2014-03-17 20:11:35 +01:00
|
|
|
package irc
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
import (
|
2016-11-16 03:02:22 +01:00
|
|
|
"fmt"
|
2016-10-11 15:51:46 +02:00
|
|
|
"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"
|
2016-10-11 15:51:46 +02:00
|
|
|
)
|
2016-06-19 02:01:30 +02:00
|
|
|
|
2017-03-11 13:01:40 +01:00
|
|
|
var (
|
|
|
|
restrictedNicknames = map[string]bool{
|
|
|
|
"=scene=": true, // used for rp commands
|
|
|
|
"chanserv": true,
|
|
|
|
"nickserv": true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 {
|
2017-11-22 22:55:29 +01:00
|
|
|
nickname := strings.TrimSpace(newnick)
|
|
|
|
cfnick, err := CasefoldName(nickname)
|
2014-03-17 20:11:35 +01:00
|
|
|
|
2017-11-22 22:55:29 +01:00
|
|
|
if len(nickname) < 1 {
|
2018-01-22 12:26:01 +01:00
|
|
|
client.Send(nil, server.name, ERR_NONICKNAMEGIVEN, client.nick, client.t("No nickname given"))
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 22:55:29 +01:00
|
|
|
if err != nil || len(nickname) > server.Limits().NickLen || restrictedNicknames[cfnick] {
|
2018-01-22 12:26:01 +01:00
|
|
|
client.Send(nil, server.name, ERR_ERRONEUSNICKNAME, client.nick, nickname, client.t("Erroneous nickname"))
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 22:55:29 +01:00
|
|
|
if target.Nick() == nickname {
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-01-22 12:26:01 +01:00
|
|
|
client.Send(nil, server.name, ERR_NICKNAMEINUSE, client.nick, nickname, client.t("Nickname is already in use"))
|
2016-11-15 18:05:33 +01:00
|
|
|
return false
|
2016-11-16 03:02:22 +01:00
|
|
|
} else if err != nil {
|
2018-01-22 12:26:01 +01:00
|
|
|
client.Send(nil, server.name, ERR_UNKNOWNERROR, client.nick, "NICK", fmt.Sprintf(client.t("Could not set or change nickname: %s"), err.Error()))
|
2016-11-16 03:02:22 +01:00
|
|
|
return false
|
2016-11-15 18:05:33 +01:00
|
|
|
}
|
2017-11-22 10:41:11 +01:00
|
|
|
|
2017-11-22 22:55:29 +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 {
|
2017-11-22 22:55:29 +01:00
|
|
|
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() {
|
2017-11-22 22:55:29 +01:00
|
|
|
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-08-13 12:54:15 +02:00
|
|
|
}
|
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
|
|
|
// 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)
|
2014-03-17 20:11:35 +01:00
|
|
|
if target == nil {
|
2018-01-22 12:26:01 +01:00
|
|
|
client.Send(nil, server.name, ERR_NOSUCHNICK, client.nick, msg.Params[0], client.t("No such nick"))
|
2016-06-20 02:04:53 +02:00
|
|
|
return false
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
2017-11-22 10:41:11 +01:00
|
|
|
return performNickChange(server, client, target, msg.Params[1])
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|