]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/TerryChay.php
Merge remote branch 'chat-interface-plugins/irc-plugin' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / TerryChay.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_TerryChay
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_TerryChay
20  */
21
22 /**
23  * Parses incoming messages for the words "Terry Chay" or tychay and responds
24  * with a random Terry fact retrieved from the Chayism web service.
25  *
26  * @category Phergie
27  * @package  Phergie_Plugin_TerryChay
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_TerryChay
31  * @uses     Phergie_Plugin_Http pear.phergie.org
32  */
33 class Phergie_Plugin_TerryChay extends Phergie_Plugin_Abstract
34 {
35     /**
36      * URL to the web service
37      *
38      * @const string
39      */
40     const URL = 'http://phpdoc.info/chayism/';
41
42     /**
43      * HTTP plugin
44      *
45      * @var Phergie_Plugin_Http
46      */
47     protected $http;
48
49     /**
50      * Checks for dependencies.
51      *
52      * @return void
53      */
54     public function onLoad()
55     {
56         $this->getPluginHandler()->getPlugin('Http');
57     }
58
59     /**
60      * Fetches a chayism.
61      *
62      * @return string|bool Fetched chayism or FALSE if the operation failed
63      */
64     public function getChayism()
65     {
66         return $this
67             ->getPluginHandler()
68             ->getPlugin('Http')
69             ->get(self::URL)
70             ->getContent();
71     }
72
73     /**
74      * Parses incoming messages for "Terry Chay" and related variations and
75      * responds with a chayism.
76      *
77      * @return void
78      */
79     public function onPrivmsg()
80     {
81         $event = $this->getEvent();
82         $source = $event->getSource();
83         $message = $event->getText();
84         $pattern
85             = '{^(' . preg_quote($this->getConfig('command.prefix')) .
86             '\s*)?.*(terry\s+chay|tychay)}ix';
87
88         if (preg_match($pattern, $message)) {
89             if($fact = $this->getChayism()) {
90                 $this->doPrivmsg($source, 'Fact: ' . $fact);
91             }
92         }
93     }
94 }