]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/command.php
@evan Fixed message domain for messages in plugins for recent commits.
[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 WhoisCommand extends Command
418 {
419     var $other = null;
420     function __construct($user, $other)
421     {
422         parent::__construct($user);
423         $this->other = $other;
424     }
425
426     function handle($channel)
427     {
428         $recipient = $this->getProfile($this->other);
429
430         // TRANS: Whois output.
431         // TRANS: %1$s nickname of the queried user, %2$s is their profile URL.
432         $whois = sprintf(_m('WHOIS',"%1\$s (%2\$s)"), $recipient->nickname,
433                          $recipient->profileurl);
434         if ($recipient->fullname) {
435             // TRANS: Whois output. %s is the full name of the queried user.
436             $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname);
437         }
438         if ($recipient->location) {
439             // TRANS: Whois output. %s is the location of the queried user.
440             $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location);
441         }
442         if ($recipient->homepage) {
443             // TRANS: Whois output. %s is the homepage of the queried user.
444             $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage);
445         }
446         if ($recipient->bio) {
447             // TRANS: Whois output. %s is the bio information of the queried user.
448             $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio);
449         }
450         $channel->output($this->user, $whois);
451     }
452 }
453
454 class MessageCommand extends Command
455 {
456     var $other = null;
457     var $text = null;
458     function __construct($user, $other, $text)
459     {
460         parent::__construct($user);
461         $this->other = $other;
462         $this->text = $text;
463     }
464
465     function handle($channel)
466     {
467         try {
468             $other = $this->getUser($this->other);
469         } catch (CommandException $e) {
470             try {
471                 $profile = $this->getProfile($this->other);
472             } catch (CommandException $f) {
473                 throw $e;
474             }
475             // TRANS: Command exception text shown when trying to send a direct message to a remote user (a user not registered at the current server).
476             // TRANS: %s is a remote profile.
477             throw new CommandException(sprintf(_('%s is a remote profile; you can only send direct messages to users on the same server.'), $this->other));
478         }
479
480         $len = mb_strlen($this->text);
481
482         if ($len == 0) {
483             // TRANS: Command exception text shown when trying to send a direct message to another user without content.
484             $channel->error($this->user, _('No content!'));
485             return;
486         }
487
488         $this->text = $this->user->shortenLinks($this->text);
489
490         if (Message::contentTooLong($this->text)) {
491             // XXX: i18n. Needs plural support.
492             // TRANS: Message given if content is too long. %1$sd is used for plural.
493             // TRANS: %1$d is the maximum number of characters, %2$d is the number of submitted characters.
494             $channel->error($this->user, sprintf(_m('Message too long - maximum is %1$d character, you sent %2$d.',
495                                                     'Message too long - maximum is %1$d characters, you sent %2$d.',
496                                                     Message::maxContent()),
497                                                  Message::maxContent(), mb_strlen($this->text)));
498             return;
499         }
500
501         if (!$other) {
502             // TRANS: Error text shown when trying to send a direct message to a user that does not exist.
503             $channel->error($this->user, _('No such user.'));
504             return;
505         } else if (!$this->user->mutuallySubscribed($other)) {
506             // 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).
507             $channel->error($this->user, _('You can\'t send a message to this user.'));
508             return;
509         } else if ($this->user->id == $other->id) {
510             // TRANS: Error text shown when trying to send a direct message to self.
511             $channel->error($this->user, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
512             return;
513         }
514         $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
515         if ($message) {
516             $message->notify();
517             // TRANS: Message given have sent a direct message to another user.
518             // TRANS: %s is the name of the other user.
519             $channel->output($this->user, sprintf(_('Direct message to %s sent.'), $this->other));
520         } else {
521             // TRANS: Error text shown sending a direct message fails with an unknown reason.
522             $channel->error($this->user, _('Error sending direct message.'));
523         }
524     }
525 }
526
527 class RepeatCommand extends Command
528 {
529     var $other = null;
530     function __construct($user, $other)
531     {
532         parent::__construct($user);
533         $this->other = $other;
534     }
535
536     function handle($channel)
537     {
538         $notice = $this->getNotice($this->other);
539
540         if($this->user->id == $notice->profile_id)
541         {
542             // TRANS: Error text shown when trying to repeat an own notice.
543             $channel->error($this->user, _('Cannot repeat your own notice.'));
544             return;
545         }
546
547         // Is it OK to repeat that notice (general enough scope)?
548
549         if ($notice->scope != Notice::SITE_SCOPE &&
550             $notice->scope != Notice::PUBLIC_SCOPE) {
551             // TRANS: Client error displayed when trying to repeat a private notice.
552             $channel->error($this->user, _('You may not repeat a private notice.'));
553         }
554
555         $profile = $this->user->getProfile();
556
557         // Can the profile actually see that notice?
558
559         if (!$notice->inScope($profile)) {
560             // TRANS: Client error displayed when trying to repeat a notice the user has no access to.
561             $channel->error($this->user, _('You have no access to that notice.'));
562         }
563
564         if ($profile->hasRepeated($notice->id)) {
565             // TRANS: Error text shown when trying to repeat an notice that was already repeated by the user.
566             $channel->error($this->user, _('Already repeated that notice.'));
567             return;
568         }
569
570         $repeat = $notice->repeat($this->user->id, $channel->source);
571
572         if ($repeat) {
573
574             // TRANS: Message given having repeated a notice from another user.
575             // TRANS: %s is the name of the user for which the notice was repeated.
576             $channel->output($this->user, sprintf(_('Notice from %s repeated.'), $recipient->nickname));
577         } else {
578             // TRANS: Error text shown when repeating a notice fails with an unknown reason.
579             $channel->error($this->user, _('Error repeating notice.'));
580         }
581     }
582 }
583
584 class ReplyCommand extends Command
585 {
586     var $other = null;
587     var $text = null;
588     function __construct($user, $other, $text)
589     {
590         parent::__construct($user);
591         $this->other = $other;
592         $this->text = $text;
593     }
594
595     function handle($channel)
596     {
597         $notice = $this->getNotice($this->other);
598         $recipient = $notice->getProfile();
599
600         $len = mb_strlen($this->text);
601
602         if ($len == 0) {
603             // TRANS: Command exception text shown when trying to reply to a notice without providing content for the reply.
604             $channel->error($this->user, _('No content!'));
605             return;
606         }
607
608         $this->text = $this->user->shortenLinks($this->text);
609
610         if (Notice::contentTooLong($this->text)) {
611             // XXX: i18n. Needs plural support.
612             // TRANS: Message given if content of a notice for a reply is too long. %1$d is used for plural.
613             // TRANS: %1$d is the maximum number of characters, %2$d is the number of submitted characters.
614             $channel->error($this->user, sprintf(_m('Notice too long - maximum is %1$d character, you sent %2$d.',
615                                                     'Notice too long - maximum is %1$d characters, you sent %2$d.',
616                                                     Notice::maxContent()),
617                                                  Notice::maxContent(), mb_strlen($this->text)));
618             return;
619         }
620
621         $notice = Notice::saveNew($this->user->id, $this->text, $channel->source(),
622                                   array('reply_to' => $notice->id));
623
624         if ($notice) {
625             // TRANS: Text shown having sent a reply to a notice successfully.
626             // TRANS: %s is the nickname of the user of the notice the reply was sent to.
627             $channel->output($this->user, sprintf(_('Reply to %s sent.'), $recipient->nickname));
628         } else {
629             // TRANS: Error text shown when a reply to a notice fails with an unknown reason.
630             $channel->error($this->user, _('Error saving notice.'));
631         }
632
633     }
634 }
635
636 class GetCommand extends Command
637 {
638     var $other = null;
639
640     function __construct($user, $other)
641     {
642         parent::__construct($user);
643         $this->other = $other;
644     }
645
646     function handle($channel)
647     {
648         $target = $this->getProfile($this->other);
649
650         $notice = $target->getCurrentNotice();
651         if (!$notice) {
652             // TRANS: Error text shown when a last user notice is requested and it does not exist.
653             $channel->error($this->user, _('User has no last notice.'));
654             return;
655         }
656         $notice_content = $notice->content;
657
658         $channel->output($this->user, $target->nickname . ": " . $notice_content);
659     }
660 }
661
662 class SubCommand extends Command
663 {
664     var $other = null;
665
666     function __construct($user, $other)
667     {
668         parent::__construct($user);
669         $this->other = $other;
670     }
671
672     function handle($channel)
673     {
674
675         if (!$this->other) {
676             // TRANS: Error text shown when no username was provided when issuing a subscribe command.
677             $channel->error($this->user, _('Specify the name of the user to subscribe to.'));
678             return;
679         }
680
681         $target = $this->getProfile($this->other);
682
683         $remote = Remote_profile::staticGet('id', $target->id);
684         if ($remote) {
685             // TRANS: Command exception text shown when trying to subscribe to an OMB profile using the subscribe command.
686             throw new CommandException(_("Can't subscribe to OMB profiles by command."));
687         }
688
689         try {
690             Subscription::start($this->user->getProfile(),
691                                 $target);
692             // TRANS: Text shown after having subscribed to another user successfully.
693             // TRANS: %s is the name of the user the subscription was requested for.
694             $channel->output($this->user, sprintf(_('Subscribed to %s.'), $this->other));
695         } catch (Exception $e) {
696             $channel->error($this->user, $e->getMessage());
697         }
698     }
699 }
700
701 class UnsubCommand extends Command
702 {
703     var $other = null;
704
705     function __construct($user, $other)
706     {
707         parent::__construct($user);
708         $this->other = $other;
709     }
710
711     function handle($channel)
712     {
713         if(!$this->other) {
714             // TRANS: Error text shown when no username was provided when issuing an unsubscribe command.
715             $channel->error($this->user, _('Specify the name of the user to unsubscribe from.'));
716             return;
717         }
718
719         $target = $this->getProfile($this->other);
720
721         try {
722             Subscription::cancel($this->user->getProfile(),
723                                  $target);
724             // TRANS: Text shown after having unsubscribed from another user successfully.
725             // TRANS: %s is the name of the user the unsubscription was requested for.
726             $channel->output($this->user, sprintf(_('Unsubscribed from %s.'), $this->other));
727         } catch (Exception $e) {
728             $channel->error($this->user, $e->getMessage());
729         }
730     }
731 }
732
733 class OffCommand extends Command
734 {
735     var $other = null;
736
737     function __construct($user, $other=null)
738     {
739         parent::__construct($user);
740         $this->other = $other;
741     }
742     function handle($channel)
743     {
744         if ($this->other) {
745             // TRANS: Error text shown when issuing the command "off" with a setting which has not yet been implemented.
746             $channel->error($this->user, _("Command not yet implemented."));
747         } else {
748             if ($channel->off($this->user)) {
749                 // TRANS: Text shown when issuing the command "off" successfully.
750                 $channel->output($this->user, _('Notification off.'));
751             } else {
752                 // TRANS: Error text shown when the command "off" fails for an unknown reason.
753                 $channel->error($this->user, _('Can\'t turn off notification.'));
754             }
755         }
756     }
757 }
758
759 class OnCommand extends Command
760 {
761     var $other = null;
762     function __construct($user, $other=null)
763     {
764         parent::__construct($user);
765         $this->other = $other;
766     }
767
768     function handle($channel)
769     {
770         if ($this->other) {
771             // TRANS: Error text shown when issuing the command "on" with a setting which has not yet been implemented.
772             $channel->error($this->user, _("Command not yet implemented."));
773         } else {
774             if ($channel->on($this->user)) {
775                 // TRANS: Text shown when issuing the command "on" successfully.
776                 $channel->output($this->user, _('Notification on.'));
777             } else {
778                 // TRANS: Error text shown when the command "on" fails for an unknown reason.
779                 $channel->error($this->user, _('Can\'t turn on notification.'));
780             }
781         }
782     }
783 }
784
785 class LoginCommand extends Command
786 {
787     function handle($channel)
788     {
789         $disabled = common_config('logincommand','disabled');
790         $disabled = isset($disabled) && $disabled;
791         if($disabled) {
792             // TRANS: Error text shown when issuing the login command while login is disabled.
793             $channel->error($this->user, _('Login command is disabled.'));
794             return;
795         }
796
797         try {
798             $login_token = Login_token::makeNew($this->user);
799         } catch (Exception $e) {
800             $channel->error($this->user, $e->getMessage());
801         }
802
803         $channel->output($this->user,
804             // TRANS: Text shown after issuing the login command successfully.
805             // TRANS: %s is a logon link..
806             sprintf(_('This link is useable only once and is valid for only 2 minutes: %s.'),
807                     common_local_url('otp',
808                         array('user_id' => $login_token->user_id, 'token' => $login_token->token))));
809     }
810 }
811
812 class LoseCommand extends Command
813 {
814     var $other = null;
815
816     function __construct($user, $other)
817     {
818         parent::__construct($user);
819         $this->other = $other;
820     }
821
822     function execute($channel)
823     {
824         if(!$this->other) {
825             // TRANS: Error text shown when no username was provided when issuing the command.
826             $channel->error($this->user, _('Specify the name of the user to unsubscribe from.'));
827             return;
828         }
829
830         $result = Subscription::cancel($this->getProfile($this->other), $this->user->getProfile());
831
832         if ($result) {
833             // TRANS: Text shown after issuing the lose command successfully (stop another user from following the current user).
834             // TRANS: %s is the name of the user the unsubscription was requested for.
835             $channel->output($this->user, sprintf(_('Unsubscribed %s.'), $this->other));
836         } else {
837             $channel->error($this->user, $result);
838         }
839     }
840 }
841
842 class SubscriptionsCommand extends Command
843 {
844     function handle($channel)
845     {
846         $profile = $this->user->getSubscriptions(0);
847         $nicknames=array();
848         while ($profile->fetch()) {
849             $nicknames[]=$profile->nickname;
850         }
851         if(count($nicknames)==0){
852             // TRANS: Text shown after requesting other users a user is subscribed to without having any subscriptions.
853             $out=_('You are not subscribed to anyone.');
854         }else{
855             // TRANS: Text shown after requesting other users a user is subscribed to.
856             // TRANS: This message supports plural forms. This message is followed by a
857             // TRANS: hard coded space and a comma separated list of subscribed users.
858             $out = ngettext('You are subscribed to this person:',
859                 'You are subscribed to these people:',
860                 count($nicknames));
861             $out .= ' ';
862             $out .= implode(', ',$nicknames);
863         }
864         $channel->output($this->user,$out);
865     }
866 }
867
868 class SubscribersCommand extends Command
869 {
870     function handle($channel)
871     {
872         $profile = $this->user->getSubscribers();
873         $nicknames=array();
874         while ($profile->fetch()) {
875             $nicknames[]=$profile->nickname;
876         }
877         if(count($nicknames)==0){
878             // TRANS: Text shown after requesting other users that are subscribed to a user
879             // TRANS: (followers) without having any subscribers.
880             $out=_('No one is subscribed to you.');
881         }else{
882             // TRANS: Text shown after requesting other users that are subscribed to a user (followers).
883             // TRANS: This message supports plural forms. This message is followed by a
884             // TRANS: hard coded space and a comma separated list of subscribing users.
885             $out = ngettext('This person is subscribed to you:',
886                 'These people are subscribed to you:',
887                 count($nicknames));
888             $out .= ' ';
889             $out .= implode(', ',$nicknames);
890         }
891         $channel->output($this->user,$out);
892     }
893 }
894
895 class GroupsCommand extends Command
896 {
897     function handle($channel)
898     {
899         $group = $this->user->getGroups();
900         $groups=array();
901         while ($group->fetch()) {
902             $groups[]=$group->nickname;
903         }
904         if(count($groups)==0){
905             // TRANS: Text shown after requesting groups a user is subscribed to without having
906             // TRANS: any group subscriptions.
907             $out=_('You are not a member of any groups.');
908         }else{
909             // TRANS: Text shown after requesting groups a user is subscribed to.
910             // TRANS: This message supports plural forms. This message is followed by a
911             // TRANS: hard coded space and a comma separated list of subscribed groups.
912             $out = ngettext('You are a member of this group:',
913                 'You are a member of these groups:',
914                 count($nicknames));
915             $out.=implode(', ',$groups);
916         }
917         $channel->output($this->user,$out);
918     }
919 }
920
921 class HelpCommand extends Command
922 {
923     function handle($channel)
924     {
925         // TRANS: Header line of help text for commands.
926         $out = array(_m('COMMANDHELP', "Commands:"));
927         $commands = array(// TRANS: Help message for IM/SMS command "on"
928                           "on" => _m('COMMANDHELP', "turn on notifications"),
929                           // TRANS: Help message for IM/SMS command "off"
930                           "off" => _m('COMMANDHELP', "turn off notifications"),
931                           // TRANS: Help message for IM/SMS command "help"
932                           "help" => _m('COMMANDHELP', "show this help"),
933                           // TRANS: Help message for IM/SMS command "follow <nickname>"
934                           "follow <nickname>" => _m('COMMANDHELP', "subscribe to user"),
935                           // TRANS: Help message for IM/SMS command "groups"
936                           "groups" => _m('COMMANDHELP', "lists the groups you have joined"),
937                           // TRANS: Help message for IM/SMS command "subscriptions"
938                           "subscriptions" => _m('COMMANDHELP', "list the people you follow"),
939                           // TRANS: Help message for IM/SMS command "subscribers"
940                           "subscribers" => _m('COMMANDHELP', "list the people that follow you"),
941                           // TRANS: Help message for IM/SMS command "leave <nickname>"
942                           "leave <nickname>" => _m('COMMANDHELP', "unsubscribe from user"),
943                           // TRANS: Help message for IM/SMS command "d <nickname> <text>"
944                           "d <nickname> <text>" => _m('COMMANDHELP', "direct message to user"),
945                           // TRANS: Help message for IM/SMS command "get <nickname>"
946                           "get <nickname>" => _m('COMMANDHELP', "get last notice from user"),
947                           // TRANS: Help message for IM/SMS command "whois <nickname>"
948                           "whois <nickname>" => _m('COMMANDHELP', "get profile info on user"),
949                           // TRANS: Help message for IM/SMS command "lose <nickname>"
950                           "lose <nickname>" => _m('COMMANDHELP', "force user to stop following you"),
951                           // TRANS: Help message for IM/SMS command "fav <nickname>"
952                           "fav <nickname>" => _m('COMMANDHELP', "add user's last notice as a 'fave'"),
953                           // TRANS: Help message for IM/SMS command "fav #<notice_id>"
954                           "fav #<notice_id>" => _m('COMMANDHELP', "add notice with the given id as a 'fave'"),
955                           // TRANS: Help message for IM/SMS command "repeat #<notice_id>"
956                           "repeat #<notice_id>" => _m('COMMANDHELP', "repeat a notice with a given id"),
957                           // TRANS: Help message for IM/SMS command "repeat <nickname>"
958                           "repeat <nickname>" => _m('COMMANDHELP', "repeat the last notice from user"),
959                           // TRANS: Help message for IM/SMS command "reply #<notice_id>"
960                           "reply #<notice_id>" => _m('COMMANDHELP', "reply to notice with a given id"),
961                           // TRANS: Help message for IM/SMS command "reply <nickname>"
962                           "reply <nickname>" => _m('COMMANDHELP', "reply to the last notice from user"),
963                           // TRANS: Help message for IM/SMS command "join <group>"
964                           "join <group>" => _m('COMMANDHELP', "join group"),
965                           // TRANS: Help message for IM/SMS command "login"
966                           "login" => _m('COMMANDHELP', "Get a link to login to the web interface"),
967                           // TRANS: Help message for IM/SMS command "drop <group>"
968                           "drop <group>" => _m('COMMANDHELP', "leave group"),
969                           // TRANS: Help message for IM/SMS command "stats"
970                           "stats" => _m('COMMANDHELP', "get your stats"),
971                           // TRANS: Help message for IM/SMS command "stop"
972                           "stop" => _m('COMMANDHELP', "same as 'off'"),
973                           // TRANS: Help message for IM/SMS command "quit"
974                           "quit" => _m('COMMANDHELP', "same as 'off'"),
975                           // TRANS: Help message for IM/SMS command "sub <nickname>"
976                           "sub <nickname>" => _m('COMMANDHELP', "same as 'follow'"),
977                           // TRANS: Help message for IM/SMS command "unsub <nickname>"
978                           "unsub <nickname>" => _m('COMMANDHELP', "same as 'leave'"),
979                           // TRANS: Help message for IM/SMS command "last <nickname>"
980                           "last <nickname>" => _m('COMMANDHELP', "same as 'get'"),
981                           // TRANS: Help message for IM/SMS command "on <nickname>"
982                           "on <nickname>" => _m('COMMANDHELP', "not yet implemented."),
983                           // TRANS: Help message for IM/SMS command "off <nickname>"
984                           "off <nickname>" => _m('COMMANDHELP', "not yet implemented."),
985                           // TRANS: Help message for IM/SMS command "nudge <nickname>"
986                           "nudge <nickname>" => _m('COMMANDHELP', "remind a user to update."),
987                           // TRANS: Help message for IM/SMS command "invite <phone number>"
988                           "invite <phone number>" => _m('COMMANDHELP', "not yet implemented."),
989                           // TRANS: Help message for IM/SMS command "track <word>"
990                           "track <word>" => _m('COMMANDHELP', "not yet implemented."),
991                           // TRANS: Help message for IM/SMS command "untrack <word>"
992                           "untrack <word>" => _m('COMMANDHELP', "not yet implemented."),
993                           // TRANS: Help message for IM/SMS command "track off"
994                           "track off" => _m('COMMANDHELP', "not yet implemented."),
995                           // TRANS: Help message for IM/SMS command "untrack all"
996                           "untrack all" => _m('COMMANDHELP', "not yet implemented."),
997                           // TRANS: Help message for IM/SMS command "tracks"
998                           "tracks" => _m('COMMANDHELP', "not yet implemented."),
999                           // TRANS: Help message for IM/SMS command "tracking"
1000                           "tracking" => _m('COMMANDHELP', "not yet implemented."));
1001
1002         // Give plugins a chance to add or override...
1003         Event::handle('HelpCommandMessages', array($this, &$commands));
1004         foreach ($commands as $command => $help) {
1005             $out[] = "$command - $help";
1006         }
1007         $channel->output($this->user, implode("\n", $out));
1008     }
1009 }