Autoload users

This commit is contained in:
Mattias Erming 2014-09-24 15:23:54 -07:00
parent 8793551371
commit 4d9b58ecd5
6 changed files with 73 additions and 17 deletions

View file

@ -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();
});

View file

@ -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.
//

View file

@ -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": {

View file

@ -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) {

View file

@ -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);
};

View file

@ -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();
}
}
};