2016-10-09 19:14:02 +00:00
"use strict" ;
2014-09-13 21:29:45 +00:00
var _ = require ( "lodash" ) ;
2016-12-11 08:29:09 +00:00
var colors = require ( "colors/safe" ) ;
2016-10-09 19:15:20 +00:00
var pkg = require ( "../package.json" ) ;
2014-09-16 20:06:13 +00:00
var Chan = require ( "./models/chan" ) ;
2014-09-15 21:13:03 +00:00
var crypto = require ( "crypto" ) ;
2014-09-13 21:29:45 +00:00
var Msg = require ( "./models/msg" ) ;
var Network = require ( "./models/network" ) ;
2016-03-07 21:09:42 +00:00
var ircFramework = require ( "irc-framework" ) ;
2014-10-01 15:59:35 +00:00
var Helper = require ( "./helper" ) ;
2017-06-21 07:58:29 +00:00
const UAParser = require ( "ua-parser-js" ) ;
2014-09-13 21:29:45 +00:00
module . exports = Client ;
var id = 0 ;
var events = [
2017-07-10 16:01:20 +00:00
"away" ,
2016-03-08 18:50:48 +00:00
"connection" ,
2016-04-24 15:12:54 +00:00
"unhandled" ,
2017-04-22 12:51:21 +00:00
"banlist" ,
2014-09-13 21:29:45 +00:00
"ctcp" ,
"error" ,
2016-02-12 11:24:13 +00:00
"invite" ,
2014-09-13 21:29:45 +00:00
"join" ,
"kick" ,
"mode" ,
"motd" ,
"message" ,
"names" ,
"nick" ,
"part" ,
"quit" ,
"topic" ,
"welcome" ,
2016-03-09 20:04:07 +00:00
"list" ,
2017-11-15 06:35:15 +00:00
"whois" ,
2014-09-13 21:29:45 +00:00
] ;
var inputs = [
2017-04-24 10:40:53 +00:00
"ban" ,
2016-03-27 15:57:59 +00:00
"ctcp" ,
2016-03-06 09:24:56 +00:00
"msg" ,
"part" ,
2017-08-23 23:08:44 +00:00
"rejoin" ,
2014-09-13 21:29:45 +00:00
"action" ,
2016-11-19 08:24:39 +00:00
"away" ,
2014-09-13 21:29:45 +00:00
"connect" ,
2016-04-14 08:56:02 +00:00
"disconnect" ,
2014-09-13 21:29:45 +00:00
"invite" ,
"kick" ,
"mode" ,
2016-10-01 17:04:03 +00:00
"nick" ,
2014-09-13 21:29:45 +00:00
"notice" ,
2016-03-24 20:40:36 +00:00
"query" ,
2014-09-13 21:29:45 +00:00
"quit" ,
"raw" ,
"topic" ,
2016-05-29 02:07:34 +00:00
"list" ,
2017-11-15 06:35:15 +00:00
"whois" ,
2016-03-14 04:21:42 +00:00
] . reduce ( function ( plugins , name ) {
var path = "./plugins/inputs/" + name ;
var plugin = require ( path ) ;
2017-04-08 12:34:31 +00:00
plugin . commands . forEach ( ( command ) => plugins [ command ] = plugin ) ;
2016-03-14 04:21:42 +00:00
return plugins ;
} , { } ) ;
2014-09-13 21:29:45 +00:00
2016-02-17 00:14:43 +00:00
function Client ( manager , name , config ) {
2016-06-30 13:06:07 +00:00
if ( typeof config !== "object" ) {
config = { } ;
}
2017-04-28 15:58:14 +00:00
2014-09-13 21:29:45 +00:00
_ . merge ( this , {
2017-04-28 15:58:14 +00:00
awayMessage : config . awayMessage || "" ,
2016-09-25 06:41:10 +00:00
lastActiveChannel : - 1 ,
attachedClients : { } ,
2014-09-13 21:29:45 +00:00
config : config ,
id : id ++ ,
2014-09-16 19:47:01 +00:00
name : name ,
2014-09-13 21:29:45 +00:00
networks : [ ] ,
2016-02-17 00:14:43 +00:00
sockets : manager . sockets ,
2017-11-15 06:35:15 +00:00
manager : manager ,
2014-09-13 21:29:45 +00:00
} ) ;
2016-05-31 21:28:31 +00:00
2014-09-15 21:13:03 +00:00
var client = this ;
2016-05-31 21:28:31 +00:00
2016-06-30 13:06:07 +00:00
var delay = 0 ;
2017-04-08 12:34:31 +00:00
( client . config . networks || [ ] ) . forEach ( ( n ) => {
2016-06-30 13:06:07 +00:00
setTimeout ( function ( ) {
client . connect ( n ) ;
} , delay ) ;
delay += 1000 ;
} ) ;
2016-04-16 11:32:38 +00:00
2017-06-21 07:58:29 +00:00
if ( typeof client . config . sessions !== "object" ) {
client . config . sessions = { } ;
}
2017-07-10 19:47:03 +00:00
_ . forOwn ( client . config . sessions , ( session ) => {
if ( session . pushSubscription ) {
this . registerPushSubscription ( session , session . pushSubscription , true ) ;
}
} ) ;
2016-06-30 13:06:07 +00:00
if ( client . name ) {
2016-12-11 08:29:09 +00:00
log . info ( ` User ${ colors . bold ( client . name ) } loaded ` ) ;
2016-06-19 08:01:50 +00:00
}
2014-09-13 21:29:45 +00:00
}
2017-07-10 19:47:03 +00:00
Client . prototype . isRegistered = function ( ) {
return this . name . length > 0 ;
} ;
2014-09-13 21:29:45 +00:00
Client . prototype . emit = function ( event , data ) {
if ( this . sockets !== null ) {
this . sockets . in ( this . id ) . emit ( event , data ) ;
}
2014-09-13 17:18:42 +00:00
} ;
2014-09-13 21:29:45 +00:00
2016-10-09 08:54:44 +00:00
Client . prototype . find = function ( channelId ) {
2014-09-13 21:29:45 +00:00
var network = null ;
var chan = null ;
for ( var i in this . networks ) {
var n = this . networks [ i ] ;
2016-10-09 08:54:44 +00:00
chan = _ . find ( n . channels , { id : channelId } ) ;
2014-09-13 21:29:45 +00:00
if ( chan ) {
network = n ;
break ;
}
}
if ( network && chan ) {
return {
network : network ,
2017-11-15 06:35:15 +00:00
chan : chan ,
2014-09-13 21:29:45 +00:00
} ;
}
2016-10-09 08:54:44 +00:00
return false ;
2014-09-13 21:29:45 +00:00
} ;
Client . prototype . connect = function ( args ) {
2016-06-08 09:26:24 +00:00
var config = Helper . config ;
2014-09-13 21:29:45 +00:00
var client = this ;
2016-02-21 12:02:35 +00:00
2016-03-07 21:09:42 +00:00
var nick = args . nick || "lounge-user" ;
2016-04-03 05:12:49 +00:00
var webirc = null ;
2016-06-17 10:46:15 +00:00
var channels = [ ] ;
2016-06-19 17:12:42 +00:00
if ( args . channels ) {
var badName = false ;
2017-04-08 12:34:31 +00:00
args . channels . forEach ( ( chan ) => {
2016-06-19 17:12:42 +00:00
if ( ! chan . name ) {
badName = true ;
return ;
}
2016-06-17 10:46:15 +00:00
channels . push ( new Chan ( {
2017-04-01 08:33:17 +00:00
name : chan . name ,
key : chan . key || "" ,
2016-06-17 10:46:15 +00:00
} ) ) ;
} ) ;
2016-06-19 17:12:42 +00:00
if ( badName && client . name ) {
log . warn ( "User '" + client . name + "' on network '" + args . name + "' has an invalid channel which has been ignored" ) ;
}
// `join` is kept for backwards compatibility when updating from versions <2.0
// also used by the "connect" window
} else if ( args . join ) {
channels = args . join
2016-10-09 08:54:44 +00:00
. replace ( /,/g , " " )
2016-06-19 17:12:42 +00:00
. split ( /\s+/g )
. map ( function ( chan ) {
return new Chan ( {
2017-11-15 06:35:15 +00:00
name : chan ,
2016-06-19 17:12:42 +00:00
} ) ;
} ) ;
2016-06-17 10:46:15 +00:00
}
2016-03-07 21:09:42 +00:00
2016-11-19 20:23:51 +00:00
args . ip = args . ip || ( client . config && client . config . ip ) || client . ip ;
args . hostname = args . hostname || ( client . config && client . config . hostname ) || client . hostname ;
2016-03-07 21:09:42 +00:00
var network = new Network ( {
2017-07-06 12:00:53 +00:00
name : args . name || ( config . displayNetwork ? "" : config . defaults . name ) || "" ,
2016-03-07 21:09:42 +00:00
host : args . host || "" ,
port : parseInt ( args . port , 10 ) || ( args . tls ? 6697 : 6667 ) ,
tls : ! ! args . tls ,
password : args . password ,
username : args . username || nick . replace ( /[^a-zA-Z0-9]/g , "" ) ,
realname : args . realname || "The Lounge User" ,
2016-04-03 05:12:49 +00:00
commands : args . commands ,
ip : args . ip ,
hostname : args . hostname ,
2016-06-17 10:46:15 +00:00
channels : channels ,
2016-03-07 21:09:42 +00:00
} ) ;
2016-05-12 11:15:38 +00:00
network . setNick ( nick ) ;
2016-03-07 21:09:42 +00:00
client . networks . push ( network ) ;
client . emit ( "network" , {
2017-11-29 19:54:09 +00:00
networks : [ network . getFilteredClone ( this . lastActiveChannel , - 1 ) ] ,
2016-03-07 21:09:42 +00:00
} ) ;
2016-02-21 12:02:35 +00:00
if ( config . lockNetwork ) {
// This check is needed to prevent invalid user configurations
if ( args . host && args . host . length > 0 && args . host !== config . defaults . host ) {
2016-04-19 10:20:18 +00:00
network . channels [ 0 ] . pushMessage ( client , new Msg ( {
type : Msg . Type . ERROR ,
2017-11-15 06:35:15 +00:00
text : "Hostname you specified is not allowed." ,
2016-09-25 06:41:10 +00:00
} ) , true ) ;
2016-02-21 12:02:35 +00:00
return ;
}
2016-03-07 21:09:42 +00:00
network . host = config . defaults . host ;
network . port = config . defaults . port ;
network . tls = config . defaults . tls ;
2016-02-21 12:02:35 +00:00
}
2016-03-07 21:09:42 +00:00
if ( network . host . length === 0 ) {
2016-04-19 10:20:18 +00:00
network . channels [ 0 ] . pushMessage ( client , new Msg ( {
type : Msg . Type . ERROR ,
2017-11-15 06:35:15 +00:00
text : "You must specify a hostname to connect." ,
2016-09-25 06:41:10 +00:00
} ) , true ) ;
2016-02-21 12:02:35 +00:00
return ;
}
2016-05-02 05:11:57 +00:00
if ( config . webirc && network . host in config . webirc ) {
2016-11-19 20:23:51 +00:00
if ( ! args . hostname ) {
args . hostname = args . ip ;
}
2016-04-03 05:12:49 +00:00
if ( args . ip ) {
if ( config . webirc [ network . host ] instanceof Function ) {
webirc = config . webirc [ network . host ] ( client , args ) ;
} else {
webirc = {
password : config . webirc [ network . host ] ,
2016-10-09 19:15:20 +00:00
username : pkg . name ,
2016-04-03 05:12:49 +00:00
address : args . ip ,
2017-11-15 06:35:15 +00:00
hostname : args . hostname ,
2016-04-03 05:12:49 +00:00
} ;
}
} else {
log . warn ( "Cannot find a valid WEBIRC configuration for " + nick
+ "!" + network . username + "@" + network . host ) ;
}
}
2017-02-02 20:52:37 +00:00
network . irc = new ircFramework . Client ( {
2016-09-05 12:37:27 +00:00
version : pkg . name + " " + Helper . getVersion ( ) + " -- " + pkg . homepage ,
2016-03-07 21:09:42 +00:00
host : network . host ,
port : network . port ,
nick : nick ,
2016-11-19 20:23:51 +00:00
username : config . useHexIp ? Helper . ip2hex ( args . ip ) : network . username ,
2016-03-07 21:09:42 +00:00
gecos : network . realname ,
password : network . password ,
tls : network . tls ,
localAddress : config . bind ,
rejectUnauthorized : false ,
2017-02-02 20:52:37 +00:00
enable _echomessage : true ,
2016-04-13 07:10:44 +00:00
auto _reconnect : true ,
2016-07-02 18:45:41 +00:00
auto _reconnect _wait : 10000 + Math . floor ( Math . random ( ) * 1000 ) , // If multiple users are connected to the same network, randomize their reconnections a little
auto _reconnect _max _retries : 360 , // At least one hour (plus timeouts) worth of reconnections
2016-04-03 05:12:49 +00:00
webirc : webirc ,
2014-09-13 21:29:45 +00:00
} ) ;
2016-11-19 20:23:51 +00:00
2017-02-02 20:52:37 +00:00
network . irc . requestCap ( [
"znc.in/self-message" , // Legacy echo-message for ZNc
] ) ;
2017-04-08 12:34:31 +00:00
events . forEach ( ( plugin ) => {
2017-02-02 20:52:37 +00:00
var path = "./plugins/irc-events/" + plugin ;
require ( path ) . apply ( client , [
network . irc ,
2017-11-15 06:35:15 +00:00
network ,
2017-02-02 20:52:37 +00:00
] ) ;
} ) ;
network . irc . connect ( ) ;
2016-11-19 20:23:51 +00:00
client . save ( ) ;
2014-09-13 21:29:45 +00:00
} ;
2017-06-21 07:58:29 +00:00
Client . prototype . generateToken = function ( callback ) {
crypto . randomBytes ( 48 , ( err , buf ) => {
2016-10-09 08:54:44 +00:00
if ( err ) {
throw err ;
}
2017-06-21 07:58:29 +00:00
callback ( buf . toString ( "hex" ) ) ;
2016-05-31 21:28:31 +00:00
} ) ;
} ;
2017-06-21 07:58:29 +00:00
Client . prototype . updateSession = function ( token , ip , request ) {
const client = this ;
const agent = UAParser ( request . headers [ "user-agent" ] || "" ) ;
let friendlyAgent = "" ;
2017-08-13 18:37:12 +00:00
if ( agent . browser . name ) {
2017-06-21 07:58:29 +00:00
friendlyAgent = ` ${ agent . browser . name } ${ agent . browser . major } ` ;
} else {
friendlyAgent = "Unknown browser" ;
}
2017-08-13 18:37:12 +00:00
if ( agent . os . name ) {
2017-08-08 19:46:55 +00:00
friendlyAgent += ` on ${ agent . os . name } ${ agent . os . version } ` ;
2017-06-21 07:58:29 +00:00
}
2017-09-17 08:09:19 +00:00
client . config . sessions [ token ] = _ . assign ( client . config . sessions [ token ] , {
2017-06-21 07:58:29 +00:00
lastUse : Date . now ( ) ,
ip : ip ,
agent : friendlyAgent ,
2017-09-17 08:09:19 +00:00
} ) ;
client . manager . updateUser ( client . name , {
2017-11-15 06:35:15 +00:00
sessions : client . config . sessions ,
2017-09-17 08:09:19 +00:00
} ) ;
2017-06-21 07:58:29 +00:00
} ;
2016-05-31 21:28:31 +00:00
Client . prototype . setPassword = function ( hash , callback ) {
var client = this ;
2017-06-21 07:58:29 +00:00
client . manager . updateUser ( client . name , {
2017-11-15 06:35:15 +00:00
password : hash ,
2017-06-21 07:58:29 +00:00
} , function ( err ) {
if ( err ) {
return callback ( false ) ;
}
2016-05-31 21:28:31 +00:00
2017-06-21 07:58:29 +00:00
client . config . password = hash ;
return callback ( true ) ;
2016-05-31 21:28:31 +00:00
} ) ;
2016-02-17 00:14:43 +00:00
} ;
2014-09-13 21:29:45 +00:00
Client . prototype . input = function ( data ) {
var client = this ;
2017-04-08 12:34:31 +00:00
data . text . split ( "\n" ) . forEach ( ( line ) => {
2016-06-05 02:48:41 +00:00
data . text = line ;
client . inputLine ( data ) ;
} ) ;
} ;
Client . prototype . inputLine = function ( data ) {
var client = this ;
2016-05-11 01:19:09 +00:00
var text = data . text ;
2014-09-13 21:29:45 +00:00
var target = client . find ( data . target ) ;
2016-09-25 06:41:10 +00:00
if ( ! target ) {
return ;
}
// Sending a message to a channel is higher priority than merely opening one
// so that reloading the page will open this channel
this . lastActiveChannel = target . chan . id ;
2016-03-06 09:24:56 +00:00
// This is either a normal message or a command escaped with a leading '/'
if ( text . charAt ( 0 ) !== "/" || text . charAt ( 1 ) === "/" ) {
2017-03-11 18:09:37 +00:00
if ( target . chan . type === Chan . Type . LOBBY ) {
target . chan . pushMessage ( this , new Msg ( {
type : Msg . Type . ERROR ,
2017-11-15 06:35:15 +00:00
text : "Messages can not be sent to lobbies." ,
2017-03-11 18:09:37 +00:00
} ) ) ;
return ;
}
2016-03-06 09:24:56 +00:00
text = "say " + text . replace ( /^\// , "" ) ;
} else {
text = text . substr ( 1 ) ;
2014-09-13 21:29:45 +00:00
}
2016-03-06 09:24:56 +00:00
2014-09-13 21:29:45 +00:00
var args = text . split ( " " ) ;
2016-03-06 09:24:56 +00:00
var cmd = args . shift ( ) . toLowerCase ( ) ;
2016-04-03 09:58:59 +00:00
var irc = target . network . irc ;
var connected = irc && irc . connection && irc . connection . connected ;
2016-03-14 04:21:42 +00:00
if ( cmd in inputs ) {
2016-04-03 09:58:59 +00:00
var plugin = inputs [ cmd ] ;
if ( connected || plugin . allowDisconnected ) {
connected = true ;
plugin . input . apply ( client , [ target . network , target . chan , cmd , args ] ) ;
}
} else if ( connected ) {
irc . raw ( text ) ;
}
if ( ! connected ) {
2016-04-19 10:20:18 +00:00
target . chan . pushMessage ( this , new Msg ( {
type : Msg . Type . ERROR ,
2017-11-15 06:35:15 +00:00
text : "You are not connected to the IRC network, unable to send your command." ,
2016-04-19 10:20:18 +00:00
} ) ) ;
2016-03-06 09:24:56 +00:00
}
2014-09-13 21:29:45 +00:00
} ;
Client . prototype . more = function ( data ) {
2017-08-23 09:32:59 +00:00
const client = this ;
const target = client . find ( data . target ) ;
2014-09-13 21:29:45 +00:00
if ( ! target ) {
return ;
}
2017-08-23 09:32:59 +00:00
const chan = target . chan ;
2017-09-12 13:05:40 +00:00
let messages = [ ] ;
let index = 0 ;
2017-08-23 09:32:59 +00:00
2017-09-12 13:05:40 +00:00
// If client requests -1, send last 100 messages
if ( data . lastId < 0 ) {
index = chan . messages . length ;
} else {
index = chan . messages . findIndex ( ( val ) => val . id === data . lastId ) ;
}
// If requested id is not found, an empty array will be sent
if ( index > 0 ) {
messages = chan . messages . slice ( Math . max ( 0 , index - 100 ) , index ) ;
}
2017-08-23 09:32:59 +00:00
2014-09-13 21:29:45 +00:00
client . emit ( "more" , {
chan : chan . id ,
2017-11-15 06:35:15 +00:00
messages : messages ,
2014-09-13 21:29:45 +00:00
} ) ;
} ;
2016-12-21 10:38:50 +00:00
Client . prototype . open = function ( socketId , target ) {
// Opening a window like settings
if ( target === null ) {
2017-07-10 19:47:03 +00:00
this . attachedClients [ socketId ] . openChannel = - 1 ;
2016-12-21 10:38:50 +00:00
return ;
}
target = this . find ( target ) ;
2016-09-25 06:41:10 +00:00
if ( ! target ) {
return ;
2014-09-21 16:46:43 +00:00
}
2016-09-25 06:41:10 +00:00
target . chan . firstUnread = 0 ;
target . chan . unread = 0 ;
2017-09-03 15:57:07 +00:00
target . chan . highlight = 0 ;
2016-09-25 06:41:10 +00:00
2017-07-10 19:47:03 +00:00
this . attachedClients [ socketId ] . openChannel = target . chan . id ;
2016-09-25 06:41:10 +00:00
this . lastActiveChannel = target . chan . id ;
this . emit ( "open" , target . chan . id ) ;
2014-09-21 16:46:43 +00:00
} ;
2014-09-24 19:42:36 +00:00
Client . prototype . sort = function ( data ) {
2017-04-22 10:25:36 +00:00
const order = data . order ;
2014-09-24 19:42:36 +00:00
2017-04-22 10:25:36 +00:00
if ( ! _ . isArray ( order ) ) {
return ;
}
2014-09-24 19:42:36 +00:00
2017-04-22 10:25:36 +00:00
switch ( data . type ) {
2014-09-24 19:42:36 +00:00
case "networks" :
2017-04-08 12:34:31 +00:00
this . networks . sort ( ( a , b ) => order . indexOf ( a . id ) - order . indexOf ( b . id ) ) ;
2017-04-22 10:25:36 +00:00
// Sync order to connected clients
2017-04-08 12:34:31 +00:00
this . emit ( "sync_sort" , { order : this . networks . map ( ( obj ) => obj . id ) , type : data . type , target : data . target } ) ;
2017-04-22 10:25:36 +00:00
2014-09-24 19:42:36 +00:00
break ;
case "channels" :
2017-04-22 10:25:36 +00:00
var network = _ . find ( this . networks , { id : data . target } ) ;
2014-09-24 19:42:36 +00:00
if ( ! network ) {
return ;
}
2017-04-22 10:25:36 +00:00
2017-04-08 12:34:31 +00:00
network . channels . sort ( ( a , b ) => order . indexOf ( a . id ) - order . indexOf ( b . id ) ) ;
2017-04-22 10:25:36 +00:00
// Sync order to connected clients
2017-04-08 12:34:31 +00:00
this . emit ( "sync_sort" , { order : network . channels . map ( ( obj ) => obj . id ) , type : data . type , target : data . target } ) ;
2017-04-22 10:25:36 +00:00
2014-09-24 19:42:36 +00:00
break ;
}
2016-06-12 10:02:37 +00:00
2017-04-22 10:25:36 +00:00
this . save ( ) ;
2014-09-24 19:42:36 +00:00
} ;
2016-02-17 02:29:44 +00:00
Client . prototype . names = function ( data ) {
var client = this ;
var target = client . find ( data . target ) ;
if ( ! target ) {
return ;
}
client . emit ( "names" , {
2016-03-23 10:15:44 +00:00
id : target . chan . id ,
2017-11-16 20:32:03 +00:00
users : target . chan . getSortedUsers ( target . network . irc ) ,
2016-02-17 02:29:44 +00:00
} ) ;
} ;
2017-08-30 17:26:45 +00:00
Client . prototype . quit = function ( signOut ) {
2017-08-29 12:43:52 +00:00
const sockets = this . sockets . sockets ;
const room = sockets . adapter . rooms [ this . id ] ;
if ( room && room . sockets ) {
for ( const user in room . sockets ) {
const socket = sockets . connected [ user ] ;
if ( socket ) {
2017-08-30 17:26:45 +00:00
if ( signOut ) {
socket . emit ( "sign-out" ) ;
}
2017-08-29 12:43:52 +00:00
socket . disconnect ( ) ;
}
2014-09-29 15:49:38 +00:00
}
}
2017-08-29 12:43:52 +00:00
2017-04-08 12:34:31 +00:00
this . networks . forEach ( ( network ) => {
2016-04-03 09:03:09 +00:00
if ( network . irc ) {
2017-08-18 19:04:16 +00:00
network . irc . quit ( Helper . config . leaveMessage ) ;
2016-04-03 09:03:09 +00:00
}
2017-08-11 12:02:58 +00:00
network . destroy ( ) ;
2014-09-13 21:29:45 +00:00
} ) ;
2014-09-13 17:18:42 +00:00
} ;
2014-10-11 20:44:56 +00:00
2017-07-10 19:47:03 +00:00
Client . prototype . clientAttach = function ( socketId , token ) {
2016-11-19 20:34:05 +00:00
var client = this ;
var save = false ;
2016-12-18 09:24:50 +00:00
if ( client . awayMessage && _ . size ( client . attachedClients ) === 0 ) {
client . networks . forEach ( function ( network ) {
// Only remove away on client attachment if
// there is no away message on this network
if ( ! network . awayMessage ) {
network . irc . raw ( "AWAY" ) ;
}
} ) ;
}
2017-07-10 19:47:03 +00:00
client . attachedClients [ socketId ] = {
token : token ,
2017-11-15 06:35:15 +00:00
openChannel : client . lastActiveChannel ,
2017-07-10 19:47:03 +00:00
} ;
2017-04-28 15:58:14 +00:00
2016-11-19 20:34:05 +00:00
// Update old networks to store ip and hostmask
2017-04-08 12:34:31 +00:00
client . networks . forEach ( ( network ) => {
2016-11-19 20:34:05 +00:00
if ( ! network . ip ) {
save = true ;
network . ip = ( client . config && client . config . ip ) || client . ip ;
}
if ( ! network . hostname ) {
var hostmask = ( client . config && client . config . hostname ) || client . hostname ;
if ( hostmask ) {
save = true ;
network . hostmask = hostmask ;
}
}
} ) ;
if ( save ) {
client . save ( ) ;
}
2016-09-25 06:41:10 +00:00
} ;
Client . prototype . clientDetach = function ( socketId ) {
2016-12-18 09:24:50 +00:00
const client = this ;
2016-09-25 06:41:10 +00:00
delete this . attachedClients [ socketId ] ;
2016-12-18 09:24:50 +00:00
if ( client . awayMessage && _ . size ( client . attachedClients ) === 0 ) {
client . networks . forEach ( function ( network ) {
// Only set away on client deattachment if
// there is no away message on this network
if ( ! network . awayMessage ) {
network . irc . raw ( "AWAY" , client . awayMessage ) ;
}
} ) ;
}
2016-09-25 06:41:10 +00:00
} ;
2017-07-10 19:47:03 +00:00
Client . prototype . registerPushSubscription = function ( session , subscription , noSave ) {
if ( ! _ . isPlainObject ( subscription ) || ! _ . isPlainObject ( subscription . keys )
|| typeof subscription . endpoint !== "string" || ! /^https?:\/\// . test ( subscription . endpoint )
|| typeof subscription . keys . p256dh !== "string" || typeof subscription . keys . auth !== "string" ) {
session . pushSubscription = null ;
return ;
}
const data = {
endpoint : subscription . endpoint ,
keys : {
p256dh : subscription . keys . p256dh ,
2017-11-15 06:35:15 +00:00
auth : subscription . keys . auth ,
} ,
2017-07-10 19:47:03 +00:00
} ;
session . pushSubscription = data ;
if ( ! noSave ) {
this . manager . updateUser ( this . name , {
2017-11-15 06:35:15 +00:00
sessions : this . config . sessions ,
2017-07-10 19:47:03 +00:00
} ) ;
}
return data ;
} ;
Client . prototype . unregisterPushSubscription = function ( token ) {
this . config . sessions [ token ] . pushSubscription = null ;
this . manager . updateUser ( this . name , {
2017-11-15 06:35:15 +00:00
sessions : this . config . sessions ,
2017-07-10 19:47:03 +00:00
} ) ;
} ;
2016-11-19 21:00:54 +00:00
Client . prototype . save = _ . debounce ( function SaveClient ( ) {
2016-06-08 09:26:24 +00:00
if ( Helper . config . public ) {
2014-10-12 05:15:03 +00:00
return ;
}
2014-11-09 16:01:22 +00:00
2016-11-19 21:00:54 +00:00
const client = this ;
2017-04-08 12:34:31 +00:00
const json = { } ;
json . networks = this . networks . map ( ( n ) => n . export ( ) ) ;
2016-02-17 00:14:43 +00:00
client . manager . updateUser ( client . name , json ) ;
2016-11-19 21:00:54 +00:00
} , 1000 , { maxWait : 10000 } ) ;