2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
|
|
|
var conf = require('./config.js');
|
|
|
|
var logger = require('./logger.js');
|
|
|
|
var miscUtil = require('./misc_util.js');
|
2014-10-20 05:30:44 +00:00
|
|
|
var database = require('./database.js');
|
2015-08-05 04:35:59 +00:00
|
|
|
var clientConns = require('./client_connections.js');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
var paths = require('path');
|
2014-10-29 11:30:20 +00:00
|
|
|
var async = require('async');
|
2014-10-31 04:59:21 +00:00
|
|
|
var util = require('util');
|
2015-04-19 08:13:13 +00:00
|
|
|
var _ = require('lodash');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
exports.bbsMain = bbsMain;
|
|
|
|
|
2015-04-19 08:13:13 +00:00
|
|
|
function bbsMain() {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function processArgs(callback) {
|
|
|
|
var args = parseArgs();
|
|
|
|
|
|
|
|
var configPath;
|
|
|
|
|
|
|
|
if(args.indexOf('--help') > 0) {
|
|
|
|
// :TODO: display help
|
|
|
|
} else {
|
|
|
|
var argCount = args.length;
|
|
|
|
for(var i = 0; i < argCount; ++i) {
|
|
|
|
var arg = args[i];
|
|
|
|
if('--config' == arg) {
|
|
|
|
configPath = args[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var configPathSupplied = _.isString(configPath);
|
|
|
|
callback(null, configPath || conf.getDefaultPath(), configPathSupplied);
|
|
|
|
},
|
|
|
|
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
|
|
|
|
//
|
|
|
|
if(configPathSupplied && err) {
|
|
|
|
if('ENOENT' === err.code) {
|
|
|
|
console.error('Configuration file does not existing: ' + configPath);
|
|
|
|
} else {
|
|
|
|
console.error('Failed parsing configuration: ' + configPath);
|
|
|
|
}
|
|
|
|
callback(err);
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function initSystem(callback) {
|
|
|
|
initialize(function init(err) {
|
|
|
|
if(err) {
|
|
|
|
console.error('Error initializing: ' + util.inspect(err));
|
|
|
|
}
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
if(!err) {
|
|
|
|
startListening();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
function parseArgs() {
|
|
|
|
var args = [];
|
|
|
|
process.argv.slice(2).forEach(function(val, index, array) {
|
|
|
|
args.push(val);
|
|
|
|
});
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
function initialize(cb) {
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function basicInit(callback) {
|
|
|
|
logger.init();
|
|
|
|
|
|
|
|
process.on('SIGINT', function onSigInt() {
|
|
|
|
// :TODO: for any client in |clientConnections|, if 'ready', send a "Server Disconnecting" + semi-gracefull hangup
|
|
|
|
// e.g. client.disconnectNow()
|
|
|
|
|
|
|
|
logger.log.info('Process interrupted, shutting down');
|
|
|
|
process.exit();
|
|
|
|
});
|
|
|
|
|
2015-08-27 22:14:56 +00:00
|
|
|
// Init some extensions
|
|
|
|
require('iconv-lite').extendNodeEncodings();
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
function initDatabases(callback) {
|
|
|
|
database.initializeDatabases();
|
2014-10-31 04:59:21 +00:00
|
|
|
callback(null);
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-10-29 11:30:20 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function onComplete(err) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
function startListening() {
|
|
|
|
if(!conf.config.servers) {
|
|
|
|
// :TODO: Log error ... output to stderr as well. We can do it all with the logger
|
2014-10-29 07:33:04 +00:00
|
|
|
logger.log.error('No servers configured');
|
2014-10-17 02:21:06 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
var moduleUtil = require('./module_util.js'); // late load so we get Config
|
|
|
|
|
2015-03-19 05:08:23 +00:00
|
|
|
moduleUtil.loadModulesForCategory('servers', function onServerModule(err, module) {
|
2014-10-17 02:21:06 +00:00
|
|
|
if(err) {
|
|
|
|
logger.log.info(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var port = parseInt(module.runtime.config.port);
|
|
|
|
if(isNaN(port)) {
|
|
|
|
logger.log.error({ port : module.runtime.config.port, server : module.moduleInfo.name }, 'Cannot load server (Invalid port)');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-19 05:08:23 +00:00
|
|
|
var moduleInst = new module.getModule();
|
|
|
|
var server = moduleInst.createServer();
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
// :TODO: handle maxConnections, e.g. conf.maxConnections
|
|
|
|
|
|
|
|
server.on('client', function onClient(client) {
|
|
|
|
//
|
|
|
|
// Start tracking the client. We'll assign it an ID which is
|
|
|
|
// just the index in our connections array.
|
|
|
|
//
|
2015-07-02 05:41:20 +00:00
|
|
|
if(_.isUndefined(client.runtime)) {
|
2014-10-17 02:21:06 +00:00
|
|
|
client.runtime = {};
|
|
|
|
}
|
|
|
|
|
2015-08-05 04:35:59 +00:00
|
|
|
clientConns.addNewClient(client);
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
client.on('ready', function onClientReady() {
|
2015-08-27 22:14:56 +00:00
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
// Go to module -- use default error handler
|
2014-10-30 04:23:44 +00:00
|
|
|
prepareClient(client, function onPrepared() {
|
2014-10-31 04:59:21 +00:00
|
|
|
require('./connect.js').connectEntry(client);
|
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) {
|
|
|
|
logger.log.info({ clientId : client.runtime.id }, 'Connection error: %s' % err.message);
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('close', function onClientClose(hadError) {
|
|
|
|
var l = hadError ? logger.log.info : logger.log.debug;
|
2015-08-01 07:00:15 +00:00
|
|
|
l( { clientId : client.runtime.id }, 'Connection closed');
|
2015-08-05 04:35:59 +00:00
|
|
|
|
|
|
|
clientConns.removeClient(client);
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('idle timeout', function idleTimeout() {
|
|
|
|
client.log.info('User idle timeout expired');
|
|
|
|
|
|
|
|
client.gotoMenuModule( { name : 'idleLogoff' }, function goMenuRes(err) {
|
|
|
|
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);
|
|
|
|
logger.log.info({ server : module.moduleInfo.name, port : port }, 'Listening for connections');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-30 04:23:44 +00:00
|
|
|
function prepareClient(client, cb) {
|
2014-10-31 04:59:21 +00:00
|
|
|
// :TODO: it feels like this should go somewhere else... and be a bit more elegant.
|
2014-10-30 04:23:44 +00:00
|
|
|
if('*' === conf.config.preLoginTheme) {
|
|
|
|
var theme = require('./theme.js');
|
2015-04-16 04:46:45 +00:00
|
|
|
|
2015-04-21 05:24:15 +00:00
|
|
|
client.user.properties.theme_id = theme.getRandomTheme() || '';
|
|
|
|
|
2015-05-13 05:04:22 +00:00
|
|
|
theme.loadTheme(client.user.properties.theme_id, function themeLoaded(err, theme) {
|
|
|
|
client.currentTheme = theme;
|
2015-04-21 05:24:15 +00:00
|
|
|
cb(null);
|
|
|
|
});
|
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-29 11:30:20 +00:00
|
|
|
cb(null);
|
2014-10-30 04:23:44 +00:00
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|