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"
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"
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"
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-05-24 19:09:56 +02:00
restrictedNicknames = [ ] string {
"=scene=" , // used for rp commands
"HistServ" , // used to play back JOIN, PART, etc. to legacy clients
2017-03-11 13:01:40 +01:00
}
2019-05-24 19:09:56 +02:00
restrictedCasefoldedNicks = make ( map [ string ] bool )
restrictedSkeletons = make ( map [ string ] bool )
2017-03-11 13:01:40 +01:00
)
2020-04-23 07:38:12 +02:00
func performNickChange ( server * Server , client * Client , target * Client , session * Session , nickname string , rb * ResponseBuffer ) error {
2019-03-19 08:35:49 +01:00
currentNick := client . Nick ( )
2020-03-16 12:54:50 +01:00
details := target . Details ( )
if details . nick == nickname {
2020-04-23 07:38:12 +02:00
return nil
2014-03-17 20:11:35 +01:00
}
2020-03-16 12:54:50 +01:00
hadNick := details . nick != "*"
origNickMask := details . nickMask
2014-03-17 20:11:35 +01:00
2020-02-19 01:38:42 +01:00
assignedNickname , err := client . server . clients . SetNick ( target , session , nickname )
2018-02-03 13:03:36 +01:00
if err == errNicknameInUse {
2020-03-17 04:37:52 +01:00
rb . Add ( nil , server . name , ERR_NICKNAMEINUSE , currentNick , utils . SafeErrorParam ( nickname ) , client . t ( "Nickname is already in use" ) )
2018-02-11 11:30:40 +01:00
} else if err == errNicknameReserved {
2020-03-17 04:37:52 +01:00
rb . Add ( nil , server . name , ERR_NICKNAMEINUSE , currentNick , utils . SafeErrorParam ( nickname ) , client . t ( "Nickname is reserved by a different account" ) )
2019-05-24 19:09:56 +02:00
} else if err == errNicknameInvalid {
2019-12-05 12:52:07 +01:00
rb . Add ( nil , server . name , ERR_ERRONEUSNICKNAME , currentNick , utils . SafeErrorParam ( nickname ) , client . t ( "Erroneous nickname" ) )
2020-03-16 12:54:50 +01:00
} else if err == errNickAccountMismatch {
// this used to use ERR_NICKNAMEINUSE, but it displayed poorly in some clients;
// ERR_UNKNOWNERROR at least has a better chance of displaying our error text
2020-03-26 21:22:23 +01:00
rb . Add ( nil , server . name , ERR_UNKNOWNERROR , currentNick , "NICK" , client . t ( "You must use your account name as your nickname" ) )
2020-03-16 12:54:50 +01:00
} else if err == errNickMissing {
rb . Add ( nil , server . name , ERR_NONICKNAMEGIVEN , currentNick , client . t ( "No nickname given" ) )
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 ( ) ) )
2019-05-24 19:09:56 +02:00
}
if err != nil {
2020-04-23 07:38:12 +02:00
return err
2016-11-15 18:05:33 +01:00
}
2017-11-22 10:41:11 +01:00
2020-01-19 05:47:05 +01:00
message := utils . MakeMessage ( "" )
2019-05-07 05:17:57 +02:00
histItem := history . Item {
Type : history . Nick ,
Nick : origNickMask ,
AccountName : details . accountName ,
Message : message ,
}
2020-02-19 01:38:42 +01:00
histItem . Params [ 0 ] = assignedNickname
2019-05-07 05:17:57 +02:00
2020-02-19 01:38:42 +01:00
client . server . logger . Debug ( "nick" , fmt . Sprintf ( "%s changed nickname to %s [%s]" , origNickMask , assignedNickname , client . NickCasefolded ( ) ) )
2017-11-22 10:41:11 +01:00
if hadNick {
2019-05-12 09:25:02 +02:00
if client == target {
2020-02-19 01:38:42 +01:00
target . server . snomasks . Send ( sno . LocalNicks , fmt . Sprintf ( ircfmt . Unescape ( "$%s$r changed nickname to %s" ) , details . nick , assignedNickname ) )
2019-05-12 09:25:02 +02:00
} else {
2020-02-19 01:38:42 +01:00
target . server . snomasks . Send ( sno . LocalNicks , fmt . Sprintf ( ircfmt . Unescape ( "Operator %s changed nickname of $%s$r to %s" ) , client . Nick ( ) , details . nick , assignedNickname ) )
2019-05-12 09:25:02 +02:00
}
2019-05-07 05:17:57 +02:00
target . server . whoWas . Append ( details . WhoWas )
2020-02-19 01:38:42 +01:00
rb . AddFromClient ( message . Time , message . Msgid , origNickMask , details . accountName , nil , "NICK" , assignedNickname )
2019-04-12 06:08:46 +02:00
for session := range target . Friends ( ) {
if session != rb . session {
2020-02-19 01:38:42 +01:00
session . sendFromClientInternal ( false , message . Time , message . Msgid , origNickMask , details . accountName , nil , "NICK" , assignedNickname )
2019-02-21 04:20:23 +01:00
}
2017-11-22 10:41:11 +01:00
}
}
2019-05-07 05:17:57 +02:00
for _ , channel := range client . Channels ( ) {
2020-05-12 18:05:40 +02:00
channel . AddHistoryItem ( histItem , details . account )
2019-05-07 05:17:57 +02: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 )
2019-07-09 02:51:14 +02:00
target . nickTimer . Touch ( rb )
} // else: these will be deferred to the end of registration (see #572)
2020-04-23 07:38:12 +02:00
return nil
2014-03-17 20:11:35 +01:00
}
2018-02-18 10:46:14 +01:00
func ( server * Server ) RandomlyRename ( client * Client ) {
2020-03-16 12:54:50 +01:00
format := server . Config ( ) . Accounts . NickReservation . GuestFormat
2018-02-18 10:46:14 +01:00
buf := make ( [ ] byte , 8 )
rand . Read ( buf )
2020-03-16 12:54:50 +01:00
nick := strings . Replace ( format , "*" , utils . B32Encoder . EncodeToString ( buf ) , - 1 )
2019-04-12 06:08:46 +02:00
sessions := client . Sessions ( )
if len ( sessions ) == 0 {
2020-04-06 06:06:06 +02:00
// this can happen if they are anonymous and BRB (in general, an always-on
// client has title to its nickname and will never be the victim of
// a call to RandomlyRename)
client . destroy ( nil )
2019-04-12 06:08:46 +02:00
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 )
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
}
2020-03-17 04:37:52 +01:00
// if force-nick-equals-account is set, account name and nickname must be equal,
// so we need to re-NICK automatically on every login event (IDENTIFY,
// VERIFY, and a REGISTER that auto-verifies). if we can't get the nick
// then we log them out (they will be able to reattach with SASL)
func fixupNickEqualsAccount ( client * Client , rb * ResponseBuffer , config * Config ) ( success bool ) {
if ! config . Accounts . NickReservation . ForceNickEqualsAccount {
return true
}
if ! client . registered {
return true
}
2020-04-23 07:38:12 +02:00
if performNickChange ( client . server , client , client , rb . session , client . AccountName ( ) , rb ) != nil {
2020-03-17 04:37:52 +01:00
client . server . accounts . Logout ( client )
nsNotice ( rb , client . t ( "A client is already using that account; try logging out and logging back in with SASL" ) )
return false
}
return true
}