2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
var Log = require('./logger.js').log;
|
|
|
|
var renegadeToAnsi = require('./color_codes.js').renegadeToAnsi;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
var iconv = require('iconv-lite');
|
|
|
|
var assert = require('assert');
|
|
|
|
var _ = require('lodash');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
exports.ClientTerminal = ClientTerminal;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
function ClientTerminal(output) {
|
2018-06-23 03:26:46 +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
|
|
|
|
this.convertLF = true;
|
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
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
var termType = 'unknown';
|
|
|
|
var termHeight = 0;
|
|
|
|
var termWidth = 0;
|
|
|
|
var termClient = 'unknown';
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +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.
|
|
|
|
this.env = {};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
Object.defineProperty(this, 'outputEncoding', {
|
|
|
|
get : function() {
|
|
|
|
return outputEncoding;
|
|
|
|
},
|
|
|
|
set : function(enc) {
|
|
|
|
if(iconv.encodingExists(enc)) {
|
|
|
|
outputEncoding = enc;
|
|
|
|
} else {
|
|
|
|
Log.warn({ encoding : enc }, 'Unknown encoding');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'termType', {
|
|
|
|
get : function() {
|
|
|
|
return termType;
|
|
|
|
},
|
|
|
|
set : function(ttype) {
|
|
|
|
termType = ttype.toLowerCase();
|
|
|
|
|
|
|
|
if(this.isANSI()) {
|
|
|
|
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
|
|
|
|
|
|
|
Log.debug( { encoding : this.outputEncoding }, 'Set output encoding due to terminal type change');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'termWidth', {
|
|
|
|
get : function() {
|
|
|
|
return termWidth;
|
|
|
|
},
|
|
|
|
set : function(width) {
|
|
|
|
if(width > 0) {
|
|
|
|
termWidth = width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'termHeight', {
|
|
|
|
get : function() {
|
|
|
|
return termHeight;
|
|
|
|
},
|
|
|
|
set : function(height) {
|
|
|
|
if(height > 0) {
|
|
|
|
termHeight = height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'termClient', {
|
|
|
|
get : function() {
|
|
|
|
return termClient;
|
|
|
|
},
|
|
|
|
set : function(tc) {
|
|
|
|
termClient = tc;
|
|
|
|
|
|
|
|
Log.debug( { termClient : this.termClient }, 'Set known terminal client');
|
|
|
|
}
|
|
|
|
});
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 02:12:31 +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
|
|
|
};
|
|
|
|
|
2017-09-24 17:15:26 +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
|
|
|
//
|
|
|
|
if(this.termType.startsWith('xterm')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ 'xterm', 'linux', 'screen', 'dumb', 'rxvt', 'konsole', 'gnome', 'x11 terminal emulator' ].includes(this.termType);
|
2017-09-24 17:15:26 +00:00
|
|
|
};
|
|
|
|
|
2014-10-17 02:21:06 +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
|
|
|
//
|
2020-11-25 20:03:16 +00:00
|
|
|
return [ 'ansi', 'pcansi', 'pc-ansi', 'ansi-bbs', 'qansi', 'scoansi', 'syncterm', 'ansi-256color' ].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
|
|
|
|
2016-01-02 19:11:40 +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
|
|
|
};
|
|
|
|
|
2016-01-02 19:11:40 +00:00
|
|
|
ClientTerminal.prototype.rawWrite = function(s, cb) {
|
2020-11-15 22:06:49 +00:00
|
|
|
if(this.output && this.output.writable) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.output.write(s, err => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(err) {
|
|
|
|
Log.warn( { error : err.message }, 'Failed writing to socket');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-07-06 01:05:55 +00:00
|
|
|
};
|
|
|
|
|
2018-09-15 02:34:39 +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
|
|
|
};
|
|
|
|
|
2015-07-06 01:05:55 +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
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(convertLineFeeds && _.isString(s)) {
|
|
|
|
s = s.replace(/\n/g, '\r\n');
|
|
|
|
}
|
|
|
|
return iconv.encode(s, this.outputEncoding);
|
2015-07-06 01:05:55 +00:00
|
|
|
};
|
|
|
|
|
2017-09-04 02:45:00 +00:00
|
|
|
|