]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Twitter.php
91f177d2aead205cfb660d41b9e7a3ff5c6bc5da
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Twitter.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_Twitter
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_Twitter
20  */
21
22 /**
23  * These requires are for library code, so they don't fit Autoload's normal 
24  * conventions.
25  *
26  * @link http://github.com/scoates/simpletweet
27  */
28 require dirname(__FILE__) . '/Twitter/twitter.class.php';
29 require dirname(__FILE__) . '/Twitter/laconica.class.php';
30
31 /**
32  * Twitter plugin; Allows tweet (if configured) and twitter commands
33  *
34  * Usage:
35  *   tweet text to tweet
36  *    (sends a message to twitter and Phergie will give you the link)
37  *   twitter username
38  *    (fetches and displays the last tweet by @username)
39  *   twitter username 3
40  *    (fetches and displays the third last tweet by @username)
41  *   twitter 1234567
42  *    (fetches and displays tweet number 1234567)
43  *   http://twitter.com/username/statuses/1234567
44  *    (same as `twitter 1234567`)
45  *
46  * @category Phergie
47  * @package  Phergie_Plugin_Twitter
48  * @author   Phergie Development Team <team@phergie.org>
49  * @license  http://phergie.org/license New BSD License
50  * @link     http://pear.phergie.org/package/Phergie_Plugin_Twitter
51  * @uses     Phergie_Plugin_Time pear.phergie.org
52  */
53 class Phergie_Plugin_Twitter extends Phergie_Plugin_Abstract
54 {
55     /**
56      * Twitter object (from Simpletweet)
57      */
58     protected $twitter;
59
60     /**
61      * Twitter user
62      */
63     protected $twitteruser = null;
64
65     /**
66      * Password
67      */
68     protected $twitterpassword = null;
69
70     /**
71      * Allow only admins to tweet
72      */
73     protected $requireAdmin = true;
74
75     /**
76      * Register with the URL plugin, if possible
77      *
78      * @return void
79      */
80     public function onConnect()
81     {
82         if ($url = $this->getPluginHandler()->getPlugin('Url')) {
83             $url->registerRenderer($this);
84         }
85     }
86
87     /**
88      * Initialize (set up configuration vars)
89      *
90      * @return void
91      */
92     public function onLoad()
93     {
94         // see if tweetrequireadmin defined in config
95         if (isset($this->config['twitter.tweetrequireadmin'])
96             && $req = $this->config['twitter.tweetrequireadmin']
97         ) {
98             // if so, override default
99             $this->requireAdmin = $req;
100         }
101         if (!isset($this->config['twitter.class'])
102             || !$twitterClass = $this->config['twitter.class']
103         ) {
104             $twitterClass = 'Twitter';
105         }
106
107         $this->twitteruser = $this->config['twitter.user'];
108         $this->twitterpassword = $this->config['twitter.password'];
109         $url = $this->config['twitter.url'];
110
111         $this->twitter = new $twitterClass(
112             $this->twitteruser,
113             $this->twitterpassword,
114             $url
115         );
116
117     }
118
119     /**
120      * Fetches the associated tweet and relays it to the channel
121      *
122      * @param string $tweeter if numeric the tweet number/id, otherwise the
123      *  twitter user name (optionally prefixed with @)
124      * @param int    $num     optional tweet number for this user (number of
125      *  tweets ago)
126      *
127      * @return void
128      */
129     public function onCommandTwitter($tweeter = null, $num = 1)
130     {
131         $source = $this->getEvent()->getSource();
132         if (is_numeric($tweeter)) {
133             $tweet = $this->twitter->getTweetByNum($tweeter);
134         } else if (is_null($tweeter) && $this->twitteruser) {
135             $tweet = $this->twitter->getLastTweet($this->twitteruser, 1);
136         } else {
137             $tweet = $this->twitter->getLastTweet(ltrim($tweeter, '@'), $num);
138         }
139         if ($tweet) {
140             $this->doPrivmsg($source, $this->formatTweet($tweet));
141         }
142     }
143
144     /**
145      * Sends a tweet to Twitter as the configured user
146      *
147      * @param string $txt the text to tweet
148      *
149      * @return void
150      */
151     public function onCommandTweet($txt)
152     {
153         echo "Tweet!\n";
154         $nick = $this->getEvent()->getNick();
155         if (!$this->twitteruser) {
156             return;
157         }
158         if ($this->requireAdmin && !$this->fromAdmin(true)) {
159             return;
160         }
161         $source = $this->getEvent()->getSource();
162         if ($tweet = $this->twitter->sendTweet($txt)) {
163             $this->doPrivmsg(
164                 $source, 'Tweeted: '
165                 . $this->twitter->getUrlOutputStatus($tweet)
166             );
167         } else {
168             $this->doNotice($nick, 'Tweet failed');
169         }
170     }
171
172     /**
173      * Formats a Tweet into a message suitable for output
174      *
175      * @param object $tweet      JSON-decoded tweet object from Twitter
176      * @param bool   $includeUrl whether or not to include the URL in the 
177      *  formatted output
178      *
179      * @return string
180      */
181     protected function formatTweet(StdClass $tweet, $includeUrl = true)
182     {
183         $ts = $this->plugins->time->getCountDown($tweet->created_at);
184         $out =  '<@' . $tweet->user->screen_name .'> '. $tweet->text
185             . ' - ' . $ts . ' ago';
186         if ($includeUrl) {
187             $out .= ' (' . $this->twitter->getUrlOutputStatus($tweet) . ')';
188         }
189         return $out;
190     }
191
192     /**
193      * Renders a URL
194      *
195      * @param array $parsed parse_url() output for the URL to render
196      *
197      * @return bool
198      */
199     public function renderUrl(array $parsed)
200     {
201         if ($parsed['host'] != 'twitter.com'
202             && $parsed['host'] != 'www.twitter.com'
203         ) {
204             // unable to render non-twitter URLs
205             return false;
206         }
207
208         $source = $this->getEvent()->getSource();
209
210         if (preg_match('#^/(.*?)/status(es)?/([0-9]+)$#', $parsed['path'], $matches)
211         ) {
212             $tweet = $this->twitter->getTweetByNum($matches[3]);
213             if ($tweet) {
214                 $this->doPrivmsg($source, $this->formatTweet($tweet, false));
215             }
216             return true;
217         }
218
219         // if we get this far, we haven't satisfied the URL, so bail:
220         return false;
221
222     }
223 }