1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
<?php
/**
* Phergie
*
* PHP version 5
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.
* It is also available through the world-wide-web at this URL:
* http://phergie.org/license
*
* @category Phergie
* @package Phergie_Plugin_Command
* @author Phergie Development Team <team@phergie.org>
* @copyright 2008-2010 Phergie Development Team (http://phergie.org)
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_Command
*/
/**
* 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
* @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
*/
class Phergie_Plugin_Command extends Phergie_Plugin_Abstract
{
/**
* Cache for command lookups used to confirm that methods exist and
* parameter counts match
*
* @var array
*/
protected $methods = array();
/**
* Prefix for command method names
*
* @var string
*/
protected $methodPrefix = 'onCommand';
/**
* Populates the methods cache.
*
* @return void
*/
protected function populateMethodCache()
{
foreach ($this->getPluginHandler() as $plugin) {
$reflector = new ReflectionClass($plugin);
foreach ($reflector->getMethods() as $method) {
$name = $method->getName();
if (strpos($name, $this->methodPrefix) === 0
&& !isset($this->methods[$name])
) {
$this->methods[$name] = array(
'total' => $method->getNumberOfParameters(),
'required' => $method->getNumberOfRequiredParameters()
);
}
}
}
}
/**
* Parses a given message and, if its format corresponds to that of a
* defined command, calls the handler method for that command with any
* provided parameters.
*
* @return void
*/
public function onPrivmsg()
{
// Populate the methods cache if needed
if (empty($this->methods)) {
$this->populateMethodCache();
}
// 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);
$method = $this->methodPrefix . ucfirst(strtolower(array_shift($parsed)));
$args = count($parsed) ? array_shift($parsed) : '';
// Check to ensure the command exists
if (empty($this->methods[$method])) {
return;
}
// If no arguments are passed...
if (empty($args)) {
// If the method requires no arguments, call it
if (empty($this->methods[$method]['required'])) {
$this->getPluginHandler()->$method();
}
} else {
// If arguments are passed...
// Parse the arguments
$args = preg_split('/\s+/', $args, $this->methods[$method]['total']);
// If the minimum arguments are passed, call the method
if ($this->methods[$method]['required'] <= count($args)) {
call_user_func_array(
array($this->getPluginHandler(), $method),
$args
);
}
}
}
}
|