3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008-2010 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 add_twitter_user($twitter_id, $screen_name)
31 // Clear out any bad old foreign_users with the new user's legit URL
32 // This can happen when users move around or fakester accounts get
33 // repoed, and things like that.
35 $luser = Foreign_user::getForeignUser($twitter_id, TWITTER_SERVICE);
38 $result = $luser->delete();
39 if ($result != false) {
42 "Twitter bridge - removed old Twitter user: $screen_name ($twitter_id)."
47 $fuser = new Foreign_user();
49 $fuser->nickname = $screen_name;
50 $fuser->uri = 'http://twitter.com/' . $screen_name;
51 $fuser->id = $twitter_id;
52 $fuser->service = TWITTER_SERVICE;
53 $fuser->created = common_sql_now();
54 $result = $fuser->insert();
57 common_log(LOG_WARNING,
58 "Twitter bridge - failed to add new Twitter user: $twitter_id - $screen_name.");
59 common_log_db_error($fuser, 'INSERT', __FILE__);
62 "Twitter bridge - Added new Twitter user: $screen_name ($twitter_id).");
68 // Creates or Updates a Twitter user
69 function save_twitter_user($twitter_id, $screen_name)
71 // Check to see whether the Twitter user is already in the system,
72 // and update its screen name and uri if so.
74 $fuser = Foreign_user::getForeignUser($twitter_id, TWITTER_SERVICE);
80 // Delete old record if Twitter user changed screen name
82 if ($fuser->nickname != $screen_name) {
83 $oldname = $fuser->nickname;
85 common_log(LOG_INFO, sprintf('Twitter bridge - Updated nickname (and URI) ' .
86 'for Twitter user %1$d - %2$s, was %3$s.',
93 return add_twitter_user($twitter_id, $screen_name);
96 function is_twitter_bound($notice, $flink) {
98 // Check to see if notice should go to Twitter
99 if (!empty($flink) && ($flink->noticesync & FOREIGN_NOTICE_SEND)) {
101 // If it's not a Twitter-style reply, or if the user WANTS to send replies.
102 if (!preg_match('/^@[a-zA-Z0-9_]{1,15}\b/u', $notice->content) ||
103 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY)) {
111 function broadcast_twitter($notice)
113 $flink = Foreign_link::getByUserID($notice->profile_id,
116 if (is_twitter_bound($notice, $flink)) {
117 if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
118 return broadcast_oauth($notice, $flink);
120 return broadcast_basicauth($notice, $flink);
127 function broadcast_oauth($notice, $flink) {
128 $user = $flink->getUser();
129 $statustxt = format_status($notice);
130 $token = TwitterOAuthClient::unpackToken($flink->credentials);
131 $client = new TwitterOAuthClient($token->key, $token->secret);
135 $status = $client->statusesUpdate($statustxt);
136 } catch (OAuthClientException $e) {
137 return process_error($e, $flink, $notice);
140 if (empty($status)) {
142 // This could represent a failure posting,
143 // or the Twitter API might just be behaving flakey.
145 $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' .
146 'trying to post notice %d for User %s (user id %d).',
151 common_log(LOG_WARNING, $errmsg);
156 // Notice crossed the great divide
158 $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' .
159 'OAuth for User %s (user id %d).',
164 common_log(LOG_INFO, $msg);
169 function broadcast_basicauth($notice, $flink)
171 $user = $flink->getUser();
173 $statustxt = format_status($notice);
175 $client = new TwitterBasicAuthClient($flink);
179 $status = $client->statusesUpdate($statustxt);
180 } catch (BasicAuthException $e) {
181 return process_error($e, $flink, $notice);
184 if (empty($status)) {
186 $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' .
187 'trying to post notice %d for %s (user id %d).',
192 common_log(LOG_WARNING, $errmsg);
194 $errmsg = sprintf('No data returned by Twitter API when ' .
195 'trying to post notice %d for %s (user id %d).',
199 common_log(LOG_WARNING, $errmsg);
203 $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' .
204 'HTTP basic auth for User %s (user id %d).',
209 common_log(LOG_INFO, $msg);
214 function process_error($e, $flink, $notice)
216 $user = $flink->getUser();
217 $code = $e->getCode();
219 $logmsg = sprintf('Twitter bridge - %d posting notice %d for ' .
220 'User %s (user id: %d): %s.',
227 common_log(LOG_WARNING, $logmsg);
231 // Probably a revoked or otherwise bad access token - nuke!
232 remove_twitter_link($flink);
236 // User has exceeder her rate limit -- toss the notice
241 // For every other case, it's probably some flakiness so try
242 // sending the notice again later (requeue).
249 function format_status($notice)
251 // XXX: Hack to get around PHP cURL's use of @ being a a meta character
252 $statustxt = preg_replace('/^@/', ' @', $notice->content);
254 // Convert !groups to #hashes
256 // XXX: Make this an optional setting?
258 $statustxt = preg_replace('/(^|\s)!([A-Za-z0-9]{1,64})/', "\\1#\\2", $statustxt);
260 if (mb_strlen($statustxt) > 140) {
261 $noticeUrl = common_shorten_url($notice->uri);
262 $urlLen = mb_strlen($noticeUrl);
263 $statustxt = mb_substr($statustxt, 0, 140 - ($urlLen + 3)) . ' … ' . $noticeUrl;
269 function remove_twitter_link($flink)
271 $user = $flink->getUser();
273 common_log(LOG_INFO, 'Removing Twitter bridge Foreign link for ' .
274 "user $user->nickname (user id: $user->id).");
276 $result = $flink->delete();
278 if (empty($result)) {
279 common_log(LOG_ERR, 'Could not remove Twitter bridge ' .
280 "Foreign_link for $user->nickname (user id: $user->id)!");
281 common_log_db_error($flink, 'DELETE', __FILE__);
284 // Notify the user that her Twitter bridge is down
286 if (isset($user->email)) {
288 $result = mail_twitter_bridge_removed($user);
292 $msg = 'Unable to send email to notify ' .
293 "$user->nickname (user id: $user->id) " .
294 'that their Twitter bridge link was ' .
297 common_log(LOG_WARNING, $msg);
304 * Send a mail message to notify a user that her Twitter bridge link
305 * has stopped working, and therefore has been removed. This can
306 * happen when the user changes her Twitter password, or otherwise
309 * @param User $user user whose Twitter bridge link has been removed
311 * @return boolean success flag
314 function mail_twitter_bridge_removed($user)
316 common_init_locale($user->language);
318 $profile = $user->getProfile();
320 $subject = sprintf(_m('Your Twitter bridge has been disabled.'));
322 $site_name = common_config('site', 'name');
324 $body = sprintf(_m('Hi, %1$s. We\'re sorry to inform you that your ' .
325 'link to Twitter has been disabled. We no longer seem to have ' .
326 'permission to update your Twitter status. (Did you revoke ' .
327 '%3$s\'s access?)' . "\n\n" .
328 'You can re-enable your Twitter bridge by visiting your ' .
329 "Twitter settings page:\n\n\t%2\$s\n\n" .
330 "Regards,\n%3\$s\n"),
331 $profile->getBestName(),
332 common_local_url('twittersettings'),
333 common_config('site', 'name'));
335 common_init_locale();
336 return mail_to_user($user, $subject, $body);