2023-01-19 11:57:43 +00:00
|
|
|
// Global networking variables
|
2023-02-02 08:16:40 +00:00
|
|
|
const pretext = {
|
2023-02-02 07:12:12 +00:00
|
|
|
original: "root@baipyr.us:~# ",
|
|
|
|
current: "root@baipyr.us:~# "
|
|
|
|
};
|
|
|
|
|
2023-04-22 18:23:38 +00:00
|
|
|
const userData = { history: [], chat: null };
|
2023-02-02 08:16:40 +00:00
|
|
|
let chatMode = "default", activityNotify;
|
2023-01-19 11:57:43 +00:00
|
|
|
|
2023-02-08 14:30:06 +00:00
|
|
|
|
|
|
|
// If no user data exists, initialize it
|
2023-02-02 07:12:12 +00:00
|
|
|
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", "");
|
2023-02-02 08:16:40 +00:00
|
|
|
|
2023-02-08 14:30:06 +00:00
|
|
|
|
|
|
|
// Activity request interval
|
2023-02-02 08:16:40 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-02-08 14:30:06 +00:00
|
|
|
// If user data is found, check for validity
|
2023-02-02 08:16:40 +00:00
|
|
|
if (window.localStorage.getItem("id") !== "" && window.localStorage.getItem("name") !== "") {
|
2023-02-02 07:12:12 +00:00
|
|
|
pretext.original = pretext.current = `${window.localStorage.getItem("name")}@baipyr.us:~# `;
|
|
|
|
|
2023-02-02 08:16:40 +00:00
|
|
|
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 => {
|
2023-02-08 14:30:06 +00:00
|
|
|
// Return JSON if status is OK
|
2023-02-02 08:16:40 +00:00
|
|
|
if (res.status === 200)
|
|
|
|
return res.json();
|
|
|
|
}).then(res => {
|
|
|
|
if (res === undefined)
|
|
|
|
return;
|
2023-02-08 14:30:06 +00:00
|
|
|
// Regularly send activity
|
2023-02-02 08:16:40 +00:00
|
|
|
if (res.success)
|
|
|
|
activityNotify = setNotification();
|
2023-02-08 14:30:06 +00:00
|
|
|
// Delete user data
|
2023-02-02 08:16:40 +00:00
|
|
|
else {
|
|
|
|
window.localStorage.setItem("id", "");
|
|
|
|
window.localStorage.setItem("name", "");
|
|
|
|
window.localStorage.setItem("connected", "");
|
|
|
|
pretext.original = pretext.current = "root@baipyr.us:~# ";
|
|
|
|
cmd_clear();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-19 11:57:43 +00:00
|
|
|
|
|
|
|
// Tell server to disconnect
|
2023-02-02 07:12:12 +00:00
|
|
|
function disconnect() {
|
|
|
|
if (!!window.localStorage.getItem("connected")) {
|
2023-02-08 14:30:06 +00:00
|
|
|
// Send disconnect request
|
2023-01-18 14:19:22 +00:00
|
|
|
fetch('/disconnect', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2023-02-02 07:12:12 +00:00
|
|
|
name: window.localStorage.getItem("name"),
|
|
|
|
id: window.localStorage.getItem("id")
|
2023-01-18 14:19:22 +00:00
|
|
|
})
|
|
|
|
}).then(res => {});
|
2023-02-08 14:30:06 +00:00
|
|
|
// Delete user data
|
2023-02-02 07:12:12 +00:00
|
|
|
window.localStorage.setItem("id", "");
|
|
|
|
window.localStorage.setItem("name", "");
|
|
|
|
window.localStorage.setItem("connected", "");
|
2023-02-08 14:30:06 +00:00
|
|
|
// Reset pretext
|
2023-02-02 07:12:12 +00:00
|
|
|
pretext.original = pretext.current = "root@baipyr.us:~# ";
|
|
|
|
}
|
|
|
|
}
|
2023-01-18 14:19:22 +00:00
|
|
|
|
2023-01-19 11:57:43 +00:00
|
|
|
// Connect to server
|
2023-01-20 07:13:35 +00:00
|
|
|
function connect(name) {
|
2023-02-02 07:12:12 +00:00
|
|
|
window.localStorage.setItem("name", name);
|
2023-01-20 07:13:35 +00:00
|
|
|
fetch('/connect', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ name })
|
|
|
|
}).then(res => {
|
2023-01-19 14:31:13 +00:00
|
|
|
if (res.status === 200) {
|
2023-02-08 14:30:06 +00:00
|
|
|
// Regularly send activity
|
2023-02-02 08:16:40 +00:00
|
|
|
activityNotify = setNotification();
|
2023-02-02 07:12:12 +00:00
|
|
|
window.localStorage.setItem("connected", "1");
|
2023-01-19 14:31:13 +00:00
|
|
|
return res.json();
|
|
|
|
}
|
|
|
|
// No valid status code was sent
|
|
|
|
console.error("Could not connect to server!");
|
|
|
|
}).then(res => {
|
|
|
|
if (res === undefined)
|
|
|
|
return;
|
2023-02-08 14:30:06 +00:00
|
|
|
// Save provided id, set pretext, output confirmation
|
2023-01-20 07:13:35 +00:00
|
|
|
const { id } = res;
|
2023-02-02 07:12:12 +00:00
|
|
|
window.localStorage.setItem("id", id);
|
|
|
|
pretext.original = pretext.current = `${name}@baipyr.us:~# `;
|
2023-01-20 07:13:35 +00:00
|
|
|
outputText({output: `Connected as '${name}#${id}'.`});
|
2023-01-19 14:31:13 +00:00
|
|
|
});
|
|
|
|
}
|
2023-01-18 14:19:22 +00:00
|
|
|
|
2023-01-19 11:57:43 +00:00
|
|
|
|
|
|
|
// Send nickname to server, receive verification
|
2023-01-19 14:31:13 +00:00
|
|
|
function sendNickname(name) {
|
2023-02-02 07:12:12 +00:00
|
|
|
const oldName = window.localStorage.getItem("name");
|
|
|
|
window.localStorage.setItem("name", name);
|
2023-01-19 11:57:43 +00:00
|
|
|
fetch('/nickname', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2023-02-02 07:12:12 +00:00
|
|
|
id: window.localStorage.getItem("id"),
|
|
|
|
oldName,
|
2023-01-19 14:31:13 +00:00
|
|
|
name
|
2023-01-19 11:57:43 +00:00
|
|
|
})
|
|
|
|
}).then(res => {
|
2023-02-08 14:30:06 +00:00
|
|
|
// Return JSON if status is OK
|
2023-01-19 11:57:43 +00:00
|
|
|
if (res.status === 200)
|
|
|
|
return res.json();
|
|
|
|
}).then(res => {
|
|
|
|
// No valid status code
|
|
|
|
if (res === undefined)
|
|
|
|
return;
|
2023-01-19 14:31:13 +00:00
|
|
|
// Receive name and save it
|
2023-01-20 07:13:35 +00:00
|
|
|
const { id } = res;
|
2023-02-02 07:12:12 +00:00
|
|
|
window.localStorage.setItem("id", id);
|
|
|
|
pretext.original = pretext.current = `${name}@baipyr.us:~# `;
|
2023-01-19 11:57:43 +00:00
|
|
|
// Reply whether name is taken or not
|
2023-01-20 07:13:35 +00:00
|
|
|
outputText({output: `Applied name '${name}#${id}'.`});
|
2023-01-19 11:57:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a direct message, do not look at response
|
2023-02-02 09:45:07 +00:00
|
|
|
function directMessage(names, message) {
|
2023-01-19 11:57:43 +00:00
|
|
|
fetch('/message', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2023-02-02 07:12:12 +00:00
|
|
|
name: window.localStorage.getItem("name"),
|
|
|
|
id: window.localStorage.getItem("id"),
|
2023-02-02 09:45:07 +00:00
|
|
|
to: names,
|
2023-01-20 07:13:35 +00:00
|
|
|
message
|
2023-01-19 11:57:43 +00:00
|
|
|
})
|
|
|
|
}).then(res => {});
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:57:29 +00:00
|
|
|
// 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 => {});
|
|
|
|
}
|
2023-01-19 14:31:13 +00:00
|
|
|
|
|
|
|
// Request all usernames and number of anonymous users
|
|
|
|
function requestUsernames() {
|
|
|
|
fetch('/getNames', {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
}
|
|
|
|
}).then(res => {
|
2023-02-08 14:30:06 +00:00
|
|
|
// Return JSON if status is OK
|
2023-01-19 14:31:13 +00:00
|
|
|
if (res.status === 200)
|
|
|
|
return res.json();
|
|
|
|
}).then(res => {
|
|
|
|
if (res === undefined)
|
|
|
|
return;
|
2023-02-08 14:30:06 +00:00
|
|
|
// Output usernames
|
2023-01-20 07:13:35 +00:00
|
|
|
let output = "";
|
2023-01-19 14:31:13 +00:00
|
|
|
for (const u of res)
|
2023-01-20 07:13:35 +00:00
|
|
|
output += `${u.name}#${u.id} `;
|
|
|
|
outputText({ output });
|
2023-01-19 14:31:13 +00:00
|
|
|
});
|
2023-01-30 07:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ping host for two-way-delay
|
|
|
|
function requestPing() {
|
|
|
|
const startTime = new Date();
|
2023-02-02 08:16:40 +00:00
|
|
|
fetch('/ping', {
|
2023-04-22 18:23:38 +00:00
|
|
|
method: 'GET'
|
2023-02-02 08:16:40 +00:00
|
|
|
}).then(res => {
|
2023-01-30 07:17:32 +00:00
|
|
|
if (res.status !== 200)
|
|
|
|
return;
|
2023-02-08 14:30:06 +00:00
|
|
|
// Output measured ping time
|
2023-01-30 07:17:32 +00:00
|
|
|
const diff = new Date() - startTime;
|
|
|
|
const output = `Host responded after ${diff}ms.`;
|
2023-04-22 18:23:38 +00:00
|
|
|
outputText({output});
|
2023-01-30 07:17:32 +00:00
|
|
|
});
|
2023-02-03 12:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 => {});
|
2023-01-19 14:31:13 +00:00
|
|
|
}
|