2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2015-04-04 20:41:04 +00:00
|
|
|
var Log = require('./logger.js').log;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
var iconv = require('iconv-lite');
|
|
|
|
var assert = require('assert');
|
2015-06-19 04:17:51 +00:00
|
|
|
var _ = require('lodash');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
iconv.extendNodeEncodings();
|
|
|
|
|
|
|
|
exports.ClientTerminal = ClientTerminal;
|
|
|
|
|
|
|
|
function ClientTerminal(output) {
|
|
|
|
this.output = output;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var outputEncoding = 'cp437';
|
|
|
|
assert(iconv.encodingExists(outputEncoding));
|
|
|
|
|
|
|
|
// convert line feeds such as \n -> \r\n
|
|
|
|
this.convertLF = true;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Some terminal we handle specially
|
|
|
|
// They can also be found in this.env{}
|
|
|
|
//
|
|
|
|
var termType = 'unknown';
|
|
|
|
var termHeight = 0;
|
|
|
|
var termWidth = 0;
|
|
|
|
|
|
|
|
// Raw values set by e.g. telnet NAWS, ENVIRONMENT, etc.
|
|
|
|
this.env = {};
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'outputEncoding', {
|
|
|
|
get : function() {
|
|
|
|
return outputEncoding;
|
|
|
|
},
|
|
|
|
set : function(enc) {
|
|
|
|
if(iconv.encodingExists(enc)) {
|
|
|
|
outputEncoding = enc;
|
|
|
|
} else {
|
2015-04-04 20:41:04 +00:00
|
|
|
Log.warn({ encoding : enc }, 'Unknown encoding');
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'termType', {
|
|
|
|
get : function() {
|
|
|
|
return termType;
|
|
|
|
},
|
|
|
|
set : function(ttype) {
|
|
|
|
termType = ttype.toLowerCase();
|
|
|
|
|
|
|
|
//
|
|
|
|
// ANSI terminals should be encoded to CP437
|
|
|
|
//
|
2015-06-19 04:17:51 +00:00
|
|
|
// Some terminal types provided by Mercyful Fate / Enthral:
|
|
|
|
// ANSI-BBS
|
|
|
|
// PC-ANSI
|
|
|
|
// QANSI
|
|
|
|
// SCOANSI
|
|
|
|
// VT100
|
|
|
|
// XTERM
|
|
|
|
// LINUX
|
|
|
|
// QNX
|
|
|
|
// SCREEN
|
|
|
|
//
|
|
|
|
if(this.isANSI()) {
|
2014-10-17 02:21:06 +00:00
|
|
|
this.outputEncoding = 'cp437';
|
|
|
|
} else {
|
|
|
|
// :TODO: See how x84 does this -- only set if local/remote are binary
|
|
|
|
this.outputEncoding = 'utf8';
|
|
|
|
}
|
2015-06-19 04:17:51 +00:00
|
|
|
|
|
|
|
Log.debug( { encoding : this.outputEncoding }, 'Set output encoding due to terminal type change');
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientTerminal.prototype.isANSI = function() {
|
2015-06-19 04:17:51 +00:00
|
|
|
// :TODO: Others??
|
|
|
|
return [ 'ansi', 'pc-ansi', 'qansi', 'scoansi' ].indexOf(this.termType) > -1;
|
2014-10-17 02:21:06 +00:00
|
|
|
};
|
|
|
|
|
2014-10-30 04:23:44 +00:00
|
|
|
ClientTerminal.prototype.write = function(s, convertLineFeeds) {
|
2015-06-19 04:17:51 +00:00
|
|
|
convertLineFeeds = _.isUndefined(convertLineFeeds) ? this.convertLF : convertLineFeeds;
|
|
|
|
if(convertLineFeeds && _.isString(s)) {
|
2014-10-17 02:21:06 +00:00
|
|
|
s = s.replace(/\n/g, '\r\n');
|
|
|
|
}
|
|
|
|
this.output.write(iconv.encode(s, this.outputEncoding));
|
2014-10-30 04:23:44 +00:00
|
|
|
};
|