]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/command.php
Add repeat command
[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         $member = new Group_member();
226
227         $member->group_id   = $group->id;
228         $member->profile_id = $cur->id;
229         $member->created    = common_sql_now();
230
231         $result = $member->insert();
232         if (!$result) {
233           common_log_db_error($member, 'INSERT', __FILE__);
234           $channel->error($cur, sprintf(_('Could not join user %s to group %s'),
235                                        $cur->nickname, $group->nickname));
236           return;
237         }
238
239         $channel->output($cur, sprintf(_('%s joined group %s'),
240                                               $cur->nickname,
241                                               $group->nickname));
242     }
243
244 }
245 class DropCommand extends Command
246 {
247     var $other = null;
248
249     function __construct($user, $other)
250     {
251         parent::__construct($user);
252         $this->other = $other;
253     }
254
255     function execute($channel)
256     {
257
258         $nickname = common_canonical_nickname($this->other);
259         $group    = User_group::staticGet('nickname', $nickname);
260         $cur      = $this->user;
261
262         if (!$group) {
263             $channel->error($cur, _('No such group.'));
264             return;
265         }
266
267         if (!$cur->isMember($group)) {
268             $channel->error($cur, _('You are not a member of that group.'));
269             return;
270         }
271
272         $member = new Group_member();
273
274         $member->group_id   = $group->id;
275         $member->profile_id = $cur->id;
276
277         if (!$member->find(true)) {
278           $channel->error($cur,_('Could not find membership record.'));
279           return;
280         }
281         $result = $member->delete();
282         if (!$result) {
283           common_log_db_error($member, 'INSERT', __FILE__);
284           $channel->error($cur, sprintf(_('Could not remove user %s to group %s'),
285                                        $cur->nickname, $group->nickname));
286           return;
287         }
288
289         $channel->output($cur, sprintf(_('%s left group %s'),
290                                               $cur->nickname,
291                                               $group->nickname));
292     }
293
294 }
295
296 class WhoisCommand extends Command
297 {
298     var $other = null;
299     function __construct($user, $other)
300     {
301         parent::__construct($user);
302         $this->other = $other;
303     }
304
305     function execute($channel)
306     {
307         $recipient =
308           common_relative_profile($this->user, common_canonical_nickname($this->other));
309
310         if (!$recipient) {
311             $channel->error($this->user, _('No such user.'));
312             return;
313         }
314
315         $whois = sprintf(_("%1\$s (%2\$s)"), $recipient->nickname,
316                          $recipient->profileurl);
317         if ($recipient->fullname) {
318             $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname);
319         }
320         if ($recipient->location) {
321             $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location);
322         }
323         if ($recipient->homepage) {
324             $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage);
325         }
326         if ($recipient->bio) {
327             $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio);
328         }
329         $channel->output($this->user, $whois);
330     }
331 }
332
333 class MessageCommand extends Command
334 {
335     var $other = null;
336     var $text = null;
337     function __construct($user, $other, $text)
338     {
339         parent::__construct($user);
340         $this->other = $other;
341         $this->text = $text;
342     }
343
344     function execute($channel)
345     {
346         $other = User::staticGet('nickname', common_canonical_nickname($this->other));
347
348         $len = mb_strlen($this->text);
349
350         if ($len == 0) {
351             $channel->error($this->user, _('No content!'));
352             return;
353         }
354
355         $this->text = common_shorten_links($this->text);
356
357         if (Message::contentTooLong($this->text)) {
358             $channel->error($this->user, sprintf(_('Message too long - maximum is %d characters, you sent %d'),
359                                                  Message::maxContent(), mb_strlen($this->text)));
360             return;
361         }
362
363         if (!$other) {
364             $channel->error($this->user, _('No such user.'));
365             return;
366         } else if (!$this->user->mutuallySubscribed($other)) {
367             $channel->error($this->user, _('You can\'t send a message to this user.'));
368             return;
369         } else if ($this->user->id == $other->id) {
370             $channel->error($this->user, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
371             return;
372         }
373         $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
374         if ($message) {
375             $channel->output($this->user, sprintf(_('Direct message to %s sent'), $this->other));
376         } else {
377             $channel->error($this->user, _('Error sending direct message.'));
378         }
379     }
380 }
381
382 class RepeatCommand extends Command
383 {
384     var $other = null;
385     function __construct($user, $other)
386     {
387         parent::__construct($user);
388         $this->other = $other;
389     }
390
391     function execute($channel)
392     {
393         if(substr($this->other,0,1)=='#'){
394             //repeating a specific notice_id
395
396             $notice = Notice::staticGet(substr($this->other,1));
397             if (!$notice) {
398                 $channel->error($this->user, _('Notice with that id does not exist'));
399                 return;
400             }
401             $recipient = $notice->getProfile();
402         }else{
403             //repeating a given user's last notice
404
405             $recipient =
406               common_relative_profile($this->user, common_canonical_nickname($this->other));
407
408             if (!$recipient) {
409                 $channel->error($this->user, _('No such user.'));
410                 return;
411             }
412             $notice = $recipient->getCurrentNotice();
413             if (!$notice) {
414                 $channel->error($this->user, _('User has no last notice'));
415                 return;
416             }
417         }
418
419         if($this->user->id == $notice->profile_id)
420         {
421             $channel->error($this->user, _('Cannot repeat your own notice'));
422             return;
423         }
424
425         if ($recipient->hasRepeated($notice->id)) {
426             $channel->error($this->user, _('Already repeated that notice'));
427             return;
428         }
429
430         $repeat = $notice->repeat($this->user->id, $channel->source);
431
432         if ($repeat) {
433             common_broadcast_notice($repeat);
434             $channel->output($this->user, sprintf(_('Notice from %s repeated'), $recipient->nickname));
435         } else {
436             $channel->error($this->user, _('Error repeating notice.'));
437         }
438     }
439 }
440
441 class ReplyCommand extends Command
442 {
443     var $other = null;
444     var $text = null;
445     function __construct($user, $other, $text)
446     {
447         parent::__construct($user);
448         $this->other = $other;
449         $this->text = $text;
450     }
451
452     function execute($channel)
453     {
454         if(substr($this->other,0,1)=='#'){
455             //replying to a specific notice_id
456
457             $notice = Notice::staticGet(substr($this->other,1));
458             if (!$notice) {
459                 $channel->error($this->user, _('Notice with that id does not exist'));
460                 return;
461             }
462             $recipient = $notice->getProfile();
463         }else{
464             //replying to a given user's last notice
465
466             $recipient =
467               common_relative_profile($this->user, common_canonical_nickname($this->other));
468
469             if (!$recipient) {
470                 $channel->error($this->user, _('No such user.'));
471                 return;
472             }
473             $notice = $recipient->getCurrentNotice();
474             if (!$notice) {
475                 $channel->error($this->user, _('User has no last notice'));
476                 return;
477             }
478         }
479
480         $len = mb_strlen($this->text);
481
482         if ($len == 0) {
483             $channel->error($this->user, _('No content!'));
484             return;
485         }
486
487         $this->text = common_shorten_links($this->text);
488
489         if (Notice::contentTooLong($this->text)) {
490             $channel->error($this->user, sprintf(_('Notice too long - maximum is %d characters, you sent %d'),
491                                                  Notice::maxContent(), mb_strlen($this->text)));
492             return;
493         }
494
495         $notice = Notice::saveNew($this->user->id, $this->text, $channel->source(),
496                                   array('reply_to' => $notice->id));
497
498         if ($notice) {
499             $channel->output($this->user, sprintf(_('Reply to %s sent'), $recipient->nickname));
500         } else {
501             $channel->error($this->user, _('Error saving notice.'));
502         }
503         common_broadcast_notice($notice);
504     }
505 }
506
507 class GetCommand extends Command
508 {
509
510     var $other = null;
511
512     function __construct($user, $other)
513     {
514         parent::__construct($user);
515         $this->other = $other;
516     }
517
518     function execute($channel)
519     {
520         $target_nickname = common_canonical_nickname($this->other);
521
522         $target =
523           common_relative_profile($this->user, $target_nickname);
524
525         if (!$target) {
526             $channel->error($this->user, _('No such user.'));
527             return;
528         }
529         $notice = $target->getCurrentNotice();
530         if (!$notice) {
531             $channel->error($this->user, _('User has no last notice'));
532             return;
533         }
534         $notice_content = $notice->content;
535
536         $channel->output($this->user, $target_nickname . ": " . $notice_content);
537     }
538 }
539
540 class SubCommand extends Command
541 {
542
543     var $other = null;
544
545     function __construct($user, $other)
546     {
547         parent::__construct($user);
548         $this->other = $other;
549     }
550
551     function execute($channel)
552     {
553
554         if (!$this->other) {
555             $channel->error($this->user, _('Specify the name of the user to subscribe to'));
556             return;
557         }
558
559         $result = subs_subscribe_user($this->user, $this->other);
560
561         if ($result == 'true') {
562             $channel->output($this->user, sprintf(_('Subscribed to %s'), $this->other));
563         } else {
564             $channel->error($this->user, $result);
565         }
566     }
567 }
568
569 class UnsubCommand extends Command
570 {
571
572     var $other = null;
573
574     function __construct($user, $other)
575     {
576         parent::__construct($user);
577         $this->other = $other;
578     }
579
580     function execute($channel)
581     {
582         if(!$this->other) {
583             $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
584             return;
585         }
586
587         $result=subs_unsubscribe_user($this->user, $this->other);
588
589         if ($result) {
590             $channel->output($this->user, sprintf(_('Unsubscribed from %s'), $this->other));
591         } else {
592             $channel->error($this->user, $result);
593         }
594     }
595 }
596
597 class OffCommand extends Command
598 {
599     var $other = null;
600     function __construct($user, $other=null)
601     {
602         parent::__construct($user);
603         $this->other = $other;
604     }
605     function execute($channel)
606     {
607         if ($other) {
608             $channel->error($this->user, _("Command not yet implemented."));
609         } else {
610             if ($channel->off($this->user)) {
611                 $channel->output($this->user, _('Notification off.'));
612             } else {
613                 $channel->error($this->user, _('Can\'t turn off notification.'));
614             }
615         }
616     }
617 }
618
619 class OnCommand extends Command
620 {
621     var $other = null;
622     function __construct($user, $other=null)
623     {
624         parent::__construct($user);
625         $this->other = $other;
626     }
627
628     function execute($channel)
629     {
630         if ($other) {
631             $channel->error($this->user, _("Command not yet implemented."));
632         } else {
633             if ($channel->on($this->user)) {
634                 $channel->output($this->user, _('Notification on.'));
635             } else {
636                 $channel->error($this->user, _('Can\'t turn on notification.'));
637             }
638         }
639     }
640 }
641
642 class LoginCommand extends Command
643 {
644     function execute($channel)
645     {
646         $disabled = common_config('logincommand','disabled');
647         $disabled = isset($disabled) && $disabled;
648         if($disabled) {
649             $channel->error($this->user, _('Login command is disabled'));
650             return;
651         }
652         $login_token = Login_token::staticGet('user_id',$this->user->id);
653         if($login_token){
654             $login_token->delete();
655         }
656         $login_token = new Login_token();
657         $login_token->user_id = $this->user->id;
658         $login_token->token = common_good_rand(16);
659         $login_token->created = common_sql_now();
660         $result = $login_token->insert();
661         if (!$result) {
662           common_log_db_error($login_token, 'INSERT', __FILE__);
663           $channel->error($this->user, sprintf(_('Could not create login token for %s'),
664                                        $this->user->nickname));
665           return;
666         }
667         $channel->output($this->user,
668             sprintf(_('This link is useable only once, and is good for only 2 minutes: %s'),
669                     common_local_url('login',
670                         array('user_id'=>$login_token->user_id, 'token'=>$login_token->token))));
671     }
672 }
673
674 class SubscriptionsCommand extends Command
675 {
676     function execute($channel)
677     {
678         $profile = $this->user->getSubscriptions(0);
679         $nicknames=array();
680         while ($profile->fetch()) {
681             $nicknames[]=$profile->nickname;
682         }
683         if(count($nicknames)==0){
684             $out=_('You are not subscribed to anyone.');
685         }else{
686             $out = ngettext('You are subscribed to this person:',
687                 'You are subscribed to these people:',
688                 count($nicknames));
689             $out .= ' ';
690             $out .= implode(', ',$nicknames);
691         }
692         $channel->output($this->user,$out);
693     }
694 }
695
696 class SubscribersCommand extends Command
697 {
698     function execute($channel)
699     {
700         $profile = $this->user->getSubscribers();
701         $nicknames=array();
702         while ($profile->fetch()) {
703             $nicknames[]=$profile->nickname;
704         }
705         if(count($nicknames)==0){
706             $out=_('No one is subscribed to you.');
707         }else{
708             $out = ngettext('This person is subscribed to you:',
709                 'These people are subscribed to you:',
710                 count($nicknames));
711             $out .= ' ';
712             $out .= implode(', ',$nicknames);
713         }
714         $channel->output($this->user,$out);
715     }
716 }
717
718 class GroupsCommand extends Command
719 {
720     function execute($channel)
721     {
722         $group = $this->user->getGroups();
723         $groups=array();
724         while ($group->fetch()) {
725             $groups[]=$group->nickname;
726         }
727         if(count($groups)==0){
728             $out=_('You are not a member of any groups.');
729         }else{
730             $out = ngettext('You are a member of this group:',
731                 'You are a member of these groups:',
732                 count($nicknames));
733             $out.=implode(', ',$groups);
734         }
735         $channel->output($this->user,$out);
736     }
737 }
738
739 class HelpCommand extends Command
740 {
741     function execute($channel)
742     {
743         $channel->output($this->user,
744                          _("Commands:\n".
745                            "on - turn on notifications\n".
746                            "off - turn off notifications\n".
747                            "help - show this help\n".
748                            "follow <nickname> - subscribe to user\n".
749                            "groups - lists the groups you have joined\n".
750                            "subscriptions - list the people you follow\n".
751                            "subscribers - list the people that follow you\n".
752                            "leave <nickname> - unsubscribe from user\n".
753                            "d <nickname> <text> - direct message to user\n".
754                            "get <nickname> - get last notice from user\n".
755                            "whois <nickname> - get profile info on user\n".
756                            "fav <nickname> - add user's last notice as a 'fave'\n".
757                            "fav #<notice_id> - add notice with the given id as a 'fave'\n".
758                            "repeat #<notice_id> - repeat a notice with a given id\n".
759                            "repeat <nickname> - repeat the last notice from user\n".
760                            "reply #<notice_id> - reply to notice with a given id\n".
761                            "reply <nickname> - reply to the last notice from user\n".
762                            "join <group> - join group\n".
763                            "login - Get a link to login to the web interface\n".
764                            "drop <group> - leave group\n".
765                            "stats - get your stats\n".
766                            "stop - same as 'off'\n".
767                            "quit - same as 'off'\n".
768                            "sub <nickname> - same as 'follow'\n".
769                            "unsub <nickname> - same as 'leave'\n".
770                            "last <nickname> - same as 'get'\n".
771                            "on <nickname> - not yet implemented.\n".
772                            "off <nickname> - not yet implemented.\n".
773                            "nudge <nickname> - remind a user to update.\n".
774                            "invite <phone number> - not yet implemented.\n".
775                            "track <word> - not yet implemented.\n".
776                            "untrack <word> - not yet implemented.\n".
777                            "track off - not yet implemented.\n".
778                            "untrack all - not yet implemented.\n".
779                            "tracks - not yet implemented.\n".
780                            "tracking - not yet implemented.\n"));
781     }
782 }