* @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_Message */ /** * Generalized plugin providing utility methods for * prefix and bot named based message extraction. * * @category Phergie * @package Phergie_Plugin_Message * @author Phergie Development Team * @license http://phergie.org/license New BSD License * @link http://pear.phergie.org/package/Phergie_Plugin_Message */ class Phergie_Plugin_Message extends Phergie_Plugin_Abstract { /** * Check whether a message is specifically targeted at the bot. * This is the case when the message starts with the bot's name * followed by [,:>] or when it is a private message. * * @return boolean true when the message is specifically targeted at the bot, * false otherwise. */ public function isTargetedMessage() { $event = $this->getEvent(); $self = preg_quote($this->connection->getNick()); $targetPattern = <<,].* # expect the bots name, followed by a [:>,] $}ix REGEX; return !$event->isInChannel() || preg_match($targetPattern, $event->getText()) > 0; } /** * Allow for prefix and bot name aware extraction of a message * * @return string|bool $message The message, which is possibly targeted at the * bot or false if a prefix requirement failed */ public function getMessage() { $event = $this->getEvent(); $prefix = preg_quote($this->getConfig('command.prefix')); $self = preg_quote($this->connection->getNick()); $message = $event->getText(); // $prefixPattern matches : Phergie, do command // where $prefix = 'do' : do command // : Phergie, command $prefixPattern = <<,]\s* # start with bot name (?:{$prefix})? # which is optionally followed by the prefix | \s*{$prefix} # or start with the prefix ) \s*(.*) # always end with the message $}ix REGEX; // $noPrefixPattern matches : Phergie, command // : command $noPrefixPattern = <<,]\s*)? # optionally start with the bot name (.*?) # always end with the message $}ix REGEX; $pattern = $noPrefixPattern; // If a prefix is set, force it as a requirement if ($prefix && $event->isInChannel()) { $pattern = $prefixPattern; } $match = null; if (!preg_match($pattern, $message, $match)) { return false; } return $match[1]; } }