Advertisement
prog

cubix

Sep 18th, 2010
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http_server = context.CreateTcpServer();
  2. http_server.onaccept = 'http_server_accept';
  3. http_server.listen(5000, "0.0.0.0");
  4.  
  5. function http_server_accept()
  6. {
  7.   peer.onreadline = 'peer_readline';
  8. }
  9.  
  10. function peer_readline()
  11. {
  12.   if (peer.hand.length == 0)
  13.   {
  14.     var args = text.split(' ');
  15.     if (args[0] == 'GET' || args[0] == 'POST')
  16.     {
  17.       peer.hand = args[1] + ' ' + args[2];
  18.     }
  19.   }
  20.   else
  21.   {
  22.     if (text.length == 0)
  23.     {
  24.       processRequest(peer.hand.split(' ')[0]);
  25.       peer.hand = '';
  26.     }
  27.   }
  28. }
  29.  
  30. function sendResponse(response)
  31. {
  32.   peer.sendline(peer.hand.split(' ')[1] + ' 200 OK');
  33.   peer.sendline('Server: cubix 7.0');
  34.   peer.sendline('Date: ' + new Date());
  35.   peer.sendline('Content-Type: text/html');
  36.   peer.sendline('Content-Length: ' + (response.length + 2));
  37.   peer.sendline('');
  38.   peer.sendline(response);
  39.   //peer.disconnect();
  40. }
  41.  
  42. function processRequest(document)
  43. {
  44.   switch (document)
  45.   {
  46.     case '/':
  47.       sendResponse("<h1>You are at the root directory</h1>");
  48.       break;
  49.     default:
  50.       sendResponse(dumpChannel('#' + document.substring(1)));
  51.       break;
  52.   }
  53. }
  54.  
  55. function dumpChannel(chan)
  56. {
  57.   var chan = irc.channel(chan);
  58.   var result = '';
  59.   result += '<html>' + '\r\n';
  60.   result += '<head>' + '\r\n';
  61.   result += '<meta http-equiv="refresh" content="5">' + '\r\n';
  62.   result += '<style type="text/css">' + '\r\n';
  63.   result += '<!--' + '\r\n';
  64.   result += 'table, td, th, h2 { border:1px solid orange; font-family:"Fixedsys Excelsior 2.00"; src: url("http://prog.ma/fixedsys.ttf"); font-size: 11pt; }' + '\r\n';
  65.   result += 'table { border-collapse:collapse; }' + '\r\n';
  66.   result += 'th { background-color:orange; color:white; }' + '\r\n';
  67.   result += 'h1 { font-size: 22pt; }' + '\r\n';
  68.   result += 'h2 { font-weight: normal; }' + '\r\n';
  69.   result += '--->' + '\r\n';
  70.   result += '</style>' + '\r\n';
  71.   result += '</head>' + '\r\n';
  72.   result += '<body>' + '\r\n';
  73.   result += '<h1>' + chan + '</h1>' + '\r\n';
  74.   result += '<h2><b>Topic:</b> ' + colorize(chan.topic) + '</h2>' + '\r\n';
  75.   result += '<h2><b>Users:</b> ' + chan.users.count + '</h2>' + '\r\n';
  76.   result += '<table border="0" width="100%" style="table-layout:fixed">' + '\r\n';
  77.   result += '<col width="110">' + '\r\n';
  78.   result += '<col width="550">' + '\r\n';
  79.   result += '<col>' + '\r\n';
  80.   result += '<tr>' + '\r\n';
  81.   result += '<th>User</th>' + '\r\n';
  82.   result += '<th>Full address</th>' + '\r\n';
  83.   result += '<th>Real name</th>' + '\r\n';
  84.   result += '</tr>' + '\r\n';
  85.   for (var i = 0; i < chan.users.count; i++)
  86.   {
  87.     var user = chan.users(i);
  88.     if (user.hasop(chan))
  89.     {
  90.       result += '<tr>' + '\r\n';
  91.       result += '<td>@' + user.nickname + '</td>' + '\r\n';
  92.       result += '<td>' + user.fulladdress + '</td>' + '\r\n';
  93.       result += '<td>' + colorize(user.realname) + '</td>' + '\r\n';
  94.       result += '</tr>' + '\r\n';
  95.     }
  96.   }
  97.   for (var i = 0; i < chan.users.count; i++)
  98.   {
  99.     var user = chan.users(i);
  100.     if (!user.hasop(chan) && user.hasvoice(chan))
  101.     {
  102.       result += '<tr>' + '\r\n';
  103.       result += '<td>+' + user.nickname + '</td>' + '\r\n';
  104.       result += '<td>' + user.fulladdress + '</td>' + '\r\n';
  105.       result += '<td>' + colorize(user.realname) + '</td>' + '\r\n';
  106.       result += '</tr>' + '\r\n';
  107.     }
  108.   }
  109.   for (var i = 0; i < chan.users.count; i++)
  110.   {
  111.     var user = chan.users(i);
  112.     if (!user.hasop(chan) && !user.hasvoice(chan))
  113.     {
  114.       result += '<tr>' + '\r\n';
  115.       result += '<td> ' + user.nickname + '</td>' + '\r\n';
  116.       result += '<td>' + user.fulladdress + '</td>' + '\r\n';
  117.       result += '<td>' + colorize(user.realname) + '</td>' + '\r\n';
  118.       result += '</tr>' + '\r\n';
  119.     }
  120.   }
  121.   result += '</table>' + '\r\n';
  122.   result += '</body>' + '\r\n';
  123.   result += '</html>' + '\r\n';
  124.  
  125.   return result;
  126. }
  127.  
  128.  
  129. // Copyright (c) 2006 Chris Chabot <chabotc@xs4all.nl>
  130. //
  131. // this script is freely distributable under the terms of an MIT-style license.
  132. // For details, see the web site: http://www.chabotc.nl/
  133.  
  134. function colorize (message) {
  135.     var pageBack  = 'white';
  136.     var pageFront = 'black';
  137.     var length    = message.length;
  138.     var newText   = '';
  139.     var bold      = false;
  140.     var color     = false;
  141.     var reverse   = false;
  142.     var underline = false;
  143.     var foreColor = '';
  144.     var backColor = '';
  145.     for (var i = 0 ; i < length ; i++) {
  146.         switch (message.charAt(i)) {
  147.             case String.fromCharCode(2):
  148.                 if (bold) {
  149.                     newText += '</b>';
  150.                     bold     = false;
  151.                 } else {
  152.                     newText += '<b>';
  153.                     bold    = true;
  154.                 }
  155.                 break;
  156.             case String.fromCharCode(3):
  157.                 if (color) {
  158.                     newText += '</span>';
  159.                     if (!backColor) {
  160.                         color = false;
  161.                     }
  162.                 }
  163.                 foreColor = '';
  164.                 if ((parseInt(message.charAt(i+1)) >= 0) && (parseInt(message.charAt(i+1)) <= 9)) {
  165.                     color = true;
  166.                     if ((parseInt(message.charAt(++i+1)) >= 0) && (parseInt(message.charAt(i+1)) <= 9)) {
  167.                         foreColor = getColor(parseInt(message.charAt(i)) * 10 + parseInt(message.charAt(++i)));
  168.                     } else {
  169.                         foreColor = getColor(parseInt(message.charAt(i)));
  170.                     }
  171.                     if ((message.charAt(i+1) == ',') && (parseInt(message.charAt(++i+1)) >= 0) && (parseInt(message.charAt(i+1)) <= 9)) {
  172.                         if ((parseInt(message.charAt(++i+1)) >= 0) && (parseInt(message.charAt(i+1)) <= 9)) {
  173.                             backColor = getColor(parseInt(message.charAt(i)) * 10 + parseInt(message.charAt(++i)));
  174.                         } else {
  175.                             backColor = getColor(parseInt(message.charAt(i)));
  176.                         }
  177.                     }
  178.                 }
  179.                 else {
  180.                     backColor = '';
  181.                 }
  182.                 if (foreColor) {
  183.                     newText += '<span style="color:'+foreColor;
  184.                     if (backColor) {
  185.                         newText += ';background-color:'+backColor;
  186.                     }
  187.                     newText += '">';
  188.                 }
  189.                 break;
  190.             case String.fromCharCode(15):
  191.                 if (bold) {
  192.                     newText += '</b>';
  193.                     bold     = false;
  194.                 }
  195.                 if (color) {
  196.                     newText += '</span>';
  197.                     color    = false;
  198.                 }
  199.                 if (reverse) {
  200.                     newText += '</span>';
  201.                     reverse  = false;
  202.                 }
  203.                 if (underline) {
  204.                     newText  += '</u>';
  205.                     underline = false;
  206.                 }
  207.                 break;
  208.             case String.fromCharCode(22):
  209.                 if (reverse) {
  210.                     newText += '</span>';
  211.                     reverse  = false;
  212.                 } else {
  213.                     newText += '<span style="color:'+pageBack+';background-color:'+pageFront+'">';
  214.                     reverse  = true;
  215.                 }
  216.             case String.fromCharCode(31):
  217.                 if (underline) {
  218.                     newText  += '</u>';
  219.                     underline = false;
  220.                 } else {
  221.                     newText  += '<u>';
  222.                     underline = true;
  223.                 }
  224.             default:
  225.                 newText += message.charAt(i);
  226.                 break;
  227.         }
  228.  
  229.     }
  230.     if (bold)      newText += '</b>';
  231.     if (color)     newText += '</span>';
  232.     if (reverse)   newText += '</span>'
  233.     if (underline) newText += '</u>';
  234.     return newText;
  235. }
  236.  
  237. function getColor(numeric)
  238. {
  239.     var num = parseInt(numeric);
  240.     switch (num) {
  241.         case 0:  return 'white';
  242.         case 1:  return 'black';
  243.         case 2:  return 'navy';
  244.         case 3:  return 'green';
  245.         case 4:  return 'red';
  246.         case 5:  return 'maroon';
  247.         case 6:  return 'purple';
  248.         case 7:  return 'orange';
  249.         case 8:  return 'yellow';
  250.         case 9:  return 'lime';
  251.         case 10: return 'teal';
  252.         case 11: return 'aqua';
  253.         case 12: return 'blue';
  254.         case 13: return 'fuchsia';
  255.         case 14: return 'gray';
  256.         default: return 'silver';
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement