TerminalHomepage/js/commands.js

208 lines
6.0 KiB
JavaScript
Raw Normal View History

2023-01-18 13:35:17 +00:00
// Run input as command if possible
function runCommand(input) {
// Return on no input
if (input === "")
return "";
// Exit current level
const modeSplit = chatMode.split(' ')[0];
if (input.toLowerCase().startsWith("exit") && modeSplit !== "default") {
2023-01-18 13:35:17 +00:00
cmd_exit();
2023-01-18 13:48:46 +00:00
return "";
2023-01-18 13:35:17 +00:00
}
// Handle different chat modes
switch (modeSplit) {
2023-01-18 13:35:17 +00:00
case "msg":
directMessage(modeSplit[1], input);
renameToSelf();
2023-01-18 13:35:17 +00:00
return "";
// case "chat":
// renameToSelf();
2023-01-18 13:35:17 +00:00
// return "";
}
// Default chat mode:
2023-01-18 13:35:17 +00:00
let output = "";
const lowerIn = input.toLowerCase();
// Go through properties of window
for (const func in window) {
const splits = func.split("cmd_");
// If property is prefixed with 'cmd_' (a 'command', so an executable function)
if (splits.length === 2) {
const name = splits[1];
const lowerNm = name.toLowerCase();
// If command is called without parameters
if (lowerIn === lowerNm) {
output = window[func]();
break;
// If command is called with parameters
} else if (lowerIn.startsWith(lowerNm + " ")) {
// Parameters always follow the command name after first space
const params = input.split(" ").filter((e,i)=>i!==0);
output = window[func](params);
break;
}
}
}
// Standard output:
if (output === "")
output = `${input.split(" ")[0]}: command not found`;
if (output === undefined)
output = "";
// Return command output
return output;
}
// Display 'help' message
function cmd_help() {
2023-01-13 11:37:25 +00:00
return "Commands list:<br>" +
" -about Information about this website<br>"+
2023-01-20 08:34:42 +00:00
" -reload Reload the page (deletes all session data)<br>" +
" -clear Clear terminal screen (keeps all session data)<br>" +
2023-01-13 11:57:46 +00:00
" -history Displays the command history of this session<br>" +
2023-01-18 15:33:57 +00:00
" -exec Execute arbitrary math and logic equations<br>" +
" -nick Choose your username. Do not use spaces in it<br>" +
2023-01-20 14:56:30 +00:00
" -msg Open a direct chat to the provided user by name<br>" +
" -ls List all connected users by name and id";
2023-01-13 11:37:25 +00:00
}
// Display 'about' message
2023-01-13 11:37:25 +00:00
function cmd_about() {
return "This website is based on the general idea and design of a terminal.<br>" +
"It serves the purpose of a homepage. It exists just for the fun of creating it.";
2023-01-13 06:27:13 +00:00
}
2023-01-20 08:34:42 +00:00
// Reload page
function cmd_reload() {
2023-01-13 06:27:13 +00:00
window.location.reload();
}
2023-01-20 08:34:42 +00:00
// Clear terminal window
function cmd_clear() {
setTimeout(() => {
const tbc = tbDiv.children;
for (let i = tbc.length-3; i > 1; i--)
tbDiv.removeChild(tbc[i]);
const prelink = document.createElement("a");
prelink.innerHTML = pretext.original;
tbDiv.replaceChild(prelink, tbc[1]);
2023-01-20 08:34:42 +00:00
pretext.current = pretext.original;
cursorPosition = 0;
cursorYOffset = 7;
updateCursor();
}, 50);
return null;
}
// Display the command history line by line
2023-01-13 11:57:46 +00:00
function cmd_history() {
2023-01-13 12:05:59 +00:00
let output = "";
const hl = history.list;
for (let i = 0; i < hl.length; i++) {
const lineBreak = (i !== hl.length - 1) ? "<br>" : "";
output += `${i+1} ${hl[i]}${lineBreak}`;
}
return output;
2023-01-13 11:57:46 +00:00
}
// Execute arbitrary math and logic equations
function cmd_exec(input) {
2023-01-13 11:37:25 +00:00
// No input was given
if (input === undefined)
return "You must enter parameters!";
2023-01-13 11:37:25 +00:00
// Input contains letters or invalid characters
const str = input.join(' ');
if (/[',:;a-zA-Z]/.test(str) || str === "")
return "Invalid input!";
2023-01-13 11:37:25 +00:00
// Input is inside of character range with exceptions
const chars = str.split('').filter(e => {
const code = e.charCodeAt(0);
const s = code === 32;
const a = code > 36;
const b = code < 63;
const c = code === 94;
const d = code === 124;
return !(s || a && b || c || d);
});
2023-01-13 11:37:25 +00:00
// If exceptions remain, invalid input was given
if (chars.length > 0)
return "Invalid input!";
2023-01-13 11:37:25 +00:00
// Execute input and return output
2023-01-18 13:35:17 +00:00
return eval(str).toString();
2023-01-16 06:15:33 +00:00
}
// Echo out any given text
2023-01-16 06:15:33 +00:00
function cmd_echo(input) {
2023-01-16 06:21:58 +00:00
if (input === undefined)
return " ";
2023-01-16 06:15:33 +00:00
return input.join(' ');
2023-01-16 14:21:30 +00:00
}
// Set users' name
2023-01-17 18:15:49 +00:00
function cmd_nick(input) {
if (input === undefined)
return "No nickname was given!";
if (user.connected)
sendNickname(input[0]);
else
connect(input[0]);
2023-01-18 07:02:41 +00:00
return null;
2023-01-17 18:15:49 +00:00
}
// Initialize direct chat
2023-01-18 07:02:41 +00:00
function cmd_msg(input) {
2023-01-16 14:21:30 +00:00
if (input === undefined)
2023-01-18 13:35:17 +00:00
return "No recipient was given!";
2023-01-17 18:15:49 +00:00
if (!user.connected)
2023-01-20 15:00:01 +00:00
return "You are not connected! Use the 'nick' command to connect using your username.";
2023-01-18 13:35:17 +00:00
if (user.name === "")
return "You do not have a name!";
// Get recipient username without spaces and pretext
2023-01-18 07:02:41 +00:00
const username = input[0];
2023-01-18 13:35:17 +00:00
pretext.current = `Chat (${username})> `;
// Set chat mode to direct messages and start pulling
2023-01-18 07:02:41 +00:00
chatMode = `msg ${username}`;
user.chatPull = setInterval(getChatMessages, 500);
2023-01-18 07:02:41 +00:00
}
// Exit current level (example: chat -> main)
function cmd_exit(error) {
2023-01-18 13:35:17 +00:00
const level = chatMode.split(' ')[0];
if (level === "default") {
console.log("Test!");
2023-01-18 13:35:17 +00:00
return "Already at top-level!";
}
2023-01-18 13:35:17 +00:00
// Set mode to default and reset pretext
2023-01-18 13:35:17 +00:00
chatMode = "default";
pretext.current = pretext.original;
// Do individual resets
2023-01-18 13:35:17 +00:00
switch (level) {
// case "chat":
case "msg":
// Stop chat pulling
2023-01-18 13:35:17 +00:00
clearInterval(user.chatPull);
break;
}
// Exit was called automatically. Print error.
if (error)
outputText({output: error});
}
// List all users to be able to chat with
2023-01-20 08:34:42 +00:00
function cmd_ls() {
if (!user.connected)
2023-01-20 15:00:01 +00:00
return "You are not connected! Use the 'nick' command to connect using your username.";
requestUsernames();
return null;
}