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

Preparing to rename Kirc... again.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/kirc/libraries/Kirc.php

    r2047 r2048  
    55 * $Id$ 
    66 * 
    7  * @package    Kirc 
     7 * @package    Kobot 
    88 * @author     Woody Gilk 
    99 * @copyright  (c) 2007-2008 Kohana Team 
    1010 * @license    http://kohanaphp.com/license.html 
    1111 */ 
    12 class Kirc_Core { 
     12class Kobot_Core { 
    1313 
    1414        // The characters that represent a newline 
     
    1717        // Log level: 1 = errors, 2 = debug 
    1818        public $log_level = 1; 
     19 
     20        // Command responses 
     21        protected $responses = array(); 
    1922 
    2023        // IRC socket, MOTD, and stats 
     
    3639        public function __construct($server, $port = NULL, $timeout = NULL) 
    3740        { 
     41                echo Kohana::debug($this->parse(':anthony.freenode.net 353 koboto @ #koboto :koboto @Shadowhand'));exit; 
     42                 
    3843                if (PHP_SAPI !== 'cli') 
    39                         throw new Kirc_Exception('kirc.command_line_only'); 
     44                        throw new Kohana_Exception('kobot.command_line_only'); 
    4045 
    4146                // Close all output buffers 
     
    7378 
    7479                        // Automatically reply to PING comamnds 
    75                         Event::add('kirc.ping', array($this, 'pong')); 
     80                        $this->set_response('ping', array($this, 'pong')); 
    7681                } 
    7782                else 
     
    109114        } 
    110115 
     116        public function set_response($command, $callback) 
     117        { 
     118                if ( ! is_callable($callback)) 
     119                        throw new Kohana_Exception('kobot.invalid_callback'); 
     120 
     121                // Set the response callback 
     122                $this->responses[$command] = $callback; 
     123 
     124                return $this; 
     125        } 
     126 
    111127        public function login($username, $password = NULL, $realname = 'Kohana PHP Bot') 
    112128        { 
     
    118134                $this->stats['last_ping'] = microtime(TRUE); 
    119135 
    120                 // Read the MOTD before continuing 
    121                 Event::add('kirc.375', array($this, 'read_motd')); 
    122                 Event::add('kirc.372', array($this, 'read_motd')); 
    123                 Event::add('kirc.376', array($this, 'read_motd')); 
    124         } 
    125  
    126         public function read_motd() 
    127         { 
    128                 switch (Event::$data['command']) 
     136                // Set the MOTD response 
     137                $this->set_response('375', array($this, 'read_motd')); 
     138                $this->set_response('372', array($this, 'read_motd')); 
     139                $this->set_response('376', array($this, 'read_motd')); 
     140        } 
     141 
     142        public function read_motd($data) 
     143        { 
     144                switch ($data['command']) 
    129145                { 
    130146                        case '375': 
     
    134150                        case '372': 
    135151                                // Read the MOTD 
    136                                 $this->motd[] = substr(Event::$data['message'], 2); 
     152                                $this->motd[] = substr($data['message'], 2); 
    137153                        break; 
    138154                        case '376': 
     
    157173 
    158174                        // Read the USERS command 
    159                         Event::add('kirc.353', array($this, 'read_userlist')); 
    160                 } 
    161         } 
    162  
    163         public function read_userlist() 
    164         { 
    165                 if (strpos(Event::$data['target'], ' @ ') !== FALSE) 
    166                 { 
    167                         // Get the channel name from the target 
    168                         list ($bot, $channel) = explode(' @ ', Event::$data['target'], 2); 
    169  
    170                         // Set the current users 
    171                         $this->channels[$channel] = explode(' ', Event::$data['message']); 
    172  
    173                         // Log the user count 
    174                         $this->log(1, 'Read '.count($this->channels[$channel]).' users'); 
     175                        $this->set_response('353', array($this, 'read_userlist')); 
    175176                } 
    176177        } 
     
    220221        } 
    221222 
    222         public function pong() 
    223         { 
    224                 // Reply with a PONG 
    225                 $this->send('PONG '.substr(Event::$data['message'], 1)); 
    226         } 
    227  
    228223        public function read() 
    229224        { 
     
    248243        protected function parse($raw) 
    249244        { 
    250                 // These will always be returned 
    251                 $sender   = NULL; 
    252                 $sendhost = NULL; 
    253                 $command  = NULL; 
    254                 $target   = NULL; 
    255                 $message  = NULL; 
    256  
    257                 // Split the message 
    258                 $message = explode(' ', trim($raw), 2); 
    259  
    260                 if ( ! empty($message[0]) AND $message[0]{0} === ':') 
    261                 { 
    262                         // Is a receivable command 
    263                         $prefix = substr($message[0], 1); 
     245                // Remove the whitespace garbage 
     246                $raw = trim($raw); 
     247 
     248                // These values are always returned 
     249                $data = array 
     250                ( 
     251                        'sender'   => NULL, 
     252                        'sendhost' => NULL, 
     253                        'command'  => NULL, 
     254                        'target'   => NULL, 
     255                        'message'  => NULL, 
     256                ); 
     257 
     258                // Extract the prefix from the string 
     259                list ($prefix, $str) = explode(' ', $raw, 2); 
     260 
     261                if ( ! empty($prefix) AND $prefix{0} === ':') 
     262                { 
     263                        // A user-level command, like PRIVMSG or NOTICE 
     264                        $prefix = substr($prefix, 1); 
    264265 
    265266                        if (strpos($prefix, '!') !== FALSE) 
    266267                        { 
    267                                 // sender!sendhost 
    268                                 list ($sender, $sendhost) = explode('!', $prefix, 2); 
     268                                // sender@host, typically a user 
     269                                list ($data['sender'], $data['sendhost']) = explode('!', $prefix, 2); 
    269270                        } 
    270271                        else 
    271272                        { 
    272                                 // sender 
    273                                 $sender = $prefix; 
    274                         } 
    275  
    276                         // Separate the command and message 
    277                         list ($command, $params) = explode(' ', $message[1], 2); 
    278  
    279                         if (strpos($params, ' :') !== FALSE) 
    280                         { 
    281                                 // target :message 
    282                                 list ($target, $message) = explode(' :', $params, 2); 
    283                         } 
    284                         elseif ($params{0} === ':') 
    285                         { 
    286                                 // :target 
    287                                 $target = substr($params, 1); 
    288                                 $message = NULL; 
     273                                // sender, Typically a server 
     274                                $data['sender'] = $prefix; 
     275                        } 
     276 
     277                        // CMD str, Extract the command from the remaining string 
     278                        list ($data['command'], $str) = explode(' ', $str, 2); 
     279 
     280                        if (strpos($str, ' :') !== FALSE) 
     281                        { 
     282                                // target :message, some kind of communication 
     283                                list ($data['target'], $data['message']) = explode(' :', $params, 2); 
     284                        } 
     285                        elseif ($str{0} === ':') 
     286                        { 
     287                                // :target, without a message 
     288                                $data['target'] = substr($str, 1); 
    289289                        } 
    290290                        else 
    291291                        { 
    292                                 // target 
    293                                 $target = $params; 
    294                                 $message = NULL; 
     292                                $data['target'] = $str; 
    295293                        } 
    296294                } 
    297295                else 
    298296                { 
    299                         // Is a raw command, like PING 
    300                         $command = $message[0]; 
    301                         $message = empty($message[1]) ? NULL : trim($message[1]); 
    302                 } 
    303  
    304                 return array($sender, $sendhost, $command, $target, $message); 
     297                        // A server-level command, like PING 
     298                        $data['command'] = $prefix; 
     299                        $data['message'] = empty($str) ? NULL : $str; 
     300                } 
     301 
     302                return $data; 
    305303        } 
    306304 
     
    404402        } 
    405403 
    406         protected function url_status($url) 
    407         { 
    408                 if (($status = $this->db_url_status($url)) === NULL) 
    409                 { 
    410                         // Extract the URL params 
    411                         extract(parse_url($url), EXTR_PREFIX_ALL, 'url'); 
    412  
    413                         // Invalid URL by default 
    414                         $status = FALSE; 
    415                         if ($socket = fsockopen($url_host, 80, $errno, $errstr, 6)) 
    416                         { 
    417                                 // Fetch the HTTP HEAD 
    418                                 fwrite($socket, "HEAD $url_path HTTP/1.0\r\nHost: $url_host\r\n\r\n"); 
    419  
    420                                 // Read the response 
    421                                 $status = fgets($socket, 22); 
    422  
    423                                 // Set the response 
    424                                 $status = (strpos($status, '200 OK') !== FALSE); 
    425  
    426                                 // Close the connection 
    427                                 fclose($socket); 
    428                         } 
    429  
    430                         // Save the URL to the database 
    431                         $this->db->insert('urls', array('url' => $url, 'status' => (int) $status)); 
    432                 } 
    433  
    434                 return $status; 
    435         } 
    436  
    437         protected function db_url_status($url) 
    438         { 
    439                 // Fetch the status of the URL 
    440                 $status = $this->db->select('status')->where('url', $url)->limit(1)->get('urls'); 
    441  
    442                 return $status->count() ? (bool) $status->current()->status : NULL; 
    443         } 
    444  
    445 } 
     404        /** 
     405         * Kobot default responses. You can overload these in your own extension class, 
     406         * or attach your own event handlers 
     407         */ 
     408 
     409        public function response_drop($data) 
     410        { 
     411                // Log dropped responses 
     412                $this->log(2, $data); 
     413        } 
     414 
     415        public function response_ping($data) 
     416        { 
     417                // Reply with a PONG 
     418                $this->send('PONG '.substr($data['message'], 1)); 
     419        } 
     420 
     421        // 353 
     422        public function response_userlist($data) 
     423        { 
     424                if (strpos($data['target'], ' @ ') !== FALSE) 
     425                { 
     426                        // Get the channel name from the target 
     427                        list ($bot, $channel) = explode(' @ ', $data['target'], 2); 
     428 
     429                        // Set the current users 
     430                        $this->channels[$channel] = explode(' ', $data['message']); 
     431 
     432                        // Log the user count 
     433                        $this->log(1, 'Read '.count($this->channels[$channel]).' users'); 
     434                } 
     435        } 
     436 
     437} // End Kobot