5 Plugin URI: http://status.net/wiki/Feed_subscription
6 Description: FeedSub allows subscribing to real-time updates from external feeds supporting PubHubSubbub protocol.
8 Author: Brion Vibber <brion@status.net>
9 Author URI: http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2009, StatusNet, Inc.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Affero General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Affero General Public License for more details.
26 * You should have received a copy of the GNU Affero General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 * @package FeedSubPlugin
32 * @maintainer Brion Vibber <brion@status.net>
35 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
37 define('FEEDSUB_SERVICE', 100); // fixme -- avoid hardcoding these?
39 // We bundle the XML_Parse_Feed library...
40 set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib');
42 class FeedSubException extends Exception
46 class OStatusPlugin extends Plugin
49 * Hook for RouterInitialized event.
51 * @param Net_URL_Mapper $m path-to-action mapper
52 * @return boolean hook return
54 function onRouterInitialized($m)
57 $m->connect('.well-known/host-meta',
58 array('action' => 'hostmeta'));
59 $m->connect('main/webfinger',
60 array('action' => 'webfinger'));
61 $m->connect('main/ostatus',
62 array('action' => 'ostatusinit'));
63 $m->connect('main/ostatus?nickname=:nickname',
64 array('action' => 'ostatusinit'), array('nickname' => '[A-Za-z0-9_-]+'));
65 $m->connect('main/ostatussub',
66 array('action' => 'ostatussub'));
67 $m->connect('main/ostatussub',
68 array('action' => 'ostatussub'), array('feed' => '[A-Za-z0-9\.\/\:]+'));
71 $m->connect('main/push/hub', array('action' => 'pushhub'));
73 $m->connect('main/push/callback/:feed',
74 array('action' => 'pushcallback'),
75 array('feed' => '[0-9]+'));
76 $m->connect('settings/feedsub',
77 array('action' => 'feedsubsettings'));
80 $m->connect('main/salmon/user/:id',
81 array('action' => 'salmon'),
82 array('id' => '[0-9]+'));
83 $m->connect('main/salmon/group/:id',
84 array('action' => 'salmongroup'),
85 array('id' => '[0-9]+'));
90 * Set up queue handlers for outgoing hub pushes
91 * @param QueueManager $qm
92 * @return boolean hook return
94 function onEndInitializeQueueManager(QueueManager $qm)
96 $qm->connect('hubverify', 'HubVerifyQueueHandler');
97 $qm->connect('hubdistrib', 'HubDistribQueueHandler');
98 $qm->connect('hubout', 'HubOutQueueHandler');
103 * Put saved notices into the queue for pubsub distribution.
105 function onStartEnqueueNotice($notice, &$transports)
107 $transports[] = 'hubdistrib';
112 * Set up a PuSH hub link to our internal link for canonical timeline
113 * Atom feeds for users and groups.
115 function onStartApiAtom(AtomNoticeFeed $feed)
119 if ($feed instanceof AtomUserNoticeFeed) {
120 $salmonAction = 'salmon';
121 $id = $feed->getUser()->id;
122 } else if ($feed instanceof AtomGroupNoticeFeed) {
123 $salmonAction = 'salmongroup';
124 $id = $feed->getGroup()->id;
130 $hub = common_config('ostatus', 'hub');
132 // Updates will be handled through our internal PuSH hub.
133 $hub = common_local_url('pushhub');
135 $feed->addLink($hub, array('rel' => 'hub'));
137 // Also, we'll add in the salmon link
138 $salmon = common_local_url($salmonAction, array('id' => $id));
139 $feed->addLink($salmon, array('rel' => 'salmon'));
144 * Add the feed settings page to the Connect Settings menu
146 * @param Action &$action The calling page
148 * @return boolean hook return
150 function onEndConnectSettingsNav(&$action)
152 $action_name = $action->trimmed('action');
154 $action->menuItem(common_local_url('feedsubsettings'),
156 _m('Feed subscription options'),
157 $action_name === 'feedsubsettings');
163 * Automatically load the actions and libraries used by the plugin
165 * @param Class $cls the class
167 * @return boolean hook return
170 function onAutoload($cls)
172 $base = dirname(__FILE__);
173 $lower = strtolower($cls);
174 $files = array("$base/classes/$cls.php",
175 "$base/lib/$lower.php");
176 if (substr($lower, -6) == 'action') {
177 $files[] = "$base/actions/" . substr($lower, 0, -6) . ".php";
179 foreach ($files as $file) {
180 if (file_exists($file)) {
189 * Add in an OStatus subscribe button
191 function onStartProfileRemoteSubscribe($output, $profile)
193 $cur = common_current_user();
196 // Add an OStatus subscribe
197 $output->elementStart('li', 'entity_subscribe');
198 $url = common_local_url('ostatusinit',
199 array('nickname' => $profile->nickname));
200 $output->element('a', array('href' => $url,
201 'class' => 'entity_remote_subscribe'),
204 $output->elementEnd('li');
211 * Check if we've got remote replies to send via Salmon.
213 * @fixme push webfinger lookup & sending to a background queue
214 * @fixme also detect short-form name for remote subscribees where not ambiguous
216 function onEndNoticeSave($notice)
218 $count = preg_match_all('/(\w+\.)*\w+@(\w+\.)*\w+(\w+\-\w+)*\.\w+/', $notice->content, $matches);
220 foreach ($matches[0] as $webfinger) {
222 // FIXME: look up locally first
224 // Check to see if we've got an actual webfinger
229 $result = $w->lookup($webfinger);
230 if (empty($result)) {
234 foreach ($result->links as $link) {
235 if ($link['rel'] == 'salmon') {
236 $endpoint_uri = $link['href'];
240 if (empty($endpoint_uri)) {
244 // FIXME: this needs to go out in a queue handler
246 $xml = '<?xml version="1.0" encoding="UTF-8" ?>';
247 $xml .= $notice->asAtomEntry();
249 $salmon = new Salmon();
250 $salmon->post($endpoint_uri, $xml);
256 * Garbage collect unused feeds on unsubscribe
258 function onEndUnsubscribe($user, $other)
260 $profile = Ostatus_profile::staticGet('profile_id', $other->id);
262 $sub = new Subscription();
263 $sub->subscribed = $other->id;
265 if (!$sub->find(true)) {
266 common_log(LOG_INFO, "Unsubscribing from now-unused feed $feed->feeduri on hub $feed->huburi");
267 $profile->unsubscribe();
274 * Make sure necessary tables are filled out.
276 function onCheckSchema() {
277 $schema = Schema::get();
278 $schema->ensureTable('ostatus_profile', Ostatus_profile::schemaDef());
279 $schema->ensureTable('hubsub', HubSub::schemaDef());
283 function onEndShowStatusNetStyles($action) {
284 $action->cssLink(common_path('plugins/OStatus/theme/base/css/ostatus.css'));
288 function onEndShowStatusNetScripts($action) {
289 $action->script(common_path('plugins/OStatus/js/ostatus.js'));