From c2475f88539ba8053596acd17be39ad0aec98bb4 Mon Sep 17 00:00:00 2001 From: James Walker Date: Tue, 9 Feb 2010 15:37:37 -0500 Subject: in progress Salmon responses --- plugins/OStatus/OStatusPlugin.php | 60 ++++++++++++++++++++++++++++++-- plugins/OStatus/actions/salmon.php | 49 +++++++++++++++++++++++++++ plugins/OStatus/actions/webfinger.php | 7 ++++ plugins/OStatus/lib/Salmon.php | 64 +++++++++++++++++++++++++++++++++++ plugins/OStatus/lib/Webfinger.php | 4 +++ 5 files changed, 181 insertions(+), 3 deletions(-) create mode 100644 plugins/OStatus/actions/salmon.php create mode 100644 plugins/OStatus/lib/Salmon.php (limited to 'plugins') diff --git a/plugins/OStatus/OStatusPlugin.php b/plugins/OStatus/OStatusPlugin.php index ce33344d2..60a4e3827 100644 --- a/plugins/OStatus/OStatusPlugin.php +++ b/plugins/OStatus/OStatusPlugin.php @@ -53,6 +53,7 @@ class OStatusPlugin extends Plugin */ function onRouterInitialized($m) { + // Discovery actions $m->connect('.well-known/host-meta', array('action' => 'hostmeta')); $m->connect('main/webfinger', @@ -65,7 +66,8 @@ class OStatusPlugin extends Plugin array('action' => 'ostatussub')); $m->connect('main/ostatussub', array('action' => 'ostatussub'), array('feed' => '[A-Za-z0-9\.\/\:]+')); - + + // PuSH actions $m->connect('main/push/hub', array('action' => 'pushhub')); $m->connect('main/push/callback/:feed', @@ -73,6 +75,11 @@ class OStatusPlugin extends Plugin array('feed' => '[0-9]+')); $m->connect('settings/feedsub', array('action' => 'feedsubsettings')); + + // Salmon endpoint + $m->connect('salmon/user/:id', + array('action' => 'salmon'), + array('id' => '[0-9]+')); return true; } @@ -111,11 +118,15 @@ class OStatusPlugin extends Plugin // Updates will be handled for our internal PuSH hub. $action->element('link', array('rel' => 'hub', 'href' => common_local_url('pushhub'))); + + // Also, we'll add in the salmon link + $action->element('link', array('rel' => 'salmon', + 'href' => common_local_url('salmon'))); } } return true; } - + /** * Add the feed settings page to the Connect Settings menu * @@ -180,8 +191,51 @@ class OStatusPlugin extends Plugin $output->elementEnd('li'); } } - + /** + * Check if we've got some Salmon stuff to send + */ + function onEndNoticeSave($notice) + { + $count = preg_match_all('/(\w+\.)*\w+@(\w+\.)*\w+(\w+\-\w+)*\.\w+/', $notice->content, $matches); + if ($count) { + foreach ($matches[0] as $webfinger) { + // Check to see if we've got an actual webfinger + $w = new Webfinger; + + $endpoint_uri = ''; + + $result = $w->lookup($webfinger); + if (empty($result)) { + continue; + } + + foreach ($result->links as $link) { + if ($link['rel'] == 'salmon') { + $endpoint_uri = $link['href']; + } + } + + if (empty($endpoint_uri)) { + continue; + } + + $profile = $notice->getProfile(); + + $acct = $profile->nickname .'@'. common_config('site', 'server'); + + $xml = ''; + $xml .= $notice->asAtomEntry(); + // TODO : need to set author/uri to webfinger acct. more cleanly + $xml = preg_replace('/([^<])*<\/uri>/i', 'acct:'.$acct.'', $xml); + + + $salmon = new Salmon(); + $salmon->post($endpoint_uri, $xml); + } + } + } + function onCheckSchema() { // warning: the autoincrement doesn't seem to set. diff --git a/plugins/OStatus/actions/salmon.php b/plugins/OStatus/actions/salmon.php new file mode 100644 index 000000000..012869cf7 --- /dev/null +++ b/plugins/OStatus/actions/salmon.php @@ -0,0 +1,49 @@ +. + */ + +/** + * @package OStatusPlugin + * @author James Walker + */ + +if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } + +class SalmonAction extends Action +{ + + function handle() + { + parent::handle(); + if ($_SERVER['REQUEST_METHOD'] == 'POST') { + $this->handlePost(); + } + } + + + function handlePost() + { + $user_id = $this->arg('id'); + common_log(LOG_DEBUG, 'Salmon: incoming post for user: '. $user_id); + + $xml = file_get_contents('php://input'); + + // TODO : Insert new $xml -> notice code + + } +} diff --git a/plugins/OStatus/actions/webfinger.php b/plugins/OStatus/actions/webfinger.php index ec2dddd53..75ba16638 100644 --- a/plugins/OStatus/actions/webfinger.php +++ b/plugins/OStatus/actions/webfinger.php @@ -58,6 +58,13 @@ class WebfingerAction extends Action $xrd->links[] = array('rel' => 'http://webfinger.net/rel/profile-page', 'type' => 'text/html', 'href' => common_profile_url($nick)); + + $salmon_url = common_local_url('salmon', + array('id' => $this->user->id)); + + $xrd->links[] = array('rel' => 'salmon', + 'href' => $salmon_url); + // TODO - finalize where the redirect should go on the publisher $url = common_local_url('ostatussub') . '?feed={uri}'; $xrd->links[] = array('rel' => 'http://ostatus.org/schema/1.0/subscribe', diff --git a/plugins/OStatus/lib/Salmon.php b/plugins/OStatus/lib/Salmon.php new file mode 100644 index 000000000..8c77222a6 --- /dev/null +++ b/plugins/OStatus/lib/Salmon.php @@ -0,0 +1,64 @@ +. + * + * @package StatusNet + * @author James Walker + * @copyright 2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 + * @link http://status.net/ + */ +class Salmon +{ + public function post($endpoint_uri, $xml) + { + if (empty($endpoint_uri)) { + return FALSE; + } + + $headers = array('Content-type: application/atom+xml'); + + try { + $client = new HTTPClient(); + $client->setBody($xml); + $response = $client->post($endpoint_uri, $headers); + } catch (HTTP_Request2_Exception $e) { + return false; + } + if ($response->getStatus() != 200) { + return false; + } + + } + + public function createMagicEnv($text, $userid) + { + + + } + + + public function verifyMagicEnv($env) + { + + + } +} diff --git a/plugins/OStatus/lib/Webfinger.php b/plugins/OStatus/lib/Webfinger.php index 7ab6b421b..417d54904 100644 --- a/plugins/OStatus/lib/Webfinger.php +++ b/plugins/OStatus/lib/Webfinger.php @@ -76,6 +76,10 @@ class Webfinger { $url = 'http://'. $domain .'/.well-known/host-meta'; $content = $this->fetchURL($url); + if (empty($content)) { + common_log(LOG_DEBUG, 'Error fetching host-meta'); + return false; + } $result = XRD::parse($content); // Ensure that the host == domain (spec may include signing later) -- cgit v1.2.3-54-g00ecf