Assembla home | Assembla project page
 

Changeset 166

Show
Ignore:
Timestamp:
03/11/08 21:58:10 (8 months ago)
Author:
Slynderdale
Message:

Added full CTCP as well as raw handlers. onRaw is called every time Phergie gets a response from the server and $this->event->getRawBuffer(); can be used to get the raw buffer from the server. On the CTCP support end, I added onVersion, onTime, onCtcp and also made it so both server pings and CTCP pings get handled by onPing. If the first argument is set in onPing, then its a CTCP ping. I also added onVersionreply, onTimereply, onPingreply and onCtcpreply as well.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Phergie/Driver/Streams.php

    r148 r166  
    7575 
    7676    /** 
     77    * The time the bot started 
     78    * 
     79    * @var int 
     80    */ 
     81    protected $startTime; 
     82 
     83    /** 
    7784    * Constructor to initialize instance properties. 
    7885    */ 
     
    120127 
    121128    /** 
     129    * Returns the start time. 
     130    * 
     131    * @return int 
     132    */ 
     133    public function getStartTime() 
     134    { 
     135        return $this->startTime; 
     136    } 
     137 
     138    /** 
    122139    * Executes a continuous loop in which the client listens for events from 
    123140    * the server and processes them until the connection is terminated. 
     
    127144    public function run() 
    128145    { 
     146        $this->startTime = time(); 
    129147        $returnCode = $this->getIni('keepalive') ? self::RETURN_KEEPALIVE : self::RETURN_END; 
    130148        $server = $this->getIni('server'); 
     
    226244                    break; 
    227245                case 'notice': 
    228                     $args = explode(' :', $args); 
     246                    $temp = preg_split('/ :?/', trim($args), 2); 
     247                    if(substr($temp[1], 0, 1) === chr(1) && substr($temp[1], -1) === chr(1)) { 
     248                        $ctcp = trim(substr($temp[1], 1, -1)); 
     249                            list ($cmd, $args) = explode(' ', $ctcp.' ', 2); 
     250                        $cmd = strtolower($cmd); 
     251 
     252                       // Check for the type of Notice message and handle it accordingly 
     253                       if ($cmd == 'action') { 
     254                           // Return sender as source and the action as its first argument 
     255                           $args = array($nick, trim($args)); 
     256                       } elseif ($cmd == 'ping' || $cmd == 'version' || $cmd == 'time') { 
     257                           // Return sender as source and the rest of the args as its first argument 
     258                           $cmd = $cmd.'reply'; 
     259                           $args = array($nick, trim($args)); 
     260                       } else { 
     261                           // Return sender as source and the ctcp as its first argument 
     262                           $cmd = 'ctcpreply'; 
     263                           $args = array($nick, $ctcp); 
     264                       } 
     265                    } else { 
     266                        $args = preg_split('/ :?/', $args, 2); 
     267                    } 
    229268                    break; 
    230269                case 'oper': 
     
    240279                    break; 
    241280                case 'privmsg': 
    242                     if ($args[0] == chr(1)) { 
    243                         /** 
    244                         * This will likely need to be changed if support is added 
    245                         * for CTCP commands other than ACTION. 
    246                         */ 
    247                         list ($target, $args) = explode(' ', $args, 2); 
    248                         $args = explode(' ', substr($args, 1, -1), 2); 
    249                         $cmd = strtolower(array_shift($args)); 
    250                         array_unshift($args, $target); 
     281                    $temp = preg_split('/ :?/', trim($args), 2); 
     282                    // Check to see if its a CTCP request, if so, handle it accordingly 
     283                    if(substr($temp[1], 0, 1) === chr(1) && substr($temp[1], -1) === chr(1)) { 
     284                        $ctcp = trim(substr($temp[1], 1, -1)); 
     285                            list ($cmd, $args) = explode(' ', $ctcp.' ', 2); 
     286                        $cmd = strtolower($cmd); 
     287 
     288                       // Check for the type of CTCP message and handle it accordingly 
     289                       if ($cmd == 'action') { 
     290                           $botnick = $this->getIni('nick'); 
     291                           // If sent within a channel, use the chanel as the source, else use the sender 
     292                           $source = (strtolower($temp[0]) == strtolower($botnick) ? $nick : $temp[0]); 
     293                           // Return channel/sender as source and the action as its first argument 
     294                           $args = array($source, trim($args)); 
     295                       } elseif ($cmd == 'ping' && !empty($args)) { 
     296                           // Return nick of sender as source and the handshake as its first argument 
     297                           $args = array($nick, trim($args)); 
     298                       } elseif (($cmd == 'version' || $cmd == 'time') && empty($args)) { 
     299                           // Return nick of sender as source and the ctcp as its first argument 
     300                           $args = array($nick, $ctcp); 
     301                       } else { 
     302                           // Return nick of sender as source and the ctcp as its first argument 
     303                           $cmd = 'ctcp'; 
     304                           $args = array($nick, $ctcp); 
     305                       } 
    251306                    } else { 
    252                         $args = explode(' :', $args, 2); 
     307                        $args = preg_split('/ :?/', $args, 2); 
    253308                    } 
    254309                    break; 
     
    259314                $event->setCode($cmd); 
    260315                $event->setDescription($args); 
     316                $event->setRawBuffer($buffer); 
    261317            } else { 
    262318                $event = new Phergie_Event_Request(); 
     
    268324                    $event->setNick($nick); 
    269325                } 
     326                $event->setRawBuffer($buffer); 
    270327            } 
    271328 
     
    282339                    call_user_func(array($plugin, 'on' . ucfirst($event->getType()))); 
    283340                } 
     341                // onRaw Handler 
     342                $plugin->onRaw(); 
    284343            } 
    285344            $this->queueing = false; 
     
    534593    *                         (optional) 
    535594    */ 
    536     protected function sendCtcp($target, $command, array $arguments = array()) 
    537     { 
    538         $this->doPrivmsg($target, chr(1) . strtoupper($command) . ' ' . implode(' ', $arguments) . chr(1)); 
     595    public function doCtcp($target, $command, $arguments = '') 
     596    { 
     597        if (is_array($arguments)) { 
     598            $arguments = implode(' ', $arguments); 
     599        } 
     600        $this->doPrivmsg($target, chr(1) . trim(strtoupper($command) . ' ' . $arguments) . chr(1)); 
    539601    } 
    540602 
     
    547609    public function doAction($target, $text) 
    548610    { 
    549         $this->sendCtcp($target, Phergie_Event_Request::TYPE_ACTION, array($text)); 
     611        $this->doCtcp($target, Phergie_Event_Request::TYPE_ACTION, array($text)); 
     612    } 
     613 
     614    /** 
     615    * Sends a ping reply to a nick. 
     616    * 
     617    * @param string $target User nickname 
     618    * @param string $hash The ping hash to use in the handshake 
     619    */ 
     620    public function doPingReply($target, $hash) 
     621    { 
     622        $this->doCtcpReply($target, 'ping', $hash); 
     623    } 
     624 
     625    /** 
     626    * Sends a version reply to a nick. 
     627    * 
     628    * @param string $target User nickname 
     629    * @param string $version The version to send 
     630    */ 
     631    public function doVersionReply($target, $version='') 
     632    { 
     633        $version = trim($version); 
     634        if (empty($version)) { 
     635                $version = 'Phergie '.PHERGIE_VERSION.' - An IRC bot written in PHP (http://www.phergie.com)'; 
     636        } 
     637        $this->doCtcpReply($target, 'version', $version); 
     638    } 
     639 
     640    /** 
     641    * Sends a time reply to a nick 
     642    * 
     643    * @param string $target User nickname 
     644    * @param string $time The time to send 
     645    */ 
     646    public function doTimeReply($target, $time='') 
     647    { 
     648        $time = trim($time); 
     649        if (empty($time) || ctype_digit($time)) { 
     650            if (empty($time)) $time = time(); 
     651            $time = date('D M d H:i:s o', $time); 
     652        } 
     653        $this->doCtcpReply($target, 'time', $time); 
     654    } 
     655 
     656    /** 
     657    * Sends a ctcp reply to a nick 
     658    * 
     659    * @param string $target User nickname 
     660    * @param string $reply The CTCP reply to send 
     661    */ 
     662    public function doCtcpReply($target, $command, $reply='') 
     663    { 
     664        $command = trim($command); 
     665        if (empty($command)) 
     666            return; 
     667        $this->send(Phergie_Event_Request::TYPE_NOTICE, array($target, chr(1).trim(strtoupper($command).' '.$reply).chr(1))); 
    550668    } 
    551669} 
  • trunk/Phergie/Event/Request.php

    r86 r166  
    8686 
    8787    /** 
    88     * Mapping of event types to their named parameters  
     88    * Mapping of event types to their named parameters 
    8989    * 
    9090    * @var array 
     
    9999            'channel' => 0 
    100100        ), 
    101          
     101 
    102102        self::TYPE_KICK => array( 
    103103            'channel' => 0, 
     
    115115            'limit'    => 2, 
    116116            'user'     => 3, 
    117             'banmask' => 4  
     117            'banmask' => 4 
    118118        ), 
    119119 
     
    132132            'text'     => 1 
    133133        ), 
    134              
     134 
    135135        self::TYPE_ACTION => array( 
    136136            'target' => 0, 
     
    173173    */ 
    174174    protected $arguments; 
     175 
     176    /** 
     177    * The raw buffer that was sent by the server 
     178    * 
     179    * @var string 
     180    */ 
     181    protected $rawBuffer; 
    175182 
    176183    /** 
     
    314321 
    315322    /** 
     323    * Sets the raw buffer for the given event 
     324    * 
     325    * @param string $buffer 
     326    * @return void 
     327    */ 
     328    public function setRawBuffer($buffer) 
     329    { 
     330        $this->rawBuffer = $buffer; 
     331    } 
     332 
     333    /** 
     334    * Returns the raw buffer that was sent from the server for that event 
     335    * 
     336    * @return string 
     337    */ 
     338    public function getRawBuffer() 
     339    { 
     340        return $this->rawBuffer; 
     341    } 
     342 
     343    /** 
    316344    * Returns the channel name or user nick representing the source of the 
    317345    * event. 
     
    361389    * 
    362390    * @param string $name Name of the method called 
    363     * @param array $arguments Arguments passed to the method (should always  
     391    * @param array $arguments Arguments passed to the method (should always 
    364392    *                         be empty) 
    365393    * @return mixed 
     
    369397        if (count($arguments) == 0 
    370398            && substr($name, 0, 3) == 'get') { 
    371             return $this->getArgument(substr($name, 3));  
     399            return $this->getArgument(substr($name, 3)); 
    372400        } 
    373401    } 
  • trunk/Phergie/Event/Response.php

    r57 r166  
    846846 
    847847    /** 
     848    * The raw buffer that was sent by the server 
     849    * 
     850    * @var string 
     851    */ 
     852    protected $rawBuffer; 
     853 
     854    /** 
    848855    * Sets the reply code sent by the server. 
    849856    * 
     
    884891        return $this->description; 
    885892    } 
     893 
     894    /** 
     895    * Sets the raw buffer for the given event 
     896    * 
     897    * @param string $buffer 
     898    * @return void 
     899    */ 
     900    public function setRawBuffer($buffer) 
     901    { 
     902        $this->rawBuffer = $buffer; 
     903    } 
     904 
     905    /** 
     906    * Returns the raw buffer that was sent from the server for that event 
     907    * 
     908    * @return string 
     909    */ 
     910    public function getRawBuffer() 
     911    { 
     912        return $this->rawBuffer; 
     913    } 
    886914} 
  • trunk/Phergie/Plugin/Abstract/Base.php

    r160 r166  
    370370 
    371371    /** 
     372    * Converts a given integer/timestamp into days, minutes and seconds 
     373    * 
     374    * @param int $time The time/integer to calulate the values from 
     375    * @return string 
     376    */ 
     377    public function getCountdown($time) 
     378    { 
     379        $return = array(); 
     380 
     381        $days = floor($time / 86400); 
     382        if ($days > 0) { 
     383            $return[] = $days . 'd'; 
     384            $time = $time % 86400; 
     385        } 
     386 
     387        $hours = floor($time / 3600); 
     388        if ($hours > 0) { 
     389            $return[] = $hours . 'h'; 
     390            $time = $time % 3600; 
     391        } 
     392 
     393        $minutes = floor($time / 60); 
     394        if ($minutes > 0) { 
     395            $return[] = $minutes . 'm'; 
     396            $time = $time % 60; 
     397        } 
     398 
     399        $return[] = $time . 's'; 
     400 
     401        return implode(' ', $return); 
     402    } 
     403 
     404    /** 
    372405    * Returns whether or not the current environment meets the requirements 
    373406    * of the plugin in order for it to be run, including the PHP version, 
     
    458491 
    459492    /** 
     493    * Handler for when an action is received from a channel or user 
     494    * 
     495    * @return void 
     496    */ 
     497    public function onAction() { } 
     498 
     499    /** 
    460500    * Handler for when a notice is received. 
    461501    * 
     
    472512 
    473513    /** 
    474     * Handler for when the server checks the client connection to ensure 
    475     * activity. 
     514    * Handler for when the server or a userchecks the client connection to 
     515    * ensure activity. 
    476516    * 
    477517    * @return void 
    478518    */ 
    479519    public function onPing() { } 
     520 
     521    /** 
     522    * Handler for when the server sends a CTCP Time request 
     523    * 
     524    * @return void 
     525    */ 
     526    public function onTime() { } 
     527 
     528    /** 
     529    * Handler for when the server sends a CTCP Version request 
     530    * 
     531    * @return void 
     532    */ 
     533    public function onVersion() { } 
     534 
     535    /** 
     536    * Handler for when the server sends a CTCP request 
     537    * 
     538    * @return void 
     539    */ 
     540    public function onCtcp() { } 
     541 
     542    /** 
     543    * Handler for the reply received when a ping CTCP is sent 
     544    * 
     545    * 
     546    * @return void 
     547    */ 
     548    public function onPingreply() { } 
     549 
     550    /** 
     551    * Handler for the reply received when a time CTCP is sent 
     552    * 
     553    * @return void 
     554    */ 
     555    public function onTimereply() { } 
     556 
     557    /** 
     558    * Handler for the reply received when a version CTCP is sent 
     559    * 
     560    * @return void 
     561    */ 
     562    public function onVersionreply() { } 
     563 
     564    /** 
     565    * Handler for the reply received when a CTCP is sent 
     566    * 
     567    * @return void 
     568    */ 
     569    public function onCtcpreply() { } 
     570 
     571    /** 
     572    * Handler for raw requests from the server 
     573    * 
     574    * @return void 
     575    */ 
     576    public function onRaw() { } 
    480577 
    481578    /** 
     
    512609                return false; 
    513610        } 
     611 
    514612        return call_user_func_array(array($this->client, $method), $arguments); 
    515613    } 
  • trunk/Phergie/Plugin/Abstract/Cron.php

    r71 r166  
    6464 
    6565    /** 
    66     * Checks to see if the run method should be called when a PING event 
     66    * Checks to see if the run method should be called when a raw event 
    6767    * occurs. 
    6868    * 
    6969    * @return void 
    7070    */ 
    71     public function onPing() 
    72     { 
    73         $this->check(); 
    74     } 
    75  
    76     /** 
    77     * Checks to see if the run method should be called when a message is 
    78     * received. 
    79     * 
    80     * @return void 
    81     */ 
    82     public function onPrivmsg() 
     71    public function onRaw() 
    8372    { 
    8473        $this->check(); 
  • trunk/Phergie/Plugin/Debug.php

    r146 r166  
    1212class Phergie_Plugin_Debug extends Phergie_Plugin_Abstract_AdminCommand 
    1313{ 
    14     /** 
    15     * Timestamp used to calculate uptime 
    16     * 
    17     * @var int 
    18     */ 
    19     protected $uptime; 
    20  
    21     /** 
    22     * Initializes a local timestamp used to calculate uptime. 
    23     * 
    24     * @return void 
    25     */ 
    26     public function init() 
    27     { 
    28         parent::init(); 
    29  
    30         $this->uptime = time(); 
    31     } 
    32  
    3314    /** 
    3415    * Responds to requests for statistics related to the bot's memory 
     
    5233    public function onDoUptime() 
    5334    { 
    54         $diff = time() - $this->uptime; 
    55         $uptime = array(); 
    56  
    57         $days = floor($diff / 86400); 
    58         if ($days > 0) { 
    59             $uptime[] = $days . 'd'; 
    60             $diff = $diff % 86400; 
    61         } 
    62  
    63         $hours = floor($diff / 3600); 
    64         if ($hours > 0) { 
    65             $uptime[] = $hours . 'h'; 
    66             $diff = $diff % 3600; 
    67         } 
    68  
    69         $minutes = floor($diff / 60); 
    70         if ($minutes > 0) { 
    71             $uptime[] = $minutes . 'm'; 
    72             $diff = $diff % 60; 
    73         } 
    74  
    75         $uptime[] = $diff . 's'; 
    76  
    77         $uptime = 'uptime : ' . implode(' ', $uptime); 
     35        $uptime = 'Uptime: ' . $this->getCountdown(time() - $this->getStartTime()); 
    7836 
    7937        $this->doPrivmsg($this->event->getSource(), $uptime); 
  • trunk/Phergie/Plugin/FeedTicker.php

    r138 r166  
    122122 
    123123    /** 
    124     * Overrides Cron method to add checkQueue to this, so that whenever a user 
    125     * sends a message to a channel in which the bot is present, the queue is 
    126     * checked. This behavior attempts to prevent the bot from spamming the 
    127     * channel if no users are conversing and everything is retained in the 
    128     * queue until channel activity is detected. 
     124    * Whenever a user sends a message to a channel in which the bot is present, 
     125    * the queue is checked. This behavior attempts to prevent the bot from 
     126    * spamming the channel if no users are conversing and everything is retained 
     127    * in the queue until channel activity is detected. 
    129128    * 
    130129    * @return void 
     
    132131    public function onPrivmsg() 
    133132    { 
    134         parent::onPrivmsg(); 
    135  
    136133        $this->checkQueue(); 
    137134    } 
  • trunk/Phergie/Plugin/Pong.php

    r68 r166  
    2020    public function onPing() 
    2121    { 
    22         $this->doPong($this->event->getArgument(0)); 
     22        // Check for a handshake hash, otherwise its a server ping request 
     23        if (!$this->event->getArgument(1)) { 
     24            $this->doPong($this->event->getArgument(0)); 
     25        } 
    2326    } 
    2427}