From 7155cf813de1502e796c0eac1e680289f3552c29 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Wed, 11 Feb 2009 17:46:53 -0800 Subject: Move/reorg Twitter broadcast code to lib/twitter.php in prep for making a twitterqueuehandler. --- lib/util.php | 87 +++++------------------------------------------------------- 1 file changed, 7 insertions(+), 80 deletions(-) (limited to 'lib/util.php') diff --git a/lib/util.php b/lib/util.php index c0c980111..3f4fae3a5 100644 --- a/lib/util.php +++ b/lib/util.php @@ -796,24 +796,6 @@ function common_redirect($url, $code=307) function common_broadcast_notice($notice, $remote=false) { - - // Check to see if notice should go to Twitter - $flink = Foreign_link::getByUserID($notice->profile_id, 1); // 1 == Twitter - if (($flink->noticesync & FOREIGN_NOTICE_SEND) == FOREIGN_NOTICE_SEND) { - - // If it's not a Twitter-style reply, or if the user WANTS to send replies... - - if (!preg_match('/^@[a-zA-Z0-9_]{1,15}\b/u', $notice->content) || - (($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) == FOREIGN_NOTICE_SEND_REPLY)) { - - $result = common_twitter_broadcast($notice, $flink); - - if (!$result) { - common_debug('Unable to send notice: ' . $notice->id . ' to Twitter.', __FILE__); - } - } - } - if (common_config('queue', 'enabled')) { // Do it later! return common_enqueue_notice($notice); @@ -822,68 +804,6 @@ function common_broadcast_notice($notice, $remote=false) } } -function common_twitter_broadcast($notice, $flink) -{ - global $config; - $success = true; - $fuser = $flink->getForeignUser(); - $twitter_user = $fuser->nickname; - $twitter_password = $flink->credentials; - $uri = 'http://www.twitter.com/statuses/update.json'; - - // XXX: Hack to get around PHP cURL's use of @ being a a meta character - $statustxt = preg_replace('/^@/', ' @', $notice->content); - - $options = array( - CURLOPT_USERPWD => "$twitter_user:$twitter_password", - CURLOPT_POST => true, - CURLOPT_POSTFIELDS => array( - 'status' => $statustxt, - 'source' => $config['integration']['source'] - ), - CURLOPT_RETURNTRANSFER => true, - CURLOPT_FAILONERROR => true, - CURLOPT_HEADER => false, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_USERAGENT => "Laconica", - CURLOPT_CONNECTTIMEOUT => 120, // XXX: Scary!!!! How long should this be? - CURLOPT_TIMEOUT => 120, - - # Twitter is strict about accepting invalid "Expect" headers - CURLOPT_HTTPHEADER => array('Expect:') - ); - - $ch = curl_init($uri); - curl_setopt_array($ch, $options); - $data = curl_exec($ch); - $errmsg = curl_error($ch); - - if ($errmsg) { - common_debug("cURL error: $errmsg - trying to send notice for $twitter_user.", - __FILE__); - $success = false; - } - - curl_close($ch); - - if (!$data) { - common_debug("No data returned by Twitter's API trying to send update for $twitter_user", - __FILE__); - $success = false; - } - - // Twitter should return a status - $status = json_decode($data); - - if (!$status->id) { - common_debug("Unexpected data returned by Twitter API trying to send update for $twitter_user", - __FILE__); - $success = false; - } - - return $success; -} - // Stick the notice on the queue function common_enqueue_notice($notice) @@ -935,6 +855,13 @@ function common_real_broadcast($notice, $remote=false) common_log(LOG_ERR, 'Error in public broadcast for notice ' . $notice->id); } } + if ($success) { + $success = broadcast_twitter($notice); + if (!$success) { + common_log(LOG_ERR, 'Error in Twitter broadcast for notice ' . $notice->id); + } + } + // XXX: broadcast notices to other IM return $success; } -- cgit v1.2.3-54-g00ecf From f8e2ad0677d6eede93e3008452e36985ea999d74 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Thu, 12 Feb 2009 14:39:21 -0800 Subject: The fabled twitterqueuehandler --- lib/twitter.php | 2 +- lib/util.php | 2 +- scripts/startdaemons.sh | 3 +- scripts/stopdaemons.sh | 2 +- scripts/twitterqueuehandler.php | 71 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 4 deletions(-) create mode 100755 scripts/twitterqueuehandler.php (limited to 'lib/util.php') diff --git a/lib/twitter.php b/lib/twitter.php index 72212c185..deb6fd276 100644 --- a/lib/twitter.php +++ b/lib/twitter.php @@ -231,7 +231,7 @@ function broadcast_twitter($notice) TWITTER_SERVICE); // XXX: Not sure WHERE to check whether a notice should go to - // Twitter. Should we even put in the queue if it's not? --Zach + // Twitter. Should we even put in the queue if it shouldn't? --Zach if (is_twitter_bound($notice, $flink)) { $fuser = $flink->getForeignUser(); diff --git a/lib/util.php b/lib/util.php index 3f4fae3a5..7923fe28a 100644 --- a/lib/util.php +++ b/lib/util.php @@ -808,7 +808,7 @@ function common_broadcast_notice($notice, $remote=false) function common_enqueue_notice($notice) { - foreach (array('jabber', 'omb', 'sms', 'public') as $transport) { + foreach (array('jabber', 'omb', 'sms', 'public', 'twitter') as $transport) { $qi = new Queue_item(); $qi->notice_id = $notice->id; $qi->transport = $transport; diff --git a/scripts/startdaemons.sh b/scripts/startdaemons.sh index 685bd938f..269036393 100755 --- a/scripts/startdaemons.sh +++ b/scripts/startdaemons.sh @@ -23,7 +23,8 @@ DIR=`dirname $0` for f in xmppdaemon.php jabberqueuehandler.php publicqueuehandler.php \ - xmppconfirmhandler.php smsqueuehandler.php ombqueuehandler.php; do + xmppconfirmhandler.php smsqueuehandler.php ombqueuehandler.php \ + twitterqueuehandler.php; do echo -n "Starting $f..."; php $DIR/$f diff --git a/scripts/stopdaemons.sh b/scripts/stopdaemons.sh index 08e1d4714..b69d296d3 100755 --- a/scripts/stopdaemons.sh +++ b/scripts/stopdaemons.sh @@ -24,7 +24,7 @@ SDIR=`dirname $0` DIR=`php $SDIR/getpiddir.php` for f in jabberhandler ombhandler publichandler smshandler \ - xmppconfirmhandler xmppdaemon; do + xmppconfirmhandler xmppdaemon twitterhandler ; do FILES="$DIR/$f.*.pid" for ff in "$FILES" ; do diff --git a/scripts/twitterqueuehandler.php b/scripts/twitterqueuehandler.php new file mode 100755 index 000000000..7da4f1e20 --- /dev/null +++ b/scripts/twitterqueuehandler.php @@ -0,0 +1,71 @@ +#!/usr/bin/env php +. + */ + +# Abort if called from a web server +if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) { + print "This script must be run from the command line\n"; + exit(); +} + +define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); +define('LACONICA', true); + +require_once(INSTALLDIR . '/lib/common.php'); +require_once(INSTALLDIR . '/lib/twitter.php'); +require_once(INSTALLDIR . '/lib/queuehandler.php'); + +set_error_handler('common_error_handler'); + +class TwitterQueueHandler extends QueueHandler +{ + + function transport() + { + return 'twitter'; + } + + function start() + { + $this->log(LOG_INFO, "INITIALIZE"); + return true; + } + + function handle_notice($notice) + { + return broadcast_twitter($notice); + } + + function finish() + { + } + +} + +ini_set("max_execution_time", "0"); +ini_set("max_input_time", "0"); +set_time_limit(0); + +mb_internal_encoding('UTF-8'); + +$id = ($argc > 1) ? $argv[1] : null; + +$handler = new TwitterQueueHandler($id); + +$handler->runOnce(); -- cgit v1.2.3-54-g00ecf