2018-11-13 05:03:28 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2018-12-01 06:20:44 +00:00
|
|
|
const { MenuModule } = require('./menu_module.js');
|
2018-11-13 05:03:28 +00:00
|
|
|
const {
|
2018-11-14 03:03:54 +00:00
|
|
|
getActiveConnectionList,
|
2018-11-13 05:03:28 +00:00
|
|
|
getConnectionByNodeId,
|
2018-12-01 06:20:44 +00:00
|
|
|
} = require('./client_connections.js');
|
|
|
|
const UserInterruptQueue = require('./user_interrupt_queue.js');
|
|
|
|
const { getThemeArt } = require('./theme.js');
|
|
|
|
const { pipeToAnsi } = require('./color_codes.js');
|
|
|
|
const stringFormat = require('./string_format.js');
|
|
|
|
const { renderStringLength } = require('./string_util.js');
|
2018-12-02 00:00:07 +00:00
|
|
|
const Events = require('./events.js');
|
2018-11-13 05:03:28 +00:00
|
|
|
|
|
|
|
// deps
|
2018-11-14 04:11:33 +00:00
|
|
|
const series = require('async/series');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const async = require('async');
|
2018-11-15 02:52:55 +00:00
|
|
|
const moment = require('moment');
|
2018-11-13 05:03:28 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
|
|
|
name : 'Node Message',
|
|
|
|
desc : 'Multi-node messaging',
|
|
|
|
author : 'NuSkooler',
|
|
|
|
};
|
|
|
|
|
|
|
|
const FormIds = {
|
|
|
|
sendMessage : 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
const MciViewIds = {
|
|
|
|
sendMessage : {
|
|
|
|
nodeSelect : 1,
|
|
|
|
message : 2,
|
|
|
|
preview : 3,
|
|
|
|
|
|
|
|
customRangeStart : 10,
|
|
|
|
}
|
2018-12-02 00:00:07 +00:00
|
|
|
};
|
2018-11-13 05:03:28 +00:00
|
|
|
|
|
|
|
exports.getModule = class NodeMessageModule extends MenuModule {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
this.config = Object.assign({}, _.get(options, 'menuConfig.config'), { extraArgs : options.extraArgs });
|
|
|
|
|
|
|
|
this.menuMethods = {
|
|
|
|
sendMessage : (formData, extraArgs, cb) => {
|
2018-11-15 03:51:11 +00:00
|
|
|
const nodeId = this.nodeList[formData.value.node].node; // index from from -> node!
|
2018-12-01 06:20:44 +00:00
|
|
|
const message = _.get(formData.value, 'message', '').trim();
|
|
|
|
|
|
|
|
if(0 === renderStringLength(message)) {
|
|
|
|
return this.prevMenu(cb);
|
|
|
|
}
|
2018-11-13 05:03:28 +00:00
|
|
|
|
2018-11-14 04:11:33 +00:00
|
|
|
this.createInterruptItem(message, (err, interruptItem) => {
|
2018-11-15 03:51:11 +00:00
|
|
|
if(-1 === nodeId) {
|
2018-11-14 04:11:33 +00:00
|
|
|
// ALL nodes
|
2018-12-01 06:20:44 +00:00
|
|
|
UserInterruptQueue.queue(interruptItem, { omit : this.client });
|
2018-11-14 04:11:33 +00:00
|
|
|
} else {
|
2018-11-15 03:51:11 +00:00
|
|
|
const conn = getConnectionByNodeId(nodeId);
|
|
|
|
if(conn) {
|
2018-12-01 06:20:44 +00:00
|
|
|
UserInterruptQueue.queue(interruptItem, { clients : conn } );
|
2018-11-15 03:51:11 +00:00
|
|
|
}
|
2018-11-14 04:11:33 +00:00
|
|
|
}
|
2018-11-13 05:03:28 +00:00
|
|
|
|
2018-12-02 00:00:07 +00:00
|
|
|
Events.emit(Events.getSystemEvents().UserSendNodeMsg, { user : this.client.user } );
|
|
|
|
|
2018-11-14 04:11:33 +00:00
|
|
|
return this.prevMenu(cb);
|
|
|
|
});
|
2018-11-13 05:03:28 +00:00
|
|
|
},
|
2018-12-01 06:20:44 +00:00
|
|
|
};
|
2018-11-13 05:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mciReady(mciData, cb) {
|
|
|
|
super.mciReady(mciData, err => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
series(
|
|
|
|
[
|
2018-11-15 03:51:11 +00:00
|
|
|
(callback) => {
|
|
|
|
return this.prepViewController('sendMessage', FormIds.sendMessage, mciData.menu, callback);
|
|
|
|
},
|
|
|
|
(callback) => {
|
|
|
|
return this.validateMCIByViewIds(
|
|
|
|
'sendMessage',
|
|
|
|
[ MciViewIds.sendMessage.nodeSelect, MciViewIds.sendMessage.message ],
|
|
|
|
callback
|
|
|
|
);
|
2018-11-13 05:03:28 +00:00
|
|
|
},
|
2018-11-15 03:51:11 +00:00
|
|
|
(callback) => {
|
2018-11-13 05:03:28 +00:00
|
|
|
const nodeSelectView = this.viewControllers.sendMessage.getView(MciViewIds.sendMessage.nodeSelect);
|
|
|
|
this.prepareNodeList();
|
|
|
|
|
|
|
|
nodeSelectView.on('index update', idx => {
|
|
|
|
this.nodeListSelectionIndexUpdate(idx);
|
|
|
|
});
|
|
|
|
|
|
|
|
nodeSelectView.setItems(this.nodeList);
|
|
|
|
nodeSelectView.redraw();
|
|
|
|
this.nodeListSelectionIndexUpdate(0);
|
2018-11-15 03:51:11 +00:00
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
(callback) => {
|
|
|
|
const previewView = this.viewControllers.sendMessage.getView(MciViewIds.sendMessage.preview);
|
|
|
|
if(!previewView) {
|
|
|
|
return callback(null); // preview is optional
|
|
|
|
}
|
|
|
|
|
|
|
|
const messageView = this.viewControllers.sendMessage.getView(MciViewIds.sendMessage.message);
|
|
|
|
let timerId;
|
|
|
|
messageView.on('key press', () => {
|
|
|
|
clearTimeout(timerId);
|
|
|
|
const focused = this.viewControllers.sendMessage.getFocusedView();
|
|
|
|
if(focused === messageView) {
|
|
|
|
previewView.setText(messageView.getData());
|
|
|
|
focused.setFocus(true);
|
|
|
|
}
|
|
|
|
}, 500);
|
2018-11-13 05:03:28 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-14 04:11:33 +00:00
|
|
|
createInterruptItem(message, cb) {
|
2018-12-01 06:20:44 +00:00
|
|
|
const dateTimeFormat = this.config.dateTimeFormat || this.client.currentTheme.helpers.getDateTimeFormat();
|
|
|
|
|
2018-11-14 04:11:33 +00:00
|
|
|
const textFormatObj = {
|
|
|
|
fromUserName : this.client.user.username,
|
|
|
|
fromRealName : this.client.user.properties.real_name,
|
|
|
|
fromNodeId : this.client.node,
|
|
|
|
message : message,
|
2018-12-01 06:20:44 +00:00
|
|
|
timestamp : moment().format(dateTimeFormat),
|
2018-11-14 04:11:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const messageFormat =
|
|
|
|
this.config.messageFormat ||
|
|
|
|
'Message from {fromUserName} on node {fromNodeId}:\r\n{message}';
|
|
|
|
|
|
|
|
const item = {
|
|
|
|
text : stringFormat(messageFormat, textFormatObj),
|
|
|
|
pause : true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const getArt = (name, callback) => {
|
|
|
|
const spec = _.get(this.config, `art.${name}`);
|
|
|
|
if(!spec) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
const getArtOpts = {
|
|
|
|
name : spec,
|
|
|
|
client : this.client,
|
|
|
|
random : false,
|
|
|
|
};
|
|
|
|
getThemeArt(getArtOpts, (err, artInfo) => {
|
|
|
|
// ignore errors
|
|
|
|
return callback(artInfo ? artInfo.data : null);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
(callback) => {
|
|
|
|
getArt('header', headerArt => {
|
|
|
|
return callback(null, headerArt);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(headerArt, callback) => {
|
|
|
|
getArt('footer', footerArt => {
|
|
|
|
return callback(null, headerArt, footerArt);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(headerArt, footerArt, callback) => {
|
|
|
|
if(headerArt || footerArt) {
|
|
|
|
item.contents = `${headerArt || ''}\r\n${pipeToAnsi(item.text)}\r\n${footerArt || ''}`;
|
|
|
|
}
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err, item);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-13 05:03:28 +00:00
|
|
|
prepareNodeList() {
|
|
|
|
// standard node list with {text} field added for compliance
|
|
|
|
this.nodeList = [{
|
|
|
|
text : '-ALL-',
|
|
|
|
// dummy fields:
|
2018-11-15 03:51:11 +00:00
|
|
|
node : -1,
|
2018-11-13 05:03:28 +00:00
|
|
|
authenticated : false,
|
|
|
|
userId : 0,
|
|
|
|
action : 'N/A',
|
|
|
|
userName : 'Everyone',
|
|
|
|
realName : 'All Users',
|
|
|
|
location : 'N/A',
|
|
|
|
affils : 'N/A',
|
|
|
|
timeOn : 'N/A',
|
2018-11-14 03:03:54 +00:00
|
|
|
}].concat(getActiveConnectionList(true)
|
2018-11-15 03:51:11 +00:00
|
|
|
.map(node => Object.assign(node, { text : -1 == node.node ? '-ALL-' : node.node.toString() } ))
|
2018-11-13 05:03:28 +00:00
|
|
|
).filter(node => node.node !== this.client.node); // remove our client's node
|
|
|
|
this.nodeList.sort( (a, b) => a.node - b.node ); // sort by node
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeListSelectionIndexUpdate(idx) {
|
|
|
|
const node = this.nodeList[idx];
|
|
|
|
if(!node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.updateCustomViewTextsWithFilter('sendMessage', MciViewIds.sendMessage.customRangeStart, node);
|
|
|
|
}
|
2018-12-01 06:20:44 +00:00
|
|
|
};
|