2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-08-13 16:05:49 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const ansi = require('./ansi_term.js');
|
2017-06-23 04:19:34 +00:00
|
|
|
const Events = require('./events.js');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2016-08-14 17:45:31 +00:00
|
|
|
// deps
|
|
|
|
const async = require('async');
|
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
exports.connectEntry = connectEntry;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2016-08-14 17:45:31 +00:00
|
|
|
function ansiDiscoverHomePosition(client, cb) {
|
|
|
|
//
|
|
|
|
// We want to find the home position. ANSI-BBS and most terminals
|
|
|
|
// utilize 1,1 as home. However, some terminals such as ConnectBot
|
|
|
|
// think of home as 0,0. If this is the case, we need to offset
|
|
|
|
// our positioning to accomodate for such.
|
|
|
|
//
|
|
|
|
const done = function(err) {
|
|
|
|
client.removeListener('cursor position report', cprListener);
|
|
|
|
clearTimeout(giveUpTimer);
|
|
|
|
return cb(err);
|
|
|
|
};
|
|
|
|
|
|
|
|
const cprListener = function(pos) {
|
|
|
|
const h = pos[0];
|
|
|
|
const w = pos[1];
|
|
|
|
|
|
|
|
//
|
|
|
|
// We expect either 0,0, or 1,1. Anything else will be filed as bad data
|
|
|
|
//
|
|
|
|
if(h > 1 || w > 1) {
|
|
|
|
client.log.warn( { height : h, width : w }, 'Ignoring ANSI home position CPR due to unexpected values');
|
|
|
|
return done(new Error('Home position CPR expected to be 0,0, or 1,1'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(0 === h & 0 === w) {
|
|
|
|
//
|
|
|
|
// Store a CPR offset in the client. All CPR's from this point on will offset by this amount
|
|
|
|
//
|
|
|
|
client.log.info('Setting CPR offset to 1');
|
|
|
|
client.cprOffset = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return done(null);
|
|
|
|
};
|
|
|
|
|
|
|
|
client.once('cursor position report', cprListener);
|
|
|
|
|
|
|
|
const giveUpTimer = setTimeout( () => {
|
|
|
|
return done(new Error('Giving up on home position CPR'));
|
|
|
|
}, 3000); // 3s
|
|
|
|
|
|
|
|
client.term.write(`${ansi.goHome()}${ansi.queryPos()}`); // go home, query pos
|
|
|
|
}
|
|
|
|
|
2015-05-16 05:02:58 +00:00
|
|
|
function ansiQueryTermSizeIfNeeded(client, cb) {
|
2014-10-29 11:30:20 +00:00
|
|
|
if(client.term.termHeight > 0 || client.term.termWidth > 0) {
|
2016-08-13 16:05:49 +00:00
|
|
|
return cb(null);
|
2014-10-29 11:30:20 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 16:05:49 +00:00
|
|
|
const done = function(err) {
|
2015-07-23 04:09:52 +00:00
|
|
|
client.removeListener('cursor position report', cprListener);
|
|
|
|
clearTimeout(giveUpTimer);
|
2016-08-13 16:05:49 +00:00
|
|
|
return cb(err);
|
2015-07-23 04:09:52 +00:00
|
|
|
};
|
|
|
|
|
2016-08-13 16:05:49 +00:00
|
|
|
const cprListener = function(pos) {
|
2014-10-29 11:30:20 +00:00
|
|
|
//
|
|
|
|
// If we've already found out, disregard
|
|
|
|
//
|
|
|
|
if(client.term.termHeight > 0 || client.term.termWidth > 0) {
|
2016-08-13 16:05:49 +00:00
|
|
|
return done(null);
|
2014-10-29 11:30:20 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 17:45:31 +00:00
|
|
|
const h = pos[0];
|
|
|
|
const w = pos[1];
|
2015-05-16 05:02:58 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Netrunner for example gives us 1x1 here. Not really useful. Ignore
|
|
|
|
// values that seem obviously bad.
|
|
|
|
//
|
|
|
|
if(h < 10 || w < 10) {
|
2015-07-04 21:21:50 +00:00
|
|
|
client.log.warn(
|
2017-06-11 00:29:00 +00:00
|
|
|
{ height : h, width : w },
|
2015-05-16 05:02:58 +00:00
|
|
|
'Ignoring ANSI CPR screen size query response due to very small values');
|
2016-08-13 16:05:49 +00:00
|
|
|
return done(new Error('Term size <= 10 considered invalid'));
|
2015-05-16 05:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client.term.termHeight = h;
|
|
|
|
client.term.termWidth = w;
|
2014-10-29 11:30:20 +00:00
|
|
|
|
2015-07-04 21:21:50 +00:00
|
|
|
client.log.debug(
|
2017-06-11 00:29:00 +00:00
|
|
|
{
|
|
|
|
termWidth : client.term.termWidth,
|
|
|
|
termHeight : client.term.termHeight,
|
|
|
|
source : 'ANSI CPR'
|
|
|
|
},
|
2015-05-16 05:02:58 +00:00
|
|
|
'Window size updated'
|
2018-01-15 19:22:11 +00:00
|
|
|
);
|
2015-07-04 21:21:50 +00:00
|
|
|
|
2016-08-13 16:05:49 +00:00
|
|
|
return done(null);
|
2014-10-29 11:30:20 +00:00
|
|
|
};
|
|
|
|
|
2015-05-16 05:02:58 +00:00
|
|
|
client.once('cursor position report', cprListener);
|
2014-10-29 11:30:20 +00:00
|
|
|
|
|
|
|
// give up after 2s
|
2016-08-13 16:05:49 +00:00
|
|
|
const giveUpTimer = setTimeout( () => {
|
|
|
|
return done(new Error('No term size established by CPR within timeout'));
|
2014-10-29 11:30:20 +00:00
|
|
|
}, 2000);
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2017-06-11 00:29:00 +00:00
|
|
|
// Start the process: Query for CPR
|
2015-08-20 04:10:18 +00:00
|
|
|
client.term.rawWrite(ansi.queryScreenSize());
|
2014-10-29 11:30:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
function prepareTerminal(term) {
|
2015-08-20 04:10:18 +00:00
|
|
|
term.rawWrite(ansi.normal());
|
2016-06-27 03:23:36 +00:00
|
|
|
//term.rawWrite(ansi.disableVT100LineWrapping());
|
2014-10-17 02:21:06 +00:00
|
|
|
// :TODO: set xterm stuff -- see x84/others
|
2014-10-31 04:59:21 +00:00
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
function displayBanner(term) {
|
2016-08-14 17:45:31 +00:00
|
|
|
// note: intentional formatting:
|
|
|
|
term.pipeWrite(`
|
2016-08-27 03:28:29 +00:00
|
|
|
|06Connected to |02EN|10i|02GMA|10½ |06BBS version |12|VN
|
2018-02-03 15:20:51 +00:00
|
|
|
|06Copyright (c) 2014-2018 Bryan Ashby |14- |12http://l33t.codes/
|
2016-08-27 03:28:29 +00:00
|
|
|
|06Updates & source |14- |12https://github.com/NuSkooler/enigma-bbs/
|
|
|
|
|00`
|
2016-08-14 17:45:31 +00:00
|
|
|
);
|
2014-10-31 04:59:21 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 16:36:08 +00:00
|
|
|
function connectEntry(client, nextMenu) {
|
2016-08-13 16:05:49 +00:00
|
|
|
const term = client.term;
|
2014-10-31 04:59:21 +00:00
|
|
|
|
2016-08-14 17:45:31 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function basicPrepWork(callback) {
|
|
|
|
term.rawWrite(ansi.queryDeviceAttributes(0));
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function discoverHomePosition(callback) {
|
|
|
|
ansiDiscoverHomePosition(client, () => {
|
|
|
|
// :TODO: If CPR for home fully fails, we should bail out on the connection with an error, e.g. ANSI support required
|
|
|
|
return callback(null); // we try to continue anyway
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function queryTermSizeByNonStandardAnsi(callback) {
|
|
|
|
ansiQueryTermSizeIfNeeded(client, err => {
|
|
|
|
if(err) {
|
|
|
|
//
|
|
|
|
// Check again; We may have got via NAWS/similar before CPR completed.
|
|
|
|
//
|
|
|
|
if(0 === term.termHeight || 0 === term.termWidth) {
|
|
|
|
//
|
|
|
|
// We still don't have something good for term height/width.
|
2017-06-11 00:29:00 +00:00
|
|
|
// Default to DOS size 80x25.
|
2016-08-14 17:45:31 +00:00
|
|
|
//
|
2017-06-11 00:29:00 +00:00
|
|
|
// :TODO: Netrunner is currenting hitting this and it feels wrong. Why is NAWS/ENV/CPR all failing???
|
2016-08-14 17:45:31 +00:00
|
|
|
client.log.warn( { reason : err.message }, 'Failed to negotiate term size; Defaulting to 80x25!');
|
2017-06-11 00:29:00 +00:00
|
|
|
|
2016-08-14 17:45:31 +00:00
|
|
|
term.termHeight = 25;
|
|
|
|
term.termWidth = 80;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
});
|
2017-06-11 00:29:00 +00:00
|
|
|
},
|
|
|
|
],
|
2016-08-14 17:45:31 +00:00
|
|
|
() => {
|
|
|
|
prepareTerminal(term);
|
2015-05-16 05:02:58 +00:00
|
|
|
|
|
|
|
//
|
2016-08-14 17:45:31 +00:00
|
|
|
// Always show an ENiGMA½ banner
|
2015-05-16 05:02:58 +00:00
|
|
|
//
|
2016-08-14 17:45:31 +00:00
|
|
|
displayBanner(term);
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2017-06-11 01:52:11 +00:00
|
|
|
// fire event
|
2018-06-03 23:02:28 +00:00
|
|
|
Events.emit(Events.getSystemEvents().TermDetected, { client : client } );
|
2017-06-11 00:29:00 +00:00
|
|
|
|
2016-08-14 17:45:31 +00:00
|
|
|
setTimeout( () => {
|
|
|
|
return client.menuStack.goto(nextMenu);
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
);
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|