]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/twitter.php
Remove more contractions
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitter.php
index 280cdb0a33d93d6fcfcdef2e8669f9cee55e383d..d48089caa238f8ef2dec8ba9c598dde8831faf6f 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
- * Laconica - a distributed open-source microblogging tool
- * Copyright (C) 2008, 2009, Control Yourself, Inc.
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2008, 2009, StatusNet, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-if (!defined('LACONICA')) {
+if (!defined('STATUSNET') && !defined('LACONICA')) {
     exit(1);
 }
 
 define('TWITTER_SERVICE', 1); // Twitter is foreign_service ID 1
 
-function update_twitter_user($twitter_id, $screen_name)
+require_once INSTALLDIR . '/plugins/TwitterBridge/twitterbasicauthclient.php';
+require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php';
+
+function updateTwitter_user($twitter_id, $screen_name)
 {
     $uri = 'http://twitter.com/' . $screen_name;
     $fuser = new Foreign_user();
 
     $fuser->query('BEGIN');
 
-    // Dropping down to SQL because regular DB_DataObject udpate stuff doesn't seem
+    // Dropping down to SQL because regular DB_DataObject udpate stuff does not seem
     // to work so good with tables that have multiple column primary keys
 
     // Any time we update the uri for a forein user we have to make sure there
@@ -115,7 +118,7 @@ function save_twitter_user($twitter_id, $screen_name)
         // Only update if Twitter screen name has changed
 
         if ($fuser->nickname != $screen_name) {
-            $result = update_twitter_user($twitter_id, $screen_name);
+            $result = updateTwitter_user($twitter_id, $screen_name);
 
             common_debug('Twitter bridge - Updated nickname (and URI) for Twitter user ' .
                 "$fuser->id to $screen_name, was $fuser->nickname");
@@ -154,72 +157,126 @@ function broadcast_twitter($notice)
                                        TWITTER_SERVICE);
 
     if (is_twitter_bound($notice, $flink)) {
+        if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
+            return broadcast_oauth($notice, $flink);
+        } else {
+            return broadcast_basicauth($notice, $flink);
+        }
+    }
 
-        $user = $flink->getUser();
-
-        // XXX: Hack to get around PHP cURL's use of @ being a a meta character
-        $statustxt = preg_replace('/^@/', ' @', $notice->content);
+    return true;
+}
 
-        $token = TwitterOAuthClient::unpackToken($flink->credentials);
+function broadcast_oauth($notice, $flink) {
+    $user = $flink->getUser();
+    $statustxt = format_status($notice);
+    // Convert !groups to #hashes
+    $statustxt = preg_replace('/(^|\s)!([A-Za-z0-9]{1,64})/', "\\1#\\2", $statustxt);
+    $token = TwitterOAuthClient::unpackToken($flink->credentials);
+    $client = new TwitterOAuthClient($token->key, $token->secret);
+    $status = null;
+
+    try {
+        $status = $client->statusesUpdate($statustxt);
+    } catch (OAuthClientCurlException $e) {
+        return process_error($e, $flink);
+    }
 
-        $client = new TwitterOAuthClient($token->key, $token->secret);
+    if (empty($status)) {
 
-        $status = null;
+        // This could represent a failure posting,
+        // or the Twitter API might just be behaving flakey.
 
-        try {
-            $status = $client->statusesUpdate($statustxt);
-        } catch (OAuthClientCurlException $e) {
+        $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' .
+                          'trying to send update for %1$s (user id %2$s).',
+                          $user->nickname, $user->id);
+        common_log(LOG_WARNING, $errmsg);
 
-            if ($e->getMessage() == 'The requested URL returned error: 401') {
+        return false;
+    }
 
-                $errmsg = sprintf('User %1$s (user id: %2$s) has an invalid ' .
-                                  'Twitter OAuth access token.',
-                                  $user->nickname, $user->id);
-                common_log(LOG_WARNING, $errmsg);
+    // Notice crossed the great divide
 
-                // Bad auth token! We need to delete the foreign_link
-                // to Twitter and inform the user.
+    $msg = sprintf('Twitter bridge - posted notice %s to Twitter using OAuth.',
+                   $notice->id);
+    common_log(LOG_INFO, $msg);
 
-                remove_twitter_link($flink);
-                return true;
+    return true;
+}
 
-            } else {
+function broadcast_basicauth($notice, $flink)
+{
+    $user = $flink->getUser();
 
-                // Some other error happened, so we should probably
-                // try to send again later.
+    $statustxt = format_status($notice);
 
-                $errmsg = sprintf('cURL error trying to send notice to Twitter ' .
-                                  'for user %1$s (user id: %2$s) - ' .
-                                  'code: %3$s message: $4$s.',
-                                  $user->nickname, $user->id,
-                                  $e->getCode(), $e->getMessage());
-                common_log(LOG_WARNING, $errmsg);
+    $client = new TwitterBasicAuthClient($flink);
+    $status = null;
 
-                return false;
-            }
-        }
+    try {
+        $status = $client->statusesUpdate($statustxt);
+    } catch (HTTP_Request2_Exception $e) {
+        return process_error($e, $flink);
+    }
 
