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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/goshuirc/irc-go/ircfmt"
  10. "github.com/oragono/oragono/irc/history"
  11. "github.com/oragono/oragono/irc/sno"
  12. "github.com/oragono/oragono/irc/utils"
  13. )
  14. var (
  15. restrictedNicknames = []string{
  16. "=scene=", // used for rp commands
  17. "HistServ", // used to play back JOIN, PART, etc. to legacy clients
  18. }
  19. restrictedCasefoldedNicks = make(map[string]bool)
  20. restrictedSkeletons = make(map[string]bool)
  21. )
  22. func performNickChange(server *Server, client *Client, target *Client, session *Session, nickname string, rb *ResponseBuffer) error {
  23. currentNick := client.Nick()
  24. details := target.Details()
  25. if details.nick == nickname {
  26. return nil
  27. }
  28. hadNick := details.nick != "*"
  29. origNickMask := details.nickMask
  30. assignedNickname, err := client.server.clients.SetNick(target, session, nickname)
  31. if err == errNicknameInUse {
  32. rb.Add(nil, server.name, ERR_NICKNAMEINUSE, currentNick, utils.SafeErrorParam(nickname), client.t("Nickname is already in use"))
  33. } else if err == errNicknameReserved {
  34. rb.Add(nil, server.name, ERR_NICKNAMEINUSE, currentNick, utils.SafeErrorParam(nickname), client.t("Nickname is reserved by a different account"))
  35. } else if err == errNicknameInvalid {
  36. rb.Add(nil, server.name, ERR_ERRONEUSNICKNAME, currentNick, utils.SafeErrorParam(nickname), client.t("Erroneous nickname"))
  37. } else if err == errNickAccountMismatch {
  38. // this used to use ERR_NICKNAMEINUSE, but it displayed poorly in some clients;
  39. // ERR_UNKNOWNERROR at least has a better chance of displaying our error text
  40. rb.Add(nil, server.name, ERR_UNKNOWNERROR, currentNick, "NICK", client.t("You must use your account name as your nickname"))
  41. } else if err == errNickMissing {
  42. rb.Add(nil, server.name, ERR_NONICKNAMEGIVEN, currentNick, client.t("No nickname given"))
  43. } else if err == errNoop {
  44. // no message
  45. } else if err != nil {
  46. rb.Add(nil, server.name, ERR_UNKNOWNERROR, currentNick, "NICK", fmt.Sprintf(client.t("Could not set or change nickname: %s"), err.Error()))
  47. }
  48. if err != nil {
  49. return err
  50. }
  51. message := utils.MakeMessage("")
  52. histItem := history.Item{
  53. Type: history.Nick,
  54. Nick: origNickMask,
  55. AccountName: details.accountName,
  56. Message: message,
  57. }
  58. histItem.Params[0] = assignedNickname
  59. client.server.logger.Debug("nick", fmt.Sprintf("%s changed nickname to %s [%s]", origNickMask, assignedNickname, client.NickCasefolded()))
  60. if hadNick {
  61. if client == target {
  62. target.server.snomasks.Send(sno.LocalNicks, fmt.Sprintf(ircfmt.Unescape("$%s$r changed nickname to %s"), details.nick, assignedNickname))
  63. } else {
  64. target.server.snomasks.Send(sno.LocalNicks, fmt.Sprintf(ircfmt.Unescape("Operator %s changed nickname of $%s$r to %s"), client.Nick(), details.nick, assignedNickname))
  65. }
  66. target.server.whoWas.Append(details.WhoWas)
  67. rb.AddFromClient(message.Time, message.Msgid, origNickMask, details.accountName, nil, "NICK", assignedNickname)
  68. for session := range target.Friends() {
  69. if session != rb.session {
  70. session.sendFromClientInternal(false, message.Time, message.Msgid, origNickMask, details.accountName, nil, "NICK", assignedNickname)
  71. }
  72. }
  73. }
  74. for _, channel := range client.Channels() {
  75. channel.AddHistoryItem(histItem, details.account)
  76. }
  77. if target.Registered() {
  78. client.server.monitorManager.AlertAbout(target, true)
  79. target.nickTimer.Touch(rb)
  80. } // else: these will be deferred to the end of registration (see #572)
  81. return nil
  82. }
  83. func (server *Server) RandomlyRename(client *Client) {
  84. format := server.Config().Accounts.NickReservation.GuestFormat
  85. buf := make([]byte, 8)
  86. rand.Read(buf)
  87. nick := strings.Replace(format, "*", utils.B32Encoder.EncodeToString(buf), -1)
  88. sessions := client.Sessions()
  89. if len(sessions) == 0 {
  90. // this can happen if they are anonymous and BRB (in general, an always-on
  91. // client has title to its nickname and will never be the victim of
  92. // a call to RandomlyRename)
  93. client.destroy(nil)
  94. return
  95. }
  96. // XXX arbitrarily pick the first session to receive error messages;
  97. // all other sessions receive a `NICK` line same as a friend would
  98. rb := NewResponseBuffer(sessions[0])
  99. performNickChange(server, client, client, nil, nick, rb)
  100. rb.Send(false)
  101. // technically performNickChange can fail to change the nick,
  102. // but if they're still delinquent, the timer will get them later
  103. }
  104. // if force-nick-equals-account is set, account name and nickname must be equal,
  105. // so we need to re-NICK automatically on every login event (IDENTIFY,
  106. // VERIFY, and a REGISTER that auto-verifies). if we can't get the nick
  107. // then we log them out (they will be able to reattach with SASL)
  108. func fixupNickEqualsAccount(client *Client, rb *ResponseBuffer, config *Config) (success bool) {
  109. if !config.Accounts.NickReservation.ForceNickEqualsAccount {
  110. return true
  111. }
  112. if !client.registered {
  113. return true
  114. }
  115. if performNickChange(client.server, client, client, rb.session, client.AccountName(), rb) != nil {
  116. client.server.accounts.Logout(client)
  117. nsNotice(rb, client.t("A client is already using that account; try logging out and logging back in with SASL"))
  118. return false
  119. }
  120. return true
  121. }