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]+'));
63 $m->connect('main/salmon/user/:id',
64 array('action' => 'usersalmon'),
65 array('id' => '[0-9]+'));
66 $m->connect('main/salmon/group/:id',
67 array('action' => 'groupsalmon'),
68 array('id' => '[0-9]+'));
73 * Set up queue handlers for outgoing hub pushes
74 * @param QueueManager $qm
75 * @return boolean hook return
77 function onEndInitializeQueueManager(QueueManager $qm)
79 // Outgoing from our internal PuSH hub
80 $qm->connect('hubverify', 'HubVerifyQueueHandler');
81 $qm->connect('hubdistrib', 'HubDistribQueueHandler');
82 $qm->connect('hubout', 'HubOutQueueHandler');
84 // Incoming from a foreign PuSH hub
85 $qm->connect('pushinput', 'PushInputQueueHandler');
90 * Put saved notices into the queue for pubsub distribution.
92 function onStartEnqueueNotice($notice, &$transports)
94 $transports[] = 'hubdistrib';
99 * Set up a PuSH hub link to our internal link for canonical timeline
100 * Atom feeds for users and groups.
102 function onStartApiAtom($feed)
106 if ($feed instanceof AtomUserNoticeFeed) {
107 $salmonAction = 'usersalmon';
108 $user = $feed->getUser();
110 $profile = $user->getProfile();
111 $feed->setActivitySubject($profile->asActivityNoun('subject'));
112 } else if ($feed instanceof AtomGroupNoticeFeed) {
113 $salmonAction = 'groupsalmon';
114 $group = $feed->getGroup();
116 $feed->setActivitySubject($group->asActivitySubject());
122 $hub = common_config('ostatus', 'hub');
124 // Updates will be handled through our internal PuSH hub.
125 $hub = common_local_url('pushhub');
127 $feed->addLink($hub, array('rel' => 'hub'));
129 // Also, we'll add in the salmon link
130 $salmon = common_local_url($salmonAction, array('id' => $id));
131 $feed->addLink($salmon, array('rel' => 'salmon'));
138 * Automatically load the actions and libraries used by the plugin
140 * @param Class $cls the class
142 * @return boolean hook return
145 function onAutoload($cls)
147 $base = dirname(__FILE__);
148 $lower = strtolower($cls);
149 $map = array('activityverb' => 'activity',
150 'activityobject' => 'activity',
151 'activityutils' => 'activity');
152 if (isset($map[$lower])) {
153 $lower = $map[$lower];
155 $files = array("$base/classes/$cls.php",
156 "$base/lib/$lower.php");
157 if (substr($lower, -6) == 'action') {
158 $files[] = "$base/actions/" . substr($lower, 0, -6) . ".php";
160 foreach ($files as $file) {
161 if (file_exists($file)) {
170 * Add in an OStatus subscribe button
172 function onStartProfileRemoteSubscribe($output, $profile)
174 $cur = common_current_user();
177 // Add an OStatus subscribe
178 $output->elementStart('li', 'entity_subscribe');
179 $url = common_local_url('ostatusinit',
180 array('nickname' => $profile->nickname));
181 $output->element('a', array('href' => $url,
182 'class' => 'entity_remote_subscribe'),
185 $output->elementEnd('li');
192 * Check if we've got remote replies to send via Salmon.
194 * @fixme push webfinger lookup & sending to a background queue
195 * @fixme also detect short-form name for remote subscribees where not ambiguous
198 function onEndNoticeSave($notice)
200 $mentioned = $notice->getReplies();
202 foreach ($mentioned as $profile_id) {
204 $oprofile = Ostatus_profile::staticGet('profile_id', $profile_id);
206 if (!empty($oprofile) && !empty($oprofile->salmonuri)) {
208 common_log(LOG_INFO, "Sending notice '{$notice->uri}' to remote profile '{$oprofile->uri}'.");
210 // FIXME: this needs to go out in a queue handler
212 $xml = '<?xml version="1.0" encoding="UTF-8" ?>';
213 $xml .= $notice->asAtomEntry(true, true);
215 $salmon = new Salmon();
216 $salmon->post($oprofile->salmonuri, $xml);
225 function onEndFindMentions($sender, $text, &$mentions)
227 preg_match_all('/(?:^|\s+)@((?:\w+\.)*\w+@(?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+)/',
230 PREG_OFFSET_CAPTURE);
232 foreach ($wmatches[1] as $wmatch) {
234 $webfinger = $wmatch[0];
236 $oprofile = Ostatus_profile::ensureWebfinger($webfinger);
238 if (!empty($oprofile)) {
240 $profile = $oprofile->localProfile();
242 $mentions[] = array('mentioned' => array($profile),
243 'text' => $wmatch[0],
244 'position' => $wmatch[1],
245 'url' => $profile->profileurl);
253 * Notify remote server and garbage collect unused feeds on unsubscribe.
254 * @fixme send these operations to background queues
257 * @param Profile $other
258 * @return hook return value
260 function onEndUnsubscribe($profile, $other)
262 $user = User::staticGet('id', $profile->id);
268 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
270 if (empty($oprofile)) {
274 // Drop the PuSH subscription if there are no other subscribers.
276 if ($other->subscriberCount() == 0) {
277 common_log(LOG_INFO, "Unsubscribing from now-unused feed $oprofile->feeduri");
278 $oprofile->unsubscribe();
281 $act = new Activity();
283 $act->verb = ActivityVerb::UNFOLLOW;
285 $act->id = TagURI::mint('unfollow:%d:%d:%s',
288 common_date_iso8601(time()));
291 $act->title = _("Unfollow");
292 $act->content = sprintf(_("%s stopped following %s."),
293 $profile->getBestName(),
294 $other->getBestName());
296 $act->actor = ActivityObject::fromProfile($profile);
297 $act->object = ActivityObject::fromProfile($other);
299 $oprofile->notifyActivity($act);
305 * Make sure necessary tables are filled out.
307 function onCheckSchema() {
308 $schema = Schema::get();
309 $schema->ensureTable('ostatus_profile', Ostatus_profile::schemaDef());
310 $schema->ensureTable('ostatus_source', Ostatus_source::schemaDef());
311 $schema->ensureTable('feedsub', FeedSub::schemaDef());
312 $schema->ensureTable('hubsub', HubSub::schemaDef());
316 function onEndShowStatusNetStyles($action) {
317 $action->cssLink(common_path('plugins/OStatus/theme/base/css/ostatus.css'));
321 function onEndShowStatusNetScripts($action) {
322 $action->script(common_path('plugins/OStatus/js/ostatus.js'));
327 * Override the "from ostatus" bit in notice lists to link to the
328 * original post and show the domain it came from.
330 * @param Notice in $notice
331 * @param string out &$name
332 * @param string out &$url
333 * @param string out &$title
334 * @return mixed hook return code
336 function onStartNoticeSourceLink($notice, &$name, &$url, &$title)
338 if ($notice->source == 'ostatus') {
339 $bits = parse_url($notice->uri);
340 $domain = $bits['host'];
344 $title = sprintf(_m("Sent from %s via OStatus"), $domain);
350 * Send incoming PuSH feeds for OStatus endpoints in for processing.
352 * @param FeedSub $feedsub
353 * @param DOMDocument $feed
354 * @return mixed hook return code
356 function onStartFeedSubReceive($feedsub, $feed)
358 $oprofile = Ostatus_profile::staticGet('feeduri', $feedsub->uri);
360 $oprofile->processFeed($feed);
362 common_log(LOG_DEBUG, "No ostatus profile for incoming feed $feedsub->uri");
366 function onEndSubscribe($subscriber, $other)
368 $user = User::staticGet('id', $subscriber->id);
374 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
376 if (empty($oprofile)) {
380 $act = new Activity();
382 $act->verb = ActivityVerb::FOLLOW;
384 $act->id = TagURI::mint('follow:%d:%d:%s',
387 common_date_iso8601(time()));
390 $act->title = _("Follow");
391 $act->content = sprintf(_("%s is now following %s."),
392 $subscriber->getBestName(),
393 $other->getBestName());
395 $act->actor = ActivityObject::fromProfile($subscriber);
396 $act->object = ActivityObject::fromProfile($other);
398 $oprofile->notifyActivity($act);
404 * Notify remote users when their notices get favorited.
406 * @param Profile or User $profile of local user doing the faving
407 * @param Notice $notice being favored
408 * @return hook return value
411 function onEndFavorNotice(Profile $profile, Notice $notice)
413 $user = User::staticGet('id', $profile->id);
419 $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
421 if (empty($oprofile)) {
425 $act = new Activity();
427 $act->verb = ActivityVerb::FAVORITE;
428 $act->id = TagURI::mint('favor:%d:%d:%s',
431 common_date_iso8601(time()));
434 $act->title = _("Favor");
435 $act->content = sprintf(_("%s marked notice %s as a favorite."),
436 $profile->getBestName(),
439 $act->actor = ActivityObject::fromProfile($profile);
440 $act->object = ActivityObject::fromNotice($notice);
442 $oprofile->notifyActivity($act);
448 * Notify remote users when their notices get de-favorited.
450 * @param Profile $profile Profile person doing the de-faving
451 * @param Notice $notice Notice being favored
453 * @return hook return value
456 function onEndDisfavorNotice(Profile $profile, Notice $notice)
458 $user = User::staticGet('id', $profile->id);
464 $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
466 if (empty($oprofile)) {
470 $act = new Activity();
472 $act->verb = ActivityVerb::UNFAVORITE;
473 $act->id = TagURI::mint('disfavor:%d:%d:%s',
476 common_date_iso8601(time()));
478 $act->title = _("Disfavor");
479 $act->content = sprintf(_("%s marked notice %s as no longer a favorite."),
480 $profile->getBestName(),
483 $act->actor = ActivityObject::fromProfile($profile);
484 $act->object = ActivityObject::fromNotice($notice);
486 $oprofile->notifyActivity($act);
491 function onStartGetProfileUri($profile, &$uri)
493 $oprofile = Ostatus_profile::staticGet('profile_id', $profile->id);
494 if (!empty($oprofile)) {
495 $uri = $oprofile->uri;
501 function onStartShowSubscriptionsContent($action)
503 $user = common_current_user();
504 if ($user && ($user->id == $action->profile->id)) {
505 $action->elementStart('div', 'entity_actions');
506 $action->elementStart('p', array('id' => 'entity_remote_subscribe',
507 'class' => 'entity_subscribe'));
508 $action->element('a', array('href' => common_local_url('ostatussub'),
509 'class' => 'entity_remote_subscribe')
510 , _m('Subscribe to remote user'));
511 $action->elementEnd('p');
512 $action->elementEnd('div');