218 lines
6.0 KiB
JavaScript
218 lines
6.0 KiB
JavaScript
// Run input as command if possible
|
|
function runCommand(input) {
|
|
// Return on no input
|
|
if (input === "")
|
|
return "";
|
|
|
|
if (input.toLowerCase().startsWith("exit")) {
|
|
cmd_exit();
|
|
return "";
|
|
}
|
|
|
|
const modeSplit = chatMode.split(' ');
|
|
switch (modeSplit[0]) {
|
|
case "msg":
|
|
directMessage(modeSplit[1], input)
|
|
return "";
|
|
// case "chat":
|
|
// // Global messages
|
|
// return "";
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
function directMessage(name, message) {
|
|
// Direct message
|
|
fetch('/message', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
id: user.id,
|
|
message,
|
|
name
|
|
})
|
|
}).then(res => {});
|
|
}
|
|
|
|
// function globalMessage(message) {}
|
|
|
|
function cmd_help() {
|
|
// Display 'help' message
|
|
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";
|
|
}
|
|
|
|
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.";
|
|
}
|
|
|
|
function cmd_clear() {
|
|
// Clear terminal by reloading page
|
|
window.location.reload();
|
|
}
|
|
|
|
function cmd_history() {
|
|
// Display the command history line by line
|
|
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;
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
function cmd_echo(input) {
|
|
if (input === undefined)
|
|
return " ";
|
|
return input.join(' ');
|
|
}
|
|
|
|
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 => {
|
|
if (res.status === 200)
|
|
return res.json();
|
|
}).then(res => {
|
|
if (res === undefined)
|
|
return;
|
|
if (res.success)
|
|
outputText({output: `Applied name '${user.name}'.`});
|
|
else {
|
|
outputText({output: `Name '${user.name}' is already taken!`});
|
|
user.name = "";
|
|
}
|
|
});
|
|
return null;
|
|
}
|
|
|
|
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!";
|
|
const username = input[0];
|
|
pretext.current = `Chat (${username})> `;
|
|
chatMode = `msg ${username}`;
|
|
user.chatPull = setInterval(() => {
|
|
fetch('/getChat', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
id: user.id
|
|
})
|
|
}).then(res => {
|
|
if (res.status === 200)
|
|
return res.json();
|
|
else if (res.status === 204)
|
|
return;
|
|
cmd_exit();
|
|
outputText({output: "Error occurred while fetching messages!"});
|
|
}).then(res => {
|
|
if (res === undefined)
|
|
return;
|
|
for (const user in res)
|
|
for (const msg of res[user])
|
|
outputText({output: msg});
|
|
});
|
|
}, 500);
|
|
}
|
|
|
|
function cmd_exit() {
|
|
const level = chatMode.split(' ')[0];
|
|
if (level === "default")
|
|
return "Already at top-level!";
|
|
|
|
chatMode = "default";
|
|
pretext.current = pretext.original;
|
|
switch (level) {
|
|
// case "chat":
|
|
case "msg":
|
|
clearInterval(user.chatPull);
|
|
break;
|
|
}
|
|
} |