Command input processing now has centralized places for looking up notice, user/profile, and group arguments.

OStatus plugin overrides these to allow using webfinger (user@example.com), profile URL (http://example.com/user) and bare profile URL (example.com/user) as arguments.
This commit is contained in:
Brion Vibber
2010-03-08 14:01:43 -08:00
parent 927a368d0e
commit 80a17387bf
2 changed files with 279 additions and 156 deletions

View File

@@ -321,6 +321,86 @@ class OStatusPlugin extends Plugin
return true;
}
/**
* Allow remote profile references to be used in commands:
* sub update@status.net
* whois evan@identi.ca
* reply http://identi.ca/evan hey what's up
*
* @param Command $command
* @param string $arg
* @param Profile &$profile
* @return hook return code
*/
function onStartCommandGetProfile($command, $arg, &$profile)
{
$oprofile = $this->pullRemoteProfile($arg);
if ($oprofile && !$oprofile->isGroup()) {
$profile = $oprofile->localProfile();
return false;
} else {
return true;
}
}
/**
* Allow remote group references to be used in commands:
* join group+statusnet@identi.ca
* join http://identi.ca/group/statusnet
* drop identi.ca/group/statusnet
*
* @param Command $command
* @param string $arg
* @param User_group &$group
* @return hook return code
*/
function onStartCommandGetGroup($command, $arg, &$group)
{
$oprofile = $this->pullRemoteProfile($arg);
if ($oprofile && $oprofile->isGroup()) {
$group = $oprofile->localGroup();
return false;
} else {
return true;
}
}
protected function pullRemoteProfile($arg)
{
$oprofile = null;
if (preg_match('!^((?:\w+\.)*\w+@(?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+)$!', $arg)) {
// webfinger lookup
try {
return Ostatus_profile::ensureWebfinger($arg);
} catch (Exception $e) {
common_log(LOG_ERR, 'Webfinger lookup failed for ' .
$arg . ': ' . $e->getMessage());
}
}
// Look for profile URLs, with or without scheme:
$urls = array();
if (preg_match('!^https?://((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)$!', $arg)) {
$urls[] = $arg;
}
if (preg_match('!^((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)$!', $arg)) {
$schemes = array('http', 'https');
foreach ($schemes as $scheme) {
$urls[] = "$scheme://$arg";
}
}
foreach ($urls as $url) {
try {
return Ostatus_profile::ensureProfile($url);
} catch (Exception $e) {
common_log(LOG_ERR, 'Profile lookup failed for ' .
$arg . ': ' . $e->getMessage());
}
}
return null;
}
/**
* Make sure necessary tables are filled out.
*/