TerminalHomepage/js/commands.js

105 lines
3.0 KiB
JavaScript
Raw Normal View History

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-13 11:37:25 +00:00
" -exec Execute arbitrary math and logic equations";
}
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();
return "";
}
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
}
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-13 14:18:53 +00:00
return (new Function(`return ${str}`))().toString();
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-17 18:15:49 +00:00
function cmd_nick(input) {
if (input === undefined)
return "No nickname was given!";
user.name = input[0];
fetch('/nickname', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: user.name,
id: user.id
})
}).then(res => {});
return `Applied name '${user.name}'.`;
}
2023-01-16 14:21:30 +00:00
async function cmd_msg(input) {
if (input === undefined)
return "No message given!";
2023-01-17 18:15:49 +00:00
if (!user.connected)
return "You are not connected!";
2023-01-16 14:21:30 +00:00
const res = await fetch('/message', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
2023-01-17 18:15:49 +00:00
body: JSON.stringify({
message: input.join(' '),
id: user.id
})
2023-01-16 14:21:30 +00:00
});
if (res.status === 200) {
2023-01-17 18:15:49 +00:00
const { message } = await res.json();
return message;
2023-01-16 14:21:30 +00:00
} else
return "Server returned status " + res.status;
}