Assembla home | Assembla project page
 

Changeset 61

Show
Ignore:
Timestamp:
02/17/08 17:24:15 (9 months ago)
Author:
tobias382
Message:

* Modified the Dns and Math plugins to use the Command plugin
* Added soda and pop aliases to the Drink plugin
* Modified the Nickserv plugin to use the AdminCommand? plugin
* Added a command to the Nickserv plugin to terminate ghost connections and reclaim the bot's primary nick
* Fixed an issue in the Pong plugin where the server hostname was being returned instead of the client hostname
* Modified the Url plugin to trim titles for HTML pages before returning them

Files:

Legend:

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

    r60 r61  
    1818    * @var array 
    1919    */ 
    20     protected $config
     20    protected $config = array()
    2121 
    2222    /** 
     
    2929    { 
    3030        $name = strtolower($name); 
    31         if (!isset($this->config[$name])) { 
     31        if (!isset ($this->config[$name])) { 
    3232            return null; 
    3333        } 
  • trunk/Phergie/Plugin/Dns.php

    r59 r61  
    22 
    33/** 
    4 * @see Phergie_Event_Handler 
     4* @see Phergie_Plugin_Abstract_Command 
    55*/ 
    6 require_once 'Phergie/Event/Handler.php'; 
     6require_once 'Phergie/Plugin/Abstract/Command.php'; 
    77 
    88/** 
     
    1111* responds with a message containing the lookup result. 
    1212*/ 
    13 class Phergie_Plugin_Dns extends Phergie_Event_Handler 
     13class Phergie_Plugin_Dns extends Phergie_Plugin_Abstract_Command 
    1414{ 
    1515    /** 
    16     * Responds to DNS or reverse DNS lookup requests 
     16    * Processes a DNS or reverse DNS lookup request. 
    1717    * 
     18    * @param string $arg Host or IP address to look up 
    1819    * @return void 
    1920    */ 
    20     public function onPrivmsg(
     21    protected function processRequest($arg
    2122    { 
     23        if (preg_match('/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/', $arg)) { 
     24            $resolved = gethostbyaddr(long2ip(ip2long($arg))); 
     25        } elseif (preg_match('/^(?:[a-z0-9]+\.)+[a-z]{2,6}$/', $arg)) { 
     26            $resolved = gethostbyname($arg); 
     27        } 
     28 
    2229        $source = $this->event->getSource(); 
    23         $text = $this->event->getArgument(1); 
    24  
    25         // Check if we have [rev]dns IP-address and if the IP is valid 
    26         if (preg_match('#^(?:rev)?dns ((?:[0-9]{1,3}\.){3}[0-9]{1,3})$#i', $text, $m) && ip2long($m[1]) !== false) { 
    27                $this->doPrivmsg($source, $m[1] . ' resolved as ' . gethostbyaddr(long2ip(ip2long($m[1])))); 
    28         // Check if we have [rev]dns host 
    29         } elseif (preg_match('#^(?:rev)?dns ((?:[a-z0-9]+\.)+[a-z]{2,6})$#i', $text, $m)) { 
    30            if(($ip = gethostbyname($m[1])) !== $m[1]) { 
    31                 $this->doPrivmsg($source, $m[1] . ' resolved as '.$ip); 
    32            } else { 
    33                 $this->doPrivmsg($source, $m[1] . ' can not be resolved'); 
    34            } 
     30        if (!isset ($resolved)) { 
     31            $this->doPrivmsg($source, $arg . ' cannot be resolved.'); 
     32        } else { 
     33            $this->doPrivmsg($source, $arg . ' resolved to ' . $resolved); 
    3534        } 
    3635    } 
     36 
     37    /** 
     38    * Forwards DNS lookup requests onto a central handler. 
     39    * 
     40    * @param string $host Host to look up 
     41    * @return void 
     42    */ 
     43    public function onDoDns($host) 
     44    { 
     45        $this->processRequest($host); 
     46    } 
     47 
     48    /** 
     49    * Forwards reverse DNS lookup requests onto a central handler. 
     50    * 
     51    * @param string $ip IP address to look up 
     52    * @return void 
     53    */ 
     54    public function onDoRevdns($ip) 
     55    { 
     56        $this->processRequest($ip); 
     57    } 
    3758} 
  • trunk/Phergie/Plugin/Drink.php

    r60 r61  
    234234 
    235235    /** 
    236     * Returns a random record value from a given table 
     236    * Returns a random record value from a given table. 
    237237    * 
    238238    * @string $table Name of the table 
     
    248248    /** 
    249249    * Returns whether or not a given value has characters that may not be 
    250     * displayed correctly 
     250    * displayed correctly. 
    251251    * 
    252252    * @param string $name Value to check 
     
    255255    { 
    256256        return (max(array_map('ord', str_split($name))) > 126); 
     257    } 
     258 
     259    /** 
     260    * Resolves a target to the appropriate nick or pronoun and returns the 
     261    * result. 
     262    * 
     263    * @param string $target Original specified target 
     264    * @return string Resolved target 
     265    */ 
     266    protected function resolveTarget($target) 
     267    { 
     268        $target = trim($target); 
     269 
     270        switch ($target) { 
     271            case 'me': 
     272                $target = $this->event->getNick(); 
     273                break; 
     274            case 'you': 
     275            case $this->getIni('nick'): 
     276                $gender = $this->getIni('gender'); 
     277                if (!$gender || $gender == 'F') { 
     278                    $target = 'herself'; 
     279                } else { 
     280                    $target = 'himself'; 
     281                } 
     282                break; 
     283        } 
     284 
     285        return $target; 
    257286    } 
    258287 
     
    271300        } 
    272301 
    273         if (preg_match('/^me(\s|$)/', $target)) { 
    274             $target = preg_replace('/^me/', $this->event->getNick(), $target); 
    275         } else if ($target == $this->getIni('nick')) { 
    276             $gender = $this->getIni('gender'); 
    277             if (!$gender || $gender == 'F') { 
    278                 $target = 'herself'; 
    279             } else { 
    280                 $target = 'himself'; 
    281             } 
    282         } 
    283  
     302        $target = $this->resolveTarget($target); 
    284303        $drink = $this->getRandomRecord($type); 
    285304 
     
    300319 
    301320    /** 
    302     * Handles beer requests 
     321    * Handles beer requests. 
     322    * 
     323    * @param string $target Target for the request 
     324    * @return void 
     325    */ 
     326    public function onDoBeer($target) 
     327    { 
     328        $this->throwDrink('beer', $target); 
     329    } 
     330 
     331    /** 
     332    * Handles cocktail requests. 
     333    * 
     334    * @param string $target Target for the request 
     335    * @return void 
     336    */ 
     337    public function onDoCocktail($target) 
     338    { 
     339        $this->throwDrink('cocktail', $target); 
     340    } 
     341 
     342    /** 
     343    * Handles coke requests. 
    303344    * 
    304345    * @param string $target Source of the request 
    305346    * @return void 
    306347    */ 
    307     public function onDoBeer($target) 
    308     { 
    309         $this->throwDrink('beer', $target); 
    310     } 
    311  
    312     /** 
    313     * Handles cocktail requests 
    314     * 
    315     * @param string $target Source of the request 
    316     * @return void 
    317     */ 
    318     public function onDoCocktail($target) 
    319     { 
    320         $this->throwDrink('cocktail', $target); 
    321     } 
    322  
    323     /** 
    324     * Handles coke requests 
    325     * 
    326     * @param string $target Source of the request 
    327     * @return void 
    328     */ 
    329348    public function onDoCoke($target) 
    330349    { 
     
    333352 
    334353    /** 
    335     * Handles tea requests 
    336     * 
    337     * @param string $target Source of the request 
     354    * Provides a soda alias for coke requests. 
     355    * 
     356    * @param string $target Target for the request 
     357    * @return void 
     358    */ 
     359    public function onDoSoda($target) 
     360    { 
     361        $this->throwDrink('coke', $target); 
     362    } 
     363 
     364    /** 
     365    * Handles pop requests. 
     366    * 
     367    * @param string $target Target for the request 
     368    * @return void 
     369    */ 
     370    public function onDoPop($target) 
     371    { 
     372        $target = $this->resolveTarget($target); 
     373 
     374        $this->doAction($this->event->getSource(), 'lays ' . $target . ' out flat.'); 
     375    } 
     376 
     377    /** 
     378    * Handles tea requests. 
     379    * 
     380    * @param string $target Target for the request 
    338381    * @return void 
    339382    */ 
  • trunk/Phergie/Plugin/Math.php

    r60 r61  
    22 
    33/** 
    4 * @see Phergie_Event_Handler 
     4* @see Phergie_Plugin_Abstract_Command 
    55*/ 
    6 require_once 'Phergie/Event/Handler.php'; 
     6require_once 'Phergie/Plugin/Abstract/Command.php'; 
    77 
    88/** 
     
    1111* result. 
    1212*/ 
    13 class Phergie_Plugin_Math extends Phergie_Event_Handler 
     13class Phergie_Plugin_Math extends Phergie_Plugin_Abstract_Command 
    1414{ 
    1515    /** 
     
    4040 
    4141    /** 
    42     * Parses a simple arithmetic expression, evaluates it, and returns the 
    43     * result to the sender. 
     42    * Processes a request to perform a calculations. 
     43    * 
     44    * @param string $expr Expression to evaluate 
     45    * @return void 
     46    */ 
     47    protected function processRequest($expr) 
     48    { 
     49        // Replace constants 
     50        $equation = str_ireplace( 
     51            array('pi', 'M_PI()', 'chucknorris', 'inf', ' e '), 
     52            array('M_PI', 'M_PI', 1e10000, 'INF', ' M_E '), 
     53            $expr 
     54        ); 
     55        $equationSrc = $equation; 
     56 
     57        // Parse equation 
     58        $out = ''; 
     59        $ptr = 1; 
     60        $allowcomma = 0; 
     61        while (strlen($equation) > 0) { 
     62            $substr = substr($equation, 0, $ptr); 
     63            // Allowed string 
     64            if (array_search($substr, $this->allowed) !== false) { 
     65                $out .= $substr; 
     66                $equation = substr($equation, $ptr); 
     67                $ptr = 0; 
     68            // Allowed func 
     69            } elseif (array_search($substr, $this->funcs) !== false) { 
     70                $out .= $substr; 
     71                $equation = substr($equation, $ptr); 
     72                $ptr = 0; 
     73                $allowcomma++; 
     74                if ($allowcomma === 1) { 
     75                    $this->allowed[] = ','; 
     76                } 
     77            // Opening parenthesis 
     78            } elseif ($substr === '(') { 
     79                if ($allowcomma > 0) { 
     80                    $allowcomma++; 
     81                } 
     82                $out .= $substr; 
     83                $equation = substr($equation, $ptr); 
     84                $ptr = 0; 
     85            // Closing parenthesis 
     86            } elseif ($substr === ')') { 
     87                if ($allowcomma > 0) { 
     88                    $allowcomma--; 
     89                    if($allowcomma === 0) { 
     90                        array_pop($this->allowed); 
     91                    } 
     92                } 
     93 
     94                $out .= $substr; 
     95                $equation = substr($equation, $ptr); 
     96                $ptr = 0; 
     97            // Parse error if we've consumed the entire equation without finding anything valid 
     98            } elseif ($ptr >= strlen($equation)) { 
     99                $this->doPrivmsg($source, 'Syntax error at "' . $substr . '" in equation "' . $equationSrc . '"'); 
     100                return; 
     101            } else { 
     102                $ptr++; 
     103            } 
     104        } 
     105        $res = @eval('return ' . $out . ';'); 
     106        $source = $this->event->getSource(); 
     107        if($res === false) { 
     108            $this->doPrivmsg($source, 'Computation error, division by zero?'); 
     109        } else { 
     110            $this->doPrivmsg($source, $res); 
     111        } 
     112    } 
     113 
     114    /** 
     115    * Forwards math commands onto a central handler. 
    44116    * 
    45117    * @return void 
    46118    */ 
    47     public function onPrivmsg(
     119    public function onDoMath($expr
    48120    { 
    49         $source = $this->event->getSource(); 
    50         $message = $this->event->getArgument(1); 
    51         if (preg_match('/^(?:math|calc) /', $message)) { 
    52             // Get equation 
    53             $equation = substr($message, 5); 
    54             // Replace constants 
    55             $equation = str_ireplace( 
    56                 array('pi', 'M_PI()', 'chucknorris', 'inf', ' e '), 
    57                 array('M_PI', 'M_PI', 1e10000, 'INF', ' M_E '), 
    58                 $equation 
    59             ); 
    60             $equationSrc = $equation; 
     121        $this->processRequest($expr); 
     122    } 
    61123 
    62             // Parse equation 
    63             $out = ''; 
    64             $ptr = 1; 
    65             $allowcomma = 0; 
    66             while (strlen($equation) > 0) { 
    67                 $substr = substr($equation, 0, $ptr); 
    68                 // Allowed string 
    69                 if (array_search($substr, $this->allowed) !== false) { 
    70                     $out .= $substr; 
    71                     $equation = substr($equation, $ptr); 
    72                     $ptr = 0; 
    73                 // Allowed func 
    74                 } elseif (array_search($substr, $this->funcs) !== false) { 
    75                     $out .= $substr; 
    76                     $equation = substr($equation, $ptr); 
    77                     $ptr = 0; 
    78                     $allowcomma++; 
    79                     if ($allowcomma === 1) { 
    80                         $this->allowed[] = ','; 
    81                     } 
    82                 // Opening parenthesis 
    83                 } elseif ($substr === '(') { 
    84                     if ($allowcomma > 0) { 
    85                         $allowcomma++; 
    86                     } 
    87                     $out .= $substr; 
    88                     $equation = substr($equation, $ptr); 
    89                     $ptr = 0; 
    90                 // Closing parenthesis 
    91                 } elseif ($substr === ')') { 
    92                     if ($allowcomma > 0) { 
    93                         $allowcomma--; 
    94                         if($allowcomma === 0) { 
    95                             array_pop($this->allowed); 
    96                         } 
    97                     } 
    98  
    99                     $out .= $substr; 
    100                     $equation = substr($equation, $ptr); 
    101                     $ptr = 0; 
    102                 // Parse error if we've consumed the entire equation without finding anything valid 
    103                 } elseif ($ptr >= strlen($equation)) { 
    104                     $this->doPrivmsg($source, 'Syntax error at "' . $substr . '" in equation "' . $equationSrc . '"'); 
    105                     return; 
    106                 } else { 
    107                     $ptr++; 
    108                 } 
    109             } 
    110             $res = @eval('return ' . $out . ';'); 
    111             if($res === false) { 
    112                 $this->doPrivmsg($source, 'Computation error, division by zero?'); 
    113             } else { 
    114                 $this->doPrivmsg($source, $res); 
    115             } 
    116         } 
     124    /** 
     125    * Forwards calc commands onto a central handler. 
     126    * 
     127    * @return void 
     128    */ 
     129    public function onDoCalc($expr) 
     130    { 
     131        $this->processRequest($expr); 
    117132    } 
    118133} 
  • trunk/Phergie/Plugin/Nickserv.php

    r60 r61  
    22 
    33/** 
    4 * @see Phergie_Event_Handler 
     4* @see Phergie_Plugin_Abstract_AdminCommand 
    55*/ 
    6 require_once 'Phergie/Event/Handler.php'; 
     6require_once 'Phergie/Plugin/Abstract/Command.php'; 
    77 
    88/** 
     
    1313* with NickServ for the nick used by the bot. 
    1414*/ 
    15 class Phergie_Plugin_Nickserv extends Phergie_Event_Handler 
     15class Phergie_Plugin_Nickserv extends Phergie_Plugin_Abstract_Command 
    1616{ 
    1717    /** 
     
    1919    * 
    2020    * @var int 
     21    */ 
     22    protected $index; 
     23 
     24    /** 
     25    * Primary nick for the bot 
     26    * 
     27    * @var string 
    2128    */ 
    2229    protected $nick; 
     
    2936    public function init() 
    3037    { 
    31         $this->nick = -1; 
     38        $this->nick = $this->getIni('nick'); 
     39        $this->index = -1; 
    3240    } 
    3341 
    3442    /** 
    3543    * Checks for a notice from NickServ and responds accordingly if it is an 
    36     * authentication request. 
     44    * authentication request or a notice that a ghost connection has been 
     45    * killed. 
    3746    * 
    38     * @todo Add a check to reclaim the bot's nick if it is already in use 
    3947    * @return void 
    4048    */ 
    4149    public function onNotice() 
    4250    { 
    43         if ($this->event->getNick() == 'NickServ' 
    44             && $this->event->getArgument(1) == 'This nickname is owned by someone else') { 
    45             $password = $this->getIni('password'); 
    46             if (! empty($password)) { 
    47                 $this->doPrivmsg('NickServ', 'IDENTIFY ' . $password); 
     51        if ($this->event->getNick() == 'NickServ') { 
     52            $message = $this->event->getArgument(1); 
     53            if ($message == 'This nickname is owned by someone else') { 
     54                $password = $this->getIni('password'); 
     55                if (! empty($password)) { 
     56                    $this->doPrivmsg('NickServ', 'IDENTIFY ' . $password); 
     57                } 
     58            } elseif (preg_match('/^.*' . $this->nick . '.* has been killed', $message)) { 
     59                $this->doNick($this->nick); 
     60                $this->setIni('nick', $this->nick); 
    4861            } 
    4962        } 
     
    5164 
    5265    /** 
    53     * Intercepts a "nickname in use" response in cases where the bot may not 
    54     * have disconnected properly and a ghost connection still has its nick in 
    55     * reserve when it tries to reconnect. 
     66    * Switches to alternate nicks as needed when nick collisions occur. 
    5667    * 
    5768    * @return void 
     
    6071    { 
    6172        if ($this->event->getCode() == Phergie_Event_Response::ERR_NICKNAMEINUSE) { 
    62             $this->nick++; 
    63             $altnick = $this->getIni('altnick' . $this->nick); 
     73            $this->index++; 
     74            $altnick = $this->getIni('altnick' . $this->index); 
    6475            if ($altnick) { 
    6576                $this->doNick($altnick); 
     
    7081 
    7182    /** 
    72     * Handles communications with NickServ to terminate ghost connections and 
    73     * recover the bot's nick. 
     83    * Provides a command to terminate ghost connections. 
    7484    * 
    7585    * @return void 
    7686    */ 
    77     /*public function onPrivmsg() 
     87    public function onDoGhostbust() 
    7888    { 
    7989        $password = $this->getIni('password'); 
    80         if ($this->event->getNick() == 'NickServ' 
    81             && ! empty ($password)) { 
    82             $msg = $this->event->getArgument(1); 
    83             if (preg_match('/The nickname .* has been recovered/', $msg)) { 
    84                 $this->doPrivmsg( 
    85                     'NickServ', 
    86                     'RELEASE ' . $this->getIni('nick') . ' ' . $password 
    87                 ); 
    88             } elseif (preg_match('/The nickname .* has been released/', $msg)) { 
    89                 $this->doNick($this->getIni('nick')); 
    90             } 
     90 
     91        if (!empty ($password) && $this->index != -1) { 
     92            $this->doPrivmsg( 
     93                'NickServ', 
     94                'GHOST ' . $this->nick . ' ' . $password 
     95            ); 
    9196        } 
    92     }*/ 
     97    } 
    9398} 
  • trunk/Phergie/Plugin/Pong.php

    r59 r61  
    1313{ 
    1414    /** 
     15    * Hostname for the bot 
     16    * 
     17    * @var string 
     18    */ 
     19    protected $hostname; 
     20 
     21    /** 
     22    * Stores the hostname for the bot to use in PONG responses. 
     23    * 
     24    * @return void 
     25    */ 
     26    public function init() 
     27    { 
     28        $this->hostname = gethostbyaddr(file_get_contents('http://whatismyip.com/automation/n09230945.asp')); 
     29    } 
     30 
     31    /** 
    1532    * Responds to PING requests from the server to indicate that the client 
    1633    * connection is still active. 
     
    2037    public function onPing() 
    2138    { 
    22         $this->doPong($this->event->getArgument(0)); 
     39        $this->doPong($this->hostname); 
    2340    } 
    2441} 
  • trunk/Phergie/Plugin/Url.php

    r60 r61  
    110110        $out = strtr($out, 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ', 'AAAAAAACEEEEIIIIDNOOOOOOUUUUYPYaaaaaaaceeeeiiiidnoooooouuuuypy'); 
    111111        $out = preg_replace('{[^a-z0-9&|"#\'\{\}()§^!°\[\]$*¨µ£%´`~=+:/;.,?><\\ _-]}i', '', $out); 
     112        $out = trim($out); 
    112113        if($trim > 0) { 
    113114            $out = substr($out, 0, $trim) . (strlen($out) > $trim ? '...' : '');