Assembla home | Assembla project page
 

root/trunk/Phergie/Plugin/Abstract/Command.php

Revision 231, 3.8 kB (checked in by Slynderdale, 5 months ago)

Made it optional to have a bot prefix for admin commands if a command prefix is set.

Line 
1 <?php
2
3 /**
4  * Handles parsing and execution of commands by users via messages sent to
5  * channels or directly to the bot.
6  */
7 abstract class Phergie_Plugin_Abstract_Command extends Phergie_Plugin_Abstract_Base
8 {
9     /**
10      * Cache for command lookups used to confirm that corresponding methods
11      * exist and parameter counts match
12      *
13      * @var array
14      */
15     private $methods;
16
17     /**
18      * Intercepts a message and processes any contained recognized commands.
19      *
20      * @return void
21      */
22     public function onPrivmsg()
23     {
24         $this->processCommand($this->event->getArgument(1));
25     }
26
27     /**
28      * Parses a given message and, if its format corresponds to that of a
29      * defined command, calls the handler method for that command with any
30      * provided parameters.
31      *
32      * @param string $message Message to analyze
33      * @param bool $ignorePrefix Whether or not to ignore the command prefix
34      * @param bool $forceBotPrefix Whether or not to require the bot's name as a
35      *                             prefix
36      * @return void
37      */
38     protected final function processCommand($message, $ignorePrefix = false, $forceBotPrefix = false)
39     {
40         if (!$this->methods) {
41             $class = new ReflectionClass(get_class($this));
42             foreach($class->getMethods() as $method) {
43                 $name = $method->getName();
44                 if (strpos($name, 'onDo') === 0) {
45                     $this->methods[strtolower(substr($name, 4))] = array(
46                         $method->getNumberOfParameters(),
47                         $method->getNumberOfRequiredParameters()
48                     );
49                 }
50             }
51         }
52
53         // Get the command prefix
54         $commandPrefix = trim($this->getIni('command_prefix'));
55
56         // Checks to see if the plugin is an admin plugin
57         if ($this->needsAdmin && empty($commandPrefix)) {
58             $forceBotPrefix = true;
59         }
60
61         // Checks to see if the command was prefixed with the bot's name
62         $source = $this->event->getSource();
63         $user = $this->event->getNick();
64         $bot = $this->getIni('nick');
65         // The Botprefix regex expression
66         $exp = '(' . preg_quote($bot) . '\s*[:,>]?\s+)' . (!($source[0] == '#' && $forceBotPrefix) ? '?' : '');
67
68         if (preg_match('/^' . $exp . '(\S+)(?:[\s' . chr(240) . ']+(.*))?$/', $message, $match)) {
69             $botPrefix = $match[1];
70             $command = strtolower($match[2]);
71             $params = isset($match[3]) ? $match[3] : array();
72
73             // Checks the command for a prefix if one is specified in the config
74             if (!empty($commandPrefix)) {
75                 if ($hasPrefix = (substr($command, 0, strlen($commandPrefix)) == $commandPrefix)) {
76                     $command = substr($command, strlen($commandPrefix));
77                 }
78                 if ($botPrefix) {
79                     $ignorePrefix = true;
80                 }
81             }
82
83             if ((empty($commandPrefix) || $hasPrefix || $ignorePrefix) && isset($this->methods[$command])) {
84                 if ($this->needsAdmin && !$this->fromAdmin()) {
85                     $this->doNotice($user, 'You do not have permission to use the command "' . $command . '."');
86                     return;
87                 }
88
89                 $method = 'onDo' . ucfirst($command);
90                 if (empty($params)) {
91                     if (!$this->methods[$command][1]) {
92                         $this->$method();
93                     }
94                 } else {
95                     $params = preg_split('/[\s' . chr(240) . ']+/', $params, $this->methods[$command][0]);
96                     if ($this->methods[$command][1] <= count($params)) {
97                         call_user_func_array(array($this, $method), $params);
98                     }
99                 }
100             }
101         }
102     }
103 }
Note: See TracBrowser for help on using the browser.