Changeset 24

Show
Ignore:
Timestamp:
02/09/08 13:53:00 (5 years ago)
Author:
tobias382
Message:

Fixes #14
* Added plugin short name retrieval and configuration namespacing methods to the base event handler class
* Added Command class, modified AdminCommand? to extend it, and modified Quit and JoinPart? to use it
* Updated Cron, Nickserv, RSSParser, and Autojoin to use the new configuration namespacing features
* Removed a debugging echo left in the Users::onResponse method

Location:
trunk/Phergie/Event
Files:
1 added
9 modified

Legend:

Unmodified
Added
Removed
  • trunk/Phergie/Event/Handler.php

    r21 r24  
    3030 
    3131    /** 
    32     * Sets a reference to the client used to initiate commands 
     32    * Sets a reference to the client used to initiate commands. 
    3333    * 
    3434    * @param Phergie_Client $client 
     
    4040 
    4141    /** 
    42      * Initializes the plugin 
    43      * 
    44      * Should it require initialization, just extend this method 
    45      */ 
     42    * Returns the short name of the current plugin. 
     43    * 
     44    * @return string 
     45    */ 
     46    public function getName() 
     47    { 
     48        return substr(get_class($this), 22); 
     49    } 
     50 
     51    /** 
     52    * Returns the value of a configuration setting for a plugin. 
     53    * 
     54    * @param string $setting Name of the setting 
     55    * @param string $plugin Name of the plugin, defaults to the current  
     56    *                       plugin (optional) 
     57    * @return string Value of the setting 
     58    */ 
     59    public function getIni($setting, $plugin = null) 
     60    { 
     61        if ($plugin === null) { 
     62            $plugin = $this->getName();  
     63        } 
     64        return $this->_client->getIni($plugin . '.' . $setting); 
     65    } 
     66 
     67    /** 
     68    * Sets the value of a configuration setting for a plugin. 
     69    * 
     70    * @param string $setting Name of the setting 
     71    * @param string $value New value for the setting 
     72    * @param string $plugin Name of the plugin, defaults to the current  
     73    *                       plugin (optional) 
     74    */ 
     75    public function setIni($setting, $value, $plugin = null) 
     76    { 
     77        if ($plugin === null) { 
     78            $plugin = $this->getName(); 
     79        } 
     80        $this->_client->setIni($plugin . '.' . $setting, $value); 
     81    } 
     82 
     83    /** 
     84    * Initializes the plugin. Should it require initialization, just  
     85    * override this method. 
     86    */ 
    4687    public function init() { } 
    4788 
  • trunk/Phergie/Event/Handler/AdminCommand.php

    r21 r24  
    22 
    33/** 
    4 * @see Phergie_Event_Handler 
     4* @see Phergie_Event_Handler_Command 
    55*/ 
    6 require_once 'Phergie/Event/Handler.php'; 
     6require_once 'Phergie/Event/Handler/Command.php'; 
    77 
    88/** 
     
    3131 * </code> 
    3232 */ 
    33 abstract class Phergie_Event_Handler_AdminCommand extends Phergie_Event_Handler 
     33abstract class Phergie_Event_Handler_AdminCommand extends Phergie_Event_Handler_Command 
    3434{ 
    3535        protected $_admins = array(); 
    36         protected $_adminOps = array(); 
     36        protected $_ops = array(); 
    3737 
    38     public function isAdmin(Phergie_Event_Request $event) 
     38    public function fromAdmin(Phergie_Event_Request $event) 
    3939    { 
    40         $class = substr(get_class($this), 22); 
    41         if (!isset($this->_admins[$class])) { 
    42                 $this->initialize($class); 
    43         } 
     40        $class = $this->getName(); 
    4441        $mask = $event->getNick() .'!'. $event->getUsername() .'@'. $event->getHost(); 
    4542                // Check if is op and ops are admins 
    46                 if($this->_adminOps[$class] === true && Phergie_Event_Handler_Users::isOp($event->getNick(), $event->getArgument(0)) === true) { 
     43                if($this->_ops[$class] === true  
     44            && Phergie_Event_Handler_Users::isOp($event->getNick(), $event->getArgument(0)) === true) { 
    4745                        return true; 
    4846                } 
    4947        // Try to match mask against admin masks 
    50         foreach ($this->_admins[$class] as $admin) { 
    51                 if (preg_match('{^'.$admin.'$}', $mask)) { 
    52                         return true; 
    53                 } 
    54         } 
     48        if (isset($this->_admins[$class])) { 
     49            foreach ($this->_admins[$class] as $admin) { 
     50                if (preg_match('{^'.$admin.'$}', $mask)) { 
     51                    return true; 
     52                } 
     53            } 
     54        } 
    5555        return false; 
    5656    } 
    5757 
    58     protected function initialize($class) 
     58    public function onPrivmsg(Phergie_Event_Request $event) 
    5959    { 
    60         // Set adminOps values 
    61         $ini = $this->getIni($class.'.admin_ops'); 
     60        if ($this->fromAdmin($event)  
     61            && preg_match('/^' . $this->getConnection()->getNick() . ':?\s+(.*)$/', $event->getArgument(1), $m)) { 
     62            $this->processCommand($m[1], $event); 
     63        } 
     64    } 
     65 
     66    public function init() 
     67    { 
     68        $class = $this->getName(); 
     69 
     70        // Set ops values 
     71        $ini = $this->getIni('ops'); 
    6272        if ($ini===null) { 
    63                 $ini = $this->getIni('admincommand.admin_ops'); 
     73                $ini = $this->getIni('ops', 'admincommand'); 
    6474        } 
    6575        if ($ini==='true' || $ini==='1') { 
    66                 $this->_adminOps[$class] = true; 
     76                $this->_ops[$class] = true; 
    6777        } else { 
    68                 $this->_adminOps[$class] = false; 
     78                $this->_ops[$class] = false; 
    6979        } 
    7080 
    7181        // Set admins values 
    72         $ini = $this->getIni($class.'.admins'); 
     82        $ini = $this->getIni('admins'); 
    7383        if ($ini===null) { 
    74                 $ini = $this->getIni('admincommand.admins'); 
     84                $ini = $this->getIni('admins', 'admincommand'); 
    7585        } 
    7686        $admins = preg_split('#[\s\r\n,]+#', $ini); 
  • trunk/Phergie/Event/Handler/Autojoin.php

    r20 r24  
    77    public function onResponse(Phergie_Event_Response $response) 
    88    { 
    9         $channels = $this->getIni('autojoin.channels'); 
     9        $channels = $this->getIni('channels'); 
    1010        if (!empty($channels) 
    1111            && $response->getCode() == Phergie_Event_Response::RPL_ENDOFMOTD) { 
  • trunk/Phergie/Event/Handler/Cron.php

    r20 r24  
    3636    { 
    3737                if($this->ttl === null) { 
    38                         $class = substr(get_class($this), 22); 
    39                         if ((($time = $this->getIni($class.'.time_to_live')) !== null) || $time = $this->defaultTTL) { 
     38                        if ((($time = $this->getIni('time_to_live')) !== null) || $time = $this->defaultTTL) { 
    4039                                $this->ttl = $time; 
    4140                        } 
  • trunk/Phergie/Event/Handler/JoinPart.php

    r20 r24  
    88class Phergie_Event_Handler_JoinPart extends Phergie_Event_Handler_AdminCommand 
    99{ 
    10     public function onPrivmsg(Phergie_Event_Request $event) 
     10    /** 
     11    * Joins the specified channel. 
     12    * 
     13    * @param string $channel Name of the channel to join 
     14    */ 
     15    public function onDoJoin($channel) 
    1116    { 
    12                 // Tries to match join #chan or part [#chan] (parts the chan where the command is called if no chan provided) 
    13         if (preg_match('{^(join|part)\s*(#.*)?$}i', $event->getArgument(1), $m)) { 
    14                 // Checks admin 
    15                 if ($this->isAdmin($event, __CLASS__)) { 
    16                         if ($m[1] === 'join' && $m[2] != null) { 
    17                                 $this->doJoin($m[2]); 
    18                         } elseif ($m[1] === 'part' && ($m[2] != null || substr($event->getArgument(0), 0, 1) === '#')) { 
    19                                 $this->doPart($m[2] == null ? $event->getArgument(0) : $m[2]); 
    20                         } 
    21                 } 
    22         } 
     17        $this->doJoin($channel); 
     18    } 
     19 
     20    /** 
     21    * Parts either the specified channel or the current channel if none is  
     22    * specified. 
     23    * 
     24    * @param mixed $spec A string containing the name of the channel to part 
     25    *                    or an instance of Phergie_Event_Request for the  
     26    *                    original event that occurred 
     27    */ 
     28    public function onDoPart($spec) 
     29    { 
     30        if ($spec instanceof Phergie_Event_Request) { 
     31            $this->doPart($spec->getArgument(0)); 
     32        } else { 
     33            $this->doPart($spec); 
     34        } 
    2335    } 
    2436} 
  • trunk/Phergie/Event/Handler/Nickserv.php

    r1 r24  
    99        if ($event->getNick() == 'NickServ' 
    1010            && $event->getArgument(1) == 'This nickname is owned by someone else') { 
    11             $password = $this->getIni('nickserv.password'); 
     11            $password = $this->getIni('password'); 
    1212            if (!empty($password)) { 
    1313                $this->doPrivmsg('NickServ', 'IDENTIFY ' . $password);  
  • trunk/Phergie/Event/Handler/Quit.php

    r20 r24  
    88class Phergie_Event_Handler_Quit extends Phergie_Event_Handler_AdminCommand 
    99{ 
    10     public function onPrivmsg(Phergie_Event_Request $event) 
     10    public function onDoQuit(Phergie_Event_Request $event) 
    1111    { 
    12         if($event->getArgument(1)=='quit' && $this->isAdmin($event)) { 
    13                 $this->doQuit('by request of ' . $event->getNick()); 
    14         } 
     12        $this->doQuit('by request of ' . $event->getNick()); 
    1513    } 
    1614} 
  • trunk/Phergie/Event/Handler/RSSParser.php

    r23 r24  
    1919                $i = 0; 
    2020                $this->feeds = array(); 
    21                 while ($i < 10 || ($this->getIni('rssparser.feed'.$i) !== null && $this->getIni('rssparser.chans'.$i) !== null)) { 
    22                         $this->feeds[] = array($this->getIni('rssparser.feed'.$i), preg_split('#[\s\r\n,]+#', $this->getIni('rssparser.chans'.$i))); 
     21                while ($i < 10 || ($this->getIni('feed'.$i) !== null && $this->getIni('chans'.$i) !== null)) { 
     22                        $this->feeds[] = array($this->getIni('feed'.$i), preg_split('#[\s\r\n,]+#', $this->getIni('chans'.$i))); 
    2323                        $i++; 
    2424                } 
  • trunk/Phergie/Event/Handler/Users.php

    r17 r24  
    6666    { 
    6767                if ($event->getCode() == Phergie_Event_Response::RPL_NAMREPLY) { 
    68         echo $event->getDescription()."\n"; 
    6968                        $desc = $event->getDescription(); 
    7069                        $desc = substr($desc, strpos($desc, '=')+2);