9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
15 * @package Phergie_Plugin_Message
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie_Plugin_Message
23 * Generalized plugin providing utility methods for
24 * prefix and bot named based message extraction.
27 * @package Phergie_Plugin_Message
28 * @author Phergie Development Team <team@phergie.org>
29 * @license http://phergie.org/license New BSD License
30 * @link http://pear.phergie.org/package/Phergie_Plugin_Message
32 class Phergie_Plugin_Message extends Phergie_Plugin_Abstract
36 * Check whether a message is specifically targeted at the bot.
37 * This is the case when the message starts with the bot's name
38 * followed by [,:>] or when it is a private message.
40 * @return boolean true when the message is specifically targeted at the bot,
43 public function isTargetedMessage()
45 $event = $this->getEvent();
47 $self = preg_quote($this->connection->getNick());
49 $targetPattern = <<<REGEX
51 \s*{$self}\s*[:>,].* # expect the bots name, followed by a [:>,]
55 return !$event->isInChannel()
56 || preg_match($targetPattern, $event->getText()) > 0;
60 * Allow for prefix and bot name aware extraction of a message
62 * @return string|bool $message The message, which is possibly targeted at the
63 * bot or false if a prefix requirement failed
65 public function getMessage()
67 $event = $this->getEvent();
69 $prefix = preg_quote($this->getConfig('command.prefix'));
70 $self = preg_quote($this->connection->getNick());
71 $message = $event->getText();
73 // $prefixPattern matches : Phergie, do command <parameters>
74 // where $prefix = 'do' : do command <parameters>
75 // : Phergie, command <parameters>
76 $prefixPattern = <<<REGEX
79 \s*{$self}\s*[:>,]\s* # start with bot name
80 (?:{$prefix})? # which is optionally followed by the prefix
82 \s*{$prefix} # or start with the prefix
84 \s*(.*) # always end with the message
88 // $noPrefixPattern matches : Phergie, command <parameters>
89 // : command <parameters>
90 $noPrefixPattern = <<<REGEX
92 \s*(?:{$self}\s*[:>,]\s*)? # optionally start with the bot name
93 (.*?) # always end with the message
97 $pattern = $noPrefixPattern;
99 // If a prefix is set, force it as a requirement
100 if ($prefix && $event->isInChannel()) {
101 $pattern = $prefixPattern;
106 if (!preg_match($pattern, $message, $match)) {