2015-08-03 00:27:05 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-07-21 20:32:06 +00:00
|
|
|
const { MenuModule } = require('./menu_module.js');
|
2018-06-23 05:06:01 +00:00
|
|
|
const DropFile = require('./dropfile.js');
|
|
|
|
const Door = require('./door.js');
|
2018-06-23 03:26:46 +00:00
|
|
|
const theme = require('./theme.js');
|
|
|
|
const ansi = require('./ansi_term.js');
|
2018-07-21 20:32:06 +00:00
|
|
|
const Events = require('./events.js');
|
2018-12-03 02:33:07 +00:00
|
|
|
const { Errors } = require('./enig_error.js');
|
2019-01-06 04:17:53 +00:00
|
|
|
const StatLog = require('./stat_log.js');
|
|
|
|
const UserProps = require('./user_property.js');
|
2017-01-26 05:18:05 +00:00
|
|
|
|
2018-12-08 07:43:20 +00:00
|
|
|
// deps
|
2018-06-23 03:26:46 +00:00
|
|
|
const async = require('async');
|
|
|
|
const assert = require('assert');
|
|
|
|
const _ = require('lodash');
|
2018-12-08 07:43:20 +00:00
|
|
|
const paths = require('path');
|
2019-01-06 04:17:53 +00:00
|
|
|
const moment = require('moment');
|
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 = {
|
2018-06-23 03:26:46 +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
|
2018-06-23 03:26:46 +00:00
|
|
|
assert(_.isString(this.config.name, 'Config \'name\' is required'));
|
|
|
|
assert(_.isString(this.config.dropFileType, 'Config \'dropFileType\' is required'));
|
|
|
|
assert(_.isString(this.config.cmd, 'Config \'cmd\' is required'));
|
2015-08-04 05:11:17 +00:00
|
|
|
|
2018-06-23 03:26:46 +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
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
:TODO:
|
|
|
|
* disconnecting wile door is open leaves dosemu
|
|
|
|
* http://bbslink.net/sysop.php support
|
|
|
|
* Font support ala all other menus... or does this just work?
|
|
|
|
*/
|
2015-08-03 00:27:05 +00:00
|
|
|
|
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) {
|
|
|
|
if(self.config.nodeMax > 0 &&
|
2018-06-23 03:26:46 +00:00
|
|
|
_.isNumber(activeDoorNodeInstances[self.config.name]) &&
|
|
|
|
activeDoorNodeInstances[self.config.name] + 1 > self.config.nodeMax)
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
|
|
|
self.client.log.info(
|
|
|
|
{
|
2018-06-23 03:26:46 +00:00
|
|
|
name : self.config.name,
|
2018-06-22 05:15:04 +00:00
|
|
|
activeCount : activeDoorNodeInstances[self.config.name]
|
|
|
|
},
|
|
|
|
'Too many active instances');
|
|
|
|
|
|
|
|
if(_.isString(self.config.tooManyArt)) {
|
|
|
|
theme.displayThemeArt( { client : self.client, name : self.config.tooManyArt }, function displayed() {
|
|
|
|
self.pausePrompt( () => {
|
2018-12-03 02:33:07 +00:00
|
|
|
return callback(Errors.AccessDenied('Too many active instances'));
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
self.client.term.write('\nToo many active instances. Try again later.\n');
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Use MenuModule.pausePrompt()
|
2018-06-22 05:15:04 +00:00
|
|
|
self.pausePrompt( () => {
|
2018-12-03 02:33:07 +00:00
|
|
|
return callback(Errors.AccessDenied('Too many active instances'));
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: JS elegant way to do this?
|
2018-06-22 05:15:04 +00:00
|
|
|
if(activeDoorNodeInstances[self.config.name]) {
|
|
|
|
activeDoorNodeInstances[self.config.name] += 1;
|
|
|
|
} else {
|
|
|
|
activeDoorNodeInstances[self.config.name] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
},
|
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) {
|
2018-06-23 05:06:01 +00:00
|
|
|
const dropFileOpts = {
|
|
|
|
fileType : self.config.dropFileType,
|
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 05:06:01 +00:00
|
|
|
self.dropFile = new DropFile(self.client, dropFileOpts);
|
|
|
|
return self.dropFile.createFile(callback);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.warn( { error : err.toString() }, 'Could not start door');
|
|
|
|
self.lastError = err;
|
|
|
|
self.prevMenu();
|
|
|
|
} else {
|
|
|
|
self.finishedLoading();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
runDoor() {
|
2019-01-06 04:17:53 +00:00
|
|
|
StatLog.incrementUserStat(this.client.user, UserProps.DoorRunTotalCount, 1);
|
2018-07-21 20:32:06 +00:00
|
|
|
Events.emit(Events.getSystemEvents().UserRunDoor, { user : this.client.user } );
|
|
|
|
|
2018-06-23 05:06:01 +00:00
|
|
|
this.client.term.write(ansi.resetScreen());
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
const exeInfo = {
|
2018-06-23 05:06:01 +00:00
|
|
|
cmd : this.config.cmd,
|
2018-12-08 07:43:20 +00:00
|
|
|
cwd : this.config.cwd || paths.dirname(this.config.cmd),
|
2018-06-23 05:06:01 +00:00
|
|
|
args : this.config.args,
|
|
|
|
io : this.config.io || 'stdio',
|
2018-08-06 03:22:32 +00:00
|
|
|
encoding : this.config.encoding || 'cp437',
|
2018-06-23 05:06:01 +00:00
|
|
|
dropFile : this.dropFile.fileName,
|
|
|
|
dropFilePath : this.dropFile.fullPath,
|
|
|
|
node : this.client.node,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2019-01-06 04:17:53 +00:00
|
|
|
const startTime = moment();
|
|
|
|
|
2018-06-23 05:06:01 +00:00
|
|
|
this.doorInstance.run(exeInfo, () => {
|
2019-01-06 04:17:53 +00:00
|
|
|
const endTime = moment();
|
|
|
|
const runTimeMinutes = Math.floor(moment.duration(endTime.diff(startTime)).asMinutes());
|
|
|
|
if(runTimeMinutes > 0) {
|
|
|
|
StatLog.incrementUserStat(this.client.user, UserProps.DoorRunTotalMinutes, runTimeMinutes);
|
|
|
|
}
|
|
|
|
|
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() +
|
2018-06-23 03:26:46 +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
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.prevMenu();
|
|
|
|
});
|
|
|
|
}
|
2015-08-03 00:27:05 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
leave() {
|
|
|
|
super.leave();
|
|
|
|
if(!this.lastError) {
|
|
|
|
activeDoorNodeInstances[this.config.name] -= 1;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
};
|