1 | "use strict";
|
2 |
|
3 | const pkg = require("../package.json");
|
4 | var _ = require("lodash");
|
5 | var path = require("path");
|
6 | var os = require("os");
|
7 | var fs = require("fs");
|
8 | var net = require("net");
|
9 | var bcrypt = require("bcryptjs");
|
10 | const colors = require("colors/safe");
|
11 |
|
12 | let homePath;
|
13 | let configPath;
|
14 | let usersPath;
|
15 | let storagePath;
|
16 | let packagesPath;
|
17 |
|
18 | const 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 |
|
41 | module.exports = Helper;
|
42 |
|
43 | Helper.config = require(path.resolve(path.join(
|
44 | __dirname,
|
45 | "..",
|
46 | "defaults",
|
47 | "config.js"
|
48 | )));
|
49 |
|
50 | function getVersion() {
|
51 | const gitCommit = getGitCommit();
|
52 | return gitCommit ? `source (${gitCommit})` : `v${pkg.version}`;
|
53 | }
|
54 |
|
55 | let _gitCommit;
|
56 | function 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")
|
63 | .toString()
|
64 | .trim();
|
65 | return _gitCommit;
|
66 | } catch (e) {
|
67 |
|
68 | _gitCommit = null;
|
69 | return null;
|
70 | }
|
71 | }
|
72 |
|
73 | function 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 |
|
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 |
|
100 | const manifest = require("../public/manifest.json");
|
101 | this.config.themeColor = manifest.theme_color;
|
102 |
|
103 |
|
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 |
|
110 |
|
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 |
|
118 | function getHomePath() {
|
119 | return homePath;
|
120 | }
|
121 |
|
122 | function getConfigPath() {
|
123 | return configPath;
|
124 | }
|
125 |
|
126 | function getUsersPath() {
|
127 | return usersPath;
|
128 | }
|
129 |
|
130 | function getUserConfigPath(name) {
|
131 | return path.join(usersPath, name + ".json");
|
132 | }
|
133 |
|
134 | function getUserLogsPath(name, network) {
|
135 | return path.join(homePath, "logs", name, network);
|
136 | }
|
137 |
|
138 | function getStoragePath() {
|
139 | return storagePath;
|
140 | }
|
141 |
|
142 | function getPackagesPath() {
|
143 | return packagesPath;
|
144 | }
|
145 |
|
146 | function getPackageModulePath(packageName) {
|
147 | return path.join(Helper.getPackagesPath(), "node_modules", packageName);
|
148 | }
|
149 |
|
150 | function ip2hex(address) {
|
151 |
|
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 |
|
168 |
|
169 | function 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 |
|
178 | function passwordRequiresUpdate(password) {
|
179 | return bcrypt.getRounds(password) !== 11;
|
180 | }
|
181 |
|
182 | function passwordHash(password) {
|
183 | return bcrypt.hashSync(password, bcrypt.genSaltSync(11));
|
184 | }
|
185 |
|
186 | function passwordCompare(password, expected) {
|
187 | return bcrypt.compare(password, expected);
|
188 | }
|