Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var newHead = '<style>\
  2. #mainContainer {\
  3.    width:100%;\
  4.    height:100%;\
  5.    overflow: hidden;\
  6.    position: absolute;\
  7.    left: 0px;\
  8.    top: 0px;\
  9. }\
  10. .sexyWindow {\
  11.  height: 100%;\
  12.  width: 100%;\
  13.  overflow-y: scroll;\
  14.  font-family: monospace;\
  15. }\
  16. \
  17. #windowSelector {\
  18.  position: absolute;\
  19.  width: 340px;\
  20.  right:32px;\
  21.  top: 4px;\
  22.  border: 1px solid #000;\
  23.  background: #fff7ee;\
  24.  padding:4px;\
  25. }\
  26. #msgLog {\
  27.    height:100%;\
  28.    width:100%;\
  29. }\
  30. \
  31. #chat {\
  32.  position: absolute;\
  33.  right: 32px;\
  34.  bottom: 4px;\
  35.  border: 1px solid #000;\
  36.  padding: 4px;\
  37. }\
  38. </style>';
  39.  
  40. var newRoot = '\
  41. <div id="mainContainer">\
  42.    <div id="msgLog"></div>\
  43.    <div id="windowSelector"><b>Windows</b><br></div>\
  44.    <div id="chat"></div>\
  45. </div>';
  46.  
  47. // Ensure we have jquery
  48. var jQuery = null;
  49. function tryToLoad() {
  50.     if(jQuery == null) {
  51.         setTimeout(tryToLoad, 100);
  52.         return;
  53.     }
  54.  
  55.     doit();
  56. }
  57. tryToLoad();
  58.  
  59. // Include jquery
  60. var s = document.createElement("script");
  61. s.type="text/javascript";
  62. s.src="http://code.jquery.com/jquery-1.11.2.min.js";
  63. document.body.appendChild(s);
  64.  
  65. var ourClient;
  66. var server = 'bee1.shamchat.com';
  67. function heyItsMeTheIFrame(server, client) {
  68.     ourClient = client;
  69. }
  70.  
  71. var sendCapcha;
  72.  
  73. function doit() {
  74.     // Ensure no conflicts
  75.     jQuery.noConflict();
  76.  
  77.     jQuery(document.documentElement).children().each(function() {
  78.         jQuery(this).hide();
  79.     });
  80.     jQuery(document.documentElement).html(newRoot);
  81.     jQuery('body').append(newHead);
  82.  
  83.     var chat = jQuery('#chat');
  84.     chat.append(jQuery('<input>').attr('type','input').attr('id', 'chatClientID'));
  85.     chat.append(jQuery('<input>').attr('type','input').attr('id', 'chatClientMSG').on('keydown', function(e) {
  86.         if (e.which == 13) {
  87.             var toSend = jQuery('#chatClientID').val();
  88.             var msg = jQuery('#chatClientMSG').val();
  89.             jQuery('#chatClientMSG').val('');
  90.  
  91.             if(msg != '') {
  92.                 sendMessage(toSend, msg);
  93.             }
  94.         }
  95.     }));
  96.     chat.append(jQuery('<input>').attr('type','submit').click(function() {
  97.         var toSend = jQuery('#chatClientID').val();
  98.         var msg = jQuery('#chatClientMSG').val();
  99.         jQuery('#chatClientMSG').val('');
  100.  
  101.         if(msg != '') {
  102.             sendMessage(toSend, msg);
  103.         }
  104.     }));
  105.  
  106.     // Load a client
  107.     $(document.body).grab(new Element("iframe", {
  108.         src: "http://" + server + "/iframe.html",
  109.         width: 0,
  110.         height: 0,
  111.         frameBorder: 0,
  112.         style: "display:none;"
  113.     }));
  114.  
  115.     var windows = {};
  116.     function newWindow(name) {
  117.         windows[name] = jQuery('<div class="sexyWindow">');
  118.  
  119.         var con = jQuery('<span>');
  120.  
  121.         var clicker = jQuery('<a>').attr('href','#').click(function() {
  122.             selectWindow(name);
  123.         }).text(name);
  124.  
  125.         if(name != 'main') {
  126.             con.append(jQuery('<a>').attr('href','#').click(function() {
  127.                 con.remove();
  128.             }).text('X'));
  129.         }
  130.  
  131.         con.append(' ');
  132.  
  133.         jQuery('#windowSelector').append(con);
  134.         con.append(clicker);
  135.         con.append('<br>');
  136.  
  137.         if(name != 'main') {
  138.             listenToChat(name);
  139.         }
  140.     }
  141.     newWindow('main');
  142.     log('Click someone to watch their conversation.<br>');
  143.  
  144.     function selectWindow(name) {
  145.         if(!windows[name]) {
  146.             newWindow(name);
  147.         }
  148.  
  149.         jQuery('#msgLog').empty();
  150.         jQuery('#msgLog').append(windows[name]);
  151.     }
  152.     selectWindow('main');
  153.  
  154.     // Tabs out names
  155.     function sexyName(name) {
  156.         if(name == null) name = '';
  157.  
  158.         return ' (<pre style="display:inline-block;width:100px;overflow:hidden;margin:0px;padding:0px;">' + name + '</pre>)';
  159.     }
  160.  
  161.     // Closes a tab
  162.     function closeTab(name) {
  163.         if(windows[name]) {
  164.             windows[name].dispose();
  165.         }
  166.  
  167.         for(var key in listenToID) {
  168.             if(listenToID[key] == name) {
  169.                 delete listenToID[key];
  170.             }
  171.         }
  172.     }
  173.  
  174.     function log(msg, window) {
  175.         if(!window) window = 'main';
  176.  
  177.         // Grab it
  178.         var div = windows[window];
  179.  
  180.         if(!div) {
  181.             console.log('Failed to find window: '+window);
  182.             console.log(msg);
  183.             return;
  184.         }
  185.  
  186.         // Scroll detection
  187.         var shouldScroll = false;
  188.         if(div.scrollTop() + div.innerHeight() + 10 >= div.prop('scrollHeight')) {
  189.             shouldScroll = true;
  190.         }
  191.  
  192.         // Add message
  193.         div.append(msg);
  194.  
  195.         // Scroll to the bottom:
  196.         if(shouldScroll) {
  197.             div.scrollTop(div.prop('scrollHeight'));
  198.         }
  199.     }
  200.  
  201.     sendCapcha = function(client) {
  202.         // Ensure we have a client
  203.         if(!ourClient) return;
  204.  
  205.         // Pack ans send message
  206.         offWithItsHeader(new ourClient.Request({
  207.             url: getRequestURL(server, "/recaptcha"),
  208.             data: {
  209.                 id: client,
  210.                 challenge: 'lol',
  211.                 response: 'lol'
  212.             }
  213.         })).send();
  214.     };
  215.  
  216.     sendPair = function(client) {
  217.         // Ensure we have a client
  218.         if(!ourClient) return;
  219.  
  220.         // Pack ans send message
  221.         offWithItsHeader(new ourClient.Request({
  222.             url: getRequestURL(server, "/pair"),
  223.             data: {
  224.                 id: client
  225.             }
  226.         })).send();
  227.     };
  228.  
  229.     function sendMessage(client, msg) {
  230.         // Ensure we have a client
  231.         if(!ourClient) return;
  232.  
  233.         // Pack ans send message
  234.         offWithItsHeader(new ourClient.Request({
  235.             url: getRequestURL(server, "/send"),
  236.             data: {
  237.                 msg: msg,
  238.                 id: client
  239.             }
  240.         })).send();
  241.     };
  242.  
  243.     function sendDisconnect(client) {
  244.         // Ensure we have a client
  245.         if(!ourClient) return;
  246.  
  247.         // Pack ans send message
  248.         offWithItsHeader(new ourClient.Request({
  249.             url: getRequestURL(server, "/disconnect"),
  250.             data: {
  251.                 id: client
  252.             }
  253.         })).send();
  254.     };
  255.  
  256.     function makeid() {
  257.         var text = "";
  258.         var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  259.  
  260.         for( var i=0; i < 10; i++ )
  261.             text += possible.charAt(Math.floor(Math.random() * possible.length));
  262.  
  263.         return text;
  264.     }
  265.  
  266.     // Searches for a peer
  267.     var neededPeers = {};
  268.     function findPeer(clientID, callback) {
  269.         var ourID = makeid();
  270.         neededPeers[ourID] = {
  271.             clientID: clientID,
  272.             callback: callback
  273.         }
  274.         sendMessage(clientID, ourID);
  275.     }
  276.  
  277.     var listenToID = {};
  278.     function listenTo(clientID) {
  279.         if(clientID) {
  280.             listenToID[clientID] = [clientID];
  281.  
  282.             log('Listening to ', clientID);
  283.             log(clickableID2(clientID), clientID);
  284.             log('<br>', clientID);
  285.         }
  286.     }
  287.  
  288.     function listenToChat(clientID) {
  289.         if(clientID) {
  290.             listenToID[clientID] = clientID;
  291.  
  292.             // Locate their partner
  293.             findPeer(clientID, function(np, theirID) {
  294.                 log('Listening to ', clientID);
  295.                 log(clickableID2(clientID), clientID);
  296.                 log('\'s partner: ', clientID)
  297.                 log(clickableID2(theirID), clientID);
  298.                 log('<br>', clientID);
  299.                 listenToID[theirID] = clientID;
  300.             });
  301.  
  302.             log('Click someone to send that person a message.<br>', clientID);
  303.             log('Listening to ', clientID);
  304.             log(clickableID2(clientID), clientID);
  305.             log('<br>', clientID);
  306.         }
  307.     }
  308.  
  309.     function clickableID(clientID) {
  310.         return jQuery('<a>').attr('href','#').text(clientID).click(function() {
  311.             selectWindow(clientID);
  312.         });
  313.     }
  314.  
  315.     function clickableID2(clientID) {
  316.         return jQuery('<a>').attr('href','#').text(clientID).click(function() {
  317.             jQuery('#chatClientID').val(clientID);
  318.             jQuery('#chatClientMSG').focus();
  319.         });
  320.     }
  321.  
  322.     // Maps IDs to names
  323.     characterMap = {};
  324.  
  325.     var ourFaye = new Faye.Client('http://bee1.shamchat.com/faye');
  326.     ourFaye.addExtension({
  327.         incoming:function(message, callback){
  328.             if(message.data){
  329.                 message.data.channel = message.channel
  330.             };callback(message);
  331.         }
  332.     });
  333.     ourFaye.disable("autodisconnect");
  334.     ourFaye.subscribe('/*', function(g){
  335.         var clientID = g.channel.substring(1);
  336.  
  337.         // Autosend a message
  338.         var replyMessage = 'kBye!';
  339.         if(g.event == 'chat') {
  340.             if(g.data != replyMessage) {
  341.                 //sendMessage(clientID, replyMessage);
  342.             }
  343.  
  344.             log(clickableID(clientID));
  345.             log(sexyName(characterMap[clientID]) + ': ' + g.data + '<br>');
  346.  
  347.             if(listenToID[clientID]) {
  348.                 log(clickableID2(clientID), listenToID[clientID]);
  349.                 log(sexyName(characterMap[clientID]) + ': ' + g.data + '<br>', listenToID[clientID]);
  350.             }
  351.  
  352.             if(neededPeers[g.data]) {
  353.                 var np = neededPeers[g.data];
  354.                 neededPeers[g.data] = null;
  355.  
  356.                 if (typeof np.callback === "function") {
  357.                     np.callback(np, clientID);
  358.                 } else {
  359.                     log(neededPeers[g.data].clientID + ' IS CONNECTED TO ' + clientID + '<br>');
  360.                 }
  361.             }
  362.         }
  363.  
  364.         if(g.event == 'connect') {
  365.             characterMap[clientID] = g.otherCharacter;
  366.         }
  367.  
  368.         if(g.event == 'disconnect') {
  369.             if(listenToID[clientID]) {
  370.                 log(clientID + (characterMap[clientID] ? (' (' + characterMap[clientID] + ')') : '') + ' has disconnected! <br>', listenToID[clientID]);
  371.             }
  372.  
  373.             // Free memory
  374.             delete characterMap[clientID];
  375.         }
  376.  
  377.         //sendDisconnect(clientID);
  378.     }, function(e){});
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement