Show
Ignore:
Timestamp:
02/14/2008 11:10:34 PM (11 months ago)
Author:
Shadowhand
Message:

I, Kobot.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/kobot/libraries/Kobot.php

    r2051 r2052  
    4141        ); 
    4242 
    43         // IRC socket, MOTD, and stats 
     43        // IRC socket, username, MOTD, and stats 
    4444        protected $socket; 
     45        protected $username; 
    4546        protected $motd; 
    4647        protected $stats = array 
     
    115116                        // Read the PART command 
    116117                        $this->set_response('PART', array($this, 'response_part')); 
     118 
     119                        // Read the PRIVMSG command 
     120                        $this->set_response('PRIVMSG', array($this, 'response_privmsg')); 
     121 
     122                        // Reply to VERSION commands 
     123                        $this->add_trigger('^VERSION$', array($this, 'trigger_version')); 
    117124                } 
    118125                else 
     
    126133        public function login($username, $password = NULL, $realname = 'Kohana PHP Bot') 
    127134        { 
    128                 // Send the login commands 
    129                 $this->send('USER '.$username.' * * :'.$realname); 
     135                // Cache the current username 
     136                $this->username = $username; 
     137 
     138                // Send the login commands, use 8 for the mask (invisible) 
     139                $this->send('USER '.$username.' 8 * :'.$realname); 
    130140                $this->send('NICK '.$username); 
    131141 
     
    160170        public function quit($message = '</Kirc> by Kohana Team') 
    161171        { 
    162                 // Quit, wait, and exit 
    163                 $this->send('QUIT '.$message); 
    164                 sleep(2); 
    165                 exit; 
     172                // Send a quit message 
     173                $this->send('QUIT :'.$message); 
    166174        } 
    167175 
     
    211219                                { 
    212220                                        // Debug the response 
    213                                         $this->log(2, 'NOT FOUND: '.$data['command'].' <<< '.trim($raw)); 
     221                                        $this->log(3, '<<< '.$data['command'].' <<< '.trim($raw)); 
    214222                                } 
    215223                        } 
     
    217225                        usleep(500000); 
    218226                } 
     227 
     228                // Disconnect 
     229                $this->log(1, 'Disconnected'); 
    219230        } 
    220231 
     
    295306        } 
    296307 
    297         protected function run() 
    298         { 
    299                 // Current username and size of username 
    300                 $bot = $this->config['username']; 
    301                 $len = strlen($bot); 
    302  
    303                 // Parts of a publicly spoken message 
    304                 $parts = array('nickname', 'username', 'hostname', 'channel', 'message'); 
    305  
    306                 while ( ! feof($this->socket)) 
    307                 { 
    308                         while ($raw = fgets($this->socket, 1024)) 
    309                         { 
    310                                 // Remove extra whitespace 
    311                                 $raw = trim($raw); 
    312  
    313                                 if (substr($raw, 0, 4) === 'PING') 
    314                                 { 
    315                                         // Send a PONG response 
    316                                         $this->send('PONG'.substr($raw, 4)); 
    317                                         break; 
    318                                 } 
    319                                 else 
    320                                 { 
    321                                         if (strpos($raw, 'PRIVMSG') !== FALSE 
    322                                             AND strpos($raw, $bot) !== FALSE 
    323                                             AND preg_match('/^:(.+?)!n=(.+?)@(.+?) PRIVMSG (#.+?) :(.+)$/', $raw, $data)) 
    324                                         { 
    325                                                 // Make an associative array of the data 
    326                                                 $data = array_combine($parts, array_slice($data, 1)); 
    327  
    328                                                 if (substr($data['message'], 0, $len) === $bot) 
    329                                                 { 
    330                                                         // A command has been sent 
    331                                                         $command = ltrim(substr($data['message'], $len), ' :;,'); 
    332  
    333                                                         if ($command === 'say hello') 
    334                                                         { 
    335                                                                 ; 
    336                                                         } 
    337                                                         elseif (preg_match('/^r(\d+)$/', $command, $match)) 
    338                                                         { 
    339                                                                 // The URL for a revision number 
    340                                                                 $url = 'http://trac.kohanaphp.com/changeset/'.$match[1]; 
    341  
    342                                                                 if ($this->url_status($url)) 
    343                                                                 { 
    344                                                                         $this->send('PRIVMSG '.$data['channel'].' :Revision r'.$match[1].', '.$url); 
    345                                                                 } 
    346                                                         } 
    347                                                         elseif (preg_match('/^#(\d+)$/', $command, $match)) 
    348                                                         { 
    349                                                                 // The URL for a ticket number 
    350                                                                 $url = 'http://trac.kohanaphp.com/ticket/'.$match[1]; 
    351  
    352                                                                 if ($this->url_status($url)) 
    353                                                                 { 
    354                                                                         $this->send('PRIVMSG '.$data['channel'].' :Ticket #'.$match[1].', '.$url); 
    355                                                                 } 
    356                                                         } 
    357                                                 } 
    358                                         } 
    359                                         else 
    360                                         { 
    361                                                 echo $raw."\n"; 
    362                                         } 
    363  
    364                                         //  
    365                                         // if (($offset = strpos($raw, ' PRIVMSG :'.$user)) !== FALSE AND ) 
    366                                         // { 
    367                                         //       
    368                                         // } 
    369                                         // list ($host, $cmd, $msg) = explode(' ', $raw, 3); 
    370                                         //  
    371                                         // $host = trim($host); 
    372                                         // $cmd  = trim($cmd); 
    373                                         // $msg  = trim($msg); 
    374                                         //  
    375                                         // list ($chan, $msg) = explode(' ', $msg); 
    376                                         // $msg = substr($msg, 1); 
    377                                         //  
    378                                         // print_r(array('host' => $host, 'cmd' => $cmd, 'chan' => $chan, 'msg' => $msg)); 
    379  
    380                                         // if (($offset = strpos($raw, ':', 1)) !== FALSE) 
    381                                         // { 
    382                                         //      if (($offset = substr($raw, $offset, $size)) === $user) 
    383                                         //      { 
    384                                         //              // Process the command 
    385                                         //              $this->send('PRIVMSG '.$chan.' :saying hello?'); 
    386                                         //      } 
    387                                         // } 
    388                                 } 
    389  
    390                                 // Flush the console output 
    391                                 flush(); 
    392                         } 
    393                 } 
    394         } 
    395  
    396308        public function log($level, $message) 
    397309        { 
     
    420332        } 
    421333 
    422         public function add_trigger($callback, $pattern) 
    423         { 
    424                 // TODO 
     334        public function add_trigger($pattern, $callback) 
     335        { 
     336                // Store the trigger and it's callback 
     337                $this->msg_triggers[$pattern] = $callback; 
     338 
    425339                return $this; 
    426340        } 
     
    533447        } 
    534448 
     449        public function response_privmsg($data) 
     450        { 
     451                if ($data['message'] === chr(1).'VERSION'.chr(1)) 
     452                { 
     453                        // Send a CTCP VERSION response 
     454                        $this->response_version($data); 
     455                } 
     456                elseif (substr($data['message'], 0, strlen($this->username)) === $this->username 
     457                    AND $trigger = trim(substr($data['message'], strlen($this->username)), ' :')) 
     458                { 
     459                        // Process triggers 
     460                        foreach ($this->msg_triggers as $pattern => $func) 
     461                        { 
     462                                if (preg_match('/'.$pattern.'/', $trigger, $matches)) 
     463                                { 
     464                                        // Execute the callback trigger: 
     465                                        // callback($this, $command_array, $captures); 
     466                                        call_user_func($func, $this, $data, $matches); 
     467                                } 
     468                        } 
     469                } 
     470        } 
     471 
     472        public function response_version($data) 
     473        { 
     474                // Send a CTCP VERSION response 
     475                $this->send('NOTICE '.$data['sender'].' :'.chr(1).'VERSION Kobot v'.KOHANA_VERSION.' Kohana Team'.chr(1)); 
     476        } 
     477 
    535478} // End Kobot