2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
2016-07-25 16:47:30 +00:00
|
|
|
/* eslint-disable no-console */
|
2014-10-17 02:21:06 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
//var SegfaultHandler = require('segfault-handler');
|
|
|
|
//SegfaultHandler.registerHandler('enigma-bbs-segfault.log');
|
|
|
|
|
2018-06-23 02:48:36 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const conf = require('./config.js');
|
|
|
|
const logger = require('./logger.js');
|
|
|
|
const database = require('./database.js');
|
|
|
|
const resolvePath = require('./misc_util.js').resolvePath;
|
|
|
|
const UserProps = require('./user_property.js');
|
|
|
|
const SysProps = require('./system_property.js');
|
|
|
|
const SysLogKeys = require('./system_log.js');
|
2018-06-23 02:48:36 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const async = require('async');
|
|
|
|
const util = require('util');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const mkdirs = require('fs-extra').mkdirs;
|
|
|
|
const fs = require('graceful-fs');
|
|
|
|
const paths = require('path');
|
|
|
|
const moment = require('moment');
|
2018-06-23 02:48:36 +00:00
|
|
|
|
|
|
|
// our main entry point
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.main = main;
|
2018-06-23 02:48:36 +00:00
|
|
|
|
|
|
|
// object with various services we want to de-init/shutdown cleanly if possible
|
2016-06-21 02:39:20 +00:00
|
|
|
const initServices = {};
|
|
|
|
|
2018-06-23 02:48:36 +00:00
|
|
|
// only include bbs.js once @ startup; this should be fine
|
2022-06-05 20:04:25 +00:00
|
|
|
const COPYRIGHT = fs
|
|
|
|
.readFileSync(paths.join(__dirname, '../LICENSE.TXT'), 'utf8')
|
|
|
|
.split(/\r?\n/g)[0];
|
2018-04-11 01:53:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const FULL_COPYRIGHT = `ENiGMA½ ${COPYRIGHT}`;
|
|
|
|
const HELP = `${FULL_COPYRIGHT}
|
2017-02-10 04:08:23 +00:00
|
|
|
usage: main.js <args>
|
2017-11-25 22:45:19 +00:00
|
|
|
eg : main.js --config /enigma_install_path/config/
|
2017-02-10 04:08:23 +00:00
|
|
|
|
|
|
|
valid args:
|
|
|
|
--version : display version
|
|
|
|
--help : displays this help
|
2017-11-25 22:45:19 +00:00
|
|
|
--config PATH : override default config path
|
2017-02-10 04:08:23 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
function printHelpAndExit() {
|
2018-06-22 05:15:04 +00:00
|
|
|
console.info(HELP);
|
|
|
|
process.exit();
|
2017-02-10 04:08:23 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 02:53:14 +00:00
|
|
|
function printVersionAndExit() {
|
|
|
|
console.info(require('../package.json').version);
|
|
|
|
}
|
|
|
|
|
2017-05-11 03:29:04 +00:00
|
|
|
function main() {
|
2020-06-19 03:29:24 +00:00
|
|
|
let errorDisplayed = false;
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function processArgs(callback) {
|
|
|
|
const argv = require('minimist')(process.argv.slice(2));
|
2017-02-10 04:08:23 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (argv.help) {
|
2018-11-15 02:53:14 +00:00
|
|
|
return printHelpAndExit();
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (argv.version) {
|
2018-11-15 02:53:14 +00:00
|
|
|
return printVersionAndExit();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const configOverridePath = argv.config;
|
2017-02-10 04:08:23 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
configOverridePath || conf.Config.getDefaultPath(),
|
|
|
|
_.isString(configOverridePath)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function initConfig(configPath, configPathSupplied, callback) {
|
|
|
|
const configFile = configPath + 'config.hjson';
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2020-06-12 03:16:15 +00:00
|
|
|
conf.Config.create(resolvePath(configFile), err => {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 02:48:36 +00:00
|
|
|
// If the user supplied a path and we can't read/parse it
|
|
|
|
// then it's a fatal error
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
if ('ENOENT' === err.code) {
|
|
|
|
if (configPathSupplied) {
|
|
|
|
console.error(
|
|
|
|
'Configuration file does not exist: ' + configFile
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
configPathSupplied = null; // make non-fatal; we'll go with defaults
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-19 03:29:24 +00:00
|
|
|
errorDisplayed = true;
|
|
|
|
console.error(`Configuration error: ${err.message}`); // eslint-disable-line no-console
|
|
|
|
if (err.hint) {
|
|
|
|
console.error(`Hint: ${err.hint}`);
|
|
|
|
}
|
|
|
|
if (err.configPath) {
|
|
|
|
console.error(`Note: ${err.configPath}`);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 01:32:27 +00:00
|
|
|
return callback(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function initSystem(callback) {
|
|
|
|
initialize(function init(err) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
console.error('Error initializing: ' + util.inspect(err));
|
|
|
|
}
|
|
|
|
return callback(err);
|
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
function complete(err) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!err) {
|
2018-07-03 01:32:27 +00:00
|
|
|
// note this is escaped:
|
2022-06-05 20:04:25 +00:00
|
|
|
fs.readFile(
|
|
|
|
paths.join(__dirname, '../misc/startup_banner.asc'),
|
|
|
|
'utf8',
|
|
|
|
(err, banner) => {
|
|
|
|
console.info(FULL_COPYRIGHT);
|
|
|
|
if (!err) {
|
|
|
|
console.info(banner);
|
|
|
|
}
|
|
|
|
console.info('System started!');
|
2018-07-03 01:32:27 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2018-07-03 01:32:27 +00:00
|
|
|
}
|
2016-09-29 03:54:25 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err && !errorDisplayed) {
|
2018-06-22 05:15:04 +00:00
|
|
|
console.error('Error initializing: ' + util.inspect(err));
|
2020-07-05 21:32:10 +00:00
|
|
|
return process.exit();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2015-04-19 08:13:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 02:39:20 +00:00
|
|
|
function shutdownSystem() {
|
2018-06-22 05:15:04 +00:00
|
|
|
const msg = 'Process interrupted. Shutting down...';
|
|
|
|
console.info(msg);
|
|
|
|
logger.log.info(msg);
|
2016-06-21 02:39:20 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function closeConnections(callback) {
|
|
|
|
const ClientConns = require('./client_connections.js');
|
|
|
|
const activeConnections = ClientConns.getActiveConnections();
|
|
|
|
let i = activeConnections.length;
|
2022-06-05 20:04:25 +00:00
|
|
|
while (i--) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const activeTerm = activeConnections[i].term;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (activeTerm) {
|
|
|
|
activeTerm.write(
|
|
|
|
'\n\nServer is shutting down NOW! Disconnecting...\n\n'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
ClientConns.removeClient(activeConnections[i]);
|
|
|
|
}
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function stopListeningServers(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
return require('./listening_server.js').shutdown(() => {
|
|
|
|
return callback(null); // ignore err
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function stopEventScheduler(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (initServices.eventScheduler) {
|
|
|
|
return initServices.eventScheduler.shutdown(() => {
|
|
|
|
return callback(null); // ignore err
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function stopFileAreaWeb(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
require('./file_area_web.js').startup(() => {
|
|
|
|
return callback(null); // ignore err
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function stopMsgNetwork(callback) {
|
|
|
|
require('./msg_network.js').shutdown(callback);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
() => {
|
|
|
|
console.info('Goodbye!');
|
|
|
|
return process.exit();
|
|
|
|
}
|
|
|
|
);
|
2016-06-21 02:39:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
function initialize(cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function createMissingDirectories(callback) {
|
2020-06-08 03:21:31 +00:00
|
|
|
const Config = conf.get();
|
2022-06-05 20:04:25 +00:00
|
|
|
async.each(
|
|
|
|
Object.keys(Config.paths),
|
|
|
|
function entry(pathKey, next) {
|
|
|
|
mkdirs(Config.paths[pathKey], function dirCreated(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(
|
|
|
|
'Could not create path: ' +
|
|
|
|
Config.paths[pathKey] +
|
|
|
|
': ' +
|
|
|
|
err.toString()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return next(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function dirCreationComplete(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function basicInit(callback) {
|
|
|
|
logger.init();
|
|
|
|
logger.log.info(
|
2020-05-14 01:04:11 +00:00
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
version: require('../package.json').version,
|
|
|
|
nodeVersion: process.version,
|
2020-05-14 01:04:11 +00:00
|
|
|
},
|
|
|
|
'**** ENiGMA½ Bulletin Board System Starting Up! ****'
|
|
|
|
);
|
2014-10-29 11:30:20 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
process.on('SIGINT', shutdownSystem);
|
2016-09-14 03:44:35 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
require('@breejs/later').date.localTime(); // use local times for later.js/scheduling
|
2017-06-11 00:29:00 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function initDatabases(callback) {
|
|
|
|
return database.initializeDatabases(callback);
|
|
|
|
},
|
|
|
|
function initMimeTypes(callback) {
|
|
|
|
return require('./mime_util.js').startup(callback);
|
|
|
|
},
|
|
|
|
function initStatLog(callback) {
|
|
|
|
return require('./stat_log.js').init(callback);
|
|
|
|
},
|
2020-06-17 06:10:51 +00:00
|
|
|
function initMenusAndThemes(callback) {
|
|
|
|
const { ThemeManager } = require('./theme');
|
|
|
|
return ThemeManager.create(callback);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function loadSysOpInformation(callback) {
|
|
|
|
//
|
2018-11-25 03:02:19 +00:00
|
|
|
// Copy over some +op information from the user DB -> system properties.
|
2018-06-23 02:48:36 +00:00
|
|
|
// * Makes this accessible for MCI codes, easy non-blocking access, etc.
|
|
|
|
// * We do this every time as the op is free to change this information just
|
|
|
|
// like any other user
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const User = require('./user.js');
|
2016-07-28 03:44:27 +00:00
|
|
|
|
2018-11-24 16:39:53 +00:00
|
|
|
const propLoadOpts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
names: [
|
|
|
|
UserProps.RealName,
|
|
|
|
UserProps.Sex,
|
|
|
|
UserProps.EmailAddress,
|
|
|
|
UserProps.Location,
|
|
|
|
UserProps.Affiliations,
|
2018-11-24 16:39:53 +00:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getOpUserName(next) {
|
|
|
|
return User.getUserName(1, next);
|
|
|
|
},
|
|
|
|
function getOpProps(opUserName, next) {
|
2022-06-05 20:04:25 +00:00
|
|
|
User.loadProperties(
|
|
|
|
User.RootUserID,
|
|
|
|
propLoadOpts,
|
|
|
|
(err, opProps) => {
|
|
|
|
return next(err, opUserName, opProps);
|
|
|
|
}
|
|
|
|
);
|
2018-11-24 16:39:53 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
2018-11-24 16:39:53 +00:00
|
|
|
(err, opUserName, opProps) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
const StatLog = require('./stat_log.js');
|
2015-08-03 00:27:05 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-11-24 16:39:53 +00:00
|
|
|
propLoadOpts.names.concat('username').forEach(v => {
|
2018-11-26 02:05:16 +00:00
|
|
|
StatLog.setNonPersistentSystemStat(`sysop_${v}`, 'N/A');
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
opProps.username = opUserName;
|
2016-07-28 03:44:27 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
_.each(opProps, (v, k) => {
|
2018-11-26 02:05:16 +00:00
|
|
|
StatLog.setNonPersistentSystemStat(`sysop_${k}`, v);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
2016-07-28 03:44:27 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2018-11-26 03:13:48 +00:00
|
|
|
function initCallsToday(callback) {
|
|
|
|
const StatLog = require('./stat_log.js');
|
|
|
|
const filter = {
|
2022-06-05 20:04:25 +00:00
|
|
|
logName: SysLogKeys.UserLoginHistory,
|
|
|
|
resultType: 'count',
|
|
|
|
date: moment(),
|
2018-11-26 03:13:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
StatLog.findSystemLogEntries(filter, (err, callsToday) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!err) {
|
|
|
|
StatLog.setNonPersistentSystemStat(
|
|
|
|
SysProps.LoginsToday,
|
|
|
|
callsToday
|
|
|
|
);
|
2018-11-26 03:13:48 +00:00
|
|
|
}
|
|
|
|
return callback(null);
|
|
|
|
});
|
|
|
|
},
|
2018-11-28 04:21:00 +00:00
|
|
|
function initMessageStats(callback) {
|
|
|
|
return require('./message_area.js').startup(callback);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
function initMCI(callback) {
|
|
|
|
return require('./predefined_mci.js').init(callback);
|
|
|
|
},
|
|
|
|
function readyMessageNetworkSupport(callback) {
|
|
|
|
return require('./msg_network.js').startup(callback);
|
|
|
|
},
|
|
|
|
function readyEvents(callback) {
|
|
|
|
return require('./events.js').startup(callback);
|
|
|
|
},
|
2018-07-21 20:32:06 +00:00
|
|
|
function genericModulesInit(callback) {
|
|
|
|
return require('./module_util.js').initializeModules(callback);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
function listenConnections(callback) {
|
|
|
|
return require('./listening_server.js').startup(callback);
|
|
|
|
},
|
|
|
|
function readyFileBaseArea(callback) {
|
|
|
|
return require('./file_base_area.js').startup(callback);
|
|
|
|
},
|
|
|
|
function readyFileAreaWeb(callback) {
|
|
|
|
return require('./file_area_web.js').startup(callback);
|
|
|
|
},
|
|
|
|
function readyPasswordReset(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const WebPasswordReset =
|
|
|
|
require('./web_password_reset.js').WebPasswordReset;
|
2018-06-22 05:15:04 +00:00
|
|
|
return WebPasswordReset.startup(callback);
|
|
|
|
},
|
2019-06-13 03:57:45 +00:00
|
|
|
function ready2FA_OTPRegister(callback) {
|
|
|
|
const User2FA_OTPWebRegister = require('./user_2fa_otp_web_register.js');
|
|
|
|
return User2FA_OTPWebRegister.startup(callback);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
function readyEventScheduler(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const EventSchedulerModule =
|
|
|
|
require('./event_scheduler.js').EventSchedulerModule;
|
|
|
|
EventSchedulerModule.loadAndStart((err, modInst) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
initServices.eventScheduler = modInst;
|
|
|
|
return callback(err);
|
|
|
|
});
|
2018-07-21 20:32:06 +00:00
|
|
|
},
|
|
|
|
function listenUserEventsForStatLog(callback) {
|
|
|
|
return require('./stat_log.js').initUserEvents(callback);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
function onComplete(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
2014-10-29 11:30:20 +00:00
|
|
|
}
|