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 throw new CommandException(sprintf(_('Could not find a user with nickname %s'), $arg));
131 * Get a local user by name
133 * @throws CommandException
135 function getUser($arg)
138 if (Event::handle('StartCommandGetUser', array($this, $arg, &$user))) {
139 $user = User::staticGet('nickname', $arg);
141 Event::handle('EndCommandGetUser', array($this, $arg, &$user));
143 throw new CommandException(sprintf(_('Could not find a local user with nickname %s'),
150 * Get a local or remote group by name.
152 * @throws CommandException
154 function getGroup($arg)
157 if (Event::handle('StartCommandGetGroup', array($this, $arg, &$group))) {
158 $group = User_group::getForNickname($arg, $this->user->getProfile());
160 Event::handle('EndCommandGetGroup', array($this, $arg, &$group));
162 throw new CommandException(_('No such group.'));
168 class CommandException extends Exception
172 class UnimplementedCommand extends Command
174 function handle($channel)
176 $channel->error($this->user, _("Sorry, this command is not yet implemented."));
180 class TrackingCommand extends UnimplementedCommand
184 class TrackOffCommand extends UnimplementedCommand
188 class TrackCommand extends UnimplementedCommand
191 function __construct($user, $word)
193 parent::__construct($user);
198 class UntrackCommand extends UnimplementedCommand
201 function __construct($user, $word)
203 parent::__construct($user);
208 class NudgeCommand extends Command
211 function __construct($user, $other)
213 parent::__construct($user);
214 $this->other = $other;
217 function handle($channel)
219 $recipient = $this->getUser($this->other);
220 if ($recipient->id == $this->user->id) {
221 throw new CommandException(_('It does not make a lot of sense to nudge yourself!'));
223 if ($recipient->email && $recipient->emailnotifynudge) {
224 mail_notify_nudge($this->user, $recipient);
227 // XXX: notify by SMS
228 $channel->output($this->user, sprintf(_('Nudge sent to %s'),
229 $recipient->nickname));
234 class InviteCommand extends UnimplementedCommand
237 function __construct($user, $other)
239 parent::__construct($user);
240 $this->other = $other;
244 class StatsCommand extends Command
246 function handle($channel)
248 $profile = $this->user->getProfile();
250 $subs_count = $profile->subscriptionCount();
251 $subbed_count = $profile->subscriberCount();
252 $notice_count = $profile->noticeCount();
254 $channel->output($this->user, sprintf(_("Subscriptions: %1\$s\n".
255 "Subscribers: %2\$s\n".
263 class FavCommand extends Command
267 function __construct($user, $other)
269 parent::__construct($user);
270 $this->other = $other;
273 function handle($channel)
275 $notice = $this->getNotice($this->other);
276 $fave = Fave::addNew($this->user->getProfile(), $notice);
279 $channel->error($this->user, _('Could not create favorite.'));
283 // @fixme favorite notification should be triggered
286 $other = User::staticGet('id', $notice->profile_id);
288 if ($other && $other->id != $user->id) {
289 if ($other->email && $other->emailnotifyfav) {
290 mail_notify_fave($other, $this->user, $notice);
294 $this->user->blowFavesCache();
296 $channel->output($this->user, _('Notice marked as fave.'));
301 class JoinCommand extends Command
305 function __construct($user, $other)
307 parent::__construct($user);
308 $this->other = $other;
311 function handle($channel)
313 $group = $this->getGroup($this->other);
316 if ($cur->isMember($group)) {
317 $channel->error($cur, _('You are already a member of that group'));
320 if (Group_block::isBlocked($group, $cur->getProfile())) {
321 $channel->error($cur, _('You have been blocked from that group by the admin.'));
326 if (Event::handle('StartJoinGroup', array($group, $cur))) {
327 Group_member::join($group->id, $cur->id);
328 Event::handle('EndJoinGroup', array($group, $cur));
330 } catch (Exception $e) {
331 $channel->error($cur, sprintf(_('Could not join user %s to group %s'),
332 $cur->nickname, $group->nickname));
336 $channel->output($cur, sprintf(_('%s joined group %s'),
342 class DropCommand extends Command
346 function __construct($user, $other)
348 parent::__construct($user);
349 $this->other = $other;
352 function handle($channel)
354 $group = $this->getGroup($this->other);
358 $channel->error($cur, _('No such group.'));
362 if (!$cur->isMember($group)) {
363 $channel->error($cur, _('You are not a member of that group.'));
368 if (Event::handle('StartLeaveGroup', array($group, $cur))) {
369 Group_member::leave($group->id, $cur->id);
370 Event::handle('EndLeaveGroup', array($group, $cur));
372 } catch (Exception $e) {
373 $channel->error($cur, sprintf(_('Could not remove user %s to group %s'),
374 $cur->nickname, $group->nickname));
378 $channel->output($cur, sprintf(_('%s left group %s'),
385 class WhoisCommand extends Command
388 function __construct($user, $other)
390 parent::__construct($user);
391 $this->other = $other;
394 function handle($channel)
396 $recipient = $this->getProfile($this->other);
398 $whois = sprintf(_("%1\$s (%2\$s)"), $recipient->nickname,
399 $recipient->profileurl);
400 if ($recipient->fullname) {
401 $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname);
403 if ($recipient->location) {
404 $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location);
406 if ($recipient->homepage) {
407 $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage);
409 if ($recipient->bio) {
410 $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio);
412 $channel->output($this->user, $whois);
416 class MessageCommand extends Command
420 function __construct($user, $other, $text)
422 parent::__construct($user);
423 $this->other = $other;
427 function handle($channel)
430 $other = $this->getUser($this->other);
431 } catch (CommandException $e) {
433 $profile = $this->getProfile($this->other);
434 } catch (CommandException $f) {
437 throw new CommandException(sprintf(_('%s is a remote profile; you can only send direct messages to users on the same server.'), $this->other));
440 $len = mb_strlen($this->text);
443 $channel->error($this->user, _('No content!'));
447 $this->text = common_shorten_links($this->text);
449 if (Message::contentTooLong($this->text)) {
450 $channel->error($this->user, sprintf(_('Message too long - maximum is %d characters, you sent %d'),
451 Message::maxContent(), mb_strlen($this->text)));
456 $channel->error($this->user, _('No such user.'));
458 } else if (!$this->user->mutuallySubscribed($other)) {
459 $channel->error($this->user, _('You can\'t send a message to this user.'));
461 } else if ($this->user->id == $other->id) {
462 $channel->error($this->user, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
465 $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
468 $channel->output($this->user, sprintf(_('Direct message to %s sent'), $this->other));
470 $channel->error($this->user, _('Error sending direct message.'));
475 class RepeatCommand extends Command
478 function __construct($user, $other)
480 parent::__construct($user);
481 $this->other = $other;
484 function handle($channel)
486 $notice = $this->getNotice($this->other);
488 if($this->user->id == $notice->profile_id)
490 $channel->error($this->user, _('Cannot repeat your own notice'));
494 if ($this->user->getProfile()->hasRepeated($notice->id)) {
495 $channel->error($this->user, _('Already repeated that notice'));
499 $repeat = $notice->repeat($this->user->id, $channel->source);
503 $channel->output($this->user, sprintf(_('Notice from %s repeated'), $recipient->nickname));
505 $channel->error($this->user, _('Error repeating notice.'));
510 class ReplyCommand extends Command
514 function __construct($user, $other, $text)
516 parent::__construct($user);
517 $this->other = $other;
521 function handle($channel)
523 $notice = $this->getNotice($this->other);
524 $recipient = $notice->getProfile();
526 $len = mb_strlen($this->text);
529 $channel->error($this->user, _('No content!'));
533 $this->text = common_shorten_links($this->text);
535 if (Notice::contentTooLong($this->text)) {
536 $channel->error($this->user, sprintf(_('Notice too long - maximum is %d characters, you sent %d'),
537 Notice::maxContent(), mb_strlen($this->text)));
541 $notice = Notice::saveNew($this->user->id, $this->text, $channel->source(),
542 array('reply_to' => $notice->id));
545 $channel->output($this->user, sprintf(_('Reply to %s sent'), $recipient->nickname));
547 $channel->error($this->user, _('Error saving notice.'));
553 class GetCommand extends Command
558 function __construct($user, $other)
560 parent::__construct($user);
561 $this->other = $other;
564 function handle($channel)
566 $target = $this->getProfile($this->other);
568 $notice = $target->getCurrentNotice();
570 $channel->error($this->user, _('User has no last notice'));
573 $notice_content = $notice->content;
575 $channel->output($this->user, $target->nickname . ": " . $notice_content);
579 class SubCommand extends Command
584 function __construct($user, $other)
586 parent::__construct($user);
587 $this->other = $other;
590 function handle($channel)
594 $channel->error($this->user, _('Specify the name of the user to subscribe to'));
598 $target = $this->getProfile($this->other);
600 $remote = Remote_profile::staticGet('id', $target->id);
602 throw new CommandException(_("Can't subscribe to OMB profiles by command."));
606 Subscription::start($this->user->getProfile(),
608 $channel->output($this->user, sprintf(_('Subscribed to %s'), $this->other));
609 } catch (Exception $e) {
610 $channel->error($this->user, $e->getMessage());
615 class UnsubCommand extends Command
620 function __construct($user, $other)
622 parent::__construct($user);
623 $this->other = $other;
626 function handle($channel)
629 $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
633 $target = $this->getProfile($this->other);
636 Subscription::cancel($this->user->getProfile(),
638 $channel->output($this->user, sprintf(_('Unsubscribed from %s'), $this->other));
639 } catch (Exception $e) {
640 $channel->error($this->user, $e->getMessage());
645 class OffCommand extends Command
648 function __construct($user, $other=null)
650 parent::__construct($user);
651 $this->other = $other;
653 function handle($channel)
656 $channel->error($this->user, _("Command not yet implemented."));
658 if ($channel->off($this->user)) {
659 $channel->output($this->user, _('Notification off.'));
661 $channel->error($this->user, _('Can\'t turn off notification.'));
667 class OnCommand extends Command
670 function __construct($user, $other=null)
672 parent::__construct($user);
673 $this->other = $other;
676 function handle($channel)
679 $channel->error($this->user, _("Command not yet implemented."));
681 if ($channel->on($this->user)) {
682 $channel->output($this->user, _('Notification on.'));
684 $channel->error($this->user, _('Can\'t turn on notification.'));
690 class LoginCommand extends Command
692 function handle($channel)
694 $disabled = common_config('logincommand','disabled');
695 $disabled = isset($disabled) && $disabled;
697 $channel->error($this->user, _('Login command is disabled'));
702 $login_token = Login_token::makeNew($this->user);
703 } catch (Exception $e) {
704 $channel->error($this->user, $e->getMessage());
707 $channel->output($this->user,
708 sprintf(_('This link is useable only once, and is good for only 2 minutes: %s'),
709 common_local_url('otp',
710 array('user_id' => $login_token->user_id, 'token' => $login_token->token))));
714 class LoseCommand extends Command
719 function __construct($user, $other)
721 parent::__construct($user);
722 $this->other = $other;
725 function execute($channel)
728 $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
732 $result = Subscription::cancel($this->getProfile($this->other), $this->user->getProfile());
735 $channel->output($this->user, sprintf(_('Unsubscribed %s'), $this->other));
737 $channel->error($this->user, $result);
742 class SubscriptionsCommand extends Command
744 function handle($channel)
746 $profile = $this->user->getSubscriptions(0);
748 while ($profile->fetch()) {
749 $nicknames[]=$profile->nickname;
751 if(count($nicknames)==0){
752 $out=_('You are not subscribed to anyone.');
754 $out = ngettext('You are subscribed to this person:',
755 'You are subscribed to these people:',
758 $out .= implode(', ',$nicknames);
760 $channel->output($this->user,$out);
764 class SubscribersCommand extends Command
766 function handle($channel)
768 $profile = $this->user->getSubscribers();
770 while ($profile->fetch()) {
771 $nicknames[]=$profile->nickname;
773 if(count($nicknames)==0){
774 $out=_('No one is subscribed to you.');
776 $out = ngettext('This person is subscribed to you:',
777 'These people are subscribed to you:',
780 $out .= implode(', ',$nicknames);
782 $channel->output($this->user,$out);
786 class GroupsCommand extends Command
788 function handle($channel)
790 $group = $this->user->getGroups();
792 while ($group->fetch()) {
793 $groups[]=$group->nickname;
795 if(count($groups)==0){
796 $out=_('You are not a member of any groups.');
798 $out = ngettext('You are a member of this group:',
799 'You are a member of these groups:',
801 $out.=implode(', ',$groups);
803 $channel->output($this->user,$out);
807 class HelpCommand extends Command
809 function handle($channel)
811 $channel->output($this->user,
813 "on - turn on notifications\n".
814 "off - turn off notifications\n".
815 "help - show this help\n".
816 "follow <nickname> - subscribe to user\n".
817 "groups - lists the groups you have joined\n".
818 "subscriptions - list the people you follow\n".
819 "subscribers - list the people that follow you\n".
820 "leave <nickname> - unsubscribe from user\n".
821 "d <nickname> <text> - direct message to user\n".
822 "get <nickname> - get last notice from user\n".
823 "whois <nickname> - get profile info on user\n".
824 "lose <nickname> - force user to stop following you\n".
825 "fav <nickname> - add user's last notice as a 'fave'\n".
826 "fav #<notice_id> - add notice with the given id as a 'fave'\n".
827 "repeat #<notice_id> - repeat a notice with a given id\n".
828 "repeat <nickname> - repeat the last notice from user\n".
829 "reply #<notice_id> - reply to notice with a given id\n".
830 "reply <nickname> - reply to the last notice from user\n".
831 "join <group> - join group\n".
832 "login - Get a link to login to the web interface\n".
833 "drop <group> - leave group\n".
834 "stats - get your stats\n".
835 "stop - same as 'off'\n".
836 "quit - same as 'off'\n".
837 "sub <nickname> - same as 'follow'\n".
838 "unsub <nickname> - same as 'leave'\n".
839 "last <nickname> - same as 'get'\n".
840 "on <nickname> - not yet implemented.\n".
841 "off <nickname> - not yet implemented.\n".
842 "nudge <nickname> - remind a user to update.\n".
843 "invite <phone number> - not yet implemented.\n".
844 "track <word> - not yet implemented.\n".
845 "untrack <word> - not yet implemented.\n".
846 "track off - not yet implemented.\n".
847 "untrack all - not yet implemented.\n".
848 "tracks - not yet implemented.\n".
849 "tracking - not yet implemented.\n"));