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