From 364038ce2acc1f02fbd0850c0371c97d1e9044a3 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Mon, 14 Dec 2009 07:33:29 +0000 Subject: [PATCH 1/2] Fix Twitter bridge so it responds reasonably to authorization errors. --- plugins/TwitterBridge/twitter.php | 96 +++++++++++-------- .../TwitterBridge/twitterbasicauthclient.php | 23 ++++- 2 files changed, 77 insertions(+), 42 deletions(-) diff --git a/plugins/TwitterBridge/twitter.php b/plugins/TwitterBridge/twitter.php index fd51506388..202cbdc4f2 100644 --- a/plugins/TwitterBridge/twitter.php +++ b/plugins/TwitterBridge/twitter.php @@ -179,7 +179,7 @@ function broadcast_oauth($notice, $flink) { try { $status = $client->statusesUpdate($statustxt); } catch (OAuthClientException $e) { - return process_error($e, $flink); + return process_error($e, $flink, $notice); } if (empty($status)) { @@ -188,8 +188,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 +200,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,62 +222,69 @@ function broadcast_basicauth($notice, $flink) try { $status = $client->statusesUpdate($statustxt); - } catch (HTTP_Request2_Exception $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; + $user = $flink->getUser(); + $code = $e->getCode(); - 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; - } + $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) diff --git a/plugins/TwitterBridge/twitterbasicauthclient.php b/plugins/TwitterBridge/twitterbasicauthclient.php index 7ee8d7d4c4..fd26293f9e 100644 --- a/plugins/TwitterBridge/twitterbasicauthclient.php +++ b/plugins/TwitterBridge/twitterbasicauthclient.php @@ -31,6 +31,20 @@ if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } +/** + * General Exception wrapper for HTTP basic auth errors + * + * @category Integration + * @package StatusNet + * @author Zach Copley + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + * + */ +class BasicAuthException extends Exception +{ +} + /** * Class for talking to the Twitter API with HTTP Basic Auth. * @@ -169,12 +183,13 @@ class TwitterBasicAuthClient } /** - * Make a HTTP request using cURL. + * Make an HTTP request * * @param string $url Where to make the request * @param array $params post parameters * * @return mixed the request + * @throws BasicAuthException */ function httpRequest($url, $params = null, $auth = true) { @@ -199,6 +214,12 @@ class TwitterBasicAuthClient $response = $request->get($url); } + $code = $response->getStatus(); + + if ($code < 200 || $code >= 400) { + throw new BasicAuthException($response->getBody(), $code); + } + return $response->getBody(); } From 918b662568c2d06a3fb41b4f82bc336f47f06166 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Mon, 14 Dec 2009 18:16:45 +0000 Subject: [PATCH 2/2] Fix issue with favorited/following always being set to false --- lib/api.php | 22 ++++++++++++---------- lib/apiauth.php | 2 -- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/api.php b/lib/api.php index eacb80dbea..ae47434a50 100644 --- a/lib/api.php +++ b/lib/api.php @@ -53,13 +53,14 @@ if (!defined('STATUSNET')) { class ApiAction extends Action { - var $format = null; - var $user = null; - var $page = null; - var $count = null; - var $max_id = null; - var $since_id = null; - var $since = null; + var $format = null; + var $user = null; + var $auth_user = null; + var $page = null; + var $count = null; + var $max_id = null; + var $since_id = null; + var $since = null; /** * Initialization. @@ -188,13 +189,14 @@ class ApiAction extends Action $twitter_user['following'] = false; $twitter_user['notifications'] = false; - if (isset($apidata['user'])) { + if (isset($this->auth_user)) { - $twitter_user['following'] = $apidata['user']->isSubscribed($profile); + $twitter_user['following'] = $this->auth_user->isSubscribed($profile); // Notifications on? $sub = Subscription::pkeyGet(array('subscriber' => - $apidata['user']->id, 'subscribed' => $profile->id)); + $this->auth_user->id, + 'subscribed' => $profile->id)); if ($sub) { $twitter_user['notifications'] = ($sub->jabber || $sub->sms); diff --git a/lib/apiauth.php b/lib/apiauth.php index 0d1613d381..7102764cba 100644 --- a/lib/apiauth.php +++ b/lib/apiauth.php @@ -53,8 +53,6 @@ require_once INSTALLDIR . '/lib/api.php'; class ApiAuthAction extends ApiAction { - var $auth_user = null; - /** * Take arguments for running, and output basic auth header if needed *