Advertisement
zenware

phpircbot

Oct 14th, 2012
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.94 KB | None | 0 0
  1. <?php
  2.  
  3. set_time_limit(0);
  4.  
  5. ini_set('display_errors', 'on')
  6.  
  7. $config = array(
  8.         'server' => 'irc.freenode.net',
  9.         'port' => 6667,
  10.         'nick' => 'phpircb',
  11.         'name' => 'anonymous',
  12.         'pass' => 'meh',
  13.     );
  14.  
  15.  
  16. interface iIRCbot
  17. {
  18.     protected function login($config)
  19.     private function main();
  20.     protected function parse($data);
  21.     protected function sent_data($cmd, $msg);
  22.     public function join_channel($channel);
  23.     private function op_user($channel, $user, $op);
  24.     private function protect_user($user);
  25. }
  26.  
  27. class IRCbot implements iIRCbot
  28. {
  29.     var $socket;
  30.     var $ex = array();
  31.  
  32.  
  33.     function __construct($config) {
  34.         $this->socket = fsockopen($config['server'], $config['port']);
  35.         $this->login($config);
  36.         $this->main();
  37.         $this->send_data('JOIN', '#bots');
  38.     }
  39.  
  40.     function login($config)
  41.     {
  42.         $this->send_data('USER', $config['nick'].' lalalalalalala'.$config['nick'].' :'.$config['name']);
  43.         $this->send_data('NICK', $config['nick'])
  44.     }
  45.  
  46.     function main()
  47.     {
  48.         $data = fgets($this->$socket, 128);
  49.         echo nl2br($data);
  50.         flush();
  51.         $this->ex explode(' ', $data);
  52.         while(true)
  53.             $this->parse($this->ex);
  54.     }
  55.  
  56.     function parse($data)
  57.     {
  58.         if($data[0] == 'PING')
  59.             $this->send_data('PONG', $data[1]);
  60.  
  61.         $command = str_replace(array(chr(10), chr(13), '', $data[3]))
  62.  
  63.         switch($command)
  64.         {
  65.             case ':!join':
  66.                 $this->join_channel($data[4]);
  67.                 break;
  68.  
  69.             case ':!quit':
  70.                 send_data('QUIT', 'Leaving...');
  71.                 break;
  72.  
  73.             case ':!op':
  74.                 $this->op_user();
  75.                 break;
  76.  
  77.             case ':!deop':
  78.                 $this->op_user('', '', false);
  79.                 break;
  80.  
  81.             case ':!protect':
  82.                 $this->protect_user();
  83.                 break;
  84.         }
  85.     }
  86.  
  87.     function send_data($cmd, $msg = null)
  88.     {
  89.         if($msg == null)
  90.         {
  91.             fputs($this->socket, $cmd."\r\n");
  92.             echo '<strong>'.$cmd.'</strong><br />';
  93.         } else {
  94.             fputs($this->socket, $cmd.' '.$msg."\r\n");
  95.             echo '<strong>'.$cmd.' '.$msg.'</strong><br />';
  96.         }
  97.     }
  98.  
  99.     function join_channel($channel)
  100.     {
  101.         if(is_array($channel))
  102.         {
  103.             foreach ($channel as $chan) {
  104.                 $this->send_data('JOIN', $chan);
  105.             } else {
  106.                 $this->send_data('JOIN', $chan);
  107.             }
  108.         }
  109.     }
  110.  
  111.     function protect_user($user = '')
  112.     {
  113.         if($user == '')
  114.         {
  115.             if(phpversion() >= '5.3.0'){
  116.                 $user = strstr($this->ex[0], '!', true);
  117.             } else {
  118.                 $length = strstr($this->ex[0], '!');
  119.                 $user = substr($this->ex[0], 0, $length);
  120.             }
  121.         }
  122.  
  123.         $this->send_data('MODE', $this->ex[2] . ' +a ' . $user);
  124.     }
  125.  
  126.     function op_user($channel = '', $user = '', $op = true)
  127.     {
  128.         if($channel == '')
  129.         {
  130.             $channel = $this->ex[2];
  131.         }
  132.  
  133.         if($user == '')
  134.         {
  135.             if(phpversion() >= '5.3.0'){
  136.                 $user = strstr($this->ex[0], '!', true);
  137.             } else {
  138.                 $length = strstr($this->ex[0], '!');
  139.                 $user = substr($this->ex[0], 0, $length);
  140.             }
  141.         }
  142.  
  143.         if($op){
  144.             $this->send_data('MODE', $channel . ' +o ' . $user);
  145.         } else {
  146.             $this->send_data('MODE', $channel . ' -o ' . $user);
  147.         }
  148.     }
  149. }
  150.  
  151. $bot = new IRCbot($config);
  152.  
  153. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement