TerminalHomepage/js/commands.js

189 lines
5.4 KiB
JavaScript

// Run input as command if possible
function runCommand(input) {
// Return on no input
if (input === "")
return "";
// Exit current level
if (input.toLowerCase().startsWith("exit")) {
cmd_exit();
return "";
}
// Handle different chat modes
const modeSplit = chatMode.split(' ');
switch (modeSplit[0]) {
case "msg":
directMessage(modeSplit[1], input);
renameToSelf();
return "";
// case "chat":
// renameToSelf();
// return "";
}
// Default chat mode:
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() {
return "Commands list:<br>" +
" -about Information about this website<br>"+
" -clear Clear terminal screen<br>" +
" -history Displays the command history of this session<br>" +
" -exec Execute arbitrary math and logic equations<br>" +
" -nick Choose your username. Do not use spaces in it<br>" +
" -msg Open a direct chat to the provided user by name";
}
// Display 'about' message
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.";
}
// Clear terminal by reloading page
function cmd_clear() {
window.location.reload();
}
// Display the command history line by line
function cmd_history() {
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;
}
// Execute arbitrary math and logic equations
function cmd_exec(input) {
// No input was given
if (input === undefined)
return "You must enter parameters!";
// Input contains letters or invalid characters
const str = input.join(' ');
if (/[',:;a-zA-Z]/.test(str) || str === "")
return "Invalid input!";
// 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);
});
// If exceptions remain, invalid input was given
if (chars.length > 0)
return "Invalid input!";
// Execute input and return output
return eval(str).toString();
}
// Echo out any given text
function cmd_echo(input) {
if (input === undefined)
return " ";
return input.join(' ');
}
// Set users' name
function cmd_nick(input) {
if (input === undefined)
return "No nickname was given!";
if (!user.connected)
return "You are not connected!";
sendNickname(input[0]);
return null;
}
// Initialize direct chat
function cmd_msg(input) {
if (input === undefined)
return "No recipient was given!";
if (!user.connected)
return "You are not connected!";
if (user.name === "")
return "You do not have a name!";
// Get recipient username without spaces and pretext
const username = input[0];
pretext.current = `Chat (${username})> `;
// Set chat mode to direct messages and start pulling
chatMode = `msg ${username}`;
user.chatPull = setInterval(getChatMessages, 500);
}
// Exit current level (example: chat -> main)
function cmd_exit(error) {
const level = chatMode.split(' ')[0];
if (level === "default")
return "Already at top-level!";
// Set mode to default and reset pretext
chatMode = "default";
pretext.current = pretext.original;
// Do individual resets
switch (level) {
// case "chat":
case "msg":
// Stop chat pulling
clearInterval(user.chatPull);
break;
}
// Exit was called automatically. Print error.
if (error)
outputText({output: error});
}
// List all users to be able to chat with
function cmd_ll() {
if (!user.connected)
return "You are not connected!";
requestUsernames();
}
// Connect to server
function cmd_connect() {
if (user.connected)
return "You are already connected!";
connect();
}