Added user loading

This commit is contained in:
Mattias Erming 2014-08-13 18:51:54 -07:00
parent c870dc8c15
commit 43b6310481
6 changed files with 64 additions and 49 deletions

3
.gitignore vendored
View File

@ -1,4 +1,3 @@
build/
node_modules/ node_modules/
npm-debug.log npm-debug.log
logs/ users/*

View File

@ -3,8 +3,8 @@
process.chdir(__dirname); process.chdir(__dirname);
var config = require("./config.json"); var config = require("./config.json");
var ClientManager= new require("./src/clientManager");
var program = require("commander"); var program = require("commander");
var ClientManager = require("./src/clientManager");
var shout = require("./src/server.js"); var shout = require("./src/server.js");
program program
@ -24,8 +24,8 @@ program
.action(function() { .action(function() {
var manager = new ClientManager(); var manager = new ClientManager();
var users = manager.getUsers(); var users = manager.getUsers();
for (var u in users) { for (var i = 0; i < users.length; i++) {
console.log(users[u]); console.log((i + 1) + " " + users[i]);
} }
}); });
@ -34,7 +34,18 @@ program
.description("Add a new user") .description("Add a new user")
.action(function(name) { .action(function(name) {
var manager = new ClientManager(); var manager = new ClientManager();
manager.addUser(name); require("read")({
prompt: "Password: "
}, function(err, password) {
if (err) {
console.log("");
return;
}
manager.addUser(
name,
password
);
});
}); });
program program

View File

@ -28,6 +28,7 @@
"connect": "~2.19.6", "connect": "~2.19.6",
"lodash": "~2.4.1", "lodash": "~2.4.1",
"moment": "~2.7.0", "moment": "~2.7.0",
"read": "^1.0.5",
"slate-irc": "~0.6.0", "slate-irc": "~0.6.0",
"socket.io": "~1.0.6" "socket.io": "~1.0.6"
}, },

View File

@ -1,17 +1,41 @@
var _ = require("lodash");
var fs = require("fs"); var fs = require("fs");
var Client = require("./client");
module.exports = ClientManager; module.exports = ClientManager;
function ClientManager() { function ClientManager() {
this.clients = []; this.clients = {};
} }
ClientManager.prototype.loadUsers = function() { ClientManager.prototype.loadUsers = function(sockets) {
var users = this.getUsers(); var users = this.getUsers();
_.each(users, function(user) { for (var i in users) {
console.log(user); if (name == "example") {
}); continue;
}
var name = users[i];
var json = this.loadUser(name);
if (!json) {
continue;
}
if (!this.clients[name]) {
this.clients[name] = new Client(
sockets,
json
);
}
}
};
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;
}; };
ClientManager.prototype.getUsers = function() { ClientManager.prototype.getUsers = function() {
@ -25,21 +49,27 @@ ClientManager.prototype.getUsers = function() {
return users; return users;
}; };
ClientManager.prototype.addUser = function(name) { ClientManager.prototype.addUser = function(name, password) {
var users = this.getUsers(); var users = this.getUsers();
if (users.indexOf(name) !== -1) { if (users.indexOf(name) !== -1) {
console.log("User '" + name + "' already exist."); console.log("User '" + name + "' already exist.");
return; return;
} }
try { try {
var path = "users/" + name; var path = "users/" + name;
var user = {
user: name,
password: password || "",
networks: []
};
fs.mkdirSync(path); fs.mkdirSync(path);
fs.writeFileSync(path + "/user.json", "{}"); fs.writeFileSync(
path + "/user.json",
JSON.stringify(user, null, " ")
);
} catch(e) { } catch(e) {
throw e; throw e;
} }
console.log( console.log(
"Added '" + name + "'." "Added '" + name + "'."
); );
@ -51,7 +81,6 @@ ClientManager.prototype.removeUser = function(name) {
console.log("User '" + name + "' doesn't exist."); console.log("User '" + name + "' doesn't exist.");
return; return;
} }
try { try {
var path = "users/" + name; var path = "users/" + name;
fs.unlinkSync(path + "/user.json"); fs.unlinkSync(path + "/user.json");
@ -59,7 +88,6 @@ ClientManager.prototype.removeUser = function(name) {
} catch(e) { } catch(e) {
throw e; throw e;
} }
console.log( console.log(
"Removed '" + name + "'." "Removed '" + name + "'."
); );

View File

@ -1,12 +1,13 @@
var _ = require("lodash"); var _ = require("lodash");
var Client = require("./client"); var Client = require("./client");
var ClientManager = require("./clientManager");
var config = require("../config.json"); var config = require("../config.json");
var fs = require("fs"); var fs = require("fs");
var http = require("connect"); var http = require("connect");
var io = require("socket.io"); var io = require("socket.io");
var sockets = null; var sockets = null;
var clients = []; var manager = new ClientManager();
var inputs = [ var inputs = [
"action", "action",
@ -46,34 +47,9 @@ module.exports = function(port) {
console.log("Press ctrl-c to stop"); console.log("Press ctrl-c to stop");
console.log(""); console.log("");
if (config.public) { if (!config.public) {
return; manager.loadUsers(sockets);
} }
fs.readdir("users/", function(err, files) {
if (err) {
console.log(err);
return;
}
_.each(files, function(file) {
fs.readFile("users/" + file + "/user.json", "utf-8", function(err, json) {
if (err) {
console.log(err);
return;
}
try {
json = JSON.parse(json);
} catch(e) {
console.log(e);
return;
}
clients.push(new Client(
sockets,
json
));
});
});
});
}; };
function index(req, res, next) { function index(req, res, next) {
@ -132,7 +108,7 @@ function auth(data) {
init(socket, client); init(socket, client);
} else { } else {
var success = 0; var success = 0;
_.each(clients, function(client) { _.each(manager.clients, function(client) {
if (client.config.name == data.name && client.config.password == data.password) { if (client.config.name == data.name && client.config.password == data.password) {
init(socket, client); init(socket, client);
success++; success++;

View File

@ -6,8 +6,8 @@
"host": "irc.rizon.net", "host": "irc.rizon.net",
"port": 6667, "port": 6667,
"nick": "example", "nick": "example",
"realname": "Example", "realname": "Example User",
"join": "#chan" "join": "#foo, #bar"
} }
] ]
} }