]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Irc/extlib/phergie/Phergie/Plugin/Command.php
Revert "Merged in Phergie changes"
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Command.php
index 48352b15dd3d8835be01b46d5a0afedf815c3d36..2ab5b69d1d6a50684379c9a9e97696aa292e7d57 100755 (executable)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Phergie
+ * Phergie 
  *
  * PHP version 5
  *
@@ -11,7 +11,7 @@
  * It is also available through the world-wide-web at this URL:
  * http://phergie.org/license
  *
- * @category  Phergie
+ * @category  Phergie 
  * @package   Phergie_Plugin_Command
  * @author    Phergie Development Team <team@phergie.org>
  * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
  */
 
 /**
- * Handles parsing and execution of commands sent by users via messages sent
+ * Handles parsing and execution of commands sent by users via messages sent 
  * to channels in which the bot is present or directly to the bot.
  *
- * @category Phergie
+ * @category Phergie 
  * @package  Phergie_Plugin_Command
  * @author   Phergie Development Team <team@phergie.org>
  * @license  http://phergie.org/license New BSD License
  * @link     http://pear.phergie.org/package/Phergie_Plugin_Command
  * @uses     extension reflection
- * @uses     Phergie_Plugin_Message pear.phergie.org
  */
 class Phergie_Plugin_Command extends Phergie_Plugin_Abstract
 {
     /**
-     * Prefix for command method names
-     *
-     * @var string
-     */
-    const METHOD_PREFIX = 'onCommand';
-
-    /**
-     * Cache for command lookups used to confirm that methods exist and
+     * Cache for command lookups used to confirm that methods exist and 
      * parameter counts match
      *
      * @var array
@@ -49,28 +41,24 @@ class Phergie_Plugin_Command extends Phergie_Plugin_Abstract
     protected $methods = array();
 
     /**
-     * Load the Message plugin
+     * Prefix for command method names
      *
-     * @return void
+     * @var string
      */
-    public function onLoad()
-    {
-        $plugins = $this->getPluginHandler();
-        $plugins->getPlugin('Message');
-    }
+    protected $methodPrefix = 'onCommand';
 
     /**
      * Populates the methods cache.
      *
      * @return void
      */
-    public function populateMethodCache()
+    protected function populateMethodCache()
     {
         foreach ($this->getPluginHandler() as $plugin) {
             $reflector = new ReflectionClass($plugin);
             foreach ($reflector->getMethods() as $method) {
                 $name = $method->getName();
-                if (strpos($name, self::METHOD_PREFIX) === 0
+                if (strpos($name, $this->methodPrefix) === 0 
                     && !isset($this->methods[$name])
                 ) {
                     $this->methods[$name] = array(
@@ -96,26 +84,26 @@ class Phergie_Plugin_Command extends Phergie_Plugin_Abstract
             $this->populateMethodCache();
         }
 
-        // Check for a prefixed message
-        $msg = $this->plugins->message->getMessage();
-        if ($msg === false) {
-            return;
+        // Get the content of the message
+        $event = $this->getEvent();
+        $msg = trim($event->getText());
+        $prefix = $this->getConfig('command.prefix');
+
+        // Check for the command prefix if one is set and needed
+        if ($prefix && $event->isInChannel()) {
+            if (strpos($msg, $prefix) !== 0) {
+                return;
+            } else {
+                $msg = substr($msg, strlen($prefix));
+            }
         }
 
         // Separate the command and arguments
         $parsed = preg_split('/\s+/', $msg, 2);
-        $command = strtolower(array_shift($parsed));
+        $method = $this->methodPrefix . ucfirst(strtolower(array_shift($parsed))); 
         $args = count($parsed) ? array_shift($parsed) : '';
 
-        // Resolve aliases to their corresponding commands
-        $aliases = $this->getConfig('command.aliases', array());
-        $result = preg_grep('/^' . $command . '$/i', array_keys($aliases));
-        if ($result) {
-            $command = $aliases[array_shift($result)];
-        }
-
         // Check to ensure the command exists
-        $method = self::METHOD_PREFIX . ucfirst($command);
         if (empty($this->methods[$method])) {
             return;
         }
@@ -134,7 +122,7 @@ class Phergie_Plugin_Command extends Phergie_Plugin_Abstract
             // Parse the arguments
             $args = preg_split('/\s+/', $args, $this->methods[$method]['total']);
 
-            // If the minimum arguments are passed, call the method
+            // If the minimum arguments are passed, call the method 
             if ($this->methods[$method]['required'] <= count($args)) {
                 call_user_func_array(
                     array($this->getPluginHandler(), $method),