]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Twitter.php
Merge branch '0.9.x' into 1.0.x
[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      * Register with the URL plugin, if possible
72      *
73      * @return void
74      */
75     public function onConnect()
76     {
77         if ($url = $this->getPluginHandler()->getPlugin('Url')) {
78             $url->registerRenderer($this);
79         }
80     }
81
82     /**
83      * Initialize (set up configuration vars)
84      *
85      * @return void
86      */
87     public function onLoad()
88     {
89         if (!isset($this->config['twitter.class'])
90             || !$twitterClass = $this->config['twitter.class']
91         ) {
92             $twitterClass = 'Twitter';
93         }
94
95         $this->twitteruser = $this->config['twitter.user'];
96         $this->twitterpassword = $this->config['twitter.password'];
97         $url = $this->config['twitter.url'];
98
99         $this->twitter = new $twitterClass(
100             $this->twitteruser,
101             $this->twitterpassword,
102             $url
103         );
104
105     }
106
107     /**
108      * Fetches the associated tweet and relays it to the channel
109      *
110      * @param string $tweeter if numeric the tweet number/id, otherwise the
111      *  twitter user name (optionally prefixed with @)
112      * @param int    $num     optional tweet number for this user (number of
113      *  tweets ago)
114      *
115      * @return void
116      */
117     public function onCommandTwitter($tweeter = null, $num = 1)
118     {
119         $source = $this->getEvent()->getSource();
120         if (is_numeric($tweeter)) {
121             $tweet = $this->twitter->getTweetByNum($tweeter);
122         } else if (is_null($tweeter) && $this->twitteruser) {
123             $tweet = $this->twitter->getLastTweet($this->twitteruser, 1);
124         } else {
125             $tweet = $this->twitter->getLastTweet(ltrim($tweeter, '@'), $num);
126         }
127         if ($tweet) {
128             $this->doPrivmsg($source, $this->formatTweet($tweet));
129         }
130     }
131
132     /**
133      * Sends a tweet to Twitter as the configured user
134      *
135      * @param string $txt the text to tweet
136      *
137      * @return void
138      */
139     public function onCommandTweet($txt)
140     {
141         $nick = $this->getEvent()->getNick();
142         if (!$this->twitteruser) {
143             return;
144         }
145         $source = $this->getEvent()->getSource();
146         if ($tweet = $this->twitter->sendTweet($txt)) {
147             $this->doPrivmsg(
148                 $source, 'Tweeted: '
149                 . $this->twitter->getUrlOutputStatus($tweet)
150             );
151         } else {
152             $this->doNotice($nick, 'Tweet failed');
153         }
154     }
155
156     /**
157      * Formats a Tweet into a message suitable for output
158      *
159      * @param object $tweet      JSON-decoded tweet object from Twitter
160      * @param bool   $includeUrl whether or not to include the URL in the
161      *  formatted output
162      *
163      * @return string
164      */
165     protected function formatTweet(StdClass $tweet, $includeUrl = true)
166     {
167         $ts = $this->plugins->time->getCountDown($tweet->created_at);
168         $out =  '<@' . $tweet->user->screen_name .'> '. $tweet->text
169             . ' - ' . $ts . ' ago';
170         if ($includeUrl) {
171             $out .= ' (' . $this->twitter->getUrlOutputStatus($tweet) . ')';
172         }
173         return $out;
174     }
175
176     /**
177      * Renders a URL
178      *
179      * @param array $parsed parse_url() output for the URL to render
180      *
181      * @return bool
182      */
183     public function renderUrl(array $parsed)
184     {
185         if ($parsed['host'] != 'twitter.com'
186             && $parsed['host'] != 'www.twitter.com'
187         ) {
188             // unable to render non-twitter URLs
189             return false;
190         }
191
192         $source = $this->getEvent()->getSource();
193
194         if (preg_match('#^/(.*?)/status(es)?/([0-9]+)$#', $parsed['path'], $matches)
195         ) {
196             $tweet = $this->twitter->getTweetByNum($matches[3]);
197             if ($tweet) {
198                 $this->doPrivmsg($source, $this->formatTweet($tweet, false));
199             }
200             return true;
201         }
202
203         // if we get this far, we haven't satisfied the URL, so bail:
204         return false;
205     }
206 }