prog

http.js

Aug 25th, 2010
60
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.   var args = text.split(' ');
  13.   if (args[0] == 'GET' || args[0] == 'POST')
  14.   {
  15.     peer.hand = args[1] + ' ' + args[2];
  16.   }
  17.   if (text.length == 0)
  18.   {
  19.     processRequest(peer.hand.split(' ')[0]);
  20.   }
  21. }
  22.  
  23. function sendResponse(response)
  24. {
  25.   peer.sendline(peer.hand.split(' ')[1] + ' 200 OK');
  26.   peer.sendline('Server: cubix 6.2');
  27.   peer.sendline('Date: ' + new Date());
  28.   peer.sendline('Content-Type: text/html');
  29.   peer.sendline('Content-Length: ' + (response.length + 2));
  30.   peer.sendline('');
  31.   peer.sendline(response);
  32. }
  33.  
  34. function processRequest(document)
  35. {
  36.   switch (document)
  37.   {
  38.     case '/':
  39.       sendResponse("<h1>You are at the root directory</h1>");
  40.       break;
  41.     default:
  42.       sendResponse(dumpChannel('#' + document.substring(1)));
  43.       break;
  44.   }
  45. }
  46.  
  47. function dumpChannel(chan)
  48. {
  49.   var chan = irc.channel(chan);
  50.   var result = '<html>' + '\r\n';
  51.   result += '<head>' + '\r\n';
  52.   result += '<meta http-equiv="refresh" content="5">' + '\r\n';
  53.   result += '<style type="text/css">' + '\r\n';
  54.   result += '<!--' + '\r\n';
  55.   result += 'td{font-family:"Fixedsys Excelsior 2.00"; src: url("http://prog.ma/fixedsys.ttf"); font-size: 11pt;}' + '\r\n';
  56.   result += '--->' + '\r\n';
  57.   result += '</style>' + '\r\n';
  58.   result += '</head>' + '\r\n';
  59.  
  60.   result += '<body>' + '\r\n';
  61.   result += '<h1>' + chan + '</h1>' + '\r\n';
  62.   result += '<h2>Topic: ' + chan.topic + '</h2>' + '\r\n';
  63.   result += '<h2>Users: ' + chan.users.count + '</h2>' + '\r\n';
  64.   result += '<table border="1" width="700" style="table-layout:fixed">' + '\r\n';
  65.   result += '<col width="130">' + '\r\n';
  66.   result += '<col width="570">' + '\r\n';
  67.   result += '<tr>' + '\r\n';
  68.   result += '<th>User</td>' + '\r\n';
  69.   result += '<th>Full address</td>' + '\r\n';
  70.   result += '</tr>' + '\r\n';
  71.   for (var i = 0; i < chan.users.count; i++)
  72.   {
  73.     result += '<tr>' + '\r\n';
  74.     result += '<td>' + chan.users(i).nickname + '</td>' + '\r\n';
  75.     result += '<td>' + chan.users(i).fulladdress + '</td>' + '\r\n';
  76.     result += '</tr>' + '\r\n';
  77.   }
  78.   result += '</table>' + '\r\n';
  79.   result += '</body>' + '\r\n';
  80.   result += '</html>' + '\r\n';
  81.  
  82.   return result;
  83. }
Add Comment
Please, Sign In to add comment