3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009-2010, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @package OStatusPlugin
22 * @maintainer Brion Vibber <brion@status.net>
25 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
27 class FeedSubException extends Exception
31 class OStatusPlugin extends Plugin
34 * Hook for RouterInitialized event.
36 * @param Net_URL_Mapper $m path-to-action mapper
37 * @return boolean hook return
39 function onRouterInitialized($m)
42 $m->connect('.well-known/host-meta',
43 array('action' => 'hostmeta'));
44 $m->connect('main/webfinger',
45 array('action' => 'webfinger'));
46 $m->connect('main/ostatus',
47 array('action' => 'ostatusinit'));
48 $m->connect('main/ostatus?nickname=:nickname',
49 array('action' => 'ostatusinit'), array('nickname' => '[A-Za-z0-9_-]+'));
50 $m->connect('main/ostatussub',
51 array('action' => 'ostatussub'));
52 $m->connect('main/ostatussub',
53 array('action' => 'ostatussub'), array('feed' => '[A-Za-z0-9\.\/\:]+'));
56 $m->connect('main/push/hub', array('action' => 'pushhub'));
58 $m->connect('main/push/callback/:feed',
59 array('action' => 'pushcallback'),
60 array('feed' => '[0-9]+'));
61 $m->connect('settings/feedsub',
62 array('action' => 'feedsubsettings'));
65 $m->connect('main/salmon/user/:id',
66 array('action' => 'usersalmon'),
67 array('id' => '[0-9]+'));
68 $m->connect('main/salmon/group/:id',
69 array('action' => 'groupsalmon'),
70 array('id' => '[0-9]+'));
75 * Set up queue handlers for outgoing hub pushes
76 * @param QueueManager $qm
77 * @return boolean hook return
79 function onEndInitializeQueueManager(QueueManager $qm)
81 $qm->connect('hubverify', 'HubVerifyQueueHandler');
82 $qm->connect('hubdistrib', 'HubDistribQueueHandler');
83 $qm->connect('hubout', 'HubOutQueueHandler');
88 * Put saved notices into the queue for pubsub distribution.
90 function onStartEnqueueNotice($notice, &$transports)
92 $transports[] = 'hubdistrib';
97 * Set up a PuSH hub link to our internal link for canonical timeline
98 * Atom feeds for users and groups.
100 function onStartApiAtom($feed)
104 if ($feed instanceof AtomUserNoticeFeed) {
105 $salmonAction = 'salmon';
106 $id = $feed->getUser()->id;
107 } else if ($feed instanceof AtomGroupNoticeFeed) {
108 $salmonAction = 'salmongroup';
109 $id = $feed->getGroup()->id;
115 $hub = common_config('ostatus', 'hub');
117 // Updates will be handled through our internal PuSH hub.
118 $hub = common_local_url('pushhub');
120 $feed->addLink($hub, array('rel' => 'hub'));
122 // Also, we'll add in the salmon link
123 $salmon = common_local_url($salmonAction, array('id' => $id));
124 $feed->addLink($salmon, array('rel' => 'salmon'));
129 * Add the feed settings page to the Connect Settings menu
131 * @param Action &$action The calling page
133 * @return boolean hook return
135 function onEndConnectSettingsNav(&$action)
137 $action_name = $action->trimmed('action');
139 $action->menuItem(common_local_url('feedsubsettings'),
141 _m('Feed subscription options'),
142 $action_name === 'feedsubsettings');
148 * Automatically load the actions and libraries used by the plugin
150 * @param Class $cls the class
152 * @return boolean hook return
155 function onAutoload($cls)
157 $base = dirname(__FILE__);
158 $lower = strtolower($cls);
159 $map = array('activityverb' => 'activity',
160 'activityobject' => 'activity',
161 'activityutils' => 'activity');
162 if (isset($map[$lower])) {
163 $lower = $map[$lower];
165 $files = array("$base/classes/$cls.php",
166 "$base/lib/$lower.php");
167 if (substr($lower, -6) == 'action') {
168 $files[] = "$base/actions/" . substr($lower, 0, -6) . ".php";
170 foreach ($files as $file) {
171 if (file_exists($file)) {
180 * Add in an OStatus subscribe button
182 function onStartProfileRemoteSubscribe($output, $profile)
184 $cur = common_current_user();
187 // Add an OStatus subscribe
188 $output->elementStart('li', 'entity_subscribe');
189 $url = common_local_url('ostatusinit',
190 array('nickname' => $profile->nickname));
191 $output->element('a', array('href' => $url,
192 'class' => 'entity_remote_subscribe'),
195 $output->elementEnd('li');
202 * Check if we've got remote replies to send via Salmon.
204 * @fixme push webfinger lookup & sending to a background queue
205 * @fixme also detect short-form name for remote subscribees where not ambiguous
207 function onEndNoticeSave($notice)
209 $count = preg_match_all('/(\w+\.)*\w+@(\w+\.)*\w+(\w+\-\w+)*\.\w+/', $notice->content, $matches);
211 foreach ($matches[0] as $webfinger) {
213 // FIXME: look up locally first
215 // Check to see if we've got an actual webfinger
220 $result = $w->lookup($webfinger);
221 if (empty($result)) {
225 foreach ($result->links as $link) {
226 if ($link['rel'] == 'salmon') {
227 $endpoint_uri = $link['href'];
231 if (empty($endpoint_uri)) {
235 // FIXME: this needs to go out in a queue handler
237 $xml = '<?xml version="1.0" encoding="UTF-8" ?>';
238 $xml .= $notice->asAtomEntry();
240 $salmon = new Salmon();
241 $salmon->post($endpoint_uri, $xml);
247 * Notify remote server and garbage collect unused feeds on unsubscribe.
248 * @fixme send these operations to background queues
251 * @param Profile $other
252 * @return hook return value
254 function onEndUnsubscribe($profile, $other)
256 $user = User::staticGet('id', $profile->id);
262 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
264 if (empty($oprofile)) {
268 // Drop the PuSH subscription if there are no other subscribers.
270 if ($other->subscriberCount() == 0) {
271 common_log(LOG_INFO, "Unsubscribing from now-unused feed $oprofile->feeduri");
272 $oprofile->unsubscribe();
275 $act = new Activity();
277 $act->verb = ActivityVerb::UNFOLLOW;
279 $act->id = TagURI::mint('unfollow:%d:%d:%s',
282 common_date_iso8601(time()));
285 $act->title = _("Unfollow");
286 $act->content = sprintf(_("%s stopped following %s."),
287 $profile->getBestName(),
288 $other->getBestName());
290 $act->actor = ActivityObject::fromProfile($subscriber);
291 $act->object = ActivityObject::fromProfile($other);
293 $oprofile->notifyActivity($act);
299 * Make sure necessary tables are filled out.
301 function onCheckSchema() {
302 $schema = Schema::get();
303 $schema->ensureTable('ostatus_profile', Ostatus_profile::schemaDef());
304 $schema->ensureTable('feedsub', FeedSub::schemaDef());
305 $schema->ensureTable('hubsub', HubSub::schemaDef());
309 function onEndShowStatusNetStyles($action) {
310 $action->cssLink(common_path('plugins/OStatus/theme/base/css/ostatus.css'));
314 function onEndShowStatusNetScripts($action) {
315 $action->script(common_path('plugins/OStatus/js/ostatus.js'));
320 * Override the "from ostatus" bit in notice lists to link to the
321 * original post and show the domain it came from.
323 * @param Notice in $notice
324 * @param string out &$name
325 * @param string out &$url
326 * @param string out &$title
327 * @return mixed hook return code
329 function onStartNoticeSourceLink($notice, &$name, &$url, &$title)
331 if ($notice->source == 'ostatus') {
332 $bits = parse_url($notice->uri);
333 $domain = $bits['host'];
337 $title = sprintf(_m("Sent from %s via OStatus"), $domain);
343 * Send incoming PuSH feeds for OStatus endpoints in for processing.
345 * @param FeedSub $feedsub
346 * @param DOMDocument $feed
347 * @return mixed hook return code
349 function onStartFeedSubReceive($feedsub, $feed)
351 $oprofile = Ostatus_profile::staticGet('feeduri', $feedsub->uri);
353 $oprofile->processFeed($feed);
355 common_log(LOG_DEBUG, "No ostatus profile for incoming feed $feedsub->uri");
359 function onEndSubscribe($subscriber, $other)
361 $user = User::staticGet('id', $subscriber->id);
367 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
369 if (empty($oprofile)) {
373 $act = new Activity();
375 $act->verb = ActivityVerb::FOLLOW;
377 $act->id = TagURI::mint('follow:%d:%d:%s',
380 common_date_iso8601(time()));
383 $act->title = _("Follow");
384 $act->content = sprintf(_("%s is now following %s."),
385 $subscriber->getBestName(),
386 $other->getBestName());
388 $act->actor = ActivityObject::fromProfile($subscriber);
389 $act->object = ActivityObject::fromProfile($other);
391 $oprofile->notifyActivity($act);
397 * Notify remote users when their notices get favorited.
399 * @param Profile or User $profile of local user doing the faving
400 * @param Notice $notice being favored
401 * @return hook return value
404 function onEndFavorNotice(Profile $profile, Notice $notice)
406 $user = User::staticGet('id', $profile->id);
412 $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
414 if (empty($oprofile)) {
418 $act = new Activity();
420 $act->verb = ActivityVerb::FAVORITE;
421 $act->id = TagURI::mint('favor:%d:%d:%s',
424 common_date_iso8601(time()));
427 $act->title = _("Favor");
428 $act->content = sprintf(_("%s marked notice %s as a favorite."),
429 $profile->getBestName(),
432 $act->actor = ActivityObject::fromProfile($profile);
433 $act->object = ActivityObject::fromNotice($notice);
435 $oprofile->notifyActivity($act);
441 * Notify remote users when their notices get de-favorited.
443 * @param Profile or User $profile of local user doing the de-faving
444 * @param Notice $notice being favored
445 * @return hook return value
448 function onEndDisfavorNotice(Profile $profile, Notice $notice)
450 $user = User::staticGet('id', $profile->id);
456 $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
458 if (empty($oprofile)) {
462 $act = new Activity();
464 $act->verb = ActivityVerb::UNFAVORITE;
465 $act->id = TagURI::mint('disfavor:%d:%d:%s',
468 common_date_iso8601(time()));
470 $act->title = _("Disfavor");
471 $act->content = sprintf(_("%s marked notice %s as no longer a favorite."),
472 $profile->getBestName(),
475 $act->actor = ActivityObject::fromProfile($profile);
476 $act->object = ActivityObject::fromNotice($notice);
478 $oprofile->notifyActivity($act);