Assembla home | Assembla project page
 

Changeset 193

Show
Ignore:
Timestamp:
03/29/08 12:59:29 (8 months ago)
Author:
Slynderdale
Message:

Added several bug fixes and also made AdminFunctions obsolete. Moved the fromAdmin function to Base.php and made the Users op check an optional check so Users can be disabled and the admin check will still work. Also added onPHPError, A custom error handler for plugins to capture PHP errors. Also added a pluginLoaded and a static pluginLoaded function to easily check to see if a plugin is loaded.

Now that AdminFunctions is obselete, Admin plugins now need to extend Command and specify the $this->needsAdmin flag to differate itself from normal plugins.

Files:

Legend:

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

    r179 r193  
    44* @see Phergie_Driver_Abstract 
    55*/ 
    6 require_once 'Phergie/Driver/Abstract.php'; 
     6require_once PHERGIE_DRIVER_DIR.'Abstract.php'; 
    77 
    88/** 
    99* @see Phergie_Event_Request 
    1010*/ 
    11 require_once 'Phergie/Event/Request.php'; 
     11require_once PHERGIE_EVENT_DIR.'Request.php'; 
    1212 
    1313/** 
    1414* @see Phergie_Event_Response 
    1515*/ 
    16 require_once 'Phergie/Event/Response.php'; 
     16require_once PHERGIE_EVENT_DIR.'Response.php'; 
    1717 
    1818/** 
     
    6060 
    6161    /** 
     62    * List of administrator hostmasks 
     63    * 
     64    * @var array 
     65    */ 
     66    protected $adminList = array(); 
     67 
     68    /** 
     69    * List of channel ops 
     70    * 
     71    * @var array 
     72    */ 
     73    protected $opList = array(); 
     74 
     75    /** 
    6276     * Flag indicating whether or not the plugin is enabled and receives events 
    6377     * 
     
    7488 
    7589    /** 
     90     * Flag indicating whether or not the plugin is a passive plugin or not 
     91     * 
     92     * @var bool 
     93     */ 
     94    public $passive = false; 
     95 
     96    /** 
    7697    * Sets a reference to the client used to initiate commands. 
    7798    * 
     
    81102    final public function __construct(Phergie_Driver_Abstract $client) 
    82103    { 
     104        set_error_handler(array(__CLASS__, 'onBaseError')); 
    83105        $this->client = $client; 
    84106 
     
    88110        if ($this->needsDir) { 
    89111            $class = new ReflectionClass($name); 
    90             $dir = 
    91                 dirname($class->getFilename()) . 
    92                 DIRECTORY_SEPARATOR . 
    93                 $this->name . 
    94                 DIRECTORY_SEPARATOR; 
     112            $dir = dirname($class->getFilename()) . DS . $this->name . DS; 
    95113            if (!file_exists($dir)) { 
    96114                mkdir($dir); 
     
    101119 
    102120    /** 
     121    * Base error handler for PHP errors. This functions called onPHPError for 
     122    * any clas extending it to give each plugin its own error handler. 
     123    * 
     124    * @return bool 
     125    */ 
     126    public final function onBaseError($errno, $errstr, $errfile, $errline) { 
     127        if (!isset($this)) { 
     128                return false; 
     129        } 
     130        return call_user_func_array(array($this, 'onPHPError'), array($errno, $errstr, $errfile, $errline)); 
     131    } 
     132 
     133    /** 
    103134    * Returns the short name of the current plugin. 
    104135    * 
     
    181212    public function debug($message) 
    182213    { 
    183         $this->client->debug(strtolower($this->name) . ' -> ' . $message); 
     214        $this->client->debug('<' . strtolower($this->name) . '> ' . $message); 
    184215    } 
    185216 
     
    224255    public function decodeTranslit($text, $charSetFrom = 'UTF-8', $charSetTo = 'ISO-8859-1') { 
    225256        $text = html_entity_decode($text, ENT_QUOTES, $charSetFrom); 
    226         $text = preg_replace('/&#0*([0-9]+);/me', '$this->codeToUtf(\\1)', $text); 
    227         $text = preg_replace('/&#x0*([a-f0-9]+);/mei', '$this->codeToUtf(hexdec(\\1))', $text); 
     257        if (strpos($text, '&#') !== false) { 
     258            $text = preg_replace('/&#0*([0-9]+);/me', '$this->codeToUtf(\\1)', $text); 
     259            $text = preg_replace('/&#x0*([a-f0-9]+);/mei', '$this->codeToUtf(hexdec(\\1))', $text); 
     260        } 
    228261 
    229262        // Use the translit extension if installed else fallback on to basic transliteration 
     
    234267            $text = transliterate($text, array('han_transliterate', 'diacritical_remove'), $charSetFrom, $charSetTo); 
    235268        } else { 
    236             $text = strtr($text, '��������������������������������������������������������������', 
     269            $text = strtr($text, 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ', 
    237270                                 'AAAAAAACEEEEIIIIDNOOOOOOUUUUYPYaaaaaaaceeeeiiiidnoooooouuuuypy'); 
    238             $text = preg_replace('{[^a-z0-9&|"#\'\{\}()�^!�\[\]$*���%�`~=+:/;.,?><\\ _-]}i', '', $text); 
     271            $text = preg_replace('{[^a-z0-9&|"#\'\{\}()§^!°\[\]$*¨µ£%´`~=+:/;.,?><\\ _-]}i', '', $text); 
    239272            $text = utf8_decode($text); 
    240273        } 
     
    405438 
    406439    /** 
     440    * Checks if a specified plugin is loaded 
     441    * 
     442    * @param string $plugin Plugin to check 
     443    * @param Phergie_Driver_Abstract $client Client instance 
     444    * @param array $plugins List of short names for plugins that the 
     445    *                       bootstrap file intends to instantiate 
     446    * @return bool 
     447    */ 
     448    public function pluginLoaded($plugin, $plugins = null, $client = null) { 
     449        $plugin = trim($plugin); 
     450        if (!empty($plugin)) { 
     451            if (!$client) { 
     452                $client = $this->client; 
     453            } 
     454            if (!is_array($plugins) || count($plugins) <= 0) { 
     455                $plugins = $this->getPluginList(); 
     456            } 
     457            $plugins = array_map('strtolower', $plugins); 
     458            if (in_array(strtolower($plugin), $plugins) && class_exists('Phergie_Plugin_' . $plugin) && 
     459                call_user_func(array('Phergie_Plugin_' . $plugin, 'checkDependencies'), $client, $plugins)) { 
     460                return true; 
     461            } 
     462        } 
     463        return false; 
     464    } 
     465 
     466    /** 
     467    * Static call to check if a specified plugin is loaded 
     468    * 
     469    * @param string $plugin Plugin to check 
     470    * @param Phergie_Driver_Abstract $client Client instance 
     471    * @param array $plugins List of short names for plugins that the 
     472    *                       bootstrap file intends to instantiate 
     473    * @return bool 
     474    */ 
     475    public static function staticPluginLoaded($plugin, $plugins, $client) { 
     476        $plugin = trim($plugin); 
     477        if (!empty($plugin) && is_array($plugins)) { 
     478            $plugins = array_map('strtolower', $plugins); 
     479            if (in_array(strtolower($plugin), $plugins) && class_exists('Phergie_Plugin_' . $plugin) && 
     480                call_user_func(array('Phergie_Plugin_' . $plugin, 'checkDependencies'), $client, $plugins)) { 
     481                return true; 
     482            } 
     483        } 
     484        return false; 
     485    } 
     486 
     487    /** 
     488    * Returns whether or not a message originated from an authorized admin or 
     489    * op. 
     490    * 
     491    * @param bool $includeOps Include ops in the admin check 
     492    * @return bool TRUE if the message originated from an authorized 
     493    *              individual, FALSE otherwise 
     494    */ 
     495    public function fromAdmin($includeOps = true) 
     496    { 
     497        $class = $this->getName(); 
     498        // Check to see if the current class has any admin or ops settings specified 
     499        if (!isset($this->adminList[$class]) || !isset($this->opList[$class])) { 
     500                // Handle the op settings 
     501                $ini = $this->getPluginIni('ops'); 
     502                if ($ini === null) { 
     503                $ini = $this->getIni('admincommand.ops'); 
     504            } 
     505            $this->opList[$class] = ((strtolower($ini) === 'true' || $ini === '1')); 
     506 
     507            // Handle the admin settings 
     508            $ini = trim($this->getPluginIni('admins') . ' ' . $this->getIni('admincommand.admins')); 
     509            $this->adminList[$class] = (!empty($ini) ? $this->hostmasksToRegex($ini) : null); 
     510            unset($ini); 
     511        } 
     512 
     513        // Try to match mask against admin masks 
     514        if (isset($this->adminList[$class]) 
     515            && preg_match($this->adminList[$class], $this->event->getHostmask())) { 
     516            return true; 
     517        } 
     518 
     519        // Check if is op and ops are admins 
     520        if ($includeOps && isset($this->opList[$class]) && $this->pluginLoaded('Users')) { 
     521            $nick = $this->event->getNick(); 
     522            $source = $this->event->getSource(); 
     523            if (Phergie_Plugin_Users::isOp($nick, $source)) { 
     524                return true; 
     525            } 
     526        } 
     527        return false; 
     528    } 
     529 
     530    /** 
    407531    * Returns whether or not the current environment meets the requirements 
    408532    * of the plugin in order for it to be run, including the PHP version, 
     
    586710 
    587711    /** 
     712    * Handler for when the server sends a kill request. 
     713    * 
     714    * @return void 
     715    */ 
     716    public function onKill() { } 
     717 
     718    /** 
    588719    * Handler for when a server response is received to a client-issued 
    589720    * command. 
     
    592723    */ 
    593724    public function onResponse() { } 
     725 
     726    /** 
     727    * Handler for when PHP throws an error 
     728    * 
     729    * @return void 
     730    */ 
     731    public function onPHPError($errno, $errstr, $errfile, $errline) { return false; } 
    594732 
    595733    /** 
     
    605743    { 
    606744        // Silence output calls if the plugin is muted for that source or globally 
    607         $source = trim($arguments[0]); 
    608         if ((($source && isset($this->muted[$source]) && $this->muted[$source]) || 
     745        $source = null; 
     746        if (isset($arguments[0])) { 
     747            $source = trim($arguments[0]); 
     748        } 
     749        if (((isset($source) && isset($this->muted[$source]) && $this->muted[$source]) || 
    609750                (isset($this->muted['global']) && $this->muted['global'])) && 
    610751                substr($method, 0, 2) === 'do') { 
  • trunk/Phergie/Plugin/Abstract/Command.php

    r179 r193  
    44* @see Phergie_Plugin_Abstract_Base 
    55*/ 
    6 require_once 'Phergie/Plugin/Abstract/Base.php'; 
     6require_once PHERGIE_PLUGIN_DIR.'Abstract'.DS.'Base.php'; 
    77 
    88/** 
     
    1919    */ 
    2020    private $methods; 
     21 
     22    /** 
     23     * Flag indicating whether or not the plugin is an admin plugin or not 
     24     * 
     25     * @var bool 
     26     */ 
     27    public $needsAdmin = false; 
    2128 
    2229    /** 
     
    3643    * 
    3744    * @param string $message Message to analyze 
     45    * @param bool $ignorePrefix Whether or not to ignore the command prefix 
     46    * @param bool $forceBotPrefix Whether or not to require the bot's name as a 
     47    *                             prefix 
    3848    * @return void 
    3949    */ 
     
    5262                } 
    5363            } 
     64        } 
     65 
     66        // Checks to see if the plugin is an admin plugin and run the checks 
     67        if ($this->needsAdmin) { 
     68                if (!$this->fromAdmin()) { 
     69                        return; 
     70                } 
     71                $ignorePrefix = $forceBotPrefix = true; 
    5472        } 
    5573