X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Fcommand.php;h=4b7f1c4f7e283beabd020ca5434d9a9885d792c9;hb=d6b28c64830f632bb2f4b6f3c9369b9e56ad217a;hp=29aa286d1db0d890950a962f0eb3053db4dceb7b;hpb=3eb900e31f49c7d973266bb76e99eb0737f37880;p=quix0rs-gnu-social.git diff --git a/lib/command.php b/lib/command.php index 29aa286d1d..4b7f1c4f7e 100644 --- a/lib/command.php +++ b/lib/command.php @@ -17,16 +17,18 @@ * along with this program. If not, see . */ -if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } +if (!defined('GNUSOCIAL')) { exit(1); } require_once(INSTALLDIR.'/lib/channel.php'); class Command { + protected $scoped = null; // The Profile of the user performing the command var $user = null; function __construct($user=null) { + $this->scoped = $user->getProfile(); $this->user = $user; } @@ -76,7 +78,7 @@ class Command if(substr($this->other,0,1)=='#'){ // A specific notice_id #123 - $notice = Notice::staticGet(substr($arg,1)); + $notice = Notice::getKV(substr($arg,1)); if (!$notice) { // TRANS: Command exception text shown when a notice ID is requested that does not exist. throw new CommandException(_('Notice with that id does not exist.')); @@ -85,7 +87,7 @@ class Command if (Validate::uri($this->other)) { // A specific notice by URI lookup - $notice = Notice::staticGet('uri', $arg); + $notice = Notice::getKV('uri', $arg); } if (!$notice) { @@ -139,7 +141,7 @@ class Command { $user = null; if (Event::handle('StartCommandGetUser', array($this, $arg, &$user))) { - $user = User::staticGet('nickname', Nickname::normalize($arg)); + $user = User::getKV('nickname', Nickname::normalize($arg)); } Event::handle('EndCommandGetUser', array($this, $arg, &$user)); if (!$user){ @@ -180,7 +182,7 @@ class UnimplementedCommand extends Command function handle($channel) { // TRANS: Error text shown when an unimplemented command is given. - $channel->error($this->user, _("Sorry, this command is not yet implemented.")); + $channel->error($this->user, _('Sorry, this command is not yet implemented.')); } } @@ -274,57 +276,6 @@ class StatsCommand extends Command } } -class FavCommand extends Command -{ - var $other = null; - - function __construct($user, $other) - { - parent::__construct($user); - $this->other = $other; - } - - function handle($channel) - { - $notice = $this->getNotice($this->other); - - $fave = new Fave(); - $fave->user_id = $this->user->id; - $fave->notice_id = $notice->id; - $fave->find(); - - if ($fave->fetch()) { - // TRANS: Error message text shown when a favorite could not be set because it has already been favorited. - $channel->error($this->user, _('Could not create favorite: already favorited.')); - return; - } - - $fave = Fave::addNew($this->user->getProfile(), $notice); - - if (!$fave) { - // TRANS: Error message text shown when a favorite could not be set. - $channel->error($this->user, _('Could not create favorite.')); - return; - } - - // @fixme favorite notification should be triggered - // at a lower level - - $other = User::staticGet('id', $notice->profile_id); - - if ($other && $other->id != $this->user->id) { - if ($other->email && $other->emailnotifyfav) { - mail_notify_fave($other, $this->user, $notice); - } - } - - $this->user->blowFavesCache(); - - // TRANS: Text shown when a notice has been marked as favourite successfully. - $channel->output($this->user, _('Notice marked as fave.')); - } -} - class JoinCommand extends Command { var $other = null; @@ -352,10 +303,7 @@ class JoinCommand extends Command } try { - if (Event::handle('StartJoinGroup', array($group, $cur))) { - Group_member::join($group->id, $cur->id); - Event::handle('EndJoinGroup', array($group, $cur)); - } + $cur->joinGroup($group); } catch (Exception $e) { // TRANS: Message given having failed to add a user to a group. // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group. @@ -400,10 +348,7 @@ class DropCommand extends Command } try { - if (Event::handle('StartLeaveGroup', array($group, $cur))) { - Group_member::leave($group->id, $cur->id); - Event::handle('EndLeaveGroup', array($group, $cur)); - } + $cur->leaveGroup($group); } catch (Exception $e) { // TRANS: Message given having failed to remove a user from a group. // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group. @@ -420,117 +365,128 @@ class DropCommand extends Command } } -class WhoisCommand extends Command +class TagCommand extends Command { var $other = null; - function __construct($user, $other) + var $tags = null; + function __construct($user, $other, $tags) { parent::__construct($user); $this->other = $other; + $this->tags = $tags; } function handle($channel) { - $recipient = $this->getProfile($this->other); + $profile = $this->getProfile($this->other); + $cur = $this->user->getProfile(); - // TRANS: Whois output. - // TRANS: %1$s nickname of the queried user, %2$s is their profile URL. - $whois = sprintf(_m('WHOIS',"%1\$s (%2\$s)"), $recipient->nickname, - $recipient->profileurl); - if ($recipient->fullname) { - // TRANS: Whois output. %s is the full name of the queried user. - $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname); + if (!$profile) { + // TRANS: Client error displayed trying to perform an action related to a non-existing profile. + $channel->error($cur, _('No such profile.')); + return; } - if ($recipient->location) { - // TRANS: Whois output. %s is the location of the queried user. - $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location); + if (!$cur->canTag($profile)) { + // TRANS: Error displayed when trying to tag a user that cannot be tagged. + $channel->error($cur, _('You cannot tag this user.')); + return; } - if ($recipient->homepage) { - // TRANS: Whois output. %s is the homepage of the queried user. - $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage); + + $privs = array(); + $tags = preg_split('/[\s,]+/', $this->tags); + $clean_tags = array(); + + foreach ($tags as $tag) { + $private = @$tag[0] === '.'; + $tag = $clean_tags[] = common_canonical_tag($tag); + + if (!common_valid_profile_tag($tag)) { + // TRANS: Error displayed if a given tag is invalid. + // TRANS: %s is the invalid tag. + $channel->error($cur, sprintf(_('Invalid tag: "%s".'), $tag)); + return; + } + $privs[$tag] = $private; } - if ($recipient->bio) { - // TRANS: Whois output. %s is the bio information of the queried user. - $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio); + + try { + foreach ($clean_tags as $tag) { + Profile_tag::setTag($cur->id, $profile->id, $tag, null, $privs[$tag]); + } + } catch (Exception $e) { + // TRANS: Error displayed if tagging a user fails. + // TRANS: %1$s is the tagged user, %2$s is the error message (no punctuation). + $channel->error($cur, sprintf(_('Error tagging %1$s: %2$s'), + $profile->nickname, $e->getMessage())); + return; } - $channel->output($this->user, $whois); + + // TRANS: Succes message displayed if tagging a user succeeds. + // TRANS: %1$s is the tagged user's nickname, %2$s is a list of tags. + // TRANS: Plural is decided based on the number of tags added (not part of message). + $channel->output($cur, sprintf(_m('%1$s was tagged %2$s', + '%1$s was tagged %2$s', + count($clean_tags)), + $profile->nickname, + // TRANS: Separator for list of tags. + implode(_(', '), $clean_tags))); } } -class MessageCommand extends Command +class UntagCommand extends TagCommand { - 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); - } 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); + $profile = $this->getProfile($this->other); + $cur = $this->user->getProfile(); - 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!')); + if (!$profile) { + // TRANS: Client error displayed trying to perform an action related to a non-existing profile. + $channel->error($cur, _('No such profile.')); + return; + } + if (!$cur->canTag($profile)) { + // TRANS: Error displayed when trying to tag a user that cannot be tagged. + $channel->error($cur, _('You cannot tag this user.')); return; } - $this->text = $this->user->shortenLinks($this->text); + $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $this->tags)); - 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; + foreach ($tags as $tag) { + if (!common_valid_profile_tag($tag)) { + // TRANS: Error displayed if a given tag is invalid. + // TRANS: %s is the invalid tag. + $channel->error($cur, sprintf(_('Invalid tag: "%s"'), $tag)); + return; + } } - if (!$other) { - // 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, _('Don\'t send a message to yourself; just say it to yourself quietly instead.')); + try { + foreach ($tags as $tag) { + Profile_tag::unTag($cur->id, $profile->id, $tag); + } + } catch (Exception $e) { + // TRANS: Error displayed if untagging a user fails. + // TRANS: %1$s is the untagged user, %2$s is the error message (no punctuation). + $channel->error($cur, sprintf(_('Error untagging %1$s: %2$s'), + $profile->nickname, $e->getMessage())); return; } - $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source()); - if ($message) { - $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)); - } else { - // TRANS: Error text shown sending a direct message fails with an unknown reason. - $channel->error($this->user, _('Error sending direct message.')); - } + + // TRANS: Succes message displayed if untagging a user succeeds. + // TRANS: %1$s is the untagged user's nickname, %2$s is a list of tags. + // TRANS: Plural is decided based on the number of tags removed (not part of message). + $channel->output($cur, sprintf(_m('The following tag was removed from user %1$s: %2$s.', + 'The following tags were removed from user %1$s: %2$s.', + count($tags)), + $profile->nickname, + // TRANS: Separator for list of tags. + implode(_(', '), $tags))); } } -class RepeatCommand extends Command +class WhoisCommand extends Command { var $other = null; function __construct($user, $other) @@ -541,32 +497,29 @@ class RepeatCommand extends Command function handle($channel) { - $notice = $this->getNotice($this->other); + $recipient = $this->getProfile($this->other); - if($this->user->id == $notice->profile_id) - { - // TRANS: Error text shown when trying to repeat an own notice. - $channel->error($this->user, _('Cannot repeat your own notice.')); - return; + // TRANS: Whois output. + // TRANS: %1$s nickname of the queried user, %2$s is their profile URL. + $whois = sprintf(_m('WHOIS',"%1\$s (%2\$s)"), $recipient->nickname, + $recipient->profileurl); + if ($recipient->fullname) { + // TRANS: Whois output. %s is the full name of the queried user. + $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname); } - - if ($this->user->getProfile()->hasRepeated($notice->id)) { - // TRANS: Error text shown when trying to repeat an notice that was already repeated by the user. - $channel->error($this->user, _('Already repeated that notice.')); - return; + if ($recipient->location) { + // TRANS: Whois output. %s is the location of the queried user. + $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location); } - - $repeat = $notice->repeat($this->user->id, $channel->source); - - if ($repeat) { - - // TRANS: Message given having repeated a notice from another user. - // TRANS: %s is the name of the user for which the notice was repeated. - $channel->output($this->user, sprintf(_('Notice from %s repeated.'), $recipient->nickname)); - } else { - // TRANS: Error text shown when repeating a notice fails with an unknown reason. - $channel->error($this->user, _('Error repeating notice.')); + if ($recipient->homepage) { + // TRANS: Whois output. %s is the homepage of the queried user. + $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage); } + if ($recipient->bio) { + // TRANS: Whois output. %s is the bio information of the queried user. + $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio); + } + $channel->output($this->user, $whois); } } @@ -669,15 +622,8 @@ class SubCommand extends Command $target = $this->getProfile($this->other); - $remote = Remote_profile::staticGet('id', $target->id); - if ($remote) { - // TRANS: Command exception text shown when trying to subscribe to an OMB profile using the subscribe command. - throw new CommandException(_("Can't subscribe to OMB profiles by command.")); - } - try { - Subscription::start($this->user->getProfile(), - $target); + Subscription::start($this->user->getProfile(), $target); // TRANS: Text shown after having subscribed to another user successfully. // TRANS: %s is the name of the user the subscription was requested for. $channel->output($this->user, sprintf(_('Subscribed to %s.'), $this->other)); @@ -708,8 +654,7 @@ class UnsubCommand extends Command $target = $this->getProfile($this->other); try { - Subscription::cancel($this->user->getProfile(), - $target); + Subscription::cancel($this->user->getProfile(), $target); // TRANS: Text shown after having unsubscribed from another user successfully. // TRANS: %s is the name of the user the unsubscription was requested for. $channel->output($this->user, sprintf(_('Unsubscribed from %s.'), $this->other)); @@ -730,7 +675,7 @@ class OffCommand extends Command } function handle($channel) { - if ($other) { + if ($this->other) { // TRANS: Error text shown when issuing the command "off" with a setting which has not yet been implemented. $channel->error($this->user, _("Command not yet implemented.")); } else { @@ -756,7 +701,7 @@ class OnCommand extends Command function handle($channel) { - if ($other) { + if ($this->other) { // TRANS: Error text shown when issuing the command "on" with a setting which has not yet been implemented. $channel->error($this->user, _("Command not yet implemented.")); } else { @@ -832,7 +777,7 @@ class SubscriptionsCommand extends Command { function handle($channel) { - $profile = $this->user->getSubscriptions(0); + $profile = $this->user->getSubscribed(0); $nicknames=array(); while ($profile->fetch()) { $nicknames[]=$profile->nickname; @@ -844,13 +789,13 @@ class SubscriptionsCommand extends Command // TRANS: Text shown after requesting other users a user is subscribed to. // TRANS: This message supports plural forms. This message is followed by a // TRANS: hard coded space and a comma separated list of subscribed users. - $out = ngettext('You are subscribed to this person:', + $out = _m('You are subscribed to this person:', 'You are subscribed to these people:', count($nicknames)); $out .= ' '; $out .= implode(', ',$nicknames); } - $channel->output($this->user,$out); + $channel->output($this->user, $out); } } @@ -871,13 +816,13 @@ class SubscribersCommand extends Command // TRANS: Text shown after requesting other users that are subscribed to a user (followers). // TRANS: This message supports plural forms. This message is followed by a // TRANS: hard coded space and a comma separated list of subscribing users. - $out = ngettext('This person is subscribed to you:', + $out = _m('This person is subscribed to you:', 'These people are subscribed to you:', count($nicknames)); $out .= ' '; $out .= implode(', ',$nicknames); } - $channel->output($this->user,$out); + $channel->output($this->user, $out); } } @@ -887,7 +832,7 @@ class GroupsCommand extends Command { $group = $this->user->getGroups(); $groups=array(); - while ($group->fetch()) { + while ($group instanceof User_group && $group->fetch()) { $groups[]=$group->nickname; } if(count($groups)==0){ @@ -898,12 +843,12 @@ class GroupsCommand extends Command // TRANS: Text shown after requesting groups a user is subscribed to. // TRANS: This message supports plural forms. This message is followed by a // TRANS: hard coded space and a comma separated list of subscribed groups. - $out = ngettext('You are a member of this group:', + $out = _m('You are a member of this group:', 'You are a member of these groups:', count($nicknames)); $out.=implode(', ',$groups); } - $channel->output($this->user,$out); + $channel->output($this->user, $out); } } @@ -911,45 +856,86 @@ class HelpCommand extends Command { function handle($channel) { - $channel->output($this->user, - // TRANS: Help text for commands. Do not translate the command names themselves; they are fixed strings. - _("Commands:\n". - "on - turn on notifications\n". - "off - turn off notifications\n". - "help - show this help\n". - "follow - subscribe to user\n". - "groups - lists the groups you have joined\n". - "subscriptions - list the people you follow\n". - "subscribers - list the people that follow you\n". - "leave - unsubscribe from user\n". - "d - direct message to user\n". - "get - get last notice from user\n". - "whois - get profile info on user\n". - "lose - force user to stop following you\n". - "fav - add user's last notice as a 'fave'\n". - "fav # - add notice with the given id as a 'fave'\n". - "repeat # - repeat a notice with a given id\n". - "repeat - repeat the last notice from user\n". - "reply # - reply to notice with a given id\n". - "reply - reply to the last notice from user\n". - "join - join group\n". - "login - Get a link to login to the web interface\n". - "drop - leave group\n". - "stats - get your stats\n". - "stop - same as 'off'\n". - "quit - same as 'off'\n". - "sub - same as 'follow'\n". - "unsub - same as 'leave'\n". - "last - same as 'get'\n". - "on - not yet implemented.\n". - "off - not yet implemented.\n". - "nudge - remind a user to update.\n". - "invite - not yet implemented.\n". - "track - not yet implemented.\n". - "untrack - not yet implemented.\n". - "track off - not yet implemented.\n". - "untrack all - not yet implemented.\n". - "tracks - not yet implemented.\n". - "tracking - not yet implemented.\n")); + // TRANS: Header line of help text for commands. + $out = array(_m('COMMANDHELP', "Commands:")); + $commands = array(// TRANS: Help message for IM/SMS command "on". + "on" => _m('COMMANDHELP', "turn on notifications"), + // TRANS: Help message for IM/SMS command "off". + "off" => _m('COMMANDHELP', "turn off notifications"), + // TRANS: Help message for IM/SMS command "help". + "help" => _m('COMMANDHELP', "show this help"), + // TRANS: Help message for IM/SMS command "follow ". + "follow " => _m('COMMANDHELP', "subscribe to user"), + // TRANS: Help message for IM/SMS command "groups". + "groups" => _m('COMMANDHELP', "lists the groups you have joined"), + // TRANS: Help message for IM/SMS command "tag". + "tag " => _m('COMMANDHELP',"tag a user"), + // TRANS: Help message for IM/SMS command "untag". + "untag " => _m('COMMANDHELP',"untag a user"), + // TRANS: Help message for IM/SMS command "subscriptions". + "subscriptions" => _m('COMMANDHELP', "list the people you follow"), + // TRANS: Help message for IM/SMS command "subscribers". + "subscribers" => _m('COMMANDHELP', "list the people that follow you"), + // TRANS: Help message for IM/SMS command "leave ". + "leave " => _m('COMMANDHELP', "unsubscribe from user"), + // TRANS: Help message for IM/SMS command "d ". + "d " => _m('COMMANDHELP', "direct message to user"), + // TRANS: Help message for IM/SMS command "get ". + "get " => _m('COMMANDHELP', "get last notice from user"), + // TRANS: Help message for IM/SMS command "whois ". + "whois " => _m('COMMANDHELP', "get profile info on user"), + // TRANS: Help message for IM/SMS command "lose ". + "lose " => _m('COMMANDHELP', "force user to stop following you"), + // TRANS: Help message for IM/SMS command "reply #". + "reply #" => _m('COMMANDHELP', "reply to notice with a given id"), + // TRANS: Help message for IM/SMS command "reply ". + "reply " => _m('COMMANDHELP', "reply to the last notice from user"), + // TRANS: Help message for IM/SMS command "join ". + "join " => _m('COMMANDHELP', "join group"), + // TRANS: Help message for IM/SMS command "login". + "login" => _m('COMMANDHELP', "Get a link to login to the web interface"), + // TRANS: Help message for IM/SMS command "drop ". + "drop " => _m('COMMANDHELP', "leave group"), + // TRANS: Help message for IM/SMS command "stats". + "stats" => _m('COMMANDHELP', "get your stats"), + // TRANS: Help message for IM/SMS command "stop". + "stop" => _m('COMMANDHELP', "same as 'off'"), + // TRANS: Help message for IM/SMS command "quit". + "quit" => _m('COMMANDHELP', "same as 'off'"), + // TRANS: Help message for IM/SMS command "sub ". + "sub " => _m('COMMANDHELP', "same as 'follow'"), + // TRANS: Help message for IM/SMS command "unsub ". + "unsub " => _m('COMMANDHELP', "same as 'leave'"), + // TRANS: Help message for IM/SMS command "last ". + "last " => _m('COMMANDHELP', "same as 'get'"), + // TRANS: Help message for IM/SMS command "on ". + "on " => _m('COMMANDHELP', "not yet implemented."), + // TRANS: Help message for IM/SMS command "off ". + "off " => _m('COMMANDHELP', "not yet implemented."), + // TRANS: Help message for IM/SMS command "nudge ". + "nudge " => _m('COMMANDHELP', "remind a user to update."), + // TRANS: Help message for IM/SMS command "invite ". + "invite " => _m('COMMANDHELP', "not yet implemented."), + // TRANS: Help message for IM/SMS command "track ". + "track " => _m('COMMANDHELP', "not yet implemented."), + // TRANS: Help message for IM/SMS command "untrack ". + "untrack " => _m('COMMANDHELP', "not yet implemented."), + // TRANS: Help message for IM/SMS command "track off". + "track off" => _m('COMMANDHELP', "not yet implemented."), + // TRANS: Help message for IM/SMS command "untrack all". + "untrack all" => _m('COMMANDHELP', "not yet implemented."), + // TRANS: Help message for IM/SMS command "tracks". + "tracks" => _m('COMMANDHELP', "not yet implemented."), + // TRANS: Help message for IM/SMS command "tracking". + "tracking" => _m('COMMANDHELP', "not yet implemented.")); + + // Give plugins a chance to add or override... + Event::handle('HelpCommandMessages', array($this, &$commands)); + + ksort($commands); + foreach ($commands as $command => $help) { + $out[] = "$command - $help"; + } + $channel->output($this->user, implode("\n", $out)); } }