2015-08-03 00:27:05 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const { MenuModule } = require('./menu_module.js');
|
|
|
|
const DropFile = require('./dropfile.js');
|
|
|
|
const Door = require('./door.js');
|
|
|
|
const theme = require('./theme.js');
|
|
|
|
const ansi = require('./ansi_term.js');
|
|
|
|
const { Errors } = require('./enig_error.js');
|
|
|
|
const { trackDoorRunBegin, trackDoorRunEnd } = require('./door_util.js');
|
|
|
|
const Log = require('./logger').log;
|
2017-01-26 05:18:05 +00:00
|
|
|
|
2018-12-08 07:43:20 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const async = require('async');
|
|
|
|
const assert = require('assert');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const paths = require('path');
|
|
|
|
const fs = require('graceful-fs');
|
2015-08-03 00:27:05 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
const activeDoorNodeInstances = {};
|
2015-08-04 05:11:17 +00:00
|
|
|
|
2015-08-03 00:27:05 +00:00
|
|
|
exports.moduleInfo = {
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'Abracadabra',
|
|
|
|
desc: 'External BBS Door Module',
|
|
|
|
author: 'NuSkooler',
|
2015-08-03 00:27:05 +00:00
|
|
|
};
|
|
|
|
|
2015-08-04 05:11:17 +00:00
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
Example configuration for LORD under DOSEMU:
|
|
|
|
|
|
|
|
{
|
|
|
|
config: {
|
|
|
|
name: PimpWars
|
|
|
|
dropFileType: DORINFO
|
|
|
|
cmd: qemu-system-i386
|
|
|
|
args: [
|
|
|
|
"-localtime",
|
|
|
|
"freedos.img",
|
|
|
|
"-chardev",
|
|
|
|
"socket,port={srvPort},nowait,host=localhost,id=s0",
|
|
|
|
"-device",
|
|
|
|
"isa-serial,chardev=s0"
|
|
|
|
]
|
|
|
|
io: socket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
listen: socket | stdio
|
|
|
|
|
|
|
|
{
|
|
|
|
"config" : {
|
|
|
|
"name" : "LORD",
|
|
|
|
"dropFileType" : "DOOR",
|
|
|
|
"cmd" : "/usr/bin/dosemu",
|
|
|
|
"args" : [ "-quiet", "-f", "/etc/dosemu/dosemu.conf", "X:\\PW\\START.BAT {dropfile} {node}" ] ],
|
|
|
|
"nodeMax" : 32,
|
|
|
|
"tooManyArt" : "toomany-lord.ans"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:TODO: See Mystic & others for other arg options that we may need to support
|
2015-08-04 05:11:17 +00:00
|
|
|
*/
|
2015-08-03 00:27:05 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.getModule = class AbracadabraModule extends MenuModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2015-08-04 05:11:17 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.config = options.menuConfig.config;
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: MenuModule.validateConfig(cb) -- validate config section gracefully instead of asserts! -- { key : type, key2 : type2, ... }
|
2018-06-23 05:06:01 +00:00
|
|
|
// .. and/or EnigAssert
|
2022-06-05 20:04:25 +00:00
|
|
|
assert(_.isString(this.config.name, "Config 'name' is required"));
|
|
|
|
assert(_.isString(this.config.cmd, "Config 'cmd' is required"));
|
2015-08-04 05:11:17 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.config.nodeMax = this.config.nodeMax || 0;
|
|
|
|
this.config.args = this.config.args || [];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2015-08-03 00:27:05 +00:00
|
|
|
|
2019-04-24 00:16:55 +00:00
|
|
|
incrementActiveDoorNodeInstances() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (activeDoorNodeInstances[this.config.name]) {
|
2019-04-24 00:16:55 +00:00
|
|
|
activeDoorNodeInstances[this.config.name] += 1;
|
|
|
|
} else {
|
|
|
|
activeDoorNodeInstances[this.config.name] = 1;
|
|
|
|
}
|
|
|
|
this.activeDoorInstancesIncremented = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
decrementActiveDoorNodeInstances() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (true === this.activeDoorInstancesIncremented) {
|
2019-04-24 00:16:55 +00:00
|
|
|
activeDoorNodeInstances[this.config.name] -= 1;
|
|
|
|
this.activeDoorInstancesIncremented = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
initSequence() {
|
|
|
|
const self = this;
|
2017-01-26 05:18:05 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function validateNodeCount(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
self.config.nodeMax > 0 &&
|
2018-06-23 03:26:46 +00:00
|
|
|
_.isNumber(activeDoorNodeInstances[self.config.name]) &&
|
2022-06-05 20:04:25 +00:00
|
|
|
activeDoorNodeInstances[self.config.name] + 1 >
|
|
|
|
self.config.nodeMax
|
|
|
|
) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.client.log.info(
|
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
name: self.config.name,
|
|
|
|
activeCount: activeDoorNodeInstances[self.config.name],
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2022-06-12 20:12:03 +00:00
|
|
|
`Too many active instances of door "${self.config.name}"`
|
|
|
|
);
|
|
|
|
|
|
|
|
if (_.isString(self.config.tooManyArt)) {
|
|
|
|
theme.displayThemeArt(
|
|
|
|
{ client: self.client, name: self.config.tooManyArt },
|
|
|
|
function displayed() {
|
|
|
|
self.pausePrompt(() => {
|
|
|
|
return callback(
|
|
|
|
Errors.AccessDenied(
|
|
|
|
'Too many active instances'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
self.client.term.write(
|
|
|
|
'\nToo many active instances. Try again later.\n'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Use MenuModule.pausePrompt()
|
2022-06-05 20:04:25 +00:00
|
|
|
self.pausePrompt(() => {
|
|
|
|
return callback(
|
|
|
|
Errors.AccessDenied('Too many active instances')
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2019-04-24 00:16:55 +00:00
|
|
|
self.incrementActiveDoorNodeInstances();
|
|
|
|
return callback(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
},
|
2018-06-23 05:06:01 +00:00
|
|
|
function prepareDoor(callback) {
|
|
|
|
self.doorInstance = new Door(self.client);
|
|
|
|
return self.doorInstance.prepare(self.config.io || 'stdio', callback);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
function generateDropfile(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
!self.config.dropFileType ||
|
|
|
|
self.config.dropFileType.toLowerCase() === 'none'
|
|
|
|
) {
|
2020-12-14 19:02:25 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.dropFile = new DropFile(self.client, {
|
|
|
|
fileType: self.config.dropFileType,
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 05:06:01 +00:00
|
|
|
return self.dropFile.createFile(callback);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
function complete(err) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
self.client.log.warn(
|
|
|
|
{ error: err.toString() },
|
|
|
|
'Could not start door'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
self.lastError = err;
|
|
|
|
self.prevMenu();
|
|
|
|
} else {
|
|
|
|
self.finishedLoading();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
runDoor() {
|
2018-06-23 05:06:01 +00:00
|
|
|
this.client.term.write(ansi.resetScreen());
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
const exeInfo = {
|
2022-06-12 20:12:03 +00:00
|
|
|
name: this.config.name,
|
|
|
|
cmd: this.config.cmd,
|
|
|
|
cwd: this.config.cwd || paths.dirname(this.config.cmd),
|
|
|
|
args: this.config.args,
|
|
|
|
io: this.config.io || 'stdio',
|
|
|
|
encoding: this.config.encoding || 'cp437',
|
|
|
|
node: this.client.node,
|
|
|
|
env: this.config.env,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2020-12-14 19:02:25 +00:00
|
|
|
if (this.dropFile) {
|
2022-06-05 20:04:25 +00:00
|
|
|
exeInfo.dropFile = this.dropFile.fileName;
|
|
|
|
exeInfo.dropFilePath = this.dropFile.fullPath;
|
2020-12-14 19:02:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 06:12:01 +00:00
|
|
|
const doorTracking = trackDoorRunBegin(this.client, this.config.name);
|
2019-01-06 04:17:53 +00:00
|
|
|
|
2022-09-09 03:31:01 +00:00
|
|
|
this.doorInstance.run(exeInfo, err => {
|
|
|
|
if (err) {
|
|
|
|
Log.error(`Error running "${this.config.name}": ${err.message}`);
|
|
|
|
}
|
|
|
|
|
2019-01-19 06:12:01 +00:00
|
|
|
trackDoorRunEnd(doorTracking);
|
2019-04-24 00:16:55 +00:00
|
|
|
this.decrementActiveDoorNodeInstances();
|
2019-01-06 04:17:53 +00:00
|
|
|
|
2020-12-14 19:02:25 +00:00
|
|
|
// Clean up dropfile, if any
|
|
|
|
if (exeInfo.dropFilePath) {
|
|
|
|
fs.unlink(exeInfo.dropFilePath, err => {
|
|
|
|
if (err) {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.warn(
|
|
|
|
{ error: err, path: exeInfo.dropFilePath },
|
|
|
|
'Failed to remove drop file.'
|
|
|
|
);
|
2020-12-14 19:02:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-06 03:53:30 +00:00
|
|
|
// client may have disconnected while process was active -
|
|
|
|
// we're done here if so.
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!this.client.term.output) {
|
2019-04-06 03:53:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Try to clean up various settings such as scroll regions that may
|
|
|
|
// have been set within the door
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
this.client.term.rawWrite(
|
|
|
|
ansi.normal() +
|
2022-06-05 20:04:25 +00:00
|
|
|
ansi.goto(this.client.term.termHeight, this.client.term.termWidth) +
|
|
|
|
ansi.setScrollRegion() +
|
|
|
|
ansi.goto(this.client.term.termHeight, 0) +
|
|
|
|
'\r\n\r\n'
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
2016-06-21 03:38:43 +00:00
|
|
|
|
2020-12-06 13:43:28 +00:00
|
|
|
this.autoNextMenu();
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
2015-08-03 00:27:05 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
leave() {
|
|
|
|
super.leave();
|
2019-04-24 00:16:55 +00:00
|
|
|
this.decrementActiveDoorNodeInstances();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2015-08-03 00:27:05 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
finishedLoading() {
|
|
|
|
this.runDoor();
|
|
|
|
}
|
2015-08-03 00:27:05 +00:00
|
|
|
};
|