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
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
23 * These requires are for library code, so they don't fit Autoload's normal
26 * @link http://github.com/scoates/simpletweet
28 require dirname(__FILE__) . '/Twitter/twitter.class.php';
29 require dirname(__FILE__) . '/Twitter/laconica.class.php';
32 * Twitter plugin; Allows tweet (if configured) and twitter commands
36 * (sends a message to twitter and Phergie will give you the link)
38 * (fetches and displays the last tweet by @username)
40 * (fetches and displays the third last tweet by @username)
42 * (fetches and displays tweet number 1234567)
43 * http://twitter.com/username/statuses/1234567
44 * (same as `twitter 1234567`)
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
53 class Phergie_Plugin_Twitter extends Phergie_Plugin_Abstract
56 * Twitter object (from Simpletweet)
63 protected $twitteruser = null;
68 protected $twitterpassword = null;
71 * Register with the URL plugin, if possible
75 public function onConnect()
77 if ($url = $this->getPluginHandler()->getPlugin('Url')) {
78 $url->registerRenderer($this);
83 * Initialize (set up configuration vars)
87 public function onLoad()
89 if (!isset($this->config['twitter.class'])
90 || !$twitterClass = $this->config['twitter.class']
92 $twitterClass = 'Twitter';
95 $this->twitteruser = $this->config['twitter.user'];
96 $this->twitterpassword = $this->config['twitter.password'];
97 $url = $this->config['twitter.url'];
99 $this->twitter = new $twitterClass(
101 $this->twitterpassword,
108 * Fetches the associated tweet and relays it to the channel
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
117 public function onCommandTwitter($tweeter = null, $num = 1)
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);
125 $tweet = $this->twitter->getLastTweet(ltrim($tweeter, '@'), $num);
128 $this->doPrivmsg($source, $this->formatTweet($tweet));
133 * Sends a tweet to Twitter as the configured user
135 * @param string $txt the text to tweet
139 public function onCommandTweet($txt)
141 $nick = $this->getEvent()->getNick();
142 if (!$this->twitteruser) {
145 $source = $this->getEvent()->getSource();
146 if ($tweet = $this->twitter->sendTweet($txt)) {
149 . $this->twitter->getUrlOutputStatus($tweet)
152 $this->doNotice($nick, 'Tweet failed');
157 * Formats a Tweet into a message suitable for output
159 * @param object $tweet JSON-decoded tweet object from Twitter
160 * @param bool $includeUrl whether or not to include the URL in the
165 protected function formatTweet(StdClass $tweet, $includeUrl = true)
167 $ts = $this->plugins->time->getCountDown($tweet->created_at);
168 $out = '<@' . $tweet->user->screen_name .'> '. $tweet->text
169 . ' - ' . $ts . ' ago';
171 $out .= ' (' . $this->twitter->getUrlOutputStatus($tweet) . ')';
179 * @param array $parsed parse_url() output for the URL to render
183 public function renderUrl(array $parsed)
185 if ($parsed['host'] != 'twitter.com'
186 && $parsed['host'] != 'www.twitter.com'
188 // unable to render non-twitter URLs
192 $source = $this->getEvent()->getSource();
194 if (preg_match('#^/(.*?)/status(es)?/([0-9]+)$#', $parsed['path'], $matches)
196 $tweet = $this->twitter->getTweetByNum($matches[3]);
198 $this->doPrivmsg($source, $this->formatTweet($tweet, false));
203 // if we get this far, we haven't satisfied the URL, so bail: