123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- /*
- Copyright (c) 2019 Farooq Karimi Zadeh <fkz@riseup.net>
- VM-IRC is under GNU LGPL 3 or at your option any later version and comes
- without any warranty from author(s).
- */
- var nick_ = null, channel = null, sock = null, firsttime = true;
- var quit = false;
- var UTF8 = {
- encode: function(s){
- return unescape(encodeURIComponent(s));
- },
- decode: function(s){
- return decodeURIComponent(escape(s));
- }
- };
- document.onkeydown = function (e) {
- switch (e.key) {
- case "Enter":
- if (sock == null || sock.readyState != "open"){
- alert("socket not ready!");
- break;
- }
- var msg_text = prompt("Message to channel");
- if (msg_text == null)
- break;
- document.getElementById("messages").innerHTML += "<" + nick_ +
- "> " + msg_text + "<br>";
- msg_text = UTF8.encode(msg_text);
- sock.send("PRIVMSG " + channel + " :" + msg_text + "\r\n");
- break;
- case "SoftRight":
- if (sock != null && sock.readyState == "open"){
- quit = true;
- sock.send("QUIT\r\n");
- sock.close();
- }
- window.close();
- break;
- case "SoftLeft":
- var towhom = prompt("To whom?");
- if (towhom == null)
- break;
- var msg_text = prompt("Message to " + towhom);
- if (msg_text == null)
- break;
- document.getElementById("messages").innerHTML += ">" + nick_
- + "< " +msg_text+ "<br>";
- msg_text = UTF8.encode(msg_text);
- sock.send("PRIVMSG " + towhom + " :" + msg_text + "\r\n");
- break;
- }
- }
- window.onload = function (e) {
- if (nick_ == null)
- nick_ = prompt("Nick (max 9 chars)");
- if (channel == null)
- channel = prompt("Channel(just one!)");
- if (!channel.startsWith("#"))
- channel = "#" + channel;
- sock = navigator.mozTCPSocket.open("irc.freenode.net",6697,{useSecureTransport: true});
- sock.onopen = function (e) {
- e.target.send("USER vm-irc vm-irc vm-irc :VM-IRC User\r\n");
- e.target.send("NICK " + nick_ + "\r\n");
- e.target.send("JOIN " + channel + "\r\n");
- }
- sock.ondata = function (e) {
- if (!(typeof(e.data) === "string"))
- return;
- var msgs = e.data.split("\r\n");
- for (let msg of msgs){
- if (msg.startsWith("PING")){
- e.currentTarget.send("PONG " + msg.split(" ")[1] + "\r\n");
- }
- msg = UTF8.decode(msg);
- var token = msg.split(" ", 3);
- var nick = token[0].split("!")[0].replace(":", "");
- if (token[1] === "PRIVMSG"){
- var target = token[2];
- var msg_text = msg.slice(msg.search(" :") + 2, msg.length);
- if (target === channel){
- nick = "<" + nick + ">";
- }else if (target === nick_){
- nick = ">" + nick + "<";
- }
- var text = nick + " " + msg_text + "<br>";
- if (msg_text.search(nick_) != -1 || target === nick_)
- text = "<b>" + text + "<b>";
- /*if (window.Notification.permission === "granted")
- var vibrate_pattern = [200, 100, 200, 100, 200];
- new window.Notification(nick, {body: msg_text,
- vibrate: vibrate_pattern
- });*/
- document.getElementById("messages").innerHTML += text;
- window.scrollByLines(3);
- }
- if (token[1] === "NOTICE"){
- var msg_text = msg.slice(msg.search(" :") + 2, msg.length);
- var text = "-" + nick + "- " + msg_text + "<br>";
- document.getElementById("messages").innerHTML += text;
- window.scrollByLines(3);
- }
- if (token[1] === "JOIN"){
- document.getElementById("messages").innerHTML += nick + " joined.<br>";
- window.scrollByLines(1);
- }
- if (token[1] === "PART"){
- document.getElementById("messages").innerHTML += nick + " left.<br>";
- window.scrollByLines(1);
- }
- if (token[1] === "QUIT"){
- document.getElementById("messages").innerHTML += nick + " has quit.<br>";
- window.scrollByLines(1);
- }
- if (token[1] === "353"){ // Channel user list
- let nicks = msg.replace(/:/g, "").split(" ").slice(5).join(" ");
- if (firsttime)
- document.getElementById("messages").innerHTML += "Users: ";
- document.getElementById("messages").innerHTML += nicks + " ";
- }
- if (token[1] === "366"){
- document.getElementById("messages").innerHTML += "<br>";
- firsttime = true;
- }
- if (token[1] === "332"){ // Reply to TOPIC command
- let topic = msg.slice(msg.replace(":", ".").search(":") + 1, msg.length);
- document.getElementById("messages").innerHTML += "Topic: " +
- topic + "<br>";
- }
- if (token[1] === "433"){ // Nick name already in use
- nick_ += "_";
- e.currentTarget.send("NICK " + nick_ + "\r\n");
- }
- if (token[1] === "NICK"){
- if (nick === nick_)
- nick_ = token[2].replace(":", "");
- else
- document.getElementById("messages").innerHTML += nick + " changed their nick to " + token[2].replace(":", "") + "<br>";
- }
- }
- }
- sock.onclose = function (e) {
- if (!quit){
- window.onload({});
- }
- }
- }
|