262 lines
7.5 KiB
JavaScript
262 lines
7.5 KiB
JavaScript
// Global networking variables
|
|
const pretext = {
|
|
original: "root@baipyr.us:~# ",
|
|
current: "root@baipyr.us:~# "
|
|
};
|
|
|
|
const userData = { history: [], chatPull: null };
|
|
let chatMode = "default", activityNotify;
|
|
|
|
if (window.localStorage.getItem("id") === null)
|
|
window.localStorage.setItem("id", "");
|
|
if (window.localStorage.getItem("name") === null)
|
|
window.localStorage.setItem("name", "");
|
|
if (window.localStorage.getItem("connected") === null)
|
|
window.localStorage.setItem("connected", "");
|
|
|
|
function setNotification() {
|
|
return setInterval(()=>{
|
|
fetch('/activity', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
name: window.localStorage.getItem("name"),
|
|
id: window.localStorage.getItem("id")
|
|
})
|
|
}).then(res => {});
|
|
}, 60000);
|
|
}
|
|
|
|
if (window.localStorage.getItem("id") !== "" && window.localStorage.getItem("name") !== "") {
|
|
pretext.original = pretext.current = `${window.localStorage.getItem("name")}@baipyr.us:~# `;
|
|
|
|
fetch('/login', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
name: window.localStorage.getItem("name"),
|
|
id: window.localStorage.getItem("id")
|
|
})
|
|
}).then(res => {
|
|
if (res.status === 200)
|
|
return res.json();
|
|
}).then(res => {
|
|
if (res === undefined)
|
|
return;
|
|
if (res.success)
|
|
activityNotify = setNotification();
|
|
else {
|
|
window.localStorage.setItem("id", "");
|
|
window.localStorage.setItem("name", "");
|
|
window.localStorage.setItem("connected", "");
|
|
pretext.original = pretext.current = "root@baipyr.us:~# ";
|
|
cmd_clear();
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
// Tell server to disconnect
|
|
function disconnect() {
|
|
if (!!window.localStorage.getItem("connected")) {
|
|
fetch('/disconnect', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
name: window.localStorage.getItem("name"),
|
|
id: window.localStorage.getItem("id")
|
|
})
|
|
}).then(res => {});
|
|
window.localStorage.setItem("id", "");
|
|
window.localStorage.setItem("name", "");
|
|
window.localStorage.setItem("connected", "");
|
|
pretext.original = pretext.current = "root@baipyr.us:~# ";
|
|
}
|
|
}
|
|
|
|
// Connect to server
|
|
function connect(name) {
|
|
window.localStorage.setItem("name", name);
|
|
fetch('/connect', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ name })
|
|
}).then(res => {
|
|
if (res.status === 200) {
|
|
activityNotify = setNotification();
|
|
window.localStorage.setItem("connected", "1");
|
|
return res.json();
|
|
}
|
|
// No valid status code was sent
|
|
console.error("Could not connect to server!");
|
|
}).then(res => {
|
|
if (res === undefined)
|
|
return;
|
|
// Save provided id
|
|
const { id } = res;
|
|
window.localStorage.setItem("id", id);
|
|
pretext.original = pretext.current = `${name}@baipyr.us:~# `;
|
|
outputText({output: `Connected as '${name}#${id}'.`});
|
|
});
|
|
}
|
|
|
|
|
|
// Send nickname to server, receive verification
|
|
function sendNickname(name) {
|
|
const oldName = window.localStorage.getItem("name");
|
|
window.localStorage.setItem("name", name);
|
|
fetch('/nickname', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
id: window.localStorage.getItem("id"),
|
|
oldName,
|
|
name
|
|
})
|
|
}).then(res => {
|
|
if (res.status === 200)
|
|
return res.json();
|
|
}).then(res => {
|
|
// No valid status code
|
|
if (res === undefined)
|
|
return;
|
|
// Receive name and save it
|
|
const { id } = res;
|
|
window.localStorage.setItem("id", id);
|
|
pretext.original = pretext.current = `${name}@baipyr.us:~# `;
|
|
// Reply whether name is taken or not
|
|
outputText({output: `Applied name '${name}#${id}'.`});
|
|
});
|
|
}
|
|
|
|
// Pull incoming chat messages
|
|
function getChatMessages() {
|
|
fetch('/getChat', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
from: chatMode.split(' ')[1].split(','),
|
|
name: window.localStorage.getItem("name"),
|
|
id: window.localStorage.getItem("id")
|
|
})
|
|
}).then(res => {
|
|
// Incoming messages
|
|
if (res.status === 200)
|
|
return res.json();
|
|
// No new messages
|
|
else if (res.status === 204)
|
|
return;
|
|
cmd_exit("Error occurred while fetching messages!");
|
|
}).then(res => {
|
|
// No messages or invalid status code
|
|
if (res === undefined)
|
|
return;
|
|
// Output all messages by user
|
|
for (const user in res)
|
|
for (const msg of res[user])
|
|
outputText({
|
|
preNext: `${user}: `,
|
|
output: msg
|
|
});
|
|
});
|
|
}
|
|
|
|
// Send a direct message, do not look at response
|
|
function directMessage(names, message) {
|
|
fetch('/message', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
name: window.localStorage.getItem("name"),
|
|
id: window.localStorage.getItem("id"),
|
|
to: names,
|
|
message
|
|
})
|
|
}).then(res => {});
|
|
}
|
|
|
|
// Send a group message
|
|
function groupMessage(chat, message) {
|
|
fetch('/sendGroup', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
name: window.localStorage.getItem("name"),
|
|
id: window.localStorage.getItem("id"),
|
|
message,
|
|
chat
|
|
})
|
|
}).then(res => {});
|
|
}
|
|
|
|
// Request all usernames and number of anonymous users
|
|
function requestUsernames() {
|
|
fetch('/getNames', {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
}
|
|
}).then(res => {
|
|
if (res.status === 200)
|
|
return res.json();
|
|
}).then(res => {
|
|
if (res === undefined)
|
|
return;
|
|
let output = "";
|
|
for (const u of res)
|
|
output += `${u.name}#${u.id} `;
|
|
outputText({ output });
|
|
});
|
|
}
|
|
|
|
// Ping host for two-way-delay
|
|
function requestPing() {
|
|
const startTime = new Date();
|
|
fetch('/ping', {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
}
|
|
}).then(res => {
|
|
if (res.status !== 200)
|
|
return;
|
|
const diff = new Date() - startTime;
|
|
const output = `Host responded after ${diff}ms.`;
|
|
outputText({output})
|
|
});
|
|
}
|
|
|
|
// Initialize group chat
|
|
function sendChatInit(chat) {
|
|
fetch('/chatInit', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
name: window.localStorage.getItem("name"),
|
|
id: window.localStorage.getItem("id"),
|
|
chat
|
|
})
|
|
}).then(res => {});
|
|
} |