hardlounge/src/clientManager.js

144 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-09-24 22:23:54 +00:00
var moment = require("moment");
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];
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-03 23:33:44 +00:00
Helper.HOME + "/users/" + name + "/user.json",
2014-08-26 18:00:12 +00:00
"utf-8"
);
2014-08-14 01:51:54 +00:00
json = JSON.parse(json);
} catch(e) {
console.log(e);
return;
}
2014-09-24 22:23:54 +00:00
if (!json) {
return;
}
if (!this.findClient(name)) {
this.clients.push(new Client(
this.sockets,
name,
json
));
2014-09-30 20:20:36 +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-08-26 18:00:12 +00:00
users = fs.readdirSync(path);
2014-08-13 23:43:11 +00:00
} catch(e) {
console.log(e);
return;
}
2014-08-14 17:25:22 +00:00
users = _.without(
users,
"example"
);
2014-08-13 23:43:11 +00:00
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-04 22:05:19 +00:00
var path = Helper.HOME + "/users/" + name;
2014-08-14 01:51:54 +00:00
var user = {
user: name,
password: password || "",
networks: []
};
2014-08-13 23:43:11 +00:00
fs.mkdirSync(path);
2014-08-14 01:51:54 +00:00
fs.writeFileSync(
path + "/user.json",
2014-08-18 23:54:14 +00:00
JSON.stringify(user, null, " "),
{mode: "0777"}
2014-08-14 01:51:54 +00:00
);
2014-08-13 23:43:11 +00:00
} catch(e) {
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-03 23:33:44 +00:00
var path = Helper.HOME + "/users/" + name;
2014-08-13 23:43:11 +00:00
fs.unlinkSync(path + "/user.json");
fs.rmdirSync(path);
} catch(e) {
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
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);
});
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);
};