]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/twitter.php
Merge branch 'testing'
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitter.php
index afc3f55bab932614744ef43732485348f53c7eba..e133ce6f745c1a8c5e9e30ef67d367e91b6f1bbc 100644 (file)
@@ -23,6 +23,9 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
 
 define('TWITTER_SERVICE', 1); // Twitter is foreign_service ID 1
 
+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;
@@ -167,16 +170,14 @@ function broadcast_twitter($notice)
 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);
+    } catch (OAuthClientException $e) {
+        return process_error($e, $flink, $notice);
     }
 
     if (empty($status)) {
@@ -185,8 +186,11 @@ function broadcast_oauth($notice, $flink) {
         // 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);
+                          'trying to post notice %d for User %s (user id %d).',
+                          $notice->id,
+                          $user->nickname,
+                          $user->id);
+
         common_log(LOG_WARNING, $errmsg);
 
         return false;
@@ -194,8 +198,12 @@ function broadcast_oauth($notice, $flink) {
 
     // Notice crossed the great divide
 
-    $msg = sprintf('Twitter bridge - posted notice %s to Twitter using OAuth.',
-                   $notice->id);
+    $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' .
+                   'OAuth for User %s (user id %d).',
+                   $notice->id,
+                   $user->nickname,
+                   $user->id);
+
     common_log(LOG_INFO, $msg);
 
     return true;
@@ -212,68 +220,80 @@ function broadcast_basicauth($notice, $flink)
 
     try {
         $status = $client->statusesUpdate($statustxt);
-    } catch (BasicAuthCurlException $e) {
-        return process_error($e, $flink);
+    } catch (BasicAuthException $e) {
+        return process_error($e, $flink, $notice);
     }
 
     if (empty($status)) {
 
         $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);
+                          'trying to post notice %d for %s (user id %d).',
+                          $notice->id,
+                          $user->nickname,
+                          $user->id);
+
         common_log(LOG_WARNING, $errmsg);
 
-            $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);
+        $errmsg = sprintf('No data returned by Twitter API when ' .
+                          'trying to post notice %d for %s (user id %d).',
+                          $notice->id,
+                          $user->nickname,
+                          $user->id);
+        common_log(LOG_WARNING, $errmsg);
         return false;
     }
 
-    $msg = sprintf('Twitter bridge - posted notice %s to Twitter using basic auth.',
-                   $notice->id);
+    $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' .
+                   'HTTP basic auth for User %s (user id %d).',
+                   $notice->id,
+                   $user->nickname,
+                   $user->id);
+
     common_log(LOG_INFO, $msg);
 
     return true;
 }
 
-function process_error($e, $flink)
+function process_error($e, $flink, $notice)
 {
-    $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;
-    }
+    $user = $flink->getUser();
+    $code = $e->getCode();
+
+    $logmsg = sprintf('Twitter bridge - %d posting notice %d for ' .
+                      'User %s (user id: %d): %s.',
+                      $code,
+                      $notice->id,
+                      $user->nickname,
+                      $user->id,
+                      $e->getMessage());
 
     common_log(LOG_WARNING, $logmsg);
 
-    return $delivered;
+    if ($code == 401) {
+
+        // Probably a revoked or otherwise bad access token - nuke!
+
+        remove_twitter_link($flink);
+        return true;
+
+    } else {
+
+        // For every other case, it's probably some flakiness so try
+        // sending the notice again later (requeue).
+
+        return false;
+    }
 }
 
 function format_status($notice)
 {
     // XXX: Hack to get around PHP cURL's use of @ being a a meta character
-    return preg_replace('/^@/', ' @', $notice->content);
+    $statustxt = preg_replace('/^@/', ' @', $notice->content);
+
+    // Convert !groups to #hashes
+    $statustxt = preg_replace('/(^|\s)!([A-Za-z0-9]{1,64})/', "\\1#\\2", $statustxt);
+
+    return $statustxt;
 }
 
 function remove_twitter_link($flink)
@@ -309,3 +329,40 @@ 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(_m('Your Twitter bridge has been disabled.'));
+
+    $site_name = common_config('site', 'name');
+
+    $body = sprintf(_m('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);
+}
+