Assembla home | Assembla project page
 

Changeset 103

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

* Moved logic to convert a hostmask expression list to a regular expression to a new method hostmasksToRegex in the base driver
* Added support for a new configuration setting to ignore events from specific users in the streams driver
* Added a new command uptime to the Debug plugin

Files:

Legend:

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

    r96 r103  
    5858 
    5959    /** 
    60     * Parses a IRC hostmask and sets nick, user and host bits 
    61    
    62     * @param string $hostmask Hostmask to parse 
    63     * @param string $nick Container for the nick 
    64     * @param string $user Container for the username 
    65     * @param string $host Container for the hostname 
    66     * @return void 
    67     */ 
     60    * Parses a IRC hostmask and sets nick, user and host bits. 
     61   
     62    * @param string $hostmask Hostmask to parse 
     63    * @param string $nick Container for the nick 
     64    * @param string $user Container for the username 
     65    * @param string $host Container for the hostname 
     66    * @return void 
     67    */ 
    6868    public function parseHostmask($hostmask, &$nick, &$user, &$host) 
    6969    { 
     
    7373            $host = $hostmask; 
    7474        } 
     75    } 
     76 
     77    /** 
     78    * Converts a delimited string of hostmasks into a regular expression  
     79    * that will match any hostmask in the original string. 
     80    * 
     81    * @param string $list Delimited string of hostmasks 
     82    * @return string Regular expression 
     83    */ 
     84    public function hostmasksToRegex($list) 
     85    { 
     86        $patterns = array(); 
     87 
     88        foreach (preg_split('#[\s\r\n,]+#', $list) as $hostmask) { 
     89            // Find out which chars are present in the config mask and exclude them from the regex match 
     90            $excluded = ''; 
     91            if (strpos($hostmask, '!')!==false) { 
     92                $excluded .= '!'; 
     93            } 
     94            if (strpos($hostmask, '@')!==false) { 
     95                $excluded .= '@'; 
     96            } 
     97 
     98            // Escape regex meta characters 
     99            $hostmask = str_replace( 
     100                array('\\',   '^',   '$',   '.',   '[',   ']',   '|',   '(',   ')',   '?',   '+',   '{',   '}'), 
     101                array('\\\\', '\\^', '\\$', '\\.', '\\[', '\\]', '\\|', '\\(', '\\)', '\\?', '\\+', '\\{', '\\}'), 
     102                $hostmask 
     103            ); 
     104 
     105            // Replace * so that they match correctly in a regex 
     106            $patterns[] = str_replace('*', ($excluded === '' ? '.*' : '[^'.$excluded.']*'), $hostmask); 
     107        } 
     108 
     109        return ('#^' . implode('|', $patterns) . '$#'); 
    75110    } 
    76111 
     
    102137 
    103138        /** 
    104         * Returns a plugin instance 
    105        
    106         * @param $plugin The plugin class name (without the Phergie_Plugin_ prefix) 
    107         * @return Phergie_Plugin_Abstract_Base 
    108         */ 
     139        * Returns a plugin instance 
     140       
     141        * @param $plugin The plugin class name (without the Phergie_Plugin_ prefix) 
     142        * @return Phergie_Plugin_Abstract_Base 
     143        */ 
    109144        public abstract function getPlugin($plugin); 
    110145 
  • trunk/Phergie/Driver/Streams.php

    r98 r103  
    165165 
    166166        unset($params); 
     167 
     168        $ignore = $this->hostmasksToRegex($this->getIni('ignore')); 
    167169 
    168170        while(true) { 
     
    249251 
    250252            foreach ($this->plugins as $plugin) { 
    251                 if ($plugin->enabled === false) { // Skip disabled plugins 
     253                // Skip disabled plugins 
     254                if ($plugin->enabled === false) { 
    252255                        continue; 
    253256                } 
     
    255258                if ($event instanceof Phergie_Event_Response) { 
    256259                    $plugin->onResponse(); 
    257                 } else { 
     260                // Skip events from ignored users 
     261                } elseif (!preg_match($ignore, $event->getHostmask())) { 
    258262                    call_user_func(array($plugin, 'on' . ucfirst($event->getType()))); 
    259263                } 
  • trunk/Phergie/Plugin/Abstract/AdminCommand.php

    r96 r103  
    8383        } 
    8484        if (!empty ($ini)) { 
    85             $admins = preg_split('#[\s\r\n,]+#', $ini); 
    86             foreach ($admins as $admin) { 
    87                 // Find out which chars are present in the config mask and exclude them from the regex match 
    88                 $excluded = ''; 
    89                 if (strpos($admin, '!')!==false) { 
    90                     $excluded .= '!'; 
    91                 } 
    92                 if (strpos($admin, '@')!==false) { 
    93                     $excluded .= '@'; 
    94                 } 
    95  
    96                 // Escape regex meta characters 
    97                 $admin = str_replace( 
    98                     array('\\',   '^',   '$',   '.',   '[',   ']',   '|',   '(',   ')',   '?',   '+',   '{',   '}'), 
    99                     array('\\\\', '\\^', '\\$', '\\.', '\\[', '\\]', '\\|', '\\(', '\\)', '\\?', '\\+', '\\{', '\\}'), 
    100                     $admin 
    101                 ); 
    102                 // Replace * so that they match correctly in a regex 
    103                 $this->admins[$class][] = str_replace('*', ($excluded === '' ? '.*' : '[^'.$excluded.']*'), $admin); 
    104             } 
    105             $this->admins[$class] = '#^' . implode('|', $this->admins[$class]) . '$#'; 
     85            $this->admins[$class] = $this->hostmasksToRegex($ini);  
    10686        } 
    10787    } 
  • trunk/Phergie/Plugin/Debug.php

    r60 r103  
    1313{ 
    1414    /** 
     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 
     33    /** 
    1534    * Responds to requests for statistics related to the bot's memory 
    1635    * consumption. 
     
    2342        $this->doPrivmsg($this->event->getSource(), $text); 
    2443    } 
     44 
     45    /** 
     46    * Returns the amount of time the bot has been up. 
     47    * 
     48    * @return void 
     49    */ 
     50    public function onDoUptime() 
     51    { 
     52        $diff = time() - $this->uptime; 
     53        $uptime = array(); 
     54 
     55        $days = floor($diff / 86400); 
     56        if ($days > 0) { 
     57            $uptime[] = $days . 'd'; 
     58            $diff = $diff % 86400; 
     59        } 
     60 
     61        $hours = floor($diff / 3600); 
     62        if ($hours > 0) { 
     63            $uptime[] = $hours . 'h'; 
     64            $diff = $diff % 3600; 
     65        } 
     66 
     67        $minutes = floor($diff / 60); 
     68        if ($minutes > 0) { 
     69            $uptime[] = $minutes . 'm'; 
     70            $diff = $diff % 60; 
     71        } 
     72 
     73        $uptime[] = $diff . 's'; 
     74 
     75        $uptime = 'uptime : ' . implode(' ', $uptime); 
     76 
     77        $this->doPrivmsg($this->event->getSource(), $uptime); 
     78    } 
    2579} 
  • trunk/Phergie/phergie.ini

    r102 r103  
    5454;   bad words, etc. defaults to false. 
    5555curses = false 
     56 
     57; ignore : 
     58;   a comma- or space-delimited list of hostmasks (may contain wildcards  
     59;   using *) of users from which events should be ignored (i. e. not  
     60;   processed by plugins), surrounded by double-quotes 
     61ignore =  
    5662 
    5763; plugins :