Merge branch 'queuehandlers' into 0.7.x
This commit is contained in:
commit
47e595b092
@ -32,6 +32,7 @@ if (!defined('LACONICA')) {
|
||||
}
|
||||
|
||||
require_once INSTALLDIR.'/lib/connectsettingsaction.php';
|
||||
require_once INSTALLDIR.'/lib/twitter.php';
|
||||
|
||||
define('SUBSCRIPTIONS', 80);
|
||||
|
||||
@ -90,7 +91,7 @@ class TwittersettingsAction extends ConnectSettingsAction
|
||||
|
||||
$fuser = null;
|
||||
|
||||
$flink = Foreign_link::getByUserID($user->id, 1); // 1 == Twitter
|
||||
$flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
|
||||
|
||||
if ($flink) {
|
||||
$fuser = $flink->getForeignUser();
|
||||
@ -358,7 +359,7 @@ class TwittersettingsAction extends ConnectSettingsAction
|
||||
|
||||
$flink->user_id = $user->id;
|
||||
$flink->foreign_id = $twit_user->id;
|
||||
$flink->service = 1; // Twitter
|
||||
$flink->service = TWITTER_SERVICE;
|
||||
$flink->credentials = $password;
|
||||
$flink->created = common_sql_now();
|
||||
|
||||
|
@ -19,6 +19,8 @@
|
||||
|
||||
if (!defined('LACONICA')) { exit(1); }
|
||||
|
||||
define("TWITTER_SERVICE", 1); // Twitter is foreign_service ID 1
|
||||
|
||||
function get_twitter_data($uri, $screen_name, $password)
|
||||
{
|
||||
|
||||
@ -28,14 +30,13 @@ function get_twitter_data($uri, $screen_name, $password)
|
||||
CURLOPT_FAILONERROR => true,
|
||||
CURLOPT_HEADER => false,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
# CURLOPT_USERAGENT => "identi.ca",
|
||||
CURLOPT_USERAGENT => "Laconica",
|
||||
CURLOPT_CONNECTTIMEOUT => 120,
|
||||
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);
|
||||
@ -95,7 +96,7 @@ function add_twitter_user($twitter_id, $screen_name)
|
||||
$fuser->nickname = $screen_name;
|
||||
$fuser->uri = 'http://twitter.com/' . $screen_name;
|
||||
$fuser->id = $twitter_id;
|
||||
$fuser->service = 1; // Twitter
|
||||
$fuser->service = TWITTER_SERVICE; // Twitter
|
||||
$fuser->created = common_sql_now();
|
||||
$result = $fuser->insert();
|
||||
|
||||
@ -206,3 +207,93 @@ function save_twitter_friends($user, $twitter_id, $screen_name, $password)
|
||||
return true;
|
||||
}
|
||||
|
||||
function is_twitter_bound($notice, $flink) {
|
||||
|
||||
// Check to see if notice should go to Twitter
|
||||
if (($flink->noticesync & 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)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function broadcast_twitter($notice)
|
||||
{
|
||||
global $config;
|
||||
$success = true;
|
||||
|
||||
$flink = Foreign_link::getByUserID($notice->profile_id,
|
||||
TWITTER_SERVICE);
|
||||
|
||||
// XXX: Not sure WHERE to check whether a notice should go to
|
||||
// Twitter. Should we even put in the queue if it shouldn't? --Zach
|
||||
if (is_twitter_bound($notice, $flink)) {
|
||||
|
||||
$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: 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;
|
||||
}
|
||||
|
||||
|
89
lib/util.php
89
lib/util.php
@ -825,24 +825,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);
|
||||
@ -851,73 +833,11 @@ 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)
|
||||
{
|
||||
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;
|
||||
@ -964,6 +884,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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
71
scripts/twitterqueuehandler.php
Executable file
71
scripts/twitterqueuehandler.php
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
/*
|
||||
* Laconica - a distributed open-source microblogging tool
|
||||
* Copyright (C) 2008, Controlez-Vous, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
# 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();
|
Loading…
Reference in New Issue
Block a user