2010-06-16 01:55:39 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2010-08-03 17:42:02 +01:00
|
|
|
* Phergie
|
2010-06-16 01:55:39 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2010-08-03 17:42:02 +01:00
|
|
|
* @category Phergie
|
2010-06-16 01:55:39 +01:00
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-08-03 17:42:02 +01:00
|
|
|
* Handles parsing and execution of commands sent by users via messages sent
|
2010-06-16 01:55:39 +01:00
|
|
|
* to channels in which the bot is present or directly to the bot.
|
|
|
|
*
|
2010-08-03 17:42:02 +01:00
|
|
|
* @category Phergie
|
2010-06-16 01:55:39 +01:00
|
|
|
* @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
|
2010-08-03 17:42:02 +01:00
|
|
|
* @uses Phergie_Plugin_Message pear.phergie.org
|
2010-06-16 01:55:39 +01:00
|
|
|
*/
|
|
|
|
class Phergie_Plugin_Command extends Phergie_Plugin_Abstract
|
|
|
|
{
|
|
|
|
/**
|
2010-08-03 17:42:02 +01:00
|
|
|
* Prefix for command method names
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const METHOD_PREFIX = 'onCommand';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache for command lookups used to confirm that methods exist and
|
2010-06-16 01:55:39 +01:00
|
|
|
* parameter counts match
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $methods = array();
|
|
|
|
|
|
|
|
/**
|
2010-08-03 17:42:02 +01:00
|
|
|
* Load the Message plugin
|
2010-06-16 01:55:39 +01:00
|
|
|
*
|
2010-08-03 17:42:02 +01:00
|
|
|
* @return void
|
2010-06-16 01:55:39 +01:00
|
|
|
*/
|
2010-08-03 17:42:02 +01:00
|
|
|
public function onLoad()
|
|
|
|
{
|
|
|
|
$plugins = $this->getPluginHandler();
|
|
|
|
$plugins->getPlugin('Message');
|
|
|
|
}
|
2010-06-16 01:55:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Populates the methods cache.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-08-03 17:42:02 +01:00
|
|
|
public function populateMethodCache()
|
2010-06-16 01:55:39 +01:00
|
|
|
{
|
|
|
|
foreach ($this->getPluginHandler() as $plugin) {
|
|
|
|
$reflector = new ReflectionClass($plugin);
|
|
|
|
foreach ($reflector->getMethods() as $method) {
|
|
|
|
$name = $method->getName();
|
2010-08-03 17:42:02 +01:00
|
|
|
if (strpos($name, self::METHOD_PREFIX) === 0
|
2010-06-16 01:55:39 +01:00
|
|
|
&& !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();
|
|
|
|
}
|
|
|
|
|
2010-08-03 17:42:02 +01:00
|
|
|
// Check for a prefixed message
|
|
|
|
$msg = $this->plugins->message->getMessage();
|
|
|
|
if ($msg === false) {
|
|
|
|
return;
|
2010-06-16 01:55:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Separate the command and arguments
|
|
|
|
$parsed = preg_split('/\s+/', $msg, 2);
|
2010-08-03 17:42:02 +01:00
|
|
|
$command = strtolower(array_shift($parsed));
|
2010-06-16 01:55:39 +01:00
|
|
|
$args = count($parsed) ? array_shift($parsed) : '';
|
|
|
|
|
2010-08-03 17:42:02 +01:00
|
|
|
// 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)];
|
|
|
|
}
|
|
|
|
|
2010-06-16 01:55:39 +01:00
|
|
|
// Check to ensure the command exists
|
2010-08-03 17:42:02 +01:00
|
|
|
$method = self::METHOD_PREFIX . ucfirst($command);
|
2010-06-16 01:55:39 +01:00
|
|
|
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']);
|
|
|
|
|
2010-08-03 17:42:02 +01:00
|
|
|
// If the minimum arguments are passed, call the method
|
2010-06-16 01:55:39 +01:00
|
|
|
if ($this->methods[$method]['required'] <= count($args)) {
|
|
|
|
call_user_func_array(
|
|
|
|
array($this->getPluginHandler(), $method),
|
|
|
|
$args
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|