2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2015-06-04 23:06:37 +00:00
|
|
|
/*
|
|
|
|
Portions of this code for key handling heavily inspired from the following:
|
|
|
|
https://github.com/chjj/blessed/blob/master/lib/keys.js
|
|
|
|
|
|
|
|
MIT license is as follows:
|
|
|
|
--------------------------
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) <year> <copyright holders>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
--------------------------
|
|
|
|
*/
|
2015-03-19 05:08:23 +00:00
|
|
|
var term = require('./client_term.js');
|
2014-10-17 02:21:06 +00:00
|
|
|
var miscUtil = require('./misc_util.js');
|
|
|
|
var ansi = require('./ansi_term.js');
|
2015-03-19 05:08:23 +00:00
|
|
|
var Log = require('./logger.js').log;
|
2014-10-30 04:23:44 +00:00
|
|
|
var user = require('./user.js');
|
2015-03-19 05:08:23 +00:00
|
|
|
var moduleUtil = require('./module_util.js');
|
2015-03-23 04:52:04 +00:00
|
|
|
var menuUtil = require('./menu_util.js');
|
2015-08-05 04:35:59 +00:00
|
|
|
var Config = require('./config.js').config;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2015-06-05 04:29:14 +00:00
|
|
|
var stream = require('stream');
|
|
|
|
var assert = require('assert');
|
|
|
|
var _ = require('lodash');
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
exports.Client = Client;
|
|
|
|
|
2014-10-20 03:06:39 +00:00
|
|
|
// :TODO: Move all of the key stuff to it's own module
|
2015-04-27 22:04:41 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Resources & Standards:
|
|
|
|
// * http://www.ansi-bbs.org/ansi-bbs-core-server.html
|
|
|
|
//
|
2014-10-20 03:06:39 +00:00
|
|
|
|
2014-10-29 07:33:04 +00:00
|
|
|
// :TODO: put this in a common area!!!!
|
|
|
|
function getIntArgArray(array) {
|
|
|
|
var i = array.length;
|
|
|
|
while(i--) {
|
|
|
|
array[i] = parseInt(array[i], 10);
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2015-07-07 04:37:11 +00:00
|
|
|
var RE_DSR_RESPONSE_ANYWHERE = /(?:\u001b\[)([0-9\;]+)(R)/;
|
|
|
|
var RE_DEV_ATTR_RESPONSE_ANYWHERE = /(?:\u001b\[)[\=\?]([0-9a-zA-Z\;]+)(c)/;
|
2015-06-05 04:29:14 +00:00
|
|
|
var RE_META_KEYCODE_ANYWHERE = /(?:\u001b)([a-zA-Z0-9])/;
|
|
|
|
var RE_META_KEYCODE = new RegExp('^' + RE_META_KEYCODE_ANYWHERE.source + '$');
|
|
|
|
var RE_FUNCTION_KEYCODE_ANYWHERE = new RegExp('(?:\u001b+)(O|N|\\[|\\[\\[)(?:' + [
|
|
|
|
'(\\d+)(?:;(\\d+))?([~^$])',
|
|
|
|
'(?:M([@ #!a`])(.)(.))', // mouse stuff
|
2015-06-06 06:33:59 +00:00
|
|
|
'(?:1;)?(\\d+)?([a-zA-Z@])'
|
2015-06-05 04:29:14 +00:00
|
|
|
].join('|') + ')');
|
|
|
|
|
|
|
|
var RE_FUNCTION_KEYCODE = new RegExp('^' + RE_FUNCTION_KEYCODE_ANYWHERE.source);
|
|
|
|
var RE_ESC_CODE_ANYWHERE = new RegExp( [
|
|
|
|
RE_FUNCTION_KEYCODE_ANYWHERE.source,
|
|
|
|
RE_META_KEYCODE_ANYWHERE.source,
|
2015-06-05 22:20:26 +00:00
|
|
|
RE_DSR_RESPONSE_ANYWHERE.source,
|
2015-07-07 04:37:11 +00:00
|
|
|
RE_DEV_ATTR_RESPONSE_ANYWHERE.source,
|
2015-06-05 04:29:14 +00:00
|
|
|
/\u001b./.source
|
|
|
|
].join('|'));
|
|
|
|
|
2015-06-04 23:06:37 +00:00
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
function Client(input, output) {
|
|
|
|
stream.call(this);
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2015-04-17 04:29:53 +00:00
|
|
|
this.input = input;
|
|
|
|
this.output = output;
|
|
|
|
this.term = new term.ClientTerminal(this.output);
|
|
|
|
this.user = new user.User();
|
2015-07-07 04:37:11 +00:00
|
|
|
this.currentTheme = { info : { name : 'N/A', description : 'None' } };
|
2015-08-05 04:35:59 +00:00
|
|
|
this.lastKeyPressMs = Date.now();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Every 1m, check for idle.
|
|
|
|
//
|
|
|
|
this.idleCheck = setInterval(function checkForIdle() {
|
|
|
|
var nowMs = Date.now();
|
|
|
|
|
|
|
|
if(nowMs - self.lastKeyPressMs >= (Config.misc.idleLogoutSeconds * 1000)) {
|
|
|
|
self.emit('idle timeout');
|
|
|
|
}
|
|
|
|
}, 1000 * 60);
|
2014-10-20 03:06:39 +00:00
|
|
|
|
2015-08-01 07:00:15 +00:00
|
|
|
Object.defineProperty(this, 'node', {
|
|
|
|
get : function() {
|
2015-09-26 05:10:18 +00:00
|
|
|
return self.session.id + 1;
|
2015-08-01 07:00:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-04 23:06:37 +00:00
|
|
|
//
|
|
|
|
// Peek at incoming |data| and emit events for any special
|
|
|
|
// handling that may include:
|
|
|
|
// * Keyboard input
|
|
|
|
// * ANSI CSR's and the like
|
|
|
|
//
|
|
|
|
// References:
|
|
|
|
// * http://www.ansi-bbs.org/ansi-bbs-core-server.html
|
2015-06-05 22:20:26 +00:00
|
|
|
// * Christopher Jeffrey's Blessed library @ https://github.com/chjj/blessed/
|
2015-06-04 23:06:37 +00:00
|
|
|
//
|
2015-07-07 04:37:11 +00:00
|
|
|
this.getTermClient = function(deviceAttr) {
|
|
|
|
var termClient = {
|
|
|
|
//
|
|
|
|
// See http://www.fbl.cz/arctel/download/techman.pdf
|
|
|
|
//
|
|
|
|
// Known clients:
|
|
|
|
// * Irssi ConnectBot (Android)
|
|
|
|
//
|
|
|
|
'63;1;2' : 'arctel',
|
|
|
|
}[deviceAttr];
|
|
|
|
|
|
|
|
if(!termClient) {
|
|
|
|
if(_.startsWith(deviceAttr, '67;84;101;114;109')) {
|
|
|
|
//
|
|
|
|
// See https://github.com/protomouse/synchronet/blob/master/src/conio/cterm.txt
|
|
|
|
//
|
|
|
|
// Known clients:
|
|
|
|
// * SyncTERM
|
|
|
|
//
|
|
|
|
termClient = 'cterm';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return termClient;
|
|
|
|
};
|
|
|
|
|
2015-06-05 04:29:14 +00:00
|
|
|
this.isMouseInput = function(data) {
|
|
|
|
return /\x1b\[M/.test(data) ||
|
|
|
|
/\u001b\[M([\x00\u0020-\uffff]{3})/.test(data) ||
|
|
|
|
/\u001b\[(\d+;\d+;\d+)M/.test(data) ||
|
|
|
|
/\u001b\[<(\d+;\d+;\d+)([mM])/.test(data) ||
|
|
|
|
/\u001b\[<(\d+;\d+;\d+;\d+)&w/.test(data) ||
|
|
|
|
/\u001b\[24([0135])~\[(\d+),(\d+)\]\r/.test(data) ||
|
|
|
|
/\u001b\[(O|I)/.test(data);
|
2015-06-04 23:06:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.getKeyComponentsFromCode = function(code) {
|
|
|
|
return {
|
2015-06-05 04:29:14 +00:00
|
|
|
// xterm/gnome
|
2015-06-04 23:06:37 +00:00
|
|
|
'OP' : { name : 'f1' },
|
2015-06-05 04:29:14 +00:00
|
|
|
'OQ' : { name : 'f2' },
|
|
|
|
'OR' : { name : 'f3' },
|
|
|
|
'OS' : { name : 'f4' },
|
|
|
|
|
|
|
|
'OA' : { name : 'up arrow' },
|
|
|
|
'OB' : { name : 'down arrow' },
|
|
|
|
'OC' : { name : 'right arrow' },
|
|
|
|
'OD' : { name : 'left arrow' },
|
|
|
|
'OE' : { name : 'clear' },
|
|
|
|
'OF' : { name : 'end' },
|
|
|
|
'OH' : { name : 'home' },
|
|
|
|
|
|
|
|
// xterm/rxvt
|
|
|
|
'[11~' : { name : 'f1' },
|
|
|
|
'[12~' : { name : 'f2' },
|
|
|
|
'[13~' : { name : 'f3' },
|
|
|
|
'[14~' : { name : 'f4' },
|
|
|
|
|
|
|
|
'[1~' : { name : 'home' },
|
|
|
|
'[2~' : { name : 'insert' },
|
|
|
|
'[3~' : { name : 'delete' },
|
|
|
|
'[4~' : { name : 'end' },
|
|
|
|
'[5~' : { name : 'page up' },
|
|
|
|
'[6~' : { name : 'page down' },
|
|
|
|
|
|
|
|
// Cygwin & libuv
|
|
|
|
'[[A' : { name : 'f1' },
|
|
|
|
'[[B' : { name : 'f2' },
|
|
|
|
'[[C' : { name : 'f3' },
|
|
|
|
'[[D' : { name : 'f4' },
|
|
|
|
'[[E' : { name : 'f5' },
|
|
|
|
|
|
|
|
// Common impls
|
|
|
|
'[15~' : { name : 'f5' },
|
|
|
|
'[17~' : { name : 'f6' },
|
|
|
|
'[18~' : { name : 'f7' },
|
|
|
|
'[19~' : { name : 'f8' },
|
|
|
|
'[20~' : { name : 'f9' },
|
|
|
|
'[21~' : { name : 'f10' },
|
|
|
|
'[23~' : { name : 'f11' },
|
|
|
|
'[24~' : { name : 'f12' },
|
|
|
|
|
|
|
|
// xterm
|
|
|
|
'[A' : { name : 'up arrow' },
|
|
|
|
'[B' : { name : 'down arrow' },
|
|
|
|
'[C' : { name : 'right arrow' },
|
|
|
|
'[D' : { name : 'left arrow' },
|
|
|
|
'[E' : { name : 'clear' },
|
|
|
|
'[F' : { name : 'end' },
|
|
|
|
'[H' : { name : 'home' },
|
|
|
|
|
|
|
|
// PuTTY
|
|
|
|
'[[5~' : { name : 'page up' },
|
|
|
|
'[[6~' : { name : 'page down' },
|
|
|
|
|
|
|
|
// rvxt
|
|
|
|
'[7~' : { name : 'home' },
|
|
|
|
'[8~' : { name : 'end' },
|
|
|
|
|
|
|
|
// rxvt with modifiers
|
|
|
|
'[a' : { name : 'up arrow', shift : true },
|
|
|
|
'[b' : { name : 'down arrow', shift : true },
|
|
|
|
'[c' : { name : 'right arrow', shift : true },
|
|
|
|
'[d' : { name : 'left arrow', shift : true },
|
|
|
|
'[e' : { name : 'clear', shift : true },
|
|
|
|
|
|
|
|
'[2$' : { name : 'insert', shift : true },
|
|
|
|
'[3$' : { name : 'delete', shift : true },
|
|
|
|
'[5$' : { name : 'page up', shift : true },
|
|
|
|
'[6$' : { name : 'page down', shift : true },
|
|
|
|
'[7$' : { name : 'home', shift : true },
|
|
|
|
'[8$' : { name : 'end', shift : true },
|
|
|
|
|
|
|
|
'Oa' : { name : 'up arrow', ctrl : true },
|
|
|
|
'Ob' : { name : 'down arrow', ctrl : true },
|
|
|
|
'Oc' : { name : 'right arrow', ctrl : true },
|
|
|
|
'Od' : { name : 'left arrow', ctrl : true },
|
|
|
|
'Oe' : { name : 'clear', ctrl : true },
|
|
|
|
|
|
|
|
'[2^' : { name : 'insert', ctrl : true },
|
|
|
|
'[3^' : { name : 'delete', ctrl : true },
|
|
|
|
'[5^' : { name : 'page up', ctrl : true },
|
|
|
|
'[6^' : { name : 'page down', ctrl : true },
|
|
|
|
'[7^' : { name : 'home', ctrl : true },
|
|
|
|
'[8^' : { name : 'end', ctrl : true },
|
|
|
|
|
2015-06-06 06:33:59 +00:00
|
|
|
// SyncTERM / EtherTerm
|
2015-06-05 22:20:26 +00:00
|
|
|
'[K' : { name : 'end' },
|
2015-06-06 06:33:59 +00:00
|
|
|
'[@' : { name : 'insert' },
|
2015-06-22 04:36:07 +00:00
|
|
|
'[V' : { name : 'page up' },
|
|
|
|
'[U' : { name : 'page down' },
|
2015-06-05 22:20:26 +00:00
|
|
|
|
2015-06-05 04:29:14 +00:00
|
|
|
// other
|
|
|
|
'[Z' : { name : 'tab', shift : true },
|
2015-06-04 23:06:37 +00:00
|
|
|
}[code];
|
|
|
|
};
|
|
|
|
|
2015-06-05 04:29:14 +00:00
|
|
|
this.on('data', function clientData(data) {
|
2015-06-04 23:06:37 +00:00
|
|
|
// create a uniform format that can be parsed below
|
|
|
|
if(data[0] > 127 && undefined === data[1]) {
|
|
|
|
data[0] -= 128;
|
|
|
|
data = '\u001b' + data.toString('utf-8');
|
|
|
|
} else {
|
|
|
|
data = data.toString('utf-8');
|
|
|
|
}
|
|
|
|
|
|
|
|
if(self.isMouseInput(data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf = [];
|
|
|
|
var m;
|
2015-06-05 04:29:14 +00:00
|
|
|
while((m = RE_ESC_CODE_ANYWHERE.exec(data))) {
|
2015-06-04 23:06:37 +00:00
|
|
|
buf = buf.concat(data.slice(0, m.index).split(''));
|
|
|
|
buf.push(m[0]);
|
|
|
|
data = data.slice(m.index + m[0].length);
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = buf.concat(data.split('')); // remainder
|
|
|
|
|
|
|
|
buf.forEach(function bufPart(s) {
|
|
|
|
var key = {
|
|
|
|
seq : s,
|
|
|
|
name : undefined,
|
|
|
|
ctrl : false,
|
|
|
|
meta : false,
|
|
|
|
shift : false,
|
|
|
|
};
|
|
|
|
|
|
|
|
var parts;
|
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
if((parts = RE_DSR_RESPONSE_ANYWHERE.exec(s))) {
|
2015-07-07 04:37:11 +00:00
|
|
|
if('R' === parts[2]) { // :TODO: this should be a assert -- currently only looking for R, unless we start to handle 'n', or others
|
2015-06-05 04:29:14 +00:00
|
|
|
var cprArgs = getIntArgArray(parts[1].split(';'));
|
|
|
|
if(2 === cprArgs.length) {
|
|
|
|
self.emit('cursor position report', cprArgs);
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 04:37:11 +00:00
|
|
|
} else if((parts = RE_DEV_ATTR_RESPONSE_ANYWHERE.exec(s))) {
|
|
|
|
assert('c' === parts[2]);
|
|
|
|
var termClient = self.getTermClient(parts[1]);
|
|
|
|
if(termClient) {
|
|
|
|
self.term.termClient = termClient;
|
|
|
|
}
|
2015-06-05 04:29:14 +00:00
|
|
|
} else if('\r' === s) {
|
2015-06-04 23:06:37 +00:00
|
|
|
key.name = 'return';
|
|
|
|
} else if('\n' === s) {
|
|
|
|
key.name = 'line feed';
|
|
|
|
} else if('\t' === s) {
|
|
|
|
key.name = 'tab';
|
|
|
|
} else if ('\b' === s || '\x7f' === s || '\x1b\x7f' === s || '\x1b\b' === s) {
|
|
|
|
// backspace, CTRL-H
|
|
|
|
key.name = 'backspace';
|
|
|
|
key.meta = ('\x1b' === s.charAt(0));
|
|
|
|
} else if('\x1b' === s || '\x1b\x1b' === s) {
|
|
|
|
key.name = 'escape';
|
|
|
|
key.meta = (2 === s.length);
|
|
|
|
} else if (' ' === s || '\x1b ' === s) {
|
|
|
|
// rather annoying that space can come in other than just " "
|
|
|
|
key.name = 'space';
|
|
|
|
key.meta = (2 === s.length);
|
|
|
|
} else if(1 === s.length && s <= '\x1a') {
|
|
|
|
// CTRL-<letter>
|
|
|
|
key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1);
|
|
|
|
key.ctrl = true;
|
|
|
|
} else if(1 === s.length && s >= 'a' && s <= 'z') {
|
|
|
|
// normal, lowercased letter
|
|
|
|
key.name = s;
|
|
|
|
} else if(1 === s.length && s >= 'A' && s <= 'Z') {
|
|
|
|
key.name = s.toLowerCase();
|
|
|
|
key.shift = true;
|
2015-06-05 04:29:14 +00:00
|
|
|
} else if ((parts = RE_META_KEYCODE.exec(s))) {
|
2015-06-04 23:06:37 +00:00
|
|
|
// meta with character key
|
|
|
|
key.name = parts[1].toLowerCase();
|
|
|
|
key.meta = true;
|
|
|
|
key.shift = /^[A-Z]$/.test(parts[1]);
|
2015-06-05 04:29:14 +00:00
|
|
|
} else if((parts = RE_FUNCTION_KEYCODE.exec(s))) {
|
2015-06-04 23:06:37 +00:00
|
|
|
var code =
|
|
|
|
(parts[1] || '') + (parts[2] || '') +
|
|
|
|
(parts[4] || '') + (parts[9] || '');
|
|
|
|
var modifier = (parts[3] || parts[8] || 1) - 1;
|
|
|
|
|
|
|
|
key.ctrl = !!(modifier & 4);
|
|
|
|
key.meta = !!(modifier & 10);
|
|
|
|
key.shift = !!(modifier & 1);
|
|
|
|
key.code = code;
|
2015-06-05 04:29:14 +00:00
|
|
|
|
|
|
|
_.assign(key, self.getKeyComponentsFromCode(code));
|
|
|
|
}
|
|
|
|
|
|
|
|
var ch;
|
|
|
|
if(1 === s.length) {
|
|
|
|
ch = s;
|
|
|
|
} else if('space' === key.name) {
|
|
|
|
// stupid hack to always get space as a regular char
|
|
|
|
ch = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_.isUndefined(key.name)) {
|
|
|
|
key = undefined;
|
2015-06-05 22:20:26 +00:00
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Adjust name for CTRL/Shift/Meta modifiers
|
|
|
|
//
|
|
|
|
key.name =
|
|
|
|
(key.ctrl ? 'ctrl + ' : '') +
|
|
|
|
(key.meta ? 'meta + ' : '') +
|
|
|
|
(key.shift ? 'shift + ' : '') +
|
|
|
|
key.name;
|
2015-06-04 23:06:37 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 04:29:14 +00:00
|
|
|
if(key || ch) {
|
2015-07-04 21:21:50 +00:00
|
|
|
self.log.trace( { key : key, ch : escape(ch) }, 'User keyboard input');
|
2015-06-06 06:33:59 +00:00
|
|
|
|
2015-08-05 04:35:59 +00:00
|
|
|
self.lastKeyPressMs = Date.now();
|
|
|
|
|
2015-06-05 04:29:14 +00:00
|
|
|
self.emit('key press', ch, key);
|
|
|
|
}
|
2015-06-04 23:06:37 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-24 05:12:19 +00:00
|
|
|
self.detachCurrentMenuModule = function() {
|
|
|
|
if(self.currentMenuModule) {
|
2015-09-18 05:11:21 +00:00
|
|
|
|
|
|
|
var savedState = self.currentMenuModule.getSaveState();
|
|
|
|
|
2015-03-24 05:12:19 +00:00
|
|
|
self.currentMenuModule.leave();
|
2015-09-18 04:53:19 +00:00
|
|
|
|
2015-09-18 05:11:21 +00:00
|
|
|
self.lastMenuModuleInfo = self.currentMenuModuleInfo;
|
|
|
|
self.lastMenuModuleInfo.savedState = savedState;
|
2015-09-18 04:53:19 +00:00
|
|
|
|
2015-03-24 05:12:19 +00:00
|
|
|
self.currentMenuModule = null;
|
|
|
|
}
|
|
|
|
};
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require('util').inherits(Client, stream);
|
|
|
|
|
|
|
|
Client.prototype.end = function () {
|
2015-03-24 05:12:19 +00:00
|
|
|
this.detachCurrentMenuModule();
|
2015-08-05 04:35:59 +00:00
|
|
|
|
|
|
|
clearInterval(this.idleCheck);
|
2015-03-24 05:12:19 +00:00
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
return this.output.end.apply(this.output, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.destroy = function () {
|
|
|
|
return this.output.destroy.apply(this.output, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.destroySoon = function () {
|
|
|
|
return this.output.destroySoon.apply(this.output, arguments);
|
|
|
|
};
|
|
|
|
|
2014-10-20 03:06:39 +00:00
|
|
|
Client.prototype.waitForKeyPress = function(cb) {
|
2015-06-05 22:20:26 +00:00
|
|
|
this.once('key press', function kp(ch, key) {
|
|
|
|
cb(ch, key);
|
2014-10-17 02:21:06 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.address = function() {
|
|
|
|
return this.input.address();
|
|
|
|
};
|
|
|
|
|
2015-04-04 20:41:04 +00:00
|
|
|
Client.prototype.gotoMenuModule = function(options, cb) {
|
2015-03-19 05:08:23 +00:00
|
|
|
var self = this;
|
2015-04-04 20:41:04 +00:00
|
|
|
|
2015-09-08 03:18:09 +00:00
|
|
|
assert(_.isString(options.name), 'Missing options.name');
|
2015-03-19 05:08:23 +00:00
|
|
|
|
|
|
|
// Assign a default missing module handler callback if none was provided
|
2015-08-05 04:35:59 +00:00
|
|
|
var callbackOnErrorOnly = !_.isFunction(cb);
|
|
|
|
|
2015-03-19 05:08:23 +00:00
|
|
|
cb = miscUtil.valueWithDefault(cb, self.defaultHandlerMissingMod());
|
|
|
|
|
2015-03-24 05:12:19 +00:00
|
|
|
self.detachCurrentMenuModule();
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2015-04-04 20:41:04 +00:00
|
|
|
var loadOptions = {
|
2015-08-16 19:35:34 +00:00
|
|
|
name : options.name,
|
|
|
|
client : self,
|
|
|
|
extraArgs : options.extraArgs,
|
2015-04-04 20:41:04 +00:00
|
|
|
};
|
|
|
|
|
2015-09-18 04:53:19 +00:00
|
|
|
menuUtil.loadMenu(loadOptions, function menuModuleLoaded(err, modInst) {
|
2015-03-19 05:08:23 +00:00
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
2015-07-03 05:28:39 +00:00
|
|
|
self.log.debug( { menuName : options.name }, 'Goto menu module');
|
2015-03-23 04:52:04 +00:00
|
|
|
|
2015-09-18 04:53:19 +00:00
|
|
|
self.currentMenuModule = modInst; // :TODO: should probably be before enter() above
|
|
|
|
|
|
|
|
self.currentMenuModuleInfo = {
|
|
|
|
// :TODO: This is quite the hack... doesn't seem right...
|
|
|
|
menuName : self.currentMenuModule.menuName,
|
|
|
|
extraArgs : options.extraArgs,
|
2015-09-08 03:18:09 +00:00
|
|
|
}
|
2015-03-23 04:52:04 +00:00
|
|
|
|
2015-09-18 05:11:21 +00:00
|
|
|
if(options.savedState) {
|
|
|
|
modInst.restoreSavedState(options.savedState);
|
|
|
|
}
|
|
|
|
|
2015-09-08 03:18:09 +00:00
|
|
|
modInst.enter(self);
|
|
|
|
|
2015-08-05 04:35:59 +00:00
|
|
|
if(!callbackOnErrorOnly) {
|
|
|
|
cb(null);
|
|
|
|
}
|
2015-03-19 05:08:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-09-24 04:24:37 +00:00
|
|
|
Client.prototype.fallbackMenuModule = function(options, cb) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var modOpts;
|
|
|
|
|
2015-09-24 05:41:06 +00:00
|
|
|
if(_.isString(self.currentMenuModule.menuConfig.fallback)) {
|
2015-09-24 04:24:37 +00:00
|
|
|
modOpts = {
|
|
|
|
name : self.currentMenuModule.menuConfig.fallback,
|
|
|
|
extraArgs : options.extraArgs,
|
|
|
|
};
|
|
|
|
} else if(self.lastMenuModuleInfo) {
|
|
|
|
modOpts = {
|
|
|
|
name : self.lastMenuModuleInfo.menuName,
|
|
|
|
extraArgs : self.lastMenuModuleInfo.extraArgs,
|
|
|
|
savedState : self.lastMenuModuleInfo.savedState,
|
|
|
|
};
|
2015-09-24 05:41:06 +00:00
|
|
|
}
|
2015-09-24 04:24:37 +00:00
|
|
|
|
2015-09-24 05:41:06 +00:00
|
|
|
if(modOpts) {
|
2015-09-24 04:24:37 +00:00
|
|
|
self.gotoMenuModule(modOpts, cb);
|
|
|
|
} else {
|
|
|
|
cb(new Error('Nothing to fallback to!'));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2015-04-16 04:46:45 +00:00
|
|
|
Client.prototype.fallbackMenuModule = function(cb) {
|
2015-09-08 03:18:09 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if(self.lastMenuModuleInfo) {
|
|
|
|
var modOpts = {
|
2015-09-18 04:53:19 +00:00
|
|
|
name : self.lastMenuModuleInfo.menuName,
|
2015-09-08 03:18:09 +00:00
|
|
|
extraArgs : self.lastMenuModuleInfo.extraArgs,
|
2015-09-18 05:11:21 +00:00
|
|
|
savedState : self.lastMenuModuleInfo.savedState,
|
2015-09-08 03:18:09 +00:00
|
|
|
};
|
2015-04-16 04:46:45 +00:00
|
|
|
|
2015-09-08 03:18:09 +00:00
|
|
|
self.gotoMenuModule(modOpts, cb);
|
2015-09-18 04:53:19 +00:00
|
|
|
} else {
|
|
|
|
cb(new Error('Nothing to fallback to!'));
|
2015-09-08 03:18:09 +00:00
|
|
|
}
|
2015-04-16 04:46:45 +00:00
|
|
|
};
|
2015-09-24 04:24:37 +00:00
|
|
|
*/
|
2015-04-16 04:46:45 +00:00
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Default error handlers
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-04-04 20:41:04 +00:00
|
|
|
// :TODO: getDefaultHandler(name) -- handlers in default_handlers.js or something
|
2014-10-17 02:21:06 +00:00
|
|
|
Client.prototype.defaultHandlerMissingMod = function(err) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
function handler(err) {
|
2015-07-03 05:28:39 +00:00
|
|
|
self.log.error(err);
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2015-04-02 04:13:29 +00:00
|
|
|
self.term.write(ansi.resetScreen());
|
2014-10-17 02:21:06 +00:00
|
|
|
self.term.write('An unrecoverable error has been encountered!\n');
|
|
|
|
self.term.write('This has been logged for your SysOp to review.\n');
|
|
|
|
self.term.write('\nGoodbye!\n');
|
|
|
|
|
|
|
|
|
|
|
|
//self.term.write(err);
|
|
|
|
|
|
|
|
//if(miscUtil.isDevelopment() && err.stack) {
|
|
|
|
// self.term.write('\n' + err.stack + '\n');
|
|
|
|
//}
|
|
|
|
|
|
|
|
self.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler;
|
|
|
|
};
|
|
|
|
|