2023-01-18 13:35:17 +00:00
|
|
|
// Run input as command if possible
|
|
|
|
function runCommand(input) {
|
|
|
|
// Return on no input
|
|
|
|
if (input === "")
|
|
|
|
return "";
|
|
|
|
|
2023-01-19 11:57:43 +00:00
|
|
|
// Exit current level
|
2023-01-18 13:35:17 +00:00
|
|
|
if (input.toLowerCase().startsWith("exit")) {
|
|
|
|
cmd_exit();
|
2023-01-18 13:48:46 +00:00
|
|
|
return "";
|
2023-01-18 13:35:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 11:57:43 +00:00
|
|
|
// Handle different chat modes
|
2023-01-18 13:35:17 +00:00
|
|
|
const modeSplit = chatMode.split(' ');
|
|
|
|
switch (modeSplit[0]) {
|
|
|
|
case "msg":
|
2023-01-19 11:57:43 +00:00
|
|
|
directMessage(modeSplit[1], input);
|
|
|
|
renameToSelf();
|
2023-01-18 13:35:17 +00:00
|
|
|
return "";
|
|
|
|
// case "chat":
|
2023-01-19 11:57:43 +00:00
|
|
|
// renameToSelf();
|
2023-01-18 13:35:17 +00:00
|
|
|
// return "";
|
|
|
|
}
|
|
|
|
|
2023-01-19 11:57:43 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:37:33 +00:00
|
|
|
function cmd_help() {
|
2023-01-13 11:37:25 +00:00
|
|
|
// Display 'help' message
|
|
|
|
return "Commands list:<br>" +
|
|
|
|
" -about Information about this website<br>"+
|
|
|
|
" -clear Clear terminal screen<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>" +
|
|
|
|
" -msg Open a direct chat to the provided user by name";
|
2023-01-13 11:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function cmd_about() {
|
|
|
|
// Display 'about' message
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
function cmd_clear() {
|
2023-01-13 11:37:25 +00:00
|
|
|
// Clear terminal by reloading page
|
2023-01-13 06:27:13 +00:00
|
|
|
window.location.reload();
|
2023-01-13 11:05:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 11:57:46 +00:00
|
|
|
function cmd_history() {
|
2023-01-13 12:25:15 +00:00
|
|
|
// Display the command history line by line
|
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
|
|
|
}
|
|
|
|
|
2023-01-13 11:05:17 +00:00
|
|
|
function cmd_exec(input) {
|
2023-01-13 11:37:25 +00:00
|
|
|
// No input was given
|
2023-01-13 11:05:17 +00:00
|
|
|
if (input === undefined)
|
|
|
|
return "You must enter parameters!";
|
2023-01-13 11:37:25 +00:00
|
|
|
// Input contains letters or invalid characters
|
2023-01-13 11:05:17 +00:00
|
|
|
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
|
2023-01-13 11:05:17 +00:00
|
|
|
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
|
2023-01-13 11:05:17 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-19 11:57:43 +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
|
|
|
}
|
|
|
|
|
2023-01-19 11:57:43 +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!";
|
|
|
|
user.name = input[0];
|
2023-01-19 11:57:43 +00:00
|
|
|
sendNickname();
|
2023-01-18 07:02:41 +00:00
|
|
|
return null;
|
2023-01-17 18:15:49 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 11:57:43 +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)
|
|
|
|
return "You are not connected!";
|
2023-01-18 13:35:17 +00:00
|
|
|
if (user.name === "")
|
|
|
|
return "You do not have a name!";
|
2023-01-19 11:57:43 +00:00
|
|
|
// 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})> `;
|
2023-01-19 11:57:43 +00:00
|
|
|
// Set chat mode to direct messages and start pulling
|
2023-01-18 07:02:41 +00:00
|
|
|
chatMode = `msg ${username}`;
|
2023-01-19 11:57:43 +00:00
|
|
|
user.chatPull = setInterval(getChatMessages, 500);
|
2023-01-18 07:02:41 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 11:57:43 +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")
|
|
|
|
return "Already at top-level!";
|
|
|
|
|
2023-01-19 11:57:43 +00:00
|
|
|
// Set mode to default and reset pretext
|
2023-01-18 13:35:17 +00:00
|
|
|
chatMode = "default";
|
|
|
|
pretext.current = pretext.original;
|
2023-01-19 11:57:43 +00:00
|
|
|
// Do individual resets
|
2023-01-18 13:35:17 +00:00
|
|
|
switch (level) {
|
|
|
|
// case "chat":
|
|
|
|
case "msg":
|
2023-01-19 11:57:43 +00:00
|
|
|
// Stop chat pulling
|
2023-01-18 13:35:17 +00:00
|
|
|
clearInterval(user.chatPull);
|
|
|
|
break;
|
|
|
|
}
|
2023-01-19 11:57:43 +00:00
|
|
|
|
|
|
|
// Exit was called automatically. Print error.
|
|
|
|
if (error)
|
|
|
|
outputText({output: error});
|
2023-01-12 13:37:33 +00:00
|
|
|
}
|