UNPKG

4.49 kBJavaScriptView Raw
1"use strict";
2
3const pkg = require("../package.json");
4var _ = require("lodash");
5var path = require("path");
6var os = require("os");
7var fs = require("fs");
8var net = require("net");
9var bcrypt = require("bcryptjs");
10const colors = require("colors/safe");
11
12let homePath;
13let configPath;
14let usersPath;
15let storagePath;
16let packagesPath;
17
18const Helper = {
19 config: null,
20 expandHome,
21 getHomePath,
22 getPackagesPath,
23 getPackageModulePath,
24 getStoragePath,
25 getConfigPath,
26 getUsersPath,
27 getUserConfigPath,
28 getUserLogsPath,
29 setHome,
30 getVersion,
31 getGitCommit,
32 ip2hex,
33
34 password: {
35 hash: passwordHash,
36 compare: passwordCompare,
37 requiresUpdate: passwordRequiresUpdate,
38 },
39};
40
41module.exports = Helper;
42
43Helper.config = require(path.resolve(path.join(
44 __dirname,
45 "..",
46 "defaults",
47 "config.js"
48)));
49
50function getVersion() {
51 const gitCommit = getGitCommit();
52 return gitCommit ? `source (${gitCommit})` : `v${pkg.version}`;
53}
54
55let _gitCommit;
56function getGitCommit() {
57 if (_gitCommit !== undefined) {
58 return _gitCommit;
59 }
60 try {
61 _gitCommit = require("child_process")
62 .execSync("git rev-parse --short HEAD 2> /dev/null") // Returns hash of current commit
63 .toString()
64 .trim();
65 return _gitCommit;
66 } catch (e) {
67 // Not a git repository or git is not installed
68 _gitCommit = null;
69 return null;
70 }
71}
72
73function setHome(newPath) {
74 homePath = expandHome(newPath);
75 configPath = path.join(homePath, "config.js");
76 usersPath = path.join(homePath, "users");
77 storagePath = path.join(homePath, "storage");
78 packagesPath = path.join(homePath, "packages");
79
80 // Reload config from new home location
81 if (fs.existsSync(configPath)) {
82 const userConfig = require(configPath);
83
84 if (_.isEmpty(userConfig)) {
85 log.warn(`The file located at ${colors.green(configPath)} does not appear to expose anything.`);
86 log.warn(`Make sure it is non-empty and the configuration is exported using ${colors.bold("module.exports = { ... }")}.`);
87 log.warn("Using default configuration...");
88 }
89
90 this.config = _.merge(this.config, userConfig);
91 }
92
93 if (!this.config.displayNetwork && !this.config.lockNetwork) {
94 this.config.lockNetwork = true;
95
96 log.warn(`${colors.bold("displayNetwork")} and ${colors.bold("lockNetwork")} are false, setting ${colors.bold("lockNetwork")} to true.`);
97 }
98
99 // Load theme color from manifest.json
100 const manifest = require("../public/manifest.json");
101 this.config.themeColor = manifest.theme_color;
102
103 // TODO: Remove in future release
104 if (this.config.debug === true) {
105 log.warn("debug option is now an object, see defaults file for more information.");
106 this.config.debug = {ircFramework: true};
107 }
108
109 // TODO: Remove in future release
110 // Backwards compatibility for old way of specifying themes in settings
111 if (this.config.theme.includes(".css")) {
112 log.warn(`Referring to CSS files in the ${colors.green("theme")} setting of ${colors.green(configPath)} is ${colors.bold.red("deprecated")} and will be removed in a future version.`);
113 } else {
114 this.config.theme = `themes/${this.config.theme}.css`;
115 }
116}
117
118function getHomePath() {
119 return homePath;
120}
121
122function getConfigPath() {
123 return configPath;
124}
125
126function getUsersPath() {
127 return usersPath;
128}
129
130function getUserConfigPath(name) {
131 return path.join(usersPath, name + ".json");
132}
133
134function getUserLogsPath(name, network) {
135 return path.join(homePath, "logs", name, network);
136}
137
138function getStoragePath() {
139 return storagePath;
140}
141
142function getPackagesPath() {
143 return packagesPath;
144}
145
146function getPackageModulePath(packageName) {
147 return path.join(Helper.getPackagesPath(), "node_modules", packageName);
148}
149
150function ip2hex(address) {
151 // no ipv6 support
152 if (!net.isIPv4(address)) {
153 return "00000000";
154 }
155
156 return address.split(".").map(function(octet) {
157 var hex = parseInt(octet, 10).toString(16);
158
159 if (hex.length === 1) {
160 hex = "0" + hex;
161 }
162
163 return hex;
164 }).join("");
165}
166
167// Expand ~ into the current user home dir.
168// This does *not* support `~other_user/tmp` => `/home/other_user/tmp`.
169function expandHome(shortenedPath) {
170 if (!shortenedPath) {
171 return "";
172 }
173
174 const home = os.homedir().replace("$", "$$$$");
175 return path.resolve(shortenedPath.replace(/^~($|\/|\\)/, home + "$1"));
176}
177
178function passwordRequiresUpdate(password) {
179 return bcrypt.getRounds(password) !== 11;
180}
181
182function passwordHash(password) {
183 return bcrypt.hashSync(password, bcrypt.genSaltSync(11));
184}
185
186function passwordCompare(password, expected) {
187 return bcrypt.compare(password, expected);
188}