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 function update_twitter_user($twitter_id, $screen_name)
28 $uri = 'http://twitter.com/' . $screen_name;
29 $fuser = new Foreign_user();
31 $fuser->query('BEGIN');
33 // Dropping down to SQL because regular DB_DataObject udpate stuff doesn't seem
34 // to work so good with tables that have multiple column primary keys
36 // Any time we update the uri for a forein user we have to make sure there
37 // are no dupe entries first -- unique constraint on the uri column
39 $qry = 'UPDATE foreign_user set uri = \'\' WHERE uri = ';
40 $qry .= '\'' . $uri . '\'' . ' AND service = ' . TWITTER_SERVICE;
46 $qry = 'UPDATE foreign_user SET nickname = ';
47 $qry .= '\'' . $screen_name . '\'' . ', uri = \'' . $uri . '\' ';
48 $qry .= 'WHERE id = ' . $twitter_id . ' AND service = ' . TWITTER_SERVICE;
50 $fuser->query('COMMIT');
58 function add_twitter_user($twitter_id, $screen_name)
61 $new_uri = 'http://twitter.com/' . $screen_name;
63 // Clear out any bad old foreign_users with the new user's legit URL
64 // This can happen when users move around or fakester accounts get
65 // repoed, and things like that.
67 $luser = new Foreign_user();
68 $luser->uri = $new_uri;
69 $luser->service = TWITTER_SERVICE;
70 $result = $luser->delete();
73 common_log(LOG_WARNING,
74 "Twitter bridge - removed invalid Twitter user squatting on uri: $new_uri");
80 // Otherwise, create a new Twitter user
82 $fuser = new Foreign_user();
84 $fuser->nickname = $screen_name;
85 $fuser->uri = 'http://twitter.com/' . $screen_name;
86 $fuser->id = $twitter_id;
87 $fuser->service = TWITTER_SERVICE;
88 $fuser->created = common_sql_now();
89 $result = $fuser->insert();
92 common_log(LOG_WARNING,
93 "Twitter bridge - failed to add new Twitter user: $twitter_id - $screen_name.");
94 common_log_db_error($fuser, 'INSERT', __FILE__);
96 common_debug("Twitter bridge - Added new Twitter user: $screen_name ($twitter_id).");
102 // Creates or Updates a Twitter user
103 function save_twitter_user($twitter_id, $screen_name)
106 // Check to see whether the Twitter user is already in the system,
107 // and update its screen name and uri if so.
109 $fuser = Foreign_user::getForeignUser($twitter_id, TWITTER_SERVICE);
111 if (!empty($fuser)) {
115 // Only update if Twitter screen name has changed
117 if ($fuser->nickname != $screen_name) {
118 $result = update_twitter_user($twitter_id, $screen_name);
120 common_debug('Twitter bridge - Updated nickname (and URI) for Twitter user ' .
121 "$fuser->id to $screen_name, was $fuser->nickname");
127 return add_twitter_user($twitter_id, $screen_name);
136 function is_twitter_bound($notice, $flink) {
138 // Check to see if notice should go to Twitter
139 if (!empty($flink) && ($flink->noticesync & FOREIGN_NOTICE_SEND)) {
141 // If it's not a Twitter-style reply, or if the user WANTS to send replies.
142 if (!preg_match('/^@[a-zA-Z0-9_]{1,15}\b/u', $notice->content) ||
143 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY)) {
151 function broadcast_twitter($notice)
153 $flink = Foreign_link::getByUserID($notice->profile_id,
156 if (is_twitter_bound($notice, $flink)) {
157 if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
158 return broadcast_oauth($notice, $flink);
160 return broadcast_basicauth($notice, $flink);
167 function broadcast_oauth($notice, $flink) {
168 $user = $flink->getUser();
169 $statustxt = format_status($notice);
170 // Convert !groups to #hashes
171 $statustxt = preg_replace('/(^|\s)!([A-Za-z0-9]{1,64})/', "\\1#\\2", $statustxt);
172 $token = TwitterOAuthClient::unpackToken($flink->credentials);
173 $client = new TwitterOAuthClient($token->key, $token->secret);
177 $status = $client->statusesUpdate($statustxt);
178 } catch (OAuthClientCurlException $e) {
179 return process_error($e, $flink);
182 if (empty($status)) {
184 // This could represent a failure posting,
185 // or the Twitter API might just be behaving flakey.
187 $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' .
188 'trying to send update for %1$s (user id %2$s).',
189 $user->nickname, $user->id);
190 common_log(LOG_WARNING, $errmsg);
195 // Notice crossed the great divide
197 $msg = sprintf('Twitter bridge - posted notice %s to Twitter using OAuth.',
199 common_log(LOG_INFO, $msg);
204 function broadcast_basicauth($notice, $flink)
206 $user = $flink->getUser();
208 $statustxt = format_status($notice);
210 $client = new TwitterBasicAuthClient($flink);
214 $status = $client->statusesUpdate($statustxt);
215 } catch (BasicAuthCurlException $e) {
216 return process_error($e, $flink);
219 if (empty($status)) {
221 $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' .
222 'trying to send update for %1$s (user id %2$s).',
223 $user->nickname, $user->id);
224 common_log(LOG_WARNING, $errmsg);
226 $errmsg = sprintf('No data returned by Twitter API when ' .
227 'trying to send update for %1$s (user id %2$s).',
228 $user->nickname, $user->id);
229 common_log(LOG_WARNING, $errmsg);
233 $msg = sprintf('Twitter bridge - posted notice %s to Twitter using basic auth.',
235 common_log(LOG_INFO, $msg);
240 function process_error($e, $flink)
242 $user = $flink->getUser();
243 $errmsg = $e->getMessage();
247 case 'The requested URL returned error: 401':
248 $logmsg = sprintf('Twiter bridge - User %1$s (user id: %2$s) has an invalid ' .
249 'Twitter screen_name/password combo or an invalid acesss token.',
250 $user->nickname, $user->id);
252 remove_twitter_link($flink);
254 case 'The requested URL returned error: 403':
255 $logmsg = sprintf('Twitter bridge - User %1$s (user id: %2$s) has exceeded ' .
256 'his/her Twitter request limit.',
257 $user->nickname, $user->id);
260 $logmsg = sprintf('Twitter bridge - cURL error trying to send notice to Twitter ' .
261 'for user %1$s (user id: %2$s) - ' .
262 'code: %3$s message: %4$s.',
263 $user->nickname, $user->id,
264 $e->getCode(), $e->getMessage());
268 common_log(LOG_WARNING, $logmsg);
273 function format_status($notice)
275 // XXX: Hack to get around PHP cURL's use of @ being a a meta character
276 return preg_replace('/^@/', ' @', $notice->content);
279 function remove_twitter_link($flink)
281 $user = $flink->getUser();
283 common_log(LOG_INFO, 'Removing Twitter bridge Foreign link for ' .
284 "user $user->nickname (user id: $user->id).");
286 $result = $flink->delete();
288 if (empty($result)) {
289 common_log(LOG_ERR, 'Could not remove Twitter bridge ' .
290 "Foreign_link for $user->nickname (user id: $user->id)!");
291 common_log_db_error($flink, 'DELETE', __FILE__);
294 // Notify the user that her Twitter bridge is down
296 if (isset($user->email)) {
298 $result = mail_twitter_bridge_removed($user);
302 $msg = 'Unable to send email to notify ' .
303 "$user->nickname (user id: $user->id) " .
304 'that their Twitter bridge link was ' .
307 common_log(LOG_WARNING, $msg);