MessageCommand moved to DirectMessage plugin

This commit is contained in:
Mikael Nordfeldth 2014-11-07 16:23:34 +01:00
parent 6ac2a5b8ff
commit 046d070ad4
4 changed files with 55 additions and 105 deletions

View File

@ -336,7 +336,7 @@ class ApiStatusesUpdateAction extends ApiAuthAction
*/ */
function supported($cmd) function supported($cmd)
{ {
static $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand', static $cmdlist = array('SubCommand', 'UnsubCommand',
'OnCommand', 'OffCommand', 'JoinCommand', 'LeaveCommand'); 'OnCommand', 'OffCommand', 'JoinCommand', 'LeaveCommand');
$supported = null; $supported = null;

View File

@ -523,79 +523,6 @@ class WhoisCommand extends Command
} }
} }
class MessageCommand extends Command
{
var $other = null;
var $text = null;
function __construct($user, $other, $text)
{
parent::__construct($user);
$this->other = $other;
$this->text = $text;
}
function handle($channel)
{
try {
$other = $this->getUser($this->other)->getProfile();
} catch (CommandException $e) {
try {
$profile = $this->getProfile($this->other);
} catch (CommandException $f) {
throw $e;
}
// TRANS: Command exception text shown when trying to send a direct message to a remote user (a user not registered at the current server).
// TRANS: %s is a remote profile.
throw new CommandException(sprintf(_('%s is a remote profile; you can only send direct messages to users on the same server.'), $this->other));
}
$len = mb_strlen($this->text);
if ($len == 0) {
// TRANS: Command exception text shown when trying to send a direct message to another user without content.
$channel->error($this->user, _('No content!'));
return;
}
$this->text = $this->user->shortenLinks($this->text);
if (Message::contentTooLong($this->text)) {
// XXX: i18n. Needs plural support.
// TRANS: Message given if content is too long. %1$sd is used for plural.
// TRANS: %1$d is the maximum number of characters, %2$d is the number of submitted characters.
$channel->error($this->user, sprintf(_m('Message too long - maximum is %1$d character, you sent %2$d.',
'Message too long - maximum is %1$d characters, you sent %2$d.',
Message::maxContent()),
Message::maxContent(), mb_strlen($this->text)));
return;
}
if (!$other instanceof Profile) {
// TRANS: Error text shown when trying to send a direct message to a user that does not exist.
$channel->error($this->user, _('No such user.'));
return;
} else if (!$this->user->mutuallySubscribed($other)) {
// TRANS: Error text shown when trying to send a direct message to a user without a mutual subscription (each user must be subscribed to the other).
$channel->error($this->user, _('You can\'t send a message to this user.'));
return;
} else if ($this->user->id == $other->id) {
// TRANS: Error text shown when trying to send a direct message to self.
$channel->error($this->user, _('Do not send a message to yourself; just say it to yourself quietly instead.'));
return;
}
try {
$message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
$message->notify();
// TRANS: Message given have sent a direct message to another user.
// TRANS: %s is the name of the other user.
$channel->output($this->user, sprintf(_('Direct message to %s sent.'), $this->other));
} catch (Exception $e) {
// TRANS: Error text shown sending a direct message fails with an unknown reason.
$channel->error($this->user, $e->getMessage());
}
}
}
class RepeatCommand extends Command class RepeatCommand extends Command
{ {
var $other = null; var $other = null;

View File

@ -28,7 +28,7 @@ class CommandInterpreter
// XXX: localise // XXX: localise
$text = preg_replace('/\s+/', ' ', trim($text)); $text = preg_replace('/\s+/', ' ', trim($text));
list($cmd, $arg) = $this->split_arg($text); list($cmd, $arg) = self::split_arg($text);
// We try to support all the same commands as Twitter, see // We try to support all the same commands as Twitter, see
// http://getsatisfaction.com/twitter/topics/what_are_the_twitter_commands // http://getsatisfaction.com/twitter/topics/what_are_the_twitter_commands
@ -55,7 +55,7 @@ class CommandInterpreter
break; break;
case 'lose': case 'lose':
if ($arg) { if ($arg) {
list($other, $extra) = $this->split_arg($arg); list($other, $extra) = self::split_arg($arg);
if ($extra) { if ($extra) {
$result = null; $result = null;
} else { } else {
@ -88,7 +88,7 @@ class CommandInterpreter
break; break;
case 'on': case 'on':
if ($arg) { if ($arg) {
list($other, $extra) = $this->split_arg($arg); list($other, $extra) = self::split_arg($arg);
if ($extra) { if ($extra) {
$result = null; $result = null;
} else { } else {
@ -100,7 +100,7 @@ class CommandInterpreter
break; break;
case 'off': case 'off':
if ($arg) { if ($arg) {
list($other, $extra) = $this->split_arg($arg); list($other, $extra) = self::split_arg($arg);
if ($extra) { if ($extra) {
$result = null; $result = null;
} else { } else {
@ -122,7 +122,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
$result = null; $result = null;
} else { } else {
list($other, $extra) = $this->split_arg($arg); list($other, $extra) = self::split_arg($arg);
if ($extra) { if ($extra) {
$result = null; $result = null;
} else { } else {
@ -134,7 +134,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
$result = null; $result = null;
} else { } else {
list($other, $extra) = $this->split_arg($arg); list($other, $extra) = self::split_arg($arg);
if ($extra) { if ($extra) {
$result = null; $result = null;
} else { } else {
@ -147,7 +147,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
$result = null; $result = null;
} else { } else {
list($other, $extra) = $this->split_arg($arg); list($other, $extra) = self::split_arg($arg);
if ($extra) { if ($extra) {
$result = null; $result = null;
} else { } else {
@ -160,7 +160,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
$result = null; $result = null;
} else { } else {
list($other, $extra) = $this->split_arg($arg); list($other, $extra) = self::split_arg($arg);
if ($extra) { if ($extra) {
$result = null; $result = null;
} else { } else {
@ -173,31 +173,19 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
$result = null; $result = null;
} }
list($other, $extra) = $this->split_arg($arg); list($other, $extra) = self::split_arg($arg);
if ($extra) { if ($extra) {
$result = null; $result = null;
} else { } else {
$result = new GetCommand($user, $other); $result = new GetCommand($user, $other);
} }
break; break;
case 'd':
case 'dm':
if (!$arg) {
$result = null;
}
list($other, $extra) = $this->split_arg($arg);
if (!$extra) {
$result = null;
} else {
$result = new MessageCommand($user, $other, $extra);
}
break;
case 'r': case 'r':
case 'reply': case 'reply':
if (!$arg) { if (!$arg) {
$result = null; $result = null;
} }
list($other, $extra) = $this->split_arg($arg); list($other, $extra) = self::split_arg($arg);
if (!$extra) { if (!$extra) {
$result = null; $result = null;
} else { } else {
@ -211,7 +199,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
$result = null; $result = null;
} else { } else {
list($other, $extra) = $this->split_arg($arg); list($other, $extra) = self::split_arg($arg);
if ($extra) { if ($extra) {
$result = null; $result = null;
} else { } else {
@ -223,7 +211,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
$result = null; $result = null;
} else { } else {
list($other, $extra) = $this->split_arg($arg); list($other, $extra) = self::split_arg($arg);
if ($extra) { if ($extra) {
$result = null; $result = null;
} else { } else {
@ -235,7 +223,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
$result = null; $result = null;
} else { } else {
list($other, $extra) = $this->split_arg($arg); list($other, $extra) = self::split_arg($arg);
if ($extra) { if ($extra) {
$result = null; $result = null;
} else { } else {
@ -254,7 +242,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
$result = null; $result = null;
} else { } else {
list($other, $extra) = $this->split_arg($arg); list($other, $extra) = self::split_arg($arg);
if ($extra) { if ($extra) {
$result = null; $result = null;
} else { } else {
@ -268,7 +256,7 @@ class CommandInterpreter
$result = null; $result = null;
break; break;
} }
list($other, $tags) = $this->split_arg($arg); list($other, $tags) = self::split_arg($arg);
if (!$tags) { if (!$tags) {
$result = null; $result = null;
} else { } else {
@ -281,7 +269,7 @@ class CommandInterpreter
$result = null; $result = null;
break; break;
} }
list($other, $tags) = $this->split_arg($arg); list($other, $tags) = self::split_arg($arg);
if (!$tags) { if (!$tags) {
$result = null; $result = null;
} else { } else {
@ -292,7 +280,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
$result = null; $result = null;
} else { } else {
list($word, $extra) = $this->split_arg($arg); list($word, $extra) = self::split_arg($arg);
if ($extra) { if ($extra) {
$result = null; $result = null;
} else if ($word == 'off') { } else if ($word == 'off') {
@ -306,7 +294,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
$result = null; $result = null;
} else { } else {
list($word, $extra) = $this->split_arg($arg); list($word, $extra) = self::split_arg($arg);
if ($extra) { if ($extra) {
$result = null; $result = null;
} else if ($word == 'all') { } else if ($word == 'all') {
@ -337,7 +325,7 @@ class CommandInterpreter
/** /**
* Split arguments without triggering a PHP notice warning * Split arguments without triggering a PHP notice warning
*/ */
function split_arg($text) static function split_arg($text)
{ {
$pieces = explode(' ', $text, 2); $pieces = explode(' ', $text, 2);
if (count($pieces) == 1) { if (count($pieces) == 1) {

View File

@ -76,6 +76,41 @@ class DirectMessagePlugin extends Plugin
return true; return true;
} }
/**
* Are we allowed to perform a certain command over the API?
*/
public function onCommandSupportedAPI(Command $cmd, &$supported)
{
$supported = $supported || $cmd instanceof MessageCommand;
return true;
}
/**
* EndInterpretCommand will handle the 'd' and 'dm' commands.
*
* @param string $cmd Command being run
* @param string $arg Rest of the message (including address)
* @param User $user User sending the message
* @param Command &$result The resulting command object to be run.
*
* @return boolean hook value
*/
public function onStartInterpretCommand($cmd, $arg, $user, &$result)
{
$dm_cmds = array('d', 'dm');
if ($result === false && in_array($cmd, $dm_cmds)) {
if (!empty($arg)) {
list($other, $extra) = CommandInterpreter::split_arg($arg);
if (!empty($extra)) {
$result = new MessageCommand($user, $other, $extra);
}
}
return false;
}
return true;
}
public function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null) public function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
{ {
if ($scoped instanceof Profile && $scoped->id == $target->id if ($scoped instanceof Profile && $scoped->id == $target->id