From c45824b3ad40c252ce8d17111c90655d09619e2a Mon Sep 17 00:00:00 2001 From: "Josh M. McKee" Date: Sat, 10 Jun 2017 18:46:28 -0700 Subject: [PATCH] Rename connect event to term_detected, add connected and disconnected events --- core/client_connections.js | 25 +++++++++++++++---------- core/connect.js | 2 +- core/events.js | 2 +- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/core/client_connections.js b/core/client_connections.js index 5cbeb3b9..471b1b79 100644 --- a/core/client_connections.js +++ b/core/client_connections.js @@ -3,6 +3,7 @@ // ENiGMA½ const logger = require('./logger.js'); +const events = require('./events.js'); // deps const _ = require('lodash'); @@ -22,7 +23,7 @@ function getActiveConnections() { return clientConnections; } function getActiveNodeList(authUsersOnly) { if(!_.isBoolean(authUsersOnly)) { - authUsersOnly = true; + authUsersOnly = true; } const now = moment(); @@ -30,7 +31,7 @@ function getActiveNodeList(authUsersOnly) { const activeConnections = getActiveConnections().filter(ac => { return ((authUsersOnly && ac.user.isAuthenticated()) || !authUsersOnly); }); - + return _.map(activeConnections, ac => { const entry = { node : ac.node, @@ -41,7 +42,7 @@ function getActiveNodeList(authUsersOnly) { // // There may be a connection, but not a logged in user as of yet - // + // if(ac.user.isAuthenticated()) { entry.userName = ac.user.username; entry.realName = ac.user.properties.real_name; @@ -49,7 +50,7 @@ function getActiveNodeList(authUsersOnly) { entry.affils = ac.user.properties.affiliation; const diff = now.diff(moment(ac.user.properties.last_login_timestamp), 'minutes'); - entry.timeOn = moment.duration(diff, 'minutes'); + entry.timeOn = moment.duration(diff, 'minutes'); } return entry; }); @@ -59,7 +60,7 @@ function addNewClient(client, clientSock) { const id = client.session.id = clientConnections.push(client) - 1; const remoteAddress = client.remoteAddress = clientSock.remoteAddress; - // Create a client specific logger + // Create a client specific logger // Note that this will be updated @ login with additional information client.log = logger.log.child( { clientId : id } ); @@ -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; } @@ -85,17 +88,19 @@ function removeClient(client) { const i = clientConnections.indexOf(client); if(i > -1) { clientConnections.splice(i, 1); - + logger.log.info( - { + { connectionCount : clientConnections.length, - clientId : client.session.id - }, + clientId : client.session.id + }, 'Client disconnected' ); + + events.emit('codes.l33t.enigma.system.disconnected', {'client': client}); } } function getConnectionByUserId(userId) { return getActiveConnections().find( ac => userId === ac.user.userId ); -} \ No newline at end of file +} diff --git a/core/connect.js b/core/connect.js index 21ab421a..0939906e 100644 --- a/core/connect.js +++ b/core/connect.js @@ -177,7 +177,7 @@ function connectEntry(client, nextMenu) { displayBanner(term); // fire event - events.emit('codes.l33t.enigma.system.connect', {'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 f558ed21..1c3b28b9 100644 --- a/core/events.js +++ b/core/events.js @@ -32,7 +32,7 @@ var self = module.exports = { } if (fs.existsSync(modPath)) { var module = require(modPath); - + if (module.registerEvents !== undefined) { logger.log.debug(modPath+" calling registerEvents function"); module.registerEvents();