-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc.html
82 lines (82 loc) · 3.48 KB
/
irc.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<html>
<head><title>IRC client</title></head>
<body><table width="100%" height="100%" style="border:0; padding:0; margin:0;">
<tr height="100%"><td colspan="2"><textarea id="irc" name="irc" readonly style="resize:none; width:100%; height:100%;"></textarea></td></tr>
<tr height="20"><td width="100%"><input id="msg" name="msg" type="text" style="width:100%; height:100%;" autocomplete="off" autofocus disabled></td><td width="20"><button id="send" name="send" type="button">Send</td></tr>
</table></body>
<script>
var socket = connect();
var nick = "guest"+Math.floor(Math.random()*100000);
var joined = false;
if (document.cookie!=="") {
cookies = document.cookie;
console.log("cookies: ",cookies);
cookielist = cookies.split(";");
for (var i=0;i<cookielist.length;i++) {
cookieparts = cookielist[i].split("=");
namepart = cookieparts[0].trim();
nickpart = cookieparts[1].trim();
if (namepart==="nick") {
nick = nickpart;
console.log("cookie nick: ",nick);
}
}
}
function submitMessage() {
msgtext = document.getElementById("msg").value;
cmdtext = "";
if (msgtext.startsWith("/")) {
cmdtext = msgtext.substring(1);
} else if (msgtext.trim().length>0) {
document.getElementById("irc").value += "(#public) " + nick+": "+msgtext+"\n";
irc.scrollTop = irc.scrollHeight;
cmdtext = "privmsg #public :"+msgtext;
}
if (cmdtext.length>0) { sendMessage(cmdtext); }
document.getElementById("msg").value = "";
}
msg.addEventListener("keyup", function(event) { if (event.key === "Enter") {submitMessage();} });
send.onclick = function(){ submitMessage(); };
function connect() {
console.log("Connecting."); joined = false;
newsocket = new WebSocket('wss://localhost/wsirc');
newsocket.onopen = function(event) { console.log("Connection open."); sendMessage("nick "+nick); sendMessage("user "+nick+" ws ws: "+nick+""); msg.disabled = false; msg.focus(); };
newsocket.onmessage = function(event) {
reader = new FileReader();
reader.onload = function(e) {
messagetext = reader.result;
console.log("Message from server: ", messagetext);
if (messagetext!==null) {
if (messagetext.toLowerCase().startsWith("ping :")) {
sendMessage("pong :"+messagetext.substring(6));
if (!joined) { joined = true; sendMessage("join #public"); }
} else {
msgparts = messagetext.split(" ");
origpart = msgparts[0].substring(1).trim();
actpart = msgparts[1].trim();
targetpart = msgparts[2].trim();
if (actpart.toLowerCase()==="privmsg") {
usrpart = origpart.substring(0,origpart.indexOf("!"));
msgtext = messagetext.substring(messagetext.toLowerCase().indexOf(targetpart.toLowerCase())+targetpart.length+2);
document.getElementById("irc").value += "(" +targetpart+ ") " + usrpart + ": " + msgtext;
irc.scrollTop = irc.scrollHeight;
} else if (actpart.toLowerCase()==="nick") {
if (origpart===nick) {
nick = targetpart.substring(1).trim();
document.cookie = "nick="+nick+"; expires=Thu, 6 Dec 2999 12:00:00 UTC";
}
}
}
}
};
reader.readAsText(event.data);
};
newsocket.onclose = function(event) { console.log("Connection close."); msg.disabled = true; socket = connect(); };
return newsocket;
}
function sendMessage(message) {
const blob = new Blob([message+"\n"], {type: '' }); socket.send(blob);
console.log("Message to server: ", message);
}
</script>
</html>