]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/command.php
Added a new "reply" 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 UnimplementedCommand
77 {
78     var $other = null;
79     function __construct($user, $other)
80     {
81         parent::__construct($user);
82         $this->other = $other;
83     }
84 }
85
86 class InviteCommand extends UnimplementedCommand
87 {
88     var $other = null;
89     function __construct($user, $other)
90     {
91         parent::__construct($user);
92         $this->other = $other;
93     }
94 }
95
96 class StatsCommand extends Command
97 {
98     function execute($channel)
99     {
100         $profile = $this->user->getProfile();
101
102         $subs_count   = $profile->subscriptionCount();
103         $subbed_count = $profile->subscriberCount();
104         $notice_count = $profile->noticeCount();
105
106         $channel->output($this->user, sprintf(_("Subscriptions: %1\$s\n".
107                                    "Subscribers: %2\$s\n".
108                                    "Notices: %3\$s"),
109                                  $subs_count,
110                                  $subbed_count,
111                                  $notice_count));
112     }
113 }
114
115 class FavCommand extends Command
116 {
117     var $other = null;
118
119     function __construct($user, $other)
120     {
121         parent::__construct($user);
122         $this->other = $other;
123     }
124
125     function execute($channel)
126     {
127         if(substr($this->other,0,1)=='#'){
128             //favoriting a specific notice_id
129
130             $notice = Notice::staticGet(substr($this->other,1));
131             if (!$notice) {
132                 $channel->error($this->user, _('Notice with that id does not exist'));
133                 return;
134             }
135             $recipient = $notice->getProfile();
136         }else{
137             //favoriting a given user's last notice
138
139             $recipient =
140               common_relative_profile($this->user, common_canonical_nickname($this->other));
141
142             if (!$recipient) {
143                 $channel->error($this->user, _('No such user.'));
144                 return;
145             }
146             $notice = $recipient->getCurrentNotice();
147             if (!$notice) {
148                 $channel->error($this->user, _('User has no last notice'));
149                 return;
150             }
151         }
152
153         $fave = Fave::addNew($this->user, $notice);
154
155         if (!$fave) {
156             $channel->error($this->user, _('Could not create favorite.'));
157             return;
158         }
159
160         $other = User::staticGet('id', $recipient->id);
161
162         if ($other && $other->id != $user->id) {
163             if ($other->email && $other->emailnotifyfav) {
164                 mail_notify_fave($other, $this->user, $notice);
165             }
166         }
167
168         $this->user->blowFavesCache();
169
170         $channel->output($this->user, _('Notice marked as fave.'));
171     }
172
173 }
174 class JoinCommand extends Command
175 {
176     var $other = null;
177
178     function __construct($user, $other)
179     {
180         parent::__construct($user);
181         $this->other = $other;
182     }
183
184     function execute($channel)
185     {
186
187         $nickname = common_canonical_nickname($this->other);
188         $group    = User_group::staticGet('nickname', $nickname);
189         $cur      = $this->user;
190
191         if (!$group) {
192             $channel->error($cur, _('No such group.'));
193             return;
194         }
195
196         if ($cur->isMember($group)) {
197             $channel->error($cur, _('You are already a member of that group'));
198             return;
199         }
200         if (Group_block::isBlocked($group, $cur->getProfile())) {
201           $channel->error($cur, _('You have been blocked from that group by the admin.'));
202             return;
203         }
204
205         $member = new Group_member();
206
207         $member->group_id   = $group->id;
208         $member->profile_id = $cur->id;
209         $member->created    = common_sql_now();
210
211         $result = $member->insert();
212         if (!$result) {
213           common_log_db_error($member, 'INSERT', __FILE__);
214           $channel->error($cur, sprintf(_('Could not join user %s to group %s'),
215                                        $cur->nickname, $group->nickname));
216           return;
217         }
218
219         $channel->output($cur, sprintf(_('%s joined group %s'),
220                                               $cur->nickname,
221                                               $group->nickname));
222     }
223
224 }
225 class DropCommand extends Command
226 {
227     var $other = null;
228
229     function __construct($user, $other)
230     {
231         parent::__construct($user);
232         $this->other = $other;
233     }
234
235     function execute($channel)
236     {
237
238         $nickname = common_canonical_nickname($this->other);
239         $group    = User_group::staticGet('nickname', $nickname);
240         $cur      = $this->user;
241
242         if (!$group) {
243             $channel->error($cur, _('No such group.'));
244             return;
245         }
246
247         if (!$cur->isMember($group)) {
248             $channel->error($cur, _('You are not a member of that group.'));
249             return;
250         }
251
252         $member = new Group_member();
253
254         $member->group_id   = $group->id;
255         $member->profile_id = $cur->id;
256
257         if (!$member->find(true)) {
258           $channel->error($cur,_('Could not find membership record.'));
259           return;
260         }
261         $result = $member->delete();
262         if (!$result) {
263           common_log_db_error($member, 'INSERT', __FILE__);
264           $channel->error($cur, sprintf(_('Could not remove user %s to group %s'),
265                                        $cur->nickname, $group->nickname));
266           return;
267         }
268
269         $channel->output($cur, sprintf(_('%s left group %s'),
270                                               $cur->nickname,
271                                               $group->nickname));
272     }
273
274 }
275
276 class WhoisCommand extends Command
277 {
278     var $other = null;
279     function __construct($user, $other)
280     {
281         parent::__construct($user);
282         $this->other = $other;
283     }
284
285     function execute($channel)
286     {
287         $recipient =
288           common_relative_profile($this->user, common_canonical_nickname($this->other));
289
290         if (!$recipient) {
291             $channel->error($this->user, _('No such user.'));
292             return;
293         }
294
295         $whois = sprintf(_("%1\$s (%2\$s)"), $recipient->nickname,
296                          $recipient->profileurl);
297         if ($recipient->fullname) {
298             $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname);
299         }
300         if ($recipient->location) {
301             $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location);
302         }
303         if ($recipient->homepage) {
304             $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage);
305         }
306         if ($recipient->bio) {
307             $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio);
308         }
309         $channel->output($this->user, $whois);
310     }
311 }
312
313 class MessageCommand extends Command
314 {
315     var $other = null;
316     var $text = null;
317     function __construct($user, $other, $text)
318     {
319         parent::__construct($user);
320         $this->other = $other;
321         $this->text = $text;
322     }
323
324     function execute($channel)
325     {
326         $other = User::staticGet('nickname', common_canonical_nickname($this->other));
327
328         $len = mb_strlen($this->text);
329
330         if ($len == 0) {
331             $channel->error($this->user, _('No content!'));
332             return;
333         }
334
335         $this->text = common_shorten_links($this->text);
336
337         if (Message::contentTooLong($this->text)) {
338             $channel->error($this->user, sprintf(_('Message too long - maximum is %d characters, you sent %d'),
339                                                  Message::maxContent(), mb_strlen($this->text)));
340             return;
341         }
342
343         if (!$other) {
344             $channel->error($this->user, _('No such user.'));
345             return;
346         } else if (!$this->user->mutuallySubscribed($other)) {
347             $channel->error($this->user, _('You can\'t send a message to this user.'));
348             return;
349         } else if ($this->user->id == $other->id) {
350             $channel->error($this->user, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
351             return;
352         }
353         $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
354         if ($message) {
355             $channel->output($this->user, sprintf(_('Direct message to %s sent'), $this->other));
356         } else {
357             $channel->error($this->user, _('Error sending direct message.'));
358         }
359     }
360 }
361
362 class ReplyCommand extends Command
363 {
364     var $other = null;
365     var $text = null;
366     function __construct($user, $other, $text)
367     {
368         parent::__construct($user);
369         $this->other = $other;
370         $this->text = $text;
371     }
372
373     function execute($channel)
374     {
375         if(substr($this->other,0,1)=='#'){
376             //replying to a specific notice_id
377
378             $notice = Notice::staticGet(substr($this->other,1));
379             if (!$notice) {
380                 $channel->error($this->user, _('Notice with that id does not exist'));
381                 return;
382             }
383             $recipient = $notice->getProfile();
384         }else{
385             //replying to a given user's last notice
386
387             $recipient =
388               common_relative_profile($this->user, common_canonical_nickname($this->other));
389
390             if (!$recipient) {
391                 $channel->error($this->user, _('No such user.'));
392                 return;
393             }
394             $notice = $recipient->getCurrentNotice();
395             if (!$notice) {
396                 $channel->error($this->user, _('User has no last notice'));
397                 return;
398             }
399         }
400
401         $len = mb_strlen($this->text);
402
403         if ($len == 0) {
404             $channel->error($this->user, _('No content!'));
405             return;
406         }
407
408         $this->text = common_shorten_links($this->text);
409
410         if (Notice::contentTooLong($this->text)) {
411             $channel->error($this->user, sprintf(_('Notice too long - maximum is %d characters, you sent %d'),
412                                                  Notice::maxContent(), mb_strlen($this->text)));
413             return;
414         }
415
416         $notice = Notice::saveNew($this->user->id, $this->text, $channel->source(), 1,
417                                   $notice->id);
418         if ($notice) {
419             $channel->output($this->user, sprintf(_('Reply to %s sent'), $recipient->nickname));
420         } else {
421             $channel->error($this->user, _('Error saving notice.'));
422         }
423         common_broadcast_notice($notice);
424     }
425 }
426
427 class GetCommand extends Command
428 {
429
430     var $other = null;
431
432     function __construct($user, $other)
433     {
434         parent::__construct($user);
435         $this->other = $other;
436     }
437
438     function execute($channel)
439     {
440         $target_nickname = common_canonical_nickname($this->other);
441
442         $target =
443           common_relative_profile($this->user, $target_nickname);
444
445         if (!$target) {
446             $channel->error($this->user, _('No such user.'));
447             return;
448         }
449         $notice = $target->getCurrentNotice();
450         if (!$notice) {
451             $channel->error($this->user, _('User has no last notice'));
452             return;
453         }
454         $notice_content = $notice->content;
455
456         $channel->output($this->user, $target_nickname . ": " . $notice_content);
457     }
458 }
459
460 class SubCommand extends Command
461 {
462
463     var $other = null;
464
465     function __construct($user, $other)
466     {
467         parent::__construct($user);
468         $this->other = $other;
469     }
470
471     function execute($channel)
472     {
473
474         if (!$this->other) {
475             $channel->error($this->user, _('Specify the name of the user to subscribe to'));
476             return;
477         }
478
479         $result = subs_subscribe_user($this->user, $this->other);
480
481         if ($result == 'true') {
482             $channel->output($this->user, sprintf(_('Subscribed to %s'), $this->other));
483         } else {
484             $channel->error($this->user, $result);
485         }
486     }
487 }
488
489 class UnsubCommand extends Command
490 {
491
492     var $other = null;
493
494     function __construct($user, $other)
495     {
496         parent::__construct($user);
497         $this->other = $other;
498     }
499
500     function execute($channel)
501     {
502         if(!$this->other) {
503             $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
504             return;
505         }
506
507         $result=subs_unsubscribe_user($this->user, $this->other);
508
509         if ($result) {
510             $channel->output($this->user, sprintf(_('Unsubscribed from %s'), $this->other));
511         } else {
512             $channel->error($this->user, $result);
513         }
514     }
515 }
516
517 class OffCommand extends Command
518 {
519     var $other = null;
520     function __construct($user, $other=null)
521     {
522         parent::__construct($user);
523         $this->other = $other;
524     }
525     function execute($channel)
526     {
527         if ($other) {
528             $channel->error($this->user, _("Command not yet implemented."));
529         } else {
530             if ($channel->off($this->user)) {
531                 $channel->output($this->user, _('Notification off.'));
532             } else {
533                 $channel->error($this->user, _('Can\'t turn off notification.'));
534             }
535         }
536     }
537 }
538
539 class OnCommand extends Command
540 {
541     var $other = null;
542     function __construct($user, $other=null)
543     {
544         parent::__construct($user);
545         $this->other = $other;
546     }
547
548     function execute($channel)
549     {
550         if ($other) {
551             $channel->error($this->user, _("Command not yet implemented."));
552         } else {
553             if ($channel->on($this->user)) {
554                 $channel->output($this->user, _('Notification on.'));
555             } else {
556                 $channel->error($this->user, _('Can\'t turn on notification.'));
557             }
558         }
559     }
560 }
561
562 class HelpCommand extends Command
563 {
564     function execute($channel)
565     {
566         $channel->output($this->user,
567                          _("Commands:\n".
568                            "on - turn on notifications\n".
569                            "off - turn off notifications\n".
570                            "help - show this help\n".
571                            "follow <nickname> - subscribe to user\n".
572                            "leave <nickname> - unsubscribe from user\n".
573                            "d <nickname> <text> - direct message to user\n".
574                            "get <nickname> - get last notice from user\n".
575                            "whois <nickname> - get profile info on user\n".
576                            "fav <nickname> - add user's last notice as a 'fave'\n".
577                            "fav #<notice_id> - add notice with the given id as a 'fave'\n".
578                            "reply #<notice_id> - reply to notice with a given id\n".
579                            "reply <nickname> - reply to the last notice from user\n".
580                            "join <group> - join group\n".
581                            "drop <group> - leave group\n".
582                            "stats - get your stats\n".
583                            "stop - same as 'off'\n".
584                            "quit - same as 'off'\n".
585                            "sub <nickname> - same as 'follow'\n".
586                            "unsub <nickname> - same as 'leave'\n".
587                            "last <nickname> - same as 'get'\n".
588                            "on <nickname> - not yet implemented.\n".
589                            "off <nickname> - not yet implemented.\n".
590                            "nudge <nickname> - not yet implemented.\n".
591                            "invite <phone number> - not yet implemented.\n".
592                            "track <word> - not yet implemented.\n".
593                            "untrack <word> - not yet implemented.\n".
594                            "track off - not yet implemented.\n".
595                            "untrack all - not yet implemented.\n".
596                            "tracks - not yet implemented.\n".
597                            "tracking - not yet implemented.\n"));
598     }
599 }