-        if (empty($status)) {
+    if (empty($status)) {
 
-            // This could represent a failure posting,
-            // or the Twitter API might just be behaving flakey.
+        $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' .
+                          'trying to send update for %1$s (user id %2$s).',
+                          $user->nickname, $user->id);
+        common_log(LOG_WARNING, $errmsg);
 
-            $errmsg = sprint('No data returned by Twitter API when ' .
+            $errmsg = sprintf('No data returned by Twitter API when ' .
                              'trying to send update for %1$s (user id %2$s).',
                              $user->nickname, $user->id);
             common_log(LOG_WARNING, $errmsg);
+        return false;
+    }
 
-            return false;
-        }
+    $msg = sprintf('Twitter bridge - posted notice %s to Twitter using basic auth.',
+                   $notice->id);
+    common_log(LOG_INFO, $msg);
 
-        // Notice crossed the great divide
+    return true;
+}
 
-        $msg = sprintf('Twitter bridge posted notice %s to Twitter.',
-                       $notice->id);
-        common_log(LOG_INFO, $msg);
+function process_error($e, $flink)
+{
+    $user        = $flink->getUser();
+    $errmsg      = $e->getMessage();
+    $delivered   = false;
+
+    switch($errmsg) {
+     case 'The requested URL returned error: 401':
+        $logmsg = sprintf('Twiter bridge - User %1$s (user id: %2$s) has an invalid ' .
+                          'Twitter screen_name/password combo or an invalid acesss token.',
+                          $user->nickname, $user->id);
+        $delivered = true;
+        remove_twitter_link($flink);
+        break;
+     case 'The requested URL returned error: 403':
+        $logmsg = sprintf('Twitter bridge - User %1$s (user id: %2$s) has exceeded ' .
+                          'his/her Twitter request limit.',
+                          $user->nickname, $user->id);
+        break;
+     default:
+        $logmsg = sprintf('Twitter bridge - cURL error trying to send notice to Twitter ' .
+                          'for user %1$s (user id: %2$s) - ' .
+                          'code: %3$s message: %4$s.',
+                          $user->nickname, $user->id,
+                          $e->getCode(), $e->getMessage());
+        break;
     }
 
-    return true;
+    common_log(LOG_WARNING, $logmsg);
+
+    return $delivered;
+}
+
+function format_status($notice)
+{
+    // XXX: Hack to get around PHP cURL's use of @ being a a meta character
+    return preg_replace('/^@/', ' @', $notice->content);
 }
 
 function remove_twitter_link($flink)
@@ -227,7 +284,7 @@ function remove_twitter_link($flink)
     $user = $flink->getUser();
 
     common_log(LOG_INFO, 'Removing Twitter bridge Foreign link for ' .
-        "user $user->nickname (user id: $user->id).");
+               "user $user->nickname (user id: $user->id).");
 
     $result = $flink->delete();
 
@@ -256,3 +313,39 @@ function remove_twitter_link($flink)
 
 }
 
+/**
+ * Send a mail message to notify a user that her Twitter bridge link
+ * has stopped working, and therefore has been removed.  This can
+ * happen when the user changes her Twitter password, or otherwise
+ * revokes access.
+ *
+ * @param User $user   user whose Twitter bridge link has been removed
+ *
+ * @return boolean success flag
+ */
+
+function mail_twitter_bridge_removed($user)
+{
+    common_init_locale($user->language);
+
+    $profile = $user->getProfile();
+
+    $subject = sprintf(_('Your Twitter bridge has been disabled.'));
+
+    $site_name = common_config('site', 'name');
+
+    $body = sprintf(_('Hi, %1$s. We\'re sorry to inform you that your ' .
+        'link to Twitter has been disabled. We no longer seem to have ' .
+    'permission to update your Twitter status. (Did you revoke ' .
+    '%3$s\'s access?)' . "\n\n" .
+    'You can re-enable your Twitter bridge by visiting your ' .
+    "Twitter settings page:\n\n\t%2\$s\n\n" .
+        "Regards,\n%3\$s\n"),
+        $profile->getBestName(),
+        common_local_url('twittersettings'),
+        common_config('site', 'name'));
+
+    common_init_locale();
+    return mail_to_user($user, $subject, $body);
+}
+