app.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright (c) 2019 Farooq Karimi Zadeh <fkz@riseup.net>
  3. VM-IRC is under GNU LGPL 3 or at your option any later version and comes
  4. without any warranty from author(s).
  5. */
  6. var nick_ = null, channel = null, sock = null, firsttime = true;
  7. var quit = false;
  8. var UTF8 = {
  9. encode: function(s){
  10. return unescape(encodeURIComponent(s));
  11. },
  12. decode: function(s){
  13. return decodeURIComponent(escape(s));
  14. }
  15. };
  16. document.onkeydown = function (e) {
  17. switch (e.key) {
  18. case "Enter":
  19. if (sock == null || sock.readyState != "open"){
  20. alert("socket not ready!");
  21. break;
  22. }
  23. var msg_text = prompt("Message to channel");
  24. if (msg_text == null)
  25. break;
  26. document.getElementById("messages").innerHTML += "&lt;" + nick_ +
  27. "&gt; " + msg_text + "<br>";
  28. msg_text = UTF8.encode(msg_text);
  29. sock.send("PRIVMSG " + channel + " :" + msg_text + "\r\n");
  30. break;
  31. case "SoftRight":
  32. if (sock != null && sock.readyState == "open"){
  33. quit = true;
  34. sock.send("QUIT\r\n");
  35. sock.close();
  36. }
  37. window.close();
  38. break;
  39. case "SoftLeft":
  40. var towhom = prompt("To whom?");
  41. if (towhom == null)
  42. break;
  43. var msg_text = prompt("Message to " + towhom);
  44. if (msg_text == null)
  45. break;
  46. document.getElementById("messages").innerHTML += "&gt;" + nick_
  47. + "&lt; " +msg_text+ "<br>";
  48. msg_text = UTF8.encode(msg_text);
  49. sock.send("PRIVMSG " + towhom + " :" + msg_text + "\r\n");
  50. break;
  51. }
  52. }
  53. window.onload = function (e) {
  54. if (nick_ == null)
  55. nick_ = prompt("Nick (max 9 chars)");
  56. if (channel == null)
  57. channel = prompt("Channel(just one!)");
  58. if (!channel.startsWith("#"))
  59. channel = "#" + channel;
  60. sock = navigator.mozTCPSocket.open("irc.freenode.net",6697,{useSecureTransport: true});
  61. sock.onopen = function (e) {
  62. e.target.send("USER vm-irc vm-irc vm-irc :VM-IRC User\r\n");
  63. e.target.send("NICK " + nick_ + "\r\n");
  64. e.target.send("JOIN " + channel + "\r\n");
  65. }
  66. sock.ondata = function (e) {
  67. if (!(typeof(e.data) === "string"))
  68. return;
  69. var msgs = e.data.split("\r\n");
  70. for (let msg of msgs){
  71. if (msg.startsWith("PING")){
  72. e.currentTarget.send("PONG " + msg.split(" ")[1] + "\r\n");
  73. }
  74. msg = UTF8.decode(msg);
  75. var token = msg.split(" ", 3);
  76. var nick = token[0].split("!")[0].replace(":", "");
  77. if (token[1] === "PRIVMSG"){
  78. var target = token[2];
  79. var msg_text = msg.slice(msg.search(" :") + 2, msg.length);
  80. if (target === channel){
  81. nick = "&lt;" + nick + "&gt;";
  82. }else if (target === nick_){
  83. nick = "&gt;" + nick + "&lt;";
  84. }
  85. var text = nick + " " + msg_text + "<br>";
  86. if (msg_text.search(nick_) != -1 || target === nick_)
  87. text = "<b>" + text + "<b>";
  88. /*if (window.Notification.permission === "granted")
  89. var vibrate_pattern = [200, 100, 200, 100, 200];
  90. new window.Notification(nick, {body: msg_text,
  91. vibrate: vibrate_pattern
  92. });*/
  93. document.getElementById("messages").innerHTML += text;
  94. window.scrollByLines(3);
  95. }
  96. if (token[1] === "NOTICE"){
  97. var msg_text = msg.slice(msg.search(" :") + 2, msg.length);
  98. var text = "-" + nick + "- " + msg_text + "<br>";
  99. document.getElementById("messages").innerHTML += text;
  100. window.scrollByLines(3);
  101. }
  102. if (token[1] === "JOIN"){
  103. document.getElementById("messages").innerHTML += nick + " joined.<br>";
  104. window.scrollByLines(1);
  105. }
  106. if (token[1] === "PART"){
  107. document.getElementById("messages").innerHTML += nick + " left.<br>";
  108. window.scrollByLines(1);
  109. }
  110. if (token[1] === "QUIT"){
  111. document.getElementById("messages").innerHTML += nick + " has quit.<br>";
  112. window.scrollByLines(1);
  113. }
  114. if (token[1] === "353"){ // Channel user list
  115. let nicks = msg.replace(/:/g, "").split(" ").slice(5).join(" ");
  116. if (firsttime)
  117. document.getElementById("messages").innerHTML += "Users: ";
  118. document.getElementById("messages").innerHTML += nicks + " ";
  119. }
  120. if (token[1] === "366"){
  121. document.getElementById("messages").innerHTML += "<br>";
  122. firsttime = true;
  123. }
  124. if (token[1] === "332"){ // Reply to TOPIC command
  125. let topic = msg.slice(msg.replace(":", ".").search(":") + 1, msg.length);
  126. document.getElementById("messages").innerHTML += "Topic: " +
  127. topic + "<br>";
  128. }
  129. if (token[1] === "433"){ // Nick name already in use
  130. nick_ += "_";
  131. e.currentTarget.send("NICK " + nick_ + "\r\n");
  132. }
  133. if (token[1] === "NICK"){
  134. if (nick === nick_)
  135. nick_ = token[2].replace(":", "");
  136. else
  137. document.getElementById("messages").innerHTML += nick + " changed their nick to " + token[2].replace(":", "") + "<br>";
  138. }
  139. }
  140. }
  141. sock.onclose = function (e) {
  142. if (!quit){
  143. window.onload({});
  144. }
  145. }
  146. }