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
|
|
|
var Log = require('./logger.js').log;
|
|
|
|
var renegadeToAnsi = require('./color_codes.js').renegadeToAnsi;
|
|
|
|
const Config = require('./config.js').get;
|
|
|
|
var iconv = require('iconv-lite');
|
|
|
|
var assert = require('assert');
|
|
|
|
var _ = require('lodash');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.ClientTerminal = ClientTerminal;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
function ClientTerminal(output) {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.output = output;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
var outputEncoding = 'cp437';
|
|
|
|
assert(iconv.encodingExists(outputEncoding));
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// convert line feeds such as \n -> \r\n
|
2022-06-05 20:04:25 +00:00
|
|
|
this.convertLF = true;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-04-26 03:09:57 +00:00
|
|
|
this.syncTermFontsEnabled = false;
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Some terminal we handle specially
|
|
|
|
// They can also be found in this.env{}
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
var termType = 'unknown';
|
|
|
|
var termHeight = 0;
|
|
|
|
var termWidth = 0;
|
|
|
|
var termClient = 'unknown';
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.currentSyncFont = 'not_set';
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// Raw values set by e.g. telnet NAWS, ENVIRONMENT, etc.
|
2022-06-05 20:04:25 +00:00
|
|
|
this.env = {};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
Object.defineProperty(this, 'outputEncoding', {
|
2022-06-05 20:04:25 +00:00
|
|
|
get: function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return outputEncoding;
|
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
set: function (enc) {
|
|
|
|
if (iconv.encodingExists(enc)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
outputEncoding = enc;
|
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.warn({ encoding: enc }, 'Unknown encoding');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'termType', {
|
2022-06-05 20:04:25 +00:00
|
|
|
get: function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return termType;
|
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
set: function (ttype) {
|
2018-06-22 05:15:04 +00:00
|
|
|
termType = ttype.toLowerCase();
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.isANSI()) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.outputEncoding = 'cp437';
|
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: See how x84 does this -- only set if local/remote are binary
|
2018-06-22 05:15:04 +00:00
|
|
|
this.outputEncoding = 'utf8';
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: according to this: http://mud-dev.wikidot.com/article:telnet-client-identification
|
|
|
|
// Windows telnet will send "VTNT". If so, set termClient='windows'
|
|
|
|
// there are some others on the page as well
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.debug(
|
|
|
|
{ encoding: this.outputEncoding },
|
|
|
|
'Set output encoding due to terminal type change'
|
|
|
|
);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'termWidth', {
|
2022-06-05 20:04:25 +00:00
|
|
|
get: function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return termWidth;
|
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
set: function (width) {
|
|
|
|
if (width > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
termWidth = width;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'termHeight', {
|
2022-06-05 20:04:25 +00:00
|
|
|
get: function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return termHeight;
|
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
set: function (height) {
|
|
|
|
if (height > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
termHeight = height;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'termClient', {
|
2022-06-05 20:04:25 +00:00
|
|
|
get: function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return termClient;
|
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
set: function (tc) {
|
2018-06-22 05:15:04 +00:00
|
|
|
termClient = tc;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.debug({ termClient: this.termClient }, 'Set known terminal client');
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ClientTerminal.prototype.disconnect = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.output = null;
|
2015-12-08 02:12:31 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ClientTerminal.prototype.isNixTerm = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Standard *nix type terminals
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.termType.startsWith('xterm')) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-23 23:43:49 +00:00
|
|
|
const utf8TermList = Config().term.utf8TermList;
|
|
|
|
return utf8TermList.includes(this.termType);
|
2017-09-24 17:15:26 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ClientTerminal.prototype.isANSI = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// ANSI terminals should be encoded to CP437
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Some terminal types provided by Mercyful Fate / Enthral:
|
|
|
|
// ANSI-BBS
|
|
|
|
// PC-ANSI
|
|
|
|
// QANSI
|
|
|
|
// SCOANSI
|
|
|
|
// VT100
|
|
|
|
// QNX
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Reports from various terminals
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2020-11-25 20:03:16 +00:00
|
|
|
// NetRunner v2.00beta 20
|
|
|
|
// * This version adds 256 colors and reports as "ansi-256color"
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// syncterm:
|
|
|
|
// * SyncTERM
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// xterm:
|
|
|
|
// * PuTTY
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// ansi-bbs:
|
|
|
|
// * fTelnet
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// pcansi:
|
|
|
|
// * ZOC
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// screen:
|
|
|
|
// * ConnectBot (Android)
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// linux:
|
|
|
|
// * JuiceSSH (note: TERM=linux also)
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-03-23 23:43:49 +00:00
|
|
|
const cp437TermList = Config().term.cp437TermList;
|
|
|
|
return cp437TermList.includes(this.termType);
|
2014-10-17 02:21:06 +00:00
|
|
|
};
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: probably need to update these to convert IAC (0xff) -> IACIAC (escape it)
|
2015-07-11 22:39:42 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ClientTerminal.prototype.write = function (s, convertLineFeeds, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.rawWrite(this.encode(s, convertLineFeeds), cb);
|
2015-07-06 01:05:55 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ClientTerminal.prototype.rawWrite = function (s, cb) {
|
|
|
|
if (this.output && this.output.writable) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.output.write(s, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
Log.warn({ error: err.message }, 'Failed writing to socket');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-07-06 01:05:55 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ClientTerminal.prototype.pipeWrite = function (s, cb) {
|
|
|
|
this.write(renegadeToAnsi(s, this), null, cb); // null = use default for |convertLineFeeds|
|
2015-08-20 04:10:18 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ClientTerminal.prototype.encode = function (s, convertLineFeeds) {
|
2018-06-22 05:15:04 +00:00
|
|
|
convertLineFeeds = _.isBoolean(convertLineFeeds) ? convertLineFeeds : this.convertLF;
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (convertLineFeeds && _.isString(s)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
s = s.replace(/\n/g, '\r\n');
|
|
|
|
}
|
|
|
|
return iconv.encode(s, this.outputEncoding);
|
2015-07-06 01:05:55 +00:00
|
|
|
};
|