]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/command.php
replace calls to subs_(un)subscribe_user with Subscription methods
[quix0rs-gnu-social.git] / lib / command.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, 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
27     var $user = null;
28
29     function __construct($user=null)
30     {
31         $this->user = $user;
32     }
33
34     function execute($channel)
35     {
36         return false;
37     }
38 }
39
40 class UnimplementedCommand extends Command
41 {
42     function execute($channel)
43     {
44         $channel->error($this->user, _("Sorry, this command is not yet implemented."));
45     }
46 }
47
48 class TrackingCommand extends UnimplementedCommand
49 {
50 }
51
52 class TrackOffCommand extends UnimplementedCommand
53 {
54 }
55
56 class TrackCommand extends UnimplementedCommand
57 {
58     var $word = null;
59     function __construct($user, $word)
60     {
61         parent::__construct($user);
62         $this->word = $word;
63     }
64 }
65
66 class UntrackCommand extends UnimplementedCommand
67 {
68     var $word = null;
69     function __construct($user, $word)
70     {
71         parent::__construct($user);
72         $this->word = $word;
73     }
74 }
75
76 class NudgeCommand extends Command
77 {
78     var $other = null;
79     function __construct($user, $other)
80     {
81         parent::__construct($user);
82         $this->other = $other;
83     }
84     function execute($channel)
85     {
86         $recipient = User::staticGet('nickname', $this->other);
87         if(! $recipient){
88             $channel->error($this->user, sprintf(_('Could not find a user with nickname %s'),
89                                $this->other));
90         }else{
91             if ($recipient->id == $this->user->id) {
92                 $channel->error($this->user, _('It does not make a lot of sense to nudge yourself!'));
93             }else{
94                 if ($recipient->email && $recipient->emailnotifynudge) {
95                     mail_notify_nudge($this->user, $recipient);
96                 }
97                 // XXX: notify by IM
98                 // XXX: notify by SMS
99                 $channel->output($this->user, sprintf(_('Nudge sent to %s'),
100                                $recipient->nickname));
101             }
102         }
103     }
104 }
105
106 class InviteCommand extends UnimplementedCommand
107 {
108     var $other = null;
109     function __construct($user, $other)
110     {
111         parent::__construct($user);
112         $this->other = $other;
113     }
114 }
115
116 class StatsCommand extends Command
117 {
118     function execute($channel)
119     {
120         $profile = $this->user->getProfile();
121
122         $subs_count   = $profile->subscriptionCount();
123         $subbed_count = $profile->subscriberCount();
124         $notice_count = $profile->noticeCount();
125
126         $channel->output($this->user, sprintf(_("Subscriptions: %1\$s\n".
127                                    "Subscribers: %2\$s\n".
128                                    "Notices: %3\$s"),
129                                  $subs_count,
130                                  $subbed_count,
131                                  $notice_count));
132     }
133 }
134
135 class FavCommand extends Command
136 {
137     var $other = null;
138
139     function __construct($user, $other)
140     {
141         parent::__construct($user);
142         $this->other = $other;
143     }
144
145     function execute($channel)
146     {
147         if(substr($this->other,0,1)=='#'){
148             //favoriting a specific notice_id
149
150             $notice = Notice::staticGet(substr($this->other,1));
151             if (!$notice) {
152                 $channel->error($this->user, _('Notice with that id does not exist'));
153                 return;
154             }
155             $recipient = $notice->getProfile();
156         }else{
157             //favoriting a given user's last notice
158
159             $recipient =
160               common_relative_profile($this->user, common_canonical_nickname($this->other));
161
162             if (!$recipient) {
163                 $channel->error($this->user, _('No such user.'));
164                 return;
165             }
166             $notice = $recipient->getCurrentNotice();
167             if (!$notice) {
168                 $channel->error($this->user, _('User has no last notice'));
169                 return;
170             }
171         }
172
173         $fave = Fave::addNew($this->user, $notice);
174
175         if (!$fave) {
176             $channel->error($this->user, _('Could not create favorite.'));
177             return;
178         }
179
180         $other = User::staticGet('id', $recipient->id);
181
182         if ($other && $other->id != $user->id) {
183             if ($other->email && $other->emailnotifyfav) {
184                 mail_notify_fave($other, $this->user, $notice);
185             }
186         }
187
188         $this->user->blowFavesCache();
189
190         $channel->output($this->user, _('Notice marked as fave.'));
191     }
192
193 }
194 class JoinCommand extends Command
195 {
196     var $other = null;
197
198     function __construct($user, $other)
199     {
200         parent::__construct($user);
201         $this->other = $other;
202     }
203
204     function execute($channel)
205     {
206
207         $nickname = common_canonical_nickname($this->other);
208         $group    = User_group::staticGet('nickname', $nickname);
209         $cur      = $this->user;
210
211         if (!$group) {
212             $channel->error($cur, _('No such group.'));
213             return;
214         }
215
216         if ($cur->isMember($group)) {
217             $channel->error($cur, _('You are already a member of that group'));
218             return;
219         }
220         if (Group_block::isBlocked($group, $cur->getProfile())) {
221           $channel->error($cur, _('You have been blocked from that group by the admin.'));
222             return;
223         }
224
225         try {
226             if (Event::handle('StartJoinGroup', array($group, $cur))) {
227                 Group_member::join($group->id, $cur->id);
228                 Event::handle('EndJoinGroup', array($group, $cur));
229             }
230         } catch (Exception $e) {
231             $channel->error($cur, sprintf(_('Could not join user %s to group %s'),
232                                           $cur->nickname, $group->nickname));
233             return;
234         }
235
236         $channel->output($cur, sprintf(_('%s joined group %s'),
237                                               $cur->nickname,
238                                               $group->nickname));
239     }
240
241 }
242 class DropCommand extends Command
243 {
244     var $other = null;
245
246     function __construct($user, $other)
247     {
248         parent::__construct($user);
249         $this->other = $other;
250     }
251
252     function execute($channel)
253     {
254
255         $nickname = common_canonical_nickname($this->other);
256         $group    = User_group::staticGet('nickname', $nickname);
257         $cur      = $this->user;
258
259         if (!$group) {
260             $channel->error($cur, _('No such group.'));
261             return;
262         }
263
264         if (!$cur->isMember($group)) {
265             $channel->error($cur, _('You are not a member of that group.'));
266             return;
267         }
268
269         try {
270             if (Event::handle('StartLeaveGroup', array($group, $cur))) {
271                 Group_member::leave($group->id, $cur->id);
272                 Event::handle('EndLeaveGroup', array($group, $cur));
273             }
274         } catch (Exception $e) {
275             $channel->error($cur, sprintf(_('Could not remove user %s to group %s'),
276                                           $cur->nickname, $group->nickname));
277             return;
278         }
279
280         $channel->output($cur, sprintf(_('%s left group %s'),
281                                               $cur->nickname,
282                                               $group->nickname));
283     }
284
285 }
286
287 class WhoisCommand extends Command
288 {
289     var $other = null;
290     function __construct($user, $other)
291     {
292         parent::__construct($user);
293         $this->other = $other;
294     }
295
296     function execute($channel)
297     {
298         $recipient =
299           common_relative_profile($this->user, common_canonical_nickname($this->other));
300
301         if (!$recipient) {
302             $channel->error($this->user, _('No such user.'));
303             return;
304         }
305
306         $whois = sprintf(_("%1\$s (%2\$s)"), $recipient->nickname,
307                          $recipient->profileurl);
308         if ($recipient->fullname) {
309             $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname);
310         }
311         if ($recipient->location) {
312             $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location);
313         }
314         if ($recipient->homepage) {
315             $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage);
316         }
317         if ($recipient->bio) {
318             $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio);
319         }
320         $channel->output($this->user, $whois);
321     }
322 }
323
324 class MessageCommand extends Command
325 {
326     var $other = null;
327     var $text = null;
328     function __construct($user, $other, $text)
329     {
330         parent::__construct($user);
331         $this->other = $other;
332         $this->text = $text;
333     }
334
335     function execute($channel)
336     {
337         $other = User::staticGet('nickname', common_canonical_nickname($this->other));
338
339         $len = mb_strlen($this->text);
340
341         if ($len == 0) {
342             $channel->error($this->user, _('No content!'));
343             return;
344         }
345
346         $this->text = common_shorten_links($this->text);
347
348         if (Message::contentTooLong($this->text)) {
349             $channel->error($this->user, sprintf(_('Message too long - maximum is %d characters, you sent %d'),
350                                                  Message::maxContent(), mb_strlen($this->text)));
351             return;
352         }
353
354         if (!$other) {
355             $channel->error($this->user, _('No such user.'));
356             return;
357         } else if (!$this->user->mutuallySubscribed($other)) {
358             $channel->error($this->user, _('You can\'t send a message to this user.'));
359             return;
360         } else if ($this->user->id == $other->id) {
361             $channel->error($this->user, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
362             return;
363         }
364         $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
365         if ($message) {
366             $message->notify();
367             $channel->output($this->user, sprintf(_('Direct message to %s sent'), $this->other));
368         } else {
369             $channel->error($this->user, _('Error sending direct message.'));
370         }
371     }
372 }
373
374 class RepeatCommand extends Command
375 {
376     var $other = null;
377     function __construct($user, $other)
378     {
379         parent::__construct($user);
380         $this->other = $other;
381     }
382
383     function execute($channel)
384     {
385         if(substr($this->other,0,1)=='#'){
386             //repeating a specific notice_id
387
388             $notice = Notice::staticGet(substr($this->other,1));
389             if (!$notice) {
390                 $channel->error($this->user, _('Notice with that id does not exist'));
391                 return;
392             }
393             $recipient = $notice->getProfile();
394         }else{
395             //repeating a given user's last notice
396
397             $recipient =
398               common_relative_profile($this->user, common_canonical_nickname($this->other));
399
400             if (!$recipient) {
401                 $channel->error($this->user, _('No such user.'));
402                 return;
403             }
404             $notice = $recipient->getCurrentNotice();
405             if (!$notice) {
406                 $channel->error($this->user, _('User has no last notice'));
407                 return;
408             }
409         }
410
411         if($this->user->id == $notice->profile_id)
412         {
413             $channel->error($this->user, _('Cannot repeat your own notice'));
414             return;
415         }
416
417         if ($recipient->hasRepeated($notice->id)) {
418             $channel->error($this->user, _('Already repeated that notice'));
419             return;
420         }
421
422         $repeat = $notice->repeat($this->user->id, $channel->source);
423
424         if ($repeat) {
425
426             $channel->output($this->user, sprintf(_('Notice from %s repeated'), $recipient->nickname));
427         } else {
428             $channel->error($this->user, _('Error repeating notice.'));
429         }
430     }
431 }
432
433 class ReplyCommand extends Command
434 {
435     var $other = null;
436     var $text = null;
437     function __construct($user, $other, $text)
438     {
439         parent::__construct($user);
440         $this->other = $other;
441         $this->text = $text;
442     }
443
444     function execute($channel)
445     {
446         if(substr($this->other,0,1)=='#'){
447             //replying to a specific notice_id
448
449             $notice = Notice::staticGet(substr($this->other,1));
450             if (!$notice) {
451                 $channel->error($this->user, _('Notice with that id does not exist'));
452                 return;
453             }
454             $recipient = $notice->getProfile();
455         }else{
456             //replying to a given user's last notice
457
458             $recipient =
459               common_relative_profile($this->user, common_canonical_nickname($this->other));
460
461             if (!$recipient) {
462                 $channel->error($this->user, _('No such user.'));
463                 return;
464             }
465             $notice = $recipient->getCurrentNotice();
466             if (!$notice) {
467                 $channel->error($this->user, _('User has no last notice'));
468                 return;
469             }
470         }
471
472         $len = mb_strlen($this->text);
473
474         if ($len == 0) {
475             $channel->error($this->user, _('No content!'));
476             return;
477         }
478
479         $this->text = common_shorten_links($this->text);
480
481         if (Notice::contentTooLong($this->text)) {
482             $channel->error($this->user, sprintf(_('Notice too long - maximum is %d characters, you sent %d'),
483                                                  Notice::maxContent(), mb_strlen($this->text)));
484             return;
485         }
486
487         $notice = Notice::saveNew($this->user->id, $this->text, $channel->source(),
488                                   array('reply_to' => $notice->id));
489
490         if ($notice) {
491             $channel->output($this->user, sprintf(_('Reply to %s sent'), $recipient->nickname));
492         } else {
493             $channel->error($this->user, _('Error saving notice.'));
494         }
495
496     }
497 }
498
499 class GetCommand extends Command
500 {
501
502     var $other = null;
503
504     function __construct($user, $other)
505     {
506         parent::__construct($user);
507         $this->other = $other;
508     }
509
510     function execute($channel)
511     {
512         $target_nickname = common_canonical_nickname($this->other);
513
514         $target =
515           common_relative_profile($this->user, $target_nickname);
516
517         if (!$target) {
518             $channel->error($this->user, _('No such user.'));
519             return;
520         }
521         $notice = $target->getCurrentNotice();
522         if (!$notice) {
523             $channel->error($this->user, _('User has no last notice'));
524             return;
525         }
526         $notice_content = $notice->content;
527
528         $channel->output($this->user, $target_nickname . ": " . $notice_content);
529     }
530 }
531
532 class SubCommand extends Command
533 {
534
535     var $other = null;
536
537     function __construct($user, $other)
538     {
539         parent::__construct($user);
540         $this->other = $other;
541     }
542
543     function execute($channel)
544     {
545
546         if (!$this->other) {
547             $channel->error($this->user, _('Specify the name of the user to subscribe to'));
548             return;
549         }
550
551         $otherUser = User::staticGet('nickname', $this->other);
552
553         if (empty($otherUser)) {
554             $channel->error($this->user, _('No such user'));
555             return;
556         }
557
558         try {
559             Subscription::start($this->user->getProfile(),
560                                 $otherUser->getProfile());
561             $channel->output($this->user, sprintf(_('Subscribed to %s'), $this->other));
562         } catch (Exception $e) {
563             $channel->error($this->user, $e->getMessage());
564         }
565     }
566 }
567
568 class UnsubCommand extends Command
569 {
570
571     var $other = null;
572
573     function __construct($user, $other)
574     {
575         parent::__construct($user);
576         $this->other = $other;
577     }
578
579     function execute($channel)
580     {
581         if(!$this->other) {
582             $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
583             return;
584         }
585
586         $otherUser = User::staticGet('nickname', $this->other);
587
588         if (empty($otherUser)) {
589             $channel->error($this->user, _('No such user'));
590         }
591
592         try {
593             Subscription::cancel($this->user->getProfile(),
594                                  $otherUser->getProfile());
595             $channel->output($this->user, sprintf(_('Unsubscribed from %s'), $this->other));
596         } catch (Exception $e) {
597             $channel->error($this->user, $e->getMessage());
598         }
599     }
600 }
601
602 class OffCommand extends Command
603 {
604     var $other = null;
605     function __construct($user, $other=null)
606     {
607         parent::__construct($user);
608         $this->other = $other;
609     }
610     function execute($channel)
611     {
612         if ($other) {
613             $channel->error($this->user, _("Command not yet implemented."));
614         } else {
615             if ($channel->off($this->user)) {
616                 $channel->output($this->user, _('Notification off.'));
617             } else {
618                 $channel->error($this->user, _('Can\'t turn off notification.'));
619             }
620         }
621     }
622 }
623
624 class OnCommand extends Command
625 {
626     var $other = null;
627     function __construct($user, $other=null)
628     {
629         parent::__construct($user);
630         $this->other = $other;
631     }
632
633     function execute($channel)
634     {
635         if ($other) {
636             $channel->error($this->user, _("Command not yet implemented."));
637         } else {
638             if ($channel->on($this->user)) {
639                 $channel->output($this->user, _('Notification on.'));
640             } else {
641                 $channel->error($this->user, _('Can\'t turn on notification.'));
642             }
643         }
644     }
645 }
646
647 class LoginCommand extends Command
648 {
649     function execute($channel)
650     {
651         $disabled = common_config('logincommand','disabled');
652         $disabled = isset($disabled) && $disabled;
653         if($disabled) {
654             $channel->error($this->user, _('Login command is disabled'));
655             return;
656         }
657
658         try {
659             $login_token = Login_token::makeNew($this->user);
660         } catch (Exception $e) {
661             $channel->error($this->user, $e->getMessage());
662         }
663
664         $channel->output($this->user,
665             sprintf(_('This link is useable only once, and is good for only 2 minutes: %s'),
666                     common_local_url('otp',
667                         array('user_id' => $login_token->user_id, 'token' => $login_token->token))));
668     }
669 }
670
671 class SubscriptionsCommand extends Command
672 {
673     function execute($channel)
674     {
675         $profile = $this->user->getSubscriptions(0);
676         $nicknames=array();
677         while ($profile->fetch()) {
678             $nicknames[]=$profile->nickname;
679         }
680         if(count($nicknames)==0){
681             $out=_('You are not subscribed to anyone.');
682         }else{
683             $out = ngettext('You are subscribed to this person:',
684                 'You are subscribed to these people:',
685                 count($nicknames));
686             $out .= ' ';
687             $out .= implode(', ',$nicknames);
688         }
689         $channel->output($this->user,$out);
690     }
691 }
692
693 class SubscribersCommand extends Command
694 {
695     function execute($channel)
696     {
697         $profile = $this->user->getSubscribers();
698         $nicknames=array();
699         while ($profile->fetch()) {
700             $nicknames[]=$profile->nickname;
701         }
702         if(count($nicknames)==0){
703             $out=_('No one is subscribed to you.');
704         }else{
705             $out = ngettext('This person is subscribed to you:',
706                 'These people are subscribed to you:',
707                 count($nicknames));
708             $out .= ' ';
709             $out .= implode(', ',$nicknames);
710         }
711         $channel->output($this->user,$out);
712     }
713 }
714
715 class GroupsCommand extends Command
716 {
717     function execute($channel)
718     {
719         $group = $this->user->getGroups();
720         $groups=array();
721         while ($group->fetch()) {
722             $groups[]=$group->nickname;
723         }
724         if(count($groups)==0){
725             $out=_('You are not a member of any groups.');
726         }else{
727             $out = ngettext('You are a member of this group:',
728                 'You are a member of these groups:',
729                 count($nicknames));
730             $out.=implode(', ',$groups);
731         }
732         $channel->output($this->user,$out);
733     }
734 }
735
736 class HelpCommand extends Command
737 {
738     function execute($channel)
739     {
740         $channel->output($this->user,
741                          _("Commands:\n".
742                            "on - turn on notifications\n".
743                            "off - turn off notifications\n".
744                            "help - show this help\n".
745                            "follow <nickname> - subscribe to user\n".
746                            "groups - lists the groups you have joined\n".
747                            "subscriptions - list the people you follow\n".
748                            "subscribers - list the people that follow you\n".
749                            "leave <nickname> - unsubscribe from user\n".
750                            "d <nickname> <text> - direct message to user\n".
751                            "get <nickname> - get last notice from user\n".
752                            "whois <nickname> - get profile info on user\n".
753                            "fav <nickname> - add user's last notice as a 'fave'\n".
754                            "fav #<notice_id> - add notice with the given id as a 'fave'\n".
755                            "repeat #<notice_id> - repeat a notice with a given id\n".
756                            "repeat <nickname> - repeat the last notice from user\n".
757                            "reply #<notice_id> - reply to notice with a given id\n".
758                            "reply <nickname> - reply to the last notice from user\n".
759                            "join <group> - join group\n".
760                            "login - Get a link to login to the web interface\n".
761                            "drop <group> - leave group\n".
762                            "stats - get your stats\n".
763                            "stop - same as 'off'\n".
764                            "quit - same as 'off'\n".
765                            "sub <nickname> - same as 'follow'\n".
766                            "unsub <nickname> - same as 'leave'\n".
767                            "last <nickname> - same as 'get'\n".
768                            "on <nickname> - not yet implemented.\n".
769                            "off <nickname> - not yet implemented.\n".
770                            "nudge <nickname> - remind a user to update.\n".
771                            "invite <phone number> - not yet implemented.\n".
772                            "track <word> - not yet implemented.\n".
773                            "untrack <word> - not yet implemented.\n".
774                            "track off - not yet implemented.\n".
775                            "untrack all - not yet implemented.\n".
776                            "tracks - not yet implemented.\n".
777                            "tracking - not yet implemented.\n"));
778     }
779 }