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 set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/');
29 class FeedSubException extends Exception
33 class OStatusPlugin extends Plugin
36 * Hook for RouterInitialized event.
38 * @param Net_URL_Mapper $m path-to-action mapper
39 * @return boolean hook return
41 function onRouterInitialized($m)
44 $m->connect('.well-known/host-meta',
45 array('action' => 'hostmeta'));
46 $m->connect('main/webfinger',
47 array('action' => 'webfinger'));
48 $m->connect('main/ostatus',
49 array('action' => 'ostatusinit'));
50 $m->connect('main/ostatus?nickname=:nickname',
51 array('action' => 'ostatusinit'), array('nickname' => '[A-Za-z0-9_-]+'));
52 $m->connect('main/ostatussub',
53 array('action' => 'ostatussub'));
54 $m->connect('main/ostatussub',
55 array('action' => 'ostatussub'), array('feed' => '[A-Za-z0-9\.\/\:]+'));
58 $m->connect('main/push/hub', array('action' => 'pushhub'));
60 $m->connect('main/push/callback/:feed',
61 array('action' => 'pushcallback'),
62 array('feed' => '[0-9]+'));
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 // Prepare outgoing distributions after notice save.
82 $qm->connect('ostatus', 'OStatusQueueHandler');
84 // Outgoing from our internal PuSH hub
85 $qm->connect('hubconf', 'HubConfQueueHandler');
86 $qm->connect('hubout', 'HubOutQueueHandler');
88 // Outgoing Salmon replies (when we don't need a return value)
89 $qm->connect('salmon', 'SalmonQueueHandler');
91 // Incoming from a foreign PuSH hub
92 $qm->connect('pushin', 'PushInQueueHandler');
97 * Put saved notices into the queue for pubsub distribution.
99 function onStartEnqueueNotice($notice, &$transports)
101 $transports[] = 'ostatus';
106 * Set up a PuSH hub link to our internal link for canonical timeline
107 * Atom feeds for users and groups.
109 function onStartApiAtom($feed)
113 if ($feed instanceof AtomUserNoticeFeed) {
114 $salmonAction = 'usersalmon';
115 $user = $feed->getUser();
117 $profile = $user->getProfile();
118 $feed->setActivitySubject($profile->asActivityNoun('subject'));
119 } else if ($feed instanceof AtomGroupNoticeFeed) {
120 $salmonAction = 'groupsalmon';
121 $group = $feed->getGroup();
123 $feed->setActivitySubject($group->asActivitySubject());
129 $hub = common_config('ostatus', 'hub');
131 // Updates will be handled through our internal PuSH hub.
132 $hub = common_local_url('pushhub');
134 $feed->addLink($hub, array('rel' => 'hub'));
136 // Also, we'll add in the salmon link
137 $salmon = common_local_url($salmonAction, array('id' => $id));
138 $feed->addLink($salmon, array('rel' => 'salmon'));
145 * Automatically load the actions and libraries used by the plugin
147 * @param Class $cls the class
149 * @return boolean hook return
152 function onAutoload($cls)
154 $base = dirname(__FILE__);
155 $lower = strtolower($cls);
156 $map = array('activityverb' => 'activity',
157 'activityobject' => 'activity',
158 'activityutils' => 'activity');
159 if (isset($map[$lower])) {
160 $lower = $map[$lower];
162 $files = array("$base/classes/$cls.php",
163 "$base/lib/$lower.php");
164 if (substr($lower, -6) == 'action') {
165 $files[] = "$base/actions/" . substr($lower, 0, -6) . ".php";
167 foreach ($files as $file) {
168 if (file_exists($file)) {
177 * Add in an OStatus subscribe button
179 function onStartProfileRemoteSubscribe($output, $profile)
181 $cur = common_current_user();
184 // Add an OStatus subscribe
185 $output->elementStart('li', 'entity_subscribe');
186 $url = common_local_url('ostatusinit',
187 array('nickname' => $profile->nickname));
188 $output->element('a', array('href' => $url,
189 'class' => 'entity_remote_subscribe'),
192 $output->elementEnd('li');
199 * Check if we've got remote replies to send via Salmon.
201 * @fixme push webfinger lookup & sending to a background queue
202 * @fixme also detect short-form name for remote subscribees where not ambiguous
205 function onEndNoticeSave($notice)
213 function onStartFindMentions($sender, $text, &$mentions)
215 preg_match_all('/(?:^|\s+)@((?:\w+\.)*\w+@(?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+)/',
218 PREG_OFFSET_CAPTURE);
220 foreach ($wmatches[1] as $wmatch) {
222 $webfinger = $wmatch[0];
224 $oprofile = Ostatus_profile::ensureWebfinger($webfinger);
226 if (!empty($oprofile)) {
228 $profile = $oprofile->localProfile();
230 $mentions[] = array('mentioned' => array($profile),
231 'text' => $wmatch[0],
232 'position' => $wmatch[1],
233 'url' => $profile->profileurl);
241 * Make sure necessary tables are filled out.
243 function onCheckSchema() {
244 $schema = Schema::get();
245 $schema->ensureTable('ostatus_profile', Ostatus_profile::schemaDef());
246 $schema->ensureTable('ostatus_source', Ostatus_source::schemaDef());
247 $schema->ensureTable('feedsub', FeedSub::schemaDef());
248 $schema->ensureTable('hubsub', HubSub::schemaDef());
249 $schema->ensureTable('magicsig', Magicsig::schemaDef());
253 function onEndShowStatusNetStyles($action) {
254 $action->cssLink(common_path('plugins/OStatus/theme/base/css/ostatus.css'));
258 function onEndShowStatusNetScripts($action) {
259 $action->script(common_path('plugins/OStatus/js/ostatus.js'));
264 * Override the "from ostatus" bit in notice lists to link to the
265 * original post and show the domain it came from.
267 * @param Notice in $notice
268 * @param string out &$name
269 * @param string out &$url
270 * @param string out &$title
271 * @return mixed hook return code
273 function onStartNoticeSourceLink($notice, &$name, &$url, &$title)
275 if ($notice->source == 'ostatus') {
277 $bits = parse_url($notice->url);
278 $domain = $bits['host'];
279 if (substr($domain, 0, 4) == 'www.') {
280 $name = substr($domain, 4);
286 $title = sprintf(_m("Sent from %s via OStatus"), $domain);
293 * Send incoming PuSH feeds for OStatus endpoints in for processing.
295 * @param FeedSub $feedsub
296 * @param DOMDocument $feed
297 * @return mixed hook return code
299 function onStartFeedSubReceive($feedsub, $feed)
301 $oprofile = Ostatus_profile::staticGet('feeduri', $feedsub->uri);
303 $oprofile->processFeed($feed, 'push');
305 common_log(LOG_DEBUG, "No ostatus profile for incoming feed $feedsub->uri");
310 * When about to subscribe to a remote user, start a server-to-server
311 * PuSH subscription if needed. If we can't establish that, abort.
313 * @fixme If something else aborts later, we could end up with a stray
314 * PuSH subscription. This is relatively harmless, though.
316 * @param Profile $subscriber
317 * @param Profile $other
319 * @return hook return code
323 function onStartSubscribe($subscriber, $other)
325 $user = User::staticGet('id', $subscriber->id);
331 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
333 if (empty($oprofile)) {
337 if (!$oprofile->subscribe()) {
338 throw new Exception(_m('Could not set up remote subscription.'));
343 * Having established a remote subscription, send a notification to the
344 * remote OStatus profile's endpoint.
346 * @param Profile $subscriber
347 * @param Profile $other
349 * @return hook return code
353 function onEndSubscribe($subscriber, $other)
355 $user = User::staticGet('id', $subscriber->id);
361 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
363 if (empty($oprofile)) {
367 $act = new Activity();
369 $act->verb = ActivityVerb::FOLLOW;
371 $act->id = TagURI::mint('follow:%d:%d:%s',
374 common_date_iso8601(time()));
377 $act->title = _("Follow");
378 $act->content = sprintf(_("%s is now following %s."),
379 $subscriber->getBestName(),
380 $other->getBestName());
382 $act->actor = ActivityObject::fromProfile($subscriber);
383 $act->object = ActivityObject::fromProfile($other);
385 $oprofile->notifyActivity($act);
391 * Notify remote server and garbage collect unused feeds on unsubscribe.
392 * @fixme send these operations to background queues
395 * @param Profile $other
396 * @return hook return value
398 function onEndUnsubscribe($profile, $other)
400 $user = User::staticGet('id', $profile->id);
406 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
408 if (empty($oprofile)) {
412 // Drop the PuSH subscription if there are no other subscribers.
413 $oprofile->garbageCollect();
415 $act = new Activity();
417 $act->verb = ActivityVerb::UNFOLLOW;
419 $act->id = TagURI::mint('unfollow:%d:%d:%s',
422 common_date_iso8601(time()));
425 $act->title = _("Unfollow");
426 $act->content = sprintf(_("%s stopped following %s."),
427 $profile->getBestName(),
428 $other->getBestName());
430 $act->actor = ActivityObject::fromProfile($profile);
431 $act->object = ActivityObject::fromProfile($other);
433 $oprofile->notifyActivity($act);
439 * When one of our local users tries to join a remote group,
440 * notify the remote server. If the notification is rejected,
443 * @param User_group $group
446 * @return mixed hook return value
449 function onStartJoinGroup($group, $user)
451 $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
453 if (!$oprofile->subscribe()) {
454 throw new Exception(_m('Could not set up remote group membership.'));
457 $member = Profile::staticGet($user->id);
459 $act = new Activity();
460 $act->id = TagURI::mint('join:%d:%d:%s',
463 common_date_iso8601(time()));
465 $act->actor = ActivityObject::fromProfile($member);
466 $act->verb = ActivityVerb::JOIN;
467 $act->object = $oprofile->asActivityObject();
470 $act->title = _m("Join");
471 $act->content = sprintf(_m("%s has joined group %s."),
472 $member->getBestName(),
473 $oprofile->getBestName());
475 if ($oprofile->notifyActivity($act)) {
478 $oprofile->garbageCollect();
479 throw new Exception(_m("Failed joining remote group."));
485 * When one of our local users leaves a remote group, notify the remote
488 * @fixme Might be good to schedule a resend of the leave notification
489 * if it failed due to a transitory error. We've canceled the local
490 * membership already anyway, but if the remote server comes back up
491 * it'll be left with a stray membership record.
493 * @param User_group $group
496 * @return mixed hook return value
499 function onEndLeaveGroup($group, $user)
501 $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
503 // Drop the PuSH subscription if there are no other subscribers.
504 $oprofile->garbageCollect();
507 $member = Profile::staticGet($user->id);
509 $act = new Activity();
510 $act->id = TagURI::mint('leave:%d:%d:%s',
513 common_date_iso8601(time()));
515 $act->actor = ActivityObject::fromProfile($member);
516 $act->verb = ActivityVerb::LEAVE;
517 $act->object = $oprofile->asActivityObject();
520 $act->title = _m("Leave");
521 $act->content = sprintf(_m("%s has left group %s."),
522 $member->getBestName(),
523 $oprofile->getBestName());
525 $oprofile->notifyActivity($act);
530 * Notify remote users when their notices get favorited.
532 * @param Profile or User $profile of local user doing the faving
533 * @param Notice $notice being favored
534 * @return hook return value
537 function onEndFavorNotice(Profile $profile, Notice $notice)
539 $user = User::staticGet('id', $profile->id);
545 $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
547 if (empty($oprofile)) {
551 $act = new Activity();
553 $act->verb = ActivityVerb::FAVORITE;
554 $act->id = TagURI::mint('favor:%d:%d:%s',
557 common_date_iso8601(time()));
560 $act->title = _("Favor");
561 $act->content = sprintf(_("%s marked notice %s as a favorite."),
562 $profile->getBestName(),
565 $act->actor = ActivityObject::fromProfile($profile);
566 $act->object = ActivityObject::fromNotice($notice);
568 $oprofile->notifyActivity($act);
574 * Notify remote users when their notices get de-favorited.
576 * @param Profile $profile Profile person doing the de-faving
577 * @param Notice $notice Notice being favored
579 * @return hook return value
582 function onEndDisfavorNotice(Profile $profile, Notice $notice)
584 $user = User::staticGet('id', $profile->id);
590 $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
592 if (empty($oprofile)) {
596 $act = new Activity();
598 $act->verb = ActivityVerb::UNFAVORITE;
599 $act->id = TagURI::mint('disfavor:%d:%d:%s',
602 common_date_iso8601(time()));
604 $act->title = _("Disfavor");
605 $act->content = sprintf(_("%s marked notice %s as no longer a favorite."),
606 $profile->getBestName(),
609 $act->actor = ActivityObject::fromProfile($profile);
610 $act->object = ActivityObject::fromNotice($notice);
612 $oprofile->notifyActivity($act);
617 function onStartGetProfileUri($profile, &$uri)
619 $oprofile = Ostatus_profile::staticGet('profile_id', $profile->id);
620 if (!empty($oprofile)) {
621 $uri = $oprofile->uri;
627 function onStartUserGroupHomeUrl($group, &$url)
629 return $this->onStartUserGroupPermalink($group, &$url);
632 function onStartUserGroupPermalink($group, &$url)
634 $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
636 // @fixme this should probably be in the user_group table
637 // @fixme this uri not guaranteed to be a profile page
638 $url = $oprofile->uri;
643 function onStartShowSubscriptionsContent($action)
645 $user = common_current_user();
646 if ($user && ($user->id == $action->profile->id)) {
647 $action->elementStart('div', 'entity_actions');
648 $action->elementStart('p', array('id' => 'entity_remote_subscribe',
649 'class' => 'entity_subscribe'));
650 $action->element('a', array('href' => common_local_url('ostatussub'),
651 'class' => 'entity_remote_subscribe')
652 , _m('Subscribe to remote user'));
653 $action->elementEnd('p');
654 $action->elementEnd('div');
661 * Ping remote profiles with updates to this profile.
662 * Salmon pings are queued for background processing.
664 function onEndBroadcastProfile(Profile $profile)
666 $user = User::staticGet('id', $profile->id);
668 // Find foreign accounts I'm subscribed to that support Salmon pings.
670 // @fixme we could run updates through the PuSH feed too,
671 // in which case we can skip Salmon pings to folks who
672 // are also subscribed to me.
673 $sql = "SELECT * FROM ostatus_profile " .
674 "WHERE profile_id IN " .
675 "(SELECT subscribed FROM subscription WHERE subscriber=%d) " .
677 "(SELECT group_id FROM group_member WHERE profile_id=%d)";
678 $oprofile = new Ostatus_profile();
679 $oprofile->query(sprintf($sql, $profile->id, $profile->id));
681 if ($oprofile->N == 0) {
682 common_log(LOG_DEBUG, "No OStatus remote subscribees for $profile->nickname");
686 $act = new Activity();
688 $act->verb = ActivityVerb::UPDATE_PROFILE;
689 $act->id = TagURI::mint('update-profile:%d:%s',
691 common_date_iso8601(time()));
693 $act->title = _m("Profile update");
694 $act->content = sprintf(_m("%s has updated their profile page."),
695 $profile->getBestName());
697 $act->actor = ActivityObject::fromProfile($profile);
698 $act->object = $act->actor;
700 while ($oprofile->fetch()) {
701 $oprofile->notifyDeferred($act);