2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
|
|
|
var conf = require('./config.js');
|
2015-03-19 05:08:23 +00:00
|
|
|
var moduleUtil = require('./module_util.js');
|
2014-10-17 02:21:06 +00:00
|
|
|
var logger = require('./logger.js');
|
|
|
|
var miscUtil = require('./misc_util.js');
|
2014-10-20 05:30:44 +00:00
|
|
|
var database = require('./database.js');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
var iconv = require('iconv-lite');
|
|
|
|
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');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
exports.bbsMain = bbsMain;
|
|
|
|
|
|
|
|
function bbsMain() {
|
2014-10-17 02:21:06 +00:00
|
|
|
var mainArgs = parseArgs();
|
|
|
|
|
|
|
|
var configPathSupplied = false;
|
|
|
|
var configPath = conf.defaultPath();
|
|
|
|
|
|
|
|
if(mainArgs.indexOf('--help') > 0) {
|
|
|
|
// :TODO: display help
|
|
|
|
} else {
|
|
|
|
var argCount = mainArgs.length;
|
|
|
|
for(var i = 0; i < argCount; ++i) {
|
|
|
|
var arg = mainArgs[i];
|
|
|
|
if('--config' == arg) {
|
|
|
|
configPathSupplied = true;
|
|
|
|
configPath = mainArgs[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
conf.initFromFile(configPath);
|
|
|
|
} catch(e) {
|
|
|
|
//
|
|
|
|
// If the user supplied a config and we can't read, parse, whatever
|
|
|
|
// then output a error and bail.
|
|
|
|
//
|
|
|
|
if(configPathSupplied) {
|
|
|
|
if(e.code === 'ENOENT') {
|
|
|
|
console.error('Configuration file does not exist: ' + configPath);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('No configuration file found, creating defaults.');
|
|
|
|
conf.createDefault();
|
|
|
|
}
|
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
initialize(function onInit(err) {
|
|
|
|
if(err) {
|
|
|
|
console.error('Error initializing: ' + util.inspect(err));
|
|
|
|
return;
|
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
startListening();
|
2014-10-17 02:21:06 +00:00
|
|
|
});
|
2014-10-31 04:59:21 +00:00
|
|
|
}
|
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();
|
|
|
|
});
|
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
iconv.extendNodeEncodings();
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function onComplete(err) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
var clientConnections = [];
|
|
|
|
|
|
|
|
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-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.
|
|
|
|
//
|
|
|
|
if(typeof client.runtime === 'undefined') {
|
|
|
|
client.runtime = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
addNewClient(client);
|
|
|
|
|
|
|
|
//logger.log.info({ clientId : client.runtime.id, from : client.address(), server : module.moduleInfo.name }, 'Client connected');
|
|
|
|
|
|
|
|
client.on('ready', function onClientReady() {
|
|
|
|
// 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() {
|
|
|
|
logger.log.info({ clientId : client.runtime.id }, 'Client disconnected');
|
|
|
|
|
|
|
|
removeClient(client);
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
l({ clientId : client.runtime.id }, 'Connection closed');
|
|
|
|
removeClient(client);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(port);
|
|
|
|
logger.log.info({ server : module.moduleInfo.name, port : port }, 'Listening for connections');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addNewClient(client) {
|
|
|
|
var id = client.runtime.id = clientConnections.push(client) - 1;
|
|
|
|
logger.log.debug('Connection count is now %d', clientConnections.length);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeClient(client) {
|
|
|
|
var i = clientConnections.indexOf(client);
|
|
|
|
if(i > -1) {
|
|
|
|
clientConnections.splice(i, 1);
|
|
|
|
logger.log.debug('Connection count is now %d', clientConnections.length);
|
|
|
|
}
|
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');
|
|
|
|
theme.getRandomTheme(function onRandTheme(err, themeId) {
|
2014-10-29 11:30:20 +00:00
|
|
|
client.user.properties.art_theme_id = themeId || '';
|
|
|
|
cb(null);
|
2014-10-30 04:23:44 +00:00
|
|
|
});
|
|
|
|
} else {
|
2014-10-29 11:30:20 +00:00
|
|
|
client.user.properties.art_theme_id = conf.config.preLoginTheme;
|
|
|
|
cb(null);
|
2014-10-30 04:23:44 +00:00
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|