You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nickname.go 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
  3. // released under the MIT license
  4. package irc
  5. import (
  6. "crypto/rand"
  7. "fmt"
  8. "strings"
  9. "github.com/ergochat/ergo/irc/history"
  10. "github.com/ergochat/ergo/irc/modes"
  11. "github.com/ergochat/ergo/irc/sno"
  12. "github.com/ergochat/ergo/irc/utils"
  13. "github.com/ergochat/irc-go/ircfmt"
  14. )
  15. var (
  16. restrictedNicknames = []string{
  17. "=scene=", // used for rp commands
  18. "Global", // global announcements on some networks
  19. // common services not implemented by us:
  20. "MemoServ", "BotServ", "OperServ",
  21. }
  22. restrictedCasefoldedNicks = make(utils.HashSet[string])
  23. restrictedSkeletons = make(utils.HashSet[string])
  24. )
  25. func performNickChange(server *Server, client *Client, target *Client, session *Session, nickname string, rb *ResponseBuffer) error {
  26. details := target.Details()
  27. hadNick := details.nick != "*"
  28. origNickMask := details.nickMask
  29. isSanick := client != target
  30. assignedNickname, err, awayChanged := client.server.clients.SetNick(target, session, nickname, false)
  31. if err == errNicknameInUse {
  32. if !isSanick {
  33. rb.Add(nil, server.name, ERR_NICKNAMEINUSE, details.nick, utils.SafeErrorParam(nickname), client.t("Nickname is already in use"))
  34. } else {
  35. rb.Add(nil, server.name, "FAIL", "SANICK", "NICKNAME_IN_USE", utils.SafeErrorParam(nickname), client.t("Nickname is already in use"))
  36. }
  37. } else if err == errNicknameReserved {
  38. if !isSanick {
  39. // see #1594 for context: ERR_NICKNAMEINUSE can confuse clients if the nickname is not
  40. // literally in use:
  41. if !client.registered {
  42. rb.Add(nil, server.name, ERR_NICKNAMEINUSE, details.nick, utils.SafeErrorParam(nickname), client.t("Nickname is reserved by a different account"))
  43. }
  44. rb.Add(nil, server.name, "FAIL", "NICK", "NICKNAME_RESERVED", utils.SafeErrorParam(nickname), client.t("Nickname is reserved by a different account"))
  45. } else {
  46. rb.Add(nil, server.name, "FAIL", "SANICK", "NICKNAME_RESERVED", utils.SafeErrorParam(nickname), client.t("Nickname is reserved by a different account"))
  47. }
  48. } else if err == errNicknameInvalid {
  49. if !isSanick {
  50. rb.Add(nil, server.name, ERR_ERRONEUSNICKNAME, details.nick, utils.SafeErrorParam(nickname), client.t("Erroneous nickname"))
  51. } else {
  52. rb.Add(nil, server.name, "FAIL", "SANICK", "NICKNAME_INVALID", utils.SafeErrorParam(nickname), client.t("Erroneous nickname"))
  53. }
  54. } else if err == errNickAccountMismatch {
  55. // this used to use ERR_NICKNAMEINUSE, but it displayed poorly in some clients;
  56. // ERR_UNKNOWNERROR at least has a better chance of displaying our error text
  57. if !isSanick {
  58. rb.Add(nil, server.name, ERR_UNKNOWNERROR, details.nick, "NICK", client.t("You must use your account name as your nickname"))
  59. } else {
  60. rb.Add(nil, server.name, "FAIL", "SANICK", "UNKNOWN_ERROR", utils.SafeErrorParam(nickname), client.t("This user's nickname and account name need to be equal"))
  61. }
  62. } else if err == errNickMissing {
  63. if !isSanick {
  64. rb.Add(nil, server.name, ERR_NONICKNAMEGIVEN, details.nick, client.t("No nickname given"))
  65. } else {
  66. rb.Add(nil, server.name, "FAIL", "SANICK", "NICKNAME_INVALID", utils.SafeErrorParam(nickname), client.t("No nickname given"))
  67. }
  68. } else if err == errNoop {
  69. if !isSanick {
  70. // no message
  71. } else {
  72. rb.Add(nil, server.name, "NOTE", "SANICK", "NOOP", utils.SafeErrorParam(nickname), client.t("Client already had the desired nickname"))
  73. }
  74. } else if err != nil {
  75. client.server.logger.Error("internal", "couldn't change nick", nickname, err.Error())
  76. if !isSanick {
  77. rb.Add(nil, server.name, ERR_UNKNOWNERROR, details.nick, "NICK", client.t("Could not set or change nickname"))
  78. } else {
  79. rb.Add(nil, server.name, "FAIL", "SANICK", "UNKNOWN_ERROR", utils.SafeErrorParam(nickname), client.t("Could not set or change nickname"))
  80. }
  81. }
  82. if err != nil {
  83. return err
  84. }
  85. isBot := !isSanick && client.HasMode(modes.Bot)
  86. message := utils.MakeMessage("")
  87. histItem := history.Item{
  88. Type: history.Nick,
  89. Nick: origNickMask,
  90. AccountName: details.accountName,
  91. Message: message,
  92. IsBot: isBot,
  93. }
  94. histItem.Params[0] = assignedNickname
  95. client.server.logger.Debug("nick", fmt.Sprintf("%s changed nickname to %s [%s]", origNickMask, assignedNickname, client.NickCasefolded()))
  96. if hadNick {
  97. if client == target {
  98. target.server.snomasks.Send(sno.LocalNicks, fmt.Sprintf(ircfmt.Unescape("$%s$r changed nickname to %s"), details.nick, assignedNickname))
  99. } else {
  100. target.server.snomasks.Send(sno.LocalNicks, fmt.Sprintf(ircfmt.Unescape("Operator %s changed nickname of $%s$r to %s"), client.Nick(), details.nick, assignedNickname))
  101. }
  102. target.server.whoWas.Append(details.WhoWas)
  103. rb.AddFromClient(message.Time, message.Msgid, origNickMask, details.accountName, isBot, nil, "NICK", assignedNickname)
  104. for session := range target.Friends() {
  105. if session != rb.session {
  106. session.sendFromClientInternal(false, message.Time, message.Msgid, origNickMask, details.accountName, isBot, nil, "NICK", assignedNickname)
  107. }
  108. }
  109. }
  110. if awayChanged {
  111. dispatchAwayNotify(session.client, session.client.AwayMessage())
  112. }
  113. for _, channel := range target.Channels() {
  114. if channel.memberIsVisible(client) {
  115. channel.AddHistoryItem(histItem, details.account)
  116. }
  117. }
  118. newCfnick := target.NickCasefolded()
  119. if newCfnick != details.nickCasefolded {
  120. client.server.monitorManager.AlertAbout(details.nick, details.nickCasefolded, false)
  121. client.server.monitorManager.AlertAbout(assignedNickname, newCfnick, true)
  122. }
  123. return nil
  124. }
  125. func (server *Server) RandomlyRename(client *Client) {
  126. format := server.Config().Accounts.NickReservation.GuestFormat
  127. buf := make([]byte, 8)
  128. rand.Read(buf)
  129. nick := strings.Replace(format, "*", utils.B32Encoder.EncodeToString(buf), -1)
  130. sessions := client.Sessions()
  131. if len(sessions) == 0 {
  132. // this can happen if they are anonymous and BRB (in general, an always-on
  133. // client has title to its nickname and will never be the victim of
  134. // a call to RandomlyRename)
  135. client.destroy(nil)
  136. return
  137. }
  138. // XXX arbitrarily pick the first session to receive error messages;
  139. // all other sessions receive a `NICK` line same as a friend would
  140. rb := NewResponseBuffer(sessions[0])
  141. performNickChange(server, client, client, nil, nick, rb)
  142. rb.Send(false)
  143. // technically performNickChange can fail to change the nick,
  144. // but if they're still delinquent, the timer will get them later
  145. }
  146. // if force-nick-equals-account is set, account name and nickname must be equal,
  147. // so we need to re-NICK automatically on every login event (IDENTIFY,
  148. // VERIFY, and a REGISTER that auto-verifies). if we can't get the nick
  149. // then we log them out (they will be able to reattach with SASL)
  150. func fixupNickEqualsAccount(client *Client, rb *ResponseBuffer, config *Config, source string) (success bool) {
  151. if !config.Accounts.NickReservation.ForceNickEqualsAccount {
  152. return true
  153. }
  154. if !client.registered {
  155. return true
  156. }
  157. err := performNickChange(client.server, client, client, rb.session, client.AccountName(), rb)
  158. if err != nil && err != errNoop {
  159. client.server.accounts.Logout(client)
  160. if source == "" {
  161. source = client.server.name
  162. }
  163. rb.Add(nil, source, "NOTICE", client.t("A client is already using that account; try logging out and logging back in with SASL"))
  164. return false
  165. }
  166. return true
  167. }