? bot_potpourri_twitter.patch
Index: bot_potpourri/bot_potpourri.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/bot/bot_potpourri/Attic/bot_potpourri.module,v
retrieving revision 1.1.2.3
diff -u -p -r1.1.2.3 bot_potpourri.module
--- bot_potpourri/bot_potpourri.module	12 May 2010 13:45:50 -0000	1.1.2.3
+++ bot_potpourri/bot_potpourri.module	15 Nov 2010 00:47:00 -0000
@@ -12,9 +12,11 @@
 function bot_potpourri_help($path, $arg) {
   switch ($path) {
     case 'irc:features':
-      return array(t('Timezones'));
+      return array(t('Timezones'), t('Tweet fetching'));
     case 'irc:features#timezones':
       return t('Display timezones with "BOTNAME: timezone BST". Convert timezones with "tz 10AM MST to EST" or "tz 14:27 UTC in Europe/London". Timestamps are allowed if combined and with no spaces: "tz 2010-10-23T10:00 EST to UTC". All returned dates are DST-aware.');
+    case 'irc:features#tweet_fetching':
+      return t('When the bot notices that a URL to a Twitter "tweet" appears in chat, it will use the Twitter API to fetch that tweet and echo it inot the channel.');
   }
 }
 
@@ -36,6 +38,26 @@ function bot_potpourri_irc_msg_channel($
     $message = bot_potpourri_timezone($matches[4]);
     bot_message($to, strtr($message, $substitutions));
   }
+  // Fetch and display Twitter posts.
+  elseif (preg_match_all('~\bhttps?://twitter\.com/[\w#!/]*/(\d+)/?\b~', $data->message, $matches, PREG_SET_ORDER)) {
+    // Things we should have matched on:
+    // http://twitter.com/foo/statuses/123
+    // https://twitter.com/#!/bar/statuses/321/
+    foreach ($matches as $match) {
+      $response = drupal_http_request("http://api.twitter.com/1/statuses/show/{$match[1]}.json", array('User-Agent' => 'Drupal IRC Bot +(http://drupal.com/project/bot)'));
+      if ($response->code == 200) {
+        $tweet = json_decode($response->data);
+        if ($tweet !== NULL && empty($tweet->error)) {
+          // We're reconstructing the tweet URL instead of just echoing the one we
+          // matched in order to be more correct, since strictly the username part
+          // of the URL can be totally wrong. For example, if someone says
+          // http://twitter.com/foo/status/444 and status 444 doesn't really belong
+          // to foo, we can handle it just fine, but Twitter can't.
+          bot_message($to, t('!url is @!scname (!name): "!status"', array('!url' => "http://twitter.com/{$tweet->user->screen_name}/status/{$tweet->id}", '!scname' => $tweet->user->screen_name, '!name' => $tweet->user->name, '!status' => $tweet->text)));
+        }
+      }
+    }
+  }
 }
 
 /**
