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.'));
225 $member = new Group_member();
227 $member->group_id = $group->id;
228 $member->profile_id = $cur->id;
229 $member->created = common_sql_now();
231 $result = $member->insert();
233 common_log_db_error($member, 'INSERT', __FILE__);
234 $channel->error($cur, sprintf(_('Could not join user %s to group %s'),
235 $cur->nickname, $group->nickname));
239 $channel->output($cur, sprintf(_('%s joined group %s'),
245 class DropCommand extends Command
249 function __construct($user, $other)
251 parent::__construct($user);
252 $this->other = $other;
255 function execute($channel)
258 $nickname = common_canonical_nickname($this->other);
259 $group = User_group::staticGet('nickname', $nickname);
263 $channel->error($cur, _('No such group.'));
267 if (!$cur->isMember($group)) {
268 $channel->error($cur, _('You are not a member of that group.'));
272 $member = new Group_member();
274 $member->group_id = $group->id;
275 $member->profile_id = $cur->id;
277 if (!$member->find(true)) {
278 $channel->error($cur,_('Could not find membership record.'));
281 $result = $member->delete();
283 common_log_db_error($member, 'INSERT', __FILE__);
284 $channel->error($cur, sprintf(_('Could not remove user %s to group %s'),
285 $cur->nickname, $group->nickname));
289 $channel->output($cur, sprintf(_('%s left group %s'),
296 class WhoisCommand extends Command
299 function __construct($user, $other)
301 parent::__construct($user);
302 $this->other = $other;
305 function execute($channel)
308 common_relative_profile($this->user, common_canonical_nickname($this->other));
311 $channel->error($this->user, _('No such user.'));
315 $whois = sprintf(_("%1\$s (%2\$s)"), $recipient->nickname,
316 $recipient->profileurl);
317 if ($recipient->fullname) {
318 $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname);
320 if ($recipient->location) {
321 $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location);
323 if ($recipient->homepage) {
324 $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage);
326 if ($recipient->bio) {
327 $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio);
329 $channel->output($this->user, $whois);
333 class MessageCommand extends Command
337 function __construct($user, $other, $text)
339 parent::__construct($user);
340 $this->other = $other;
344 function execute($channel)
346 $other = User::staticGet('nickname', common_canonical_nickname($this->other));
348 $len = mb_strlen($this->text);
351 $channel->error($this->user, _('No content!'));
355 $this->text = common_shorten_links($this->text);
357 if (Message::contentTooLong($this->text)) {
358 $channel->error($this->user, sprintf(_('Message too long - maximum is %d characters, you sent %d'),
359 Message::maxContent(), mb_strlen($this->text)));
364 $channel->error($this->user, _('No such user.'));
366 } else if (!$this->user->mutuallySubscribed($other)) {
367 $channel->error($this->user, _('You can\'t send a message to this user.'));
369 } else if ($this->user->id == $other->id) {
370 $channel->error($this->user, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
373 $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
375 $channel->output($this->user, sprintf(_('Direct message to %s sent'), $this->other));
377 $channel->error($this->user, _('Error sending direct message.'));
382 class ReplyCommand extends Command
386 function __construct($user, $other, $text)
388 parent::__construct($user);
389 $this->other = $other;
393 function execute($channel)
395 if(substr($this->other,0,1)=='#'){
396 //replying to a specific notice_id
398 $notice = Notice::staticGet(substr($this->other,1));
400 $channel->error($this->user, _('Notice with that id does not exist'));
403 $recipient = $notice->getProfile();
405 //replying to a given user's last notice
408 common_relative_profile($this->user, common_canonical_nickname($this->other));
411 $channel->error($this->user, _('No such user.'));
414 $notice = $recipient->getCurrentNotice();
416 $channel->error($this->user, _('User has no last notice'));
421 $len = mb_strlen($this->text);
424 $channel->error($this->user, _('No content!'));
428 $this->text = common_shorten_links($this->text);
430 if (Notice::contentTooLong($this->text)) {
431 $channel->error($this->user, sprintf(_('Notice too long - maximum is %d characters, you sent %d'),
432 Notice::maxContent(), mb_strlen($this->text)));
436 $notice = Notice::saveNew($this->user->id, $this->text, $channel->source(),
437 array('reply_to' => $notice->id));
440 $channel->output($this->user, sprintf(_('Reply to %s sent'), $recipient->nickname));
442 $channel->error($this->user, _('Error saving notice.'));
444 common_broadcast_notice($notice);
448 class GetCommand extends Command
453 function __construct($user, $other)
455 parent::__construct($user);
456 $this->other = $other;
459 function execute($channel)
461 $target_nickname = common_canonical_nickname($this->other);
464 common_relative_profile($this->user, $target_nickname);
467 $channel->error($this->user, _('No such user.'));
470 $notice = $target->getCurrentNotice();
472 $channel->error($this->user, _('User has no last notice'));
475 $notice_content = $notice->content;
477 $channel->output($this->user, $target_nickname . ": " . $notice_content);
481 class SubCommand extends Command
486 function __construct($user, $other)
488 parent::__construct($user);
489 $this->other = $other;
492 function execute($channel)
496 $channel->error($this->user, _('Specify the name of the user to subscribe to'));
500 $result = subs_subscribe_user($this->user, $this->other);
502 if ($result == 'true') {
503 $channel->output($this->user, sprintf(_('Subscribed to %s'), $this->other));
505 $channel->error($this->user, $result);
510 class UnsubCommand extends Command
515 function __construct($user, $other)
517 parent::__construct($user);
518 $this->other = $other;
521 function execute($channel)
524 $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
528 $result=subs_unsubscribe_user($this->user, $this->other);
531 $channel->output($this->user, sprintf(_('Unsubscribed from %s'), $this->other));
533 $channel->error($this->user, $result);
538 class OffCommand extends Command
541 function __construct($user, $other=null)
543 parent::__construct($user);
544 $this->other = $other;
546 function execute($channel)
549 $channel->error($this->user, _("Command not yet implemented."));
551 if ($channel->off($this->user)) {
552 $channel->output($this->user, _('Notification off.'));
554 $channel->error($this->user, _('Can\'t turn off notification.'));
560 class OnCommand extends Command
563 function __construct($user, $other=null)
565 parent::__construct($user);
566 $this->other = $other;
569 function execute($channel)
572 $channel->error($this->user, _("Command not yet implemented."));
574 if ($channel->on($this->user)) {
575 $channel->output($this->user, _('Notification on.'));
577 $channel->error($this->user, _('Can\'t turn on notification.'));
583 class LoginCommand extends Command
585 function execute($channel)
587 $disabled = common_config('logincommand','disabled');
588 $disabled = isset($disabled) && $disabled;
590 $channel->error($this->user, _('Login command is disabled'));
593 $login_token = Login_token::staticGet('user_id',$this->user->id);
595 $login_token->delete();
597 $login_token = new Login_token();
598 $login_token->user_id = $this->user->id;
599 $login_token->token = common_good_rand(16);
600 $login_token->created = common_sql_now();
601 $result = $login_token->insert();
603 common_log_db_error($login_token, 'INSERT', __FILE__);
604 $channel->error($this->user, sprintf(_('Could not create login token for %s'),
605 $this->user->nickname));
608 $channel->output($this->user,
609 sprintf(_('This link is useable only once, and is good for only 2 minutes: %s'),
610 common_local_url('login',
611 array('user_id'=>$login_token->user_id, 'token'=>$login_token->token))));
615 class SubscriptionsCommand extends Command
617 function execute($channel)
619 $profile = $this->user->getSubscriptions(0);
621 while ($profile->fetch()) {
622 $nicknames[]=$profile->nickname;
624 if(count($nicknames)==0){
625 $out=_('You are not subscribed to anyone.');
627 $out = ngettext('You are subscribed to this person:',
628 'You are subscribed to these people:',
631 $out .= implode(', ',$nicknames);
633 $channel->output($this->user,$out);
637 class SubscribersCommand extends Command
639 function execute($channel)
641 $profile = $this->user->getSubscribers();
643 while ($profile->fetch()) {
644 $nicknames[]=$profile->nickname;
646 if(count($nicknames)==0){
647 $out=_('No one is subscribed to you.');
649 $out = ngettext('This person is subscribed to you:',
650 'These people are subscribed to you:',
653 $out .= implode(', ',$nicknames);
655 $channel->output($this->user,$out);
659 class GroupsCommand extends Command
661 function execute($channel)
663 $group = $this->user->getGroups();
665 while ($group->fetch()) {
666 $groups[]=$group->nickname;
668 if(count($groups)==0){
669 $out=_('You are not a member of any groups.');
671 $out = ngettext('You are a member of this group:',
672 'You are a member of these groups:',
674 $out.=implode(', ',$groups);
676 $channel->output($this->user,$out);
680 class HelpCommand extends Command
682 function execute($channel)
684 $channel->output($this->user,
686 "on - turn on notifications\n".
687 "off - turn off notifications\n".
688 "help - show this help\n".
689 "follow <nickname> - subscribe to user\n".
690 "groups - lists the groups you have joined\n".
691 "subscriptions - list the people you follow\n".
692 "subscribers - list the people that follow you\n".
693 "leave <nickname> - unsubscribe from user\n".
694 "d <nickname> <text> - direct message to user\n".
695 "get <nickname> - get last notice from user\n".
696 "whois <nickname> - get profile info on user\n".
697 "fav <nickname> - add user's last notice as a 'fave'\n".
698 "fav #<notice_id> - add notice with the given id as a 'fave'\n".
699 "reply #<notice_id> - reply to notice with a given id\n".
700 "reply <nickname> - reply to the last notice from user\n".
701 "join <group> - join group\n".
702 "login - Get a link to login to the web interface\n".
703 "drop <group> - leave group\n".
704 "stats - get your stats\n".
705 "stop - same as 'off'\n".
706 "quit - same as 'off'\n".
707 "sub <nickname> - same as 'follow'\n".
708 "unsub <nickname> - same as 'leave'\n".
709 "last <nickname> - same as 'get'\n".
710 "on <nickname> - not yet implemented.\n".
711 "off <nickname> - not yet implemented.\n".
712 "nudge <nickname> - remind a user to update.\n".
713 "invite <phone number> - not yet implemented.\n".
714 "track <word> - not yet implemented.\n".
715 "untrack <word> - not yet implemented.\n".
716 "track off - not yet implemented.\n".
717 "untrack all - not yet implemented.\n".
718 "tracks - not yet implemented.\n".
719 "tracking - not yet implemented.\n"));