Advertisement
Xenios

PHP IRC BOT

Oct 23rd, 2012
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.29 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>IRC Bot</title>
  4. </head>
  5. <body>
  6.  
  7. <?php
  8.  
  9.     require('api_calls.php'); // functions for communication with other servers
  10.     require('irc_commands.php'); // functions for communction bewteen the IRC server and bot only
  11.  
  12. /*
  13.  *
  14.  * IRC Bot by Phil, modified by Xenios
  15.  * Copyright Phil 2010
  16.  *
  17.  */
  18.  
  19. // Bot may only be run from a command line interface.
  20. $sapi_type = php_sapi_name();
  21. if (substr($sapi_type, 0, 3) == 'cli')
  22. {
  23.        
  24.     // Ensure all errors are reported
  25.     error_reporting(E_ALL);
  26.  
  27.     // Ensure we don't time out the script
  28.     set_time_limit(0);
  29.  
  30.     // Variable setting
  31.     $read = null; // For reading in data
  32.     $sock = null; // Socket
  33.  
  34.     // Timer definitions
  35.     // Timer for sending timeout checks
  36.     $timers = array();
  37.     $timers['ping'] = 0;
  38.  
  39.     // Functions
  40.     // Send message to server
  41.     function send($message, $output = true) {
  42.         global $sock;
  43.  
  44.         fputs($sock, "{$message}\n");
  45.  
  46.         if ($output) {
  47.             echo "-> {$message}\n";
  48.         }
  49.     }
  50.  
  51.     // Formatting - Change these and they'll break
  52.     define('C_BOLD', chr(2));
  53.     define('C_COLOR', chr(3));
  54.     define('C_ITALIC', chr(29));
  55.     define('C_REVERSE', chr(22));
  56.     define('C_UNDERLINE', chr(31));
  57.  
  58.     // Config
  59.     define('SERVER', 'serverName'); // Server to connect to (irc.rizon.no or irc.rizon.net)
  60.     define('PORT', 'serverPort'); // Port on server
  61.     define('BOT_NICK', 'botName'); // Bot's nick
  62.     define('BOT_ANICK', 'botAnick'); // Nick to use if main is taken
  63.     define('IDENT', 'botIdent'); // Ident
  64.     define('GECOS', 'botGecos'); // Real name
  65.     define('NSPASS', 'botPassword'); // NickServ password
  66.     define('CHANS', 'channelName'); // Channels to join (, separated list)
  67.  
  68.     // The juice
  69.     // Loop forever
  70.     while (1) {
  71.         // Connect
  72.         $sock = fsockopen(SERVER, PORT);
  73.  
  74.         // Bitch, Rinse and Repeat if it's broken
  75.         if (!$sock) {
  76.             echo 'Error connecting to ' . SERVER . ' on port ' . PORT . '! Trying again in 30 seconds';
  77.             sleep(30);
  78.         }
  79.         else {
  80.             // Stop the stream blocking, sets timeout value (so the bot quits if it's disconnected)
  81.             stream_set_timeout($sock, 0, 1);
  82.             stream_set_blocking($sock, 0);
  83.  
  84.             // Send connect stuff
  85.             send('USER ' . IDENT . ' 0 * :' . GECOS);
  86.             send('NICK ' . BOT_NICK);
  87.            
  88.             // Identify to services - change this if this isn't the correct command
  89.             send('NS IDENTIFY ' . NSPASS);
  90.            
  91.             // Join channels
  92.             send('JOIN ' . CHANS);
  93.  
  94.             // Loop until socket dies
  95.             while (!feof($sock)) {
  96.                 // Read delay of 0.1 seconds
  97.                 // Reduce this to increase the responsiveness of the bot in busy channels
  98.                 // Be aware that it'll raise CPU load
  99.                 usleep(100000);
  100.  
  101.                 // Increment all timers
  102.                 foreach ($timers as $key => $timer) {
  103.                     $timers[$key]++;
  104.                 }
  105.  
  106.                 // Timer events
  107.                 if ($timers['ping'] == 600) {
  108.                     send('PING :TIMEOUTCHECK', false);
  109.                     $timers['ping'] = 0;
  110.                 }
  111.  
  112.                 // Reading data
  113.                 $read .= fread($sock, 512);
  114.  
  115.                 // Loop the individual lines
  116.                 while (substr_count($read, "\n") != 0) {
  117.                     // Get end of line
  118.                     $offset = strpos($read, "\n");
  119.                     // Get data
  120.                     $data = substr($read, 0, $offset);
  121.                     // Remove data from end of line
  122.                     $read = substr($read, $offset + 1);
  123.  
  124.                     // Print the data for all but PONG
  125.                     if (strpos($data, 'PONG') === false) {
  126.                         echo "-> {$data}\n";
  127.                     }
  128.  
  129.                     // Split the data
  130.                     $dataSplit = explode(' ', $data);
  131.  
  132.                     // Handle the data
  133.                     // PINGs
  134.                     if ($dataSplit[0] == 'PING') {
  135.                         send('PONG ' . substr($data, 5));
  136.                     }
  137.                     // Regular message handler
  138.                     elseif ($dataSplit[1] == 'PRIVMSG') {
  139.  
  140.                         // Get the required info from the message
  141.                         $message = trim(ltrim(implode(' ', array_slice($dataSplit, 3)), ':'));
  142.                         $words = explode(' ', trim($message));
  143.                         $to = $dataSplit[2];
  144.                         $nick = explode('!', $dataSplit[0]);
  145.                         $nick = ltrim($nick[0], ':');
  146.  
  147.                         // Private messages
  148.                         if (strtolower($to) == strtolower(BOT_NICK)) {
  149.                             if ($words[0] == '!hi') {
  150.                                 send("PRIVMSG {$nick} :Hi {$nick}!");
  151.                             }
  152.                         }
  153.  
  154.                         // Channel messages
  155.                         else
  156.                         {
  157.                             // Trigger to a specific command if command is in the begin of $words.
  158.                             if (strpos($words[0], '!call') === 0)
  159.                             {
  160.                                 // reads
  161.                                 $temp = get_data_call($words, '!call');
  162.                                 if ($temp !== FALSE)
  163.                                 {
  164.                                     send ("NAMES CHANS");
  165.  
  166.                                     // the next echo lines are for debugging
  167.                                     echo "\n\n\n";
  168.                                     echo "\nwords\n";
  169.                                     print_r($words);
  170.                                     echo "\nmessage\n";
  171.                                     print_r($message);
  172.                                     echo "\ndata\n";
  173.                                     print_r($data);
  174.                                     echo "\ndatasplit\n";
  175.                                     print_r($dataSplit);
  176.                                     echo "\nread names\n";
  177.                                     // the above echo lines are for debugging
  178.                                      
  179.                                     while (!feof($sock))
  180.                                     {
  181.                                         $readCall = fread($sock, 9024);
  182.                                         if ($readCall === true)
  183.                                         {
  184.                                             while ($readCall == ':irc.rizon.no')
  185.                                             {
  186.                                                 echo "irc.rizon.no found!";
  187.                                                 continue;
  188.                                             }
  189.                                         }
  190.                                         else
  191.                                         {
  192.                                              echo "readCall returns false.";
  193.                                         }
  194.                                     }
  195.                                 }
  196.                                
  197.                                 else
  198.                                 {
  199.                                     echo "Something went wrong.";  
  200.                                 }                  
  201.                                
  202.                             }
  203.  
  204.                             // Private messages
  205.                             if (strtolower($to) == strtolower(BOT_NICK)) {
  206.                                 if ($words[0] == '!die') {
  207.                                     send ("QUIT : quit message");
  208.                                     fclose($sock);
  209.                                     exit;
  210.                             }
  211.                                                    
  212.                         }
  213.                         // Channel messages
  214.                         else {
  215.                             if ($words[0] == '!die') {
  216.                                 send ("QUIT : quit message");
  217.                                 fclose($sock);
  218.                                 exit;
  219.                              }
  220.                   }
  221.                }                    
  222.                     }
  223.                     // Notice handler
  224.                     elseif ($dataSplit[1] == 'NOTICE') {
  225.                         // Get the required info from the message
  226.                         $message = trim(ltrim(implode(' ', array_slice($dataSplit, 3)), ':'));
  227.                         $words = explode(' ', trim($message));
  228.                         $to = $dataSplit[2];
  229.                         $nick = explode('!', $dataSplit[0]);
  230.                         $nick = ltrim($nick[0], ':');
  231.  
  232.                         // Private notices
  233.                         if (strtolower($to) == strtolower(BOT_NICK)) {
  234.                             // no code yet
  235.                         }
  236.                         // Channel notices
  237.                         else {
  238.                             // no code yet
  239.                         }
  240.                     }
  241.                 }
  242.             }
  243.         }
  244.     }
  245. }
  246. else
  247. {
  248.     echo "This script can only be run in a CLI. ";
  249. }
  250.  
  251. ?>
  252.  
  253. </body>
  254. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement