From 1256e73d90c43803fdab97944d7a61ffbc1648f2 Mon Sep 17 00:00:00 2001 From: William Boman Date: Thu, 30 Jun 2016 03:17:42 +0200 Subject: [PATCH 1/2] src/client: pass the updated token as argument to callback This also fixes an issue where the token would not be stored in the user file. --- src/client.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/client.js b/src/client.js index fa7fbbff..fbee8975 100644 --- a/src/client.js +++ b/src/client.js @@ -69,8 +69,8 @@ function Client(manager, name, config) { if (config) { if (!config.token) { - client.updateToken(function() { - client.manager.updateUser(client.name, {token: config.token}); + client.updateToken(function(token) { + client.manager.updateUser(client.name, {token: token}); }); } @@ -245,17 +245,16 @@ Client.prototype.updateToken = function(callback) { var client = this; crypto.randomBytes(48, function(err, buf) { - client.config.token = buf.toString("hex"); - callback(); + callback(client.config.token = buf.toString("hex")); }); }; Client.prototype.setPassword = function(hash, callback) { var client = this; - client.updateToken(function() { + client.updateToken(function(token) { client.manager.updateUser(client.name, { - token: client.config.token, + token: token, password: hash }); From 32b46bb32d8f21bf381bbf1f6f9ae0bc8feb6239 Mon Sep 17 00:00:00 2001 From: William Boman Date: Thu, 30 Jun 2016 15:06:07 +0200 Subject: [PATCH 2/2] src/client: make sure config is always an object --- src/client.js | 34 ++++++++++++++++++---------------- src/server.js | 2 +- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/client.js b/src/client.js index fbee8975..11ef2b37 100644 --- a/src/client.js +++ b/src/client.js @@ -55,6 +55,9 @@ var inputs = [ }, {}); function Client(manager, name, config) { + if (typeof config !== "object") { + config = {}; + } _.merge(this, { activeChannel: -1, config: config, @@ -67,22 +70,22 @@ function Client(manager, name, config) { var client = this; - if (config) { - if (!config.token) { - client.updateToken(function(token) { - client.manager.updateUser(client.name, {token: token}); - }); - } - - var delay = 0; - (config.networks || []).forEach(function(n) { - setTimeout(function() { - client.connect(n); - }, delay); - delay += 1000; + if (client.name && !client.config.token) { + client.updateToken(function(token) { + client.manager.updateUser(client.name, {token: token}); }); + } - log.info("User '" + name + "' loaded"); + var delay = 0; + (client.config.networks || []).forEach(function(n) { + setTimeout(function() { + client.connect(n); + }, delay); + delay += 1000; + }); + + if (client.name) { + log.info("User '" + client.name + "' loaded"); } } @@ -90,8 +93,7 @@ Client.prototype.emit = function(event, data) { if (this.sockets !== null) { this.sockets.in(this.id).emit(event, data); } - var config = this.config || {}; - if (config.log === true) { + if (this.config.log === true) { if (event === "msg") { var target = this.find(data.chan); if (target) { diff --git a/src/server.js b/src/server.js index cac09f17..01afe3dc 100644 --- a/src/server.js +++ b/src/server.js @@ -196,7 +196,7 @@ function init(socket, client) { socket.emit("init", { active: client.activeChannel, networks: client.networks, - token: client.config ? client.config.token : null + token: client.config.token || null }); } }