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

120 lines
3.9 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 (
"crypto/rand"
"encoding/hex"
2016-11-16 03:02:22 +01:00
"fmt"
"strings"
2017-11-22 10:41:11 +01:00
"github.com/goshuirc/irc-go/ircfmt"
2019-05-07 05:17:57 +02:00
"github.com/oragono/oragono/irc/history"
2017-11-22 10:41:11 +01:00
"github.com/oragono/oragono/irc/sno"
2019-05-07 05:17:57 +02:00
"github.com/oragono/oragono/irc/utils"
)
var (
2019-01-04 16:19:13 +01:00
// anything added here MUST be casefolded:
restrictedNicknames = map[string]bool{
"=scene=": true, // used for rp commands
2019-01-04 16:19:13 +01:00
"histserv": true, // TODO(slingamn) this should become a real service
}
)
2018-02-27 03:44:03 +01:00
// returns whether the change succeeded or failed
func performNickChange(server *Server, client *Client, target *Client, session *Session, newnick string, rb *ResponseBuffer) bool {
nickname := strings.TrimSpace(newnick)
cfnick, err := CasefoldName(nickname)
2019-03-19 08:35:49 +01:00
currentNick := client.Nick()
if len(nickname) < 1 {
2019-03-19 08:35:49 +01:00
rb.Add(nil, server.name, ERR_NONICKNAMEGIVEN, currentNick, client.t("No nickname given"))
2016-06-20 02:04:53 +02:00
return false
}
if err != nil || len(nickname) > server.Limits().NickLen || restrictedNicknames[cfnick] {
2019-03-19 08:35:49 +01:00
rb.Add(nil, server.name, ERR_ERRONEUSNICKNAME, currentNick, nickname, client.t("Erroneous nickname"))
2016-06-20 02:04:53 +02:00
return false
}
if target.Nick() == nickname {
2018-02-27 03:44:03 +01:00
return true
}
2017-11-22 10:41:11 +01:00
hadNick := target.HasNick()
origNickMask := target.NickMaskString()
2019-05-07 05:17:57 +02:00
details := target.Details()
err = client.server.clients.SetNick(target, session, nickname)
2018-02-03 13:03:36 +01:00
if err == errNicknameInUse {
2019-03-19 08:35:49 +01:00
rb.Add(nil, server.name, ERR_NICKNAMEINUSE, currentNick, nickname, client.t("Nickname is already in use"))
return false
} else if err == errNicknameReserved {
2019-03-19 08:35:49 +01:00
rb.Add(nil, server.name, ERR_NICKNAMEINUSE, currentNick, nickname, client.t("Nickname is reserved by a different account"))
return false
2016-11-16 03:02:22 +01:00
} else if err != nil {
2019-03-19 08:35:49 +01:00
rb.Add(nil, server.name, ERR_UNKNOWNERROR, currentNick, "NICK", fmt.Sprintf(client.t("Could not set or change nickname: %s"), err.Error()))
2016-11-16 03:02:22 +01:00
return false
}
2017-11-22 10:41:11 +01:00
2019-05-07 05:17:57 +02:00
message := utils.MakeSplitMessage("", true)
histItem := history.Item{
Type: history.Nick,
Nick: origNickMask,
AccountName: details.accountName,
Message: message,
}
histItem.Params[0] = nickname
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-05-12 09:25:02 +02:00
if client == target {
target.server.snomasks.Send(sno.LocalNicks, fmt.Sprintf(ircfmt.Unescape("$%s$r changed nickname to %s"), details.nick, nickname))
} else {
target.server.snomasks.Send(sno.LocalNicks, fmt.Sprintf(ircfmt.Unescape("Operator %s changed nickname of $%s$r to %s"), client.Nick(), details.nick, nickname))
}
2019-05-07 05:17:57 +02:00
target.server.whoWas.Append(details.WhoWas)
rb.AddFromClient(message.Time, message.Msgid, origNickMask, details.accountName, nil, "NICK", nickname)
for session := range target.Friends() {
if session != rb.session {
2019-05-07 05:17:57 +02:00
session.sendFromClientInternal(false, message.Time, message.Msgid, origNickMask, details.accountName, nil, "NICK", nickname)
}
2017-11-22 10:41:11 +01:00
}
}
2019-05-07 05:17:57 +02:00
for _, channel := range client.Channels() {
channel.history.Add(histItem)
}
2019-04-15 00:13:01 +02:00
target.nickTimer.Touch(rb)
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)
}
2018-02-27 03:44:03 +01:00
// else: Run() will attempt registration immediately after this
return true
}
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))
sessions := client.Sessions()
if len(sessions) == 0 {
return
}
// XXX arbitrarily pick the first session to receive error messages;
// all other sessions receive a `NICK` line same as a friend would
rb := NewResponseBuffer(sessions[0])
performNickChange(server, client, client, nil, nick, rb)
2018-12-28 19:45:55 +01:00
rb.Send(false)
// technically performNickChange can fail to change the nick,
// but if they're still delinquent, the timer will get them later
}