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½
|
|
|
|
const conf = require('./config.js');
|
|
|
|
const logger = require('./logger.js');
|
|
|
|
const database = require('./database.js');
|
|
|
|
const resolvePath = require('./misc_util.js').resolvePath;
|
|
|
|
|
|
|
|
// deps
|
|
|
|
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');
|
|
|
|
|
|
|
|
// our main entry point
|
|
|
|
exports.main = main;
|
|
|
|
|
|
|
|
// 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
|
2018-04-11 01:53:04 +00:00
|
|
|
const COPYRIGHT = fs.readFileSync(paths.join(__dirname, '../LICENSE.TXT'), 'utf8').split(/\r?\n/g)[0];
|
|
|
|
|
2018-06-23 02:48:36 +00:00
|
|
|
const FULL_COPYRIGHT = `ENiGMA½ ${COPYRIGHT}`;
|
2017-06-11 00:29:00 +00:00
|
|
|
const HELP =
|
2018-04-11 01:53:04 +00:00
|
|
|
`${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() {
|
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
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(argv.help) {
|
2018-11-15 02:53:14 +00:00
|
|
|
return printHelpAndExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(argv.version) {
|
|
|
|
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
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null, configOverridePath || conf.getDefaultPath(), _.isString(configOverridePath));
|
|
|
|
},
|
|
|
|
function initConfig(configPath, configPathSupplied, callback) {
|
|
|
|
const configFile = configPath + 'config.hjson';
|
|
|
|
conf.init(resolvePath(configFile), function configInit(err) {
|
2015-04-19 08:13:13 +00:00
|
|
|
|
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
|
|
|
//
|
|
|
|
if(err) {
|
|
|
|
if('ENOENT' === err.code) {
|
|
|
|
if(configPathSupplied) {
|
|
|
|
console.error('Configuration file does not exist: ' + configFile);
|
|
|
|
} else {
|
2018-06-23 02:48:36 +00:00
|
|
|
configPathSupplied = null; // make non-fatal; we'll go with defaults
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-03 01:32:27 +00:00
|
|
|
console.error(err.message);
|
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) {
|
|
|
|
if(err) {
|
|
|
|
console.error('Error initializing: ' + util.inspect(err));
|
|
|
|
}
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
2018-07-03 01:32:27 +00:00
|
|
|
if(!err) {
|
|
|
|
// note this is escaped:
|
|
|
|
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!');
|
|
|
|
});
|
|
|
|
}
|
2016-09-29 03:54:25 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(err) {
|
|
|
|
console.error('Error initializing: ' + util.inspect(err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
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;
|
|
|
|
while(i--) {
|
|
|
|
const activeTerm = activeConnections[i].term;
|
|
|
|
if(activeTerm) {
|
|
|
|
activeTerm.write('\n\nServer is shutting down NOW! Disconnecting...\n\n');
|
|
|
|
}
|
|
|
|
ClientConns.removeClient(activeConnections[i]);
|
|
|
|
}
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function stopListeningServers(callback) {
|
|
|
|
return require('./listening_server.js').shutdown( () => {
|
2018-06-23 02:48:36 +00:00
|
|
|
return callback(null); // ignore err
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function stopEventScheduler(callback) {
|
|
|
|
if(initServices.eventScheduler) {
|
|
|
|
return initServices.eventScheduler.shutdown( () => {
|
2018-06-23 02:48:36 +00:00
|
|
|
return callback(null); // ignore err
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function stopFileAreaWeb(callback) {
|
|
|
|
require('./file_area_web.js').startup( () => {
|
2018-06-23 02:48:36 +00:00
|
|
|
return callback(null); // ignore err
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function stopMsgNetwork(callback) {
|
|
|
|
require('./msg_network.js').shutdown(callback);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
() => {
|
|
|
|
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) {
|
|
|
|
async.each(Object.keys(conf.config.paths), function entry(pathKey, next) {
|
|
|
|
mkdirs(conf.config.paths[pathKey], function dirCreated(err) {
|
|
|
|
if(err) {
|
|
|
|
console.error('Could not create path: ' + conf.config.paths[pathKey] + ': ' + err.toString());
|
|
|
|
}
|
|
|
|
return next(err);
|
|
|
|
});
|
|
|
|
}, function dirCreationComplete(err) {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function basicInit(callback) {
|
|
|
|
logger.init();
|
|
|
|
logger.log.info(
|
|
|
|
{ version : require('../package.json').version },
|
|
|
|
'**** 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
|
|
|
|
2018-06-23 02:48:36 +00:00
|
|
|
require('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);
|
|
|
|
},
|
|
|
|
function initConfigs(callback) {
|
|
|
|
return require('./config_util.js').init(callback);
|
|
|
|
},
|
|
|
|
function initThemes(callback) {
|
2018-06-23 02:48:36 +00:00
|
|
|
// Have to pull in here so it's after Config init
|
2018-06-22 05:15:04 +00:00
|
|
|
require('./theme.js').initAvailableThemes( (err, themeCount) => {
|
|
|
|
logger.log.info({ themeCount }, 'Themes initialized');
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function loadSysOpInformation(callback) {
|
|
|
|
//
|
2018-06-23 02:48:36 +00:00
|
|
|
// Copy over some +op information from the user DB -> system propertys.
|
|
|
|
// * 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-06-22 05:15:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getOpUserName(next) {
|
|
|
|
return User.getUserName(1, next);
|
|
|
|
},
|
|
|
|
function getOpProps(opUserName, next) {
|
|
|
|
const propLoadOpts = {
|
2018-06-23 02:48:36 +00:00
|
|
|
names : [ 'real_name', 'sex', 'email_address', 'location', 'affiliation' ],
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
User.loadProperties(User.RootUserID, propLoadOpts, (err, opProps) => {
|
|
|
|
return next(err, opUserName, opProps);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
(err, opUserName, opProps) => {
|
|
|
|
const StatLog = require('./stat_log.js');
|
2015-08-03 00:27:05 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(err) {
|
|
|
|
[ 'username', 'real_name', 'sex', 'email_address', 'location', 'affiliation' ].forEach(v => {
|
|
|
|
StatLog.setNonPeristentSystemStat(`sysop_${v}`, 'N/A');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
opProps.username = opUserName;
|
2016-07-28 03:44:27 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
_.each(opProps, (v, k) => {
|
|
|
|
StatLog.setNonPeristentSystemStat(`sysop_${k}`, v);
|
|
|
|
});
|
|
|
|
}
|
2016-07-28 03:44:27 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function initFileAreaStats(callback) {
|
|
|
|
const getAreaStats = require('./file_base_area.js').getAreaStats;
|
|
|
|
getAreaStats( (err, stats) => {
|
|
|
|
if(!err) {
|
|
|
|
const StatLog = require('./stat_log.js');
|
|
|
|
StatLog.setNonPeristentSystemStat('file_base_area_stats', stats);
|
|
|
|
}
|
2017-09-09 17:48:43 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
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) {
|
|
|
|
const WebPasswordReset = require('./web_password_reset.js').WebPasswordReset;
|
|
|
|
return WebPasswordReset.startup(callback);
|
|
|
|
},
|
|
|
|
function readyEventScheduler(callback) {
|
|
|
|
const EventSchedulerModule = require('./event_scheduler.js').EventSchedulerModule;
|
|
|
|
EventSchedulerModule.loadAndStart( (err, modInst) => {
|
|
|
|
initServices.eventScheduler = modInst;
|
|
|
|
return callback(err);
|
|
|
|
});
|
2018-07-21 20:32:06 +00:00
|
|
|
},
|
|
|
|
function listenUserEventsForStatLog(callback) {
|
|
|
|
return require('./stat_log.js').initUserEvents(callback);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function onComplete(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
2014-10-29 11:30:20 +00:00
|
|
|
}
|