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-13 23:43:11 +00:00
|
|
|
|
|
|
|
module.exports = ClientManager;
|
|
|
|
|
|
|
|
function ClientManager() {
|
2014-08-14 16:35:37 +00:00
|
|
|
this.clients = [];
|
2014-08-13 23:43:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-14 16:35:37 +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-08-14 01:51:54 +00:00
|
|
|
ClientManager.prototype.loadUsers = function(sockets) {
|
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) {
|
|
|
|
if (name == "example") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var name = users[i];
|
|
|
|
var json = this.loadUser(name);
|
|
|
|
if (!json) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-08-14 16:35:37 +00:00
|
|
|
if (!this.findClient(name)) {
|
|
|
|
this.clients.push(new Client(
|
2014-08-14 01:51:54 +00:00
|
|
|
sockets,
|
|
|
|
json
|
2014-08-14 16:35:37 +00:00
|
|
|
));
|
2014-08-14 01:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ClientManager.prototype.loadUser = function(name) {
|
|
|
|
try {
|
|
|
|
var json = fs.readFileSync("users/" + name + "/user.json", "utf-8");
|
|
|
|
json = JSON.parse(json);
|
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return json;
|
2014-08-13 23:43:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ClientManager.prototype.getUsers = function() {
|
|
|
|
var users = [];
|
|
|
|
try {
|
|
|
|
users = fs.readdirSync("users/");
|
|
|
|
} catch(e) {
|
|
|
|
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) {
|
|
|
|
console.log("User '" + name + "' already exist.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
var path = "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",
|
|
|
|
JSON.stringify(user, null, " ")
|
|
|
|
);
|
2014-08-13 23:43:11 +00:00
|
|
|
} catch(e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
console.log(
|
|
|
|
"Added '" + name + "'."
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ClientManager.prototype.removeUser = function(name) {
|
|
|
|
var users = this.getUsers();
|
|
|
|
if (users.indexOf(name) === -1) {
|
|
|
|
console.log("User '" + name + "' doesn't exist.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
var path = "users/" + name;
|
|
|
|
fs.unlinkSync(path + "/user.json");
|
|
|
|
fs.rmdirSync(path);
|
|
|
|
} catch(e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
console.log(
|
|
|
|
"Removed '" + name + "'."
|
|
|
|
);
|
|
|
|
};
|