2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const ansi = require('./ansi_term.js');
|
|
|
|
const Events = require('./events.js');
|
|
|
|
const Config = require('./config.js').get;
|
|
|
|
const { Errors } = require('./enig_error.js');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const async = require('async');
|
2016-08-14 17:45:31 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.connectEntry = connectEntry;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2022-04-26 03:09:57 +00:00
|
|
|
const withCursorPositionReport = (client, cprHandler, failMessage, cb) => {
|
|
|
|
let giveUpTimer;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const done = function (err) {
|
2022-04-26 03:09:57 +00:00
|
|
|
client.removeListener('cursor position report', cprListener);
|
|
|
|
clearTimeout(giveUpTimer);
|
|
|
|
return cb(err);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const cprListener = pos => {
|
2022-04-26 03:09:57 +00:00
|
|
|
cprHandler(pos);
|
|
|
|
return done(null);
|
|
|
|
};
|
|
|
|
|
|
|
|
client.once('cursor position report', cprListener);
|
|
|
|
|
|
|
|
// give up after 2s
|
2022-06-05 20:04:25 +00:00
|
|
|
giveUpTimer = setTimeout(() => {
|
2022-04-26 03:09:57 +00:00
|
|
|
return done(Errors.General(failMessage));
|
|
|
|
}, 2000);
|
2022-06-05 20:04:25 +00:00
|
|
|
};
|
2022-04-26 03:09:57 +00:00
|
|
|
|
2022-03-28 19:18:00 +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 accommodate for such.
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!Config().term.checkAnsiHomePosition) {
|
2022-03-28 19:18:00 +00:00
|
|
|
// Skip (and assume 1,1) if the home position check is disabled.
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
2022-04-26 03:09:57 +00:00
|
|
|
withCursorPositionReport(
|
|
|
|
client,
|
|
|
|
pos => {
|
|
|
|
const [h, w] = pos;
|
2022-03-28 19:18:00 +00:00
|
|
|
|
|
|
|
//
|
2022-04-26 03:09:57 +00:00
|
|
|
// We expect either 0,0, or 1,1. Anything else will be filed as bad data
|
2022-03-28 19:18:00 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (h > 1 || w > 1) {
|
|
|
|
return client.log.warn(
|
|
|
|
{ height: h, width: w },
|
|
|
|
'Ignoring ANSI home position CPR due to unexpected values'
|
|
|
|
);
|
2022-04-26 03:09:57 +00:00
|
|
|
}
|
2022-03-28 19:18:00 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if ((0 === h) & (0 === w)) {
|
2022-04-26 03:09:57 +00:00
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'ANSI query home position timed out',
|
|
|
|
cb
|
|
|
|
);
|
2022-03-28 19:18:00 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
client.term.write(`${ansi.goHome()}${ansi.queryPos()}`); // go home, query pos
|
2022-03-28 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 17:31:03 +00:00
|
|
|
function ansiAttemptDetectUTF8(client, cb) {
|
|
|
|
//
|
|
|
|
// Trick to attempt and detect UTF-8. While there is a lot more than
|
|
|
|
// just UTF-8 and CP437, many those are the main concerns, when it comes
|
|
|
|
// terminals that for example tell us they are "xterm" but still want CP437.
|
|
|
|
//
|
|
|
|
// Try to detect UTF-8 by discovering the cursor position, writing some
|
|
|
|
// multi-byte UTF-8, and checking the position again. If the term is really
|
|
|
|
// UTF-8, we should get a proper position, otherwise we'll be further out.
|
|
|
|
//
|
|
|
|
// We currently only do this if the term hasn't already been ID'd as a
|
|
|
|
// "*nix" terminal -- that is, xterm, etc.
|
2022-03-23 23:43:49 +00:00
|
|
|
// Also skip this check if checkUtf8Encoding is disabled in the config
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!client.term.isNixTerm() || !Config().term.checkUtf8Encoding) {
|
2019-01-12 17:31:03 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
let initialPosition;
|
|
|
|
const ASCIIPortion = ' Character encoding detection ';
|
|
|
|
|
2022-04-26 03:09:57 +00:00
|
|
|
withCursorPositionReport(
|
|
|
|
client,
|
|
|
|
pos => {
|
|
|
|
initialPosition = pos;
|
2019-01-12 17:31:03 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
withCursorPositionReport(
|
|
|
|
client,
|
2022-04-26 03:09:57 +00:00
|
|
|
pos => {
|
|
|
|
const [_, w] = pos;
|
|
|
|
const len = w - initialPosition[1];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!isNaN(len) && len >= ASCIIPortion.length + 6) {
|
|
|
|
// CP437 displays 3 chars each Unicode skull
|
|
|
|
client.log.info(
|
|
|
|
'Terminal identified as UTF-8 but does not appear to be. Overriding to "ansi".'
|
|
|
|
);
|
2019-01-12 17:31:03 +00:00
|
|
|
client.setTermType('ansi');
|
|
|
|
}
|
2022-04-26 03:09:57 +00:00
|
|
|
},
|
|
|
|
'Detect UTF-8 stage 2 timed out',
|
2022-06-05 20:04:25 +00:00
|
|
|
cb
|
2022-04-26 03:09:57 +00:00
|
|
|
);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
client.term.rawWrite(`\u9760${ASCIIPortion}\u9760`); // Unicode skulls on each side
|
2022-04-26 03:09:57 +00:00
|
|
|
client.term.rawWrite(ansi.queryPos());
|
|
|
|
},
|
|
|
|
'Detect UTF-8 stage 1 timed out',
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
// bailing early
|
|
|
|
return cb(err);
|
|
|
|
}
|
2019-01-12 17:31:03 +00:00
|
|
|
}
|
2022-04-26 03:09:57 +00:00
|
|
|
);
|
2019-01-12 17:31:03 +00:00
|
|
|
|
2022-04-26 03:09:57 +00:00
|
|
|
client.term.rawWrite(`${ansi.goHome()}${ansi.queryPos()}`);
|
|
|
|
}
|
2019-01-12 17:31:03 +00:00
|
|
|
|
2022-04-26 03:09:57 +00:00
|
|
|
const ansiQuerySyncTermFontSupport = (client, cb) => {
|
|
|
|
// If we know it's SyncTERM, then good to go
|
|
|
|
if (client.term.termClient === 'cterm') {
|
|
|
|
client.term.syncTermFontsEnabled = true;
|
|
|
|
return cb(null);
|
|
|
|
}
|
2022-03-23 23:43:49 +00:00
|
|
|
|
2022-04-26 03:09:57 +00:00
|
|
|
// Some terminals do not properly ignore an unknown ESC sequence for
|
|
|
|
// setting SyncTERM style fonts. Some draw only a 'D' while others
|
|
|
|
// show more of the sequence
|
|
|
|
//
|
|
|
|
// If the cursor remains at 1,1, they are at least ignoring it, if not
|
|
|
|
// supporting it.
|
|
|
|
//
|
|
|
|
// We also need CPR support. Since all known terminals that support
|
|
|
|
// SyncTERM fonts also support CPR, this works out.
|
|
|
|
//
|
|
|
|
withCursorPositionReport(
|
|
|
|
client,
|
|
|
|
pos => {
|
|
|
|
const [_, w] = pos;
|
|
|
|
if (w === 1) {
|
|
|
|
// cursor didn't move
|
2022-06-05 20:04:25 +00:00
|
|
|
client.log.info(
|
|
|
|
'Client supports SyncTERM fonts or properly ignores unknown ESC sequence'
|
|
|
|
);
|
2022-04-26 03:09:57 +00:00
|
|
|
client.term.syncTermFontsEnabled = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'SyncTERM font detection timed out',
|
|
|
|
cb
|
|
|
|
);
|
2022-03-23 23:43:49 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
client.term.rawWrite(
|
|
|
|
`${ansi.goto(1, 1)}${ansi.setSyncTermFont('cp437')}${ansi.queryPos()}`
|
|
|
|
);
|
|
|
|
};
|
2019-01-12 17:31:03 +00:00
|
|
|
|
2015-05-16 05:02:58 +00:00
|
|
|
function ansiQueryTermSizeIfNeeded(client, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (client.term.termHeight > 0 || client.term.termWidth > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
2022-04-26 03:09:57 +00:00
|
|
|
withCursorPositionReport(
|
|
|
|
client,
|
|
|
|
pos => {
|
|
|
|
//
|
|
|
|
// If we've already found out, disregard
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (client.term.termHeight > 0 || client.term.termWidth > 0) {
|
2022-04-26 03:09:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-04-26 03:09:57 +00:00
|
|
|
//
|
|
|
|
// NetRunner for example gives us 1x1 here. Not really useful. Ignore
|
|
|
|
// values that seem obviously bad. Included in the set is the explicit
|
|
|
|
// 999x999 values we asked to move to.
|
|
|
|
//
|
|
|
|
const [h, w] = pos;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (h < 10 || h === 999 || w < 10 || w === 999) {
|
2022-04-26 03:09:57 +00:00
|
|
|
return client.log.warn(
|
2022-06-05 20:04:25 +00:00
|
|
|
{ height: h, width: w },
|
|
|
|
'Ignoring ANSI CPR screen size query response due to non-sane values'
|
|
|
|
);
|
2022-04-26 03:09:57 +00:00
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
client.term.termHeight = h;
|
|
|
|
client.term.termWidth = w;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-04-26 03:09:57 +00:00
|
|
|
client.log.debug(
|
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
termWidth: client.term.termWidth,
|
|
|
|
termHeight: client.term.termHeight,
|
|
|
|
source: 'ANSI CPR',
|
2022-04-26 03:09:57 +00:00
|
|
|
},
|
|
|
|
'Window size updated'
|
|
|
|
);
|
|
|
|
},
|
|
|
|
'No term size established by CPR within timeout',
|
|
|
|
cb
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-12-31 18:30:40 +00:00
|
|
|
// Start the process:
|
|
|
|
// 1 - Ask to goto 999,999 -- a very much "bottom right" (generally 80x25 for example
|
|
|
|
// is the real size)
|
|
|
|
// 2 - Query for screen size with bansi.txt style specialized Device Status Report (DSR)
|
|
|
|
// request. We expect a CPR of:
|
|
|
|
// a - Terms that support bansi.txt style: Screen size
|
|
|
|
// b - Terms that do not support bansi.txt style: Since we moved to the bottom right
|
|
|
|
// we should still be able to determine a screen size.
|
|
|
|
//
|
|
|
|
client.term.rawWrite(`${ansi.goto(999, 999)}${ansi.queryScreenSize()}`);
|
2014-10-29 11:30:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
function prepareTerminal(term) {
|
2018-12-31 18:30:40 +00:00
|
|
|
term.rawWrite(`${ansi.normal()}${ansi.clearScreen()}`);
|
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) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// note: intentional formatting:
|
2018-06-22 05:15:04 +00:00
|
|
|
term.pipeWrite(`
|
2016-08-27 03:28:29 +00:00
|
|
|
|06Connected to |02EN|10i|02GMA|10½ |06BBS version |12|VN
|
2022-04-06 02:51:03 +00:00
|
|
|
|06Copyright (c) 2014-2022 Bryan Ashby |14- |12http://l33t.codes/
|
2016-08-27 03:28:29 +00:00
|
|
|
|06Updates & source |14- |12https://github.com/NuSkooler/enigma-bbs/
|
2022-06-05 20:04:25 +00:00
|
|
|
|00`);
|
2014-10-31 04:59:21 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 16:36:08 +00:00
|
|
|
function connectEntry(client, nextMenu) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const term = client.term;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function basicPrepWork(callback) {
|
|
|
|
term.rawWrite(ansi.queryDeviceAttributes(0));
|
2022-04-26 03:09:57 +00:00
|
|
|
term.write('\nDetecting client features...');
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
},
|
2022-03-28 19:18:00 +00:00
|
|
|
function discoverHomePosition(callback) {
|
2022-04-26 03:09:57 +00:00
|
|
|
return ansiDiscoverHomePosition(client, callback);
|
2022-03-28 19:18:00 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
function queryTermSizeByNonStandardAnsi(callback) {
|
|
|
|
ansiQueryTermSizeIfNeeded(client, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Check again; We may have got via NAWS/similar before CPR completed.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === term.termHeight || 0 === term.termWidth) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// We still don't have something good for term height/width.
|
|
|
|
// Default to DOS size 80x25.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2019-01-12 17:31:03 +00:00
|
|
|
// :TODO: Netrunner is currently hitting this and it feels wrong. Why is NAWS/ENV/CPR all failing???
|
2022-06-05 20:04:25 +00:00
|
|
|
client.log.warn(
|
|
|
|
{ reason: err.message },
|
|
|
|
'Failed to negotiate term size; Defaulting to 80x25!'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
term.termHeight = 25;
|
2022-06-05 20:04:25 +00:00
|
|
|
term.termWidth = 80;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
});
|
|
|
|
},
|
2019-01-12 17:31:03 +00:00
|
|
|
function checkUtf8IfNeeded(callback) {
|
|
|
|
return ansiAttemptDetectUTF8(client, callback);
|
2022-04-26 03:09:57 +00:00
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
function querySyncTERMFontSupport(callback) {
|
2022-04-26 03:09:57 +00:00
|
|
|
return ansiQuerySyncTermFontSupport(client, callback);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
() => {
|
|
|
|
prepareTerminal(term);
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Always show an ENiGMA½ banner
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
displayBanner(term);
|
|
|
|
|
|
|
|
// fire event
|
2022-06-05 20:04:25 +00:00
|
|
|
Events.emit(Events.getSystemEvents().TermDetected, { client: client });
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
setTimeout(() => {
|
2018-06-22 05:15:04 +00:00
|
|
|
return client.menuStack.goto(nextMenu);
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
);
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|