PageRenderTime 67ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Phergie/Plugin/Twitter.php

https://github.com/dingram/phergie
PHP | 212 lines | 116 code | 13 blank | 83 comment | 13 complexity | 33b6a41158b1d7ff526698b80fbc42f8 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. * These requires are for library code, so they don't fit Autoload's normal
  23. * conventions.
  24. *
  25. * @link http://github.com/scoates/simpletweet
  26. */
  27. require dirname(__FILE__) . '/Twitter/twitter.class.php';
  28. require dirname(__FILE__) . '/Twitter/laconica.class.php';
  29. /**
  30. * Fetches tweets from Twitter.
  31. *
  32. * Usage:
  33. * twitter username
  34. * (fetches and displays the last tweet by @username)
  35. * twitter username 3
  36. * (fetches and displays the third last tweet by @username)
  37. * twitter 1234567
  38. * (fetches and displays tweet number 1234567)
  39. * http://twitter.com/username/statuses/1234567
  40. * (same as `twitter 1234567`)
  41. *
  42. * @category Phergie
  43. * @package Phergie_Plugin_Twitter
  44. * @author Phergie Development Team <team@phergie.org>
  45. * @license http://phergie.org/license New BSD License
  46. * @link http://pear.phergie.org/package/Phergie_Plugin_Twitter
  47. * @uses Phergie_Plugin_Time pear.phergie.org
  48. * @uses Phergie_Plugin_Encoding pear.phergie.org
  49. */
  50. class Phergie_Plugin_Twitter extends Phergie_Plugin_Abstract
  51. {
  52. /**
  53. * Twitter object (from Simpletweet)
  54. */
  55. protected $twitter;
  56. /**
  57. * Register with the URL plugin, if possible
  58. *
  59. * @return void
  60. */
  61. public function onConnect()
  62. {
  63. $plugins = $this->getPluginHandler();
  64. if ($plugins->hasPlugin('Url')) {
  65. $plugins->getPlugin('Url')->registerRenderer($this);
  66. }
  67. }
  68. /**
  69. * Initialize (set up configuration vars)
  70. *
  71. * @return void
  72. */
  73. public function onLoad()
  74. {
  75. $twitterClass = $this->getConfig('twitter.class', 'Twitter');
  76. $this->setTwitter(
  77. new $twitterClass(
  78. $this->config['twitter.user'],
  79. $this->config['twitter.password'],
  80. $this->config['twitter.url']
  81. )
  82. );
  83. $plugins = $this->getPluginHandler();
  84. $plugins->getPlugin('Encoding');
  85. $plugins->getPlugin('Time');
  86. }
  87. /**
  88. * Sets the Twitter client instance to use.
  89. *
  90. * @param Twitter $twitter Twitter instance to set
  91. *
  92. * @return Phergie_Plugin_Twitter Provides a fluent interface
  93. */
  94. public function setTwitter(Twitter $twitter)
  95. {
  96. $this->twitter = $twitter;
  97. return $this;
  98. }
  99. /**
  100. * Returns the Twitter client instance in use.
  101. *
  102. * @return Twitter Twitter client instance
  103. */
  104. public function getTwitter()
  105. {
  106. return $this->twitter;
  107. }
  108. /**
  109. * Fetches the associated tweet and relays it to the channel.
  110. *
  111. * @param string $tweeter if numeric the tweet number/id, otherwise the
  112. * twitter user name (optionally prefixed with @, or a URL to a
  113. * tweet)
  114. * @param int $num optional offset for this user (number of
  115. * tweets ago)
  116. *
  117. * @return void
  118. */
  119. public function onCommandTwitter($tweeter = null, $num = 1)
  120. {
  121. $source = $this->getEvent()->getSource();
  122. $nick = $this->getEvent()->getHostmask()->getNick();
  123. if (is_numeric($tweeter)) {
  124. $tweet = $this->twitter->getTweetByNum($tweeter);
  125. } else if (is_null($tweeter) && $this->twitteruser) {
  126. $tweet = $this->twitter->getLastTweet($this->twitteruser, 1);
  127. } else if (preg_match('/^https?:\/\/(www\.)?twitter\.com/i', $tweeter)) {
  128. if (stripos($tweeter, 'status') !== false) {
  129. $tweeter = preg_replace('/[^\d]+([\d]+$)/i', '\1', $tweeter);
  130. $tweet = $this->twitter->getTweetByNum($tweeter);
  131. } else {
  132. $twit = explode('/', rtrim($tweeter, '/'));
  133. $tweeter = array_pop($twit);
  134. $tweet = $this->twitter->getLastTweet(ltrim($tweeter, '@'), $num);
  135. }
  136. } else {
  137. $tweet = $this->twitter->getLastTweet(ltrim($tweeter, '@'), $num);
  138. }
  139. if ($tweet) {
  140. $this->doPrivmsg($source, $this->formatTweet($tweet));
  141. } else {
  142. $this->doPrivmsg($source, "Sorry, $nick I couldn't get that tweet :-(");
  143. }
  144. }
  145. /**
  146. * Formats a Tweet into a message suitable for output.
  147. *
  148. * @param object $tweet JSON-decoded tweet object from Twitter
  149. * @param bool $includeUrl whether or not to include the URL in the
  150. * formatted output
  151. *
  152. * @return string
  153. */
  154. protected function formatTweet(StdClass $tweet, $includeUrl = true)
  155. {
  156. $ts = $this->plugins->time->getCountDown($tweet->created_at);
  157. $out = '<@' . $tweet->user->screen_name .'> '
  158. . preg_replace('/\s+/', ' ', $tweet->text)
  159. . ' - ' . $ts . ' ago';
  160. if ($includeUrl) {
  161. $out .= ' (' . $this->twitter->getUrlOutputStatus($tweet) . ')';
  162. }
  163. $encode = $this->getPluginHandler()->getPlugin('Encoding');
  164. return $encode->decodeEntities($out);
  165. }
  166. /**
  167. * Renders Twitter URLs.
  168. *
  169. * @param array $parsed parse_url() output for the URL to render
  170. *
  171. * @return bool
  172. */
  173. public function renderUrl(array $parsed)
  174. {
  175. if ($parsed['host'] != 'twitter.com'
  176. && $parsed['host'] != 'www.twitter.com'
  177. ) {
  178. // unable to render non-twitter URLs
  179. return false;
  180. }
  181. $source = $this->getEvent()->getSource();
  182. if (preg_match('#^/(.*?)/status(es)?/([0-9]+)$#', $parsed['path'], $matches)
  183. ) {
  184. $tweet = $this->twitter->getTweetByNum($matches[3]);
  185. if ($tweet) {
  186. $this->doPrivmsg($source, $this->formatTweet($tweet, false));
  187. }
  188. return true;
  189. }
  190. // if we get this far, we haven't satisfied the URL, so bail:
  191. return false;
  192. }
  193. }