]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/command.php
Merge remote-tracking branch 'mainline/1.0.x' into people_tags_rebase
[quix0rs-gnu-social.git] / lib / command.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, 2010 StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/channel.php');
23
24 class Command
25 {
26     var $user = null;
27
28     function __construct($user=null)
29     {
30         $this->user = $user;
31     }
32
33     /**
34      * Execute the command and send success or error results
35      * back via the given communications channel.
36      *
37      * @param Channel
38      */
39     public function execute($channel)
40     {
41         try {
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());
48         }
49     }
50
51     /**
52      * Override this with the meat!
53      *
54      * An error to send back to the user may be sent by throwing
55      * a CommandException with a formatted message.
56      *
57      * @param Channel
58      * @throws CommandException
59      */
60     function handle($channel)
61     {
62         return false;
63     }
64
65     /**
66      * Look up a notice from an argument, by poster's name to get last post
67      * or notice_id prefixed with #.
68      *
69      * @return Notice
70      * @throws CommandException
71      */
72     function getNotice($arg)
73     {
74         $notice = null;
75         if (Event::handle('StartCommandGetNotice', array($this, $arg, &$notice))) {
76             if(substr($this->other,0,1)=='#'){
77                 // A specific notice_id #123
78
79                 $notice = Notice::staticGet(substr($arg,1));
80                 if (!$notice) {
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.'));
83                 }
84             }
85
86             if (Validate::uri($this->other)) {
87                 // A specific notice by URI lookup
88                 $notice = Notice::staticGet('uri', $arg);
89             }
90
91             if (!$notice) {
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);
95
96                 $notice = $recipient->getCurrentNotice();
97                 if (!$notice) {
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.'));
100                 }
101             }
102         }
103         Event::handle('EndCommandGetNotice', array($this, $arg, &$notice));
104         if (!$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.'));
107         }
108         return $notice;
109     }
110
111     /**
112      * Look up a local or remote profile by nickname.
113      *
114      * @return Profile
115      * @throws CommandException
116      */
117     function getProfile($arg)
118     {
119         $profile = null;
120         if (Event::handle('StartCommandGetProfile', array($this, $arg, &$profile))) {
121             $profile =
122               common_relative_profile($this->user, common_canonical_nickname($arg));
123         }
124         Event::handle('EndCommandGetProfile', array($this, $arg, &$profile));
125         if (!$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));
129         }
130         return $profile;
131     }
132
133     /**
134      * Get a local user by name
135      * @return User
136      * @throws CommandException
137      */
138     function getUser($arg)
139     {
140         $user = null;
141         if (Event::handle('StartCommandGetUser', array($this, $arg, &$user))) {
142             $user = User::staticGet('nickname', Nickname::normalize($arg));
143         }
144         Event::handle('EndCommandGetUser', array($this, $arg, &$user));
145         if (!$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.'),
149                                $arg));
150         }
151         return $user;
152     }
153
154     /**
155      * Get a local or remote group by name.
156      * @return User_group
157      * @throws CommandException
158      */
159     function getGroup($arg)
160     {
161         $group = null;
162         if (Event::handle('StartCommandGetGroup', array($this, $arg, &$group))) {
163             $group = User_group::getForNickname($arg, $this->user->getProfile());
164         }
165         Event::handle('EndCommandGetGroup', array($this, $arg, &$group));
166         if (!$group) {
167             // TRANS: Command exception text shown when a group is requested that does not exist.
168             throw new CommandException(_('No such group.'));
169         }
170         return $group;
171     }
172 }
173
174 class CommandException extends Exception
175 {
176 }
177
178 class UnimplementedCommand extends Command
179 {
180     function handle($channel)
181     {
182         // TRANS: Error text shown when an unimplemented command is given.
183         $channel->error($this->user, _("Sorry, this command is not yet implemented."));
184     }
185 }
186
187 class TrackingCommand extends UnimplementedCommand
188 {
189 }
190
191 class TrackOffCommand extends UnimplementedCommand
192 {
193 }
194
195 class TrackCommand extends UnimplementedCommand
196 {
197     var $word = null;
198     function __construct($user, $word)
199     {
200         parent::__construct($user);
201         $this->word = $word;
202     }
203 }
204
205 class UntrackCommand extends UnimplementedCommand
206 {
207     var $word = null;
208     function __construct($user, $word)
209     {
210         parent::__construct($user);
211         $this->word = $word;
212     }
213 }
214
215 class NudgeCommand extends Command
216 {
217     var $other = null;
218     function __construct($user, $other)
219     {
220         parent::__construct($user);
221         $this->other = $other;
222     }
223
224     function handle($channel)
225     {
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!'));
230         } else {
231             if ($recipient->email && $recipient->emailnotifynudge) {
232                 mail_notify_nudge($this->user, $recipient);
233             }
234             // XXX: notify by IM
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));
240         }
241     }
242 }
243
244 class InviteCommand extends UnimplementedCommand
245 {
246     var $other = null;
247     function __construct($user, $other)
248     {
249         parent::__construct($user);
250         $this->other = $other;
251     }
252 }
253
254 class StatsCommand extends Command
255 {
256     function handle($channel)
257     {
258         $profile = $this->user->getProfile();
259
260         $subs_count   = $profile->subscriptionCount();
261         $subbed_count = $profile->subscriberCount();
262         $notice_count = $profile->noticeCount();
263
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".
270                                    "Notices: %3\$s"),
271                                  $subs_count,
272                                  $subbed_count,
273                                  $notice_count));
274     }
275 }
276
277 class FavCommand extends Command
278 {
279     var $other = null;
280
281     function __construct($user, $other)
282     {
283         parent::__construct($user);
284         $this->other = $other;
285     }
286
287     function handle($channel)
288     {
289         $notice = $this->getNotice($this->other);
290
291         $fave            = new Fave();
292         $fave->user_id   = $this->user->id;
293         $fave->notice_id = $notice->id;
294         $fave->find();
295
296         if ($fave->fetch()) {
297             // TRANS: Error message text shown when a favorite could not be set because it has already been favorited.
298             $channel->error($this->user, _('Could not create favorite: already favorited.'));
299             return;
300         }
301
302         $fave = Fave::addNew($this->user->getProfile(), $notice);
303
304         if (!$fave) {
305             // TRANS: Error message text shown when a favorite could not be set.
306             $channel->error($this->user, _('Could not create favorite.'));
307             return;
308         }
309
310         // @fixme favorite notification should be triggered
311         // at a lower level
312
313         $other = User::staticGet('id', $notice->profile_id);
314
315         if ($other && $other->id != $this->user->id) {
316             if ($other->email && $other->emailnotifyfav) {
317                 mail_notify_fave($other, $this->user, $notice);
318             }
319         }
320
321         $this->user->blowFavesCache();
322
323         // TRANS: Text shown when a notice has been marked as favourite successfully.
324         $channel->output($this->user, _('Notice marked as fave.'));
325     }
326 }
327
328 class JoinCommand extends Command
329 {
330     var $other = null;
331
332     function __construct($user, $other)
333     {
334         parent::__construct($user);
335         $this->other = $other;
336     }
337
338     function handle($channel)
339     {
340         $group = $this->getGroup($this->other);
341         $cur   = $this->user;
342
343         if ($cur->isMember($group)) {
344             // TRANS: Error text shown a user tries to join a group they already are a member of.
345             $channel->error($cur, _('You are already a member of that group.'));
346             return;
347         }
348         if (Group_block::isBlocked($group, $cur->getProfile())) {
349             // TRANS: Error text shown when a user tries to join a group they are blocked from joining.
350           $channel->error($cur, _('You have been blocked from that group by the admin.'));
351             return;
352         }
353
354         try {
355             $cur->joinGroup($group);
356         } catch (Exception $e) {
357             // TRANS: Message given having failed to add a user to a group.
358             // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group.
359             $channel->error($cur, sprintf(_('Could not join user %1$s to group %2$s.'),
360                                           $cur->nickname, $group->nickname));
361             return;
362         }
363
364         // TRANS: Message given having added a user to a group.
365         // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group.
366         $channel->output($cur, sprintf(_('%1$s joined group %2$s.'),
367                                               $cur->nickname,
368                                               $group->nickname));
369     }
370 }
371
372 class DropCommand extends Command
373 {
374     var $other = null;
375
376     function __construct($user, $other)
377     {
378         parent::__construct($user);
379         $this->other = $other;
380     }
381
382     function handle($channel)
383     {
384         $group = $this->getGroup($this->other);
385         $cur   = $this->user;
386
387         if (!$group) {
388             // TRANS: Error text shown when trying to leave a group that does not exist.
389             $channel->error($cur, _('No such group.'));
390             return;
391         }
392
393         if (!$cur->isMember($group)) {
394             // TRANS: Error text shown when trying to leave an existing group the user is not a member of.
395             $channel->error($cur, _('You are not a member of that group.'));
396             return;
397         }
398
399         try {
400             $cur->leaveGroup($group);
401         } catch (Exception $e) {
402             // TRANS: Message given having failed to remove a user from a group.
403             // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group.
404             $channel->error($cur, sprintf(_('Could not remove user %1$s from group %2$s.'),
405                                           $cur->nickname, $group->nickname));
406             return;
407         }
408
409         // TRANS: Message given having removed a user from a group.
410         // TRANS: %1$s is the nickname of the user, %2$s is the nickname of the group.
411         $channel->output($cur, sprintf(_('%1$s left group %2$s.'),
412                                               $cur->nickname,
413                                               $group->nickname));
414     }
415 }
416
417 class TagCommand extends Command
418 {
419     var $other = null;
420     var $tags = null;
421     function __construct($user, $other, $tags)
422     {
423         parent::__construct($user);
424         $this->other = $other;
425         $this->tags = $tags;
426     }
427
428     function handle($channel)
429     {
430         $profile = $this->getProfile($this->other);
431         $cur     = $this->user->getProfile();
432
433         if (!$profile) {
434             $channel->error($cur, _('No such profile.'));
435             return;
436         }
437         if (!$cur->canTag($profile)) {
438             $channel->error($cur, _('You cannot tag this user.'));
439             return;
440         }
441
442         $privs = array();
443         $tags = preg_split('/[\s,]+/', $this->tags);
444         $clean_tags = array();
445
446         foreach ($tags as $tag) {
447             $private = @$tag[0] === '.';
448             $tag = $clean_tags[] = common_canonical_tag($tag);
449
450             if (!common_valid_profile_tag($tag)) {
451                 $channel->error($cur, sprintf(_('Invalid tag: "%s"'), $tag));
452                 return;
453             }
454             $privs[$tag] = $private;
455         }
456
457         try {
458             foreach ($clean_tags as $tag) {
459                 Profile_tag::setTag($cur->id, $profile->id, $tag, null, $privs[$tag]);
460             }
461         } catch (Exception $e) {
462             $channel->error($cur, sprintf(_('Error tagging %s: %s'),
463                                           $profile->nickname, $e->getMessage()));
464             return;
465         }
466
467         $channel->output($cur, sprintf(_('%1$s was tagged %2$s'),
468                                               $profile->nickname,
469                                               implode(', ', $clean_tags)));
470     }
471 }
472
473
474 class UntagCommand extends TagCommand
475 {
476     function handle($channel)
477     {
478         $profile = $this->getProfile($this->other);
479         $cur     = $this->user->getProfile();
480
481         if (!$profile) {
482             $channel->error($cur, _('No such profile.'));
483             return;
484         }
485         if (!$cur->canTag($profile)) {
486             $channel->error($cur, _('You cannot tag this user.'));
487             return;
488         }
489
490         $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $this->tags));
491
492         foreach ($tags as $tag) {
493             if (!common_valid_profile_tag($tag)) {
494                 $channel->error($cur, sprintf(_('Invalid tag: "%s"'), $tag));
495                 return;
496             }
497         }
498
499         try {
500             foreach ($tags as $tag) {
501                 Profile_tag::unTag($cur->id, $profile->id, $tag);
502             }
503         } catch (Exception $e) {
504             $channel->error($cur, sprintf(_('Error untagging %s: %s'),
505                                           $profile->nickname, $e->getMessage()));
506             return;
507         }
508
509         $channel->output($cur, sprintf(_('The following tag(s) were removed from user %1$s: %2$s.'),
510                                               $profile->nickname,
511                                               implode(', ', $tags)));
512     }
513 }
514
515 class WhoisCommand extends Command
516 {
517     var $other = null;
518     function __construct($user, $other)
519     {
520         parent::__construct($user);
521         $this->other = $other;
522     }
523
524     function handle($channel)
525     {
526         $recipient = $this->getProfile($this->other);
527
528         // TRANS: Whois output.
529         // TRANS: %1$s nickname of the queried user, %2$s is their profile URL.
530         $whois = sprintf(_m('WHOIS',"%1\$s (%2\$s)"), $recipient->nickname,
531                          $recipient->profileurl);
532         if ($recipient->fullname) {
533             // TRANS: Whois output. %s is the full name of the queried user.
534             $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname);
535         }
536         if ($recipient->location) {
537             // TRANS: Whois output. %s is the location of the queried user.
538             $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location);
539         }
540         if ($recipient->homepage) {
541             // TRANS: Whois output. %s is the homepage of the queried user.
542             $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage);
543         }
544         if ($recipient->bio) {
545             // TRANS: Whois output. %s is the bio information of the queried user.
546             $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio);
547         }
548         $channel->output($this->user, $whois);
549     }
550 }
551
552 class MessageCommand extends Command
553 {
554     var $other = null;
555     var $text = null;
556     function __construct($user, $other, $text)
557     {
558         parent::__construct($user);
559         $this->other = $other;
560         $this->text = $text;
561     }
562
563     function handle($channel)
564     {
565         try {
566             $other = $this->getUser($this->other);
567         } catch (CommandException $e) {
568             try {
569                 $profile = $this->getProfile($this->other);
570             } catch (CommandException $f) {
571                 throw $e;
572             }
573             // TRANS: Command exception text shown when trying to send a direct message to a remote user (a user not registered at the current server).
574             // TRANS: %s is a remote profile.
575             throw new CommandException(sprintf(_('%s is a remote profile; you can only send direct messages to users on the same server.'), $this->other));
576         }
577
578         $len = mb_strlen($this->text);
579
580         if ($len == 0) {
581             // TRANS: Command exception text shown when trying to send a direct message to another user without content.
582             $channel->error($this->user, _('No content!'));
583             return;
584         }
585
586         $this->text = $this->user->shortenLinks($this->text);
587
588         if (Message::contentTooLong($this->text)) {
589             // XXX: i18n. Needs plural support.
590             // TRANS: Message given if content is too long. %1$sd is used for plural.
591             // TRANS: %1$d is the maximum number of characters, %2$d is the number of submitted characters.
592             $channel->error($this->user, sprintf(_m('Message too long - maximum is %1$d character, you sent %2$d.',
593                                                     'Message too long - maximum is %1$d characters, you sent %2$d.',
594                                                     Message::maxContent()),
595                                                  Message::maxContent(), mb_strlen($this->text)));
596             return;
597         }
598
599         if (!$other) {
600             // TRANS: Error text shown when trying to send a direct message to a user that does not exist.
601             $channel->error($this->user, _('No such user.'));
602             return;
603         } else if (!$this->user->mutuallySubscribed($other)) {
604             // 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).
605             $channel->error($this->user, _('You can\'t send a message to this user.'));
606             return;
607         } else if ($this->user->id == $other->id) {
608             // TRANS: Error text shown when trying to send a direct message to self.
609             $channel->error($this->user, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
610             return;
611         }
612         $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
613         if ($message) {
614             $message->notify();
615             // TRANS: Message given have sent a direct message to another user.
616             // TRANS: %s is the name of the other user.
617             $channel->output($this->user, sprintf(_('Direct message to %s sent.'), $this->other));
618         } else {
619             // TRANS: Error text shown sending a direct message fails with an unknown reason.
620             $channel->error($this->user, _('Error sending direct message.'));
621         }
622     }
623 }
624
625 class RepeatCommand extends Command
626 {
627     var $other = null;
628     function __construct($user, $other)
629     {
630         parent::__construct($user);
631         $this->other = $other;
632     }
633
634     function handle($channel)
635     {
636         $notice = $this->getNotice($this->other);
637
638         try {
639             $repeat = $notice->repeat($this->user->id, $channel->source());
640             $recipient = $notice->getProfile();
641
642             // TRANS: Message given having repeated a notice from another user.
643             // TRANS: %s is the name of the user for which the notice was repeated.
644             $channel->output($this->user, sprintf(_('Notice from %s repeated.'), $recipient->nickname));
645         } catch (Exception $e) {
646             $channel->error($this->user, $e->getMessage());
647         }
648     }
649 }
650
651 class ReplyCommand extends Command
652 {
653     var $other = null;
654     var $text = null;
655     function __construct($user, $other, $text)
656     {
657         parent::__construct($user);
658         $this->other = $other;
659         $this->text = $text;
660     }
661
662     function handle($channel)
663     {
664         $notice = $this->getNotice($this->other);
665         $recipient = $notice->getProfile();
666
667         $len = mb_strlen($this->text);
668
669         if ($len == 0) {
670             // TRANS: Command exception text shown when trying to reply to a notice without providing content for the reply.
671             $channel->error($this->user, _('No content!'));
672             return;
673         }
674
675         $this->text = $this->user->shortenLinks($this->text);
676
677         if (Notice::contentTooLong($this->text)) {
678             // XXX: i18n. Needs plural support.
679             // TRANS: Message given if content of a notice for a reply is too long. %1$d is used for plural.
680             // TRANS: %1$d is the maximum number of characters, %2$d is the number of submitted characters.
681             $channel->error($this->user, sprintf(_m('Notice too long - maximum is %1$d character, you sent %2$d.',
682                                                     'Notice too long - maximum is %1$d characters, you sent %2$d.',
683                                                     Notice::maxContent()),
684                                                  Notice::maxContent(), mb_strlen($this->text)));
685             return;
686         }
687
688         $notice = Notice::saveNew($this->user->id, $this->text, $channel->source(),
689                                   array('reply_to' => $notice->id));
690
691         if ($notice) {
692             // TRANS: Text shown having sent a reply to a notice successfully.
693             // TRANS: %s is the nickname of the user of the notice the reply was sent to.
694             $channel->output($this->user, sprintf(_('Reply to %s sent.'), $recipient->nickname));
695         } else {
696             // TRANS: Error text shown when a reply to a notice fails with an unknown reason.
697             $channel->error($this->user, _('Error saving notice.'));
698         }
699
700     }
701 }
702
703 class GetCommand extends Command
704 {
705     var $other = null;
706
707     function __construct($user, $other)
708     {
709         parent::__construct($user);
710         $this->other = $other;
711     }
712
713     function handle($channel)
714     {
715         $target = $this->getProfile($this->other);
716
717         $notice = $target->getCurrentNotice();
718         if (!$notice) {
719             // TRANS: Error text shown when a last user notice is requested and it does not exist.
720             $channel->error($this->user, _('User has no last notice.'));
721             return;
722         }
723         $notice_content = $notice->content;
724
725         $channel->output($this->user, $target->nickname . ": " . $notice_content);
726     }
727 }
728
729 class SubCommand extends Command
730 {
731     var $other = null;
732
733     function __construct($user, $other)
734     {
735         parent::__construct($user);
736         $this->other = $other;
737     }
738
739     function handle($channel)
740     {
741
742         if (!$this->other) {
743             // TRANS: Error text shown when no username was provided when issuing a subscribe command.
744             $channel->error($this->user, _('Specify the name of the user to subscribe to.'));
745             return;
746         }
747
748         $target = $this->getProfile($this->other);
749
750         $remote = Remote_profile::staticGet('id', $target->id);
751         if ($remote) {
752             // TRANS: Command exception text shown when trying to subscribe to an OMB profile using the subscribe command.
753             throw new CommandException(_("Can't subscribe to OMB profiles by command."));
754         }
755
756         try {
757             Subscription::start($this->user->getProfile(),
758                                 $target);
759             // TRANS: Text shown after having subscribed to another user successfully.
760             // TRANS: %s is the name of the user the subscription was requested for.
761             $channel->output($this->user, sprintf(_('Subscribed to %s.'), $this->other));
762         } catch (Exception $e) {
763             $channel->error($this->user, $e->getMessage());
764         }
765     }
766 }
767
768 class UnsubCommand extends Command
769 {
770     var $other = null;
771
772     function __construct($user, $other)
773     {
774         parent::__construct($user);
775         $this->other = $other;
776     }
777
778     function handle($channel)
779     {
780         if(!$this->other) {
781             // TRANS: Error text shown when no username was provided when issuing an unsubscribe command.
782             $channel->error($this->user, _('Specify the name of the user to unsubscribe from.'));
783             return;
784         }
785
786         $target = $this->getProfile($this->other);
787
788         try {
789             Subscription::cancel($this->user->getProfile(),
790                                  $target);
791             // TRANS: Text shown after having unsubscribed from another user successfully.
792             // TRANS: %s is the name of the user the unsubscription was requested for.
793             $channel->output($this->user, sprintf(_('Unsubscribed from %s.'), $this->other));
794         } catch (Exception $e) {
795             $channel->error($this->user, $e->getMessage());
796         }
797     }
798 }
799
800 class OffCommand extends Command
801 {
802     var $other = null;
803
804     function __construct($user, $other=null)
805     {
806         parent::__construct($user);
807         $this->other = $other;
808     }
809     function handle($channel)
810     {
811         if ($this->other) {
812             // TRANS: Error text shown when issuing the command "off" with a setting which has not yet been implemented.
813             $channel->error($this->user, _("Command not yet implemented."));
814         } else {
815             if ($channel->off($this->user)) {
816                 // TRANS: Text shown when issuing the command "off" successfully.
817                 $channel->output($this->user, _('Notification off.'));
818             } else {
819                 // TRANS: Error text shown when the command "off" fails for an unknown reason.
820                 $channel->error($this->user, _('Can\'t turn off notification.'));
821             }
822         }
823     }
824 }
825
826 class OnCommand extends Command
827 {
828     var $other = null;
829     function __construct($user, $other=null)
830     {
831         parent::__construct($user);
832         $this->other = $other;
833     }
834
835     function handle($channel)
836     {
837         if ($this->other) {
838             // TRANS: Error text shown when issuing the command "on" with a setting which has not yet been implemented.
839             $channel->error($this->user, _("Command not yet implemented."));
840         } else {
841             if ($channel->on($this->user)) {
842                 // TRANS: Text shown when issuing the command "on" successfully.
843                 $channel->output($this->user, _('Notification on.'));
844             } else {
845                 // TRANS: Error text shown when the command "on" fails for an unknown reason.
846                 $channel->error($this->user, _('Can\'t turn on notification.'));
847             }
848         }
849     }
850 }
851
852 class LoginCommand extends Command
853 {
854     function handle($channel)
855     {
856         $disabled = common_config('logincommand','disabled');
857         $disabled = isset($disabled) && $disabled;
858         if($disabled) {
859             // TRANS: Error text shown when issuing the login command while login is disabled.
860             $channel->error($this->user, _('Login command is disabled.'));
861             return;
862         }
863
864         try {
865             $login_token = Login_token::makeNew($this->user);
866         } catch (Exception $e) {
867             $channel->error($this->user, $e->getMessage());
868         }
869
870         $channel->output($this->user,
871             // TRANS: Text shown after issuing the login command successfully.
872             // TRANS: %s is a logon link..
873             sprintf(_('This link is useable only once and is valid for only 2 minutes: %s.'),
874                     common_local_url('otp',
875                         array('user_id' => $login_token->user_id, 'token' => $login_token->token))));
876     }
877 }
878
879 class LoseCommand extends Command
880 {
881     var $other = null;
882
883     function __construct($user, $other)
884     {
885         parent::__construct($user);
886         $this->other = $other;
887     }
888
889     function execute($channel)
890     {
891         if(!$this->other) {
892             // TRANS: Error text shown when no username was provided when issuing the command.
893             $channel->error($this->user, _('Specify the name of the user to unsubscribe from.'));
894             return;
895         }
896
897         $result = Subscription::cancel($this->getProfile($this->other), $this->user->getProfile());
898
899         if ($result) {
900             // TRANS: Text shown after issuing the lose command successfully (stop another user from following the current user).
901             // TRANS: %s is the name of the user the unsubscription was requested for.
902             $channel->output($this->user, sprintf(_('Unsubscribed %s.'), $this->other));
903         } else {
904             $channel->error($this->user, $result);
905         }
906     }
907 }
908
909 class SubscriptionsCommand extends Command
910 {
911     function handle($channel)
912     {
913         $profile = $this->user->getSubscriptions(0);
914         $nicknames=array();
915         while ($profile->fetch()) {
916             $nicknames[]=$profile->nickname;
917         }
918         if(count($nicknames)==0){
919             // TRANS: Text shown after requesting other users a user is subscribed to without having any subscriptions.
920             $out=_('You are not subscribed to anyone.');
921         }else{
922             // TRANS: Text shown after requesting other users a user is subscribed to.
923             // TRANS: This message supports plural forms. This message is followed by a
924             // TRANS: hard coded space and a comma separated list of subscribed users.
925             $out = _m('You are subscribed to this person:',
926                 'You are subscribed to these people:',
927                 count($nicknames));
928             $out .= ' ';
929             $out .= implode(', ',$nicknames);
930         }
931         $channel->output($this->user,$out);
932     }
933 }
934
935 class SubscribersCommand extends Command
936 {
937     function handle($channel)
938     {
939         $profile = $this->user->getSubscribers();
940         $nicknames=array();
941         while ($profile->fetch()) {
942             $nicknames[]=$profile->nickname;
943         }
944         if(count($nicknames)==0){
945             // TRANS: Text shown after requesting other users that are subscribed to a user
946             // TRANS: (followers) without having any subscribers.
947             $out=_('No one is subscribed to you.');
948         }else{
949             // TRANS: Text shown after requesting other users that are subscribed to a user (followers).
950             // TRANS: This message supports plural forms. This message is followed by a
951             // TRANS: hard coded space and a comma separated list of subscribing users.
952             $out = _m('This person is subscribed to you:',
953                 'These people are subscribed to you:',
954                 count($nicknames));
955             $out .= ' ';
956             $out .= implode(', ',$nicknames);
957         }
958         $channel->output($this->user,$out);
959     }
960 }
961
962 class GroupsCommand extends Command
963 {
964     function handle($channel)
965     {
966         $group = $this->user->getGroups();
967         $groups=array();
968         while ($group->fetch()) {
969             $groups[]=$group->nickname;
970         }
971         if(count($groups)==0){
972             // TRANS: Text shown after requesting groups a user is subscribed to without having
973             // TRANS: any group subscriptions.
974             $out=_('You are not a member of any groups.');
975         }else{
976             // TRANS: Text shown after requesting groups a user is subscribed to.
977             // TRANS: This message supports plural forms. This message is followed by a
978             // TRANS: hard coded space and a comma separated list of subscribed groups.
979             $out = _m('You are a member of this group:',
980                 'You are a member of these groups:',
981                 count($nicknames));
982             $out.=implode(', ',$groups);
983         }
984         $channel->output($this->user,$out);
985     }
986 }
987
988 class HelpCommand extends Command
989 {
990     function handle($channel)
991     {
992         // TRANS: Header line of help text for commands.
993         $out = array(_m('COMMANDHELP', "Commands:"));
994         $commands = array(// TRANS: Help message for IM/SMS command "on"
995                           "on" => _m('COMMANDHELP', "turn on notifications"),
996                           // TRANS: Help message for IM/SMS command "off"
997                           "off" => _m('COMMANDHELP', "turn off notifications"),
998                           // TRANS: Help message for IM/SMS command "help"
999                           "help" => _m('COMMANDHELP', "show this help"),
1000                           // TRANS: Help message for IM/SMS command "follow <nickname>"
1001                           "follow <nickname>" => _m('COMMANDHELP', "subscribe to user"),
1002                           // TRANS: Help message for IM/SMS command "groups"
1003                           "groups" => _m('COMMANDHELP', "lists the groups you have joined"),
1004                           // TRANS: Help message for IM/SMS command "tag"
1005                           "tag <nickname> <tags>" => _m('COMMANDHELP',"tag a user"),
1006                           // TRANS: Help message for IM/SMS command "untag"
1007                           "untag <nickname> <tags>" => _m('COMMANDHELP',"untag a user"),
1008                           // TRANS: Help message for IM/SMS command "subscriptions"
1009                           "subscriptions" => _m('COMMANDHELP', "list the people you follow"),
1010                           // TRANS: Help message for IM/SMS command "subscribers"
1011                           "subscribers" => _m('COMMANDHELP', "list the people that follow you"),
1012                           // TRANS: Help message for IM/SMS command "leave <nickname>"
1013                           "leave <nickname>" => _m('COMMANDHELP', "unsubscribe from user"),
1014                           // TRANS: Help message for IM/SMS command "d <nickname> <text>"
1015                           "d <nickname> <text>" => _m('COMMANDHELP', "direct message to user"),
1016                           // TRANS: Help message for IM/SMS command "get <nickname>"
1017                           "get <nickname>" => _m('COMMANDHELP', "get last notice from user"),
1018                           // TRANS: Help message for IM/SMS command "whois <nickname>"
1019                           "whois <nickname>" => _m('COMMANDHELP', "get profile info on user"),
1020                           // TRANS: Help message for IM/SMS command "lose <nickname>"
1021                           "lose <nickname>" => _m('COMMANDHELP', "force user to stop following you"),
1022                           // TRANS: Help message for IM/SMS command "fav <nickname>"
1023                           "fav <nickname>" => _m('COMMANDHELP', "add user's last notice as a 'fave'"),
1024                           // TRANS: Help message for IM/SMS command "fav #<notice_id>"
1025                           "fav #<notice_id>" => _m('COMMANDHELP', "add notice with the given id as a 'fave'"),
1026                           // TRANS: Help message for IM/SMS command "repeat #<notice_id>"
1027                           "repeat #<notice_id>" => _m('COMMANDHELP', "repeat a notice with a given id"),
1028                           // TRANS: Help message for IM/SMS command "repeat <nickname>"
1029                           "repeat <nickname>" => _m('COMMANDHELP', "repeat the last notice from user"),
1030                           // TRANS: Help message for IM/SMS command "reply #<notice_id>"
1031                           "reply #<notice_id>" => _m('COMMANDHELP', "reply to notice with a given id"),
1032                           // TRANS: Help message for IM/SMS command "reply <nickname>"
1033                           "reply <nickname>" => _m('COMMANDHELP', "reply to the last notice from user"),
1034                           // TRANS: Help message for IM/SMS command "join <group>"
1035                           "join <group>" => _m('COMMANDHELP', "join group"),
1036                           // TRANS: Help message for IM/SMS command "login"
1037                           "login" => _m('COMMANDHELP', "Get a link to login to the web interface"),
1038                           // TRANS: Help message for IM/SMS command "drop <group>"
1039                           "drop <group>" => _m('COMMANDHELP', "leave group"),
1040                           // TRANS: Help message for IM/SMS command "stats"
1041                           "stats" => _m('COMMANDHELP', "get your stats"),
1042                           // TRANS: Help message for IM/SMS command "stop"
1043                           "stop" => _m('COMMANDHELP', "same as 'off'"),
1044                           // TRANS: Help message for IM/SMS command "quit"
1045                           "quit" => _m('COMMANDHELP', "same as 'off'"),
1046                           // TRANS: Help message for IM/SMS command "sub <nickname>"
1047                           "sub <nickname>" => _m('COMMANDHELP', "same as 'follow'"),
1048                           // TRANS: Help message for IM/SMS command "unsub <nickname>"
1049                           "unsub <nickname>" => _m('COMMANDHELP', "same as 'leave'"),
1050                           // TRANS: Help message for IM/SMS command "last <nickname>"
1051                           "last <nickname>" => _m('COMMANDHELP', "same as 'get'"),
1052                           // TRANS: Help message for IM/SMS command "on <nickname>"
1053                           "on <nickname>" => _m('COMMANDHELP', "not yet implemented."),
1054                           // TRANS: Help message for IM/SMS command "off <nickname>"
1055                           "off <nickname>" => _m('COMMANDHELP', "not yet implemented."),
1056                           // TRANS: Help message for IM/SMS command "nudge <nickname>"
1057                           "nudge <nickname>" => _m('COMMANDHELP', "remind a user to update."),
1058                           // TRANS: Help message for IM/SMS command "invite <phone number>"
1059                           "invite <phone number>" => _m('COMMANDHELP', "not yet implemented."),
1060                           // TRANS: Help message for IM/SMS command "track <word>"
1061                           "track <word>" => _m('COMMANDHELP', "not yet implemented."),
1062                           // TRANS: Help message for IM/SMS command "untrack <word>"
1063                           "untrack <word>" => _m('COMMANDHELP', "not yet implemented."),
1064                           // TRANS: Help message for IM/SMS command "track off"
1065                           "track off" => _m('COMMANDHELP', "not yet implemented."),
1066                           // TRANS: Help message for IM/SMS command "untrack all"
1067                           "untrack all" => _m('COMMANDHELP', "not yet implemented."),
1068                           // TRANS: Help message for IM/SMS command "tracks"
1069                           "tracks" => _m('COMMANDHELP', "not yet implemented."),
1070                           // TRANS: Help message for IM/SMS command "tracking"
1071                           "tracking" => _m('COMMANDHELP', "not yet implemented."));
1072
1073         // Give plugins a chance to add or override...
1074         Event::handle('HelpCommandMessages', array($this, &$commands));
1075         foreach ($commands as $command => $help) {
1076             $out[] = "$command - $help";
1077         }
1078         $channel->output($this->user, implode("\n", $out));
1079     }
1080 }