From 43b631048123f3c52680a19217664b8b9996620f Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Wed, 13 Aug 2014 18:51:54 -0700 Subject: [PATCH] Added user loading --- .gitignore | 3 +-- index.js | 19 +++++++++++---- package.json | 1 + src/clientManager.js | 52 +++++++++++++++++++++++++++++++---------- src/server.js | 34 ++++----------------------- users/example/user.json | 4 ++-- 6 files changed, 64 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index af5f4d75..61e43f8d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -build/ node_modules/ npm-debug.log -logs/ +users/* diff --git a/index.js b/index.js index eadee18d..7178a4d9 100755 --- a/index.js +++ b/index.js @@ -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 diff --git a/package.json b/package.json index 8157f931..6a64c2cc 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/clientManager.js b/src/clientManager.js index 7a225b13..8aba1544 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -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 + "'." ); diff --git a/src/server.js b/src/server.js index 50da77c5..c700ce0f 100644 --- a/src/server.js +++ b/src/server.js @@ -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++; diff --git a/users/example/user.json b/users/example/user.json index f7f56ce5..20d94cd3 100644 --- a/users/example/user.json +++ b/users/example/user.json @@ -6,8 +6,8 @@ "host": "irc.rizon.net", "port": 6667, "nick": "example", - "realname": "Example", - "join": "#chan" + "realname": "Example User", + "join": "#foo, #bar" } ] }