Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.19 KB | None | 0 0
  1. /*
  2. *
  3. * Controller component
  4. * Handles communication with server and login
  5. * Three main areas of the controller: Authentication, Messages to server, Messages from server
  6. *
  7. */
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14. /*
  15. *
  16. * Main variables used for website
  17. *
  18. */
  19. var ws;
  20. var http = "http:%2F%2F";
  21. var hostname = "ec2-122-248-200-91.ap-southeast-1.compute.amazonaws.com";
  22. //var hostname = "localhost";
  23. var port = "80";
  24. var VM;
  25. var pingVariable; // To be able to stop ping messages on disconnect
  26. var pingFunction = function(x){ // Looping function to send ping to server
  27. pingVariable = setTimeout(function(){
  28. sendping();
  29. pingFunction(x);
  30. },x)
  31. };
  32. var pingPeriod = 120*1000;
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. /*
  40. *
  41. * Authentication
  42. *
  43. */
  44.  
  45. // Initialisation of webpage
  46. $(document).ready(function () {
  47. // Create VM
  48. initUI();
  49. VM = new HangoutViewModel();
  50. ko.applyBindings(VM);
  51.  
  52. // Try get token
  53. var token = getParameterByName("token");
  54. if (token != undefined){
  55. setCookie("token",token,365);
  56. setCookie("tokenvalid","1",365);
  57. window.location.replace("chat.html");
  58. }
  59. // Try get cookie
  60. else {
  61. token = getCookie("token");
  62. tokenvalid = getCookie("tokenvalid");
  63. if (token != undefined && tokenvalid != "0"){
  64. auth("auth", token);
  65. }
  66. }
  67. });
  68.  
  69.  
  70. // Function to get query GET parameter for ivle token
  71. function getParameterByName(name){
  72. name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  73. var regexS = "[\\?&]" + name + "=([^&#]*)";
  74. var regex = new RegExp(regexS);
  75. var results = regex.exec(window.location.search);
  76. if(results == null)
  77. return undefined;
  78. else
  79. return decodeURIComponent(results[1].replace(/\+/g, " "));
  80. }
  81.  
  82.  
  83. // To store cookie, used for ivle token
  84. function getCookie(cookiename){
  85. var i,x,y,ARRcookies=document.cookie.split(";");
  86. for (i=0;i<ARRcookies.length;i++)
  87. {
  88. x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  89. y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  90. x=x.replace(/^\s+|\s+$/g,"");
  91. if (x==cookiename){
  92. return unescape(y);
  93. }
  94. }
  95. return undefined;
  96. }
  97.  
  98.  
  99. // To set cookie, used for ivle token
  100. function setCookie(cookiename,value,exdays){
  101. var exdate=new Date();
  102. exdate.setDate(exdate.getDate() + exdays);
  103. var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
  104. document.cookie=cookiename + "=" + c_value;
  105. }
  106.  
  107.  
  108. function initSize(){
  109. // Set viewport
  110. var userWidth = $(window).width();
  111. var userHeight = $(window).height();
  112.  
  113. document.getElementById("document-area").style.height = (userHeight - 260)+"px";
  114. document.getElementById("channels-bar-area").style.width = (userWidth - 680)+"px";
  115. document.getElementById("chat-area").style.width = (userWidth - 680-20)+"px";
  116. document.getElementById("chat-area").style.height = (userHeight - 165)+"px";
  117. document.getElementById("chatbox-input").style.width = (userWidth - 680-20)+"px";
  118. document.getElementById("joined-area").style.height = (userHeight - 380)+"px";
  119. document.getElementById("channels-bar-items-id").style.width = (userWidth - 680-100)+"px";
  120. }
  121.  
  122. // Any UI initialisation needed
  123. function initUI(){
  124.  
  125. // Dimensions based on screen
  126. window.onresize = function(event) {
  127. initSize();
  128. };
  129. initSize();
  130.  
  131. // Set enter to send message
  132. $('#chatbox-input').keydown(function(e){
  133. if (e.keyCode == 13) {
  134. sendchat();
  135. }});
  136.  
  137.  
  138. // Right click to close channel search window
  139. $('#search-channel-popup').bind('contextmenu', function(e){
  140. e.preventDefault();
  141. document.getElementById('search-channel-popup').style.display = 'none';
  142. });
  143.  
  144.  
  145. // Enter to search channels
  146. $('#search-channel-input').keydown(function(e){
  147. if (e.keyCode == 13) {
  148. searchchannel($("#search-channel-input").val());
  149. }});
  150.  
  151.  
  152. // Right click to close user search window
  153. $('#search-user-popup').bind('contextmenu', function(e){
  154. e.preventDefault();
  155. document.getElementById('search-user-popup').style.display = 'none';
  156. });
  157.  
  158.  
  159. // Enter to search users
  160. $('#search-user-input').keydown(function(e){
  161. if (e.keyCode == 13) {
  162. searchuser($("#search-user-input").val());
  163. }
  164. });
  165.  
  166.  
  167. // Enter to create/join channels
  168. $('#create-channel-input').keydown(function(e){
  169. if (e.keyCode == 13) {
  170. requestjoin($("#create-channel-input").val());
  171. closeSearchChannelPopup();
  172. }
  173. });
  174.  
  175.  
  176. // Enter to create/join channels
  177. $('#nickname-input').keydown(function(e){
  178. if (e.keyCode == 13) {
  179. var newnickname = $("#nickname-input").val();
  180. //$("#nickname-input").val(VM.selfnickname());
  181. document.getElementById("nickname-input").blur();
  182. changenickname(newnickname);
  183. }
  184. });
  185.  
  186.  
  187. // Right click to close channel options
  188. $('#channel-options-popup').bind('contextmenu', function(e){
  189. e.preventDefault();
  190. document.getElementById('channel-options-popup').style.display = 'none';
  191. });
  192. }
  193.  
  194.  
  195.  
  196.  
  197.  
  198. /*
  199. *
  200. * Messages to server
  201. *
  202. */
  203.  
  204. // Send chat, privchat to server (Deprecated, pelase remove soon)
  205. function sendchat() {
  206. var text = $("#chatbox-input").val();
  207. if (text[0] == '/')
  208. ws.send(text.substring(1));
  209. else if (VM.selectedPrivateChat() != undefined)
  210. ws.send("user,sendprivchat,"+VM.selectedPrivateChat().user().ivleid+","+text);
  211. else if (VM.selectedChannel != undefined)
  212. ws.send("channel,sendchat,"+VM.selectedChannel.name+","+text);
  213. else
  214. alert("Invalid command. Please prefix with \"/\".");
  215. $("#chatbox-input").val("");
  216. }
  217.  
  218. // Send raw command to server
  219. function sendcommand(command){
  220. ws.send(command);
  221. }
  222.  
  223.  
  224. // Request to send a chat to a channel
  225. function sendchattrial(channame, text){
  226. ws.send("channel,sendchat,"+channame+","+text);
  227. }
  228.  
  229.  
  230. // Request to register a channel
  231. function registerchannel(channame){
  232. ws.send("channel,registerchannel,"+channame);
  233. }
  234.  
  235.  
  236. // Request to deregister a channel
  237. function deregisterchannel(channame){
  238. ws.send("channel,deregisterchannel,"+channame);
  239. }
  240.  
  241.  
  242. // Request to join a channel
  243. function requestjoin(channame){
  244. ws.send("channel,requestjoin,"+channame);
  245. }
  246.  
  247.  
  248. // Request to leave a channel
  249. function requestleave(channame){
  250. ws.send("channel,requestleave,"+channame);
  251. }
  252.  
  253.  
  254. // Request to invite a user to a channel
  255. function requestinvite(channame, ivleid){
  256. ws.send("channel,requestinvite,"+channame+","+ivleid);
  257. }
  258.  
  259.  
  260. // Request to kick a user in a channel
  261. function requestkick(channame, ivleid){
  262. ws.send("channel,requestkick,"+channame+","+ivleid);
  263. }
  264.  
  265.  
  266. // Request to change topic of a channel
  267. function changetopic(channame, topic){
  268. ws.send("channel,changetopic,"+channame+","+topic);
  269. }
  270.  
  271.  
  272. // Request to set a channel's privacy
  273. function setprivacy(channame, publicBit){
  274. if (publicBit)
  275. ws.send("channel,setprivacy,"+channame+",public");
  276. else
  277. ws.send("channel,setprivacy,"+channame+",private");
  278. }
  279.  
  280.  
  281. // Request to set access of a user of a channel
  282. function setaccess(channame, ivleid, level){
  283. ws.send("channel,setaccess,"+channame+","+ivleid+","+level);
  284. }
  285.  
  286.  
  287. // Set permissions of a channel
  288. function setpermissions(channame, permissions){
  289. ws.send("channel,setpermissions,"+channame+","+JSON.stringify(permissions));
  290. }
  291.  
  292.  
  293. // Search a channel with a query string
  294. function searchchannel(query){
  295. ws.send("channel,searchchannel,"+query);
  296. }
  297.  
  298.  
  299. // Change user's nickname
  300. function changenickname(nickname){
  301. ws.send("user,changenickname,"+nickname);
  302. }
  303.  
  304.  
  305. // Mute a user
  306. function addmute(ivleid){
  307. ws.send("user,addmute,"+ivleid);
  308. }
  309.  
  310.  
  311. // Unmute a user
  312. function unmute(ivleid){
  313. ws.send("user,removemute,"+ivleid);
  314. }
  315.  
  316.  
  317. // Request to send a private chat to a user
  318. function sendprivchat(ivleid, text){
  319. ws.send("user,sendprivchat,"+ivleid+","+text);
  320. }
  321.  
  322.  
  323. // Adds a channel to autojoin list
  324. function addautojoin(channame){
  325. ws.send("user,addautojoin,"+channame);
  326. }
  327.  
  328.  
  329. // Deletes a channel from autojoin list
  330. function deleteautojoin(channame){
  331. ws.send("user,deleteautojoin,"+channame);
  332. }
  333.  
  334.  
  335. // Logout
  336. function logout(){
  337. ws.send("user,sessionclose");
  338. }
  339.  
  340.  
  341. // Search for a user by nickname
  342. function searchuser(query){
  343. ws.send("user,searchuser,"+query);
  344. }
  345.  
  346.  
  347. // Request for specific user's info
  348. function getuserinfo(ivleid){
  349. ws.send("user,getuserinfo,"+ivleid);
  350. }
  351.  
  352.  
  353. // Send raw command to server
  354. function sendping(){
  355. ws.send("ping");
  356. }
  357.  
  358.  
  359. // Send auth command to server
  360. function auth(type, key){
  361. ws = new WebSocket("ws://"+ hostname +":8888/socket");
  362. ws.onopen = function() {
  363. ws.send("auth,"+type+","+key);
  364. };
  365. ws.onmessage = function(event) {
  366. handleMessage(event.data);
  367. };
  368. ws.onclose = function(event) {
  369. handleMessage(event.data);
  370. };
  371. }
  372.  
  373.  
  374.  
  375.  
  376.  
  377. /*
  378. *
  379. * Messages from server
  380. * Most of them are used to edit the ViewModel's data
  381. *
  382. */
  383.  
  384. function handleMessage(message){
  385. try{
  386.  
  387. // Handle invalid login
  388. if (message == "Invalid login."){
  389. setCookie("token","",365);
  390. setCookie("tokenvalid","0",365);
  391. alert(message);
  392. return;
  393. }
  394.  
  395.  
  396. // Log all messages
  397. console.log(message);
  398.  
  399.  
  400. // Prepare message with standard format
  401. comma = message.indexOf(",");
  402. var commandType = message.substring(0, comma);
  403. message = message.substring(comma+1, message.length);
  404.  
  405. comma2 = message.indexOf(",");
  406. if (comma2 == -1)
  407. comma2 = message.length;
  408. var command = message.substring(0, comma2);
  409.  
  410. var args;
  411. if (comma2+1 < message.length)
  412. args = message.substring(comma2+1, message.length);
  413. else
  414. args = "";
  415.  
  416.  
  417. // Case of user type
  418. if (commandType == "user"){
  419. switch (command)
  420. {
  421. case "userinfo":
  422. var user = JSON.parse(args);
  423. VM.updateUserInfo(user);
  424. break;
  425.  
  426. case "changednickname":
  427. args = args.split(",");
  428. VM.updateUserNickname(args[0],args[1],true);
  429. break;
  430.  
  431. case "addedautojoin":
  432. VM.addedAutojoin(args);
  433. break;
  434.  
  435. case "deletedautojoin":
  436. VM.deletedAutojoin(args);
  437. break;
  438.  
  439. case "muted":
  440. VM.addedMute(args);
  441. break;
  442.  
  443. case "removedmute":
  444. VM.removedMute(args);
  445. break;
  446.  
  447. case "privchat":
  448. args = args.split(",");
  449. VM.addPrivateChat(args[0], args[1], args[2]);
  450. break;
  451.  
  452. case "searcheduser":
  453. args = JSON.parse(args);
  454. VM.receiveSearchedUser(args);
  455. break;
  456.  
  457.  
  458. case "sessionclose":
  459. setCookie("token","",365);
  460. location.reload(true);
  461. break;
  462.  
  463. default:
  464. break;
  465. }
  466. }
  467. // Case of channel type
  468. else if (commandType == "channel"){
  469. switch (command)
  470. {
  471. case "chat":
  472. var split = args.indexOf(",");
  473. var channame = args.substring(0,split);
  474. args = args.substring(split+1);
  475. var split2 = args.indexOf(",");
  476. var ivleid = args.substring(0,split2);
  477. if (channame == "drawsomething" && ivleid != VM.selfivleid()){
  478. var draw = JSON.parse(args.substring(split2+1));
  479. receiveArrays(draw.x, draw.y);
  480. }
  481. else
  482. VM.addChannelMessage(channame, ivleid, args.substring(split2+1));
  483. break;
  484.  
  485. case "join":
  486. var split = args.indexOf(",");
  487. var channame = args.substring(0,split);
  488. var user = JSON.parse(args.substring(split+1,args.length));
  489. VM.updateUserInfo(user);
  490. VM.addJoined(channame, user.ivleid);
  491. break;
  492.  
  493. case "leave":
  494. args = args.split(",");
  495. if (args[2] == "undefined")
  496. args[2] = "leave";
  497. VM.removeJoined(args[0], args[1], args[2]);
  498. break;
  499.  
  500. case "joinedchannel":
  501. joinedchannel = JSON.parse(args);
  502. VM.addChannel(joinedchannel);
  503. break;
  504.  
  505. case "topic":
  506. var split = args.indexOf(",");
  507. var channame = args.substring(0,split);
  508. args = args.substring(split+1,args.length);
  509. var split2 = args.indexOf(",");
  510. var ivleid = args.substring(0,split2);
  511. VM.setTopic(channame, ivleid, args.substring(split2+1,args.length));
  512. break;
  513.  
  514. case "permissions":
  515. var split = args.indexOf(",");
  516. var channame = args.substring(0,split);
  517. var permissions = JSON.parse(args.substring(split+1,args.length));
  518. VM.setPermissions(channame, permissions);
  519. break;
  520.  
  521. case "setaccess":
  522. args = args.split(",");
  523. VM.setACL(args[0], args[1], args[2]);
  524. break;
  525.  
  526. case "registered":
  527. VM.setRegistered(args);
  528. break;
  529.  
  530. case "deregistered":
  531. VM.setDeregistered(args);
  532. break;
  533.  
  534. case "privacy":
  535. args = args.split(",");
  536. VM.setPrivacy(args[0], args[1]);
  537. break;
  538.  
  539. case "invite":
  540. args = args.split(",");
  541. VM.receiveInvited(args[0], args[1], args[2], args[3]);
  542. break;
  543.  
  544. case "invited":
  545. args = args.split(",");
  546. VM.receiveInvite(args[0], args[1], args[2]);
  547. break;
  548.  
  549. case "kicked":
  550. args = args.split(",");
  551. VM.receiveKicked(args[0], args[1], args[2]);
  552. break;
  553.  
  554. case "searchedchannel":
  555. args = JSON.parse(args);
  556. VM.receiveSearchedChannel(args);
  557. break;
  558.  
  559. default:
  560. break;
  561. }
  562. }
  563. // Case of notices (Needs to change according to UI)
  564. else if (commandType == "notice"){
  565. alert(args);
  566. }
  567. // First login message (Needs to change according to UI)
  568. else if (commandType == "welcome"){
  569. switch (command)
  570. {
  571. case "userinfo":
  572. $("#pre-login").css('display','none');
  573. setCookie("tokenvalid","0",365);
  574. pingFunction(pingPeriod);
  575. args = JSON.parse(args);
  576. VM.updateUserInfo(args);
  577. VM.selfivleid(args.ivleid);
  578. VM.selfnickname(args.nickname);
  579. VM.selffullname(args.fullname);
  580. VM.selfemail(args.email);
  581. break;
  582. case "userpreference":
  583. args = JSON.parse(args);
  584. VM.myMute = args.mute;
  585. VM.myAutojoin = args.autojoin;
  586. break;
  587. case "availablelevels":
  588. VM.availablerolesMap = JSON.parse(args);
  589. for (var p in VM.availablerolesMap){
  590. VM.availablelevels.push(p);
  591. VM.availablelevelsMap[VM.availablerolesMap[p]] = p;
  592. }
  593. break;
  594. case "availablepermissions":
  595. var arr = JSON.parse(args);
  596. for (var p in arr)
  597. VM.availablepermissions.push(p);
  598. break;
  599. default:
  600. break;
  601. }
  602. }
  603. } catch (err) {
  604. // Exception
  605. alert(err+"\n"+message+"\n"+"Connection ended.");
  606. clearTimeout(pingVariable);
  607. }
  608. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement