diff --git a/src/clientManager.js b/src/clientManager.js index 36cc048d..33db9474 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -3,7 +3,6 @@ var fs = require("fs"); var Client = require("./client"); var mkdirp = require("mkdirp"); var Helper = require("./helper"); -var path = require("path"); module.exports = ClientManager; @@ -46,10 +45,9 @@ ClientManager.prototype.loadUser = function(name) { ClientManager.prototype.getUsers = function() { var users = []; - var path = Helper.HOME + "/users"; - mkdirp.sync(path); + mkdirp.sync(Helper.USERS_PATH); try { - var files = fs.readdirSync(path); + var files = fs.readdirSync(Helper.USERS_PATH); files.forEach(function(file) { if (file.indexOf(".json") !== -1) { users.push(file.replace(".json", "")); @@ -68,10 +66,9 @@ ClientManager.prototype.addUser = function(name, password) { return false; } try { - var usersPath = path.join(Helper.HOME, "users"); - mkdirp.sync(usersPath); + mkdirp.sync(Helper.USERS_PATH); - if (path.basename(name) !== name) { + if (require("path").basename(name) !== name) { throw new Error(name + " is an invalid username."); } @@ -82,7 +79,7 @@ ClientManager.prototype.addUser = function(name, password) { networks: [] }; fs.writeFileSync( - path.join(usersPath, name + ".json"), + Helper.getUserConfigPath(name), JSON.stringify(user, null, " ") ); } catch (e) { @@ -100,14 +97,13 @@ ClientManager.prototype.updateUser = function(name, opts) { if (typeof opts === "undefined") { return false; } - var path = Helper.HOME + "/users/" + name + ".json"; - var user = {}; + var user = {}; try { user = this.readUserConfig(name); _.assign(user, opts); fs.writeFileSync( - path, + Helper.getUserConfigPath(name), JSON.stringify(user, null, " ") ); } catch (e) { @@ -122,9 +118,8 @@ ClientManager.prototype.readUserConfig = function(name) { if (users.indexOf(name) === -1) { return false; } - var path = Helper.HOME + "/users/" + name + ".json"; var user = {}; - var data = fs.readFileSync(path, "utf-8"); + var data = fs.readFileSync(Helper.getUserConfigPath(name), "utf-8"); user = JSON.parse(data); return user; }; @@ -135,8 +130,7 @@ ClientManager.prototype.removeUser = function(name) { return false; } try { - var path = Helper.HOME + "/users/" + name + ".json"; - fs.unlinkSync(path); + fs.unlinkSync(Helper.getUserConfigPath(name)); } catch (e) { throw e; } diff --git a/src/command-line/add.js b/src/command-line/add.js index 69410f09..7f335d8f 100644 --- a/src/command-line/add.js +++ b/src/command-line/add.js @@ -9,21 +9,21 @@ program .command("add ") .description("Add a new user") .action(function(name/* , password */) { - var path = Helper.HOME + "/users"; try { - mkdirp.sync(path); + mkdirp.sync(Helper.USERS_PATH); } catch (e) { - log.error("Could not create", path); + log.error("Could not create", Helper.USERS_PATH); log.info("Try running the command as sudo."); return; } try { - var test = path + "/.test"; + var test = require("path").join(Helper.USERS_PATH, ".test"); fs.mkdirSync(test); fs.rmdirSync(test); } catch (e) { - log.error("You have no permissions to write to", path); + log.error("You have no permissions to write to", Helper.USERS_PATH); log.info("Try running the command as sudo."); + log.error("Could not create", Helper.USERS_PATH); return; } var manager = new ClientManager(); @@ -50,5 +50,5 @@ function add(manager, name, password) { hash ); log.info("User '" + name + "' created:"); - log.info(Helper.HOME + "/users/" + name + ".json"); + log.info(Helper.getUserConfigPath(name)); } diff --git a/src/command-line/config.js b/src/command-line/config.js index e2658bdd..1fef7dde 100644 --- a/src/command-line/config.js +++ b/src/command-line/config.js @@ -4,11 +4,11 @@ var Helper = require("../helper"); program .command("config") - .description("Edit config: '" + Helper.HOME + "/config.js'") + .description("Edit config: " + Helper.CONFIG_PATH) .action(function() { child.spawn( process.env.EDITOR || "vi", - [Helper.HOME + "/config.js"], + [Helper.CONFIG_PATH], {stdio: "inherit"} ); }); diff --git a/src/command-line/edit.js b/src/command-line/edit.js index f9671102..5d849eb9 100644 --- a/src/command-line/edit.js +++ b/src/command-line/edit.js @@ -5,7 +5,7 @@ var Helper = require("../helper"); program .command("edit ") - .description("Edit user: '" + Helper.HOME + "/users/.json'") + .description("Edit user: " + Helper.getUserConfigPath("")) .action(function(name) { var users = new ClientManager().getUsers(); if (users.indexOf(name) === -1) { @@ -14,7 +14,7 @@ program } child.spawn( process.env.EDITOR || "vi", - [require("path").join(Helper.HOME, "users", name + ".json")], + [Helper.getUserConfigPath(name)], {stdio: "inherit"} ); }); diff --git a/src/command-line/index.js b/src/command-line/index.js index 95a51f42..ac8909d3 100644 --- a/src/command-line/index.js +++ b/src/command-line/index.js @@ -4,6 +4,7 @@ var program = require("commander"); var pkg = require("../../package.json"); var fs = require("fs"); var mkdirp = require("mkdirp"); +var path = require("path"); var Helper = require("../helper"); program.version(pkg.version, "-v, --version"); @@ -12,17 +13,22 @@ program.option(" --home " , "home path"); var argv = program.parseOptions(process.argv); if (program.home) { - Helper.HOME = program.home; + Helper.HOME = path.resolve(program.home); } -var config = Helper.HOME + "/config.js"; -if (!fs.existsSync(config)) { +if (!fs.existsSync(Helper.CONFIG_PATH)) { mkdirp.sync(Helper.HOME, {mode: "0700"}); fs.writeFileSync( - config, - fs.readFileSync(__dirname + "/../../defaults/config.js") + Helper.CONFIG_PATH, + fs.readFileSync(path.resolve(path.join( + __dirname, + "..", + "..", + "defaults", + "config.js" + ))) ); - log.info("Config created:", config); + log.info("Config created:", Helper.CONFIG_PATH); } require("./start"); diff --git a/src/command-line/remove.js b/src/command-line/remove.js index 683892f5..d6f383e6 100644 --- a/src/command-line/remove.js +++ b/src/command-line/remove.js @@ -8,12 +8,11 @@ program .description("Remove an existing user") .action(function(name) { try { - var path = Helper.HOME + "/users"; - var test = path + "/.test"; + var test = require("path").join(Helper.USERS_PATH, ".test"); fs.mkdirSync(test); fs.rmdirSync(test); } catch (e) { - log.warn("You have no permissions to delete from " + path); + log.warn("You have no permissions to delete from " + Helper.USERS_PATH); log.info("Try running the command as sudo."); return; } diff --git a/src/command-line/reset.js b/src/command-line/reset.js index d953ce66..38449fa4 100644 --- a/src/command-line/reset.js +++ b/src/command-line/reset.js @@ -13,7 +13,7 @@ program log.error("User '" + name + "' doesn't exist."); return; } - var file = Helper.HOME + "/users/" + name + ".json"; + var file = Helper.getUserConfigPath(name); var user = require(file); require("read")({ prompt: "[thelounge] New password: ", diff --git a/src/helper.js b/src/helper.js index 38ab2596..d391c761 100644 --- a/src/helper.js +++ b/src/helper.js @@ -1,17 +1,26 @@ var path = require("path"); var os = require("os"); +var HOME = expandHome("~/.lounge"); + module.exports = { - HOME: (process.env.HOME || process.env.USERPROFILE) + "/.lounge", - getConfig: getConfig, + HOME: HOME, + CONFIG_PATH: path.join(HOME, "config.js"), + USERS_PATH: path.join(HOME, "users"), expandHome: expandHome, + getConfig: getConfig, + getUserConfigPath: getUserConfigPath, }; -function getConfig() { - return require(path.resolve(this.HOME) + "/config"); +function getUserConfigPath(name) { + return path.join(this.USERS_PATH, name + ".json"); } -function expandHome(path) { +function getConfig() { + return require(this.CONFIG_PATH); +} + +function expandHome(shortenedPath) { var home; if (os.homedir) { @@ -24,6 +33,5 @@ function expandHome(path) { home = home.replace("$", "$$$$"); - return path.replace(/^~($|\/|\\)/, home + "$1"); + return path.resolve(shortenedPath.replace(/^~($|\/|\\)/, home + "$1")); } - diff --git a/src/userLog.js b/src/userLog.js index e9f436ec..a559b190 100644 --- a/src/userLog.js +++ b/src/userLog.js @@ -5,7 +5,7 @@ var Helper = require("./helper"); module.exports.write = function(user, network, chan, msg) { try { - var path = Helper.HOME + "/logs/" + user + "/" + network; + var path = require("path").join(Helper.HOME, "logs", user, network); mkdirp.sync(path); } catch (e) { log.error("Unabled to create logs directory", e);