3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, 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)
34 function execute($channel)
40 class UnimplementedCommand extends Command
42 function execute($channel)
44 $channel->error($this->user, _("Sorry, this command is not yet implemented."));
48 class TrackingCommand extends UnimplementedCommand
52 class TrackOffCommand extends UnimplementedCommand
56 class TrackCommand extends UnimplementedCommand
59 function __construct($user, $word)
61 parent::__construct($user);
66 class UntrackCommand extends UnimplementedCommand
69 function __construct($user, $word)
71 parent::__construct($user);
76 class NudgeCommand extends Command
79 function __construct($user, $other)
81 parent::__construct($user);
82 $this->other = $other;
84 function execute($channel)
86 $recipient = User::staticGet('nickname', $this->other);
88 $channel->error($this->user, sprintf(_('Could not find a user with nickname %s'),
91 if ($recipient->id == $this->user->id) {
92 $channel->error($this->user, _('It does not make a lot of sense to nudge yourself!'));
94 if ($recipient->email && $recipient->emailnotifynudge) {
95 mail_notify_nudge($this->user, $recipient);
99 $channel->output($this->user, sprintf(_('Nudge sent to %s'),
100 $recipient->nickname));
106 class InviteCommand extends UnimplementedCommand
109 function __construct($user, $other)
111 parent::__construct($user);
112 $this->other = $other;
116 class StatsCommand extends Command
118 function execute($channel)
120 $profile = $this->user->getProfile();
122 $subs_count = $profile->subscriptionCount();
123 $subbed_count = $profile->subscriberCount();
124 $notice_count = $profile->noticeCount();
126 $channel->output($this->user, sprintf(_("Subscriptions: %1\$s\n".
127 "Subscribers: %2\$s\n".
135 class FavCommand extends Command
139 function __construct($user, $other)
141 parent::__construct($user);
142 $this->other = $other;
145 function execute($channel)
147 if(substr($this->other,0,1)=='#'){
148 //favoriting a specific notice_id
150 $notice = Notice::staticGet(substr($this->other,1));
152 $channel->error($this->user, _('Notice with that id does not exist'));
155 $recipient = $notice->getProfile();
157 //favoriting a given user's last notice
160 common_relative_profile($this->user, common_canonical_nickname($this->other));
163 $channel->error($this->user, _('No such user.'));
166 $notice = $recipient->getCurrentNotice();
168 $channel->error($this->user, _('User has no last notice'));
173 $fave = Fave::addNew($this->user, $notice);
176 $channel->error($this->user, _('Could not create favorite.'));
180 $other = User::staticGet('id', $recipient->id);
182 if ($other && $other->id != $user->id) {
183 if ($other->email && $other->emailnotifyfav) {
184 mail_notify_fave($other, $this->user, $notice);
188 $this->user->blowFavesCache();
190 $channel->output($this->user, _('Notice marked as fave.'));
194 class JoinCommand extends Command
198 function __construct($user, $other)
200 parent::__construct($user);
201 $this->other = $other;
204 function execute($channel)
207 $nickname = common_canonical_nickname($this->other);
208 $group = User_group::staticGet('nickname', $nickname);
212 $channel->error($cur, _('No such group.'));
216 if ($cur->isMember($group)) {
217 $channel->error($cur, _('You are already a member of that group'));
220 if (Group_block::isBlocked($group, $cur->getProfile())) {
221 $channel->error($cur, _('You have been blocked from that group by the admin.'));
226 if (Event::handle('StartJoinGroup', array($group, $cur))) {
227 Group_member::join($group->id, $cur->id);
228 Event::handle('EndJoinGroup', array($group, $cur));
230 } catch (Exception $e) {
231 $channel->error($cur, sprintf(_('Could not join user %s to group %s'),
232 $cur->nickname, $group->nickname));
236 $channel->output($cur, sprintf(_('%s joined group %s'),
242 class DropCommand extends Command
246 function __construct($user, $other)
248 parent::__construct($user);
249 $this->other = $other;
252 function execute($channel)
255 $nickname = common_canonical_nickname($this->other);
256 $group = User_group::staticGet('nickname', $nickname);
260 $channel->error($cur, _('No such group.'));
264 if (!$cur->isMember($group)) {
265 $channel->error($cur, _('You are not a member of that group.'));
270 if (Event::handle('StartLeaveGroup', array($group, $cur))) {
271 Group_member::leave($group->id, $cur->id);
272 Event::handle('EndLeaveGroup', array($group, $cur));
274 } catch (Exception $e) {
275 $channel->error($cur, sprintf(_('Could not remove user %s to group %s'),
276 $cur->nickname, $group->nickname));
280 $channel->output($cur, sprintf(_('%s left group %s'),
287 class WhoisCommand extends Command
290 function __construct($user, $other)
292 parent::__construct($user);
293 $this->other = $other;
296 function execute($channel)
299 common_relative_profile($this->user, common_canonical_nickname($this->other));
302 $channel->error($this->user, _('No such user.'));
306 $whois = sprintf(_("%1\$s (%2\$s)"), $recipient->nickname,
307 $recipient->profileurl);
308 if ($recipient->fullname) {
309 $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname);
311 if ($recipient->location) {
312 $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location);
314 if ($recipient->homepage) {
315 $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage);
317 if ($recipient->bio) {
318 $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio);
320 $channel->output($this->user, $whois);
324 class MessageCommand extends Command
328 function __construct($user, $other, $text)
330 parent::__construct($user);
331 $this->other = $other;
335 function execute($channel)
337 $other = User::staticGet('nickname', common_canonical_nickname($this->other));
339 $len = mb_strlen($this->text);
342 $channel->error($this->user, _('No content!'));
346 $this->text = common_shorten_links($this->text);
348 if (Message::contentTooLong($this->text)) {
349 $channel->error($this->user, sprintf(_('Message too long - maximum is %d characters, you sent %d'),
350 Message::maxContent(), mb_strlen($this->text)));
355 $channel->error($this->user, _('No such user.'));
357 } else if (!$this->user->mutuallySubscribed($other)) {
358 $channel->error($this->user, _('You can\'t send a message to this user.'));
360 } else if ($this->user->id == $other->id) {
361 $channel->error($this->user, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
364 $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
367 $channel->output($this->user, sprintf(_('Direct message to %s sent'), $this->other));
369 $channel->error($this->user, _('Error sending direct message.'));
374 class RepeatCommand extends Command
377 function __construct($user, $other)
379 parent::__construct($user);
380 $this->other = $other;
383 function execute($channel)
385 if(substr($this->other,0,1)=='#'){
386 //repeating a specific notice_id
388 $notice = Notice::staticGet(substr($this->other,1));
390 $channel->error($this->user, _('Notice with that id does not exist'));
393 $recipient = $notice->getProfile();
395 //repeating a given user's last notice
398 common_relative_profile($this->user, common_canonical_nickname($this->other));
401 $channel->error($this->user, _('No such user.'));
404 $notice = $recipient->getCurrentNotice();
406 $channel->error($this->user, _('User has no last notice'));
411 if($this->user->id == $notice->profile_id)
413 $channel->error($this->user, _('Cannot repeat your own notice'));
417 if ($recipient->hasRepeated($notice->id)) {
418 $channel->error($this->user, _('Already repeated that notice'));
422 $repeat = $notice->repeat($this->user->id, $channel->source);
426 $channel->output($this->user, sprintf(_('Notice from %s repeated'), $recipient->nickname));
428 $channel->error($this->user, _('Error repeating notice.'));
433 class ReplyCommand extends Command
437 function __construct($user, $other, $text)
439 parent::__construct($user);
440 $this->other = $other;
444 function execute($channel)
446 if(substr($this->other,0,1)=='#'){
447 //replying to a specific notice_id
449 $notice = Notice::staticGet(substr($this->other,1));
451 $channel->error($this->user, _('Notice with that id does not exist'));
454 $recipient = $notice->getProfile();
456 //replying to a given user's last notice
459 common_relative_profile($this->user, common_canonical_nickname($this->other));
462 $channel->error($this->user, _('No such user.'));
465 $notice = $recipient->getCurrentNotice();
467 $channel->error($this->user, _('User has no last notice'));
472 $len = mb_strlen($this->text);
475 $channel->error($this->user, _('No content!'));
479 $this->text = common_shorten_links($this->text);
481 if (Notice::contentTooLong($this->text)) {
482 $channel->error($this->user, sprintf(_('Notice too long - maximum is %d characters, you sent %d'),
483 Notice::maxContent(), mb_strlen($this->text)));
487 $notice = Notice::saveNew($this->user->id, $this->text, $channel->source(),
488 array('reply_to' => $notice->id));
491 $channel->output($this->user, sprintf(_('Reply to %s sent'), $recipient->nickname));
493 $channel->error($this->user, _('Error saving notice.'));
499 class GetCommand extends Command
504 function __construct($user, $other)
506 parent::__construct($user);
507 $this->other = $other;
510 function execute($channel)
512 $target_nickname = common_canonical_nickname($this->other);
515 common_relative_profile($this->user, $target_nickname);
518 $channel->error($this->user, _('No such user.'));
521 $notice = $target->getCurrentNotice();
523 $channel->error($this->user, _('User has no last notice'));
526 $notice_content = $notice->content;
528 $channel->output($this->user, $target_nickname . ": " . $notice_content);
532 class SubCommand extends Command
537 function __construct($user, $other)
539 parent::__construct($user);
540 $this->other = $other;
543 function execute($channel)
547 $channel->error($this->user, _('Specify the name of the user to subscribe to'));
551 $otherUser = User::staticGet('nickname', $this->other);
553 if (empty($otherUser)) {
554 $channel->error($this->user, _('No such user'));
559 Subscription::start($this->user->getProfile(),
560 $otherUser->getProfile());
561 $channel->output($this->user, sprintf(_('Subscribed to %s'), $this->other));
562 } catch (Exception $e) {
563 $channel->error($this->user, $e->getMessage());
568 class UnsubCommand extends Command
573 function __construct($user, $other)
575 parent::__construct($user);
576 $this->other = $other;
579 function execute($channel)
582 $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
586 $otherUser = User::staticGet('nickname', $this->other);
588 if (empty($otherUser)) {
589 $channel->error($this->user, _('No such user'));
593 Subscription::cancel($this->user->getProfile(),
594 $otherUser->getProfile());
595 $channel->output($this->user, sprintf(_('Unsubscribed from %s'), $this->other));
596 } catch (Exception $e) {
597 $channel->error($this->user, $e->getMessage());
602 class OffCommand extends Command
605 function __construct($user, $other=null)
607 parent::__construct($user);
608 $this->other = $other;
610 function execute($channel)
613 $channel->error($this->user, _("Command not yet implemented."));
615 if ($channel->off($this->user)) {
616 $channel->output($this->user, _('Notification off.'));
618 $channel->error($this->user, _('Can\'t turn off notification.'));
624 class OnCommand extends Command
627 function __construct($user, $other=null)
629 parent::__construct($user);
630 $this->other = $other;
633 function execute($channel)
636 $channel->error($this->user, _("Command not yet implemented."));
638 if ($channel->on($this->user)) {
639 $channel->output($this->user, _('Notification on.'));
641 $channel->error($this->user, _('Can\'t turn on notification.'));
647 class LoginCommand extends Command
649 function execute($channel)
651 $disabled = common_config('logincommand','disabled');
652 $disabled = isset($disabled) && $disabled;
654 $channel->error($this->user, _('Login command is disabled'));
659 $login_token = Login_token::makeNew($this->user);
660 } catch (Exception $e) {
661 $channel->error($this->user, $e->getMessage());
664 $channel->output($this->user,
665 sprintf(_('This link is useable only once, and is good for only 2 minutes: %s'),
666 common_local_url('otp',
667 array('user_id' => $login_token->user_id, 'token' => $login_token->token))));
671 class SubscriptionsCommand extends Command
673 function execute($channel)
675 $profile = $this->user->getSubscriptions(0);
677 while ($profile->fetch()) {
678 $nicknames[]=$profile->nickname;
680 if(count($nicknames)==0){
681 $out=_('You are not subscribed to anyone.');
683 $out = ngettext('You are subscribed to this person:',
684 'You are subscribed to these people:',
687 $out .= implode(', ',$nicknames);
689 $channel->output($this->user,$out);
693 class SubscribersCommand extends Command
695 function execute($channel)
697 $profile = $this->user->getSubscribers();
699 while ($profile->fetch()) {
700 $nicknames[]=$profile->nickname;
702 if(count($nicknames)==0){
703 $out=_('No one is subscribed to you.');
705 $out = ngettext('This person is subscribed to you:',
706 'These people are subscribed to you:',
709 $out .= implode(', ',$nicknames);
711 $channel->output($this->user,$out);
715 class GroupsCommand extends Command
717 function execute($channel)
719 $group = $this->user->getGroups();
721 while ($group->fetch()) {
722 $groups[]=$group->nickname;
724 if(count($groups)==0){
725 $out=_('You are not a member of any groups.');
727 $out = ngettext('You are a member of this group:',
728 'You are a member of these groups:',
730 $out.=implode(', ',$groups);
732 $channel->output($this->user,$out);
736 class HelpCommand extends Command
738 function execute($channel)
740 $channel->output($this->user,
742 "on - turn on notifications\n".
743 "off - turn off notifications\n".
744 "help - show this help\n".
745 "follow <nickname> - subscribe to user\n".
746 "groups - lists the groups you have joined\n".
747 "subscriptions - list the people you follow\n".
748 "subscribers - list the people that follow you\n".
749 "leave <nickname> - unsubscribe from user\n".
750 "d <nickname> <text> - direct message to user\n".
751 "get <nickname> - get last notice from user\n".
752 "whois <nickname> - get profile info on user\n".
753 "fav <nickname> - add user's last notice as a 'fave'\n".
754 "fav #<notice_id> - add notice with the given id as a 'fave'\n".
755 "repeat #<notice_id> - repeat a notice with a given id\n".
756 "repeat <nickname> - repeat the last notice from user\n".
757 "reply #<notice_id> - reply to notice with a given id\n".
758 "reply <nickname> - reply to the last notice from user\n".
759 "join <group> - join group\n".
760 "login - Get a link to login to the web interface\n".
761 "drop <group> - leave group\n".
762 "stats - get your stats\n".
763 "stop - same as 'off'\n".
764 "quit - same as 'off'\n".
765 "sub <nickname> - same as 'follow'\n".
766 "unsub <nickname> - same as 'leave'\n".
767 "last <nickname> - same as 'get'\n".
768 "on <nickname> - not yet implemented.\n".
769 "off <nickname> - not yet implemented.\n".
770 "nudge <nickname> - remind a user to update.\n".
771 "invite <phone number> - not yet implemented.\n".
772 "track <word> - not yet implemented.\n".
773 "untrack <word> - not yet implemented.\n".
774 "track off - not yet implemented.\n".
775 "untrack all - not yet implemented.\n".
776 "tracks - not yet implemented.\n".
777 "tracking - not yet implemented.\n"));