Show
Ignore:
Timestamp:
02/14/2008 06:43:47 PM (9 months ago)
Author:
Shadowhand
Message:

Updating Kirc and setting properties

Files:
1 modified

Legend:

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

    • Property svn:eol-style set to LF
    • Property copyright set to Copyright (c) 2007-2008 Kohana Team
    • Property svn:keywords set to Id
    r2043 r2047  
    1818        public $log_level = 1; 
    1919 
    20         // IRC socket and stats 
     20        // IRC socket, MOTD, and stats 
    2121        protected $socket; 
     22        protected $motd; 
    2223        protected $stats = array 
    2324        ( 
     
    7071                        // Connection is complete 
    7172                        $this->log(1, 'Connected to '.$server.':'.$port); 
     73 
     74                        // Automatically reply to PING comamnds 
     75                        Event::add('kirc.ping', array($this, 'pong')); 
    7276                } 
    7377                else 
     
    100104                if ($level >= $this->log_level) 
    101105                { 
     106                        // Display the message with a timestamp, flush the output 
    102107                        echo date('Y-m-d g:i:s').' --- '.$message."\n"; flush(); 
    103108                } 
     
    114119 
    115120                // Read the MOTD before continuing 
    116                 $this->read(375, 376, FALSE); 
    117                 $this->log(2, 'Received MOTD (suppressed)'); 
     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']) 
     129                { 
     130                        case '375': 
     131                                // Prepare to read the MOTD 
     132                                $this->motd = array(); 
     133                        break; 
     134                        case '372': 
     135                                // Read the MOTD 
     136                                $this->motd[] = substr(Event::$data['message'], 2); 
     137                        break; 
     138                        case '376': 
     139                                // Log the number of lines in the MOTD 
     140                                $this->log(1, 'Read '.count($this->motd).' MOTD lines'); 
     141 
     142                                // Make the MOTD into a string 
     143                                $this->motd = implode("\n", $this->motd); 
     144                        break; 
     145                } 
    118146        } 
    119147 
     
    122150                if (empty($this->channels[$channel])) 
    123151                { 
     152                        // Set the channel as joined 
     153                        $this->channels[$channel] = array(); 
     154 
    124155                        // Join the channel 
    125156                        $this->send('JOIN '.$channel); 
    126157 
    127158                        // Read the USERS command 
    128                         $this->channels[$channel] = explode(':', $this->read(353)); 
    129                         $this->channels[$channel] = explode(' ', $this->channels[$channel][1]); 
    130  
    131                         // The end of the USERS command 
    132                         $this->read(366, FALSE); 
    133                         $this->log(2, 'Received USERS (suppressed)'); 
     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'); 
    134175                } 
    135176        } 
     
    179220        } 
    180221 
    181         public function read($start = NULL, $end = NULL, $return = TRUE) 
    182         { 
    183                 if (is_bool($end)) 
    184                 { 
    185                         // Return value and end are shifted one place 
    186                         $return = $end; 
    187                         $end = NULL; 
    188                 } 
    189  
    190                 // Make sure the start and end are strings 
    191                 ($start === NULL) or $start = (string) $start; 
    192                 ($end   === NULL) or $end   = (string) $end; 
    193  
    194                 $buffer = ''; 
     222        public function pong() 
     223        { 
     224                // Reply with a PONG 
     225                $this->send('PONG '.substr(Event::$data['message'], 1)); 
     226        } 
     227 
     228        public function read() 
     229        { 
    195230                while ( ! feof($this->socket)) 
    196231                { 
    197                         while ($raw = fgets($this->socket, 128)) 
    198                         { 
    199                                 if ( ! empty($start)) 
    200                                 { 
    201                                         // Get the host and command from the stream 
    202                                         list ($host, $cmd, $msg) = explode(' ', $raw, 3); 
    203  
    204                                         if ($cmd === $start) 
    205                                         { 
    206                                                 // Trim the message 
    207                                                 $msg = trim($msg); 
    208  
    209                                                 if (empty($end)) 
    210                                                 { 
    211                                                         // Return the message 
    212                                                         return ($return === TRUE) ? $msg : NULL; 
    213                                                 } 
    214  
    215                                                 $buffer .= $msg; 
    216                                         } 
    217  
    218                                         if ( ! empty($buffer)) 
    219                                         { 
    220                                                 if ($return === TRUE) 
    221                                                 { 
    222                                                         // Only add to the buffer for returns 
    223                                                         $buffer .= $msg; 
    224                                                 } 
    225  
    226                                                 if ($cmd === $end) 
    227                                                 { 
    228                                                         // Return the complete buffer 
    229                                                         return ($return === TRUE) ? $buffer : NULL; 
    230                                                 } 
    231                                         } 
    232                                 } 
    233                                 else 
    234                                 { 
    235                                         $this->log(2, '<<< '.trim($raw)); 
    236                                 } 
     232                        while ($raw = fgets($this->socket, 512)) 
     233                        { 
     234                                $this->log(2, '<<< '.trim($raw)); 
     235 
     236                                // Parse the command 
     237                                $data = array_combine(array('sender', 'sendhost', 'command', 'target', 'message'), $this->parse($raw)); 
     238 
     239                                // Run the event 
     240                                Event::run('kirc.'.strtolower($data['command']), $data); 
    237241                        } 
    238242                        // One half-second is high enough interactivity 
    239243                        usleep(500000); 
    240244                } 
     245        } 
     246 
     247        // Return: array(sender, sendhost, command, target, message) 
     248        protected function parse($raw) 
     249        { 
     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); 
     264 
     265                        if (strpos($prefix, '!') !== FALSE) 
     266                        { 
     267                                // sender!sendhost 
     268                                list ($sender, $sendhost) = explode('!', $prefix, 2); 
     269                        } 
     270                        else 
     271                        { 
     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; 
     289                        } 
     290                        else 
     291                        { 
     292                                // target 
     293                                $target = $params; 
     294                                $message = NULL; 
     295                        } 
     296                } 
     297                else 
     298                { 
     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); 
    241305        } 
    242306