enigma-bbs/core/login_server_module.js

90 lines
2.8 KiB
JavaScript
Raw Normal View History

/* jslint node: true */
'use strict';
// ENiGMA½
const conf = require('./config.js');
const logger = require('./logger.js');
const ServerModule = require('./server_module.js').ServerModule;
const clientConns = require('./client_connections.js');
2018-11-24 00:41:16 +00:00
const UserProps = require('./user_property.js');
// deps
const _ = require('lodash');
module.exports = class LoginServerModule extends ServerModule {
constructor() {
super();
}
// :TODO: we need to max connections -- e.g. from config 'maxConnections'
prepareClient(client, cb) {
const theme = require('./theme.js');
//
// Choose initial theme before we have user context
//
const preLoginTheme = _.get(conf.config, 'theme.preLogin');
if('*' === preLoginTheme) {
2018-11-24 00:41:16 +00:00
client.user.properties[UserProps.ThemeId] = theme.getRandomTheme() || '';
} else {
2018-11-24 00:41:16 +00:00
client.user.properties[UserProps.ThemeId] = preLoginTheme;
}
2018-11-24 00:41:16 +00:00
theme.setClientTheme(client, client.user.properties[UserProps.ThemeId]);
return cb(null); // note: currently useless to use cb here - but this may change...again...
}
handleNewClient(client, clientSock, modInfo) {
//
2019-01-04 04:02:57 +00:00
// Start tracking the client. A session ID aka client ID
// will be established in addNewClient() below.
//
if(_.isUndefined(client.session)) {
client.session = {};
}
client.session.serverName = modInfo.name;
client.session.isSecure = _.isBoolean(client.isSecure) ? client.isSecure : (modInfo.isSecure || false);
clientConns.addNewClient(client, clientSock);
client.on('ready', readyOptions => {
client.startIdleMonitor();
// Go to module -- use default error handler
this.prepareClient(client, () => {
require('./connect.js').connectEntry(client, readyOptions.firstMenu);
});
});
client.on('end', () => {
clientConns.removeClient(client);
});
client.on('error', err => {
logger.log.info({ clientId : client.session.id }, 'Connection error: %s' % err.message);
});
client.on('close', err => {
const logFunc = err ? logger.log.info : logger.log.debug;
logFunc( { clientId : client.session.id }, 'Connection closed');
clientConns.removeClient(client);
});
client.on('idle timeout', () => {
client.log.info('User idle timeout expired');
client.menuStack.goto('idleLogoff', err => {
if(err) {
// likely just doesn't exist
client.term.write('\nIdle timeout expired. Goodbye!\n');
client.end();
}
});
});
}
};