Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: PHP  |  size: 9.43 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. set_time_limit(0);
  4. ini_set('display_errors', 'on');
  5.  
  6.  
  7. $config = array(
  8.         'server' => 'irc.rizon.net',
  9.         'port'   => 6667,
  10.         'name'   => 'php',
  11.         'nick'   => 'php-stats',
  12.         'pass'   => 'WillJeWelWetenHeSero',
  13.        
  14.         'channel' => '#Aha2Y',
  15.         'logging' => true,
  16.             'warning' => true,
  17. );
  18.  
  19. class IRCBot {
  20.  
  21.         var $socket;
  22.         var $ex = array();    
  23.         //var $logging = true;
  24.  
  25.  
  26.         function __construct($config)
  27.         {
  28.                 $this->socket = fsockopen($config['server'], $config['port']);
  29.                 $this->login($config);
  30.                 $this->main($config);
  31.         }
  32.  
  33.         function login($config)
  34.         {
  35.                 $this->send_data('USER', $config['nick'].' wildphp.com '.$config['nick'].' :'.$config['name']);
  36.                 $this->send_data('NICK', $config['nick']);
  37.                                 $this->join_channel($config['channel']);
  38.                                
  39.                                  if($config['logging']) {
  40.                         $date = date("n-j-y");
  41.                         $time = date('h:i:s A');
  42.                         $logfile = fopen("$date-log.html","a");
  43.                         fwrite($logfile,"<br/>**************** Logging Started at $time ****************<br/>");
  44.                         fclose($logfile);
  45.                                
  46.                          if($config['warning']) {
  47.                                         $this->send_data('PRIVMSG '.$config['channel'].' :', "Hello, I'm a logger made by Aha2Y!");
  48.                                 }
  49.                  }
  50.         }
  51.  
  52.         function main($config)
  53.         {            
  54.                 $data = fgets($this->socket, 256);
  55.                
  56.                 echo nl2br($data);
  57.                                
  58.                 flush();
  59.  
  60.                 $this->ex = explode(' ', $data);
  61.                
  62.                 if($this->ex[0] == 'PING')
  63.                 {
  64.                         $this->send_data('PONG', $this->ex[0]); //Plays ping-pong with the server to stay connected.
  65.                 }
  66.                
  67.                  //Logs the chat
  68.                 if($config['logging'])
  69.                 {
  70.                         $logtxt = $this->filter_log($this->ex[1], $this->ex[2], $this->ex[0], $this->get_msg($this->ex)); //Gets human readable text from irc data
  71.                         if($logtxt != null) { //Writes to log if it is a message
  72.                                 $date = date("n-j-y");
  73.                                 $logfile = fopen("$date-log.html","a");
  74.                                 fwrite($logfile,"$logtxt<br />");
  75.                                 fclose($logfile);      
  76.                         }
  77.                 }
  78.  
  79.                 $command = str_replace(array(chr(10), chr(13)), '', $this->ex[3]);
  80.  
  81.                 switch($command) //List of commands the bot responds to from a user.
  82.                 {                      
  83.                         case ':!join':
  84.                                 $this->join_channel($this->ex[4]);
  85.                                 break;
  86.                                
  87.                         case ':!quit':
  88.                                 $this->send_data('QUIT', 'http://hackz.ch PHP log bot.');
  89.                                 break;
  90.  
  91.                         case ':!op':
  92.                                 $this->op_user();
  93.                                 break;
  94.  
  95.                         case ':!deop':
  96.                                 $this->op_user('','', false);
  97.                                 break;
  98.  
  99.                         case ':!protect':
  100.                                 $this->protect_user();
  101.                                 break;
  102.                                
  103.                                
  104.                         case ':!say':
  105.                                 $message = "";
  106.                                 for($i=4; $i <= (count($this->ex)); $i++)
  107.                                 {
  108.                                         $message .= $this->ex[$i]." ";
  109.                                 }
  110.                                
  111.                                 $this->send_data('PRIVMSG '.$config['channel'].' :', $message);
  112.                                 break;
  113.                        
  114.                         case ':!restart':
  115.                                          //Warn that logging has been disabled
  116.                                  if($config['warning']) {
  117.                                                          $this->send_data('PRIVMSG '.$config['channel'].' :', "Chat Logging has been [Disabled]");
  118.                                                 }
  119.                        
  120.                                 echo "<meta http-equiv=\"refresh\" content=\"3\">";
  121.                                 if($config['logging']) {
  122.                                         $date = date("n-j-y");
  123.                                         $time = date('h:i:s A');
  124.                                         $logfile = fopen("$date-log.html","a");
  125.                                         fwrite($logfile,"<br/>**************** Logging Ended at $time ****************<br/>");
  126.                                         fclose($logfile);
  127.                                  }
  128.                                 exit;
  129.                         case ':!shutdown':
  130.                                         //Warn that logging has been disabled
  131.                                 if($config['warning']) {
  132.                                                          $this->send_data('PRIVMSG '.$config['channel'].' :', "Chat Logging has been [Disabled]");
  133.                                                 }
  134.                        
  135.                                 if($config['logging']) {
  136.                                         $date = date("n-j-y");
  137.                                         $time = date('h:i:s A');
  138.                                         $logfile = fopen("$date-log.html","a");
  139.                                         fwrite($logfile,"<br/>**************** Logging Ended at $time ****************<br/>");
  140.                                         fclose($logfile);  
  141.                                  }
  142.                                 exit;
  143.                 }
  144.  
  145.                 $this->main($config);
  146.         }
  147.  
  148.  
  149.                 /* --- IRCBot Class's Functions --- */
  150.                
  151.                 function filter_log($type, $chan, $nick, $msg)
  152.                 {
  153.                         $nick = ltrim($nick, ":");
  154.                         $nick = substr($nick, 0, strpos($nick, "!"));
  155.                        
  156.                         $msg = ltrim($msg, ":");
  157.                        
  158.                         if($type == "PRIVMSG")
  159.                         {                              
  160.                                 return date("[H:i:s]")." <span class='bracket'>&lt;</span><span class='nickname'>".$nick."</span><span class='bracket'>&gt;</span> ".htmlentities($msg);
  161.                         }
  162.                         if($type == "JOIN")
  163.                         {                              
  164.                                 return date("[H:i:s]")." <span class='nickname'>*</span> ".$nick." has joined ".$chan."";
  165.                         }
  166.                         if($type == "PART")
  167.                         {                              
  168.                                 return date("[H:i:s]")." <span class='nickname'>*</span> ".$nick." has left ".$chan."";
  169.                         }
  170.                         return null     ;                              
  171.                 }
  172.                
  173.                 function get_msg($arr)
  174.                 {
  175.                         $message = "";
  176.             for($i=3; $i <= (count($this->ex)); $i++)
  177.             {
  178.               $message .= $this->ex[$i]." ";
  179.             }
  180.                         return $message;
  181.                 }
  182.  
  183.         function send_data($cmd, $msg = null) //displays stuff to the broswer and sends data to the server.
  184.         {
  185.                 if($msg == null)
  186.                 {
  187.                         fputs($this->socket, $cmd."\r\n");
  188.                         echo '<strong>'.$cmd.'</strong><br />';
  189.                 } else {
  190.  
  191.                         fputs($this->socket, $cmd.' '.$msg."\r\n");
  192.                         echo '<strong>'.$cmd.' '.$msg.'</strong><br />';
  193.                 }
  194.  
  195.         }
  196.  
  197.  
  198.  
  199.         function join_channel($channel) //Joins a channel, used in the join function.
  200.         {
  201.                 if(is_array($channel))
  202.                 {
  203.                         foreach($channel as $chan)
  204.                         {
  205.                                 $this->send_data('JOIN', $chan);
  206.                         }
  207.  
  208.                 } else {
  209.                         $this->send_data('JOIN', $channel);
  210.                 }
  211.         }
  212.  
  213.  
  214.  
  215.         function protect_user($user = '')
  216.         {
  217.                 if($user == '')
  218.                 {
  219.                         if(php_version() >= '5.3.0')
  220.                         {
  221.                                 $user = strstr($this->ex[0], '!', true);
  222.                         } else {
  223.                                 $length = strstr($this->ex[0], '!');
  224.                                 $user   = substr($this->ex[0], 0, $length);
  225.                         }
  226.                 }
  227.                
  228.                 $this->send_data('MODE', $this->ex[2] . ' +a ' . $user);
  229.         }              
  230.  
  231.         function op_user($channel = '', $user = '', $op = true) {
  232.                 if($channel == '' || $user == '')
  233.                 {
  234.                         if($channel == '')
  235.                         {
  236.                                 $channel = $this->ex[2];
  237.                         }
  238.  
  239.                         if($user == '')
  240.                         {
  241.  
  242.                                 if(php_version() >= '5.3.0')
  243.                                 {
  244.                                         $user = strstr($this->ex[0], '!', true);
  245.                                 } else {
  246.                                         $length = strstr($this->ex[0], '!');
  247.                                         $user   = substr($this->ex[0], 0, $length);
  248.                                 }
  249.                         }
  250.                 }
  251.  
  252.  
  253.                 if($op)
  254.                 {
  255.                         $this->send_data('MODE', $channel . ' +o ' . $user);
  256.                 } else {
  257.                         $this->send_data('MODE', $channel . ' -o ' . $user);
  258.                 }
  259.         }
  260. }
  261.  
  262. //Start the bot
  263. $bot = new IRCBot($config);
  264. ?>