From 4d9b58ecd51111c6952cfb8720795b5144314462 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Wed, 24 Sep 2014 15:23:54 -0700 Subject: [PATCH] Autoload users --- client/js/shout.js | 6 ++++- config.js | 10 +++++++ package.json | 2 +- src/client.js | 3 +++ src/clientManager.js | 62 ++++++++++++++++++++++++++++++++++---------- src/server.js | 7 ++++- 6 files changed, 73 insertions(+), 17 deletions(-) diff --git a/client/js/shout.js b/client/js/shout.js index 46ee7f45..5a68ac58 100644 --- a/client/js/shout.js +++ b/client/js/shout.js @@ -63,7 +63,11 @@ $(function() { console.log(e); }); - socket.on("connect_error", function(e) { + socket.on("connect_error", function() { + refresh(); + }); + + socket.on("disconnect", function() { refresh(); }); diff --git a/config.js b/config.js index c3dcb791..4c7ca12d 100644 --- a/config.js +++ b/config.js @@ -44,6 +44,16 @@ module.exports = { home: "", // + // Autoload users + // + // When this setting is enabled, your 'users/' folder will be monitored. This is useful + // if you want to add/remove users while the server is running. + // + // @type boolean + // @default true + // + autoload: true, + // Enable debug mode. // This is only useful for development. // diff --git a/package.json b/package.json index 9c4d2cd0..f5874215 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "shout", "description": "The self-hosted web IRC client", - "version": "0.33.2", + "version": "0.34.0", "author": "Mattias Erming", "preferGlobal": true, "bin": { diff --git a/src/client.js b/src/client.js index beb45d0e..0eeac25f 100644 --- a/src/client.js +++ b/src/client.js @@ -283,6 +283,9 @@ Client.prototype.sort = function(data) { }; Client.prototype.quit = function() { + this.sockets.in(this.id).sockets.forEach(function(socket) { + socket.disconnect(true); + }); this.networks.forEach(function(network) { var irc = network.irc; if (network.connected) { diff --git a/src/clientManager.js b/src/clientManager.js index fa4f2781..18d4e186 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -3,6 +3,7 @@ var fs = require("fs"); var Client = require("./client"); var mkdirp = require("mkdirp"); var Helper = require("./helper"); +var moment = require("moment"); module.exports = ClientManager; @@ -20,21 +21,10 @@ ClientManager.prototype.findClient = function(name) { return false; }; -ClientManager.prototype.loadUsers = function(sockets) { +ClientManager.prototype.loadUsers = function() { var users = this.getUsers(); for (var i in users) { - var name = users[i]; - var json = this.loadUser(name); - if (!json) { - continue; - } - if (!this.findClient(name)) { - this.clients.push(new Client( - sockets, - name, - json - )); - } + this.loadUser(users[i]); } }; @@ -45,11 +35,20 @@ ClientManager.prototype.loadUser = function(name) { "utf-8" ); json = JSON.parse(json); - return json; } catch(e) { console.log(e); return; } + if (!json) { + return; + } + if (!this.findClient(name)) { + this.clients.push(new Client( + this.sockets, + name, + json + )); + } }; ClientManager.prototype.getUsers = function() { @@ -109,3 +108,38 @@ ClientManager.prototype.removeUser = function(name) { } return true; }; + +ClientManager.prototype.autoload = function(sockets) { + var self = this; + var loaded = ["erming"]; + setInterval(function() { + var loaded = _.pluck( + self.clients, + "name" + ); + + var added = _.difference(self.getUsers(), loaded); + _.each(added, function(name) { + self.loadUser(name); + console.log( + "User '" + name + "' loaded." + ); + }); + + var removed = _.difference(loaded, self.getUsers()); + _.each(removed, function(name) { + var client = _.find( + self.clients, { + name: name + } + ); + if (client) { + client.quit(); + self.clients = _.without(self.clients, client); + console.log( + "User '" + name + "' disconnected." + ); + } + }); + }, 1000); +}; diff --git a/src/server.js b/src/server.js index c3b0b81a..01a90117 100644 --- a/src/server.js +++ b/src/server.js @@ -31,13 +31,18 @@ module.exports = function(port, host, isPublic) { } }); + manager.sockets = sockets; + console.log(""); console.log("Shout is now running on http://" + config.host + ":" + config.port + "/"); console.log("Press ctrl-c to stop"); console.log(""); if (!config.public) { - manager.loadUsers(sockets); + manager.loadUsers(); + if (config.autoload) { + manager.autoload(); + } } };