3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/channel.php');
28 function __construct($user=null)
34 * Execute the command and send success or error results
35 * back via the given communications channel.
39 public function execute($channel)
42 $this->handle($channel);
43 } catch (CommandException $e) {
44 $channel->error($this->user, $e->getMessage());
45 } catch (Exception $e) {
46 common_log(LOG_ERR, "Error handling " . get_class($this) . ": " . $e->getMessage());
47 $channel->error($this->user, $e->getMessage());
52 * Override this with the meat!
54 * An error to send back to the user may be sent by throwing
55 * a CommandException with a formatted message.
58 * @throws CommandException
60 function handle($channel)
66 * Look up a notice from an argument, by poster's name to get last post
67 * or notice_id prefixed with #.
70 * @throws CommandException
72 function getNotice($arg)
75 if (Event::handle('StartCommandGetNotice', array($this, $arg, &$notice))) {
76 if(substr($this->other,0,1)=='#'){
77 // A specific notice_id #123
79 $notice = Notice::staticGet(substr($arg,1));
81 // TRANS: Command exception text shown when a notice ID is requested that does not exist.
82 throw new CommandException(_('Notice with that id does not exist.'));
86 if (Validate::uri($this->other)) {
87 // A specific notice by URI lookup
88 $notice = Notice::staticGet('uri', $arg);
92 // Local or remote profile name to get their last notice.
93 // May throw an exception and report 'no such user'
94 $recipient = $this->getProfile($arg);
96 $notice = $recipient->getCurrentNotice();
98 // TRANS: Command exception text shown when a last user notice is requested and it does not exist.
99 throw new CommandException(_('User has no last notice.'));
103 Event::handle('EndCommandGetNotice', array($this, $arg, &$notice));
105 // TRANS: Command exception text shown when a notice ID is requested that does not exist.
106 throw new CommandException(_('Notice with that id does not exist.'));
112 * Look up a local or remote profile by nickname.
115 * @throws CommandException
117 function getProfile($arg)
120 if (Event::handle('StartCommandGetProfile', array($this, $arg, &$profile))) {
122 common_relative_profile($this->user, common_canonical_nickname($arg));
124 Event::handle('EndCommandGetProfile', array($this, $arg, &$profile));
126 // TRANS: Message given requesting a profile for a non-existing user.
127 // TRANS: %s is the nickname of the user for which the profile could not be found.
128 throw new CommandException(sprintf(_('Could not find a user with nickname %s.'), $arg));
134 * Get a local user by name
136 * @throws CommandException
138 function getUser($arg)
141 if (Event::handle('StartCommandGetUser', array($this, $arg, &$user))) {
142 $user = User::staticGet('nickname', $arg);
144 Event::handle('EndCommandGetUser', array($this, $arg, &$user));
146 // TRANS: Message given getting a non-existing user.
147 // TRANS: %s is the nickname of the user that could not be found.
148 throw new CommandException(sprintf(_('Could not find a local user with nickname %s.'),
155 * Get a local or remote group by name.
157 * @throws CommandException
159 function getGroup($arg)
162 if (Event::handle('StartCommandGetGroup', array($this, $arg, &$group))) {
163 $group = User_group::getForNickname($arg, $this->user->getProfile());
165 Event::handle('EndCommandGetGroup', array($this, $arg, &$group));
167 // TRANS: Command exception text shown when a group is requested that does not exist.
168 throw new CommandException(_('No such group.'));
174 class CommandException extends Exception
178 class UnimplementedCommand extends Command
180 function handle($channel)
182 // TRANS: Error text shown when an unimplemented command is given.
183 $channel->error($this->user, _("Sorry, this command is not yet implemented."));
187 class TrackingCommand extends UnimplementedCommand
191 class TrackOffCommand extends UnimplementedCommand
195 class TrackCommand extends UnimplementedCommand
198 function __construct($user, $word)
200 parent::__construct($user);
205 class UntrackCommand extends UnimplementedCommand
208 function __construct($user, $word)
210 parent::__construct($user);
215 class NudgeCommand extends Command
218 function __construct($user, $other)
220 parent::__construct($user);
221 $this->other = $other;
224 function handle($channel)
226 $recipient = $this->getUser($this->other);
227 if ($recipient->id == $this->user->id) {
228 // TRANS: Command exception text shown when a user tries to nudge themselves.
229 throw new CommandException(_('It does not make a lot of sense to nudge yourself!'));
231 if ($recipient->email && $recipient->emailnotifynudge) {
232 mail_notify_nudge($this->user, $recipient);
235 // XXX: notify by SMS
236 // TRANS: Message given having nudged another user.
237 // TRANS: %s is the nickname of the user that was nudged.
238 $channel->output($this->user, sprintf(_('Nudge sent to %s.'),
239 $recipient->nickname));
244 class InviteCommand extends UnimplementedCommand
247 function __construct($user, $other)
249 parent::__construct($user);
250 $this->other = $other;
254 class StatsCommand extends Command
256 function handle($channel)
258 $profile = $this->user->getProfile();
260 $subs_count = $profile->subscriptionCount();
261 $subbed_count = $profile->subscriberCount();
262 $notice_count = $profile->noticeCount();
264 // TRANS: User statistics text.
265 // TRANS: %1$s is the number of other user the user is subscribed to.
266 // TRANS: %2$s is the number of users that are subscribed to the user.
267 // TRANS: %3$s is the number of notices the user has sent.
268 $channel->output($this->user, sprintf(_("Subscriptions: %1\$s\n".
269 "Subscribers: %2\$s\n".
277 class FavCommand extends Command
281 function __construct($user, $other)
283 parent::__construct($user);
284 $this->other = $other;
287 function handle($channel)
289 $notice = $this->getNotice($this->other);
290 $fave = Fave::addNew($this->user->getProfile(), $notice);
293 // TRANS: Error message text shown when a favorite could not be set.
294 $channel->error($this->user, _('Could not create favorite.'));
298 // @fixme favorite notification should be triggered
301 $other = User::staticGet('id', $notice->profile_id);
303 if ($other && $other->id != $user->id) {
304 if ($other->email && $other->emailnotifyfav) {
305 mail_notify_fave($other, $this->user, $notice);
309 $this->user->blowFavesCache();
311 // TRANS: Text shown when a notice has been marked as favourite successfully.
312 $channel->output($this->user, _('Notice marked as fave.'));
316 class JoinCommand extends Command
320 function __construct($user, $other)
322 parent::__construct($user);
323 $this->other = $other;
326 function handle($channel)
328 $group = $this->getGroup($this->other);
331 if ($cur->isMember($group)) {
332 // TRANS: Error text shown a user tries to join a group they already are a member of.
333 $channel->error($cur, _('You are already a member of that group.'));
336 if (Group_block::isBlocked($group, $cur->getProfile())) {
337 // TRANS: Error text shown when a user tries to join a group they are blocked from joining.
338 $channel->error($cur, _('You have been blocked from that group by the admin.'));
343 if (Event::handle('StartJoinGroup', array($group, $cur))) {
344 Group_member::join($group->id, $cur->id);
345 Event::handle('EndJoinGroup', array($group, $cur));
347 } catch (Exception $e) {
348 // TRANS: Message given having failed to add a user to a group.
349 // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group.
350 $channel->error($cur, sprintf(_('Could not join user %1$s to group %2$s.'),
351 $cur->nickname, $group->nickname));
355 // TRANS: Message given having added a user to a group.
356 // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group.
357 $channel->output($cur, sprintf(_('%1$s joined group %2$s.'),
363 class DropCommand extends Command
367 function __construct($user, $other)
369 parent::__construct($user);
370 $this->other = $other;
373 function handle($channel)
375 $group = $this->getGroup($this->other);
379 // TRANS: Error text shown when trying to leave a group that does not exist.
380 $channel->error($cur, _('No such group.'));
384 if (!$cur->isMember($group)) {
385 // TRANS: Error text shown when trying to leave an existing group the user is not a member of.
386 $channel->error($cur, _('You are not a member of that group.'));
391 if (Event::handle('StartLeaveGroup', array($group, $cur))) {
392 Group_member::leave($group->id, $cur->id);
393 Event::handle('EndLeaveGroup', array($group, $cur));
395 } catch (Exception $e) {
396 // TRANS: Message given having failed to remove a user from a group.
397 // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group.
398 $channel->error($cur, sprintf(_('Could not remove user %1$s from group %2$s.'),
399 $cur->nickname, $group->nickname));
403 // TRANS: Message given having removed a user from a group.
404 // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group.
405 $channel->output($cur, sprintf(_('%1$s left group %2$s.'),
411 class WhoisCommand extends Command
414 function __construct($user, $other)
416 parent::__construct($user);
417 $this->other = $other;
420 function handle($channel)
422 $recipient = $this->getProfile($this->other);
424 // TRANS: Whois output.
425 // TRANS: %1$s nickname of the queried user, %2$s is their profile URL.
426 $whois = sprintf(_("%1\$s (%2\$s)"), $recipient->nickname,
427 $recipient->profileurl);
428 if ($recipient->fullname) {
429 // TRANS: Whois output. %s is the full name of the queried user.
430 $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname);
432 if ($recipient->location) {
433 // TRANS: Whois output. %s is the location of the queried user.
434 $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location);
436 if ($recipient->homepage) {
437 // TRANS: Whois output. %s is the homepage of the queried user.
438 $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage);
440 if ($recipient->bio) {
441 // TRANS: Whois output. %s is the bio information of the queried user.
442 $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio);
444 $channel->output($this->user, $whois);
448 class MessageCommand extends Command
452 function __construct($user, $other, $text)
454 parent::__construct($user);
455 $this->other = $other;
459 function handle($channel)
462 $other = $this->getUser($this->other);
463 } catch (CommandException $e) {
465 $profile = $this->getProfile($this->other);
466 } catch (CommandException $f) {
469 // TRANS: Command exception text shown when trying to send a direct message to a remote user (a user not registered at the current server).
470 // TRANS: %s is a remote profile.
471 throw new CommandException(sprintf(_('%s is a remote profile; you can only send direct messages to users on the same server.'), $this->other));
474 $len = mb_strlen($this->text);
477 // TRANS: Command exception text shown when trying to send a direct message to another user without content.
478 $channel->error($this->user, _('No content!'));
482 $this->text = common_shorten_links($this->text);
484 if (Message::contentTooLong($this->text)) {
485 // XXX: i18n. Needs plural support.
486 // TRANS: Message given if content is too long.
487 // TRANS: %1$d is the maximum number of characters, %2$d is the number of submitted characters.
488 $channel->error($this->user, sprintf(_('Message too long - maximum is %1$d characters, you sent %2$d.'),
489 Message::maxContent(), mb_strlen($this->text)));
494 // TRANS: Error text shown when trying to send a direct message to a user that does not exist.
495 $channel->error($this->user, _('No such user.'));
497 } else if (!$this->user->mutuallySubscribed($other)) {
498 // 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).
499 $channel->error($this->user, _('You can\'t send a message to this user.'));
501 } else if ($this->user->id == $other->id) {
502 // TRANS: Error text shown when trying to send a direct message to self.
503 $channel->error($this->user, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
506 $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
509 // TRANS: Message given have sent a direct message to another user.
510 // TRANS: %s is the name of the other user.
511 $channel->output($this->user, sprintf(_('Direct message to %s sent.'), $this->other));
513 // TRANS: Error text shown sending a direct message fails with an unknown reason.
514 $channel->error($this->user, _('Error sending direct message.'));
519 class RepeatCommand extends Command
522 function __construct($user, $other)
524 parent::__construct($user);
525 $this->other = $other;
528 function handle($channel)
530 $notice = $this->getNotice($this->other);
532 if($this->user->id == $notice->profile_id)
534 // TRANS: Error text shown when trying to repeat an own notice.
535 $channel->error($this->user, _('Cannot repeat your own notice.'));
539 if ($this->user->getProfile()->hasRepeated($notice->id)) {
540 // TRANS: Error text shown when trying to repeat an notice that was already repeated by the user.
541 $channel->error($this->user, _('Already repeated that notice.'));
545 $repeat = $notice->repeat($this->user->id, $channel->source);
549 // TRANS: Message given having repeated a notice from another user.
550 // TRANS: %s is the name of the user for which the notice was repeated.
551 $channel->output($this->user, sprintf(_('Notice from %s repeated.'), $recipient->nickname));
553 // TRANS: Error text shown when repeating a notice fails with an unknown reason.
554 $channel->error($this->user, _('Error repeating notice.'));
559 class ReplyCommand extends Command
563 function __construct($user, $other, $text)
565 parent::__construct($user);
566 $this->other = $other;
570 function handle($channel)
572 $notice = $this->getNotice($this->other);
573 $recipient = $notice->getProfile();
575 $len = mb_strlen($this->text);
578 // TRANS: Command exception text shown when trying to reply to a notice without providing content for the reply.
579 $channel->error($this->user, _('No content!'));
583 $this->text = common_shorten_links($this->text);
585 if (Notice::contentTooLong($this->text)) {
586 // XXX: i18n. Needs plural support.
587 // TRANS: Message given if content of a notice for a reply is too long.
588 // TRANS: %1$d is the maximum number of characters, %2$d is the number of submitted characters.
589 $channel->error($this->user, sprintf(_('Notice too long - maximum is %1$d characters, you sent %2$d.'),
590 Notice::maxContent(), mb_strlen($this->text)));
594 $notice = Notice::saveNew($this->user->id, $this->text, $channel->source(),
595 array('reply_to' => $notice->id));
598 // TRANS: Text shown having sent a reply to a notice successfully.
599 // TRANS: %s is the nickname of the user of the notice the reply was sent to.
600 $channel->output($this->user, sprintf(_('Reply to %s sent.'), $recipient->nickname));
602 // TRANS: Error text shown when a reply to a notice fails with an unknown reason.
603 $channel->error($this->user, _('Error saving notice.'));
609 class GetCommand extends Command
613 function __construct($user, $other)
615 parent::__construct($user);
616 $this->other = $other;
619 function handle($channel)
621 $target = $this->getProfile($this->other);
623 $notice = $target->getCurrentNotice();
625 // TRANS: Error text shown when a last user notice is requested and it does not exist.
626 $channel->error($this->user, _('User has no last notice.'));
629 $notice_content = $notice->content;
631 $channel->output($this->user, $target->nickname . ": " . $notice_content);
635 class SubCommand extends Command
639 function __construct($user, $other)
641 parent::__construct($user);
642 $this->other = $other;
645 function handle($channel)
649 // TRANS: Error text shown when no username was provided when issuing a subscribe command.
650 $channel->error($this->user, _('Specify the name of the user to subscribe to.'));
654 $target = $this->getProfile($this->other);
656 $remote = Remote_profile::staticGet('id', $target->id);
658 // TRANS: Command exception text shown when trying to subscribe to an OMB profile using the subscribe command.
659 throw new CommandException(_("Can't subscribe to OMB profiles by command."));
663 Subscription::start($this->user->getProfile(),
665 // TRANS: Text shown after having subscribed to another user successfully.
666 // TRANS: %s is the name of the user the subscription was requested for.
667 $channel->output($this->user, sprintf(_('Subscribed to %s.'), $this->other));
668 } catch (Exception $e) {
669 $channel->error($this->user, $e->getMessage());
674 class UnsubCommand extends Command
678 function __construct($user, $other)
680 parent::__construct($user);
681 $this->other = $other;
684 function handle($channel)
687 // TRANS: Error text shown when no username was provided when issuing an unsubscribe command.
688 $channel->error($this->user, _('Specify the name of the user to unsubscribe from.'));
692 $target = $this->getProfile($this->other);
695 Subscription::cancel($this->user->getProfile(),
697 // TRANS: Text shown after having unsubscribed from another user successfully.
698 // TRANS: %s is the name of the user the unsubscription was requested for.
699 $channel->output($this->user, sprintf(_('Unsubscribed from %s.'), $this->other));
700 } catch (Exception $e) {
701 $channel->error($this->user, $e->getMessage());
706 class OffCommand extends Command
710 function __construct($user, $other=null)
712 parent::__construct($user);
713 $this->other = $other;
715 function handle($channel)
718 // TRANS: Error text shown when issuing the command "off" with a setting which has not yet been implemented.
719 $channel->error($this->user, _("Command not yet implemented."));
721 if ($channel->off($this->user)) {
722 // TRANS: Text shown when issuing the command "off" successfully.
723 $channel->output($this->user, _('Notification off.'));
725 // TRANS: Error text shown when the command "off" fails for an unknown reason.
726 $channel->error($this->user, _('Can\'t turn off notification.'));
732 class OnCommand extends Command
735 function __construct($user, $other=null)
737 parent::__construct($user);
738 $this->other = $other;
741 function handle($channel)
744 // TRANS: Error text shown when issuing the command "on" with a setting which has not yet been implemented.
745 $channel->error($this->user, _("Command not yet implemented."));
747 if ($channel->on($this->user)) {
748 // TRANS: Text shown when issuing the command "on" successfully.
749 $channel->output($this->user, _('Notification on.'));
751 // TRANS: Error text shown when the command "on" fails for an unknown reason.
752 $channel->error($this->user, _('Can\'t turn on notification.'));
758 class LoginCommand extends Command
760 function handle($channel)
762 $disabled = common_config('logincommand','disabled');
763 $disabled = isset($disabled) && $disabled;
765 // TRANS: Error text shown when issuing the login command while login is disabled.
766 $channel->error($this->user, _('Login command is disabled.'));
771 $login_token = Login_token::makeNew($this->user);
772 } catch (Exception $e) {
773 $channel->error($this->user, $e->getMessage());
776 $channel->output($this->user,
777 // TRANS: Text shown after issuing the login command successfully.
778 // TRANS: %s is a logon link..
779 sprintf(_('This link is useable only once and is valid for only 2 minutes: %s.'),
780 common_local_url('otp',
781 array('user_id' => $login_token->user_id, 'token' => $login_token->token))));
785 class LoseCommand extends Command
789 function __construct($user, $other)
791 parent::__construct($user);
792 $this->other = $other;
795 function execute($channel)
798 // TRANS: Error text shown when no username was provided when issuing the command.
799 $channel->error($this->user, _('Specify the name of the user to unsubscribe from.'));
803 $result = Subscription::cancel($this->getProfile($this->other), $this->user->getProfile());
806 // TRANS: Text shown after issuing the lose command successfully (stop another user from following the current user).
807 // TRANS: %s is the name of the user the unsubscription was requested for.
808 $channel->output($this->user, sprintf(_('Unsubscribed %s.'), $this->other));
810 $channel->error($this->user, $result);
815 class SubscriptionsCommand extends Command
817 function handle($channel)
819 $profile = $this->user->getSubscriptions(0);
821 while ($profile->fetch()) {
822 $nicknames[]=$profile->nickname;
824 if(count($nicknames)==0){
825 // TRANS: Text shown after requesting other users a user is subscribed to without having any subscriptions.
826 $out=_('You are not subscribed to anyone.');
828 // TRANS: Text shown after requesting other users a user is subscribed to.
829 // TRANS: This message supports plural forms. This message is followed by a
830 // TRANS: hard coded space and a comma separated list of subscribed users.
831 $out = ngettext('You are subscribed to this person:',
832 'You are subscribed to these people:',
835 $out .= implode(', ',$nicknames);
837 $channel->output($this->user,$out);
841 class SubscribersCommand extends Command
843 function handle($channel)
845 $profile = $this->user->getSubscribers();
847 while ($profile->fetch()) {
848 $nicknames[]=$profile->nickname;
850 if(count($nicknames)==0){
851 // TRANS: Text shown after requesting other users that are subscribed to a user
852 // TRANS: (followers) without having any subscribers.
853 $out=_('No one is subscribed to you.');
855 // TRANS: Text shown after requesting other users that are subscribed to a user (followers).
856 // TRANS: This message supports plural forms. This message is followed by a
857 // TRANS: hard coded space and a comma separated list of subscribing users.
858 $out = ngettext('This person is subscribed to you:',
859 'These people are subscribed to you:',
862 $out .= implode(', ',$nicknames);
864 $channel->output($this->user,$out);
868 class GroupsCommand extends Command
870 function handle($channel)
872 $group = $this->user->getGroups();
874 while ($group->fetch()) {
875 $groups[]=$group->nickname;
877 if(count($groups)==0){
878 // TRANS: Text shown after requesting groups a user is subscribed to without having
879 // TRANS: any group subscriptions.
880 $out=_('You are not a member of any groups.');
882 // TRANS: Text shown after requesting groups a user is subscribed to.
883 // TRANS: This message supports plural forms. This message is followed by a
884 // TRANS: hard coded space and a comma separated list of subscribed groups.
885 $out = ngettext('You are a member of this group:',
886 'You are a member of these groups:',
888 $out.=implode(', ',$groups);
890 $channel->output($this->user,$out);
894 class HelpCommand extends Command
896 function handle($channel)
898 $channel->output($this->user,
899 // TRANS: Help text for commands. Do not translate the command names themselves; they are fixed strings.
901 "on - turn on notifications\n".
902 "off - turn off notifications\n".
903 "help - show this help\n".
904 "follow <nickname> - subscribe to user\n".
905 "groups - lists the groups you have joined\n".
906 "subscriptions - list the people you follow\n".
907 "subscribers - list the people that follow you\n".
908 "leave <nickname> - unsubscribe from user\n".
909 "d <nickname> <text> - direct message to user\n".
910 "get <nickname> - get last notice from user\n".
911 "whois <nickname> - get profile info on user\n".
912 "lose <nickname> - force user to stop following you\n".
913 "fav <nickname> - add user's last notice as a 'fave'\n".
914 "fav #<notice_id> - add notice with the given id as a 'fave'\n".
915 "repeat #<notice_id> - repeat a notice with a given id\n".
916 "repeat <nickname> - repeat the last notice from user\n".
917 "reply #<notice_id> - reply to notice with a given id\n".
918 "reply <nickname> - reply to the last notice from user\n".
919 "join <group> - join group\n".
920 "login - Get a link to login to the web interface\n".
921 "drop <group> - leave group\n".
922 "stats - get your stats\n".
923 "stop - same as 'off'\n".
924 "quit - same as 'off'\n".
925 "sub <nickname> - same as 'follow'\n".
926 "unsub <nickname> - same as 'leave'\n".
927 "last <nickname> - same as 'get'\n".
928 "on <nickname> - not yet implemented.\n".
929 "off <nickname> - not yet implemented.\n".
930 "nudge <nickname> - remind a user to update.\n".
931 "invite <phone number> - not yet implemented.\n".
932 "track <word> - not yet implemented.\n".
933 "untrack <word> - not yet implemented.\n".
934 "track off - not yet implemented.\n".
935 "untrack all - not yet implemented.\n".
936 "tracks - not yet implemented.\n".
937 "tracking - not yet implemented.\n"));