hardlounge/src/helper.js

159 lines
3.9 KiB
JavaScript
Raw Normal View History

"use strict";
const pkg = require("../package.json");
var _ = require("lodash");
2014-10-04 23:22:23 +00:00
var path = require("path");
var os = require("os");
2016-07-04 20:15:30 +00:00
var fs = require("fs");
2016-11-19 18:32:47 +00:00
var net = require("net");
var bcrypt = require("bcryptjs");
const colors = require("colors/safe");
2014-10-04 23:22:23 +00:00
2016-05-09 16:19:16 +00:00
var Helper = {
config: null,
expandHome: expandHome,
2017-06-22 21:09:55 +00:00
getPackagesPath: getPackagesPath,
getPackageModulePath: getPackageModulePath,
getStoragePath: getStoragePath,
getUserConfigPath: getUserConfigPath,
2016-05-15 21:13:51 +00:00
getUserLogsPath: getUserLogsPath,
2016-05-09 16:19:16 +00:00
setHome: setHome,
getVersion: getVersion,
getGitCommit: getGitCommit,
2016-11-19 18:32:47 +00:00
ip2hex: ip2hex,
2017-07-10 19:47:03 +00:00
cleanIrcMessage: cleanIrcMessage,
2016-10-21 19:00:43 +00:00
password: {
hash: passwordHash,
compare: passwordCompare,
requiresUpdate: passwordRequiresUpdate,
},
};
2016-05-09 16:19:16 +00:00
module.exports = Helper;
Helper.config = require(path.resolve(path.join(
__dirname,
"..",
"defaults",
"config.js"
)));
function getVersion() {
const gitCommit = getGitCommit();
return gitCommit ? `source (${gitCommit})` : `v${pkg.version}`;
}
let _gitCommit;
function getGitCommit() {
if (_gitCommit !== undefined) {
return _gitCommit;
}
try {
_gitCommit = require("child_process")
.execSync("git rev-parse --short HEAD 2> /dev/null") // Returns hash of current commit
.toString()
.trim();
return _gitCommit;
} catch (e) {
// Not a git repository or git is not installed
_gitCommit = null;
return null;
}
}
2016-05-09 16:19:16 +00:00
function setHome(homePath) {
this.HOME = expandHome(homePath);
2016-05-09 16:19:16 +00:00
this.CONFIG_PATH = path.join(this.HOME, "config.js");
this.USERS_PATH = path.join(this.HOME, "users");
// Reload config from new home location
2016-07-04 20:15:30 +00:00
if (fs.existsSync(this.CONFIG_PATH)) {
var userConfig = require(this.CONFIG_PATH);
2017-07-06 12:00:43 +00:00
this.config = _.merge(this.config, userConfig);
2016-07-04 20:15:30 +00:00
}
2016-12-10 08:53:06 +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.`);
}
2016-12-10 08:53:06 +00:00
// TODO: Remove in future release
if (this.config.debug === true) {
log.warn("debug option is now an object, see defaults file for more information.");
this.config.debug = {ircFramework: true};
}
// TODO: Remove in future release
// Backwards compatibility for old way of specifying themes in settings
if (this.config.theme.includes(".css")) {
log.warn(`Referring to CSS files in the ${colors.green("theme")} setting of ${colors.green(Helper.CONFIG_PATH)} is ${colors.bold("deprecated")} and will be removed in a future version.`);
} else {
this.config.theme = `themes/${this.config.theme}.css`;
}
2016-05-09 16:19:16 +00:00
}
function getUserConfigPath(name) {
return path.join(this.USERS_PATH, name + ".json");
}
2016-05-15 21:13:51 +00:00
function getUserLogsPath(name, network) {
return path.join(this.HOME, "logs", name, network);
}
function getStoragePath() {
return path.join(this.HOME, "storage");
}
2017-06-22 21:09:55 +00:00
function getPackagesPath() {
return path.join(this.HOME, "packages", "node_modules");
}
function getPackageModulePath(packageName) {
return path.join(Helper.getPackagesPath(), packageName);
}
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) {
var hex = parseInt(octet, 10).toString(16);
if (hex.length === 1) {
hex = "0" + hex;
}
return hex;
}).join("");
}
function expandHome(shortenedPath) {
if (!shortenedPath) {
return "";
}
2017-08-16 07:06:20 +00:00
const home = os.homedir().replace("$", "$$$$");
return path.resolve(shortenedPath.replace(/^~($|\/|\\)/, home + "$1"));
}
2016-10-21 19:00:43 +00:00
2017-07-10 19:47:03 +00:00
function cleanIrcMessage(message) {
// TODO: This does not strip hex based colours
return message.replace(/\x02|\x1D|\x1F|\x16|\x0F|\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?/g, "");
}
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) {
return bcrypt.compare(password, expected);
2016-10-21 19:00:43 +00:00
}