Merge pull request #114 from snazzware/master

Events subsystem
This commit is contained in:
Bryan Ashby 2017-06-11 20:56:04 -06:00 committed by GitHub
commit d92b8e6e2c
6 changed files with 108 additions and 39 deletions

View File

@ -11,6 +11,7 @@ const logger = require('./logger.js');
const database = require('./database.js'); const database = require('./database.js');
const clientConns = require('./client_connections.js'); const clientConns = require('./client_connections.js');
const resolvePath = require('./misc_util.js').resolvePath; const resolvePath = require('./misc_util.js').resolvePath;
const events = require('./events.js');
// deps // deps
const async = require('async'); const async = require('async');
@ -84,9 +85,11 @@ function main() {
} }
return callback(err); return callback(err);
}); });
}, }
], ],
function complete(err) { function complete(err) {
events.registerModules();
// note this is escaped: // note this is escaped:
fs.readFile(paths.join(__dirname, '../misc/startup_banner.asc'), 'utf8', (err, banner) => { fs.readFile(paths.join(__dirname, '../misc/startup_banner.asc'), 'utf8', (err, banner) => {
console.info(ENIGMA_COPYRIGHT); console.info(ENIGMA_COPYRIGHT);

View File

@ -3,6 +3,7 @@
// ENiGMA½ // ENiGMA½
const logger = require('./logger.js'); const logger = require('./logger.js');
const events = require('./events.js');
// deps // deps
const _ = require('lodash'); const _ = require('lodash');
@ -76,6 +77,8 @@ function addNewClient(client, clientSock) {
client.log.info(connInfo, 'Client connected'); client.log.info(connInfo, 'Client connected');
events.emit('codes.l33t.enigma.system.connected', {'client': client});
return id; return id;
} }
@ -93,6 +96,8 @@ function removeClient(client) {
}, },
'Client disconnected' 'Client disconnected'
); );
events.emit('codes.l33t.enigma.system.disconnected', {'client': client});
} }
} }

View File

@ -3,6 +3,7 @@
// ENiGMA½ // ENiGMA½
const ansi = require('./ansi_term.js'); const ansi = require('./ansi_term.js');
const events = require('./events.js');
// deps // deps
const async = require('async'); const async = require('async');
@ -175,6 +176,9 @@ function connectEntry(client, nextMenu) {
// //
displayBanner(term); displayBanner(term);
// fire event
events.emit('codes.l33t.enigma.system.term_detected', {'client': client});
setTimeout( () => { setTimeout( () => {
return client.menuStack.goto(nextMenu); return client.menuStack.goto(nextMenu);
}, 500); }, 500);

50
core/events.js Normal file
View File

@ -0,0 +1,50 @@
/* 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');
var eventEmitter = new events.EventEmitter();
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');
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';
}
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");
}
} else {
logger.log.debug(modPath+" - file not found");
}
});
});
}
}

View File

@ -15,6 +15,7 @@ const async = require('async');
exports.loadModuleEx = loadModuleEx; exports.loadModuleEx = loadModuleEx;
exports.loadModule = loadModule; exports.loadModule = loadModule;
exports.loadModulesForCategory = loadModulesForCategory; exports.loadModulesForCategory = loadModulesForCategory;
exports.getModulePaths = getModulePaths;
function loadModuleEx(options, cb) { function loadModuleEx(options, cb) {
assert(_.isObject(options)); assert(_.isObject(options));
@ -97,3 +98,9 @@ function loadModulesForCategory(category, iterator, complete) {
}); });
}); });
} }
function getModulePaths() {
return [
Config.paths.mods
];
}