summaryrefslogtreecommitdiff
path: root/plugins/OStatus/actions
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/OStatus/actions')
-rw-r--r--plugins/OStatus/actions/feedsubsettings.php230
-rw-r--r--plugins/OStatus/actions/ostatusinit.php4
-rw-r--r--plugins/OStatus/actions/pushcallback.php34
-rw-r--r--plugins/OStatus/actions/pushhub.php141
-rw-r--r--plugins/OStatus/actions/usersalmon.php12
5 files changed, 113 insertions, 308 deletions
diff --git a/plugins/OStatus/actions/feedsubsettings.php b/plugins/OStatus/actions/feedsubsettings.php
deleted file mode 100644
index aee4cee9a..000000000
--- a/plugins/OStatus/actions/feedsubsettings.php
+++ /dev/null
@@ -1,230 +0,0 @@
-<?php
-/*
- * StatusNet - the distributed open-source microblogging tool
- * Copyright (C) 2009, StatusNet, 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/>.
- */
-
-/**
- * @package FeedSubPlugin
- * @maintainer Brion Vibber <brion@status.net>
- */
-
-if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
-
-class FeedSubSettingsAction extends ConnectSettingsAction
-{
- protected $profile_uri;
- protected $preview;
- protected $munger;
-
- /**
- * Title of the page
- *
- * @return string Title of the page
- */
-
- function title()
- {
- return _m('Feed subscriptions');
- }
-
- /**
- * Instructions for use
- *
- * @return instructions for use
- */
-
- function getInstructions()
- {
- return _m('You can subscribe to feeds from other sites; ' .
- 'updates will appear in your personal timeline.');
- }
-
- /**
- * Content area of the page
- *
- * Shows a form for associating a Twitter account with this
- * StatusNet account. Also lets the user set preferences.
- *
- * @return void
- */
-
- function showContent()
- {
- $user = common_current_user();
-
- $profile = $user->getProfile();
-
- $this->elementStart('form', array('method' => 'post',
- 'id' => 'form_settings_feedsub',
- 'class' => 'form_settings',
- 'action' =>
- common_local_url('feedsubsettings')));
-
- $this->hidden('token', common_session_token());
-
- $this->elementStart('fieldset', array('id' => 'settings_feeds'));
-
- $this->elementStart('ul', 'form_data');
- $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
- $this->input('profile_uri',
- _m('Feed URL'),
- $this->profile_uri,
- _m('Enter the profile URL of a PubSubHubbub-enabled feed'));
- $this->elementEnd('li');
- $this->elementEnd('ul');
-
- if ($this->preview) {
- $this->submit('subscribe', _m('Subscribe'));
- } else {
- $this->submit('validate', _m('Continue'));
- }
-
- $this->elementEnd('fieldset');
-
- $this->elementEnd('form');
-
- if ($this->preview) {
- $this->previewFeed();
- }
- }
-
- /**
- * Handle posts to this form
- *
- * Based on the button that was pressed, muxes out to other functions
- * to do the actual task requested.
- *
- * All sub-functions reload the form with a message -- success or failure.
- *
- * @return void
- */
-
- function handlePost()
- {
- // CSRF protection
- $token = $this->trimmed('token');
- if (!$token || $token != common_session_token()) {
- $this->showForm(_('There was a problem with your session token. '.
- 'Try again, please.'));
- return;
- }
-
- if ($this->arg('validate')) {
- $this->validateAndPreview();
- } else if ($this->arg('subscribe')) {
- $this->saveFeed();
- } else {
- $this->showForm(_('Unexpected form submission.'));
- }
- }
-
- /**
- * Set up and add a feed
- *
- * @return boolean true if feed successfully read
- * Sends you back to input form if not.
- */
- function validateFeed()
- {
- $profile_uri = trim($this->arg('profile_uri'));
-
- if ($profile_uri == '') {
- $this->showForm(_m('Empty remote profile URL!'));
- return;
- }
- $this->profile_uri = $profile_uri;
-
- // @fixme validate, normalize bla bla
- try {
- $oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
- $this->oprofile = $oprofile;
- return true;
- } catch (FeedSubBadURLException $e) {
- $err = _m('Invalid URL or could not reach server.');
- } catch (FeedSubBadResponseException $e) {
- $err = _m('Cannot read feed; server returned error.');
- } catch (FeedSubEmptyException $e) {
- $err = _m('Cannot read feed; server returned an empty page.');
- } catch (FeedSubBadHTMLException $e) {
- $err = _m('Bad HTML, could not find feed link.');
- } catch (FeedSubNoFeedException $e) {
- $err = _m('Could not find a feed linked from this URL.');
- } catch (FeedSubUnrecognizedTypeException $e) {
- $err = _m('Not a recognized feed type.');
- } catch (FeedSubException $e) {
- // Any new ones we forgot about
- $err = sprintf(_m('Bad feed URL: %s %s'), get_class($e), $e->getMessage());
- }
-
- $this->showForm($err);
- return false;
- }
-
- function saveFeed()
- {
- if ($this->validateFeed()) {
- $this->preview = true;
-
- // And subscribe the current user to the local profile
- $user = common_current_user();
-
- if (!$this->oprofile->subscribe()) {
- $this->showForm(_m("Failed to set up server-to-server subscription."));
- return;
- }
-
- if ($this->oprofile->isGroup()) {
- $group = $this->oprofile->localGroup();
- if ($user->isMember($group)) {
- $this->showForm(_m('Already a member!'));
- } elseif (Group_member::join($this->profile->group_id, $user->id)) {
- $this->showForm(_m('Joined remote group!'));
- } else {
- $this->showForm(_m('Remote group join failed!'));
- }
- } else {
- $local = $this->oprofile->localProfile();
- if ($user->isSubscribed($local)) {
- $this->showForm(_m('Already subscribed!'));
- } elseif ($this->oprofile->subscribeLocalToRemote($user)) {
- $this->showForm(_m('Remote user subscribed!'));
- } else {
- $this->showForm(_m('Remote subscription failed!'));
- }
- }
- }
- }
-
- function validateAndPreview()
- {
- if ($this->validateFeed()) {
- $this->preview = true;
- $this->showForm(_m('Previewing feed:'));
- }
- }
-
- function previewFeed()
- {
- $this->text('Profile preview should go here');
- }
-
- function showScripts()
- {
- parent::showScripts();
- $this->autofocus('feedurl');
- }
-}
diff --git a/plugins/OStatus/actions/ostatusinit.php b/plugins/OStatus/actions/ostatusinit.php
index 4afde2c36..abd8cb541 100644
--- a/plugins/OStatus/actions/ostatusinit.php
+++ b/plugins/OStatus/actions/ostatusinit.php
@@ -119,7 +119,7 @@ class OStatusInitAction extends Action
} else {
$this->connectProfile($this->acct);
}
- } elseif (strpos('@', $this->acct) !== false) {
+ } elseif (strpos($this->acct, '@') !== false) {
$this->connectWebfinger($this->acct);
}
}
@@ -139,7 +139,7 @@ class OStatusInitAction extends Action
$user = User::staticGet('nickname', $this->nickname);
$target_profile = common_local_url('userbyid', array('id' => $user->id));
- $url = $w->applyTemplate($link['template'], $feed_url);
+ $url = $w->applyTemplate($link['template'], $target_profile);
common_redirect($url, 303);
}
diff --git a/plugins/OStatus/actions/pushcallback.php b/plugins/OStatus/actions/pushcallback.php
index 35c92c732..4184f0e0c 100644
--- a/plugins/OStatus/actions/pushcallback.php
+++ b/plugins/OStatus/actions/pushcallback.php
@@ -72,7 +72,7 @@ class PushCallbackAction extends Action
}
/**
- * Handler for GET verification requests from the hub
+ * Handler for GET verification requests from the hub.
*/
function handleGet()
{
@@ -81,31 +81,37 @@ class PushCallbackAction extends Action
$challenge = $this->arg('hub_challenge');
$lease_seconds = $this->arg('hub_lease_seconds');
$verify_token = $this->arg('hub_verify_token');
-
+
if ($mode != 'subscribe' && $mode != 'unsubscribe') {
- common_log(LOG_WARNING, __METHOD__ . ": bogus hub callback with mode \"$mode\"");
- throw new ServerException("Bogus hub callback: bad mode", 404);
+ throw new ClientException("Bad hub.mode $mode", 404);
}
-
+
$feedsub = FeedSub::staticGet('uri', $topic);
if (!$feedsub) {
- common_log(LOG_WARNING, __METHOD__ . ": bogus hub callback for unknown feed $topic");
- throw new ServerException("Bogus hub callback: unknown feed", 404);
+ throw new ClientException("Bad hub.topic feed $topic", 404);
}
if ($feedsub->verify_token !== $verify_token) {
- common_log(LOG_WARNING, __METHOD__ . ": bogus hub callback with bad token \"$verify_token\" for feed $topic");
- throw new ServerException("Bogus hub callback: bad token", 404);
+ throw new ClientException("Bad hub.verify_token $token for $topic", 404);
}
- if ($mode != $feedsub->sub_state) {
- common_log(LOG_WARNING, __METHOD__ . ": bogus hub callback with bad mode \"$mode\" for feed $topic in state \"{$feedsub->sub_state}\"");
- throw new ServerException("Bogus hub callback: mode doesn't match subscription state.", 404);
+ if ($mode == 'subscribe') {
+ // We may get re-sub requests legitimately.
+ if ($feedsub->sub_state != 'subscribe' && $feedsub->sub_state != 'active') {
+ throw new ClientException("Unexpected subscribe request for $topic.", 404);
+ }
+ } else {
+ if ($feedsub->sub_state != 'unsubscribe') {
+ throw new ClientException("Unexpected unsubscribe request for $topic.", 404);
+ }
}
- // OK!
if ($mode == 'subscribe') {
- common_log(LOG_INFO, __METHOD__ . ': sub confirmed');
+ if ($feedsub->sub_state == 'active') {
+ common_log(LOG_INFO, __METHOD__ . ': sub update confirmed');
+ } else {
+ common_log(LOG_INFO, __METHOD__ . ': sub confirmed');
+ }
$feedsub->confirmSubscribe($lease_seconds);
} else {
common_log(LOG_INFO, __METHOD__ . ": unsub confirmed; deleting sub record for $topic");
diff --git a/plugins/OStatus/actions/pushhub.php b/plugins/OStatus/actions/pushhub.php
index 19599d815..f33690bc4 100644
--- a/plugins/OStatus/actions/pushhub.php
+++ b/plugins/OStatus/actions/pushhub.php
@@ -59,102 +59,121 @@ class PushHubAction extends Action
$mode = $this->trimmed('hub.mode');
switch ($mode) {
case "subscribe":
- $this->subscribe();
- break;
case "unsubscribe":
- $this->unsubscribe();
+ $this->subunsub($mode);
break;
case "publish":
- throw new ServerException("Publishing outside feeds not supported.", 400);
+ throw new ClientException("Publishing outside feeds not supported.", 400);
default:
- throw new ServerException("Unrecognized mode '$mode'.", 400);
+ throw new ClientException("Unrecognized mode '$mode'.", 400);
}
}
/**
- * Process a PuSH feed subscription request.
+ * Process a request for a new or modified PuSH feed subscription.
+ * If asynchronous verification is requested, updates won't be saved immediately.
*
* HTTP return codes:
* 202 Accepted - request saved and awaiting verification
* 204 No Content - already subscribed
- * 403 Forbidden - rejecting this (not specifically spec'd)
+ * 400 Bad Request - rejecting this (not specifically spec'd)
*/
- function subscribe()
+ function subunsub($mode)
{
- $feed = $this->argUrl('hub.topic');
$callback = $this->argUrl('hub.callback');
- $token = $this->arg('hub.verify_token', null);
- common_log(LOG_DEBUG, __METHOD__ . ": checking sub'd to $feed $callback");
- if ($this->getSub($feed, $callback)) {
- // Already subscribed; return 204 per spec.
- header('HTTP/1.1 204 No Content');
- common_log(LOG_DEBUG, __METHOD__ . ': already subscribed');
- return;
+ $topic = $this->argUrl('hub.topic');
+ if (!$this->recognizedFeed($topic)) {
+ throw new ClientException("Unsupported hub.topic $topic; this hub only serves local user and group Atom feeds.");
}
- common_log(LOG_DEBUG, __METHOD__ . ': setting up');
- $sub = new HubSub();
- $sub->topic = $feed;
- $sub->callback = $callback;
- $sub->secret = $this->arg('hub.secret', null);
- if (strlen($sub->secret) > 200) {
- throw new ClientException("hub.secret must be no longer than 200 chars", 400);
+ $verify = $this->arg('hub.verify'); // @fixme may be multiple
+ if ($verify != 'sync' && $verify != 'async') {
+ throw new ClientException("Invalid hub.verify $verify; must be sync or async.");
}
- $sub->setLease(intval($this->arg('hub.lease_seconds')));
- // @fixme check for feeds we don't manage
- // @fixme check the verification mode, might want a return immediately?
+ $lease = $this->arg('hub.lease_seconds', null);
+ if ($mode == 'subscribe' && $lease != '' && !preg_match('/^\d+$/', $lease)) {
+ throw new ClientException("Invalid hub.lease $lease; must be empty or positive integer.");
+ }
+
+ $token = $this->arg('hub.verify_token', null);
- common_log(LOG_DEBUG, __METHOD__ . ': inserting');
- $ok = $sub->insert();
-
- if (!$ok) {
- throw new ServerException("Failed to save subscription record", 500);
+ $secret = $this->arg('hub.secret', null);
+ if ($secret != '' && strlen($secret) >= 200) {
+ throw new ClientException("Invalid hub.secret $secret; must be under 200 bytes.");
}
- // @fixme check errors ;)
+ $sub = HubSub::staticGet($sub->topic, $sub->callback);
+ if (!$sub) {
+ // Creating a new one!
+ $sub = new HubSub();
+ $sub->topic = $topic;
+ $sub->callback = $callback;
+ }
+ if ($mode == 'subscribe') {
+ if ($secret) {
+ $sub->secret = $secret;
+ }
+ if ($lease) {
+ $sub->setLease(intval($lease));
+ }
+ }
- $data = array('sub' => $sub, 'mode' => 'subscribe', 'token' => $token);
- $qm = QueueManager::get();
- $qm->enqueue($data, 'hubverify');
-
- header('HTTP/1.1 202 Accepted');
- common_log(LOG_DEBUG, __METHOD__ . ': done');
+ if (!common_config('queue', 'enabled')) {
+ // Won't be able to background it.
+ $verify = 'sync';
+ }
+ if ($verify == 'async') {
+ $sub->scheduleVerify($mode, $token);
+ header('HTTP/1.1 202 Accepted');
+ } else {
+ $sub->verify($mode, $token);
+ header('HTTP/1.1 204 No Content');
+ }
}
/**
- * Process a PuSH feed unsubscription request.
- *
- * HTTP return codes:
- * 202 Accepted - request saved and awaiting verification
- * 204 No Content - already subscribed
- * 400 Bad Request - invalid params or rejected feed
+ * Check whether the given URL represents one of our canonical
+ * user or group Atom feeds.
*
- * @fixme background this
+ * @param string $feed URL
+ * @return boolean true if it matches
*/
- function unsubscribe()
+ function recognizedFeed($feed)
{
- $feed = $this->argUrl('hub.topic');
- $callback = $this->argUrl('hub.callback');
- $sub = $this->getSub($feed, $callback);
-
- if ($sub) {
- $token = $this->arg('hub.verify_token', null);
- if ($sub->verify('unsubscribe', $token)) {
- $sub->delete();
- common_log(LOG_INFO, "PuSH unsubscribed $feed for $callback");
- } else {
- throw new ServerException("Failed PuSH unsubscription: verification failed! $feed for $callback");
+ $matches = array();
+ if (preg_match('!/(\d+)\.atom$!', $feed, $matches)) {
+ $id = $matches[1];
+ $params = array('id' => $id, 'format' => 'atom');
+ $userFeed = common_local_url('ApiTimelineUser', $params);
+ $groupFeed = common_local_url('ApiTimelineGroup', $params);
+
+ if ($feed == $userFeed) {
+ $user = User::staticGet('id', $id);
+ if (!$user) {
+ throw new ClientException("Invalid hub.topic $feed; user doesn't exist.");
+ } else {
+ return true;
+ }
}
- } else {
- throw new ServerException("Failed PuSH unsubscription: not subscribed! $feed for $callback");
+ if ($feed == $groupFeed) {
+ $user = User_group::staticGet('id', $id);
+ if (!$user) {
+ throw new ClientException("Invalid hub.topic $feed; group doesn't exist.");
+ } else {
+ return true;
+ }
+ }
+ common_log(LOG_DEBUG, "Not a user or group feed? $feed $userFeed $groupFeed");
}
+ common_log(LOG_DEBUG, "LOST $feed");
+ return false;
}
/**
* Grab and validate a URL from POST parameters.
- * @throws ServerException for malformed or non-http/https URLs
+ * @throws ClientException for malformed or non-http/https URLs
*/
protected function argUrl($arg)
{
@@ -164,7 +183,7 @@ class PushHubAction extends Action
if (Validate::uri($url, $params)) {
return $url;
} else {
- throw new ServerException("Invalid URL passed for $arg: '$url'", 400);
+ throw new ClientException("Invalid URL passed for $arg: '$url'");
}
}
diff --git a/plugins/OStatus/actions/usersalmon.php b/plugins/OStatus/actions/usersalmon.php
index 12c74798f..c8a16e06f 100644
--- a/plugins/OStatus/actions/usersalmon.php
+++ b/plugins/OStatus/actions/usersalmon.php
@@ -55,6 +55,8 @@ class UsersalmonAction extends SalmonAction
*/
function handlePost()
{
+ common_log(LOG_INFO, "Received post of '{$this->act->object->id}' from '{$this->act->actor->id}'");
+
switch ($this->act->object->type) {
case ActivityObject::ARTICLE:
case ActivityObject::BLOGENTRY:
@@ -80,13 +82,21 @@ class UsersalmonAction extends SalmonAction
throw new ClientException("In reply to a notice not by this user");
}
} else if (!empty($context->attention)) {
- if (!in_array($context->attention, $this->user->uri)) {
+ if (!in_array($this->user->uri, $context->attention)) {
+ common_log(LOG_ERR, "{$this->user->uri} not in attention list (".implode(',', $context->attention).")");
throw new ClientException("To the attention of user(s) not including this one!");
}
} else {
throw new ClientException("Not to anyone in reply to anything!");
}
+ $existing = Notice::staticGet('uri', $this->act->object->id);
+
+ if (!empty($existing)) {
+ common_log(LOG_ERR, "Not saving notice '{$existing->uri}'; already exists.");
+ return;
+ }
+
$this->saveNotice();
}