-
-
Save matheusfillipe/6cc6e1076a503e7bf5f474014120ac77 to your computer and use it in GitHub Desktop.
Minimal javascript IRC client in console
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function irc(nickname, server, chan, onmsg, onjoin) { | |
nickname = nickname || "nick_" + new Date().getTime().toString(36) + new Date().getMilliseconds().toString(36) | |
chan = chan || "Z" + new Date().getTime().toString(18) + Math.random().toString(36).substring(2) | |
server = server || "irc.unrealircd.org" | |
var init = 0 | |
var ws = new WebSocket("wss://" + server); | |
var s = (c,l)=>setTimeout(console.log.bind(this, "%c%s", "font-size: 14px; color:" + c, new Date().toLocaleString("it") + ": " + l)) | |
ws.onmessage = m=>{ | |
if (m.data.indexOf("PING") == 0) | |
ws.send(m.data.replace("PI", "PO")); | |
else | |
if (irc.DEBUG) s("green", m.data) | |
if ((init == 0) && (m.data.split(" ")[1] == "376")) { | |
init = 1; | |
ws.send("join #" + chan); | |
s("orange", "CONNECTED") | |
/* callback goes here */ | |
} | |
if (init == 1) { | |
if (m.data.split(" ")[1] == "PRIVMSG") { | |
//s("blue",m.data.split(" ")[2]+"> "+m.data.split(" ").splice(3).join(" ").substring(1)) | |
onmsg && (onmsg(m.data.split(":")[1].split("!")[0], m.data.split(" ").splice(3).join(" ").substring(1))) || s("blue", m.data.split(":")[1].split("!")[0] + "> " + m.data.split(" ").splice(3).join(" ").substring(1)) | |
} else if (m.data.split(" ")[1] == "JOIN") { | |
//s("blue",m.data.split(" ")[2]+"> "+m.data.split(" ").splice(3).join(" ").substring(1)) | |
onjoin && (onjoin(m.data.split(":")[1].split("!")[0], m.data.split(" ").splice(3).join(" ").substring(1))) || s("blue", m.data.split(":")[1].split("!")[0] + " joined.") | |
} else if (m.data.split(" ")[1] == "353") { | |
if (m.data.split(":")[2][0] == "@") { | |
ws.send("mode #" + chan + " +s"); | |
} | |
} | |
} | |
} | |
ws.onerror = e=>{ | |
s("red", "ERROR"); | |
fetch("https://"+server).then(c=>c.text()) | |
} | |
ws.onclose = e=>{ | |
s("red", "CONNECTION CLOSED"); | |
setTimeout((function() { | |
init = 0; | |
var ws2 = new WebSocket(ws.url); | |
ws2.onopen = ws.onopen; | |
ws2.onmessage = ws.onmessage; | |
ws2.onclose = ws.onclose; | |
ws2.onerror = ws.onerror; | |
ws2.f = ws.f; | |
ws = ws2 | |
} | |
).bind(this), 5000) | |
} | |
ws.onopen = e=>{ | |
ws.send("user websocket * * :WebSocket User"); | |
ws.send("nick " + nickname) | |
} | |
ws.f = (message,nickorchan)=>ws.send("PRIVMSG " + (nickorchan || "#" + chan) + " :" + (message || nickorchan)) | |
ws.f.j = m=>m && ((chan = m) && (ws.send("JOIN #" + m))) || chan | |
ws.f.l = m=>(ws.send("PART #" + chan + (m?" :"+m:""))) | |
ws.f.q = m=>{ | |
if (m) | |
ws.send(m); | |
else | |
ws.send("HELP") | |
} | |
ws.f.d = ()=>ws.close() | |
ws.f.nick = nickname | |
ws.f.w = (m)=>ws.send("WHO #" + (m || chan)) | |
ws.f.wi = (m)=>ws.send("WHOIS " + (m || nickname)) | |
return ws.f | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment