]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/twitter.php
Confirm dialog for reset OAuth consumer key and secret button
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitter.php
index 1a5248a9b92fc6d6a0e35d033d1655aa65cf13dc..33dfb788bf5d3d27d11b447bbc8644cdf51660a7 100644 (file)
@@ -170,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)) {
@@ -188,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;
@@ -197,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;
@@ -215,68 +220,84 @@ 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;
+    $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);
+
+    switch($code) {
+     case 401:
+        // Probably a revoked or otherwise bad access token - nuke!
         remove_twitter_link($flink);
+        return true;
         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);
+     case 403:
+        // User has exceeder her rate limit -- toss the notice
+        return true;
         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;
-    }
 
-    common_log(LOG_WARNING, $logmsg);
+        // For every other case, it's probably some flakiness so try
+        // sending the notice again later (requeue).
 
-    return $delivered;
+        return false;
+        break;
+    }
 }
 
 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)
@@ -330,11 +351,11 @@ function mail_twitter_bridge_removed($user)
 
     $profile = $user->getProfile();
 
-    $subject = sprintf(_('Your Twitter bridge has been disabled.'));
+    $subject = sprintf(_m('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 ' .
+    $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" .