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 * Allow only admins to tweet
73 protected $requireAdmin = true;
76 * Register with the URL plugin, if possible
80 public function onConnect()
82 if ($url = $this->getPluginHandler()->getPlugin('Url')) {
83 $url->registerRenderer($this);
88 * Initialize (set up configuration vars)
92 public function onLoad()
94 // see if tweetrequireadmin defined in config
95 if (isset($this->config['twitter.tweetrequireadmin'])
96 && $req = $this->config['twitter.tweetrequireadmin']
98 // if so, override default
99 $this->requireAdmin = $req;
101 if (!isset($this->config['twitter.class'])
102 || !$twitterClass = $this->config['twitter.class']
104 $twitterClass = 'Twitter';
107 $this->twitteruser = $this->config['twitter.user'];
108 $this->twitterpassword = $this->config['twitter.password'];
109 $url = $this->config['twitter.url'];
111 $this->twitter = new $twitterClass(
113 $this->twitterpassword,
120 * Fetches the associated tweet and relays it to the channel
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
129 public function onCommandTwitter($tweeter = null, $num = 1)
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);
137 $tweet = $this->twitter->getLastTweet(ltrim($tweeter, '@'), $num);
140 $this->doPrivmsg($source, $this->formatTweet($tweet));
145 * Sends a tweet to Twitter as the configured user
147 * @param string $txt the text to tweet
151 public function onCommandTweet($txt)
154 $nick = $this->getEvent()->getNick();
155 if (!$this->twitteruser) {
158 if ($this->requireAdmin && !$this->fromAdmin(true)) {
161 $source = $this->getEvent()->getSource();
162 if ($tweet = $this->twitter->sendTweet($txt)) {
165 . $this->twitter->getUrlOutputStatus($tweet)
168 $this->doNotice($nick, 'Tweet failed');
173 * Formats a Tweet into a message suitable for output
175 * @param object $tweet JSON-decoded tweet object from Twitter
176 * @param bool $includeUrl whether or not to include the URL in the
181 protected function formatTweet(StdClass $tweet, $includeUrl = true)
183 $ts = $this->plugins->time->getCountDown($tweet->created_at);
184 $out = '<@' . $tweet->user->screen_name .'> '. $tweet->text
185 . ' - ' . $ts . ' ago';
187 $out .= ' (' . $this->twitter->getUrlOutputStatus($tweet) . ')';
195 * @param array $parsed parse_url() output for the URL to render
199 public function renderUrl(array $parsed)
201 if ($parsed['host'] != 'twitter.com'
202 && $parsed['host'] != 'www.twitter.com'
204 // unable to render non-twitter URLs
208 $source = $this->getEvent()->getSource();
210 if (preg_match('#^/(.*?)/status(es)?/([0-9]+)$#', $parsed['path'], $matches)
212 $tweet = $this->twitter->getTweetByNum($matches[3]);
214 $this->doPrivmsg($source, $this->formatTweet($tweet, false));
219 // if we get this far, we haven't satisfied the URL, so bail: