From 7343ab2ff25fc84da21968ef9e9ab6fd656bf165 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Thu, 22 Jun 2017 22:19:34 -0600 Subject: [PATCH] Updated Events - use glob for finding nested modules/etc. --- core/bbs.js | 12 ++-- core/client_connections.js | 6 +- core/connect.js | 4 +- core/events.js | 99 ++++++++++++++++++++------------- core/module_util.js | 5 +- core/scanner_tossers/ftn_bso.js | 6 +- core/servers/content/web.js | 2 +- core/user_login.js | 26 +++++---- package.json | 3 +- 9 files changed, 96 insertions(+), 67 deletions(-) diff --git a/core/bbs.js b/core/bbs.js index 4fb50c3a..75446c45 100644 --- a/core/bbs.js +++ b/core/bbs.js @@ -9,9 +9,7 @@ const conf = require('./config.js'); const logger = require('./logger.js'); const database = require('./database.js'); -const clientConns = require('./client_connections.js'); const resolvePath = require('./misc_util.js').resolvePath; -const events = require('./events.js'); // deps const async = require('async'); @@ -88,8 +86,6 @@ function main() { } ], function complete(err) { - events.registerModules(); - // note this is escaped: fs.readFile(paths.join(__dirname, '../misc/startup_banner.asc'), 'utf8', (err, banner) => { console.info(ENIGMA_COPYRIGHT); @@ -114,11 +110,12 @@ function shutdownSystem() { async.series( [ function closeConnections(callback) { - const activeConnections = clientConns.getActiveConnections(); + const ClientConns = require('./client_connections.js'); + 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]); + ClientConns.removeClient(activeConnections[i]); } callback(null); }, @@ -240,6 +237,9 @@ function initialize(cb) { function readyMessageNetworkSupport(callback) { return require('./msg_network.js').startup(callback); }, + function readyEvents(callback) { + return require('./events.js').startup(callback); + }, function listenConnections(callback) { return require('./listening_server.js').startup(callback); }, diff --git a/core/client_connections.js b/core/client_connections.js index 471b1b79..7e74e29d 100644 --- a/core/client_connections.js +++ b/core/client_connections.js @@ -3,7 +3,7 @@ // ENiGMA½ const logger = require('./logger.js'); -const events = require('./events.js'); +const Events = require('./events.js'); // deps const _ = require('lodash'); @@ -77,7 +77,7 @@ function addNewClient(client, clientSock) { client.log.info(connInfo, 'Client connected'); - events.emit('codes.l33t.enigma.system.connected', {'client': client}); + Events.emit('codes.l33t.enigma.system.connected', { client : client, connectionCount : clientConnections.length } ); return id; } @@ -97,7 +97,7 @@ function removeClient(client) { 'Client disconnected' ); - events.emit('codes.l33t.enigma.system.disconnected', {'client': client}); + Events.emit('codes.l33t.enigma.system.disconnected', { client : client, connectionCount : clientConnections.length } ); } } diff --git a/core/connect.js b/core/connect.js index 85273577..b94fa586 100644 --- a/core/connect.js +++ b/core/connect.js @@ -3,7 +3,7 @@ // ENiGMA½ const ansi = require('./ansi_term.js'); -const events = require('./events.js'); +const Events = require('./events.js'); // deps const async = require('async'); @@ -177,7 +177,7 @@ function connectEntry(client, nextMenu) { displayBanner(term); // fire event - events.emit('codes.l33t.enigma.system.term_detected', {'client': client}); + Events.emit('codes.l33t.enigma.system.term_detected', { client : client } ); setTimeout( () => { return client.menuStack.goto(nextMenu); diff --git a/core/events.js b/core/events.js index 79b41429..8e16a374 100644 --- a/core/events.js +++ b/core/events.js @@ -1,50 +1,73 @@ /* jslint node: true */ 'use strict'; -const Config = require('./config.js'); -const fs = require('fs'); -const path = require('path'); -const events = require('events'); -const logger = require('./logger.js'); +const paths = require('path'); +const events = require('events'); +const Log = require('./logger.js').log; -var eventEmitter = new events.EventEmitter(); +// deps +const _ = require('lodash'); +const async = require('async'); +const glob = require('glob'); -var self = module.exports = { - emit: function(eventName, args) { - logger.log.debug("Emit "+eventName); - eventEmitter.emit(eventName, args); - }, - on: function(eventName, listener) { - logger.log.debug("Register listener for "+eventName); - eventEmitter.on(eventName, listener); - }, - remove: function(eventName, listener) { - logger.log.debug("Remove listener for "+eventName); - eventEmitter.removeListener(eventName, listener); - }, - registerModules: function() { - const moduleUtil = require('./module_util.js'); +module.exports = new class Events extends events.EventEmitter { + constructor() { + super(); + } - moduleUtil.getModulePaths().forEach(function(modulePath) { - var mods = fs.readdirSync(modulePath); - mods.forEach(function(item) { - var modPath = modulePath+item; - if (item.substr(item.length-3) != '.js') { - modPath += path.sep+item+'.js'; + addListener(event, listener) { + Log.trace( { event : event }, 'Registering event listener'); + return super.addListener(event, listener); + } + + emit(event, ...args) { + Log.trace( { event : event }, 'Emitting event'); + return super.emit(event, args); + } + + on(event, listener) { + Log.trace( { event : event }, 'Registering event listener'); + return super.on(event, listener); + } + + once(event, listener) { + Log.trace( { event : event }, 'Registering single use event listener'); + return super.once(event, listener); + } + + removeListener(event, listener) { + Log.trace( { event : event }, 'Removing listener'); + return super.removeListener(event, listener); + } + + startup(cb) { + async.each(require('./module_util.js').getModulePaths(), (modulePath, nextPath) => { + glob('*{.js,/*.js}', { cwd : modulePath }, (err, files) => { + if(err) { + return nextPath(err); } - if (fs.existsSync(modPath)) { - var module = require(modPath); - if (module.registerEvents !== undefined) { - logger.log.debug(modPath+" calling registerEvents function"); - module.registerEvents(); - } else { - logger.log.debug(modPath+" has no registerEvents function"); + async.each(files, (moduleName, nextModule) => { + modulePath = paths.join(modulePath, moduleName); + + try { + const mod = require(modulePath); + + if(_.isFunction(mod.registerEvents)) { + // :TODO: ... or just systemInit() / systemShutdown() & mods could call Events.on() / Events.removeListener() ? + mod.registerEvents(this); + } + } catch(e) { + } - } else { - logger.log.debug(modPath+" - file not found"); - } + + return nextModule(null); + }, err => { + return nextPath(err); + }); }); + }, err => { + return cb(err); }); } -} +}; diff --git a/core/module_util.js b/core/module_util.js index 194efca5..67e87306 100644 --- a/core/module_util.js +++ b/core/module_util.js @@ -101,6 +101,9 @@ function loadModulesForCategory(category, iterator, complete) { function getModulePaths() { return [ - Config.paths.mods + Config.paths.mods, + Config.paths.loginServers, + Config.paths.contentServers, + Config.paths.scannerTossers, ]; } diff --git a/core/scanner_tossers/ftn_bso.js b/core/scanner_tossers/ftn_bso.js index 21c8b1e0..8381789e 100644 --- a/core/scanner_tossers/ftn_bso.js +++ b/core/scanner_tossers/ftn_bso.js @@ -297,10 +297,10 @@ function FTNMessageScanTossModule() { }); }, (err, finalSuffix) => { if(finalSuffix) { - cb(null, paths.join(basePath, fileName + finalSuffix)); - } else { - cb(new Error('Could not acquire a bundle filename!')); + return cb(null, paths.join(basePath, fileName + finalSuffix)); } + + return cb(new Error('Could not acquire a bundle filename!')); }); }; diff --git a/core/servers/content/web.js b/core/servers/content/web.js index 9ec194a0..048de693 100644 --- a/core/servers/content/web.js +++ b/core/servers/content/web.js @@ -116,7 +116,7 @@ exports.getModule = class WebServerModule extends ServerModule { // additional options Object.assign(options, Config.contentServers.web.https.options || {} ); - this.httpsServer = https.createServer(options, this.routeRequest); + this.httpsServer = https.createServer(options, this.routeRequest); } } diff --git a/core/user_login.js b/core/user_login.js index 76b01598..4bd9176c 100644 --- a/core/user_login.js +++ b/core/user_login.js @@ -37,14 +37,16 @@ function userLogin(client, username, password, cb) { }); if(existingClientConnection) { - client.log.info( { - existingClientId : existingClientConnection.session.id, - username : user.username, - userId : user.userId }, + client.log.info( + { + existingClientId : existingClientConnection.session.id, + username : user.username, + userId : user.userId + }, 'Already logged in' ); - var existingConnError = new Error('Already logged in as supplied user'); + const existingConnError = new Error('Already logged in as supplied user'); existingConnError.existingConn = true; // :TODO: We should use EnigError & pass existing connection as second param @@ -61,24 +63,24 @@ function userLogin(client, username, password, cb) { [ function setTheme(callback) { setClientTheme(client, user.properties.theme_id); - callback(null); + return callback(null); }, function updateSystemLoginCount(callback) { - StatLog.incrementSystemStat('login_count', 1, callback); + return StatLog.incrementSystemStat('login_count', 1, callback); }, function recordLastLogin(callback) { - StatLog.setUserStat(user, 'last_login_timestamp', StatLog.now, callback); + return StatLog.setUserStat(user, 'last_login_timestamp', StatLog.now, callback); }, function updateUserLoginCount(callback) { - StatLog.incrementUserStat(user, 'login_count', 1, callback); + return StatLog.incrementUserStat(user, 'login_count', 1, callback); }, function recordLoginHistory(callback) { const LOGIN_HISTORY_MAX = 200; // history of up to last 200 callers - StatLog.appendSystemLogEntry('user_login_history', user.userId, LOGIN_HISTORY_MAX, StatLog.KeepType.Max, callback); + return StatLog.appendSystemLogEntry('user_login_history', user.userId, LOGIN_HISTORY_MAX, StatLog.KeepType.Max, callback); } ], - function complete(err) { - cb(err); + err => { + return cb(err); } ); }); diff --git a/package.json b/package.json index 494fc495..252b6763 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,8 @@ "uuid-parse": "^1.0.0", "ws" : "^3.0.0", "graceful-fs" : "^4.1.11", - "exiftool" : "^0.0.3" + "exiftool" : "^0.0.3", + "node-glob" : "^1.2.0" }, "devDependencies": {}, "engines": {