]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/TerryChay.php
Merge remote branch 'statusnet/1.0.x' into irc-plugin
[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->http = $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->http->get(self::URL)->getContent();
67     }
68
69     /**
70      * Parses incoming messages for "Terry Chay" and related variations and 
71      * responds with a chayism.
72      *
73      * @return void
74      */
75     public function onPrivmsg()
76     {
77         $event = $this->getEvent();
78         $source = $event->getSource();
79         $message = $event->getText();
80         $pattern 
81             = '{^(' . preg_quote($this->getConfig('command.prefix')) . 
82             '\s*)?.*(terry\s+chay|tychay)}ix';
83
84         if (preg_match($pattern, $message)
85             && $fact = $this->getChayism()
86         ) {
87             $this->doPrivmsg($source, 'Fact: ' . $fact);
88         }
89     }
90
91     /**
92      * Parses incoming CTCP request for "Terry Chay" and related variations 
93      * and responds with a chayism.
94      *
95      * @return void
96      */
97     public function onCtcp()
98     {
99         $event = $this->getEvent();
100         $source = $event->getSource();
101         $ctcp = $event->getArgument(1);
102
103         if (preg_match('({terry[\s_+-]*chay}|tychay)ix', $ctcp)
104             && $fact = $this->getChayism()
105         ) {
106             $this->doCtcpReply($source, 'TERRYCHAY', $fact);
107         }
108     }
109 }