2021-04-27 11:30:33 +00:00
|
|
|
import {ServerCommand} from "tc-shared/connection/ConnectionBase";
|
2020-07-23 22:29:36 +00:00
|
|
|
|
|
|
|
function unescapeCommandValue(value: string) : string {
|
|
|
|
let result = "", index = 0, lastIndex = 0;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
index = value.indexOf('\\', lastIndex);
|
2021-03-18 17:25:20 +00:00
|
|
|
if(index === -1 || index >= value.length + 1) {
|
2020-07-23 22:29:36 +00:00
|
|
|
break;
|
2021-03-18 17:25:20 +00:00
|
|
|
}
|
2020-07-23 22:29:36 +00:00
|
|
|
|
|
|
|
let replace;
|
|
|
|
switch (value.charAt(index + 1)) {
|
|
|
|
case 's': replace = ' '; break;
|
|
|
|
case '/': replace = '/'; break;
|
|
|
|
case 'p': replace = '|'; break;
|
|
|
|
case 'b': replace = '\b'; break;
|
|
|
|
case 'f': replace = '\f'; break;
|
|
|
|
case 'n': replace = '\n'; break;
|
|
|
|
case 'r': replace = '\r'; break;
|
|
|
|
case 't': replace = '\t'; break;
|
|
|
|
case 'a': replace = '\x07'; break;
|
|
|
|
case 'v': replace = '\x0B'; break;
|
|
|
|
case '\\': replace = '\\'; break;
|
|
|
|
default:
|
|
|
|
lastIndex = index + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
result += value.substring(lastIndex, index) + replace;
|
|
|
|
lastIndex = index + 2;
|
2020-01-30 23:54:00 +00:00
|
|
|
}
|
2020-07-23 22:29:36 +00:00
|
|
|
|
|
|
|
return result + value.substring(lastIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
const escapeCharacterMap = {
|
|
|
|
"\\": "\\",
|
|
|
|
" ": "s",
|
|
|
|
"/": "/",
|
|
|
|
"|": "p",
|
|
|
|
"\b": "b",
|
|
|
|
"\f": "f",
|
|
|
|
"\n": "n",
|
|
|
|
"\r": "r",
|
|
|
|
"\t": "t",
|
|
|
|
"\x07": "a",
|
|
|
|
"\x0B": "b"
|
|
|
|
};
|
|
|
|
|
2020-08-10 12:41:34 +00:00
|
|
|
const escapeCommandValue = (value: string) => value.replace(/[\\ \/|\b\f\n\r\t\x07]/g, value => "\\" + escapeCharacterMap[value]);
|
2020-07-23 22:29:36 +00:00
|
|
|
|
2021-04-27 11:30:33 +00:00
|
|
|
export function parseCommand(command: string): ServerCommand {
|
2020-07-23 22:29:36 +00:00
|
|
|
const parts = command.split("|").map(element => element.split(" ").map(e => e.trim()).filter(e => !!e));
|
|
|
|
|
|
|
|
let cmd;
|
2021-03-18 17:25:20 +00:00
|
|
|
if(parts[0][0].indexOf("=") === -1) {
|
2020-07-23 22:29:36 +00:00
|
|
|
cmd = parts[0].pop_front();
|
2021-03-18 17:25:20 +00:00
|
|
|
}
|
2020-07-23 22:29:36 +00:00
|
|
|
|
|
|
|
let switches = [];
|
|
|
|
let payloads = [];
|
|
|
|
parts.forEach(element => {
|
|
|
|
const payload = {};
|
|
|
|
for(const keyValue of element) {
|
|
|
|
if(keyValue[0] === '-') {
|
|
|
|
switches.push(keyValue.substring(1));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const separator = keyValue.indexOf('=');
|
2021-03-18 17:25:20 +00:00
|
|
|
if(separator === -1) {
|
2020-09-03 10:14:15 +00:00
|
|
|
payload[keyValue] = "";
|
2021-03-18 17:25:20 +00:00
|
|
|
} else {
|
2020-07-23 22:29:36 +00:00
|
|
|
payload[keyValue.substring(0, separator)] = unescapeCommandValue(keyValue.substring(separator + 1));
|
2021-03-18 17:25:20 +00:00
|
|
|
}
|
2020-07-23 22:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
payloads.push(payload)
|
|
|
|
});
|
|
|
|
|
2021-04-27 11:30:33 +00:00
|
|
|
return new ServerCommand(cmd, payloads, switches);
|
2020-07-23 22:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function buildCommand(data: any | any[], switches?: string[], command?: string) {
|
|
|
|
let result = "";
|
|
|
|
|
|
|
|
for(const payload of Array.isArray(data) ? data : [data]) {
|
|
|
|
result += " |";
|
|
|
|
for(const key of Object.keys(payload)) {
|
|
|
|
result += " " + key;
|
2021-03-18 17:25:20 +00:00
|
|
|
if(payload[key] !== undefined && payload[key] !== null) {
|
2020-07-23 22:29:36 +00:00
|
|
|
result += " " + key + "=" + escapeCommandValue(payload[key].toString());
|
2021-03-18 17:25:20 +00:00
|
|
|
}
|
2020-07-23 22:29:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 17:25:20 +00:00
|
|
|
if(switches?.length) {
|
2020-07-23 22:29:36 +00:00
|
|
|
result += " " + switches.map(e => "-" + e).join(" ");
|
2021-03-18 17:25:20 +00:00
|
|
|
}
|
2020-07-23 22:29:36 +00:00
|
|
|
|
|
|
|
return command ? command + result.substring(2) : result.substring(3);
|
2020-01-30 23:54:00 +00:00
|
|
|
}
|