2019-04-20 07:27:33 +02:00
|
|
|
import {
|
|
|
|
evaluateDirectoryPath,
|
|
|
|
getAllParentDirectories,
|
|
|
|
} from "./DirectoryHelpers";
|
2019-05-12 04:20:20 +02:00
|
|
|
import { getSubdirectories } from "./DirectoryServerHelpers";
|
2019-04-20 07:27:33 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
import {
|
|
|
|
Aliases,
|
2021-04-30 05:52:56 +02:00
|
|
|
GlobalAliases,
|
2019-04-10 08:07:12 +02:00
|
|
|
} from "../Alias";
|
|
|
|
import { DarkWebItems } from "../DarkWeb/DarkWebItems";
|
|
|
|
import { Message } from "../Message/Message";
|
|
|
|
import { IPlayer } from "../PersonObjects/IPlayer"
|
|
|
|
import { AllServers } from "../Server/AllServers";
|
|
|
|
|
|
|
|
// An array of all Terminal commands
|
|
|
|
const commands = [
|
|
|
|
"alias",
|
|
|
|
"analyze",
|
2021-04-29 02:07:26 +02:00
|
|
|
"backdoor",
|
2019-04-10 08:07:12 +02:00
|
|
|
"cat",
|
|
|
|
"cd",
|
|
|
|
"check",
|
|
|
|
"clear",
|
|
|
|
"cls",
|
|
|
|
"connect",
|
|
|
|
"download",
|
|
|
|
"expr",
|
|
|
|
"free",
|
|
|
|
"hack",
|
|
|
|
"help",
|
|
|
|
"home",
|
|
|
|
"hostname",
|
|
|
|
"ifconfig",
|
|
|
|
"kill",
|
|
|
|
"killall",
|
|
|
|
"ls",
|
|
|
|
"lscpu",
|
|
|
|
"mem",
|
|
|
|
"mv",
|
|
|
|
"nano",
|
|
|
|
"ps",
|
|
|
|
"rm",
|
|
|
|
"run",
|
|
|
|
"scan",
|
|
|
|
"scan-analyze",
|
|
|
|
"scp",
|
|
|
|
"sudov",
|
|
|
|
"tail",
|
|
|
|
"theme",
|
2021-04-30 05:52:56 +02:00
|
|
|
"top",
|
2019-04-10 08:07:12 +02:00
|
|
|
];
|
2019-04-05 11:08:41 +02:00
|
|
|
|
2021-04-30 05:52:56 +02:00
|
|
|
export function determineAllPossibilitiesForTabCompletion(p: IPlayer, input: string, index: number, currPath=""): string[] {
|
2019-04-05 11:08:41 +02:00
|
|
|
let allPos: string[] = [];
|
|
|
|
allPos = allPos.concat(Object.keys(GlobalAliases));
|
|
|
|
const currServ = p.getCurrentServer();
|
|
|
|
const homeComputer = p.getHomeComputer();
|
2019-04-20 07:27:33 +02:00
|
|
|
|
2021-04-30 05:52:56 +02:00
|
|
|
let parentDirPath = "";
|
2019-04-20 07:27:33 +02:00
|
|
|
let evaledParentDirPath: string | null = null;
|
2019-04-05 11:08:41 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
// Helper functions
|
2021-05-01 09:17:31 +02:00
|
|
|
function addAllCodingContracts(): void {
|
2019-04-10 08:07:12 +02:00
|
|
|
for (const cct of currServ.contracts) {
|
|
|
|
allPos.push(cct.fn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
function addAllLitFiles(): void {
|
2019-04-10 08:07:12 +02:00
|
|
|
for (const file of currServ.messages) {
|
|
|
|
if (!(file instanceof Message)) {
|
|
|
|
allPos.push(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
function addAllMessages(): void {
|
2019-04-10 08:07:12 +02:00
|
|
|
for (const file of currServ.messages) {
|
|
|
|
if (file instanceof Message) {
|
|
|
|
allPos.push(file.filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
function addAllPrograms(): void {
|
2019-04-13 01:52:59 +02:00
|
|
|
for (const program of homeComputer.programs) {
|
2019-04-10 08:07:12 +02:00
|
|
|
allPos.push(program);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
function addAllScripts(): void {
|
2019-04-10 08:07:12 +02:00
|
|
|
for (const script of currServ.scripts) {
|
2019-04-20 07:27:33 +02:00
|
|
|
const res = processFilepath(script.filename);
|
|
|
|
if (res) {
|
|
|
|
allPos.push(res);
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
function addAllTextFiles(): void {
|
2019-04-10 08:07:12 +02:00
|
|
|
for (const txt of currServ.textFiles) {
|
2019-04-20 07:27:33 +02:00
|
|
|
const res = processFilepath(txt.fn);
|
|
|
|
if (res) {
|
|
|
|
allPos.push(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
function addAllDirectories(): void {
|
2019-04-20 07:27:33 +02:00
|
|
|
// Directories are based on the currently evaluated path
|
|
|
|
const subdirs = getSubdirectories(currServ, evaledParentDirPath == null ? "/" : evaledParentDirPath);
|
|
|
|
|
|
|
|
for (let i = 0; i < subdirs.length; ++i) {
|
|
|
|
const assembledDirPath = (evaledParentDirPath == null ? subdirs[i] : evaledParentDirPath + subdirs[i]);
|
|
|
|
const res = processFilepath(assembledDirPath);
|
|
|
|
if (res != null) {
|
|
|
|
subdirs[i] = res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
allPos = allPos.concat(subdirs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert from the real absolute path back to the original path used in the input
|
|
|
|
function convertParentPath(filepath: string): string {
|
|
|
|
if (parentDirPath == null || evaledParentDirPath == null) {
|
|
|
|
console.warn(`convertParentPath() called when paths are null`);
|
|
|
|
return filepath;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!filepath.startsWith(evaledParentDirPath)) {
|
|
|
|
console.warn(`convertParentPath() called for invalid path. (filepath=${filepath}) (evaledParentDirPath=${evaledParentDirPath})`);
|
|
|
|
return filepath;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
2019-04-20 07:27:33 +02:00
|
|
|
|
|
|
|
return parentDirPath + filepath.slice(evaledParentDirPath.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given an a full, absolute filepath, converts it to the proper value
|
|
|
|
// for autocompletion purposes
|
|
|
|
function processFilepath(filepath: string): string | null {
|
|
|
|
if (evaledParentDirPath) {
|
|
|
|
if (filepath.startsWith(evaledParentDirPath)) {
|
|
|
|
return convertParentPath(filepath);
|
|
|
|
}
|
|
|
|
} else if (parentDirPath !== "") {
|
|
|
|
// If the parent directory is the root directory, but we're not searching
|
|
|
|
// it from the root directory, we have to add the original path
|
|
|
|
let t_parentDirPath = parentDirPath;
|
|
|
|
if (!t_parentDirPath.endsWith("/")) { t_parentDirPath += "/"; }
|
|
|
|
return parentDirPath + filepath;
|
|
|
|
} else {
|
|
|
|
return filepath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
function isCommand(cmd: string): boolean {
|
2019-04-10 08:07:12 +02:00
|
|
|
let t_cmd = cmd;
|
|
|
|
if (!t_cmd.endsWith(" ")) {
|
|
|
|
t_cmd += " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return input.startsWith(t_cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the command starts with './' and the index == -1, then the user
|
|
|
|
* has input ./partialexecutablename so autocomplete the script or program.
|
|
|
|
* Put './' in front of each script/executable
|
|
|
|
*/
|
|
|
|
if (isCommand("./") && index == -1) {
|
2019-04-05 11:08:41 +02:00
|
|
|
//All programs and scripts
|
2021-05-01 09:17:31 +02:00
|
|
|
for (let i = 0; i < currServ.scripts.length; ++i) {
|
2019-04-05 11:08:41 +02:00
|
|
|
allPos.push("./" + currServ.scripts[i].filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Programs are on home computer
|
2021-05-01 09:17:31 +02:00
|
|
|
for(let i = 0; i < homeComputer.programs.length; ++i) {
|
2019-04-05 11:08:41 +02:00
|
|
|
allPos.push("./" + homeComputer.programs[i]);
|
|
|
|
}
|
|
|
|
return allPos;
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
// Autocomplete the command
|
2019-04-20 07:27:33 +02:00
|
|
|
if (index === -1) {
|
2019-04-10 08:07:12 +02:00
|
|
|
return commands.concat(Object.keys(Aliases)).concat(Object.keys(GlobalAliases));
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
|
|
|
|
2019-04-20 07:27:33 +02:00
|
|
|
// Since we're autocompleting an argument and not a command, the argument might
|
|
|
|
// be a file/directory path. We have to account for that when autocompleting
|
|
|
|
const commandArray = input.split(" ");
|
|
|
|
if (commandArray.length === 0) {
|
|
|
|
console.warn(`Tab autocompletion logic reached invalid branch`);
|
|
|
|
return allPos;
|
|
|
|
}
|
|
|
|
const arg = commandArray[commandArray.length - 1];
|
|
|
|
parentDirPath = getAllParentDirectories(arg);
|
|
|
|
evaledParentDirPath = evaluateDirectoryPath(parentDirPath, currPath);
|
|
|
|
if (evaledParentDirPath === "/") {
|
|
|
|
evaledParentDirPath = null;
|
|
|
|
} else if (evaledParentDirPath == null) {
|
|
|
|
return allPos; // Invalid path
|
|
|
|
} else {
|
|
|
|
evaledParentDirPath += "/";
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
if (isCommand("buy")) {
|
2021-04-30 05:52:56 +02:00
|
|
|
const options = [];
|
2019-04-05 11:08:41 +02:00
|
|
|
for (const i in DarkWebItems) {
|
|
|
|
const item = DarkWebItems[i]
|
|
|
|
options.push(item.program);
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
return options.concat(Object.keys(GlobalAliases));
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
if (isCommand("scp") && index === 1) {
|
|
|
|
for (const iphostname in AllServers) {
|
|
|
|
allPos.push(AllServers[iphostname].ip);
|
|
|
|
allPos.push(AllServers[iphostname].hostname);
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
|
|
|
return allPos;
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
if (isCommand("scp") && index === 0) {
|
|
|
|
addAllScripts();
|
|
|
|
addAllLitFiles();
|
|
|
|
addAllTextFiles();
|
2019-04-20 07:27:33 +02:00
|
|
|
addAllDirectories();
|
2019-04-10 08:07:12 +02:00
|
|
|
|
|
|
|
return allPos;
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
if (isCommand("connect")) {
|
|
|
|
// All network connections
|
2021-05-01 09:17:31 +02:00
|
|
|
for (let i = 0; i < currServ.serversOnNetwork.length; ++i) {
|
2021-04-30 05:52:56 +02:00
|
|
|
const serv = AllServers[currServ.serversOnNetwork[i]];
|
2019-04-10 08:07:12 +02:00
|
|
|
if (serv == null) { continue; }
|
|
|
|
allPos.push(serv.ip);
|
|
|
|
allPos.push(serv.hostname);
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
return allPos;
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
if (isCommand("kill") || isCommand("tail") || isCommand("mem") || isCommand("check")) {
|
|
|
|
addAllScripts();
|
2019-04-20 07:27:33 +02:00
|
|
|
addAllDirectories();
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
return allPos;
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
if (isCommand("nano")) {
|
|
|
|
addAllScripts();
|
|
|
|
addAllTextFiles();
|
2019-04-05 11:08:41 +02:00
|
|
|
allPos.push(".fconf");
|
2019-04-20 07:27:33 +02:00
|
|
|
addAllDirectories();
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
return allPos;
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
if (isCommand("rm")) {
|
|
|
|
addAllScripts();
|
|
|
|
addAllPrograms();
|
|
|
|
addAllLitFiles();
|
|
|
|
addAllTextFiles();
|
|
|
|
addAllCodingContracts();
|
2019-04-20 07:27:33 +02:00
|
|
|
addAllDirectories();
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
return allPos;
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
if (isCommand("run")) {
|
|
|
|
addAllScripts();
|
|
|
|
addAllPrograms();
|
|
|
|
addAllCodingContracts();
|
2019-04-20 07:27:33 +02:00
|
|
|
addAllDirectories();
|
2019-04-05 11:08:41 +02:00
|
|
|
|
|
|
|
return allPos;
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
if (isCommand("cat")) {
|
|
|
|
addAllMessages();
|
|
|
|
addAllLitFiles();
|
|
|
|
addAllTextFiles();
|
2019-04-20 07:27:33 +02:00
|
|
|
addAllDirectories();
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
return allPos;
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
if (isCommand("download") || isCommand("mv")) {
|
|
|
|
addAllScripts();
|
|
|
|
addAllTextFiles();
|
2019-04-20 07:27:33 +02:00
|
|
|
addAllDirectories();
|
|
|
|
|
|
|
|
return allPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCommand("cd")) {
|
|
|
|
addAllDirectories();
|
2019-04-05 11:08:41 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
return allPos;
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2019-04-20 07:27:33 +02:00
|
|
|
if (isCommand("ls") && index === 0) {
|
|
|
|
addAllDirectories();
|
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
return allPos;
|
|
|
|
}
|