2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2015-06-04 23:06:37 +00:00
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
Portions of this code for key handling heavily inspired from the following:
|
|
|
|
https://github.com/chjj/blessed/blob/master/lib/keys.js
|
|
|
|
|
|
|
|
chji's blessed is MIT licensed:
|
|
|
|
|
|
|
|
----/snip/----------------------
|
|
|
|
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.
|
|
|
|
----/snip/----------------------
|
2015-06-04 23:06:37 +00:00
|
|
|
*/
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const term = require('./client_term.js');
|
|
|
|
const ansi = require('./ansi_term.js');
|
|
|
|
const User = require('./user.js');
|
|
|
|
const Config = require('./config.js').get;
|
|
|
|
const MenuStack = require('./menu_stack.js');
|
|
|
|
const ACS = require('./acs.js');
|
|
|
|
const Events = require('./events.js');
|
|
|
|
const UserInterruptQueue = require('./user_interrupt_queue.js');
|
|
|
|
const UserProps = require('./user_property.js');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const stream = require('stream');
|
|
|
|
const assert = require('assert');
|
|
|
|
const _ = require('lodash');
|
2015-06-05 04:29:14 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.Client = Client;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Move all of the key stuff to it's own module
|
2015-04-27 22:04:41 +00:00
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Resources & Standards:
|
|
|
|
// * http://www.ansi-bbs.org/ansi-bbs-core-server.html
|
2015-04-27 22:04:41 +00:00
|
|
|
//
|
2018-12-09 09:33:48 +00:00
|
|
|
/* eslint-disable no-control-regex */
|
2022-06-05 20:04:25 +00:00
|
|
|
const RE_DSR_RESPONSE_ANYWHERE = /(?:\u001b\[)([0-9;]+)(R)/;
|
2018-06-23 03:26:46 +00:00
|
|
|
const RE_DEV_ATTR_RESPONSE_ANYWHERE = /(?:\u001b\[)[=?]([0-9a-zA-Z;]+)(c)/;
|
2022-06-05 20:04:25 +00:00
|
|
|
const RE_META_KEYCODE_ANYWHERE = /(?:\u001b)([a-zA-Z0-9])/;
|
|
|
|
const RE_META_KEYCODE = new RegExp('^' + RE_META_KEYCODE_ANYWHERE.source + '$');
|
|
|
|
const RE_FUNCTION_KEYCODE_ANYWHERE = new RegExp(
|
|
|
|
'(?:\u001b+)(O|N|\\[|\\[\\[)(?:' +
|
|
|
|
[
|
|
|
|
'(\\d+)(?:;(\\d+))?([~^$])',
|
|
|
|
'(?:M([@ #!a`])(.)(.))', // mouse stuff
|
|
|
|
'(?:1;)?(\\d+)?([a-zA-Z@])',
|
|
|
|
].join('|') +
|
|
|
|
')'
|
|
|
|
);
|
2018-12-09 09:33:48 +00:00
|
|
|
/* eslint-enable no-control-regex */
|
2015-06-05 04:29:14 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const RE_FUNCTION_KEYCODE = new RegExp('^' + RE_FUNCTION_KEYCODE_ANYWHERE.source);
|
|
|
|
const RE_ESC_CODE_ANYWHERE = new RegExp(
|
|
|
|
[
|
|
|
|
RE_FUNCTION_KEYCODE_ANYWHERE.source,
|
|
|
|
RE_META_KEYCODE_ANYWHERE.source,
|
|
|
|
RE_DSR_RESPONSE_ANYWHERE.source,
|
|
|
|
RE_DEV_ATTR_RESPONSE_ANYWHERE.source,
|
|
|
|
/\u001b./.source, // eslint-disable-line no-control-regex
|
|
|
|
].join('|')
|
|
|
|
);
|
2015-06-04 23:06:37 +00:00
|
|
|
|
2018-01-15 19:22:11 +00:00
|
|
|
function Client(/*input, output*/) {
|
2018-06-22 05:15:04 +00:00
|
|
|
stream.call(this);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const self = this;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.user = new User();
|
|
|
|
this.currentThemeConfig = { info: { name: 'N/A', description: 'None' } };
|
|
|
|
this.lastActivityTime = Date.now();
|
|
|
|
this.menuStack = new MenuStack(this);
|
|
|
|
this.acs = new ACS({ client: this, user: this.user });
|
|
|
|
this.interruptQueue = new UserInterruptQueue(this);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
Object.defineProperty(this, 'currentTheme', {
|
2022-06-05 20:04:25 +00:00
|
|
|
get: () => {
|
2020-06-17 06:10:51 +00:00
|
|
|
if (this.currentThemeConfig) {
|
2022-08-04 16:52:43 +00:00
|
|
|
// :TODO: clean this up: We have a ugly transition state in which we have a pure raw config vs a ConfigLoader in which get() must be called
|
|
|
|
try {
|
|
|
|
return this.currentThemeConfig.get();
|
2022-08-04 17:32:09 +00:00
|
|
|
} catch (e) {
|
2022-08-04 16:52:43 +00:00
|
|
|
return this.currentThemeConfig;
|
|
|
|
}
|
2020-06-17 06:10:51 +00:00
|
|
|
} else {
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
info: {
|
|
|
|
name: 'N/A',
|
|
|
|
author: 'N/A',
|
|
|
|
description: 'N/A',
|
|
|
|
group: 'N/A',
|
|
|
|
},
|
2020-06-17 06:10:51 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
set: theme => {
|
2020-06-17 06:10:51 +00:00
|
|
|
this.currentThemeConfig = theme;
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2020-06-17 06:10:51 +00:00
|
|
|
});
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
Object.defineProperty(this, 'node', {
|
2022-06-05 20:04:25 +00:00
|
|
|
get: function () {
|
2020-05-14 01:30:57 +00:00
|
|
|
return self.session.id;
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'currentMenuModule', {
|
2022-06-05 20:04:25 +00:00
|
|
|
get: function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return self.menuStack.currentModule;
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.setTemporaryDirectDataHandler = function (handler) {
|
|
|
|
this.dataPassthrough = true; // let implementations do with what they will here
|
2018-06-22 05:15:04 +00:00
|
|
|
this.input.removeAllListeners('data');
|
|
|
|
this.input.on('data', handler);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.restoreDataHandler = function () {
|
2020-05-21 02:24:12 +00:00
|
|
|
this.dataPassthrough = false;
|
2018-06-22 05:15:04 +00:00
|
|
|
this.input.removeAllListeners('data');
|
|
|
|
this.input.on('data', this.dataHandler);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.themeChangedListener = function ({ themeId }) {
|
|
|
|
if (_.get(self.currentTheme, 'info.themeId') === themeId) {
|
|
|
|
self.currentThemeConfig = require('./theme.js')
|
|
|
|
.getAvailableThemes()
|
|
|
|
.get(themeId);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-07-16 04:08:09 +00:00
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-07-16 04:08:09 +00:00
|
|
|
Events.on(Events.getSystemEvents().ThemeChanged, this.themeChangedListener);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Peek at incoming |data| and emit events for any special
|
|
|
|
// handling that may include:
|
|
|
|
// * Keyboard input
|
|
|
|
// * ANSI CSR's and the like
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// References:
|
|
|
|
// * http://www.ansi-bbs.org/ansi-bbs-core-server.html
|
|
|
|
// * Christopher Jeffrey's Blessed library @ https://github.com/chjj/blessed/
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getTermClient = function (deviceAttr) {
|
2018-06-22 05:15:04 +00:00
|
|
|
let termClient = {
|
2022-06-05 20:04:25 +00:00
|
|
|
'63;1;2': 'arctel', // http://www.fbl.cz/arctel/download/techman.pdf - Irssi ConnectBot (Android)
|
|
|
|
'50;86;84;88': 'vtx', // https://github.com/codewar65/VTX_ClientServer/blob/master/vtx.txt
|
2018-06-22 05:15:04 +00:00
|
|
|
}[deviceAttr];
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!termClient) {
|
|
|
|
if (_.startsWith(deviceAttr, '67;84;101;114;109')) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// See https://github.com/protomouse/synchronet/blob/master/src/conio/cterm.txt
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Known clients:
|
|
|
|
// * SyncTERM
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
termClient = 'cterm';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return termClient;
|
|
|
|
};
|
|
|
|
|
2018-12-09 09:33:48 +00:00
|
|
|
/* eslint-disable no-control-regex */
|
2022-06-05 20:04:25 +00:00
|
|
|
this.isMouseInput = function (data) {
|
|
|
|
return (
|
|
|
|
/\x1b\[M/.test(data) ||
|
2018-12-09 09:33:48 +00:00
|
|
|
/\u001b\[M([\x00\u0020-\uffff]{3})/.test(data) ||
|
2018-06-23 03:26:46 +00:00
|
|
|
/\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) ||
|
2022-06-05 20:04:25 +00:00
|
|
|
/\u001b\[(O|I)/.test(data)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
2018-12-09 09:33:48 +00:00
|
|
|
/* eslint-enable no-control-regex */
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getKeyComponentsFromCode = function (code) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return {
|
2018-06-23 03:26:46 +00:00
|
|
|
// xterm/gnome
|
2022-06-05 20:04:25 +00:00
|
|
|
OP: { name: 'f1' },
|
|
|
|
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' },
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// xterm/rxvt
|
2022-06-05 20:04:25 +00:00
|
|
|
'[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' },
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// Cygwin & libuv
|
2022-06-05 20:04:25 +00:00
|
|
|
'[[A': { name: 'f1' },
|
|
|
|
'[[B': { name: 'f2' },
|
|
|
|
'[[C': { name: 'f3' },
|
|
|
|
'[[D': { name: 'f4' },
|
|
|
|
'[[E': { name: 'f5' },
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// Common impls
|
2022-06-05 20:04:25 +00:00
|
|
|
'[15~': { name: 'f5' },
|
|
|
|
'[17~': { name: 'f6' },
|
|
|
|
'[18~': { name: 'f7' },
|
|
|
|
'[19~': { name: 'f8' },
|
|
|
|
'[20~': { name: 'f9' },
|
|
|
|
'[21~': { name: 'f10' },
|
|
|
|
'[23~': { name: 'f11' },
|
|
|
|
'[24~': { name: 'f12' },
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// xterm
|
2022-06-05 20:04:25 +00:00
|
|
|
'[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' },
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// PuTTY
|
2022-06-05 20:04:25 +00:00
|
|
|
'[[5~': { name: 'page up' },
|
|
|
|
'[[6~': { name: 'page down' },
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// rvxt
|
2022-06-05 20:04:25 +00:00
|
|
|
'[7~': { name: 'home' },
|
|
|
|
'[8~': { name: 'end' },
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// rxvt with modifiers
|
2022-06-05 20:04:25 +00:00
|
|
|
'[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 },
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// SyncTERM / EtherTerm
|
2022-06-05 20:04:25 +00:00
|
|
|
'[K': { name: 'end' },
|
|
|
|
'[@': { name: 'insert' },
|
|
|
|
'[V': { name: 'page up' },
|
|
|
|
'[U': { name: 'page down' },
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// other
|
2022-06-05 20:04:25 +00:00
|
|
|
'[Z': { name: 'tab', shift: true },
|
2018-06-22 05:15:04 +00:00
|
|
|
}[code];
|
|
|
|
};
|
|
|
|
|
|
|
|
this.on('data', function clientData(data) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// create a uniform format that can be parsed below
|
2022-06-05 20:04:25 +00:00
|
|
|
if (data[0] > 127 && undefined === data[1]) {
|
2018-06-22 05:15:04 +00:00
|
|
|
data[0] -= 128;
|
|
|
|
data = '\u001b' + data.toString('utf-8');
|
|
|
|
} else {
|
|
|
|
data = data.toString('utf-8');
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.isMouseInput(data)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf = [];
|
|
|
|
var m;
|
2022-06-05 20:04:25 +00:00
|
|
|
while ((m = RE_ESC_CODE_ANYWHERE.exec(data))) {
|
2018-06-22 05:15:04 +00:00
|
|
|
buf = buf.concat(data.slice(0, m.index).split(''));
|
|
|
|
buf.push(m[0]);
|
|
|
|
data = data.slice(m.index + m[0].length);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
buf = buf.concat(data.split('')); // remainder
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
buf.forEach(function bufPart(s) {
|
|
|
|
var key = {
|
2022-06-05 20:04:25 +00:00
|
|
|
seq: s,
|
|
|
|
name: undefined,
|
|
|
|
ctrl: false,
|
|
|
|
meta: false,
|
|
|
|
shift: false,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var parts;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if ((parts = RE_DSR_RESPONSE_ANYWHERE.exec(s))) {
|
|
|
|
if ('R' === parts[2]) {
|
|
|
|
const cprArgs = parts[1].split(';').map(v => parseInt(v, 10) || 0);
|
|
|
|
if (2 === cprArgs.length) {
|
|
|
|
if (self.cprOffset) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cprArgs[0] = cprArgs[0] + self.cprOffset;
|
|
|
|
cprArgs[1] = cprArgs[1] + self.cprOffset;
|
|
|
|
}
|
|
|
|
self.emit('cursor position report', cprArgs);
|
|
|
|
}
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if ((parts = RE_DEV_ATTR_RESPONSE_ANYWHERE.exec(s))) {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert('c' === parts[2]);
|
|
|
|
var termClient = self.getTermClient(parts[1]);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (termClient) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.term.termClient = termClient;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if ('\r' === s) {
|
2018-06-22 05:15:04 +00:00
|
|
|
key.name = 'return';
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if ('\n' === s) {
|
2018-06-22 05:15:04 +00:00
|
|
|
key.name = 'line feed';
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if ('\t' === s) {
|
2018-06-22 05:15:04 +00:00
|
|
|
key.name = 'tab';
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if ('\x7f' === s) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Backspace vs delete is a crazy thing, especially in *nix.
|
|
|
|
// - ANSI-BBS uses 0x7f for DEL
|
|
|
|
// - xterm et. al clients send 0x7f for backspace... ugg.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// See http://www.hypexr.org/linux_ruboff.php
|
|
|
|
// And a great discussion @ https://lists.debian.org/debian-i18n/1998/04/msg00015.html
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.term.isNixTerm()) {
|
|
|
|
key.name = 'backspace';
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
key.name = 'delete';
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
} else if ('\b' === s || '\x1b\x7f' === s || '\x1b\b' === s) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// backspace, CTRL-H
|
2022-06-05 20:04:25 +00:00
|
|
|
key.name = 'backspace';
|
|
|
|
key.meta = '\x1b' === s.charAt(0);
|
|
|
|
} else if ('\x1b' === s || '\x1b\x1b' === s) {
|
|
|
|
key.name = 'escape';
|
|
|
|
key.meta = 2 === s.length;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else if (' ' === s || '\x1b ' === s) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// rather annoying that space can come in other than just " "
|
2022-06-05 20:04:25 +00:00
|
|
|
key.name = 'space';
|
|
|
|
key.meta = 2 === s.length;
|
|
|
|
} else if (1 === s.length && s <= '\x1a') {
|
2018-06-23 03:26:46 +00:00
|
|
|
// CTRL-<letter>
|
2022-06-05 20:04:25 +00:00
|
|
|
key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1);
|
|
|
|
key.ctrl = true;
|
|
|
|
} else if (1 === s.length && s >= 'a' && s <= 'z') {
|
2018-06-23 03:26:46 +00:00
|
|
|
// normal, lowercased letter
|
2022-06-05 20:04:25 +00:00
|
|
|
key.name = s;
|
|
|
|
} else if (1 === s.length && s >= 'A' && s <= 'Z') {
|
|
|
|
key.name = s.toLowerCase();
|
|
|
|
key.shift = true;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else if ((parts = RE_META_KEYCODE.exec(s))) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// meta with character key
|
2022-06-05 20:04:25 +00:00
|
|
|
key.name = parts[1].toLowerCase();
|
|
|
|
key.meta = true;
|
|
|
|
key.shift = /^[A-Z]$/.test(parts[1]);
|
|
|
|
} else if ((parts = RE_FUNCTION_KEYCODE.exec(s))) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var code =
|
2022-06-05 20:04:25 +00:00
|
|
|
(parts[1] || '') +
|
|
|
|
(parts[2] || '') +
|
|
|
|
(parts[4] || '') +
|
|
|
|
(parts[9] || '');
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
var modifier = (parts[3] || parts[8] || 1) - 1;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
key.ctrl = !!(modifier & 4);
|
|
|
|
key.meta = !!(modifier & 10);
|
|
|
|
key.shift = !!(modifier & 1);
|
|
|
|
key.code = code;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
_.assign(key, self.getKeyComponentsFromCode(code));
|
|
|
|
}
|
|
|
|
|
|
|
|
var ch;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (1 === s.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
ch = s;
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if ('space' === key.name) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// stupid hack to always get space as a regular char
|
2018-06-22 05:15:04 +00:00
|
|
|
ch = ' ';
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isUndefined(key.name)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
key = undefined;
|
|
|
|
} else {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Adjust name for CTRL/Shift/Meta modifiers
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
key.name =
|
2018-06-23 03:26:46 +00:00
|
|
|
(key.ctrl ? 'ctrl + ' : '') +
|
|
|
|
(key.meta ? 'meta + ' : '') +
|
|
|
|
(key.shift ? 'shift + ' : '') +
|
|
|
|
key.name;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2015-06-04 23:06:37 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (key || ch) {
|
|
|
|
if (Config().logging.traceUserKeyboardInput) {
|
|
|
|
self.log.trace({ key: key, ch: escape(ch) }, 'User keyboard input'); // jshint ignore:line
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2015-06-06 06:33:59 +00:00
|
|
|
|
2020-05-06 23:47:13 +00:00
|
|
|
self.lastActivityTime = Date.now();
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!self.ignoreInput) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.emit('key press', ch, key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require('util').inherits(Client, stream);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Client.prototype.setInputOutput = function (input, output) {
|
|
|
|
this.input = input;
|
2018-06-23 03:26:46 +00:00
|
|
|
this.output = output;
|
2015-10-20 21:39:33 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.term = new term.ClientTerminal(this.output);
|
2015-10-20 21:39:33 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Client.prototype.setTermType = function (termType) {
|
|
|
|
this.term.env.TERM = termType;
|
|
|
|
this.term.termType = termType;
|
2015-10-21 22:30:32 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.log.debug({ termType: termType }, 'Set terminal type');
|
2015-10-21 22:30:32 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Client.prototype.startIdleMonitor = function () {
|
2019-06-09 19:41:11 +00:00
|
|
|
// clear existing, if any
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.idleCheck) {
|
2019-06-09 19:41:11 +00:00
|
|
|
this.stopIdleMonitor();
|
|
|
|
}
|
|
|
|
|
2020-05-06 23:47:13 +00:00
|
|
|
this.lastActivityTime = Date.now();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Every 1m, check for idle.
|
2019-01-11 03:34:52 +00:00
|
|
|
// We also update minutes spent online the system here,
|
|
|
|
// if we have a authenticated user.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
this.idleCheck = setInterval(() => {
|
2018-06-23 03:26:46 +00:00
|
|
|
const nowMs = Date.now();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2019-01-11 03:34:52 +00:00
|
|
|
let idleLogoutSeconds;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.user.isAuthenticated()) {
|
2019-01-11 03:34:52 +00:00
|
|
|
idleLogoutSeconds = Config().users.idleLogoutSeconds;
|
|
|
|
|
|
|
|
//
|
|
|
|
// We don't really want to be firing off an event every 1m for
|
|
|
|
// every user, but want at least some updates for various things
|
|
|
|
// such as achievements. Send off every 5m.
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
const minOnline = this.user.incrementProperty(
|
|
|
|
UserProps.MinutesOnlineTotalCount,
|
|
|
|
1
|
|
|
|
);
|
|
|
|
if (0 === minOnline % 5) {
|
|
|
|
Events.emit(Events.getSystemEvents().UserStatIncrement, {
|
|
|
|
user: this.user,
|
|
|
|
statName: UserProps.MinutesOnlineTotalCount,
|
|
|
|
statIncrementBy: 1,
|
|
|
|
statValue: minOnline,
|
|
|
|
});
|
2019-01-11 03:34:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
idleLogoutSeconds = Config().users.preAuthIdleLogoutSeconds;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2019-06-09 19:41:11 +00:00
|
|
|
// use override value if set
|
|
|
|
idleLogoutSeconds = this.idleLogoutSecondsOverride || idleLogoutSeconds;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
idleLogoutSeconds > 0 &&
|
|
|
|
nowMs - this.lastActivityTime >= idleLogoutSeconds * 1000
|
|
|
|
) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.emit('idle timeout');
|
|
|
|
}
|
|
|
|
}, 1000 * 60);
|
2015-10-21 22:30:32 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Client.prototype.stopIdleMonitor = function () {
|
|
|
|
if (this.idleCheck) {
|
2019-06-09 19:41:11 +00:00
|
|
|
clearInterval(this.idleCheck);
|
|
|
|
delete this.idleCheck;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Client.prototype.explicitActivityTimeUpdate = function () {
|
2020-05-06 23:47:13 +00:00
|
|
|
this.lastActivityTime = Date.now();
|
2020-06-16 01:08:55 +00:00
|
|
|
};
|
2020-05-06 23:47:13 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Client.prototype.overrideIdleLogoutSeconds = function (seconds) {
|
2019-06-09 19:41:11 +00:00
|
|
|
this.idleLogoutSecondsOverride = seconds;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Client.prototype.restoreIdleLogoutSeconds = function () {
|
2019-06-09 19:41:11 +00:00
|
|
|
delete this.idleLogoutSecondsOverride;
|
2018-03-04 16:17:27 +00:00
|
|
|
};
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
Client.prototype.end = function () {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.term) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.term.disconnect();
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Events.removeListener(
|
|
|
|
Events.getSystemEvents().ThemeChanged,
|
|
|
|
this.themeChangedListener
|
|
|
|
);
|
2018-07-16 04:08:09 +00:00
|
|
|
|
|
|
|
const currentModule = this.menuStack.getCurrentModule;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (currentModule) {
|
2018-06-22 05:15:04 +00:00
|
|
|
currentModule.leave();
|
|
|
|
}
|
|
|
|
|
2019-01-11 03:34:52 +00:00
|
|
|
// persist time online for authenticated users
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.user.isAuthenticated()) {
|
2019-01-11 03:34:52 +00:00
|
|
|
this.user.persistProperty(
|
|
|
|
UserProps.MinutesOnlineTotalCount,
|
|
|
|
this.user.getProperty(UserProps.MinutesOnlineTotalCount)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.stopIdleMonitor();
|
|
|
|
|
|
|
|
try {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// We can end up calling 'end' before TTY/etc. is established, e.g. with SSH
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isFunction(this.disconnect)) {
|
2019-02-02 17:20:22 +00:00
|
|
|
return this.disconnect();
|
|
|
|
} else {
|
|
|
|
// legacy fallback
|
|
|
|
return this.output.end.apply(this.output, arguments);
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
2018-07-16 04:08:09 +00:00
|
|
|
// ie TypeError
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.destroy = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.output.destroy.apply(this.output, arguments);
|
2014-10-17 02:21:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.destroySoon = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.output.destroySoon.apply(this.output, arguments);
|
2014-10-17 02:21:06 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Client.prototype.waitForKeyPress = function (cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.once('key press', function kp(ch, key) {
|
|
|
|
cb(ch, key);
|
|
|
|
});
|
2014-10-17 02:21:06 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Client.prototype.isLocal = function () {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Handle ipv6 better
|
2022-06-05 20:04:25 +00:00
|
|
|
return ['127.0.0.1', '::ffff:127.0.0.1'].includes(this.remoteAddress);
|
2015-11-12 07:31:25 +00:00
|
|
|
};
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
Client.prototype.friendlyRemoteAddress = function () {
|
2022-06-04 23:39:48 +00:00
|
|
|
// convert any :ffff: IPv4's to 32bit version
|
2022-06-12 20:12:03 +00:00
|
|
|
return this.remoteAddress.replace(/^::ffff:/, '').replace(/^::1$/, 'localhost');
|
|
|
|
};
|
2022-06-04 23:39:48 +00:00
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-06-23 03:26:46 +00:00
|
|
|
// Default error handlers
|
2014-10-17 02:21:06 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: getDefaultHandler(name) -- handlers in default_handlers.js or something
|
2022-06-05 20:04:25 +00:00
|
|
|
Client.prototype.defaultHandlerMissingMod = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
var self = this;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
function handler(err) {
|
|
|
|
self.log.error(err);
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
self.term.write(ansi.resetScreen());
|
|
|
|
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');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
//self.term.write(err);
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
//if(miscUtil.isDevelopment() && err.stack) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// self.term.write('\n' + err.stack + '\n');
|
2018-06-22 05:15:04 +00:00
|
|
|
//}
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
self.end();
|
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return handler;
|
2014-10-17 02:21:06 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Client.prototype.terminalSupports = function (query) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const termClient = this.term.termClient;
|
2017-09-22 03:23:30 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
switch (query) {
|
|
|
|
case 'vtx_audio':
|
2018-06-23 03:26:46 +00:00
|
|
|
// https://github.com/codewar65/VTX_ClientServer/blob/master/vtx.txt
|
2018-06-22 05:15:04 +00:00
|
|
|
return 'vtx' === termClient;
|
2017-09-22 03:23:30 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'vtx_hyperlink':
|
2018-06-22 05:15:04 +00:00
|
|
|
return 'vtx' === termClient;
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
default:
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-09-02 22:23:34 +00:00
|
|
|
};
|