Merge branch 'master' of github.com:NuSkooler/enigma-bbs into 0.0.7-alpha

This commit is contained in:
Bryan Ashby 2017-06-11 20:22:10 -06:00
commit 2055c76f98
7 changed files with 111 additions and 41 deletions

View File

@ -11,6 +11,7 @@ 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');
@ -84,9 +85,11 @@ function main() {
}
return callback(err);
});
},
}
],
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);

View File

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

View File

@ -3,6 +3,7 @@
// ENiGMA½
const ansi = require('./ansi_term.js');
const events = require('./events.js');
// deps
const async = require('async');
@ -175,6 +176,9 @@ function connectEntry(client, nextMenu) {
//
displayBanner(term);
// fire event
events.emit('codes.l33t.enigma.system.term_detected', {'client': client});
setTimeout( () => {
return client.menuStack.goto(nextMenu);
}, 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.loadModule = loadModule;
exports.loadModulesForCategory = loadModulesForCategory;
exports.getModulePaths = getModulePaths;
function loadModuleEx(options, cb) {
assert(_.isObject(options));
@ -97,3 +98,9 @@ function loadModulesForCategory(category, iterator, complete) {
});
});
}
function getModulePaths() {
return [
Config.paths.mods
];
}

View File

@ -59,7 +59,8 @@ function WebSocketClient(ws, req, serverType) {
});
ws.on('close', () => {
this.end();
// we'll remove client connection which will in turn end() via our SocketBridge above
return this.emit('end');
});
//