3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) {
24 define('TWITTER_SERVICE', 1); // Twitter is foreign_service ID 1
26 require_once INSTALLDIR . '/plugins/TwitterBridge/twitterbasicauthclient.php';
27 require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php';
29 function updateTwitter_user($twitter_id, $screen_name)
31 $uri = 'http://twitter.com/' . $screen_name;
32 $fuser = new Foreign_user();
34 $fuser->query('BEGIN');
36 // Dropping down to SQL because regular DB_DataObject udpate stuff doesn't seem
37 // to work so good with tables that have multiple column primary keys
39 // Any time we update the uri for a forein user we have to make sure there
40 // are no dupe entries first -- unique constraint on the uri column
42 $qry = 'UPDATE foreign_user set uri = \'\' WHERE uri = ';
43 $qry .= '\'' . $uri . '\'' . ' AND service = ' . TWITTER_SERVICE;
49 $qry = 'UPDATE foreign_user SET nickname = ';
50 $qry .= '\'' . $screen_name . '\'' . ', uri = \'' . $uri . '\' ';
51 $qry .= 'WHERE id = ' . $twitter_id . ' AND service = ' . TWITTER_SERVICE;
53 $fuser->query('COMMIT');
61 function add_twitter_user($twitter_id, $screen_name)
64 $new_uri = 'http://twitter.com/' . $screen_name;
66 // Clear out any bad old foreign_users with the new user's legit URL
67 // This can happen when users move around or fakester accounts get
68 // repoed, and things like that.
70 $luser = new Foreign_user();
71 $luser->uri = $new_uri;
72 $luser->service = TWITTER_SERVICE;
73 $result = $luser->delete();
76 common_log(LOG_WARNING,
77 "Twitter bridge - removed invalid Twitter user squatting on uri: $new_uri");
83 // Otherwise, create a new Twitter user
85 $fuser = new Foreign_user();
87 $fuser->nickname = $screen_name;
88 $fuser->uri = 'http://twitter.com/' . $screen_name;
89 $fuser->id = $twitter_id;
90 $fuser->service = TWITTER_SERVICE;
91 $fuser->created = common_sql_now();
92 $result = $fuser->insert();
95 common_log(LOG_WARNING,
96 "Twitter bridge - failed to add new Twitter user: $twitter_id - $screen_name.");
97 common_log_db_error($fuser, 'INSERT', __FILE__);
99 common_debug("Twitter bridge - Added new Twitter user: $screen_name ($twitter_id).");
105 // Creates or Updates a Twitter user
106 function save_twitter_user($twitter_id, $screen_name)
109 // Check to see whether the Twitter user is already in the system,
110 // and update its screen name and uri if so.
112 $fuser = Foreign_user::getForeignUser($twitter_id, TWITTER_SERVICE);
114 if (!empty($fuser)) {
118 // Only update if Twitter screen name has changed
120 if ($fuser->nickname != $screen_name) {
121 $result = updateTwitter_user($twitter_id, $screen_name);
123 common_debug('Twitter bridge - Updated nickname (and URI) for Twitter user ' .
124 "$fuser->id to $screen_name, was $fuser->nickname");
130 return add_twitter_user($twitter_id, $screen_name);
139 function is_twitter_bound($notice, $flink) {
141 // Check to see if notice should go to Twitter
142 if (!empty($flink) && ($flink->noticesync & FOREIGN_NOTICE_SEND)) {
144 // If it's not a Twitter-style reply, or if the user WANTS to send replies.
145 if (!preg_match('/^@[a-zA-Z0-9_]{1,15}\b/u', $notice->content) ||
146 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY)) {
154 function broadcast_twitter($notice)
156 $flink = Foreign_link::getByUserID($notice->profile_id,
159 if (is_twitter_bound($notice, $flink)) {
160 if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
161 return broadcast_oauth($notice, $flink);
163 return broadcast_basicauth($notice, $flink);
170 function broadcast_oauth($notice, $flink) {
171 $user = $flink->getUser();
172 $statustxt = format_status($notice);
173 $token = TwitterOAuthClient::unpackToken($flink->credentials);
174 $client = new TwitterOAuthClient($token->key, $token->secret);
178 $status = $client->statusesUpdate($statustxt);
179 } catch (OAuthClientException $e) {
180 return process_error($e, $flink, $notice);
183 if (empty($status)) {
185 // This could represent a failure posting,
186 // or the Twitter API might just be behaving flakey.
188 $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' .
189 'trying to post notice %d for User %s (user id %d).',
194 common_log(LOG_WARNING, $errmsg);
199 // Notice crossed the great divide
201 $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' .
202 'OAuth for User %s (user id %d).',
207 common_log(LOG_INFO, $msg);
212 function broadcast_basicauth($notice, $flink)
214 $user = $flink->getUser();
216 $statustxt = format_status($notice);
218 $client = new TwitterBasicAuthClient($flink);
222 $status = $client->statusesUpdate($statustxt);
223 } catch (BasicAuthException $e) {
224 return process_error($e, $flink, $notice);
227 if (empty($status)) {
229 $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' .
230 'trying to post notice %d for %s (user id %d).',
235 common_log(LOG_WARNING, $errmsg);
237 $errmsg = sprintf('No data returned by Twitter API when ' .
238 'trying to post notice %d for %s (user id %d).',
242 common_log(LOG_WARNING, $errmsg);
246 $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' .
247 'HTTP basic auth for User %s (user id %d).',
252 common_log(LOG_INFO, $msg);
257 function process_error($e, $flink, $notice)
259 $user = $flink->getUser();
260 $code = $e->getCode();
262 $logmsg = sprintf('Twitter bridge - %d posting notice %d for ' .
263 'User %s (user id: %d): %s.',
270 common_log(LOG_WARNING, $logmsg);
274 // Probably a revoked or otherwise bad access token - nuke!
276 remove_twitter_link($flink);
281 // For every other case, it's probably some flakiness so try
282 // sending the notice again later (requeue).
288 function format_status($notice)
290 // XXX: Hack to get around PHP cURL's use of @ being a a meta character
291 $statustxt = preg_replace('/^@/', ' @', $notice->content);
293 // Convert !groups to #hashes
294 $statustxt = preg_replace('/(^|\s)!([A-Za-z0-9]{1,64})/', "\\1#\\2", $statustxt);
299 function remove_twitter_link($flink)
301 $user = $flink->getUser();
303 common_log(LOG_INFO, 'Removing Twitter bridge Foreign link for ' .
304 "user $user->nickname (user id: $user->id).");
306 $result = $flink->delete();
308 if (empty($result)) {
309 common_log(LOG_ERR, 'Could not remove Twitter bridge ' .
310 "Foreign_link for $user->nickname (user id: $user->id)!");
311 common_log_db_error($flink, 'DELETE', __FILE__);
314 // Notify the user that her Twitter bridge is down
316 if (isset($user->email)) {
318 $result = mail_twitter_bridge_removed($user);
322 $msg = 'Unable to send email to notify ' .
323 "$user->nickname (user id: $user->id) " .
324 'that their Twitter bridge link was ' .
327 common_log(LOG_WARNING, $msg);
334 * Send a mail message to notify a user that her Twitter bridge link
335 * has stopped working, and therefore has been removed. This can
336 * happen when the user changes her Twitter password, or otherwise
339 * @param User $user user whose Twitter bridge link has been removed
341 * @return boolean success flag
344 function mail_twitter_bridge_removed($user)
346 common_init_locale($user->language);
348 $profile = $user->getProfile();
350 $subject = sprintf(_m('Your Twitter bridge has been disabled.'));
352 $site_name = common_config('site', 'name');
354 $body = sprintf(_m('Hi, %1$s. We\'re sorry to inform you that your ' .
355 'link to Twitter has been disabled. We no longer seem to have ' .
356 'permission to update your Twitter status. (Did you revoke ' .
357 '%3$s\'s access?)' . "\n\n" .
358 'You can re-enable your Twitter bridge by visiting your ' .
359 "Twitter settings page:\n\n\t%2\$s\n\n" .
360 "Regards,\n%3\$s\n"),
361 $profile->getBestName(),
362 common_local_url('twittersettings'),
363 common_config('site', 'name'));
365 common_init_locale();
366 return mail_to_user($user, $subject, $body);