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');
29 function __construct($user=null)
35 * Execute the command and send success or error results
36 * back via the given communications channel.
40 public function execute($channel)
43 $this->handle($channel);
44 } catch (CommandException $e) {
45 $channel->error($this->user, $e->getMessage());
46 } catch (Exception $e) {
47 common_log(LOG_ERR, "Error handling " . get_class($this) . ": " . $e->getMessage());
48 $channel->error($this->user, $e->getMessage());
54 * Override this with the meat!
56 * An error to send back to the user may be sent by throwing
57 * a CommandException with a formatted message.
60 * @throws CommandException
62 function handle($channel)
68 * Look up a notice from an argument, by poster's name to get last post
69 * or notice_id prefixed with #.
72 * @throws CommandException
74 function getNotice($arg)
77 if (Event::handle('StartCommandGetNotice', array($this, $arg, &$notice))) {
78 if(substr($this->other,0,1)=='#'){
79 // A specific notice_id #123
81 $notice = Notice::staticGet(substr($arg,1));
83 throw new CommandException(_('Notice with that id does not exist'));
87 if (Validate::uri($this->other)) {
88 // A specific notice by URI lookup
89 $notice = Notice::staticGet('uri', $arg);
93 // Local or remote profile name to get their last notice.
94 // May throw an exception and report 'no such user'
95 $recipient = $this->getProfile($arg);
97 $notice = $recipient->getCurrentNotice();
99 throw new CommandException(_('User has no last notice'));
103 Event::handle('EndCommandGetNotice', array($this, $arg, &$notice));
105 throw new CommandException(_('Notice with that id does not exist'));
111 * Look up a local or remote profile by nickname.
114 * @throws CommandException
116 function getProfile($arg)
119 if (Event::handle('StartCommandGetProfile', array($this, $arg, &$profile))) {
121 common_relative_profile($this->user, common_canonical_nickname($arg));
123 Event::handle('EndCommandGetProfile', array($this, $arg, &$profile));
125 // TRANS: Message given requesting a profile for a non-existing user.
126 // TRANS: %s is the nickname of the user for which the profile could not be found.
127 throw new CommandException(sprintf(_('Could not find a user with nickname %s'), $arg));
133 * Get a local user by name
135 * @throws CommandException
137 function getUser($arg)
140 if (Event::handle('StartCommandGetUser', array($this, $arg, &$user))) {
141 $user = User::staticGet('nickname', $arg);
143 Event::handle('EndCommandGetUser', array($this, $arg, &$user));
145 // TRANS: Message given getting a non-existing user.
146 // TRANS: %s is the nickname of the user that could not be found.
147 throw new CommandException(sprintf(_('Could not find a local user with nickname %s'),
154 * Get a local or remote group by name.
156 * @throws CommandException
158 function getGroup($arg)
161 if (Event::handle('StartCommandGetGroup', array($this, $arg, &$group))) {
162 $group = User_group::getForNickname($arg, $this->user->getProfile());
164 Event::handle('EndCommandGetGroup', array($this, $arg, &$group));
166 throw new CommandException(_('No such group.'));
172 class CommandException extends Exception
176 class UnimplementedCommand extends Command
178 function handle($channel)
180 $channel->error($this->user, _("Sorry, this command is not yet implemented."));
184 class TrackingCommand extends UnimplementedCommand
188 class TrackOffCommand extends UnimplementedCommand
192 class TrackCommand extends UnimplementedCommand
195 function __construct($user, $word)
197 parent::__construct($user);
202 class UntrackCommand extends UnimplementedCommand
205 function __construct($user, $word)
207 parent::__construct($user);
212 class NudgeCommand extends Command
215 function __construct($user, $other)
217 parent::__construct($user);
218 $this->other = $other;
221 function handle($channel)
223 $recipient = $this->getUser($this->other);
224 if ($recipient->id == $this->user->id) {
225 throw new CommandException(_('It does not make a lot of sense to nudge yourself!'));
227 if ($recipient->email && $recipient->emailnotifynudge) {
228 mail_notify_nudge($this->user, $recipient);
231 // XXX: notify by SMS
232 // TRANS: Message given having nudged another user.
233 // TRANS: %s is the nickname of the user that was nudged.
234 $channel->output($this->user, sprintf(_('Nudge sent to %s'),
235 $recipient->nickname));
240 class InviteCommand extends UnimplementedCommand
243 function __construct($user, $other)
245 parent::__construct($user);
246 $this->other = $other;
250 class StatsCommand extends Command
252 function handle($channel)
254 $profile = $this->user->getProfile();
256 $subs_count = $profile->subscriptionCount();
257 $subbed_count = $profile->subscriberCount();
258 $notice_count = $profile->noticeCount();
260 $channel->output($this->user, sprintf(_("Subscriptions: %1\$s\n".
261 "Subscribers: %2\$s\n".
269 class FavCommand extends Command
273 function __construct($user, $other)
275 parent::__construct($user);
276 $this->other = $other;
279 function handle($channel)
281 $notice = $this->getNotice($this->other);
282 $fave = Fave::addNew($this->user->getProfile(), $notice);
285 $channel->error($this->user, _('Could not create favorite.'));
289 // @fixme favorite notification should be triggered
292 $other = User::staticGet('id', $notice->profile_id);
294 if ($other && $other->id != $user->id) {
295 if ($other->email && $other->emailnotifyfav) {
296 mail_notify_fave($other, $this->user, $notice);
300 $this->user->blowFavesCache();
302 $channel->output($this->user, _('Notice marked as fave.'));
307 class JoinCommand extends Command
311 function __construct($user, $other)
313 parent::__construct($user);
314 $this->other = $other;
317 function handle($channel)
319 $group = $this->getGroup($this->other);
322 if ($cur->isMember($group)) {
323 $channel->error($cur, _('You are already a member of that group'));
326 if (Group_block::isBlocked($group, $cur->getProfile())) {
327 $channel->error($cur, _('You have been blocked from that group by the admin.'));
332 if (Event::handle('StartJoinGroup', array($group, $cur))) {
333 Group_member::join($group->id, $cur->id);
334 Event::handle('EndJoinGroup', array($group, $cur));
336 } catch (Exception $e) {
337 // TRANS: Message given having failed to add a user to a group.
338 // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group.
339 $channel->error($cur, sprintf(_('Could not join user %1$s to group %2$s'),
340 $cur->nickname, $group->nickname));
344 // TRANS: Message given having added a user to a group.
345 // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group.
346 $channel->output($cur, sprintf(_('%1$s joined group %2$s'),
352 class DropCommand extends Command
356 function __construct($user, $other)
358 parent::__construct($user);
359 $this->other = $other;
362 function handle($channel)
364 $group = $this->getGroup($this->other);
368 $channel->error($cur, _('No such group.'));
372 if (!$cur->isMember($group)) {
373 $channel->error($cur, _('You are not a member of that group.'));
378 if (Event::handle('StartLeaveGroup', array($group, $cur))) {
379 Group_member::leave($group->id, $cur->id);
380 Event::handle('EndLeaveGroup', array($group, $cur));
382 } catch (Exception $e) {
383 // TRANS: Message given having failed to remove a user from a group.
384 // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group.
385 $channel->error($cur, sprintf(_('Could not remove user %1$s from group %2$s'),
386 $cur->nickname, $group->nickname));
390 // TRANS: Message given having removed a user from a group.
391 // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group.
392 $channel->output($cur, sprintf(_('%1$s left group %2$s'),
399 class WhoisCommand extends Command
402 function __construct($user, $other)
404 parent::__construct($user);
405 $this->other = $other;
408 function handle($channel)
410 $recipient = $this->getProfile($this->other);
412 // TRANS: Whois output.
413 // TRANS: %1$s nickname of the queried user, %2$s is their profile URL.
414 $whois = sprintf(_("%1\$s (%2\$s)"), $recipient->nickname,
415 $recipient->profileurl);
416 if ($recipient->fullname) {
417 // TRANS: Whois output. %s is the full name of the queried user.
418 $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname);
420 if ($recipient->location) {
421 // TRANS: Whois output. %s is the location of the queried user.
422 $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location);
424 if ($recipient->homepage) {
425 // TRANS: Whois output. %s is the homepage of the queried user.
426 $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage);
428 if ($recipient->bio) {
429 // TRANS: Whois output. %s is the bio information of the queried user.
430 $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio);
432 $channel->output($this->user, $whois);
436 class MessageCommand extends Command
440 function __construct($user, $other, $text)
442 parent::__construct($user);
443 $this->other = $other;
447 function handle($channel)
450 $other = $this->getUser($this->other);
451 } catch (CommandException $e) {
453 $profile = $this->getProfile($this->other);
454 } catch (CommandException $f) {
457 throw new CommandException(sprintf(_('%s is a remote profile; you can only send direct messages to users on the same server.'), $this->other));
460 $len = mb_strlen($this->text);
463 $channel->error($this->user, _('No content!'));
467 $this->text = common_shorten_links($this->text);
469 if (Message::contentTooLong($this->text)) {
470 // TRANS: Message given if content is too long.
471 // TRANS: %1$d is the maximum number of characters, %2$d is the number of submitted characters.
472 $channel->error($this->user, sprintf(_('Message too long - maximum is %1$d characters, you sent %2$d'),
473 Message::maxContent(), mb_strlen($this->text)));
478 $channel->error($this->user, _('No such user.'));
480 } else if (!$this->user->mutuallySubscribed($other)) {
481 $channel->error($this->user, _('You can\'t send a message to this user.'));
483 } else if ($this->user->id == $other->id) {
484 $channel->error($this->user, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
487 $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
490 // TRANS: Message given have sent a direct message to another user.
491 // TRANS: %s is the name of the other user.
492 $channel->output($this->user, sprintf(_('Direct message to %s sent'), $this->other));
494 $channel->error($this->user, _('Error sending direct message.'));
499 class RepeatCommand extends Command
502 function __construct($user, $other)
504 parent::__construct($user);
505 $this->other = $other;
508 function handle($channel)
510 $notice = $this->getNotice($this->other);
512 if($this->user->id == $notice->profile_id)
514 $channel->error($this->user, _('Cannot repeat your own notice'));
518 if ($this->user->getProfile()->hasRepeated($notice->id)) {
519 $channel->error($this->user, _('Already repeated that notice'));
523 $repeat = $notice->repeat($this->user->id, $channel->source);
527 // TRANS: Message given having repeated a notice from another user.
528 // TRANS: %s is the name of the user for which the notice was repeated.
529 $channel->output($this->user, sprintf(_('Notice from %s repeated'), $recipient->nickname));
531 $channel->error($this->user, _('Error repeating notice.'));
536 class ReplyCommand extends Command
540 function __construct($user, $other, $text)
542 parent::__construct($user);
543 $this->other = $other;
547 function handle($channel)
549 $notice = $this->getNotice($this->other);
550 $recipient = $notice->getProfile();
552 $len = mb_strlen($this->text);
555 $channel->error($this->user, _('No content!'));
559 $this->text = common_shorten_links($this->text);
561 if (Notice::contentTooLong($this->text)) {
562 $channel->error($this->user, sprintf(_('Notice too long - maximum is %d characters, you sent %d'),
563 Notice::maxContent(), mb_strlen($this->text)));
567 $notice = Notice::saveNew($this->user->id, $this->text, $channel->source(),
568 array('reply_to' => $notice->id));
571 $channel->output($this->user, sprintf(_('Reply to %s sent'), $recipient->nickname));
573 $channel->error($this->user, _('Error saving notice.'));
579 class GetCommand extends Command
584 function __construct($user, $other)
586 parent::__construct($user);
587 $this->other = $other;
590 function handle($channel)
592 $target = $this->getProfile($this->other);
594 $notice = $target->getCurrentNotice();
596 $channel->error($this->user, _('User has no last notice'));
599 $notice_content = $notice->content;
601 $channel->output($this->user, $target->nickname . ": " . $notice_content);
605 class SubCommand extends Command
610 function __construct($user, $other)
612 parent::__construct($user);
613 $this->other = $other;
616 function handle($channel)
620 $channel->error($this->user, _('Specify the name of the user to subscribe to'));
624 $target = $this->getProfile($this->other);
626 $remote = Remote_profile::staticGet('id', $target->id);
628 throw new CommandException(_("Can't subscribe to OMB profiles by command."));
632 Subscription::start($this->user->getProfile(),
634 $channel->output($this->user, sprintf(_('Subscribed to %s'), $this->other));
635 } catch (Exception $e) {
636 $channel->error($this->user, $e->getMessage());
641 class UnsubCommand extends Command
646 function __construct($user, $other)
648 parent::__construct($user);
649 $this->other = $other;
652 function handle($channel)
655 $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
659 $target = $this->getProfile($this->other);
662 Subscription::cancel($this->user->getProfile(),
664 $channel->output($this->user, sprintf(_('Unsubscribed from %s'), $this->other));
665 } catch (Exception $e) {
666 $channel->error($this->user, $e->getMessage());
671 class OffCommand extends Command
674 function __construct($user, $other=null)
676 parent::__construct($user);
677 $this->other = $other;
679 function handle($channel)
682 $channel->error($this->user, _("Command not yet implemented."));
684 if ($channel->off($this->user)) {
685 $channel->output($this->user, _('Notification off.'));
687 $channel->error($this->user, _('Can\'t turn off notification.'));
693 class OnCommand extends Command
696 function __construct($user, $other=null)
698 parent::__construct($user);
699 $this->other = $other;
702 function handle($channel)
705 $channel->error($this->user, _("Command not yet implemented."));
707 if ($channel->on($this->user)) {
708 $channel->output($this->user, _('Notification on.'));
710 $channel->error($this->user, _('Can\'t turn on notification.'));
716 class LoginCommand extends Command
718 function handle($channel)
720 $disabled = common_config('logincommand','disabled');
721 $disabled = isset($disabled) && $disabled;
723 $channel->error($this->user, _('Login command is disabled'));
728 $login_token = Login_token::makeNew($this->user);
729 } catch (Exception $e) {
730 $channel->error($this->user, $e->getMessage());
733 $channel->output($this->user,
734 sprintf(_('This link is useable only once, and is good for only 2 minutes: %s'),
735 common_local_url('otp',
736 array('user_id' => $login_token->user_id, 'token' => $login_token->token))));
740 class LoseCommand extends Command
745 function __construct($user, $other)
747 parent::__construct($user);
748 $this->other = $other;
751 function execute($channel)
754 $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
758 $result = Subscription::cancel($this->getProfile($this->other), $this->user->getProfile());
761 $channel->output($this->user, sprintf(_('Unsubscribed %s'), $this->other));
763 $channel->error($this->user, $result);
768 class SubscriptionsCommand extends Command
770 function handle($channel)
772 $profile = $this->user->getSubscriptions(0);
774 while ($profile->fetch()) {
775 $nicknames[]=$profile->nickname;
777 if(count($nicknames)==0){
778 $out=_('You are not subscribed to anyone.');
780 $out = ngettext('You are subscribed to this person:',
781 'You are subscribed to these people:',
784 $out .= implode(', ',$nicknames);
786 $channel->output($this->user,$out);
790 class SubscribersCommand extends Command
792 function handle($channel)
794 $profile = $this->user->getSubscribers();
796 while ($profile->fetch()) {
797 $nicknames[]=$profile->nickname;
799 if(count($nicknames)==0){
800 $out=_('No one is subscribed to you.');
802 $out = ngettext('This person is subscribed to you:',
803 'These people are subscribed to you:',
806 $out .= implode(', ',$nicknames);
808 $channel->output($this->user,$out);
812 class GroupsCommand extends Command
814 function handle($channel)
816 $group = $this->user->getGroups();
818 while ($group->fetch()) {
819 $groups[]=$group->nickname;
821 if(count($groups)==0){
822 $out=_('You are not a member of any groups.');
824 $out = ngettext('You are a member of this group:',
825 'You are a member of these groups:',
827 $out.=implode(', ',$groups);
829 $channel->output($this->user,$out);
833 class HelpCommand extends Command
835 function handle($channel)
837 $channel->output($this->user,
839 "on - turn on notifications\n".
840 "off - turn off notifications\n".
841 "help - show this help\n".
842 "follow <nickname> - subscribe to user\n".
843 "groups - lists the groups you have joined\n".
844 "subscriptions - list the people you follow\n".
845 "subscribers - list the people that follow you\n".
846 "leave <nickname> - unsubscribe from user\n".
847 "d <nickname> <text> - direct message to user\n".
848 "get <nickname> - get last notice from user\n".
849 "whois <nickname> - get profile info on user\n".
850 "lose <nickname> - force user to stop following you\n".
851 "fav <nickname> - add user's last notice as a 'fave'\n".
852 "fav #<notice_id> - add notice with the given id as a 'fave'\n".
853 "repeat #<notice_id> - repeat a notice with a given id\n".
854 "repeat <nickname> - repeat the last notice from user\n".
855 "reply #<notice_id> - reply to notice with a given id\n".
856 "reply <nickname> - reply to the last notice from user\n".
857 "join <group> - join group\n".
858 "login - Get a link to login to the web interface\n".
859 "drop <group> - leave group\n".
860 "stats - get your stats\n".
861 "stop - same as 'off'\n".
862 "quit - same as 'off'\n".
863 "sub <nickname> - same as 'follow'\n".
864 "unsub <nickname> - same as 'leave'\n".
865 "last <nickname> - same as 'get'\n".
866 "on <nickname> - not yet implemented.\n".
867 "off <nickname> - not yet implemented.\n".
868 "nudge <nickname> - remind a user to update.\n".
869 "invite <phone number> - not yet implemented.\n".
870 "track <word> - not yet implemented.\n".
871 "untrack <word> - not yet implemented.\n".
872 "track off - not yet implemented.\n".
873 "untrack all - not yet implemented.\n".
874 "tracks - not yet implemented.\n".
875 "tracking - not yet implemented.\n"));