2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
//var SegfaultHandler = require('segfault-handler');
|
|
|
|
//SegfaultHandler.registerHandler('enigma-bbs-segfault.log');
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
// ENiGMA½
|
2016-06-21 02:39:20 +00:00
|
|
|
const conf = require('./config.js');
|
|
|
|
const logger = require('./logger.js');
|
|
|
|
const database = require('./database.js');
|
|
|
|
const clientConns = require('./client_connections.js');
|
|
|
|
|
|
|
|
const async = require('async');
|
|
|
|
const util = require('util');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const mkdirs = require('fs-extra').mkdirs;
|
2016-02-17 05:11:55 +00:00
|
|
|
|
|
|
|
// our main entry point
|
|
|
|
exports.bbsMain = bbsMain;
|
2014-10-29 11:30:20 +00:00
|
|
|
|
2016-06-21 02:39:20 +00:00
|
|
|
// object with various services we want to de-init/shutdown cleanly if possible
|
|
|
|
const initServices = {};
|
|
|
|
|
2015-04-19 08:13:13 +00:00
|
|
|
function bbsMain() {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function processArgs(callback) {
|
2016-02-17 05:11:55 +00:00
|
|
|
const args = process.argv.slice(2);
|
2015-04-19 08:13:13 +00:00
|
|
|
|
|
|
|
var configPath;
|
|
|
|
|
|
|
|
if(args.indexOf('--help') > 0) {
|
|
|
|
// :TODO: display help
|
|
|
|
} else {
|
2016-02-17 05:11:55 +00:00
|
|
|
let argCount = args.length;
|
|
|
|
for(let i = 0; i < argCount; ++i) {
|
|
|
|
const arg = args[i];
|
|
|
|
if('--config' === arg) {
|
2015-04-19 08:13:13 +00:00
|
|
|
configPath = args[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
callback(null, configPath || conf.getDefaultPath(), _.isString(configPath));
|
2015-04-19 08:13:13 +00:00
|
|
|
},
|
|
|
|
function initConfig(configPath, configPathSupplied, callback) {
|
|
|
|
conf.init(configPath, function configInit(err) {
|
|
|
|
|
|
|
|
//
|
|
|
|
// If the user supplied a path and we can't read/parse it
|
|
|
|
// then it's a fatal error
|
|
|
|
//
|
2015-11-21 06:46:48 +00:00
|
|
|
if(err) {
|
2015-11-21 20:29:24 +00:00
|
|
|
if('ENOENT' === err.code) {
|
|
|
|
if(configPathSupplied) {
|
|
|
|
console.error('Configuration file does not exist: ' + configPath);
|
|
|
|
} else {
|
|
|
|
configPathSupplied = null; // make non-fatal; we'll go with defaults
|
|
|
|
}
|
2015-11-21 06:46:48 +00:00
|
|
|
} else {
|
|
|
|
console.error(err.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback(err);
|
2015-04-19 08:13:13 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function initSystem(callback) {
|
|
|
|
initialize(function init(err) {
|
|
|
|
if(err) {
|
|
|
|
console.error('Error initializing: ' + util.inspect(err));
|
|
|
|
}
|
|
|
|
callback(err);
|
|
|
|
});
|
2016-02-17 05:11:55 +00:00
|
|
|
},
|
|
|
|
function listenConnections(callback) {
|
|
|
|
startListening(callback);
|
2015-04-19 08:13:13 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
2016-02-17 05:11:55 +00:00
|
|
|
if(err) {
|
2016-03-27 06:19:31 +00:00
|
|
|
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() {
|
|
|
|
logger.log.info('Process interrupted, shutting down...');
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function closeConnections(callback) {
|
|
|
|
const activeConnections = clientConns.getActiveConnections();
|
|
|
|
let i = activeConnections.length;
|
|
|
|
while(i--) {
|
|
|
|
activeConnections[i].term.write('\n\nServer is shutting down NOW! Disconnecting...\n\n');
|
|
|
|
clientConns.removeClient(activeConnections[i]);
|
|
|
|
}
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function stopEventScheduler(callback) {
|
|
|
|
if(initServices.eventScheduler) {
|
2016-06-21 03:05:47 +00:00
|
|
|
return initServices.eventScheduler.shutdown( () => {
|
|
|
|
callback(null); // ignore err
|
|
|
|
});
|
2016-06-21 02:39:20 +00:00
|
|
|
} else {
|
|
|
|
return callback(null);
|
|
|
|
}
|
2016-06-21 03:05:47 +00:00
|
|
|
},
|
|
|
|
function stopMsgNetwork(callback) {
|
|
|
|
require('./msg_network.js').shutdown(callback);
|
2016-06-21 02:39:20 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
() => {
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
function initialize(cb) {
|
|
|
|
async.series(
|
|
|
|
[
|
2015-11-06 23:14:30 +00:00
|
|
|
function createMissingDirectories(callback) {
|
|
|
|
async.each(Object.keys(conf.config.paths), function entry(pathKey, next) {
|
2016-03-29 04:07:21 +00:00
|
|
|
mkdirs(conf.config.paths[pathKey], function dirCreated(err) {
|
2015-11-06 23:14:30 +00:00
|
|
|
if(err) {
|
|
|
|
console.error('Could not create path: ' + conf.config.paths[pathKey] + ': ' + err.toString());
|
|
|
|
}
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
}, function dirCreationComplete(err) {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
2014-10-29 11:30:20 +00:00
|
|
|
function basicInit(callback) {
|
|
|
|
logger.init();
|
|
|
|
|
2016-06-21 02:39:20 +00:00
|
|
|
process.on('SIGINT', shutdownSystem);
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2015-08-27 22:14:56 +00:00
|
|
|
// Init some extensions
|
|
|
|
require('string-format').extend(String.prototype, require('./string_util.js').stringFormatExtensions);
|
2014-10-31 04:59:21 +00:00
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
callback(null);
|
2015-11-06 23:14:30 +00:00
|
|
|
},
|
2014-10-29 11:30:20 +00:00
|
|
|
function initDatabases(callback) {
|
2015-11-07 01:25:07 +00:00
|
|
|
database.initializeDatabases(callback);
|
2014-10-29 11:30:20 +00:00
|
|
|
},
|
2015-10-18 02:03:51 +00:00
|
|
|
function initSystemProperties(callback) {
|
|
|
|
require('./system_property.js').loadSystemProperties(callback);
|
|
|
|
},
|
2014-10-29 11:30:20 +00:00
|
|
|
function initThemes(callback) {
|
|
|
|
// Have to pull in here so it's after Config init
|
|
|
|
var theme = require('./theme.js');
|
|
|
|
theme.initAvailableThemes(function onThemesInit(err, themeCount) {
|
|
|
|
logger.log.info({ themeCount : themeCount }, 'Themes initialized');
|
|
|
|
callback(err);
|
|
|
|
});
|
2015-08-03 00:27:05 +00:00
|
|
|
},
|
|
|
|
function loadSysOpInformation(callback) {
|
|
|
|
//
|
|
|
|
// If user 1 has been created, we have a SysOp. Cache some information
|
|
|
|
// into Config.
|
|
|
|
//
|
|
|
|
var user = require('./user.js'); // must late load
|
|
|
|
|
|
|
|
user.getUserName(1, function unLoaded(err, sysOpUsername) {
|
|
|
|
if(err) {
|
|
|
|
callback(null); // non-fatal here
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Load some select properties to cache
|
|
|
|
//
|
|
|
|
var propLoadOpts = {
|
|
|
|
userId : 1,
|
|
|
|
names : [ 'real_name', 'sex', 'email_address' ],
|
2015-08-21 19:47:01 +00:00
|
|
|
};
|
2015-08-27 22:14:56 +00:00
|
|
|
|
2015-08-03 00:27:05 +00:00
|
|
|
user.loadProperties(propLoadOpts, function propsLoaded(err, props) {
|
|
|
|
if(!err) {
|
|
|
|
conf.config.general.sysOp = {
|
|
|
|
username : sysOpUsername,
|
|
|
|
properties : props,
|
|
|
|
};
|
|
|
|
|
|
|
|
logger.log.info( { sysOp : conf.config.general.sysOp }, 'System Operator information cached');
|
|
|
|
}
|
|
|
|
callback(null); // any error is again, non-fatal here
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-02-17 05:11:55 +00:00
|
|
|
},
|
|
|
|
function readyMessageNetworkSupport(callback) {
|
|
|
|
require('./msg_network.js').startup(callback);
|
2016-06-20 03:09:45 +00:00
|
|
|
},
|
|
|
|
function readyEventScheduler(callback) {
|
|
|
|
const EventSchedulerModule = require('./event_scheduler.js').EventSchedulerModule;
|
2016-06-21 02:39:20 +00:00
|
|
|
EventSchedulerModule.loadAndStart( (err, modInst) => {
|
|
|
|
initServices.eventScheduler = modInst;
|
|
|
|
return callback(err);
|
|
|
|
});
|
2014-10-29 11:30:20 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function onComplete(err) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
function startListening(cb) {
|
2014-10-17 02:21:06 +00:00
|
|
|
if(!conf.config.servers) {
|
|
|
|
// :TODO: Log error ... output to stderr as well. We can do it all with the logger
|
2016-02-17 05:11:55 +00:00
|
|
|
//logger.log.error('No servers configured');
|
|
|
|
cb(new Error('No servers configured'));
|
|
|
|
return;
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
let moduleUtil = require('./module_util.js'); // late load so we get Config
|
2015-04-21 04:50:58 +00:00
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
moduleUtil.loadModulesForCategory('servers', (err, module) => {
|
2014-10-17 02:21:06 +00:00
|
|
|
if(err) {
|
2016-07-16 19:05:32 +00:00
|
|
|
if('EENIGMODDISABLED' === err.code) {
|
|
|
|
logger.log.debug(err.message);
|
|
|
|
} else {
|
|
|
|
logger.log.info( { err : err }, 'Failed loading module');
|
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
const port = parseInt(module.runtime.config.port);
|
2014-10-17 02:21:06 +00:00
|
|
|
if(isNaN(port)) {
|
2015-10-22 04:51:35 +00:00
|
|
|
logger.log.error( { port : module.runtime.config.port, server : module.moduleInfo.name }, 'Cannot load server (Invalid port)');
|
2014-10-17 02:21:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-16 19:05:32 +00:00
|
|
|
const moduleInst = new module.getModule();
|
|
|
|
let server;
|
|
|
|
try {
|
|
|
|
server = moduleInst.createServer();
|
|
|
|
} catch(e) {
|
|
|
|
logger.log.warn(e, 'Exception caught creating server!');
|
|
|
|
return;
|
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
// :TODO: handle maxConnections, e.g. conf.maxConnections
|
|
|
|
|
2015-10-20 21:39:33 +00:00
|
|
|
server.on('client', function newClient(client, clientSock) {
|
2014-10-17 02:21:06 +00:00
|
|
|
//
|
|
|
|
// Start tracking the client. We'll assign it an ID which is
|
|
|
|
// just the index in our connections array.
|
2015-10-22 04:51:35 +00:00
|
|
|
//
|
2015-09-26 05:10:18 +00:00
|
|
|
if(_.isUndefined(client.session)) {
|
|
|
|
client.session = {};
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 06:03:18 +00:00
|
|
|
client.session.serverName = module.moduleInfo.name;
|
|
|
|
client.session.isSecure = module.moduleInfo.isSecure || false;
|
2015-10-22 04:51:35 +00:00
|
|
|
|
2015-10-20 21:39:33 +00:00
|
|
|
clientConns.addNewClient(client, clientSock);
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2015-10-22 18:22:03 +00:00
|
|
|
client.on('ready', function clientReady(readyOptions) {
|
2015-08-27 22:14:56 +00:00
|
|
|
|
2015-10-21 22:30:32 +00:00
|
|
|
client.startIdleMonitor();
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
// Go to module -- use default error handler
|
2015-10-22 18:22:03 +00:00
|
|
|
prepareClient(client, function clientPrepared() {
|
|
|
|
require('./connect.js').connectEntry(client, readyOptions.firstMenu);
|
2014-10-30 04:23:44 +00:00
|
|
|
});
|
2014-10-17 02:21:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
client.on('end', function onClientEnd() {
|
2015-08-05 04:35:59 +00:00
|
|
|
clientConns.removeClient(client);
|
2014-10-17 02:21:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
client.on('error', function onClientError(err) {
|
2015-09-26 05:10:18 +00:00
|
|
|
logger.log.info({ clientId : client.session.id }, 'Connection error: %s' % err.message);
|
2014-10-17 02:21:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
client.on('close', function onClientClose(hadError) {
|
|
|
|
var l = hadError ? logger.log.info : logger.log.debug;
|
2015-09-26 05:10:18 +00:00
|
|
|
l( { clientId : client.session.id }, 'Connection closed');
|
2015-08-05 04:35:59 +00:00
|
|
|
|
|
|
|
clientConns.removeClient(client);
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('idle timeout', function idleTimeout() {
|
2015-11-04 06:15:49 +00:00
|
|
|
client.log.info('User idle timeout expired');
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2015-11-04 06:15:49 +00:00
|
|
|
client.menuStack.goto('idleLogoff', function goMenuRes(err) {
|
2015-08-05 04:35:59 +00:00
|
|
|
if(err) {
|
|
|
|
// likely just doesn't exist
|
|
|
|
client.term.write('\nIdle timeout expired. Goodbye!\n');
|
|
|
|
client.end();
|
|
|
|
}
|
|
|
|
});
|
2014-10-17 02:21:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-06-22 03:11:11 +00:00
|
|
|
server.on('error', function serverErr(err) {
|
|
|
|
logger.log.info(err); // 'close' should be handled after
|
|
|
|
});
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
server.listen(port);
|
2016-02-17 05:11:55 +00:00
|
|
|
|
|
|
|
logger.log.info(
|
|
|
|
{ server : module.moduleInfo.name, port : port }, 'Listening for connections');
|
|
|
|
}, err => {
|
|
|
|
cb(err);
|
2014-10-17 02:21:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-30 04:23:44 +00:00
|
|
|
function prepareClient(client, cb) {
|
2015-09-27 21:35:24 +00:00
|
|
|
var theme = require('./theme.js');
|
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
// :TODO: it feels like this should go somewhere else... and be a bit more elegant.
|
2015-04-16 04:46:45 +00:00
|
|
|
|
2015-09-27 21:35:24 +00:00
|
|
|
if('*' === conf.config.preLoginTheme) {
|
2015-04-21 05:24:15 +00:00
|
|
|
client.user.properties.theme_id = theme.getRandomTheme() || '';
|
2014-10-30 04:23:44 +00:00
|
|
|
} else {
|
2015-04-21 04:50:58 +00:00
|
|
|
client.user.properties.theme_id = conf.config.preLoginTheme;
|
2014-10-30 04:23:44 +00:00
|
|
|
}
|
2016-01-15 05:44:33 +00:00
|
|
|
|
|
|
|
theme.setClientTheme(client, client.user.properties.theme_id);
|
|
|
|
cb(null); // note: currently useless to use cb here - but this may change...again...
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|