2016-06-15 21:50:56 +10:00
// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2014-2015 Edmund Huber
2017-03-27 22:15:02 +10:00
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
2016-06-15 21:50:56 +10:00
// released under the MIT license
2012-04-07 11:44:59 -07:00
package irc
import (
2012-04-17 22:11:35 -07:00
"fmt"
2012-04-07 11:44:59 -07:00
"net"
2016-10-16 20:35:50 +10:00
"runtime/debug"
2016-06-30 15:35:34 +10:00
"strconv"
2017-01-20 23:51:36 +10:00
"strings"
2017-04-18 22:26:01 +10:00
"sync"
2017-10-22 19:50:16 -04:00
"sync/atomic"
2012-12-11 23:12:35 -08:00
"time"
2016-06-17 22:17:42 +10:00
2017-06-15 10:14:19 -06:00
"github.com/goshuirc/irc-go/ircfmt"
"github.com/goshuirc/irc-go/ircmsg"
2017-06-14 12:00:53 -06:00
ident "github.com/oragono/go-ident"
2017-09-29 12:07:52 +10:00
"github.com/oragono/oragono/irc/caps"
2019-01-01 16:45:37 -05:00
"github.com/oragono/oragono/irc/connection_limits"
2018-11-26 05:23:27 -05:00
"github.com/oragono/oragono/irc/history"
2018-02-03 20:21:32 +10:00
"github.com/oragono/oragono/irc/modes"
2017-06-14 12:00:53 -06:00
"github.com/oragono/oragono/irc/sno"
2017-10-05 23:47:43 +10:00
"github.com/oragono/oragono/irc/utils"
2012-04-07 11:44:59 -07:00
)
2014-03-12 17:52:25 -07:00
const (
2017-06-19 14:53:16 -06:00
// IdentTimeoutSeconds is how many seconds before our ident (username) check times out.
2018-11-26 05:23:27 -05:00
IdentTimeoutSeconds = 1.5
2018-12-30 14:55:11 -05:00
IRCv3TimestampFormat = "2006-01-02T15:04:05.000Z"
2016-06-30 15:35:34 +10:00
)
2018-11-26 05:23:27 -05:00
// ResumeDetails is a place to stash data at various stages of
// the resume process: when handling the RESUME command itself,
// when completing the registration, and when rejoining channels.
type ResumeDetails struct {
OldClient * Client
PresentedToken string
Timestamp time . Time
ResumedAt time . Time
Channels [ ] string
HistoryIncomplete bool
}
2016-10-23 11:48:57 +10:00
// Client is an IRC client.
2012-04-07 11:44:59 -07:00
type Client struct {
2018-02-11 05:30:40 -05:00
account string
2019-01-01 13:00:16 -05:00
accountName string // display name of the account: uncasefolded, '*' if not logged in
2019-05-19 04:27:44 -04:00
accountSettings AccountSettings
2016-10-11 23:51:46 +10:00
atime time . Time
2019-04-28 15:10:03 -04:00
away bool
2016-10-11 23:51:46 +10:00
awayMessage string
certfp string
channels ChannelSet
ctime time . Time
2017-06-11 10:01:39 -06:00
exitedSnomaskSent bool
2019-03-11 19:24:45 -04:00
flags modes . ModeSet
2016-10-11 23:51:46 +10:00
hostname string
2018-12-23 13:25:02 -05:00
invitedTo map [ string ] bool
2019-02-25 21:50:43 -05:00
isTor bool
2018-01-21 16:49:17 +10:00
languages [ ] string
2019-01-01 16:45:37 -05:00
loginThrottle connection_limits . GenericThrottle
2016-10-11 23:51:46 +10:00
nick string
nickCasefolded string
nickMaskCasefolded string
2017-04-18 22:26:01 +10:00
nickMaskString string // cache for nickmask string since it's used with lots of replies
2019-03-08 03:12:21 -05:00
nickTimer NickTimer
2018-04-19 02:48:19 -04:00
oper * Oper
2018-02-26 21:44:03 -05:00
preregNick string
2018-02-01 15:53:49 -05:00
proxiedIP net . IP // actual remote IP if using the PROXY protocol
2017-04-18 22:26:01 +10:00
rawHostname string
2019-05-12 02:17:57 -04:00
cloakedHostname string
2016-10-11 23:51:46 +10:00
realname string
2019-02-05 00:19:03 -05:00
realIP net . IP
2016-10-11 23:51:46 +10:00
registered bool
2018-01-21 11:23:33 +10:00
resumeDetails * ResumeDetails
2019-02-12 00:27:57 -05:00
resumeID string
2016-10-11 23:51:46 +10:00
saslInProgress bool
saslMechanism string
saslValue string
2019-02-05 00:19:03 -05:00
sentPassCommand bool
2016-10-11 23:51:46 +10:00
server * Server
2019-01-30 18:59:49 -05:00
skeleton string
2019-04-12 00:08:46 -04:00
sessions [ ] * Session
2017-11-22 04:41:11 -05:00
stateMutex sync . RWMutex // tier 1
2016-10-11 23:51:46 +10:00
username string
2017-04-18 22:26:01 +10:00
vhost string
2018-11-26 05:23:27 -05:00
history * history . Buffer
2012-12-16 19:13:53 -08:00
}
2019-04-12 00:08:46 -04:00
// Session is an individual client connection to the server (TCP connection
// and associated per-connection data, such as capabilities). There is a
// many-one relationship between sessions and clients.
type Session struct {
client * Client
2019-05-08 04:11:54 -04:00
ctime time . Time
atime time . Time
socket * Socket
realIP net . IP
proxiedIP net . IP
rawHostname string
2019-04-12 00:08:46 -04:00
idletimer IdleTimer
fakelag Fakelag
quitMessage string
capabilities caps . Set
maxlenRest uint32
capState caps . State
capVersion caps . Version
}
// sets the session quit message, if there isn't one already
func ( sd * Session ) SetQuitMessage ( message string ) ( set bool ) {
if message == "" {
if sd . quitMessage == "" {
sd . quitMessage = "Connection closed"
return true
} else {
return false
}
} else {
sd . quitMessage = message
return true
}
}
// set the negotiated message length based on session capabilities
func ( session * Session ) SetMaxlenRest ( ) {
maxlenRest := 512
if session . capabilities . Has ( caps . MaxLine ) {
maxlenRest = session . client . server . Config ( ) . Limits . LineLen . Rest
}
atomic . StoreUint32 ( & session . maxlenRest , uint32 ( maxlenRest ) )
}
// allow the negotiated message length limit to be read without locks; this is a convenience
// so that Session.SendRawMessage doesn't have to acquire any Client locks
func ( session * Session ) MaxlenRest ( ) int {
return int ( atomic . LoadUint32 ( & session . maxlenRest ) )
}
2019-01-01 13:00:16 -05:00
// WhoWas is the subset of client details needed to answer a WHOWAS query
type WhoWas struct {
nick string
nickCasefolded string
username string
hostname string
realname string
}
// ClientDetails is a standard set of details about a client
type ClientDetails struct {
WhoWas
nickMask string
nickMaskCasefolded string
account string
accountName string
}
2019-05-12 20:57:34 -04:00
// RunClient sets up a new client and runs its goroutine.
func ( server * Server ) RunClient ( conn clientConn ) {
var isBanned bool
var banMsg string
var realIP net . IP
if conn . IsTor {
realIP = utils . IPv4LoopbackAddress
isBanned , banMsg = server . checkTorLimits ( )
} else {
realIP = utils . AddrToIP ( conn . Conn . RemoteAddr ( ) )
isBanned , banMsg = server . checkBans ( realIP )
}
if isBanned {
// this might not show up properly on some clients,
// but our objective here is just to close the connection out before it has a load impact on us
conn . Conn . Write ( [ ] byte ( fmt . Sprintf ( errorMsg , banMsg ) ) )
conn . Conn . Close ( )
return
}
server . logger . Info ( "localconnect-ip" , fmt . Sprintf ( "Client connecting from %v" , realIP ) )
2019-05-12 03:12:50 -04:00
now := time . Now ( ) . UTC ( )
2018-07-16 03:46:40 -04:00
config := server . Config ( )
2019-03-07 02:31:46 -05:00
fullLineLenLimit := ircmsg . MaxlenTagsFromClient + config . Limits . LineLen . Rest
// give them 1k of grace over the limit:
socket := NewSocket ( conn . Conn , fullLineLenLimit + 1024 , config . Server . MaxSendQBytes )
2012-12-09 12:51:50 -08:00
client := & Client {
2019-04-12 00:08:46 -04:00
atime : now ,
channels : make ( ChannelSet ) ,
ctime : now ,
isTor : conn . IsTor ,
languages : server . Languages ( ) . Default ( ) ,
2019-01-01 16:45:37 -05:00
loginThrottle : connection_limits . GenericThrottle {
Duration : config . Accounts . LoginThrottling . Duration ,
Limit : config . Accounts . LoginThrottling . MaxAttempts ,
} ,
2016-09-19 22:30:29 +10:00
server : server ,
2019-01-01 13:00:16 -05:00
accountName : "*" ,
2016-10-11 23:51:46 +10:00
nick : "*" , // * is used until actual nick is given
nickCasefolded : "*" ,
2016-09-19 22:30:29 +10:00
nickMaskString : "*" , // * is used until actual nick is given
2018-11-26 05:23:27 -05:00
history : history . NewHistoryBuffer ( config . History . ClientLength ) ,
2012-12-09 12:51:50 -08:00
}
2019-04-12 00:08:46 -04:00
session := & Session {
client : client ,
socket : socket ,
capVersion : caps . Cap301 ,
capState : caps . NoneState ,
2019-05-08 04:11:54 -04:00
ctime : now ,
atime : now ,
2019-05-12 20:57:34 -04:00
realIP : realIP ,
2019-04-12 00:08:46 -04:00
}
session . SetMaxlenRest ( )
client . sessions = [ ] * Session { session }
2016-09-07 21:32:58 +10:00
2019-02-25 21:50:43 -05:00
if conn . IsTLS {
client . SetMode ( modes . TLS , true )
2016-09-07 21:32:58 +10:00
// error is not useful to us here anyways so we can ignore it
2019-04-12 00:08:46 -04:00
client . certfp , _ = socket . CertFP ( )
2016-06-29 01:09:07 +10:00
}
2019-02-25 21:50:43 -05:00
if conn . IsTor {
client . SetMode ( modes . TLS , true )
2019-05-08 04:11:54 -04:00
// cover up details of the tor proxying infrastructure (not a user privacy concern,
// but a hardening measure):
session . proxiedIP = utils . IPv4LoopbackAddress
session . rawHostname = config . Server . TorListeners . Vhost
2019-02-25 21:50:43 -05:00
} else {
2019-05-08 04:11:54 -04:00
// set the hostname for this client (may be overridden later by PROXY or WEBIRC)
session . rawHostname = utils . LookupHostname ( session . realIP . String ( ) )
2019-05-12 02:17:57 -04:00
client . cloakedHostname = config . Server . Cloaks . ComputeCloak ( session . realIP )
2019-05-12 20:57:34 -04:00
remoteAddr := conn . Conn . RemoteAddr ( )
2019-05-08 04:11:54 -04:00
if utils . AddrIsLocal ( remoteAddr ) {
// treat local connections as secure (may be overridden later by WEBIRC)
client . SetMode ( modes . TLS , true )
}
2019-02-25 21:50:43 -05:00
if config . Server . CheckIdent && ! utils . AddrIsUnix ( remoteAddr ) {
client . doIdentLookup ( conn . Conn )
2016-06-30 19:28:34 +10:00
}
2019-02-25 21:50:43 -05:00
}
2019-05-08 04:11:54 -04:00
client . realIP = session . realIP
client . rawHostname = session . rawHostname
client . proxiedIP = session . proxiedIP
2019-02-25 21:50:43 -05:00
2019-04-12 00:08:46 -04:00
client . run ( session )
2019-02-25 21:50:43 -05:00
}
func ( client * Client ) doIdentLookup ( conn net . Conn ) {
_ , serverPortString , err := net . SplitHostPort ( conn . LocalAddr ( ) . String ( ) )
if err != nil {
client . server . logger . Error ( "internal" , "bad server address" , err . Error ( ) )
return
}
serverPort , _ := strconv . Atoi ( serverPortString )
clientHost , clientPortString , err := net . SplitHostPort ( conn . RemoteAddr ( ) . String ( ) )
if err != nil {
client . server . logger . Error ( "internal" , "bad client address" , err . Error ( ) )
return
}
clientPort , _ := strconv . Atoi ( clientPortString )
2016-06-30 19:28:34 +10:00
2019-02-25 21:50:43 -05:00
client . Notice ( client . t ( "*** Looking up your username" ) )
resp , err := ident . Query ( clientHost , serverPort , clientPort , IdentTimeoutSeconds )
if err == nil {
err := client . SetNames ( resp . Identifier , "" , true )
2016-06-30 19:28:34 +10:00
if err == nil {
2019-02-25 21:50:43 -05:00
client . Notice ( client . t ( "*** Found your username" ) )
// we don't need to updateNickMask here since nickMask is not used for anything yet
2016-06-30 19:28:34 +10:00
} else {
2019-02-25 21:50:43 -05:00
client . Notice ( client . t ( "*** Got a malformed username, ignoring" ) )
2016-06-30 19:28:34 +10:00
}
2019-02-25 21:50:43 -05:00
} else {
client . Notice ( client . t ( "*** Could not find your username" ) )
2016-06-30 19:28:34 +10:00
}
2012-04-07 11:44:59 -07:00
}
2019-02-05 00:19:03 -05:00
func ( client * Client ) isAuthorized ( config * Config ) bool {
saslSent := client . account != ""
2019-02-25 21:50:43 -05:00
// PASS requirement
2019-02-25 22:56:08 -05:00
if ( config . Server . passwordBytes != nil ) && ! client . sentPassCommand && ! ( config . Accounts . SkipServerPassword && saslSent ) {
2019-02-05 00:19:03 -05:00
return false
}
2019-02-25 21:50:43 -05:00
// Tor connections may be required to authenticate with SASL
2019-02-25 22:56:08 -05:00
if client . isTor && config . Server . TorListeners . RequireSasl && ! saslSent {
2019-02-25 21:50:43 -05:00
return false
}
// finally, enforce require-sasl
return ! config . Accounts . RequireSasl . Enabled || saslSent || utils . IPInNets ( client . IP ( ) , config . Accounts . RequireSasl . exemptedNets )
2019-02-05 00:19:03 -05:00
}
2019-04-12 00:08:46 -04:00
func ( session * Session ) resetFakelag ( ) {
var flc FakelagConfig = session . client . server . Config ( ) . Fakelag
flc . Enabled = flc . Enabled && ! session . client . HasRoleCapabs ( "nofakelag" )
session . fakelag . Initialize ( flc )
2018-03-22 11:04:21 -04:00
}
2017-05-24 00:58:36 -06:00
// IP returns the IP address of this client.
func ( client * Client ) IP ( ) net . IP {
2019-02-05 00:19:03 -05:00
client . stateMutex . RLock ( )
defer client . stateMutex . RUnlock ( )
2018-02-01 15:53:49 -05:00
if client . proxiedIP != nil {
return client . proxiedIP
2017-09-11 01:04:08 -04:00
}
2019-02-05 00:19:03 -05:00
return client . realIP
2017-05-24 00:58:36 -06:00
}
2017-06-23 05:15:10 +10:00
// IPString returns the IP address of this client as a string.
func ( client * Client ) IPString ( ) string {
ip := client . IP ( ) . String ( )
if 0 < len ( ip ) && ip [ 0 ] == ':' {
ip = "0" + ip
}
return ip
}
2014-02-23 22:21:39 -08:00
//
// command goroutine
//
2019-04-12 00:08:46 -04:00
func ( client * Client ) run ( session * Session ) {
2014-04-15 08:49:52 -07:00
2017-10-23 18:38:32 -04:00
defer func ( ) {
2017-10-26 05:15:55 -04:00
if r := recover ( ) ; r != nil {
client . server . logger . Error ( "internal" ,
fmt . Sprintf ( "Client caused panic: %v\n%s" , r , debug . Stack ( ) ) )
if client . server . RecoverFromErrors ( ) {
client . server . logger . Error ( "internal" , "Disconnecting client and attempting to recover" )
} else {
panic ( r )
2017-10-26 04:19:01 -04:00
}
2017-10-23 18:38:32 -04:00
}
// ensure client connection gets closed
2019-04-12 00:08:46 -04:00
client . destroy ( false , session )
2017-10-23 18:38:32 -04:00
} ( )
2019-04-12 00:08:46 -04:00
session . idletimer . Initialize ( session )
session . resetFakelag ( )
2017-10-15 12:24:28 -04:00
2019-04-12 00:08:46 -04:00
isReattach := client . Registered ( )
2019-05-08 18:14:49 -04:00
if isReattach {
client . playReattachMessages ( session )
} else {
// don't reset the nick timer during a reattach
2019-04-12 00:08:46 -04:00
client . nickTimer . Initialize ( client )
}
2018-03-22 11:04:21 -04:00
2018-09-03 00:19:10 -04:00
firstLine := true
2016-06-17 22:17:42 +10:00
for {
2019-04-12 00:08:46 -04:00
maxlenRest := session . MaxlenRest ( )
2017-10-22 19:50:16 -04:00
2019-04-12 00:08:46 -04:00
line , err := session . socket . Read ( )
2016-06-17 22:17:42 +10:00
if err != nil {
2018-03-17 21:32:12 -04:00
quitMessage := "connection closed"
if err == errReadQ {
quitMessage = "readQ exceeded"
}
2019-04-12 00:08:46 -04:00
client . Quit ( quitMessage , session )
2016-06-17 22:17:42 +10:00
break
}
2018-10-28 15:44:13 -04:00
if client . server . logger . IsLoggingRawIO ( ) {
client . server . logger . Debug ( "userinput" , client . nick , "<- " , line )
}
2018-09-03 00:19:10 -04:00
// special-cased handling of PROXY protocol, see `handleProxyCommand` for details:
2019-04-12 00:08:46 -04:00
if ! isReattach && firstLine {
2018-09-03 00:19:10 -04:00
firstLine = false
if strings . HasPrefix ( line , "PROXY" ) {
2019-04-12 00:08:46 -04:00
err = handleProxyCommand ( client . server , client , session , line )
2018-09-03 00:19:10 -04:00
if err != nil {
break
} else {
continue
}
}
}
2017-03-06 22:11:10 +10:00
2019-04-12 00:08:46 -04:00
msg , err := ircmsg . ParseLineStrict ( line , true , maxlenRest )
2017-01-19 07:56:33 +10:00
if err == ircmsg . ErrorLineIsEmpty {
continue
2019-03-07 02:31:46 -05:00
} else if err == ircmsg . ErrorLineTooLong {
2019-05-15 22:30:35 -04:00
session . Send ( nil , client . server . name , ERR_INPUTTOOLONG , client . Nick ( ) , client . t ( "Input line too long" ) )
2019-03-07 02:31:46 -05:00
continue
2017-01-19 07:56:33 +10:00
} else if err != nil {
2019-04-12 00:08:46 -04:00
client . Quit ( client . t ( "Received malformed line" ) , session )
2016-06-17 22:17:42 +10:00
break
2014-02-23 22:21:39 -08:00
}
2014-04-15 08:49:52 -07:00
2016-06-19 10:01:30 +10:00
cmd , exists := Commands [ msg . Command ]
if ! exists {
2016-11-04 21:38:47 +10:00
if len ( msg . Command ) > 0 {
2019-05-15 22:30:35 -04:00
session . Send ( nil , client . server . name , ERR_UNKNOWNCOMMAND , client . Nick ( ) , msg . Command , client . t ( "Unknown command" ) )
2016-11-04 21:38:47 +10:00
} else {
2019-05-15 22:30:35 -04:00
session . Send ( nil , client . server . name , ERR_UNKNOWNCOMMAND , client . Nick ( ) , "lastcmd" , client . t ( "No command given" ) )
2016-11-04 21:38:47 +10:00
}
2016-06-20 22:53:45 +10:00
continue
2016-06-19 10:01:30 +10:00
}
2019-04-12 00:08:46 -04:00
isExiting := cmd . Run ( client . server , client , session , msg )
if isExiting {
break
} else if session . client != client {
// bouncer reattach
go session . client . run ( session )
2016-06-17 22:17:42 +10:00
break
}
2014-02-23 22:21:39 -08:00
}
2014-04-15 08:49:52 -07:00
}
2019-05-08 18:14:49 -04:00
func ( client * Client ) playReattachMessages ( session * Session ) {
client . server . playRegistrationBurst ( session )
2019-04-12 00:08:46 -04:00
for _ , channel := range session . client . Channels ( ) {
channel . playJoinForSession ( session )
}
}
2016-06-17 22:17:42 +10:00
//
2017-05-09 20:37:48 +10:00
// idle, quit, timers and timeouts
2016-06-17 22:17:42 +10:00
//
2014-04-15 08:49:52 -07:00
2017-05-09 20:37:48 +10:00
// Active updates when the client was last 'active' (i.e. the user should be sitting in front of their client).
2019-05-08 04:11:54 -04:00
func ( client * Client ) Active ( session * Session ) {
2019-05-12 03:12:50 -04:00
now := time . Now ( ) . UTC ( )
2017-12-02 20:05:06 -05:00
client . stateMutex . Lock ( )
defer client . stateMutex . Unlock ( )
2019-05-08 04:11:54 -04:00
session . atime = now
client . atime = now
2014-02-18 13:25:21 -08:00
}
2014-02-13 18:39:33 -08:00
2017-10-15 12:24:28 -04:00
// Ping sends the client a PING message.
2019-04-12 00:08:46 -04:00
func ( session * Session ) Ping ( ) {
session . Send ( nil , "" , "PING" , session . client . Nick ( ) )
2017-05-09 20:37:48 +10:00
}
2019-02-09 20:01:47 -05:00
// tryResume tries to resume if the client asked us to.
func ( client * Client ) tryResume ( ) ( success bool ) {
2018-01-21 11:23:33 +10:00
server := client . server
2018-11-26 05:23:27 -05:00
config := server . Config ( )
2018-01-21 11:23:33 +10:00
2019-02-09 20:01:47 -05:00
defer func ( ) {
if ! success {
client . resumeDetails = nil
}
} ( )
2018-01-21 11:23:33 +10:00
timestamp := client . resumeDetails . Timestamp
var timestampString string
2018-11-26 05:23:27 -05:00
if ! timestamp . IsZero ( ) {
timestampString = timestamp . UTC ( ) . Format ( IRCv3TimestampFormat )
2018-01-21 11:23:33 +10:00
}
2019-02-12 00:27:57 -05:00
oldClient := server . resumeManager . VerifyToken ( client . resumeDetails . PresentedToken )
2018-11-26 05:23:27 -05:00
if oldClient == nil {
2019-02-12 00:27:57 -05:00
client . Send ( nil , server . name , "RESUME" , "ERR" , client . t ( "Cannot resume connection, token is not valid" ) )
2018-01-21 12:23:47 +10:00
return
}
2018-11-26 05:23:27 -05:00
oldNick := oldClient . Nick ( )
oldNickmask := oldClient . NickMaskString ( )
2018-01-21 12:23:47 +10:00
2018-11-26 05:23:27 -05:00
resumeAllowed := config . Server . AllowPlaintextResume || ( oldClient . HasMode ( modes . TLS ) && client . HasMode ( modes . TLS ) )
if ! resumeAllowed {
2019-02-12 00:27:57 -05:00
client . Send ( nil , server . name , "RESUME" , "ERR" , client . t ( "Cannot resume connection, old and new clients must have TLS" ) )
2018-01-21 11:23:33 +10:00
return
}
2019-02-26 16:39:10 -05:00
if oldClient . isTor != client . isTor {
client . Send ( nil , server . name , "RESUME" , "ERR" , client . t ( "Cannot resume connection from Tor to non-Tor or vice versa" ) )
return
}
2019-04-12 00:08:46 -04:00
if 1 < len ( oldClient . Sessions ( ) ) {
client . Send ( nil , server . name , "RESUME" , "ERR" , client . t ( "Cannot resume a client with multiple attached sessions" ) )
return
}
2018-11-26 05:23:27 -05:00
err := server . clients . Resume ( client , oldClient )
if err != nil {
client . Send ( nil , server . name , "RESUME" , "ERR" , client . t ( "Cannot resume connection" ) )
2018-01-21 11:23:33 +10:00
return
}
2019-02-09 20:01:47 -05:00
success = true
2018-11-26 05:23:27 -05:00
// this is a bit racey
2019-05-12 03:12:50 -04:00
client . resumeDetails . ResumedAt = time . Now ( ) . UTC ( )
2018-01-21 12:39:09 +10:00
2019-04-14 18:13:01 -04:00
client . nickTimer . Touch ( nil )
2018-11-26 05:23:27 -05:00
// resume successful, proceed to copy client state (nickname, flags, etc.)
// after this, the server thinks that `newClient` owns the nickname
client . resumeDetails . OldClient = oldClient
// transfer monitor stuff
server . monitorManager . Resume ( client , oldClient )
// record the names, not the pointers, of the channels,
// to avoid dumb annoying race conditions
channels := oldClient . Channels ( )
client . resumeDetails . Channels = make ( [ ] string , len ( channels ) )
for i , channel := range channels {
client . resumeDetails . Channels [ i ] = channel . Name ( )
2018-01-21 11:23:33 +10:00
}
2018-11-26 05:23:27 -05:00
username := client . Username ( )
hostname := client . Hostname ( )
friends := make ( ClientSet )
2019-05-12 03:12:50 -04:00
oldestLostMessage := time . Now ( ) . UTC ( )
2018-11-26 05:23:27 -05:00
// work out how much time, if any, is not covered by history buffers
for _ , channel := range channels {
for _ , member := range channel . Members ( ) {
friends . Add ( member )
lastDiscarded := channel . history . LastDiscarded ( )
if lastDiscarded . Before ( oldestLostMessage ) {
oldestLostMessage = lastDiscarded
}
}
}
2018-12-28 13:45:55 -05:00
privmsgMatcher := func ( item history . Item ) bool {
2019-05-06 23:17:57 -04:00
return item . Type == history . Privmsg || item . Type == history . Notice || item . Type == history . Tagmsg
2018-12-28 13:45:55 -05:00
}
2019-02-04 05:18:17 -05:00
privmsgHistory := oldClient . history . Match ( privmsgMatcher , false , 0 )
2018-11-26 05:23:27 -05:00
lastDiscarded := oldClient . history . LastDiscarded ( )
if lastDiscarded . Before ( oldestLostMessage ) {
oldestLostMessage = lastDiscarded
}
2018-12-28 13:45:55 -05:00
for _ , item := range privmsgHistory {
2019-05-06 23:17:57 -04:00
sender := server . clients . Get ( stripMaskFromNick ( item . Nick ) )
2018-12-28 13:45:55 -05:00
if sender != nil {
friends . Add ( sender )
2018-11-26 05:23:27 -05:00
}
}
gap := lastDiscarded . Sub ( timestamp )
client . resumeDetails . HistoryIncomplete = gap > 0
gapSeconds := int ( gap . Seconds ( ) ) + 1 // round up to avoid confusion
// send quit/resume messages to friends
for friend := range friends {
2019-04-12 00:08:46 -04:00
for _ , session := range friend . Sessions ( ) {
if session . capabilities . Has ( caps . Resume ) {
if timestamp . IsZero ( ) {
session . Send ( nil , oldNickmask , "RESUMED" , username , hostname )
} else {
session . Send ( nil , oldNickmask , "RESUMED" , username , hostname , timestampString )
}
2018-11-26 05:23:27 -05:00
} else {
2019-04-12 00:08:46 -04:00
if client . resumeDetails . HistoryIncomplete {
session . Send ( nil , oldNickmask , "QUIT" , fmt . Sprintf ( friend . t ( "Client reconnected (up to %d seconds of history lost)" ) , gapSeconds ) )
} else {
session . Send ( nil , oldNickmask , "QUIT" , fmt . Sprintf ( friend . t ( "Client reconnected" ) ) )
}
2018-11-26 05:23:27 -05:00
}
2018-01-21 11:23:33 +10:00
}
}
2018-11-26 05:23:27 -05:00
if client . resumeDetails . HistoryIncomplete {
2018-12-28 13:45:55 -05:00
client . Send ( nil , client . server . name , "RESUME" , "WARN" , fmt . Sprintf ( client . t ( "Resume may have lost up to %d seconds of history" ) , gapSeconds ) )
2018-11-26 05:23:27 -05:00
}
2018-01-21 11:23:33 +10:00
2018-12-28 13:45:55 -05:00
client . Send ( nil , client . server . name , "RESUME" , "SUCCESS" , oldNick )
2018-01-21 13:13:20 +10:00
2018-11-26 05:23:27 -05:00
// after we send the rest of the registration burst, we'll try rejoining channels
2019-02-09 20:01:47 -05:00
return
2018-11-26 05:23:27 -05:00
}
2018-04-24 03:11:11 -04:00
2018-11-26 05:23:27 -05:00
func ( client * Client ) tryResumeChannels ( ) {
details := client . resumeDetails
2018-01-21 11:23:33 +10:00
2018-11-26 05:23:27 -05:00
for _ , name := range details . Channels {
channel := client . server . channels . Get ( name )
if channel == nil {
continue
2018-01-22 20:55:20 +10:00
}
2018-11-26 05:23:27 -05:00
channel . Resume ( client , details . OldClient , details . Timestamp )
}
2018-01-22 20:55:20 +10:00
2018-11-26 05:23:27 -05:00
// replay direct PRIVSMG history
if ! details . Timestamp . IsZero ( ) {
2019-05-12 03:12:50 -04:00
now := time . Now ( ) . UTC ( )
2019-02-04 05:18:17 -05:00
items , complete := client . history . Between ( details . Timestamp , now , false , 0 )
2019-04-12 00:08:46 -04:00
rb := NewResponseBuffer ( client . Sessions ( ) [ 0 ] )
2019-02-04 05:18:17 -05:00
client . replayPrivmsgHistory ( rb , items , complete )
rb . Send ( true )
2018-04-24 03:11:11 -04:00
}
2018-01-21 11:23:33 +10:00
2019-04-12 00:08:46 -04:00
details . OldClient . destroy ( true , nil )
2018-11-26 05:23:27 -05:00
}
2019-02-04 05:18:17 -05:00
func ( client * Client ) replayPrivmsgHistory ( rb * ResponseBuffer , items [ ] history . Item , complete bool ) {
2019-05-06 23:17:57 -04:00
var batchID string
2019-05-19 02:14:36 -04:00
details := client . Details ( )
nick := details . nick
2019-05-06 23:17:57 -04:00
if 0 < len ( items ) {
batchID = rb . StartNestedHistoryBatch ( nick )
}
allowTags := rb . session . capabilities . Has ( caps . MessageTags )
2019-02-04 05:18:17 -05:00
for _ , item := range items {
var command string
switch item . Type {
case history . Privmsg :
command = "PRIVMSG"
case history . Notice :
command = "NOTICE"
2019-05-06 23:17:57 -04:00
case history . Tagmsg :
if allowTags {
command = "TAGMSG"
} else {
continue
}
2019-02-04 05:18:17 -05:00
default :
continue
}
2019-03-07 02:31:46 -05:00
var tags map [ string ] string
2019-05-06 23:17:57 -04:00
if allowTags {
tags = item . Tags
2019-02-04 05:18:17 -05:00
}
2019-05-19 02:14:36 -04:00
if item . Params [ 0 ] == "" {
// this message was sent *to* the client from another nick
rb . AddSplitMessageFromClient ( item . Nick , item . AccountName , tags , command , nick , item . Message )
} else {
// this message was sent *from* the client to another nick; the target is item.Params[0]
// substitute the client's current nickmask in case they changed nick
rb . AddSplitMessageFromClient ( details . nickMask , item . AccountName , tags , command , item . Params [ 0 ] , item . Message )
}
2019-02-04 05:18:17 -05:00
}
2019-05-06 23:17:57 -04:00
rb . EndNestedBatch ( batchID )
2019-02-04 05:18:17 -05:00
if ! complete {
rb . Add ( nil , "HistServ" , "NOTICE" , nick , client . t ( "Some additional message history may have been lost" ) )
}
}
2018-11-26 05:23:27 -05:00
// copy applicable state from oldClient to client as part of a resume
func ( client * Client ) copyResumeData ( oldClient * Client ) {
oldClient . stateMutex . RLock ( )
history := oldClient . history
nick := oldClient . nick
nickCasefolded := oldClient . nickCasefolded
vhost := oldClient . vhost
account := oldClient . account
accountName := oldClient . accountName
2019-01-30 18:59:49 -05:00
skeleton := oldClient . skeleton
2018-11-26 05:23:27 -05:00
oldClient . stateMutex . RUnlock ( )
// copy all flags, *except* TLS (in the case that the admins enabled
// resume over plaintext)
hasTLS := client . flags . HasMode ( modes . TLS )
temp := modes . NewModeSet ( )
2019-03-11 19:24:45 -04:00
temp . Copy ( & oldClient . flags )
2018-11-26 05:23:27 -05:00
temp . SetMode ( modes . TLS , hasTLS )
client . flags . Copy ( temp )
2018-01-21 11:23:33 +10:00
2018-11-26 05:23:27 -05:00
client . stateMutex . Lock ( )
defer client . stateMutex . Unlock ( )
2018-01-21 11:23:33 +10:00
2018-11-26 05:23:27 -05:00
// reuse the old client's history buffer
client . history = history
// copy other data
client . nick = nick
client . nickCasefolded = nickCasefolded
client . vhost = vhost
client . account = account
client . accountName = accountName
2019-01-30 18:59:49 -05:00
client . skeleton = skeleton
2018-11-26 05:23:27 -05:00
client . updateNickMaskNoMutex ( )
2018-01-21 11:23:33 +10:00
}
2016-10-23 11:48:57 +10:00
// IdleTime returns how long this client's been idle.
2014-02-17 15:25:32 -08:00
func ( client * Client ) IdleTime ( ) time . Duration {
2017-12-02 20:05:06 -05:00
client . stateMutex . RLock ( )
defer client . stateMutex . RUnlock ( )
2014-02-17 15:25:32 -08:00
return time . Since ( client . atime )
}
2016-10-23 11:48:57 +10:00
// SignonTime returns this client's signon time as a unix timestamp.
2014-02-17 19:56:06 -08:00
func ( client * Client ) SignonTime ( ) int64 {
return client . ctime . Unix ( )
}
2016-10-23 11:48:57 +10:00
// IdleSeconds returns the number of seconds this client's been idle.
2014-02-17 19:08:57 -08:00
func ( client * Client ) IdleSeconds ( ) uint64 {
return uint64 ( client . IdleTime ( ) . Seconds ( ) )
}
2016-10-23 11:48:57 +10:00
// HasNick returns true if the client's nickname is set (used in registration).
2014-02-08 17:10:04 -08:00
func ( client * Client ) HasNick ( ) bool {
2017-11-22 04:41:11 -05:00
client . stateMutex . RLock ( )
defer client . stateMutex . RUnlock ( )
2016-10-11 23:51:46 +10:00
return client . nick != "" && client . nick != "*"
2012-12-16 19:13:53 -08:00
}
2017-04-16 11:31:33 +10:00
// HasUsername returns true if the client's username is set (used in registration).
2014-02-08 17:10:04 -08:00
func ( client * Client ) HasUsername ( ) bool {
2017-11-22 04:41:11 -05:00
client . stateMutex . RLock ( )
defer client . stateMutex . RUnlock ( )
2016-10-11 23:51:46 +10:00
return client . username != "" && client . username != "*"
2014-02-08 17:10:04 -08:00
}
2019-02-03 18:49:42 +10:00
// SetNames sets the client's ident and realname.
2019-02-05 02:40:49 -05:00
func ( client * Client ) SetNames ( username , realname string , fromIdent bool ) error {
limit := client . server . Config ( ) . Limits . IdentLen
if ! fromIdent {
limit -= 1 // leave room for the prepended ~
}
2019-02-05 03:04:52 -05:00
if limit < len ( username ) {
2019-02-05 02:40:49 -05:00
username = username [ : limit ]
}
2019-02-03 18:49:42 +10:00
if ! isIdent ( username ) {
2018-11-26 05:23:27 -05:00
return errInvalidUsername
}
2019-02-05 02:40:49 -05:00
if ! fromIdent {
username = "~" + username
}
2019-02-03 18:49:42 +10:00
2018-11-26 05:23:27 -05:00
client . stateMutex . Lock ( )
defer client . stateMutex . Unlock ( )
if client . username == "" {
2019-02-05 02:40:49 -05:00
client . username = username
2018-11-26 05:23:27 -05:00
}
if client . realname == "" {
client . realname = realname
}
return nil
}
2017-09-29 12:11:06 +10:00
// HasRoleCapabs returns true if client has the given (role) capabilities.
func ( client * Client ) HasRoleCapabs ( capabs ... string ) bool {
2018-04-19 02:48:19 -04:00
oper := client . Oper ( )
if oper == nil {
2016-10-23 11:13:08 +10:00
return false
}
for _ , capab := range capabs {
2018-04-19 02:48:19 -04:00
if ! oper . Class . Capabilities [ capab ] {
2016-10-23 11:13:08 +10:00
return false
}
}
return true
}
2017-04-16 11:31:33 +10:00
// ModeString returns the mode string for this client.
func ( client * Client ) ModeString ( ) ( str string ) {
2018-04-22 18:47:10 -04:00
return "+" + client . flags . String ( )
2012-04-17 20:24:26 -07:00
}
2012-04-17 21:13:12 -07:00
2016-06-17 22:17:42 +10:00
// Friends refers to clients that share a channel with this client.
2019-04-12 00:08:46 -04:00
func ( client * Client ) Friends ( capabs ... caps . Capability ) ( result map [ * Session ] bool ) {
result = make ( map [ * Session ] bool )
2016-10-27 00:44:36 +10:00
2019-04-12 00:08:46 -04:00
// look at the client's own sessions
for _ , session := range client . Sessions ( ) {
if session . capabilities . HasAll ( capabs ... ) {
result [ session ] = true
2016-10-27 00:44:36 +10:00
}
}
2017-10-22 19:50:16 -04:00
for _ , channel := range client . Channels ( ) {
for _ , member := range channel . Members ( ) {
2019-04-12 00:08:46 -04:00
for _ , session := range member . Sessions ( ) {
if session . capabilities . HasAll ( capabs ... ) {
result [ session ] = true
2016-10-13 18:08:08 +10:00
}
}
2014-02-18 15:28:20 -08:00
}
2014-02-16 17:23:47 -08:00
}
2019-04-12 00:08:46 -04:00
return
2014-02-16 17:23:47 -08:00
}
2019-01-30 18:59:49 -05:00
func ( client * Client ) SetOper ( oper * Oper ) {
client . stateMutex . Lock ( )
defer client . stateMutex . Unlock ( )
client . oper = oper
// operators typically get a vhost, update the nickmask
client . updateNickMaskNoMutex ( )
}
2018-04-19 02:48:19 -04:00
// XXX: CHGHOST requires prefix nickmask to have original hostname,
// this is annoying to do correctly
func ( client * Client ) sendChghost ( oldNickMask string , vhost string ) {
username := client . Username ( )
for fClient := range client . Friends ( caps . ChgHost ) {
2018-11-26 05:23:27 -05:00
fClient . sendFromClientInternal ( false , time . Time { } , "" , oldNickMask , client . AccountName ( ) , nil , "CHGHOST" , username , vhost )
2018-04-19 02:48:19 -04:00
}
}
// choose the correct vhost to display
func ( client * Client ) getVHostNoMutex ( ) string {
// hostserv vhost OR operclass vhost OR nothing (i.e., normal rdns hostmask)
if client . vhost != "" {
return client . vhost
} else if client . oper != nil {
return client . oper . Vhost
} else {
return ""
}
}
// SetVHost updates the client's hostserv-based vhost
func ( client * Client ) SetVHost ( vhost string ) ( updated bool ) {
client . stateMutex . Lock ( )
defer client . stateMutex . Unlock ( )
updated = ( client . vhost != vhost )
client . vhost = vhost
if updated {
client . updateNickMaskNoMutex ( )
}
return
}
2017-10-04 00:57:03 -04:00
// updateNick updates `nick` and `nickCasefolded`.
2019-01-30 18:59:49 -05:00
func ( client * Client ) updateNick ( nick , nickCasefolded , skeleton string ) {
2017-10-04 00:57:03 -04:00
client . stateMutex . Lock ( )
2019-01-30 18:59:49 -05:00
defer client . stateMutex . Unlock ( )
2017-10-04 00:57:03 -04:00
client . nick = nick
2019-01-30 18:59:49 -05:00
client . nickCasefolded = nickCasefolded
client . skeleton = skeleton
client . updateNickMaskNoMutex ( )
2016-10-16 20:35:50 +10:00
}
2019-01-30 18:59:49 -05:00
// updateNickMaskNoMutex updates the casefolded nickname and nickmask, not acquiring any mutexes.
2018-01-22 20:55:20 +10:00
func ( client * Client ) updateNickMaskNoMutex ( ) {
2018-04-19 02:48:19 -04:00
client . hostname = client . getVHostNoMutex ( )
if client . hostname == "" {
2019-05-12 02:17:57 -04:00
client . hostname = client . cloakedHostname
if client . hostname == "" {
client . hostname = client . rawHostname
}
2016-10-23 11:28:31 +10:00
}
2019-01-28 13:36:15 -05:00
cfhostname , err := Casefold ( client . hostname )
2016-10-11 23:51:46 +10:00
if err != nil {
2019-01-28 13:36:15 -05:00
client . server . logger . Error ( "internal" , "hostname couldn't be casefolded" , client . hostname , err . Error ( ) )
cfhostname = client . hostname // YOLO
2016-10-11 23:51:46 +10:00
}
2017-10-04 00:57:03 -04:00
2019-01-28 13:36:15 -05:00
client . nickMaskString = fmt . Sprintf ( "%s!%s@%s" , client . nick , client . username , client . hostname )
2019-02-05 02:40:49 -05:00
client . nickMaskCasefolded = fmt . Sprintf ( "%s!%s@%s" , client . nickCasefolded , strings . ToLower ( client . username ) , cfhostname )
2016-06-19 15:37:29 +10:00
}
2017-01-11 22:38:16 +10:00
// AllNickmasks returns all the possible nickmasks for the client.
2019-01-28 23:03:30 -05:00
func ( client * Client ) AllNickmasks ( ) ( masks [ ] string ) {
2018-04-19 02:48:19 -04:00
client . stateMutex . RLock ( )
2019-01-28 23:03:30 -05:00
nick := client . nickCasefolded
2019-02-05 02:40:49 -05:00
username := client . username
2018-04-19 02:48:19 -04:00
rawHostname := client . rawHostname
2019-05-12 02:17:57 -04:00
cloakedHostname := client . cloakedHostname
2018-04-19 02:48:19 -04:00
vhost := client . getVHostNoMutex ( )
client . stateMutex . RUnlock ( )
2019-02-05 03:04:52 -05:00
username = strings . ToLower ( username )
2018-04-19 02:48:19 -04:00
if len ( vhost ) > 0 {
2019-01-28 23:03:30 -05:00
cfvhost , err := Casefold ( vhost )
2017-01-11 22:38:16 +10:00
if err == nil {
2019-01-28 23:03:30 -05:00
masks = append ( masks , fmt . Sprintf ( "%s!%s@%s" , nick , username , cfvhost ) )
2017-01-11 22:38:16 +10:00
}
}
2019-01-28 23:03:30 -05:00
var rawhostmask string
cfrawhost , err := Casefold ( rawHostname )
2017-01-11 22:38:16 +10:00
if err == nil {
2019-01-28 23:03:30 -05:00
rawhostmask = fmt . Sprintf ( "%s!%s@%s" , nick , username , cfrawhost )
masks = append ( masks , rawhostmask )
2017-01-11 22:38:16 +10:00
}
2019-05-12 02:17:57 -04:00
if cloakedHostname != "" {
masks = append ( masks , fmt . Sprintf ( "%s!%s@%s" , nick , username , cloakedHostname ) )
}
2017-01-11 22:38:16 +10:00
2019-01-28 23:03:30 -05:00
ipmask := fmt . Sprintf ( "%s!%s@%s" , nick , username , client . IPString ( ) )
if ipmask != rawhostmask {
masks = append ( masks , ipmask )
2017-01-11 22:38:16 +10:00
}
2019-01-28 23:03:30 -05:00
return
2017-01-11 22:38:16 +10:00
}
2017-09-28 15:49:01 +10:00
// LoggedIntoAccount returns true if this client is logged into an account.
func ( client * Client ) LoggedIntoAccount ( ) bool {
2018-02-11 05:30:40 -05:00
return client . Account ( ) != ""
2017-09-28 15:49:01 +10:00
}
2017-10-05 23:39:57 +10:00
// RplISupport outputs our ISUPPORT lines to the client. This is used on connection and in VERSION responses.
2018-02-06 00:21:08 +10:00
func ( client * Client ) RplISupport ( rb * ResponseBuffer ) {
2018-01-23 23:31:29 +10:00
translatedISupport := client . t ( "are supported by this server" )
2018-04-20 14:13:25 -04:00
nick := client . Nick ( )
2019-05-10 00:27:28 -04:00
config := client . server . Config ( )
for _ , cachedTokenLine := range config . Server . isupport . CachedReply {
2018-04-20 14:13:25 -04:00
length := len ( cachedTokenLine ) + 2
tokenline := make ( [ ] string , length )
tokenline [ 0 ] = nick
copy ( tokenline [ 1 : ] , cachedTokenLine )
tokenline [ length - 1 ] = translatedISupport
rb . Add ( nil , client . server . name , RPL_ISUPPORT , tokenline ... )
2017-10-05 23:39:57 +10:00
}
}
2019-02-10 13:57:32 -05:00
// Quit sets the given quit message for the client.
// (You must ensure separately that destroy() is called, e.g., by returning `true` from
// the command handler or calling it yourself.)
2019-04-12 00:08:46 -04:00
func ( client * Client ) Quit ( message string , session * Session ) {
setFinalData := func ( sess * Session ) {
message := sess . quitMessage
var finalData [ ] byte
// #364: don't send QUIT lines to unregistered clients
if client . registered {
quitMsg := ircmsg . MakeMessage ( nil , client . nickMaskString , "QUIT" , message )
finalData , _ = quitMsg . LineBytesStrict ( false , 512 )
}
2017-10-10 20:49:29 -04:00
2019-04-12 00:08:46 -04:00
errorMsg := ircmsg . MakeMessage ( nil , "" , "ERROR" , message )
errorMsgBytes , _ := errorMsg . LineBytesStrict ( false , 512 )
finalData = append ( finalData , errorMsgBytes ... )
2017-10-10 20:49:29 -04:00
2019-04-12 00:08:46 -04:00
sess . socket . SetFinalData ( finalData )
2019-02-10 13:57:32 -05:00
}
2017-10-10 20:49:29 -04:00
2019-04-12 00:08:46 -04:00
client . stateMutex . Lock ( )
defer client . stateMutex . Unlock ( )
var sessions [ ] * Session
if session != nil {
sessions = [ ] * Session { session }
} else {
sessions = client . sessions
}
2017-10-10 20:49:29 -04:00
2019-04-12 00:08:46 -04:00
for _ , session := range sessions {
if session . SetQuitMessage ( message ) {
setFinalData ( session )
}
}
2016-06-17 22:17:42 +10:00
}
2016-10-23 11:48:57 +10:00
// destroy gets rid of a client, removes them from server lists etc.
2019-04-12 00:08:46 -04:00
// if `session` is nil, destroys the client unconditionally, removing all sessions;
// otherwise, destroys one specific session, only destroying the client if it
// has no more sessions.
func ( client * Client ) destroy ( beingResumed bool , session * Session ) {
var sessionsToDestroy [ ] * Session
2017-10-11 12:48:06 -04:00
// allow destroy() to execute at most once
2018-11-26 05:23:27 -05:00
client . stateMutex . Lock ( )
2019-05-08 18:14:49 -04:00
details := client . detailsNoMutex ( )
wasReattach := session != nil && session . client != client
2019-04-12 00:08:46 -04:00
sessionRemoved := false
var remainingSessions int
if session == nil {
sessionsToDestroy = client . sessions
client . sessions = nil
remainingSessions = 0
} else {
sessionRemoved , remainingSessions = client . removeSession ( session )
if sessionRemoved {
sessionsToDestroy = [ ] * Session { session }
}
}
2018-11-26 05:23:27 -05:00
client . stateMutex . Unlock ( )
2019-05-08 18:14:49 -04:00
if len ( sessionsToDestroy ) == 0 {
2019-04-12 00:08:46 -04:00
return
}
2019-05-08 18:14:49 -04:00
// destroy all applicable sessions:
var quitMessage string
2019-04-12 00:08:46 -04:00
for _ , session := range sessionsToDestroy {
if session . client != client {
// session has been attached to a new client; do not destroy it
continue
}
session . idletimer . Stop ( )
// send quit/error message to client if they haven't been sent already
client . Quit ( "" , session )
2019-05-08 18:14:49 -04:00
quitMessage = session . quitMessage
session . socket . Close ( )
// remove from connection limits
var source string
if client . isTor {
client . server . torLimiter . RemoveClient ( )
source = "tor"
} else {
ip := session . realIP
if session . proxiedIP != nil {
ip = session . proxiedIP
}
client . server . connectionLimiter . RemoveClient ( ip )
source = ip . String ( )
}
client . server . logger . Info ( "localconnect-ip" , fmt . Sprintf ( "disconnecting session of %s from %s" , details . nick , source ) )
2019-04-12 00:08:46 -04:00
}
2019-05-08 18:14:49 -04:00
// ok, now destroy the client, unless it still has sessions:
2019-04-12 00:08:46 -04:00
if remainingSessions != 0 {
2018-01-22 20:55:20 +10:00
return
2014-02-18 13:25:21 -08:00
}
2014-02-19 18:46:46 -08:00
2018-04-24 20:34:28 -04:00
// see #235: deduplicating the list of PART recipients uses (comparatively speaking)
// a lot of RAM, so limit concurrency to avoid thrashing
client . server . semaphores . ClientDestroy . Acquire ( )
defer client . server . semaphores . ClientDestroy . Release ( )
2018-01-21 11:23:33 +10:00
if beingResumed {
2019-05-06 23:17:57 -04:00
client . server . logger . Debug ( "quit" , fmt . Sprintf ( "%s is being resumed" , details . nick ) )
2019-05-08 18:14:49 -04:00
} else if ! wasReattach {
2019-05-06 23:17:57 -04:00
client . server . logger . Debug ( "quit" , fmt . Sprintf ( "%s is no longer on the server" , details . nick ) )
2018-01-21 11:23:33 +10:00
}
2017-03-06 22:11:10 +10:00
2019-05-08 18:14:49 -04:00
registered := client . Registered ( )
if ! beingResumed && registered {
2018-05-04 00:24:54 -04:00
client . server . whoWas . Append ( client . WhoWas ( ) )
2018-01-21 11:23:33 +10:00
}
2016-06-17 22:17:42 +10:00
2019-02-12 00:27:57 -05:00
client . server . resumeManager . Delete ( client )
2016-10-16 20:14:56 +10:00
// alert monitors
2019-05-08 18:14:49 -04:00
if registered {
client . server . monitorManager . AlertAbout ( client , false )
}
2017-10-04 00:57:03 -04:00
// clean up monitor state
2017-10-04 02:59:59 -04:00
client . server . monitorManager . RemoveAll ( client )
2016-10-16 20:14:56 +10:00
2019-05-06 23:17:57 -04:00
splitQuitMessage := utils . MakeSplitMessage ( quitMessage , true )
2016-06-17 22:17:42 +10:00
// clean up channels
2019-05-08 18:14:49 -04:00
// (note that if this is a reattach, client has no channels and therefore no friends)
2018-04-24 20:23:01 -04:00
friends := make ( ClientSet )
2017-10-30 05:21:47 -04:00
for _ , channel := range client . Channels ( ) {
2018-01-21 11:23:33 +10:00
if ! beingResumed {
channel . Quit ( client )
2018-11-26 05:23:27 -05:00
channel . history . Add ( history . Item {
Type : history . Quit ,
2019-05-08 18:14:49 -04:00
Nick : details . nickMask ,
AccountName : details . accountName ,
2019-05-06 23:17:57 -04:00
Message : splitQuitMessage ,
2018-11-26 05:23:27 -05:00
} )
2018-01-21 11:23:33 +10:00
}
2017-10-22 19:50:16 -04:00
for _ , member := range channel . Members ( ) {
friends . Add ( member )
}
2016-06-17 22:17:42 +10:00
}
2018-04-24 20:23:01 -04:00
friends . Remove ( client )
2016-06-17 22:17:42 +10:00
// clean up server
2018-01-21 11:23:33 +10:00
if ! beingResumed {
client . server . clients . Remove ( client )
}
2016-06-17 22:17:42 +10:00
// clean up self
2018-02-28 00:14:44 -05:00
client . nickTimer . Stop ( )
2016-06-17 22:17:42 +10:00
2018-02-11 05:30:40 -05:00
client . server . accounts . Logout ( client )
2016-11-29 21:06:01 +10:00
// send quit messages to friends
2018-01-21 11:23:33 +10:00
if ! beingResumed {
2019-05-12 03:20:31 -04:00
if registered {
2018-08-08 17:56:12 -04:00
client . server . stats . ChangeTotal ( - 1 )
}
2018-04-20 22:48:15 +02:00
if client . HasMode ( modes . Invisible ) {
client . server . stats . ChangeInvisible ( - 1 )
}
if client . HasMode ( modes . Operator ) || client . HasMode ( modes . LocalOperator ) {
client . server . stats . ChangeOperators ( - 1 )
}
2018-01-21 11:23:33 +10:00
for friend := range friends {
2018-11-26 05:23:27 -05:00
if quitMessage == "" {
quitMessage = "Exited"
2018-01-21 11:23:33 +10:00
}
2019-05-06 23:17:57 -04:00
friend . sendFromClientInternal ( false , splitQuitMessage . Time , splitQuitMessage . Msgid , details . nickMask , details . accountName , nil , "QUIT" , quitMessage )
2017-09-25 22:47:03 -04:00
}
2016-11-29 21:06:01 +10:00
}
2017-06-11 10:01:39 -06:00
if ! client . exitedSnomaskSent {
2018-01-21 11:23:33 +10:00
if beingResumed {
client . server . snomasks . Send ( sno . LocalQuits , fmt . Sprintf ( ircfmt . Unescape ( "%s$r is resuming their connection, old client has been destroyed" ) , client . nick ) )
2019-05-12 03:20:31 -04:00
} else if registered {
2019-05-06 23:17:57 -04:00
client . server . snomasks . Send ( sno . LocalQuits , fmt . Sprintf ( ircfmt . Unescape ( "%s$r exited the network" ) , details . nick ) )
2018-01-21 11:23:33 +10:00
}
2017-06-11 10:01:39 -06:00
}
2016-06-19 10:01:30 +10:00
}
2014-02-18 13:25:21 -08:00
2017-01-14 15:28:50 +10:00
// SendSplitMsgFromClient sends an IRC PRIVMSG/NOTICE coming from a specific client.
// Adds account-tag to the line as well.
2019-05-13 00:39:59 -04:00
func ( session * Session ) sendSplitMsgFromClientInternal ( blocking bool , nickmask , accountName string , tags map [ string ] string , command , target string , message utils . SplitMessage ) {
2019-04-12 00:08:46 -04:00
if session . capabilities . Has ( caps . MaxLine ) || message . Wrapped == nil {
2019-05-13 00:39:59 -04:00
session . sendFromClientInternal ( blocking , message . Time , message . Msgid , nickmask , accountName , tags , command , target , message . Message )
2017-01-14 15:28:50 +10:00
} else {
2019-03-07 02:31:46 -05:00
for _ , messagePair := range message . Wrapped {
2019-05-13 00:39:59 -04:00
session . sendFromClientInternal ( blocking , message . Time , messagePair . Msgid , nickmask , accountName , tags , command , target , messagePair . Message )
2017-01-14 15:28:50 +10:00
}
}
}
2019-05-06 23:17:57 -04:00
// Sends a line with `nickmask` as the prefix, adding `time` and `account` tags if supported
2019-04-12 00:08:46 -04:00
func ( client * Client ) sendFromClientInternal ( blocking bool , serverTime time . Time , msgid string , nickmask , accountName string , tags map [ string ] string , command string , params ... string ) ( err error ) {
for _ , session := range client . Sessions ( ) {
err_ := session . sendFromClientInternal ( blocking , serverTime , msgid , nickmask , accountName , tags , command , params ... )
if err_ != nil {
err = err_
}
}
return
}
func ( session * Session ) sendFromClientInternal ( blocking bool , serverTime time . Time , msgid string , nickmask , accountName string , tags map [ string ] string , command string , params ... string ) ( err error ) {
2019-03-07 02:31:46 -05:00
msg := ircmsg . MakeMessage ( tags , nickmask , command , params ... )
2016-09-12 11:25:31 +10:00
// attach account-tag
2019-04-12 00:08:46 -04:00
if session . capabilities . Has ( caps . AccountTag ) && accountName != "*" {
2019-03-07 02:31:46 -05:00
msg . SetTag ( "account" , accountName )
2016-09-12 11:25:31 +10:00
}
2017-01-14 19:52:47 +10:00
// attach message-id
2019-04-12 00:08:46 -04:00
if msgid != "" && session . capabilities . Has ( caps . MessageTags ) {
2019-05-15 01:30:21 -04:00
msg . SetTag ( "msgid" , msgid )
2019-03-07 02:31:46 -05:00
}
// attach server-time
2019-04-12 00:08:46 -04:00
if session . capabilities . Has ( caps . ServerTime ) {
2019-05-06 23:17:57 -04:00
if serverTime . IsZero ( ) {
serverTime = time . Now ( ) . UTC ( )
}
msg . SetTag ( "time" , serverTime . Format ( IRCv3TimestampFormat ) )
2017-01-14 19:52:47 +10:00
}
2016-09-12 11:25:31 +10:00
2019-04-12 00:08:46 -04:00
return session . SendRawMessage ( msg , blocking )
2016-09-12 11:25:31 +10:00
}
2017-01-21 00:07:10 +10:00
var (
2017-01-23 09:03:49 +10:00
// these are all the output commands that MUST have their last param be a trailing.
2017-10-08 11:05:05 +10:00
// this is needed because dumb clients like to treat trailing params separately from the
2017-01-23 09:03:49 +10:00
// other params in messages.
2017-01-21 00:07:10 +10:00
commandsThatMustUseTrailing = map [ string ] bool {
"PRIVMSG" : true ,
"NOTICE" : true ,
2017-01-23 09:03:49 +10:00
RPL_WHOISCHANNELS : true ,
2017-03-06 15:50:23 +10:00
RPL_USERHOST : true ,
2017-01-21 00:07:10 +10:00
}
)
2017-10-08 11:05:05 +10:00
// SendRawMessage sends a raw message to the client.
2019-04-12 00:08:46 -04:00
func ( session * Session ) SendRawMessage ( message ircmsg . IrcMessage , blocking bool ) error {
2017-10-08 11:05:05 +10:00
// use dumb hack to force the last param to be a trailing param if required
2017-05-09 20:37:48 +10:00
var usedTrailingHack bool
2019-05-09 14:18:30 -04:00
config := session . client . server . Config ( )
if config . Server . Compatibility . forceTrailing && commandsThatMustUseTrailing [ message . Command ] && len ( message . Params ) > 0 {
2017-10-08 11:05:05 +10:00
lastParam := message . Params [ len ( message . Params ) - 1 ]
2017-05-09 20:37:48 +10:00
// to force trailing, we ensure the final param contains a space
2019-03-07 02:31:46 -05:00
if strings . IndexByte ( lastParam , ' ' ) == - 1 {
2017-10-08 11:05:05 +10:00
message . Params [ len ( message . Params ) - 1 ] = lastParam + " "
2017-05-09 20:37:48 +10:00
usedTrailingHack = true
2017-01-20 23:51:36 +10:00
}
}
2017-10-08 11:05:05 +10:00
// assemble message
2019-04-12 00:08:46 -04:00
maxlenRest := session . MaxlenRest ( )
2019-03-07 02:31:46 -05:00
line , err := message . LineBytesStrict ( false , maxlenRest )
2016-06-19 10:01:30 +10:00
if err != nil {
2018-02-11 05:30:40 -05:00
logline := fmt . Sprintf ( "Error assembling message for sending: %v\n%s" , err , debug . Stack ( ) )
2019-04-12 00:08:46 -04:00
session . client . server . logger . Error ( "internal" , logline )
2016-11-04 21:38:47 +10:00
2019-04-12 00:08:46 -04:00
message = ircmsg . MakeMessage ( nil , session . client . server . name , ERR_UNKNOWNERROR , "*" , "Error assembling message for sending" )
2019-03-07 02:31:46 -05:00
line , _ := message . LineBytesStrict ( false , 0 )
2017-10-08 11:05:05 +10:00
2018-11-26 05:23:27 -05:00
if blocking {
2019-04-12 00:08:46 -04:00
session . socket . BlockingWrite ( line )
2018-11-26 05:23:27 -05:00
} else {
2019-04-12 00:08:46 -04:00
session . socket . Write ( line )
2018-11-26 05:23:27 -05:00
}
2016-06-19 10:01:30 +10:00
return err
2014-02-16 17:23:47 -08:00
}
2017-01-20 23:51:36 +10:00
2018-04-16 02:28:25 +10:00
// if we used the trailing hack, we need to strip the final space we appended earlier on
if usedTrailingHack {
2019-03-07 02:31:46 -05:00
copy ( line [ len ( line ) - 3 : ] , "\r\n" )
2018-04-26 15:32:32 -04:00
line = line [ : len ( line ) - 1 ]
2018-04-16 02:28:25 +10:00
}
2019-04-12 00:08:46 -04:00
if session . client . server . logger . IsLoggingRawIO ( ) {
2018-04-26 15:32:32 -04:00
logline := string ( line [ : len ( line ) - 2 ] ) // strip "\r\n"
2019-04-12 00:08:46 -04:00
session . client . server . logger . Debug ( "useroutput" , session . client . Nick ( ) , " ->" , logline )
2018-04-26 15:32:32 -04:00
}
2017-03-06 22:11:10 +10:00
2018-11-26 05:23:27 -05:00
if blocking {
2019-04-12 00:08:46 -04:00
return session . socket . BlockingWrite ( line )
2018-11-26 05:23:27 -05:00
} else {
2019-04-12 00:08:46 -04:00
return session . socket . Write ( line )
2018-11-26 05:23:27 -05:00
}
2017-10-08 11:05:05 +10:00
}
2018-11-26 05:23:27 -05:00
// Send sends an IRC line to the client.
2019-04-12 00:08:46 -04:00
func ( client * Client ) Send ( tags map [ string ] string , prefix string , command string , params ... string ) ( err error ) {
for _ , session := range client . Sessions ( ) {
err_ := session . Send ( tags , prefix , command , params ... )
if err_ != nil {
err = err_
}
}
return
}
func ( session * Session ) Send ( tags map [ string ] string , prefix string , command string , params ... string ) ( err error ) {
2019-03-07 02:31:46 -05:00
msg := ircmsg . MakeMessage ( tags , prefix , command , params ... )
2019-04-12 00:08:46 -04:00
if session . capabilities . Has ( caps . ServerTime ) && ! msg . HasTag ( "time" ) {
2019-03-07 02:31:46 -05:00
msg . SetTag ( "time" , time . Now ( ) . UTC ( ) . Format ( IRCv3TimestampFormat ) )
}
2019-04-12 00:08:46 -04:00
return session . SendRawMessage ( msg , false )
2018-11-26 05:23:27 -05:00
}
2016-06-19 10:01:30 +10:00
// Notice sends the client a notice from the server.
func ( client * Client ) Notice ( text string ) {
2019-04-12 00:08:46 -04:00
client . Send ( nil , client . server . name , "NOTICE" , client . Nick ( ) , text )
2014-02-16 17:23:47 -08:00
}
2017-10-22 19:50:16 -04:00
func ( client * Client ) addChannel ( channel * Channel ) {
client . stateMutex . Lock ( )
client . channels [ channel ] = true
client . stateMutex . Unlock ( )
}
func ( client * Client ) removeChannel ( channel * Channel ) {
client . stateMutex . Lock ( )
delete ( client . channels , channel )
client . stateMutex . Unlock ( )
}
2018-11-26 05:23:27 -05:00
2018-12-23 13:25:02 -05:00
// Records that the client has been invited to join an invite-only channel
func ( client * Client ) Invite ( casefoldedChannel string ) {
client . stateMutex . Lock ( )
defer client . stateMutex . Unlock ( )
if client . invitedTo == nil {
client . invitedTo = make ( map [ string ] bool )
}
client . invitedTo [ casefoldedChannel ] = true
}
// Checks that the client was invited to join a given channel
func ( client * Client ) CheckInvited ( casefoldedChannel string ) ( invited bool ) {
client . stateMutex . Lock ( )
defer client . stateMutex . Unlock ( )
invited = client . invitedTo [ casefoldedChannel ]
// joining an invited channel "uses up" your invite, so you can't rejoin on kick
delete ( client . invitedTo , casefoldedChannel )
return
}