2016-10-09 19:14:02 +00:00
|
|
|
"use strict";
|
|
|
|
|
2016-09-05 12:37:27 +00:00
|
|
|
const pkg = require("../package.json");
|
2018-01-11 11:33:36 +00:00
|
|
|
const _ = require("lodash");
|
2018-06-15 20:31:06 +00:00
|
|
|
const log = require("./log");
|
2018-01-11 11:33:36 +00:00
|
|
|
const path = require("path");
|
|
|
|
const os = require("os");
|
|
|
|
const fs = require("fs");
|
|
|
|
const net = require("net");
|
|
|
|
const bcrypt = require("bcryptjs");
|
2018-03-02 18:28:54 +00:00
|
|
|
const colors = require("chalk");
|
2014-10-04 23:22:23 +00:00
|
|
|
|
2017-12-07 03:54:54 +00:00
|
|
|
let homePath;
|
|
|
|
let configPath;
|
|
|
|
let usersPath;
|
|
|
|
let storagePath;
|
|
|
|
let packagesPath;
|
2018-04-17 08:06:08 +00:00
|
|
|
let userLogsPath;
|
2017-12-07 03:54:54 +00:00
|
|
|
|
|
|
|
const Helper = {
|
2016-06-08 09:26:24 +00:00
|
|
|
config: null,
|
2017-12-07 03:54:54 +00:00
|
|
|
expandHome,
|
|
|
|
getHomePath,
|
|
|
|
getPackagesPath,
|
|
|
|
getPackageModulePath,
|
|
|
|
getStoragePath,
|
|
|
|
getConfigPath,
|
|
|
|
getUsersPath,
|
|
|
|
getUserConfigPath,
|
|
|
|
getUserLogsPath,
|
|
|
|
setHome,
|
|
|
|
getVersion,
|
|
|
|
getGitCommit,
|
|
|
|
ip2hex,
|
2018-03-10 11:54:51 +00:00
|
|
|
mergeConfig,
|
2018-04-03 14:49:22 +00:00
|
|
|
getDefaultNick,
|
2018-03-11 18:17:57 +00:00
|
|
|
parseHostmask,
|
|
|
|
compareHostmask,
|
2016-10-21 19:00:43 +00:00
|
|
|
|
|
|
|
password: {
|
|
|
|
hash: passwordHash,
|
|
|
|
compare: passwordCompare,
|
|
|
|
requiresUpdate: passwordRequiresUpdate,
|
|
|
|
},
|
2014-09-13 12:23:17 +00:00
|
|
|
};
|
|
|
|
|
2016-05-09 16:19:16 +00:00
|
|
|
module.exports = Helper;
|
|
|
|
|
2016-06-08 09:26:24 +00:00
|
|
|
Helper.config = require(path.resolve(path.join(
|
|
|
|
__dirname,
|
|
|
|
"..",
|
|
|
|
"defaults",
|
|
|
|
"config.js"
|
|
|
|
)));
|
|
|
|
|
2016-09-05 12:37:27 +00:00
|
|
|
function getVersion() {
|
|
|
|
const gitCommit = getGitCommit();
|
2018-04-15 22:19:25 +00:00
|
|
|
const version = `v${pkg.version}`;
|
|
|
|
return gitCommit ? `source (${gitCommit} / ${version})` : version;
|
2016-09-05 12:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let _gitCommit;
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2016-09-05 12:37:27 +00:00
|
|
|
function getGitCommit() {
|
|
|
|
if (_gitCommit !== undefined) {
|
|
|
|
return _gitCommit;
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2018-04-16 14:08:30 +00:00
|
|
|
if (!fs.existsSync(path.resolve(__dirname, "..", ".git", "HEAD"))) {
|
|
|
|
_gitCommit = null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-09-05 12:37:27 +00:00
|
|
|
try {
|
|
|
|
_gitCommit = require("child_process")
|
2018-04-16 13:35:17 +00:00
|
|
|
.execSync(
|
|
|
|
"git rev-parse --short HEAD", // Returns hash of current commit
|
|
|
|
{stdio: ["ignore", "pipe", "ignore"]}
|
|
|
|
)
|
2016-09-05 12:37:27 +00:00
|
|
|
.toString()
|
|
|
|
.trim();
|
|
|
|
return _gitCommit;
|
|
|
|
} catch (e) {
|
|
|
|
// Not a git repository or git is not installed
|
|
|
|
_gitCommit = null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 03:54:54 +00:00
|
|
|
function setHome(newPath) {
|
|
|
|
homePath = expandHome(newPath);
|
|
|
|
configPath = path.join(homePath, "config.js");
|
|
|
|
usersPath = path.join(homePath, "users");
|
|
|
|
storagePath = path.join(homePath, "storage");
|
2018-02-08 05:19:44 +00:00
|
|
|
packagesPath = path.join(homePath, "packages");
|
2018-04-17 08:06:08 +00:00
|
|
|
userLogsPath = path.join(homePath, "logs");
|
2016-06-08 09:26:24 +00:00
|
|
|
|
|
|
|
// Reload config from new home location
|
2017-12-07 03:54:54 +00:00
|
|
|
if (fs.existsSync(configPath)) {
|
2017-12-10 06:03:00 +00:00
|
|
|
const userConfig = require(configPath);
|
|
|
|
|
|
|
|
if (_.isEmpty(userConfig)) {
|
|
|
|
log.warn(`The file located at ${colors.green(configPath)} does not appear to expose anything.`);
|
|
|
|
log.warn(`Make sure it is non-empty and the configuration is exported using ${colors.bold("module.exports = { ... }")}.`);
|
|
|
|
log.warn("Using default configuration...");
|
|
|
|
}
|
|
|
|
|
2018-03-10 11:54:51 +00:00
|
|
|
mergeConfig(this.config, userConfig);
|
2016-07-04 20:15:30 +00:00
|
|
|
}
|
2016-12-10 08:53:06 +00:00
|
|
|
|
2017-06-08 18:40:17 +00:00
|
|
|
if (!this.config.displayNetwork && !this.config.lockNetwork) {
|
|
|
|
this.config.lockNetwork = true;
|
|
|
|
|
|
|
|
log.warn(`${colors.bold("displayNetwork")} and ${colors.bold("lockNetwork")} are false, setting ${colors.bold("lockNetwork")} to true.`);
|
|
|
|
}
|
|
|
|
|
2017-11-12 18:24:21 +00:00
|
|
|
// Load theme color from manifest.json
|
|
|
|
const manifest = require("../public/manifest.json");
|
|
|
|
this.config.themeColor = manifest.theme_color;
|
2018-04-02 06:00:21 +00:00
|
|
|
|
|
|
|
// TODO: Remove in future release
|
|
|
|
if (["example", "crypto", "zenburn"].includes(this.config.theme)) {
|
|
|
|
if (this.config.theme === "example") {
|
|
|
|
log.warn(`The default theme ${colors.red("example")} was renamed to ${colors.green("default")} as of The Lounge v3.`);
|
|
|
|
} else {
|
|
|
|
log.warn(`The theme ${colors.red(this.config.theme)} was moved to a separate theme as of The Lounge v3.`);
|
|
|
|
log.warn(`Install it with ${colors.bold("thelounge install thelounge-theme-" + this.config.theme)}.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
log.warn(`Falling back to theme ${colors.green("default")} will be removed in a future release.`);
|
|
|
|
log.warn("Please update your configuration file accordingly.");
|
|
|
|
|
|
|
|
this.config.theme = "default";
|
|
|
|
}
|
2016-05-09 16:19:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 03:54:54 +00:00
|
|
|
function getHomePath() {
|
|
|
|
return homePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getConfigPath() {
|
|
|
|
return configPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getUsersPath() {
|
|
|
|
return usersPath;
|
|
|
|
}
|
|
|
|
|
2016-05-08 06:21:31 +00:00
|
|
|
function getUserConfigPath(name) {
|
2017-12-07 03:54:54 +00:00
|
|
|
return path.join(usersPath, name + ".json");
|
2016-05-08 06:21:31 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:06:08 +00:00
|
|
|
function getUserLogsPath() {
|
|
|
|
return userLogsPath;
|
2016-05-15 21:13:51 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 15:33:09 +00:00
|
|
|
function getStoragePath() {
|
2017-12-07 03:54:54 +00:00
|
|
|
return storagePath;
|
2017-07-06 15:33:09 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 21:09:55 +00:00
|
|
|
function getPackagesPath() {
|
2017-12-07 03:54:54 +00:00
|
|
|
return packagesPath;
|
2017-06-22 21:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getPackageModulePath(packageName) {
|
2018-02-08 05:19:44 +00:00
|
|
|
return path.join(Helper.getPackagesPath(), "node_modules", packageName);
|
2017-06-22 21:09:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-19 18:32:47 +00:00
|
|
|
function ip2hex(address) {
|
|
|
|
// no ipv6 support
|
|
|
|
if (!net.isIPv4(address)) {
|
|
|
|
return "00000000";
|
|
|
|
}
|
|
|
|
|
|
|
|
return address.split(".").map(function(octet) {
|
2018-01-11 11:33:36 +00:00
|
|
|
let hex = parseInt(octet, 10).toString(16);
|
2016-11-19 18:32:47 +00:00
|
|
|
|
|
|
|
if (hex.length === 1) {
|
|
|
|
hex = "0" + hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex;
|
|
|
|
}).join("");
|
|
|
|
}
|
|
|
|
|
2017-12-08 04:33:43 +00:00
|
|
|
// Expand ~ into the current user home dir.
|
|
|
|
// This does *not* support `~other_user/tmp` => `/home/other_user/tmp`.
|
2016-05-08 06:21:31 +00:00
|
|
|
function expandHome(shortenedPath) {
|
2017-04-17 19:48:28 +00:00
|
|
|
if (!shortenedPath) {
|
|
|
|
return "";
|
|
|
|
}
|
2016-04-27 08:13:25 +00:00
|
|
|
|
2017-08-16 07:06:20 +00:00
|
|
|
const home = os.homedir().replace("$", "$$$$");
|
2016-05-08 06:21:31 +00:00
|
|
|
return path.resolve(shortenedPath.replace(/^~($|\/|\\)/, home + "$1"));
|
2016-04-27 08:13:25 +00:00
|
|
|
}
|
2016-10-21 19:00:43 +00:00
|
|
|
|
|
|
|
function passwordRequiresUpdate(password) {
|
|
|
|
return bcrypt.getRounds(password) !== 11;
|
|
|
|
}
|
|
|
|
|
|
|
|
function passwordHash(password) {
|
|
|
|
return bcrypt.hashSync(password, bcrypt.genSaltSync(11));
|
|
|
|
}
|
|
|
|
|
|
|
|
function passwordCompare(password, expected) {
|
2017-03-23 07:47:51 +00:00
|
|
|
return bcrypt.compare(password, expected);
|
2016-10-21 19:00:43 +00:00
|
|
|
}
|
2018-03-10 11:54:51 +00:00
|
|
|
|
2018-04-03 14:49:22 +00:00
|
|
|
function getDefaultNick() {
|
|
|
|
if (!this.config.defaults.nick) {
|
|
|
|
return "thelounge";
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.config.defaults.nick.replace(/%/g, () => Math.floor(Math.random() * 10));
|
|
|
|
}
|
|
|
|
|
2018-03-10 11:54:51 +00:00
|
|
|
function mergeConfig(oldConfig, newConfig) {
|
|
|
|
return _.mergeWith(oldConfig, newConfig, (objValue, srcValue, key) => {
|
|
|
|
// Do not override config variables if the type is incorrect (e.g. object changed into a string)
|
|
|
|
if (typeof objValue !== "undefined" && objValue !== null && typeof objValue !== typeof srcValue) {
|
|
|
|
log.warn(`Incorrect type for "${colors.bold(key)}", please verify your config.`);
|
|
|
|
|
|
|
|
return objValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For arrays, simply override the value with user provided one.
|
|
|
|
if (_.isArray(objValue)) {
|
|
|
|
return srcValue;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-03-11 18:17:57 +00:00
|
|
|
|
|
|
|
function parseHostmask(hostmask) {
|
|
|
|
let nick = "";
|
|
|
|
let ident = "*";
|
|
|
|
let hostname = "*";
|
|
|
|
let parts = [];
|
|
|
|
|
|
|
|
// Parse hostname first, then parse the rest
|
|
|
|
parts = hostmask.split("@");
|
|
|
|
|
|
|
|
if (parts.length >= 2) {
|
|
|
|
hostname = parts[1] || "*";
|
|
|
|
hostmask = parts[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
hostname = hostname.toLowerCase();
|
|
|
|
|
|
|
|
parts = hostmask.split("!");
|
|
|
|
|
|
|
|
if (parts.length >= 2) {
|
|
|
|
ident = parts[1] || "*";
|
|
|
|
hostmask = parts[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
ident = ident.toLowerCase();
|
|
|
|
|
|
|
|
nick = hostmask.toLowerCase() || "*";
|
|
|
|
|
|
|
|
const result = {
|
|
|
|
nick: nick,
|
|
|
|
ident: ident,
|
|
|
|
hostname: hostname,
|
|
|
|
};
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function compareHostmask(a, b) {
|
|
|
|
return (a.nick.toLowerCase() === b.nick.toLowerCase() || a.nick === "*") && (a.ident.toLowerCase() === b.ident.toLowerCase() || a.ident === "*") && (a.hostname.toLowerCase() === b.hostname.toLowerCase() || a.hostname === "*");
|
|
|
|
}
|