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 // Outgoing from our internal PuSH hub
82 $qm->connect('hubverify', 'HubVerifyQueueHandler');
83 $qm->connect('hubdistrib', 'HubDistribQueueHandler');
84 $qm->connect('hubout', 'HubOutQueueHandler');
86 // Incoming from a foreign PuSH hub
87 $qm->connect('pushinput', 'PushInputQueueHandler');
92 * Put saved notices into the queue for pubsub distribution.
94 function onStartEnqueueNotice($notice, &$transports)
96 $transports[] = 'hubdistrib';
101 * Set up a PuSH hub link to our internal link for canonical timeline
102 * Atom feeds for users and groups.
104 function onStartApiAtom($feed)
108 if ($feed instanceof AtomUserNoticeFeed) {
109 $salmonAction = 'usersalmon';
110 $user = $feed->getUser();
112 $profile = $user->getProfile();
113 $feed->setActivitySubject($profile->asActivityNoun('subject'));
114 } else if ($feed instanceof AtomGroupNoticeFeed) {
115 $salmonAction = 'groupsalmon';
116 $group = $feed->getGroup();
118 $feed->setActivitySubject($group->asActivitySubject());
124 $hub = common_config('ostatus', 'hub');
126 // Updates will be handled through our internal PuSH hub.
127 $hub = common_local_url('pushhub');
129 $feed->addLink($hub, array('rel' => 'hub'));
131 // Also, we'll add in the salmon link
132 $salmon = common_local_url($salmonAction, array('id' => $id));
133 $feed->addLink($salmon, array('rel' => 'salmon'));
140 * Automatically load the actions and libraries used by the plugin
142 * @param Class $cls the class
144 * @return boolean hook return
147 function onAutoload($cls)
149 $base = dirname(__FILE__);
150 $lower = strtolower($cls);
151 $map = array('activityverb' => 'activity',
152 'activityobject' => 'activity',
153 'activityutils' => 'activity');
154 if (isset($map[$lower])) {
155 $lower = $map[$lower];
157 $files = array("$base/classes/$cls.php",
158 "$base/lib/$lower.php");
159 if (substr($lower, -6) == 'action') {
160 $files[] = "$base/actions/" . substr($lower, 0, -6) . ".php";
162 foreach ($files as $file) {
163 if (file_exists($file)) {
172 * Add in an OStatus subscribe button
174 function onStartProfileRemoteSubscribe($output, $profile)
176 $cur = common_current_user();
179 // Add an OStatus subscribe
180 $output->elementStart('li', 'entity_subscribe');
181 $url = common_local_url('ostatusinit',
182 array('nickname' => $profile->nickname));
183 $output->element('a', array('href' => $url,
184 'class' => 'entity_remote_subscribe'),
187 $output->elementEnd('li');
194 * Check if we've got remote replies to send via Salmon.
196 * @fixme push webfinger lookup & sending to a background queue
197 * @fixme also detect short-form name for remote subscribees where not ambiguous
200 function onEndNoticeSave($notice)
202 $mentioned = $notice->getReplies();
204 foreach ($mentioned as $profile) {
206 $oprofile = Ostatus_profile::staticGet('profile_id', $profile->id);
208 if (!empty($oprofile) && !empty($oprofile->salmonuri)) {
210 // FIXME: this needs to go out in a queue handler
212 $xml = '<?xml version="1.0" encoding="UTF-8" ?>';
213 $xml .= $notice->asAtomEntry();
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('feedsub', FeedSub::schemaDef());
311 $schema->ensureTable('hubsub', HubSub::schemaDef());
315 function onEndShowStatusNetStyles($action) {
316 $action->cssLink(common_path('plugins/OStatus/theme/base/css/ostatus.css'));
320 function onEndShowStatusNetScripts($action) {
321 $action->script(common_path('plugins/OStatus/js/ostatus.js'));
326 * Override the "from ostatus" bit in notice lists to link to the
327 * original post and show the domain it came from.
329 * @param Notice in $notice
330 * @param string out &$name
331 * @param string out &$url
332 * @param string out &$title
333 * @return mixed hook return code
335 function onStartNoticeSourceLink($notice, &$name, &$url, &$title)
337 if ($notice->source == 'ostatus') {
338 $bits = parse_url($notice->uri);
339 $domain = $bits['host'];
343 $title = sprintf(_m("Sent from %s via OStatus"), $domain);
349 * Send incoming PuSH feeds for OStatus endpoints in for processing.
351 * @param FeedSub $feedsub
352 * @param DOMDocument $feed
353 * @return mixed hook return code
355 function onStartFeedSubReceive($feedsub, $feed)
357 $oprofile = Ostatus_profile::staticGet('feeduri', $feedsub->uri);
359 $oprofile->processFeed($feed);
361 common_log(LOG_DEBUG, "No ostatus profile for incoming feed $feedsub->uri");
365 function onEndSubscribe($subscriber, $other)
367 $user = User::staticGet('id', $subscriber->id);
373 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
375 if (empty($oprofile)) {
379 $act = new Activity();
381 $act->verb = ActivityVerb::FOLLOW;
383 $act->id = TagURI::mint('follow:%d:%d:%s',
386 common_date_iso8601(time()));
389 $act->title = _("Follow");
390 $act->content = sprintf(_("%s is now following %s."),
391 $subscriber->getBestName(),
392 $other->getBestName());
394 $act->actor = ActivityObject::fromProfile($subscriber);
395 $act->object = ActivityObject::fromProfile($other);
397 $oprofile->notifyActivity($act);
403 * Notify remote users when their notices get favorited.
405 * @param Profile or User $profile of local user doing the faving
406 * @param Notice $notice being favored
407 * @return hook return value
410 function onEndFavorNotice(Profile $profile, Notice $notice)
412 $user = User::staticGet('id', $profile->id);
418 $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
420 if (empty($oprofile)) {
424 $act = new Activity();
426 $act->verb = ActivityVerb::FAVORITE;
427 $act->id = TagURI::mint('favor:%d:%d:%s',
430 common_date_iso8601(time()));
433 $act->title = _("Favor");
434 $act->content = sprintf(_("%s marked notice %s as a favorite."),
435 $profile->getBestName(),
438 $act->actor = ActivityObject::fromProfile($profile);
439 $act->object = ActivityObject::fromNotice($notice);
441 $oprofile->notifyActivity($act);
447 * Notify remote users when their notices get de-favorited.
449 * @param Profile $profile Profile person doing the de-faving
450 * @param Notice $notice Notice being favored
452 * @return hook return value
455 function onEndDisfavorNotice(Profile $profile, Notice $notice)
457 $user = User::staticGet('id', $profile->id);
463 $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
465 if (empty($oprofile)) {
469 $act = new Activity();
471 $act->verb = ActivityVerb::UNFAVORITE;
472 $act->id = TagURI::mint('disfavor:%d:%d:%s',
475 common_date_iso8601(time()));
477 $act->title = _("Disfavor");
478 $act->content = sprintf(_("%s marked notice %s as no longer a favorite."),
479 $profile->getBestName(),
482 $act->actor = ActivityObject::fromProfile($profile);
483 $act->object = ActivityObject::fromNotice($notice);
485 $oprofile->notifyActivity($act);