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 * Add the feed settings page to the Connect Settings menu
142 * @param Action &$action The calling page
144 * @return boolean hook return
146 function onEndConnectSettingsNav(&$action)
148 $action_name = $action->trimmed('action');
150 $action->menuItem(common_local_url('feedsubsettings'),
152 _m('Feed subscription options'),
153 $action_name === 'feedsubsettings');
159 * Automatically load the actions and libraries used by the plugin
161 * @param Class $cls the class
163 * @return boolean hook return
166 function onAutoload($cls)
168 $base = dirname(__FILE__);
169 $lower = strtolower($cls);
170 $map = array('activityverb' => 'activity',
171 'activityobject' => 'activity',
172 'activityutils' => 'activity');
173 if (isset($map[$lower])) {
174 $lower = $map[$lower];
176 $files = array("$base/classes/$cls.php",
177 "$base/lib/$lower.php");
178 if (substr($lower, -6) == 'action') {
179 $files[] = "$base/actions/" . substr($lower, 0, -6) . ".php";
181 foreach ($files as $file) {
182 if (file_exists($file)) {
191 * Add in an OStatus subscribe button
193 function onStartProfileRemoteSubscribe($output, $profile)
195 $cur = common_current_user();
198 // Add an OStatus subscribe
199 $output->elementStart('li', 'entity_subscribe');
200 $url = common_local_url('ostatusinit',
201 array('nickname' => $profile->nickname));
202 $output->element('a', array('href' => $url,
203 'class' => 'entity_remote_subscribe'),
206 $output->elementEnd('li');
213 * Check if we've got remote replies to send via Salmon.
215 * @fixme push webfinger lookup & sending to a background queue
216 * @fixme also detect short-form name for remote subscribees where not ambiguous
218 function onEndNoticeSave($notice)
220 $count = preg_match_all('/(\w+\.)*\w+@(\w+\.)*\w+(\w+\-\w+)*\.\w+/', $notice->content, $matches);
222 foreach ($matches[0] as $webfinger) {
224 // FIXME: look up locally first
226 // Check to see if we've got an actual webfinger
231 $result = $w->lookup($webfinger);
232 if (empty($result)) {
236 foreach ($result->links as $link) {
237 if ($link['rel'] == 'salmon') {
238 $endpoint_uri = $link['href'];
242 if (empty($endpoint_uri)) {
246 // FIXME: this needs to go out in a queue handler
248 $xml = '<?xml version="1.0" encoding="UTF-8" ?>';
249 $xml .= $notice->asAtomEntry();
251 $salmon = new Salmon();
252 $salmon->post($endpoint_uri, $xml);
258 * Notify remote server and garbage collect unused feeds on unsubscribe.
259 * @fixme send these operations to background queues
262 * @param Profile $other
263 * @return hook return value
265 function onEndUnsubscribe($profile, $other)
267 $user = User::staticGet('id', $profile->id);
273 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
275 if (empty($oprofile)) {
279 // Drop the PuSH subscription if there are no other subscribers.
281 if ($other->subscriberCount() == 0) {
282 common_log(LOG_INFO, "Unsubscribing from now-unused feed $oprofile->feeduri");
283 $oprofile->unsubscribe();
286 $act = new Activity();
288 $act->verb = ActivityVerb::UNFOLLOW;
290 $act->id = TagURI::mint('unfollow:%d:%d:%s',
293 common_date_iso8601(time()));
296 $act->title = _("Unfollow");
297 $act->content = sprintf(_("%s stopped following %s."),
298 $profile->getBestName(),
299 $other->getBestName());
301 $act->actor = ActivityObject::fromProfile($profile);
302 $act->object = ActivityObject::fromProfile($other);
304 $oprofile->notifyActivity($act);
310 * Make sure necessary tables are filled out.
312 function onCheckSchema() {
313 $schema = Schema::get();
314 $schema->ensureTable('ostatus_profile', Ostatus_profile::schemaDef());
315 $schema->ensureTable('feedsub', FeedSub::schemaDef());
316 $schema->ensureTable('hubsub', HubSub::schemaDef());
320 function onEndShowStatusNetStyles($action) {
321 $action->cssLink(common_path('plugins/OStatus/theme/base/css/ostatus.css'));
325 function onEndShowStatusNetScripts($action) {
326 $action->script(common_path('plugins/OStatus/js/ostatus.js'));
331 * Override the "from ostatus" bit in notice lists to link to the
332 * original post and show the domain it came from.
334 * @param Notice in $notice
335 * @param string out &$name
336 * @param string out &$url
337 * @param string out &$title
338 * @return mixed hook return code
340 function onStartNoticeSourceLink($notice, &$name, &$url, &$title)
342 if ($notice->source == 'ostatus') {
343 $bits = parse_url($notice->uri);
344 $domain = $bits['host'];
348 $title = sprintf(_m("Sent from %s via OStatus"), $domain);
354 * Send incoming PuSH feeds for OStatus endpoints in for processing.
356 * @param FeedSub $feedsub
357 * @param DOMDocument $feed
358 * @return mixed hook return code
360 function onStartFeedSubReceive($feedsub, $feed)
362 $oprofile = Ostatus_profile::staticGet('feeduri', $feedsub->uri);
364 $oprofile->processFeed($feed);
366 common_log(LOG_DEBUG, "No ostatus profile for incoming feed $feedsub->uri");
370 function onEndSubscribe($subscriber, $other)
372 $user = User::staticGet('id', $subscriber->id);
378 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
380 if (empty($oprofile)) {
384 $act = new Activity();
386 $act->verb = ActivityVerb::FOLLOW;
388 $act->id = TagURI::mint('follow:%d:%d:%s',
391 common_date_iso8601(time()));
394 $act->title = _("Follow");
395 $act->content = sprintf(_("%s is now following %s."),
396 $subscriber->getBestName(),
397 $other->getBestName());
399 $act->actor = ActivityObject::fromProfile($subscriber);
400 $act->object = ActivityObject::fromProfile($other);
402 $oprofile->notifyActivity($act);
408 * Notify remote users when their notices get favorited.
410 * @param Profile or User $profile of local user doing the faving
411 * @param Notice $notice being favored
412 * @return hook return value
415 function onEndFavorNotice(Profile $profile, Notice $notice)
417 $user = User::staticGet('id', $profile->id);
423 $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
425 if (empty($oprofile)) {
429 $act = new Activity();
431 $act->verb = ActivityVerb::FAVORITE;
432 $act->id = TagURI::mint('favor:%d:%d:%s',
435 common_date_iso8601(time()));
438 $act->title = _("Favor");
439 $act->content = sprintf(_("%s marked notice %s as a favorite."),
440 $profile->getBestName(),
443 $act->actor = ActivityObject::fromProfile($profile);
444 $act->object = ActivityObject::fromNotice($notice);
446 $oprofile->notifyActivity($act);
452 * Notify remote users when their notices get de-favorited.
454 * @param Profile $profile Profile person doing the de-faving
455 * @param Notice $notice Notice being favored
457 * @return hook return value
460 function onEndDisfavorNotice(Profile $profile, Notice $notice)
462 $user = User::staticGet('id', $profile->id);
468 $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
470 if (empty($oprofile)) {
474 $act = new Activity();
476 $act->verb = ActivityVerb::UNFAVORITE;
477 $act->id = TagURI::mint('disfavor:%d:%d:%s',
480 common_date_iso8601(time()));
482 $act->title = _("Disfavor");
483 $act->content = sprintf(_("%s marked notice %s as no longer a favorite."),
484 $profile->getBestName(),
487 $act->actor = ActivityObject::fromProfile($profile);
488 $act->object = ActivityObject::fromNotice($notice);
490 $oprofile->notifyActivity($act);