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-02-08 14:30:06 +00:00
|
|
|
// Get current mode
|
2023-01-21 21:13:29 +00:00
|
|
|
const modeSplit = chatMode.split(' ');
|
2023-02-02 10:09:33 +00:00
|
|
|
if (modeSplit[0] !== "default") {
|
|
|
|
const lowerIn = input.toLowerCase();
|
2023-02-08 14:30:06 +00:00
|
|
|
// Run exit command in other mode
|
2023-02-02 10:09:33 +00:00
|
|
|
if (lowerIn.startsWith("exit")) {
|
|
|
|
cmd_exit();
|
|
|
|
return "";
|
2023-02-08 14:30:06 +00:00
|
|
|
// Run clear command in other modes
|
2023-02-02 10:09:33 +00:00
|
|
|
} else if (lowerIn.startsWith("clear")) {
|
|
|
|
cmd_clear();
|
|
|
|
return "";
|
|
|
|
}
|
2023-01-18 13:35:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 11:57:43 +00:00
|
|
|
// Handle different chat modes
|
2023-01-21 21:13:29 +00:00
|
|
|
switch (modeSplit[0]) {
|
2023-01-18 13:35:17 +00:00
|
|
|
case "msg":
|
2023-02-08 14:30:06 +00:00
|
|
|
// Send direct message, rename previous pretext to own name
|
2023-02-02 09:45:07 +00:00
|
|
|
directMessage(modeSplit[1].split(','), input);
|
2023-01-19 11:57:43 +00:00
|
|
|
renameToSelf();
|
2023-02-08 14:30:06 +00:00
|
|
|
// Return without message
|
2023-01-18 13:35:17 +00:00
|
|
|
return "";
|
2023-02-03 12:57:29 +00:00
|
|
|
case "chat":
|
2023-02-08 14:30:06 +00:00
|
|
|
// Send group message, rename previous pretext to own name
|
2023-02-06 08:25:14 +00:00
|
|
|
groupMessage(modeSplit[1].split(','), input);
|
2023-02-03 12:57:29 +00:00
|
|
|
renameToSelf();
|
2023-02-08 14:30:06 +00:00
|
|
|
// Return without message
|
2023-02-03 12:57:29 +00:00
|
|
|
return "";
|
2023-01-18 13:35:17 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2023-02-08 14:30:06 +00:00
|
|
|
// If command is called with parameters
|
2023-01-18 13:35:17 +00:00
|
|
|
} 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-19 14:31:13 +00:00
|
|
|
// Display 'help' message
|
2023-01-12 13:37:33 +00:00
|
|
|
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-02-06 09:36:35 +00:00
|
|
|
" -msg Open a direct chat to the provided user/-s by name<br>" +
|
2023-01-30 07:17:32 +00:00
|
|
|
" -ls List all connected users by name and id<br>" +
|
2023-02-06 09:36:35 +00:00
|
|
|
" -ping Ping the host to request two-way-delay<br>" +
|
2023-04-22 18:23:38 +00:00
|
|
|
" -chat Open a group chat to the provided chat/-s by name<br>" +
|
|
|
|
" -logout Disconnect from the host. This deletes your user";
|
2023-01-13 11:37:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 14:31:13 +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-02-08 14:30:06 +00:00
|
|
|
|
|
|
|
// Return without message
|
|
|
|
return null;
|
2023-01-13 11:05:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 08:34:42 +00:00
|
|
|
// Clear terminal window
|
|
|
|
function cmd_clear() {
|
|
|
|
setTimeout(() => {
|
2023-02-08 14:30:06 +00:00
|
|
|
// Remove all children except core elements and banner
|
2023-01-20 08:34:42 +00:00
|
|
|
const tbc = tbDiv.children;
|
|
|
|
for (let i = tbc.length-3; i > 1; i--)
|
|
|
|
tbDiv.removeChild(tbc[i]);
|
|
|
|
|
2023-02-08 14:30:06 +00:00
|
|
|
// Replace last child with new (current) pretext
|
2023-01-21 20:49:12 +00:00
|
|
|
const prelink = document.createElement("a");
|
2023-02-02 10:09:33 +00:00
|
|
|
prelink.innerHTML = pretext.current;
|
2023-01-21 20:49:12 +00:00
|
|
|
tbDiv.replaceChild(prelink, tbc[1]);
|
|
|
|
|
2023-02-08 14:30:06 +00:00
|
|
|
// Reset cursor position
|
2023-01-20 08:34:42 +00:00
|
|
|
cursorPosition = 0;
|
|
|
|
cursorYOffset = 7;
|
|
|
|
updateCursor();
|
|
|
|
}, 50);
|
2023-02-08 14:30:06 +00:00
|
|
|
|
|
|
|
// Return without message
|
2023-01-20 08:34:42 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-01-19 14:31:13 +00:00
|
|
|
// 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 = "";
|
|
|
|
|
2023-02-08 14:30:06 +00:00
|
|
|
// Get history
|
2023-01-13 12:05:59 +00:00
|
|
|
const hl = history.list;
|
|
|
|
for (let i = 0; i < hl.length; i++) {
|
2023-02-08 14:30:06 +00:00
|
|
|
// Add line break on every command except last
|
2023-01-13 12:05:59 +00:00
|
|
|
const lineBreak = (i !== hl.length - 1) ? "<br>" : "";
|
|
|
|
output += `${i+1} ${hl[i]}${lineBreak}`;
|
|
|
|
}
|
|
|
|
|
2023-02-08 14:30:06 +00:00
|
|
|
// Return output list
|
2023-01-13 12:05:59 +00:00
|
|
|
return output;
|
2023-01-13 11:57:46 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 14:31:13 +00:00
|
|
|
// Execute arbitrary math and logic equations
|
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!";
|
2023-02-08 14:30:06 +00:00
|
|
|
// Rename user
|
2023-02-02 07:12:12 +00:00
|
|
|
if (!!window.localStorage.getItem("connected"))
|
2023-01-20 07:13:35 +00:00
|
|
|
sendNickname(input[0]);
|
2023-02-08 14:30:06 +00:00
|
|
|
// Connect with given name
|
2023-01-20 07:13:35 +00:00
|
|
|
else
|
|
|
|
connect(input[0]);
|
2023-01-18 07:02:41 +00:00
|
|
|
return null;
|
2023-01-17 18:15:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 12:57:29 +00:00
|
|
|
// Check whether user is allowed to chat
|
|
|
|
function canChatCheck(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-02-02 07:12:12 +00:00
|
|
|
if (!window.localStorage.getItem("connected"))
|
2023-01-20 15:00:01 +00:00
|
|
|
return "You are not connected! Use the 'nick' command to connect using your username.";
|
2023-02-02 07:12:12 +00:00
|
|
|
if (window.localStorage.getItem("name") === "")
|
2023-01-18 13:35:17 +00:00
|
|
|
return "You do not have a name!";
|
2023-02-03 12:57:29 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize messaging
|
|
|
|
function messagingIniti(mode, name) {
|
2023-01-19 11:57:43 +00:00
|
|
|
// Get recipient username without spaces and pretext
|
2023-02-03 12:57:29 +00:00
|
|
|
pretext.current = `Chat (${name})> `;
|
2023-01-19 11:57:43 +00:00
|
|
|
// Set chat mode to direct messages and start pulling
|
2023-02-03 12:57:29 +00:00
|
|
|
chatMode = `${mode} ${name}`;
|
2023-04-22 18:23:38 +00:00
|
|
|
|
|
|
|
const ownName = window.localStorage.getItem("name");
|
|
|
|
const id = window.localStorage.getItem("id");
|
|
|
|
const encode = encodeURIComponent(`${ownName}#${id}`);
|
|
|
|
userData.chat = new EventSource(`/getChat?full=${encode}`);
|
|
|
|
|
|
|
|
userData.chat.onmessage = (event) => {
|
|
|
|
const data = JSON.parse(event.data);
|
|
|
|
outputText({
|
|
|
|
preNext: `${data.from}: `,
|
|
|
|
output: data.message
|
|
|
|
});
|
|
|
|
}
|
2023-01-18 07:02:41 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 12:57:29 +00:00
|
|
|
// Initialize direct chat
|
|
|
|
function cmd_msg(input) {
|
|
|
|
let output = canChatCheck(input);
|
|
|
|
if (output !== "") return output;
|
|
|
|
messagingIniti('msg', input[0]);
|
|
|
|
}
|
|
|
|
|
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];
|
2023-02-02 07:12:12 +00:00
|
|
|
if (level === "default")
|
2023-01-18 13:35:17 +00:00
|
|
|
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) {
|
2023-04-22 18:23:38 +00:00
|
|
|
case "chat":
|
2023-01-18 13:35:17 +00:00
|
|
|
case "msg":
|
2023-01-19 11:57:43 +00:00
|
|
|
// Stop chat pulling
|
2023-04-22 18:23:38 +00:00
|
|
|
userData.chat.close();
|
|
|
|
userData.chat = null;
|
2023-01-18 13:35:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-01-19 11:57:43 +00:00
|
|
|
|
|
|
|
// Exit was called automatically. Print error.
|
|
|
|
if (error)
|
|
|
|
outputText({output: error});
|
2023-01-19 14:31:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List all users to be able to chat with
|
2023-01-20 08:34:42 +00:00
|
|
|
function cmd_ls() {
|
2023-02-02 07:12:12 +00:00
|
|
|
if (!window.localStorage.getItem("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-19 14:31:13 +00:00
|
|
|
requestUsernames();
|
2023-01-20 07:13:35 +00:00
|
|
|
return null;
|
2023-01-30 07:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ping host for two-way-delay
|
|
|
|
function cmd_ping() {
|
2023-02-02 07:12:12 +00:00
|
|
|
if (!window.localStorage.getItem("connected"))
|
|
|
|
return "You are not connected! Use the 'nick' command to connect using your username.";
|
2023-01-30 07:17:32 +00:00
|
|
|
requestPing();
|
|
|
|
return null;
|
2023-02-02 07:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// User wants to log out
|
|
|
|
function cmd_logout() {
|
|
|
|
if (!window.localStorage.getItem("connected"))
|
|
|
|
return "You are not even connected yet!";
|
|
|
|
disconnect();
|
2023-02-03 12:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize group chat
|
|
|
|
function cmd_chat(input) {
|
|
|
|
let output = canChatCheck(input);
|
|
|
|
if (output !== "") return output;
|
|
|
|
messagingIniti('chat', input[0]);
|
2023-02-06 08:25:14 +00:00
|
|
|
sendChatInit(input[0].split(','));
|
2023-01-12 13:37:33 +00:00
|
|
|
}
|