]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/command.php
Implemented reply # command, allowing users to favorite specific notices by the notice id
[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             //replying to 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             //replying to 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 GetCommand extends Command
363 {
364
365     var $other = null;
366
367     function __construct($user, $other)
368     {
369         parent::__construct($user);
370         $this->other = $other;
371     }
372
373     function execute($channel)
374     {
375         $target_nickname = common_canonical_nickname($this->other);
376
377         $target =
378           common_relative_profile($this->user, $target_nickname);
379
380         if (!$target) {
381             $channel->error($this->user, _('No such user.'));
382             return;
383         }
384         $notice = $target->getCurrentNotice();
385         if (!$notice) {
386             $channel->error($this->user, _('User has no last notice'));
387             return;
388         }
389         $notice_content = $notice->content;
390
391         $channel->output($this->user, $target_nickname . ": " . $notice_content);
392     }
393 }
394
395 class SubCommand extends Command
396 {
397
398     var $other = null;
399
400     function __construct($user, $other)
401     {
402         parent::__construct($user);
403         $this->other = $other;
404     }
405
406     function execute($channel)
407     {
408
409         if (!$this->other) {
410             $channel->error($this->user, _('Specify the name of the user to subscribe to'));
411             return;
412         }
413
414         $result = subs_subscribe_user($this->user, $this->other);
415
416         if ($result == 'true') {
417             $channel->output($this->user, sprintf(_('Subscribed to %s'), $this->other));
418         } else {
419             $channel->error($this->user, $result);
420         }
421     }
422 }
423
424 class UnsubCommand extends Command
425 {
426
427     var $other = null;
428
429     function __construct($user, $other)
430     {
431         parent::__construct($user);
432         $this->other = $other;
433     }
434
435     function execute($channel)
436     {
437         if(!$this->other) {
438             $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
439             return;
440         }
441
442         $result=subs_unsubscribe_user($this->user, $this->other);
443
444         if ($result) {
445             $channel->output($this->user, sprintf(_('Unsubscribed from %s'), $this->other));
446         } else {
447             $channel->error($this->user, $result);
448         }
449     }
450 }
451
452 class OffCommand extends Command
453 {
454     var $other = null;
455     function __construct($user, $other=null)
456     {
457         parent::__construct($user);
458         $this->other = $other;
459     }
460     function execute($channel)
461     {
462         if ($other) {
463             $channel->error($this->user, _("Command not yet implemented."));
464         } else {
465             if ($channel->off($this->user)) {
466                 $channel->output($this->user, _('Notification off.'));
467             } else {
468                 $channel->error($this->user, _('Can\'t turn off notification.'));
469             }
470         }
471     }
472 }
473
474 class OnCommand extends Command
475 {
476     var $other = null;
477     function __construct($user, $other=null)
478     {
479         parent::__construct($user);
480         $this->other = $other;
481     }
482
483     function execute($channel)
484     {
485         if ($other) {
486             $channel->error($this->user, _("Command not yet implemented."));
487         } else {
488             if ($channel->on($this->user)) {
489                 $channel->output($this->user, _('Notification on.'));
490             } else {
491                 $channel->error($this->user, _('Can\'t turn on notification.'));
492             }
493         }
494     }
495 }
496
497 class HelpCommand extends Command
498 {
499     function execute($channel)
500     {
501         $channel->output($this->user,
502                          _("Commands:\n".
503                            "on - turn on notifications\n".
504                            "off - turn off notifications\n".
505                            "help - show this help\n".
506                            "follow <nickname> - subscribe to user\n".
507                            "leave <nickname> - unsubscribe from user\n".
508                            "d <nickname> <text> - direct message to user\n".
509                            "get <nickname> - get last notice from user\n".
510                            "whois <nickname> - get profile info on user\n".
511                            "fav <nickname> - add user's last notice as a 'fave'\n".
512                            "fav #<notice_id> - add notice with the given id as a 'fave'\n".
513                            "join <group> - join group\n".
514                            "drop <group> - leave group\n".
515                            "stats - get your stats\n".
516                            "stop - same as 'off'\n".
517                            "quit - same as 'off'\n".
518                            "sub <nickname> - same as 'follow'\n".
519                            "unsub <nickname> - same as 'leave'\n".
520                            "last <nickname> - same as 'get'\n".
521                            "on <nickname> - not yet implemented.\n".
522                            "off <nickname> - not yet implemented.\n".
523                            "nudge <nickname> - not yet implemented.\n".
524                            "invite <phone number> - not yet implemented.\n".
525                            "track <word> - not yet implemented.\n".
526                            "untrack <word> - not yet implemented.\n".
527                            "track off - not yet implemented.\n".
528                            "untrack all - not yet implemented.\n".
529                            "tracks - not yet implemented.\n".
530                            "tracking - not yet implemented.\n"));
531     }
532 }