2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2015-08-20 04:10:18 +00:00
|
|
|
var Log = require('./logger.js').log;
|
|
|
|
var enigmaToAnsi = require('./color_codes.js').enigmaToAnsi;
|
2015-10-13 06:35:37 +00:00
|
|
|
var renegadeToAnsi = require('./color_codes.js').renegadeToAnsi;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2015-08-20 04:10:18 +00:00
|
|
|
var iconv = require('iconv-lite');
|
|
|
|
var assert = require('assert');
|
|
|
|
var _ = require('lodash');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
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;
|
2015-07-07 04:37:11 +00:00
|
|
|
var termClient = 'unknown';
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
// 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();
|
2015-12-14 17:33:15 +00:00
|
|
|
|
2015-06-19 04:17:51 +00:00
|
|
|
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
|
|
|
|
2015-07-07 04:37:11 +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
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2015-07-07 04:37:11 +00:00
|
|
|
|
|
|
|
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() {
|
|
|
|
this.output = null;
|
|
|
|
};
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
ClientTerminal.prototype.isANSI = function() {
|
2015-12-14 17:33:15 +00:00
|
|
|
//
|
|
|
|
// ANSI terminals should be encoded to CP437
|
|
|
|
//
|
|
|
|
// Some terminal types provided by Mercyful Fate / Enthral:
|
|
|
|
// ANSI-BBS
|
|
|
|
// PC-ANSI
|
|
|
|
// QANSI
|
|
|
|
// SCOANSI
|
|
|
|
// VT100
|
|
|
|
// QNX
|
|
|
|
//
|
|
|
|
// Reports from various terminals
|
|
|
|
//
|
|
|
|
// syncterm:
|
|
|
|
// * SyncTERM
|
|
|
|
//
|
|
|
|
// xterm:
|
|
|
|
// * PuTTY
|
|
|
|
//
|
|
|
|
// ansi-bbs:
|
|
|
|
// * fTelnet
|
|
|
|
//
|
|
|
|
// screen:
|
|
|
|
// * ConnectBot (Android)
|
|
|
|
//
|
|
|
|
// linux:
|
|
|
|
// * JuiceSSH (note: TERM=linux also)
|
|
|
|
//
|
|
|
|
|
2015-06-19 04:17:51 +00:00
|
|
|
// :TODO: Others??
|
2015-12-14 17:33:15 +00:00
|
|
|
return [ 'ansi', 'pc-ansi', 'ansi-bbs', 'qansi', 'scoansi', 'syncterm' ].indexOf(this.termType) > -1;
|
2014-10-17 02:21:06 +00:00
|
|
|
};
|
|
|
|
|
2015-07-11 22:39:42 +00:00
|
|
|
// :TODO: probably need to update these to convert IAC (0xff) -> IACIAC (escape it)
|
|
|
|
|
2016-01-02 19:11:40 +00:00
|
|
|
ClientTerminal.prototype.write = function(s, convertLineFeeds, cb) {
|
|
|
|
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) {
|
2015-12-08 02:12:31 +00:00
|
|
|
if(this.output) {
|
|
|
|
this.output.write(s, function written(err) {
|
2016-01-02 19:11:40 +00:00
|
|
|
if(_.isFunction(cb)) {
|
|
|
|
cb(err);
|
|
|
|
} else if(err) {
|
2015-12-08 02:12:31 +00:00
|
|
|
Log.warn('Failed writing to socket: ' + err.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-07-06 01:05:55 +00:00
|
|
|
};
|
|
|
|
|
2016-01-02 19:11:40 +00:00
|
|
|
ClientTerminal.prototype.pipeWrite = function(s, spec, cb) {
|
2015-12-22 01:07:03 +00:00
|
|
|
spec = spec || 'renegade';
|
2015-10-13 06:35:37 +00:00
|
|
|
|
|
|
|
var conv = {
|
|
|
|
enigma : enigmaToAnsi,
|
|
|
|
renegade : renegadeToAnsi,
|
|
|
|
}[spec] || enigmaToAnsi;
|
|
|
|
|
2016-01-02 19:11:40 +00:00
|
|
|
this.write(conv(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) {
|
2016-01-02 19:11:40 +00:00
|
|
|
convertLineFeeds = _.isBoolean(convertLineFeeds) ? convertLineFeeds : this.convertLF;
|
|
|
|
|
2015-07-06 01:05:55 +00:00
|
|
|
if(convertLineFeeds && _.isString(s)) {
|
|
|
|
s = s.replace(/\n/g, '\r\n');
|
|
|
|
}
|
|
|
|
return iconv.encode(s, this.outputEncoding);
|
|
|
|
};
|
|
|
|
|