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 (
|
2018-02-18 10:46:14 +01:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
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"
|
|
|
|
"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 (
|
2019-01-04 16:19:13 +01:00
|
|
|
// anything added here MUST be casefolded:
|
2017-03-11 13:01:40 +01:00
|
|
|
restrictedNicknames = map[string]bool{
|
2018-11-26 11:23:27 +01:00
|
|
|
"=scene=": true, // used for rp commands
|
2019-01-04 16:19:13 +01:00
|
|
|
"histserv": true, // TODO(slingamn) this should become a real service
|
2017-03-11 13:01:40 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-02-27 03:44:03 +01:00
|
|
|
// returns whether the change succeeded or failed
|
2018-02-05 15:21:08 +01:00
|
|
|
func performNickChange(server *Server, client *Client, target *Client, newnick string, rb *ResponseBuffer) 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-02-05 15:21:08 +01:00
|
|
|
rb.Add(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-02-05 15:21:08 +01:00
|
|
|
rb.Add(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 {
|
2018-02-27 03:44:03 +01:00
|
|
|
return true
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 10:41:11 +01:00
|
|
|
hadNick := target.HasNick()
|
|
|
|
origNickMask := target.NickMaskString()
|
2018-05-04 06:24:54 +02:00
|
|
|
whowas := client.WhoWas()
|
2017-11-22 10:41:11 +01:00
|
|
|
err = client.server.clients.SetNick(target, nickname)
|
2018-02-03 13:03:36 +01:00
|
|
|
if err == errNicknameInUse {
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(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
|
2018-02-11 11:30:40 +01:00
|
|
|
} else if err == errNicknameReserved {
|
2018-02-27 03:44:03 +01:00
|
|
|
rb.Add(nil, server.name, ERR_NICKNAMEINUSE, client.nick, nickname, client.t("Nickname is reserved by a different account"))
|
2018-02-11 11:30:40 +01:00
|
|
|
return false
|
2016-11-16 03:02:22 +01:00
|
|
|
} else if err != nil {
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(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
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
client.nickTimer.Touch()
|
|
|
|
|
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 {
|
2019-01-01 19:00:16 +01:00
|
|
|
target.server.snomasks.Send(sno.LocalNicks, fmt.Sprintf(ircfmt.Unescape("$%s$r changed nickname to %s"), whowas.nick, nickname))
|
2018-05-04 06:24:54 +02:00
|
|
|
target.server.whoWas.Append(whowas)
|
2019-02-21 04:20:23 +01:00
|
|
|
rb.Add(nil, origNickMask, "NICK", nickname)
|
2017-11-22 10:41:11 +01:00
|
|
|
for friend := range target.Friends() {
|
2019-02-21 04:20:23 +01:00
|
|
|
if friend != client {
|
|
|
|
friend.Send(nil, origNickMask, "NICK", nickname)
|
|
|
|
}
|
2017-11-22 10:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 03:44:03 +01:00
|
|
|
if target.Registered() {
|
2017-11-22 10:41:11 +01:00
|
|
|
client.server.monitorManager.AlertAbout(target, true)
|
2016-08-13 12:54:15 +02:00
|
|
|
}
|
2018-02-27 03:44:03 +01:00
|
|
|
// else: Run() will attempt registration immediately after this
|
|
|
|
return true
|
2014-03-17 20:11:35 +01:00
|
|
|
}
|
2018-02-18 10:46:14 +01:00
|
|
|
|
|
|
|
func (server *Server) RandomlyRename(client *Client) {
|
|
|
|
prefix := server.AccountConfig().NickReservation.RenamePrefix
|
|
|
|
if prefix == "" {
|
|
|
|
prefix = "Guest-"
|
|
|
|
}
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
rand.Read(buf)
|
|
|
|
nick := fmt.Sprintf("%s%s", prefix, hex.EncodeToString(buf))
|
|
|
|
rb := NewResponseBuffer(client)
|
|
|
|
performNickChange(server, client, client, nick, rb)
|
2018-12-28 19:45:55 +01:00
|
|
|
rb.Send(false)
|
2018-02-18 10:46:14 +01:00
|
|
|
// technically performNickChange can fail to change the nick,
|
|
|
|
// but if they're still delinquent, the timer will get them later
|
|
|
|
}
|