2018-11-13 05:03:28 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +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,
|
2022-06-05 20:04:25 +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');
|
|
|
|
const Events = require('./events.js');
|
2018-11-13 05:03:28 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const series = require('async/series');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const async = require('async');
|
|
|
|
const moment = require('moment');
|
2018-11-13 05:03:28 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'Node Message',
|
|
|
|
desc: 'Multi-node messaging',
|
|
|
|
author: 'NuSkooler',
|
2018-11-13 05:03:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const FormIds = {
|
2022-06-05 20:04:25 +00:00
|
|
|
sendMessage: 0,
|
2018-11-13 05:03:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const MciViewIds = {
|
2022-06-05 20:04:25 +00:00
|
|
|
sendMessage: {
|
|
|
|
nodeSelect: 1,
|
|
|
|
message: 2,
|
|
|
|
preview: 3,
|
2018-11-13 05:03:28 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
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);
|
2022-06-05 20:04:25 +00:00
|
|
|
this.config = Object.assign({}, _.get(options, 'menuConfig.config'), {
|
|
|
|
extraArgs: options.extraArgs,
|
|
|
|
});
|
2018-11-13 05:03:28 +00:00
|
|
|
|
|
|
|
this.menuMethods = {
|
2022-06-05 20:04:25 +00:00
|
|
|
sendMessage: (formData, extraArgs, cb) => {
|
|
|
|
const nodeId = this.nodeList[formData.value.node].node; // index from from -> node!
|
|
|
|
const message = _.get(formData.value, 'message', '').trim();
|
2018-12-01 06:20:44 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === renderStringLength(message)) {
|
2018-12-01 06:20:44 +00:00
|
|
|
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) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (-1 === nodeId) {
|
2018-11-14 04:11:33 +00:00
|
|
|
// ALL nodes
|
2022-06-05 20:04:25 +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);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (conn) {
|
|
|
|
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
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Events.emit(Events.getSystemEvents().UserSendNodeMsg, {
|
|
|
|
user: this.client.user,
|
|
|
|
global: -1 === nodeId,
|
|
|
|
});
|
2018-12-02 00:00:07 +00:00
|
|
|
|
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 => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-11-13 05:03:28 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
series(
|
|
|
|
[
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
|
|
|
return this.prepViewController(
|
|
|
|
'sendMessage',
|
|
|
|
FormIds.sendMessage,
|
|
|
|
mciData.menu,
|
|
|
|
callback
|
|
|
|
);
|
2018-11-15 03:51:11 +00:00
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
2018-11-15 03:51:11 +00:00
|
|
|
return this.validateMCIByViewIds(
|
|
|
|
'sendMessage',
|
2022-06-05 20:04:25 +00:00
|
|
|
[
|
|
|
|
MciViewIds.sendMessage.nodeSelect,
|
|
|
|
MciViewIds.sendMessage.message,
|
|
|
|
],
|
2018-11-15 03:51:11 +00:00
|
|
|
callback
|
|
|
|
);
|
2018-11-13 05:03:28 +00:00
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
|
|
|
const nodeSelectView = this.viewControllers.sendMessage.getView(
|
|
|
|
MciViewIds.sendMessage.nodeSelect
|
|
|
|
);
|
2018-11-13 05:03:28 +00:00
|
|
|
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);
|
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
|
|
|
const previewView = this.viewControllers.sendMessage.getView(
|
|
|
|
MciViewIds.sendMessage.preview
|
|
|
|
);
|
|
|
|
if (!previewView) {
|
|
|
|
return callback(null); // preview is optional
|
2018-11-15 03:51:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const messageView = this.viewControllers.sendMessage.getView(
|
|
|
|
MciViewIds.sendMessage.message
|
|
|
|
);
|
2018-11-15 03:51:11 +00:00
|
|
|
let timerId;
|
2022-06-05 20:04:25 +00:00
|
|
|
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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const dateTimeFormat =
|
|
|
|
this.config.dateTimeFormat ||
|
|
|
|
this.client.currentTheme.helpers.getDateTimeFormat();
|
2018-12-01 06:20:44 +00:00
|
|
|
|
2018-11-14 04:11:33 +00:00
|
|
|
const textFormatObj = {
|
2022-06-05 20:04:25 +00:00
|
|
|
fromUserName: this.client.user.username,
|
|
|
|
fromRealName: this.client.user.properties.real_name,
|
|
|
|
fromNodeId: this.client.node,
|
|
|
|
message: message,
|
|
|
|
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 = {
|
2022-06-05 20:04:25 +00:00
|
|
|
text: stringFormat(messageFormat, textFormatObj),
|
|
|
|
pause: true,
|
2018-11-14 04:11:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const getArt = (name, callback) => {
|
|
|
|
const spec = _.get(this.config, `art.${name}`);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!spec) {
|
2018-11-14 04:11:33 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
const getArtOpts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
name: spec,
|
|
|
|
client: this.client,
|
|
|
|
random: false,
|
2018-11-14 04:11:33 +00:00
|
|
|
};
|
|
|
|
getThemeArt(getArtOpts, (err, artInfo) => {
|
|
|
|
// ignore errors
|
|
|
|
return callback(artInfo ? artInfo.data : null);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
2018-11-14 04:11:33 +00:00
|
|
|
getArt('header', headerArt => {
|
|
|
|
return callback(null, headerArt);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(headerArt, callback) => {
|
|
|
|
getArt('footer', footerArt => {
|
|
|
|
return callback(null, headerArt, footerArt);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(headerArt, footerArt, callback) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (headerArt || footerArt) {
|
|
|
|
item.contents = `${headerArt || ''}\r\n${pipeToAnsi(
|
|
|
|
item.text
|
|
|
|
)}\r\n${footerArt || ''}`;
|
2018-11-14 04:11:33 +00:00
|
|
|
}
|
|
|
|
return callback(null);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-11-14 04:11:33 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err, item);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-13 05:03:28 +00:00
|
|
|
prepareNodeList() {
|
|
|
|
// standard node list with {text} field added for compliance
|
2022-06-05 20:04:25 +00:00
|
|
|
this.nodeList = [
|
|
|
|
{
|
|
|
|
text: '-ALL-',
|
|
|
|
// dummy fields:
|
|
|
|
node: -1,
|
|
|
|
authenticated: false,
|
|
|
|
userId: 0,
|
|
|
|
action: 'N/A',
|
|
|
|
userName: 'Everyone',
|
|
|
|
realName: 'All Users',
|
|
|
|
location: 'N/A',
|
|
|
|
affils: 'N/A',
|
|
|
|
timeOn: 'N/A',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
.concat(
|
|
|
|
getActiveConnectionList(true).map(node =>
|
|
|
|
Object.assign(node, {
|
|
|
|
text: -1 == node.node ? '-ALL-' : node.node.toString(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.filter(node => node.node !== this.client.node); // remove our client's node
|
|
|
|
this.nodeList.sort((a, b) => a.node - b.node); // sort by node
|
2018-11-13 05:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nodeListSelectionIndexUpdate(idx) {
|
|
|
|
const node = this.nodeList[idx];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!node) {
|
2018-11-13 05:03:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
this.updateCustomViewTextsWithFilter(
|
|
|
|
'sendMessage',
|
|
|
|
MciViewIds.sendMessage.customRangeStart,
|
|
|
|
node
|
|
|
|
);
|
2018-11-13 05:03:28 +00:00
|
|
|
}
|
2018-12-01 06:20:44 +00:00
|
|
|
};
|