Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 20th, 2010 | Syntax: PHP | Size: 3.13 KB | Hits: 69 | Expires: Never
Copy text to clipboard
  1. #!/usr/bin/php5
  2. <?php
  3.  
  4. date_default_timezone_set('Europe/Berlin');
  5.  
  6.  
  7.  
  8. $server = 'irc.myleap.de';
  9. $port = 6667;
  10. $nick = 'BubiBot';
  11.  
  12. $chan_list = array('#irc');
  13. $commandprefix = ':';
  14.  
  15. $fp_connection = fsockopen($server, $port);
  16.  
  17. if(!$fp_connection)
  18. {
  19.     die("Konnte nicht verbinden!\n");
  20. }
  21.  
  22.  
  23. $buffer = "";
  24.  
  25. sendline("NICK ".$nick."\r\n");
  26. sendline("USER ".$nick." * * :bubis bot\r\n");
  27.  
  28. while(!feof($fp_connection))
  29. {
  30.     $buffer .= str_replace("\r", "\n", fread($fp_connection, 4096));
  31.    
  32.     $nlpos = strpos($buffer, "\n");
  33.     while($nlpos !== false)
  34.     {
  35.         $line = trim(substr($buffer, 0, $nlpos));
  36.         $buffer = substr($buffer, $nlpos+1);
  37.        
  38.         if($line != '')
  39.         {
  40.             parse_line($line);
  41.         }
  42.        
  43.         $nlpos = strpos($buffer, "\n");
  44.     }
  45. }
  46.  
  47.  
  48.  
  49.  
  50. function parse_line($line)
  51. {
  52.     global $chan_list, $commandprefix;
  53.    
  54.     if($line[0] == ':') $line = substr($line, 1);
  55.    
  56.     if(param($line, 1) == 'PING')
  57.     {
  58.         sendline('PONG '.param($line,2,true));
  59.         return;
  60.     }
  61.    
  62.     //nick, ident und host ermitteln
  63.     $nick = param($line, 1);
  64.     $ident = "";
  65.     $host = "";
  66.     $pos_a = strpos($nick, '!');
  67.     $pos_b = strpos($nick, '@');
  68.     if($pos_a !== false)
  69.     {
  70.         $ident = substr($nick, $pos_a+1);
  71.         $nick = substr($nick, 0, $pos_a);
  72.         $pos_b = strpos($ident, '@');
  73.         if($pos_b !== false)
  74.         {
  75.             $host = substr($ident, $pos_b+1);
  76.             $ident = substr($ident, 0, $pos_b);
  77.         }
  78.     }
  79.     elseif($pos_b !== false)
  80.     {
  81.         $host = substr($nick, $pos_b+1);
  82.         $nick = substr($nick, 0, $pos_b);
  83.     }
  84.    
  85.     $command = strtoupper(param($line, 2));
  86.    
  87.     if($command == '001')
  88.     {
  89.         //channel joinen
  90.         $tmp_chan_list = $chan_list;
  91.         while(count($tmp_chan_list) > 0)
  92.         {
  93.             $chans_per_join = 5;
  94.             $some_chans = array_slice($tmp_chan_list, 0, $chans_per_join);
  95.             $tmp_chan_list = array_slice($tmp_chan_list, $chans_per_join);
  96.             sendline('JOIN '.implode(',', $some_chans));
  97.         }
  98.     }
  99.    
  100.    
  101.    
  102.    
  103. }
  104.  
  105.  
  106.  
  107. //some functions
  108.  
  109. function sendline($line)
  110. {
  111.     global $fp_connection;
  112.     fwrite($fp_connection, $line."\r\n");
  113. }
  114.  
  115. /* überprüft ob ein string valides utf-8 ist
  116. function is_utf8($str)
  117. {
  118.     if(preg_match(
  119.         '/^(([\x00-\x7F])|'.
  120.           '([\xC0-\xDF][\x80-\xBF])|'.
  121.           '([\xE0-\xEF][\x80-\xBF]{2})|'.
  122.           '([\xF0-\xF7][\x80-\xBF]{3}))*$/',
  123.           $str, $match))
  124.     {
  125.         return true;
  126.     }
  127.     return false;
  128. }
  129. */
  130.  
  131.  
  132.  
  133. function param($line, $pos, $and_rest = false)
  134. {
  135.     $line = trim($line);
  136.     while(strpos($line, "  ") !== false)
  137.     {
  138.         $line = str_replace("  ", " ", $line);
  139.     }
  140.    
  141.     $params = explode(" ", $line);
  142.    
  143.     if(isset($params[$pos-1]))
  144.     {
  145.         if($and_rest)
  146.         {
  147.             return implode(" ", array_slice($params, $pos-1));
  148.         }
  149.         else
  150.         {
  151.             return $params[$pos-1];
  152.         }
  153.     }
  154.     else
  155.     {
  156.         return "";
  157.     }
  158. }
  159.  
  160. ?>