// Global networking variables const pretext = { original: "root@baipyr.us:~# ", current: "root@baipyr.us:~# " }; const userData = { history: [], chat: null }; let chatMode = "default", activityNotify; // If no user data exists, initialize it 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", ""); // Activity request interval 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 user data is found, check for validity 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 => { // Return JSON if status is OK if (res.status === 200) return res.json(); }).then(res => { if (res === undefined) return; // Regularly send activity if (res.success) activityNotify = setNotification(); // Delete user data 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")) { // Send disconnect request fetch('/disconnect', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: window.localStorage.getItem("name"), id: window.localStorage.getItem("id") }) }).then(res => {}); // Delete user data window.localStorage.setItem("id", ""); window.localStorage.setItem("name", ""); window.localStorage.setItem("connected", ""); // Reset pretext 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) { // Regularly send activity 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, set pretext, output confirmation 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 => { // Return JSON if status is OK 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}'.`}); }); } // 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 => { // Return JSON if status is OK if (res.status === 200) return res.json(); }).then(res => { if (res === undefined) return; // Output usernames 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' }).then(res => { if (res.status !== 200) return; // Output measured ping time 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 => {}); }