Assembla home | Assembla project page
 

Changeset 96

Show
Ignore:
Timestamp:
03/01/08 20:50:42 (9 months ago)
Author:
Seldaek
Message:

+ Adds Keepalive/Timeout

Files:

Legend:

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

    r82 r96  
    162162* Execute the event handling loop for the client 
    163163*/ 
    164 $client->run(); 
     164while(true) { 
     165        $state = $client->run(); 
     166 
     167        switch($state) 
     168        { 
     169                case Phergie_Driver_Abstract::RETURN_RECONNECT: 
     170                        sleep(1); 
     171                        break; 
     172                case Phergie_Driver_Abstract::RETURN_KEEPALIVE: 
     173                        sleep(15); 
     174                        break; 
     175                case Phergie_Driver_Abstract::RETURN_END: 
     176                        break 2; 
     177        } 
     178
  • trunk/Phergie/Driver/Abstract.php

    r92 r96  
    1212abstract class Phergie_Driver_Abstract 
    1313{ 
     14        /**#@+ 
     15         * Return codes for the run function, that tells the Bot script whether 
     16         * it should re-run or not 
     17         */ 
     18        const RETURN_RECONNECT = "reconnect"; 
     19        const RETURN_KEEPALIVE = "keepalive"; 
     20        const RETURN_END = "end"; 
     21        /**#@-*/ 
     22 
    1423    /** 
    1524    * Associative array mapping configuration setting names to their 
     
    112121    * 
    113122    * @param string $reason Reason for connection termination (optional) 
    114     * @return void 
    115     */ 
    116     public abstract function doQuit($reason = null); 
     123    * @param bool $reconnect if true, the bot will reconnect to the server 
     124    * @return void 
     125    */ 
     126    public abstract function doQuit($reason = null, $reconnect = false); 
    117127 
    118128    /** 
  • trunk/Phergie/Driver/Streams.php

    r88 r96  
    117117    public function run() 
    118118    { 
     119        $returnCode = $this->getIni('keepalive') ? self::RETURN_KEEPALIVE : self::RETURN_END; 
    119120        $server = $this->getIni('server'); 
    120121        $port = $this->getIni('port'); 
     
    128129            $errstr 
    129130        ); 
     131 
     132        if ($this->getIni('timeout') !== null) { 
     133                stream_set_timeout($this->socket, $this->getIni('timeout') * 60); 
     134        } elseif ($this->getIni('keepalive')) { 
     135                stream_set_timeout($this->socket, 400); 
     136                } 
    130137 
    131138        unset($port); 
     
    166173            while (empty ($buffer)) { 
    167174                $buffer = fgets($this->socket, 512); 
     175 
     176                                $checkTimeout = stream_get_meta_data($this->socket); 
     177                                if ($checkTimeout['timed_out'] === true) { 
     178                                        $this->debug('Timed out'); 
     179                        foreach ($this->plugins as $plugin) { 
     180                            $plugin->shutdown(); 
     181                        } 
     182                                        break 2; 
     183                                } 
    168184            } 
    169185            $buffer = rtrim($buffer); 
     
    260276                    $plugin->shutdown(); 
    261277                } 
     278                if (isset($this->queue['quit'][0][1])) { 
     279                        $returnCode = ($this->queue['quit'][0][1] === true ? self::RETURN_RECONNECT : self::RETURN_END); 
     280                } 
    262281                $this->doQuit($reason); 
    263282                break; 
    264283            } 
     284 
    265285            unset($this->queue, $event, $command, $arguments); 
    266286        } 
    267287 
    268288        fclose($this->socket); 
     289 
     290        return $returnCode; 
    269291    } 
    270292 
     
    299321    * 
    300322    * @param string $reason Reason for connection termination (optional) 
    301     */ 
    302     public function doQuit($reason = null) 
    303     { 
    304         $this->send(Phergie_Event_Request::TYPE_QUIT, array($reason)); 
     323    * @param bool $reconnect if true, the bot will reconnect to the server 
     324    */ 
     325    public function doQuit($reason = null, $reconnect = false) 
     326    { 
     327        $this->send(Phergie_Event_Request::TYPE_QUIT, array($reason, $reconnect)); 
    305328    } 
    306329 
  • trunk/Phergie/Plugin/Abstract/AdminCommand.php

    r78 r96  
    9696                // Escape regex meta characters 
    9797                $admin = str_replace( 
    98                     array('\\', '^', '$', '.', '[', ']', '|', '(', ')', '?', '+', '{', '}'), 
     98                    array('\\',   '^',   '$',   '.',   '[',   ']',   '|',   '(',   ')',   '?',   '+',   '{',  '}'), 
    9999                    array('\\\\', '\\^', '\\$', '\\.', '\\[', '\\]', '\\|', '\\(', '\\)', '\\?', '\\+', '\\{', '\\}'), 
    100100                    $admin 
     
    110110    * Returns whether or not the plugin's dependencies are met. 
    111111    * 
    112     * @param array $plugins List of short names for plugins that the  
     112    * @param array $plugins List of short names for plugins that the 
    113113    *                       bootstrap file intends to instantiate 
    114114    * @return bool TRUE if the plugins dependencies are met, FALSE otherwise 
  • trunk/Phergie/phergie.ini

    r93 r96  
    2929password = 
    3030 
     31; keepalive : 
     32;    boolean flag indicating whether or not the bot should reconnect when it 
     33;    gets disconnected from the server, defaults to false. 
     34;    if this is set to true, you should set timeout value to 6 or above. 
     35keepalive = false 
     36 
     37; timeout : 
     38;    defines the number of minutes to wait until a connection times out, if the 
     39;    bot gets disconnected when it shouldn't (i.e. when your connection didn't 
     40;    die), increase the timeout value so it waits longer before disconnecting. 
     41;    0 means it will never time out, but if you use keepalive you should set 
     42;    this to 6 or above. 
     43timeout = 
     44 
    3145; gender : 
    3246;   M or F to indicate the gender of the bot for instances when the bot must 
     
    3751;   boolean flag indicating whether or not the bot is allowed to use curses, 
    3852;   bad words, etc. defaults to false. 
    39 curses = 
     53curses = false 
    4054 
    4155; plugins : 
     
    5064;    class would be Autojoin) 
    5165; plugins = all 
    52 plugins = all 
     66plugins = "all" 
    5367 
    5468; debug : 
     
    5771 
    5872; log : 
    59 ;    path to a file to which debugging output will be written if debugging  
     73;    path to a file to which debugging output will be written if debugging 
    6074;    mode is enabled 
    6175log = debug.log 
     
    6882;    positive integer indicating the maximum number of possible meanings for 
    6983;    an acronym that should be displayed in a given instance (defaults to 5) 
    70 acronym.limit = 
     84acronym.limit = 5 
    7185 
    7286; acronym.filter : 
    7387;    comma- or space-delimited list of acronyms for which meanings should not 
    7488;    be returned 
    75 acronym.filter = 
     89acronym.filter = "" 
    7690 
    7791;----------------------------------------------------------------------------- 
     
    8397;    bot's admin features, must be enclosed by double quotes and entries are 
    8498;    separated by spaces or commas 
    85 admincommand.admins = 
     99admincommand.admins = "" 
    86100 
    87101; admincommand.ops : 
     
    106120;    a comma-delimited list of channels which the bot should automatically 
    107121;    join upon successfully connecting 
    108 autojoin.channels = 
     122autojoin.channels = "" 
    109123 
    110124;----------------------------------------------------------------------------- 
     
    146160; karma.static : 
    147161;   If non-empty, the static karma response to use for the bot's nick 
    148 karma.static = 
     162karma.static = "" 
    149163 
    150164;-----------------------------------------------------------------------------