// Run input as command if possible
function runCommand(input) {
// Return on no input
if (input === "")
return "";
// Exit current level
const modeSplit = chatMode.split(' ');
if (modeSplit[0] !== "default") {
const lowerIn = input.toLowerCase();
if (lowerIn.startsWith("exit")) {
cmd_exit();
return "";
} else if (lowerIn.startsWith("clear")) {
cmd_clear();
return "";
}
}
// Handle different chat modes
switch (modeSplit[0]) {
case "msg":
directMessage(modeSplit[1].split(','), 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:
" +
" -about Information about this website
"+
" -reload Reload the page (deletes all session data)
" +
" -clear Clear terminal screen (keeps all session data)
" +
" -history Displays the command history of this session
" +
" -exec Execute arbitrary math and logic equations
" +
" -nick Choose your username. Do not use spaces in it
" +
" -msg Open a direct chat to the provided user by name
" +
" -ls List all connected users by name and id
" +
" -ping Ping the host to request two-way-delay";
}
// Display 'about' message
function cmd_about() {
return "This website is based on the general idea and design of a terminal.
" +
"It serves the purpose of a homepage. It exists just for the fun of creating it.";
}
// Reload page
function cmd_reload() {
window.location.reload();
}
// 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.current;
tbDiv.replaceChild(prelink, tbc[1]);
cursorPosition = 0;
cursorYOffset = 7;
updateCursor();
}, 50);
return null;
}
// 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) ? "
" : "";
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 (!!window.localStorage.getItem("connected"))
sendNickname(input[0]);
else
connect(input[0]);
return null;
}
// Initialize direct chat
function cmd_msg(input) {
if (input === undefined)
return "No recipient was given!";
if (!window.localStorage.getItem("connected"))
return "You are not connected! Use the 'nick' command to connect using your username.";
if (window.localStorage.getItem("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}`;
userData.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(userData.chatPull);
break;
}
// Exit was called automatically. Print error.
if (error)
outputText({output: error});
}
// List all users to be able to chat with
function cmd_ls() {
if (!window.localStorage.getItem("connected"))
return "You are not connected! Use the 'nick' command to connect using your username.";
requestUsernames();
return null;
}
// Ping host for two-way-delay
function cmd_ping() {
if (!window.localStorage.getItem("connected"))
return "You are not connected! Use the 'nick' command to connect using your username.";
requestPing();
return null;
}
// User wants to log out
function cmd_logout() {
if (!window.localStorage.getItem("connected"))
return "You are not even connected yet!";
disconnect();
}