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/
npm-debug.log
logs/
users/*

View File

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

View File

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

View File

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

View File

@ -1,12 +1,13 @@
var _ = require("lodash");
var Client = require("./client");
var ClientManager = require("./clientManager");
var config = require("../config.json");
var fs = require("fs");
var http = require("connect");
var io = require("socket.io");
var sockets = null;
var clients = [];
var manager = new ClientManager();
var inputs = [
"action",
@ -46,34 +47,9 @@ module.exports = function(port) {
console.log("Press ctrl-c to stop");
console.log("");
if (config.public) {
return;
if (!config.public) {
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) {
@ -132,7 +108,7 @@ function auth(data) {
init(socket, client);
} else {
var success = 0;
_.each(clients, function(client) {
_.each(manager.clients, function(client) {
if (client.config.name == data.name && client.config.password == data.password) {
init(socket, client);
success++;

View File

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