]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Message.php
Cosmetic whitespace change
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Message.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
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
13  *
14  * @category  Phergie
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
20  */
21
22 /**
23  * Generalized plugin providing utility methods for
24  * prefix and bot named based message extraction.
25  *
26  * @category Phergie
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
31  */
32 class Phergie_Plugin_Message extends Phergie_Plugin_Abstract
33 {
34
35     /**
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.
39      *
40      * @return boolean true when the message is specifically targeted at the bot,
41      *                 false otherwise.
42      */
43     public function isTargetedMessage()
44     {
45         $event = $this->getEvent();
46
47         $self = preg_quote($this->connection->getNick());
48
49         $targetPattern = <<<REGEX
50         {^
51         \s*{$self}\s*[:>,].* # expect the bots name, followed by a [:>,]
52         $}ix
53 REGEX;
54
55         return !$event->isInChannel() 
56             || preg_match($targetPattern, $event->getText()) > 0;
57     }
58
59     /**
60      * Allow for prefix and bot name aware extraction of a message
61      *
62      * @return string|bool $message The message, which is possibly targeted at the 
63      *                              bot or false if a prefix requirement failed
64      */
65     public function getMessage()
66     {
67         $event = $this->getEvent();
68
69         $prefix = preg_quote($this->getConfig('command.prefix'));
70         $self = preg_quote($this->connection->getNick());
71         $message = $event->getText();
72
73         // $prefixPattern matches : Phergie, do command <parameters>
74         // where $prefix = 'do'   : do command <parameters>
75         //                        : Phergie, command <parameters>
76         $prefixPattern = <<<REGEX
77         {^
78         (?:
79                 \s*{$self}\s*[:>,]\s* # start with bot name
80                         (?:{$prefix})?        # which is optionally followed by the prefix
81         |
82                 \s*{$prefix}          # or start with the prefix
83         )
84         \s*(.*)                   # always end with the message
85         $}ix
86 REGEX;
87
88         // $noPrefixPattern matches : Phergie, command <parameters>
89         //                          : command <parameters>
90         $noPrefixPattern = <<<REGEX
91         {^
92         \s*(?:{$self}\s*[:>,]\s*)? # optionally start with the bot name
93         (.*?)                      # always end with the message
94         $}ix
95 REGEX;
96
97         $pattern = $noPrefixPattern;
98
99         // If a prefix is set, force it as a requirement
100         if ($prefix && $event->isInChannel()) {
101             $pattern = $prefixPattern;
102         }
103
104         $match = null;
105
106         if (!preg_match($pattern, $message, $match)) {
107             return false;
108         }
109
110         return $match[1];
111     }
112 }