aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/managers
diff options
context:
space:
mode:
authorNeel Kamath <neelkamath@protonmail.com>2018-05-13 12:39:55 +0200
committerNeel Kamath <neelkamath@protonmail.com>2018-05-13 12:39:55 +0200
commit2bb5ced363b692a5696b176bc317fe525c0c05df (patch)
tree1532d9456c5f2b25ac05f7cec620a3af890eff83 /server/src/managers
parentRe-add module documentation (diff)
downloadhackchat-2bb5ced363b692a5696b176bc317fe525c0c05df.tar.gz
hackchat-2bb5ced363b692a5696b176bc317fe525c0c05df.zip
Flatten
Diffstat (limited to 'server/src/managers')
-rw-r--r--server/src/managers/commands.js239
-rw-r--r--server/src/managers/config.js224
-rw-r--r--server/src/managers/imports-manager.js148
-rw-r--r--server/src/managers/index.js6
-rw-r--r--server/src/managers/stats.js59
5 files changed, 0 insertions, 676 deletions
diff --git a/server/src/managers/commands.js b/server/src/managers/commands.js
deleted file mode 100644
index c38fb4d..0000000
--- a/server/src/managers/commands.js
+++ /dev/null
@@ -1,239 +0,0 @@
-/**
- * Commands / protocol manager- loads, validates and handles command execution
- *
- * Version: v2.0.0
- * Developer: Marzavec ( https://github.com/marzavec )
- * License: WTFPL ( http://www.wtfpl.net/txt/copying/ )
- *
- */
-
-const path = require('path');
-const chalk = require('chalk');
-const didYouMean = require('didyoumean2');
-
-class CommandManager {
- /**
- * Create a `CommandManager` instance for handling commands/protocol
- *
- * @param {Object} core Reference to the global core object
- */
- constructor (core) {
- this.core = core;
- this._commands = [];
- this._categories = [];
- }
-
- /**
- * (Re)initializes name spaces for commands and starts load routine
- *
- */
- loadCommands () {
- this._commands = [];
- this._categories = [];
-
- const core = this.core;
-
- const commandImports = core.managers.dynamicImports.getImport('src/commands');
- let cmdErrors = '';
- Object.keys(commandImports).forEach(file => {
- let command = commandImports[file];
- let name = path.basename(file);
- cmdErrors += this._validateAndLoad(command, file, name);
- });
-
- return cmdErrors;
- }
-
- /**
- * Checks the module after having been `require()`ed in and reports errors
- *
- * @param {Object} command reference to the newly loaded object
- * @param {String} file file path to the module
- * @param {String} name command (`cmd`) name
- */
- _validateAndLoad (command, file, name) {
- let error = this._validateCommand(command);
-
- if (error) {
- let errText = `Failed to load '${name}': ${error}`;
- console.log(errText);
- return errText;
- }
-
- if (!command.category) {
- let base = path.join(this.core.managers.dynamicImports.base, 'commands');
-
- let category = 'Uncategorized';
- if (file.indexOf(path.sep) > -1) {
- category = path.dirname(path.relative(base, file))
- .replace(new RegExp(path.sep.replace('\\', '\\\\'), 'g'), '/');
- }
-
- command.info.category = category;
-
- if (this._categories.indexOf(category) === -1)
- this._categories.push(category);
- }
-
- if (typeof command.init === 'function') {
- try {
- command.init(this.core);
- } catch (err) {
- let errText = `Failed to initialize '${name}': ${err}`;
- console.log(errText);
- return errText;
- }
- }
-
- this._commands.push(command);
-
- return '';
- }
-
- /**
- * Checks the module after having been `require()`ed in and reports errors
- *
- * @param {Object} object reference to the newly loaded object
- */
- _validateCommand (object) {
- if (typeof object !== 'object')
- return 'command setup is invalid';
-
- if (typeof object.run !== 'function')
- return 'run function is missing';
-
- if (typeof object.info !== 'object')
- return 'info object is missing';
-
- if (typeof object.info.name !== 'string')
- return 'info object is missing a valid name field';
-
- return null;
- }
-
- /**
- * Pulls all command names from a passed `category`
- *
- * @param {String} category reference to the newly loaded object
- */
- all (category) {
- return !category ? this._commands : this._commands.filter(c => c.info.category.toLowerCase() === category.toLowerCase());
- }
-
- /**
- * Pulls all category names
- *
- */
- categories () {
- return this._categories;
- }
-
- /**
- * Pulls command by name or alia(s)
- *
- * @param {String} name name or alias of command
- */
- get (name) {
- return this.findBy('name', name)
- || this._commands.find(command => command.info.aliases instanceof Array && command.info.aliases.indexOf(name) > -1);
- }
-
- /**
- * Pulls command by arbitrary search of the `module.info` attribute
- *
- * @param {String} key name or alias of command
- * @param {String} value name or alias of command
- */
- findBy (key, value) {
- return this._commands.find(c => c.info[key] === value);
- }
-
- /**
- * Finds and executes the requested command, or fails with semi-intelligent error
- *
- * @param {Object} server main server reference
- * @param {Object} socket calling socket reference
- * @param {Object} data command structure passed by socket (client)
- */
- handleCommand (server, socket, data) {
- // Try to find command first
- let command = this.get(data.cmd);
-
- if (command) {
- return this.execute(command, server, socket, data);
- } else {
- // Then fail with helpful (sorta) message
- return this._handleFail(server, socket, data);
- }
- }
-
- /**
- * Requested command failure handler, attempts to find command and reports back
- *
- * @param {Object} server main server reference
- * @param {Object} socket calling socket reference
- * @param {Object} data command structure passed by socket (client)
- */
- _handleFail(server, socket, data) {
- const maybe = didYouMean(data.cmd, this.all().map(c => c.info.name), {
- threshold: 5,
- thresholdType: 'edit-distance'
- });
-
- if (maybe) {
- // Found a suggestion, pass it on to their dyslexic self
- return server.reply({
- cmd: 'warn',
- text: `Command not found, did you mean: \`${maybe}\`?`
- }, socket);
- }
-
- // Request so mangled that I don't even, silently fail
- return;
- }
-
- /**
- * Attempt to execute the requested command, fail if err or bad params
- *
- * @param {Object} command target command module
- * @param {Object} server main server reference
- * @param {Object} socket calling socket reference
- * @param {Object} data command structure passed by socket (client)
- */
- async execute(command, server, socket, data) {
- if (typeof command.requiredData !== 'undefined') {
- let missing = [];
- for (let i = 0, len = command.requiredData.length; i < len; i++) {
- if (typeof data[command.requiredData[i]] === 'undefined')
- missing.push(command.requiredData[i]);
- }
-
- if (missing.length > 0) {
- let errText = `Failed to execute '${command.info.name}': missing required ${missing.join(', ')}\n\n`;
-
- server.reply({
- cmd: 'warn',
- text: errText
- }, socket);
-
- return null;
- }
- }
-
- try {
- return await command.run(this.core, server, socket, data);
- } catch (err) {
- let errText = `Failed to execute '${command.info.name}': ${err}`;
- console.log(errText);
-
- server.reply({
- cmd: 'warn',
- text: errText
- }, socket);
-
- return null;
- }
- }
-}
-
-module.exports = CommandManager;
diff --git a/server/src/managers/config.js b/server/src/managers/config.js
deleted file mode 100644
index 2865d00..0000000
--- a/server/src/managers/config.js
+++ /dev/null
@@ -1,224 +0,0 @@
-/**
- * Server configuration manager, handling loading, creation, parsing and saving
- * of the main config.json file
- *
- * Version: v2.0.0
- * Developer: Marzavec ( https://github.com/marzavec )
- * License: WTFPL ( http://www.wtfpl.net/txt/copying/ )
- *
- */
-
-const stripIndents = require('common-tags').stripIndents;
-const dateFormat = require('dateformat');
-const chalk = require('chalk');
-const fse = require('fs-extra');
-const prompt = require('prompt');
-const path = require('path');
-const deSync = require('deasync');
-
-class ConfigManager {
- /**
- * Create a `ConfigManager` instance for (re)loading classes and config
- *
- * @param {Object} core reference to the global core object
- * @param {String} base executing directory name; __dirname
- * @param {Object} dynamicImports dynamic import engine reference
- */
- constructor (core, base, dynamicImports) {
- this._core = core;
- this._base = base;
-
- this._configPath = path.resolve(base, 'config/config.json');
-
- this._dynamicImports = dynamicImports;
- }
-
- /**
- * Pulls both core config questions along with any optional config questions,
- * used in building the initial config.json or re-building it.
- *
- * @param {Object} currentConfig an object containing current server settings, if any
- * @param {Object} optionalConfigs optional (non-core) module config
- */
- getQuestions (currentConfig, optionalConfigs) {
- // core server setup questions
- const questions = {
- properties: {
- adminName: {
- pattern: /^"?[a-zA-Z0-9_]+"?$/,
- type: 'string',
- message: 'Nicks can only contain letters, numbers and underscores',
- required: !currentConfig.adminName,
- default: currentConfig.adminName,
- before: value => value.replace(/"/g, '')
- },
- adminPass: {
- type: 'string',
- required: !currentConfig.adminPass,
- default: currentConfig.adminPass,
- hidden: true,
- replace: '*',
- },
- websocketPort: {
- type: 'number',
- required: !currentConfig.websocketPort,
- default: currentConfig.websocketPort || 6060
- },
- tripSalt: {
- type: 'string',
- required: !currentConfig.tripSalt,
- default: currentConfig.tripSalt,
- hidden: true,
- replace: '*',
- }
- }
- };
-
- // non-core server setup questions, for future plugin support
- Object.keys(optionalConfigs).forEach(configName => {
- const config = optionalConfigs[configName];
- const question = config.getQuestion(currentConfig, configName);
-
- if (!question) {
- return;
- }
-
- question.description = (question.description || configName) + ' (Optional)';
- questions.properties[configName] = question;
- });
-
- return questions;
- }
-
- /**
- * `load` function overload, only blocking
- *
- */
- loadSync () {
- let conf = {};
- conf = this.load();
-
- // trip salt is the last core config question, wait until it's been populated
- // TODO: update this to work with new plugin support
- while(conf === null || typeof conf.tripSalt === 'undefined') {
- deSync.sleep(100);
- }
-
- return conf;
- }
-
- /**
- * (Re)builds the config.json (main server config), or loads the config into mem
- * if rebuilding, process will exit- this is to allow a process manager to take over
- *
- * @param {Boolean} reconfiguring set to true by `scripts/configure.js`, will exit if true
- */
- load (reconfiguring = false) {
- if (reconfiguring || !fse.existsSync(this._configPath)) {
- // gotta have that sexy console
- console.log(stripIndents`
- ${chalk.magenta('°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°º¤ø,¸¸,ø¤º°`°º¤ø,¸°º¤ø,¸¸,ø¤º°`°º¤ø')}
- ${chalk.gray('--------------(') + chalk.white(' HackChat Setup Wizard v1.0 ') + chalk.gray(')--------------')}
- ${chalk.magenta('°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°º¤ø,¸¸,ø¤º°`°º¤ø,¸°º¤ø,¸¸,ø¤º°`°º¤ø')}
-
- For advanced setup, see the HackChat wiki at:
- ${chalk.green('https://github.com/')}
-
- ${chalk.white('Note:')} ${chalk.green('npm/yarn run config')} will re-run this utility.
-
- You will now be asked for the following:
- - ${chalk.magenta('Admin Name')}, the initial admin username
- - ${chalk.magenta('Admin Pass')}, the initial admin password
- - ${chalk.magenta(' Port')}, the port for the websocket
- - ${chalk.magenta(' Salt')}, the salt for username trip
- \u200b
- `);
-
- let currentConfig = this._config || {};
- if (reconfiguring && fse.existsSync(this._configPath)) {
- this._backup();
- currentConfig = fse.readJSONSync(this._configPath);
- }
-
- prompt.get(this.getQuestions(currentConfig, this._dynamicImports.optionalConfigs), (err, res) => {
- if (typeof res.mods === 'undefined') {
- res.mods = [];
- }
-
- if (err) {
- console.error(err);
- process.exit(666); // SPOOKY!
- }
-
- try {
- fse.outputJsonSync(this._configPath, res);
- } catch (e) {
- console.error(`Couldn't write config to ${this._configPath}\n${e.stack}`);
- if (!reconfiguring) {
- process.exit(666); // SPOOKY!
- }
- }
-
- console.log('Config generated! You may now start the server normally.')
-
- process.exit(reconfiguring ? 0 : 42);
- });
-
- return null;
- }
-
- this._config = fse.readJSONSync(this._configPath);
-
- return this._config;
- }
-
- /**
- * Creates backup of current config into _configPath
- *
- */
- _backup () {
- const backupPath = `${this._configPath}.${dateFormat('dd-mm-yy-HH-MM-ss')}.bak`;
- fse.copySync(this._configPath, backupPath);
-
- return backupPath;
- }
-
- /**
- * First makes a backup of the current `config.json`, then writes current config
- * to disk
- *
- */
- save () {
- const backupPath = this._backup();
-
- if (!fse.existsSync(this._configPath)){
- fse.mkdirSync(this._configPath);
- }
-
- try {
- fse.writeJSONSync(this._configPath, this._config);
- fse.removeSync(backupPath);
-
- return true;
- } catch (err) {
- console.log(`Failed to save config file: ${err}`);
-
- return false;
- }
- }
-
- /**
- * Updates current config[`key`] with `value` then writes changes to disk
- *
- * @param {*} key arbitrary configuration key
- * @param {*} value new value to change `key` to
- */
- set (key, value) {
- const realKey = `${key}`;
- this._config[realKey] = value;
-
- this.save();
- }
-}
-
-module.exports = ConfigManager;
diff --git a/server/src/managers/imports-manager.js b/server/src/managers/imports-manager.js
deleted file mode 100644
index d8b2144..0000000
--- a/server/src/managers/imports-manager.js
+++ /dev/null
@@ -1,148 +0,0 @@
-/**
- * Import managment base, used to load commands/protocol and configuration objects
- *
- * Version: v2.0.0
- * Developer: Marzavec ( https://github.com/marzavec )
- * License: WTFPL ( http://www.wtfpl.net/txt/copying/ )
- *
- */
-
-const read = require('readdir-recursive');
-const path = require('path');
-
-class ImportsManager {
- /**
- * Create a `ImportsManager` instance for (re)loading classes and config
- *
- * @param {Object} core reference to the global core object
- * @param {String} base executing directory name; __dirname
- */
- constructor (core, base) {
- this._core = core;
- this._base = base;
-
- this._imports = {};
- this._optionalConfigs = {};
- }
-
- /**
- * Pull core reference
- *
- * @type {Object} readonly
- */
- get core () {
- return this._core;
- }
-
- /**
- * Pull base path that all imports are required in from
- *
- * @type {String} readonly
- */
- get base () {
- return this._base;
- }
-
- /**
- * Pull optional (none-core) config options
- *
- * @type {Object}
- */
- get optionalConfigs () {
- return Object.assign({}, this._optionalConfigs);
- }
-
- /**
- * Initialize this class and start loading target directories
- *
- */
- init () {
- let errorText = '';
- ImportsManager.load_dirs.forEach(dir => {
- errorText += this.loadDir(dir);
- });
-
- return errorText;
- }
-
- /**
- * Gather all js files from target directory, then verify and load
- *
- * @param {String} dirName The name of the dir to load, relative to the _base path.
- */
- loadDir (dirName) {
- const dir = path.resolve(this._base, dirName);
-
- let errorText = '';
- try {
- read.fileSync(dir).forEach(file => {
- const basename = path.basename(file);
- if (basename.startsWith('_') || !basename.endsWith('.js')) return;
-
- let imported;
- try {
- imported = require(file);
- } catch (e) {
- let err = `Unable to load modules from ${dirName} (${path.relative(dir, file)})\n${e}`;
- errorText += err;
- console.error(err);
- return errorText;
- }
-
- if (imported.configs) {
- imported.configs.forEach(config => {
- this._optionalConfigs[config.name] = config;
- });
- }
-
- if (!this._imports[dirName]) {
- this._imports[dirName] = {};
- }
-
- this._imports[dirName][file] = imported;
- });
- } catch (e) {
- let err = `Unable to load modules from ${dirName}\n${e}`;
- errorText += err;
- console.error(err);
- return errorText;
- }
-
- return errorText;
- }
-
- /**
- * Unlink references to each loaded module, pray to google that gc knows it's job,
- * then reinitialize this class to start the reload
- *
- * @param {String} dirName The name of the dir to load, relative to the _base path.
- */
- reloadDirCache (dirName) {
- Object.keys(this._imports[dirName]).forEach((mod) => {
- delete require.cache[require.resolve(mod)];
- });
-
- return this.init();
- }
-
- /**
- * Pull reference to imported modules that were imported from dirName, or
- * load required directory if not found
- *
- * @param {String} dirName The name of the dir to load, relative to the _base path.
- */
- getImport (dirName) {
- let imported = this._imports[dirName];
-
- if (!imported) {
- this.loadDir(dirName);
- }
-
- return Object.assign({}, this._imports[dirName]);
- }
-}
-
-// automagically loaded directorys on instantiation
-ImportsManager.load_dirs = ['src/commands'];
-
-module.exports = ImportsManager;
diff --git a/server/src/managers/index.js b/server/src/managers/index.js
deleted file mode 100644
index 2fac8fb..0000000
--- a/server/src/managers/index.js
+++ /dev/null
@@ -1,6 +0,0 @@
-module.exports = {
- CommandManager: require('./commands'),
- Config: require('./config'),
- ImportsManager: require('./imports-manager'),
- Stats: require('./stats')
-};
diff --git a/server/src/managers/stats.js b/server/src/managers/stats.js
deleted file mode 100644
index 20f1ae3..0000000
--- a/server/src/managers/stats.js
+++ /dev/null
@@ -1,59 +0,0 @@
-/**
- * Simple generic stats collection script for events occurances (etc)
- *
- * Version: v2.0.0
- * Developer: Marzavec ( https://github.com/marzavec )
- * License: WTFPL ( http://www.wtfpl.net/txt/copying/ )
- *
- */
-
-class Stats {
- /**
- * Create a stats instance.
- *
- */
- constructor () {
- this._stats = {};
- }
-
- /**
- * Retrieve value of arbitrary `key` reference
- *
- * @param {String} key Reference to the arbitrary store name
- */
- get (key) {
- return this._stats[key];
- }
-
- /**
- * Set value of arbitrary `key` reference
- *
- * @param {String} key Reference to the arbitrary store name
- * @param {Number} value New value for `key`
- */
- set (key, value) {
- this._stats[key] = value;
- }
-
- /**
- * Increase value of arbitrary `key` reference, by 1 or `amount`
- *
- * @param {String} key Reference to the arbitrary store name
- * @param {Number} amount Value to increase `key` by, or 1 if omitted
- */
- increment (key, amount) {
- this.set(key, (this.get(key) || 0) + (amount || 1));
- }
-
- /**
- * Reduce value of arbitrary `key` reference, by 1 or `amount`
- *
- * @param {String} key Reference to the arbitrary store name
- * @param {Number} amount Value to decrease `key` by, or 1 if omitted
- */
- decrement (key, amount) {
- this.set(key, (this.get(key) || 0) - (amount || 1));
- }
-}
-
-module.exports = Stats;