hardlounge/src/clientManager.js

138 lines
2.6 KiB
JavaScript
Raw Normal View History

2014-08-14 17:25:22 +00:00
var _ = require("lodash");
2014-08-13 23:43:11 +00:00
var fs = require("fs");
2014-08-14 01:51:54 +00:00
var Client = require("./client");
2014-08-26 18:00:12 +00:00
var mkdirp = require("mkdirp");
var Helper = require("./helper");
2014-08-13 23:43:11 +00:00
module.exports = ClientManager;
function ClientManager() {
this.clients = [];
2014-08-13 23:43:11 +00:00
}
ClientManager.prototype.findClient = function(name) {
for (var i in this.clients) {
var client = this.clients[i];
2015-09-30 22:39:57 +00:00
if (client.name === name) {
return client;
}
}
return false;
};
2014-09-24 22:23:54 +00:00
ClientManager.prototype.loadUsers = function() {
2014-08-13 23:43:11 +00:00
var users = this.getUsers();
2014-08-14 01:51:54 +00:00
for (var i in users) {
2014-09-24 22:23:54 +00:00
this.loadUser(users[i]);
2014-08-14 01:51:54 +00:00
}
};
ClientManager.prototype.loadUser = function(name) {
try {
2014-08-26 18:00:12 +00:00
var json = fs.readFileSync(
2014-10-14 20:05:16 +00:00
Helper.HOME + "/users/" + name + ".json",
2014-08-26 18:00:12 +00:00
"utf-8"
);
2014-08-14 01:51:54 +00:00
json = JSON.parse(json);
2015-09-30 22:39:57 +00:00
} catch (e) {
2014-08-14 01:51:54 +00:00
console.log(e);
return;
}
2014-09-24 22:23:54 +00:00
if (!this.findClient(name)) {
this.clients.push(new Client(
this.sockets,
name,
json
));
2014-10-14 19:13:29 +00:00
console.log(
"User '" + name + "' loaded."
);
2014-09-24 22:23:54 +00:00
}
2014-08-13 23:43:11 +00:00
};
ClientManager.prototype.getUsers = function() {
var users = [];
2014-10-03 23:33:44 +00:00
var path = Helper.HOME + "/users";
2014-08-26 18:00:12 +00:00
mkdirp.sync(path);
2014-08-13 23:43:11 +00:00
try {
2014-10-14 22:24:15 +00:00
var files = fs.readdirSync(path);
files.forEach(function(file) {
if (file.indexOf(".json") !== -1) {
users.push(file.replace(".json", ""));
}
2014-10-14 20:05:16 +00:00
});
2015-09-30 22:39:57 +00:00
} catch (e) {
2014-08-13 23:43:11 +00:00
console.log(e);
return;
}
return users;
};
2014-08-14 01:51:54 +00:00
ClientManager.prototype.addUser = function(name, password) {
2014-08-13 23:43:11 +00:00
var users = this.getUsers();
if (users.indexOf(name) !== -1) {
2014-08-14 17:25:22 +00:00
return false;
2014-08-13 23:43:11 +00:00
}
try {
2014-10-14 20:05:16 +00:00
var path = Helper.HOME + "/users";
2014-08-14 01:51:54 +00:00
var user = {
user: name,
password: password || "",
2014-10-14 19:25:04 +00:00
log: false,
2014-08-14 01:51:54 +00:00
networks: []
};
2014-10-14 20:05:16 +00:00
mkdirp.sync(path);
2014-08-14 01:51:54 +00:00
fs.writeFileSync(
2014-10-14 20:05:16 +00:00
path + "/" + name + ".json",
2014-08-18 23:54:14 +00:00
JSON.stringify(user, null, " "),
{mode: "0777"}
2014-08-14 01:51:54 +00:00
);
2015-09-30 22:39:57 +00:00
} catch (e) {
2014-08-13 23:43:11 +00:00
throw e;
}
2014-08-14 17:25:22 +00:00
return true;
2014-08-13 23:43:11 +00:00
};
ClientManager.prototype.removeUser = function(name) {
var users = this.getUsers();
if (users.indexOf(name) === -1) {
2014-08-14 17:25:22 +00:00
return false;
2014-08-13 23:43:11 +00:00
}
try {
2014-10-14 20:05:16 +00:00
var path = Helper.HOME + "/users/" + name + ".json";
fs.unlinkSync(path);
2015-09-30 22:39:57 +00:00
} catch (e) {
2014-08-13 23:43:11 +00:00
throw e;
}
2014-08-14 17:25:22 +00:00
return true;
2014-08-13 23:43:11 +00:00
};
2014-09-24 22:23:54 +00:00
2015-09-30 22:39:57 +00:00
ClientManager.prototype.autoload = function(/* sockets */) {
var self = this;
2014-10-14 19:13:29 +00:00
setInterval(function() {
2016-02-14 17:09:51 +00:00
var loaded = _.map(
2014-10-14 19:13:29 +00:00
self.clients,
"name"
);
var added = _.difference(self.getUsers(), loaded);
_.each(added, function(name) {
self.loadUser(name);
});
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);
2014-09-24 22:23:54 +00:00
};