Advertisement
Guest User

Untitled

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