]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/command.php
Merge branch 'master' into testing
[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
128         $recipient =
129           common_relative_profile($this->user, common_canonical_nickname($this->other));
130
131         if (!$recipient) {
132             $channel->error($this->user, _('No such user.'));
133             return;
134         }
135         $notice = $recipient->getCurrentNotice();
136         if (!$notice) {
137             $channel->error($this->user, _('User has no last notice'));
138             return;
139         }
140
141         $fave = Fave::addNew($this->user, $notice);
142
143         if (!$fave) {
144             $channel->error($this->user, _('Could not create favorite.'));
145             return;
146         }
147
148         $other = User::staticGet('id', $recipient->id);
149
150         if ($other && $other->id != $user->id) {
151             if ($other->email && $other->emailnotifyfav) {
152                 mail_notify_fave($other, $this->user, $notice);
153             }
154         }
155
156         $this->user->blowFavesCache();
157
158         $channel->output($this->user, _('Notice marked as fave.'));
159     }
160
161 }
162 class JoinCommand extends Command
163 {
164     var $other = null;
165
166     function __construct($user, $other)
167     {
168         parent::__construct($user);
169         $this->other = $other;
170     }
171
172     function execute($channel)
173     {
174
175         $nickname = common_canonical_nickname($this->other);
176         $group    = User_group::staticGet('nickname', $nickname);
177         $cur      = $this->user;
178
179         if (!$group) {
180             $channel->error($cur, _('No such group.'));
181             return;
182         }
183
184         if ($cur->isMember($group)) {
185             $channel->error($cur, _('You are already a member of that group'));
186             return;
187         }
188         if (Group_block::isBlocked($group, $cur->getProfile())) {
189           $channel->error($cur, _('You have been blocked from that group by the admin.'));
190             return;
191         }
192
193         $member = new Group_member();
194
195         $member->group_id   = $group->id;
196         $member->profile_id = $cur->id;
197         $member->created    = common_sql_now();
198
199         $result = $member->insert();
200         if (!$result) {
201           common_log_db_error($member, 'INSERT', __FILE__);
202           $channel->error($cur, sprintf(_('Could not join user %s to group %s'),
203                                        $cur->nickname, $group->nickname));
204           return;
205         }
206
207         $channel->output($cur, sprintf(_('%s joined group %s'),
208                                               $cur->nickname,
209                                               $group->nickname));
210     }
211
212 }
213 class DropCommand extends Command
214 {
215     var $other = null;
216
217     function __construct($user, $other)
218     {
219         parent::__construct($user);
220         $this->other = $other;
221     }
222
223     function execute($channel)
224     {
225
226         $nickname = common_canonical_nickname($this->other);
227         $group    = User_group::staticGet('nickname', $nickname);
228         $cur      = $this->user;
229
230         if (!$group) {
231             $channel->error($cur, _('No such group.'));
232             return;
233         }
234
235         if (!$cur->isMember($group)) {
236             $channel->error($cur, _('You are not a member of that group.'));
237             return;
238         }
239
240         $member = new Group_member();
241
242         $member->group_id   = $group->id;
243         $member->profile_id = $cur->id;
244
245         if (!$member->find(true)) {
246           $channel->error($cur,_('Could not find membership record.'));
247           return;
248         }
249         $result = $member->delete();
250         if (!$result) {
251           common_log_db_error($member, 'INSERT', __FILE__);
252           $channel->error($cur, sprintf(_('Could not remove user %s to group %s'),
253                                        $cur->nickname, $group->nickname));
254           return;
255         }
256
257         $channel->output($cur, sprintf(_('%s left group %s'),
258                                               $cur->nickname,
259                                               $group->nickname));
260     }
261
262 }
263
264 class WhoisCommand extends Command
265 {
266     var $other = null;
267     function __construct($user, $other)
268     {
269         parent::__construct($user);
270         $this->other = $other;
271     }
272
273     function execute($channel)
274     {
275         $recipient =
276           common_relative_profile($this->user, common_canonical_nickname($this->other));
277
278         if (!$recipient) {
279             $channel->error($this->user, _('No such user.'));
280             return;
281         }
282
283         $whois = sprintf(_("%1\$s (%2\$s)"), $recipient->nickname,
284                          $recipient->profileurl);
285         if ($recipient->fullname) {
286             $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname);
287         }
288         if ($recipient->location) {
289             $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location);
290         }
291         if ($recipient->homepage) {
292             $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage);
293         }
294         if ($recipient->bio) {
295             $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio);
296         }
297         $channel->output($this->user, $whois);
298     }
299 }
300
301 class MessageCommand extends Command
302 {
303     var $other = null;
304     var $text = null;
305     function __construct($user, $other, $text)
306     {
307         parent::__construct($user);
308         $this->other = $other;
309         $this->text = $text;
310     }
311
312     function execute($channel)
313     {
314         $other = User::staticGet('nickname', common_canonical_nickname($this->other));
315         $len = mb_strlen($this->text);
316         if ($len == 0) {
317             $channel->error($this->user, _('No content!'));
318             return;
319         } else if ($len > 140) {
320             $content = common_shorten_links($content);
321             if (mb_strlen($content) > 140) {
322                 $channel->error($this->user, sprintf(_('Message too long - maximum is 140 characters, you sent %d'), $len));
323                 return;
324             }
325         }
326
327         if (!$other) {
328             $channel->error($this->user, _('No such user.'));
329             return;
330         } else if (!$this->user->mutuallySubscribed($other)) {
331             $channel->error($this->user, _('You can\'t send a message to this user.'));
332             return;
333         } else if ($this->user->id == $other->id) {
334             $channel->error($this->user, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
335             return;
336         }
337         $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
338         if ($message) {
339             $channel->output($this->user, sprintf(_('Direct message to %s sent'), $this->other));
340         } else {
341             $channel->error($this->user, _('Error sending direct message.'));
342         }
343     }
344 }
345
346 class GetCommand extends Command
347 {
348
349     var $other = null;
350
351     function __construct($user, $other)
352     {
353         parent::__construct($user);
354         $this->other = $other;
355     }
356
357     function execute($channel)
358     {
359         $target_nickname = common_canonical_nickname($this->other);
360
361         $target =
362           common_relative_profile($this->user, $target_nickname);
363
364         if (!$target) {
365             $channel->error($this->user, _('No such user.'));
366             return;
367         }
368         $notice = $target->getCurrentNotice();
369         if (!$notice) {
370             $channel->error($this->user, _('User has no last notice'));
371             return;
372         }
373         $notice_content = $notice->content;
374
375         $channel->output($this->user, $target_nickname . ": " . $notice_content);
376     }
377 }
378
379 class SubCommand extends Command
380 {
381
382     var $other = null;
383
384     function __construct($user, $other)
385     {
386         parent::__construct($user);
387         $this->other = $other;
388     }
389
390     function execute($channel)
391     {
392
393         if (!$this->other) {
394             $channel->error($this->user, _('Specify the name of the user to subscribe to'));
395             return;
396         }
397
398         $result = subs_subscribe_user($this->user, $this->other);
399
400         if ($result == 'true') {
401             $channel->output($this->user, sprintf(_('Subscribed to %s'), $this->other));
402         } else {
403             $channel->error($this->user, $result);
404         }
405     }
406 }
407
408 class UnsubCommand extends Command
409 {
410
411     var $other = null;
412
413     function __construct($user, $other)
414     {
415         parent::__construct($user);
416         $this->other = $other;
417     }
418
419     function execute($channel)
420     {
421         if(!$this->other) {
422             $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
423             return;
424         }
425
426         $result=subs_unsubscribe_user($this->user, $this->other);
427
428         if ($result) {
429             $channel->output($this->user, sprintf(_('Unsubscribed from %s'), $this->other));
430         } else {
431             $channel->error($this->user, $result);
432         }
433     }
434 }
435
436 class OffCommand extends Command
437 {
438     var $other = null;
439     function __construct($user, $other=null)
440     {
441         parent::__construct($user);
442         $this->other = $other;
443     }
444     function execute($channel)
445     {
446         if ($other) {
447             $channel->error($this->user, _("Command not yet implemented."));
448         } else {
449             if ($channel->off($this->user)) {
450                 $channel->output($this->user, _('Notification off.'));
451             } else {
452                 $channel->error($this->user, _('Can\'t turn off notification.'));
453             }
454         }
455     }
456 }
457
458 class OnCommand extends Command
459 {
460     var $other = null;
461     function __construct($user, $other=null)
462     {
463         parent::__construct($user);
464         $this->other = $other;
465     }
466
467     function execute($channel)
468     {
469         if ($other) {
470             $channel->error($this->user, _("Command not yet implemented."));
471         } else {
472             if ($channel->on($this->user)) {
473                 $channel->output($this->user, _('Notification on.'));
474             } else {
475                 $channel->error($this->user, _('Can\'t turn on notification.'));
476             }
477         }
478     }
479 }
480
481 class HelpCommand extends Command
482 {
483     function execute($channel)
484     {
485         $channel->output($this->user,
486                          _("Commands:\n".
487                            "on - turn on notifications\n".
488                            "off - turn off notifications\n".
489                            "help - show this help\n".
490                            "follow <nickname> - subscribe to user\n".
491                            "leave <nickname> - unsubscribe from user\n".
492                            "d <nickname> <text> - direct message to user\n".
493                            "get <nickname> - get last notice from user\n".
494                            "whois <nickname> - get profile info on user\n".
495                            "fav <nickname> - add user's last notice as a 'fave'\n".
496                            "join <group> - join group\n".
497                            "drop <group> - leave group\n".
498                            "stats - get your stats\n".
499                            "stop - same as 'off'\n".
500                            "quit - same as 'off'\n".
501                            "sub <nickname> - same as 'follow'\n".
502                            "unsub <nickname> - same as 'leave'\n".
503                            "last <nickname> - same as 'get'\n".
504                            "on <nickname> - not yet implemented.\n".
505                            "off <nickname> - not yet implemented.\n".
506                            "nudge <nickname> - not yet implemented.\n".
507                            "invite <phone number> - not yet implemented.\n".
508                            "track <word> - not yet implemented.\n".
509                            "untrack <word> - not yet implemented.\n".
510                            "track off - not yet implemented.\n".
511                            "untrack all - not yet implemented.\n".
512                            "tracks - not yet implemented.\n".
513                            "tracking - not yet implemented.\n"));
514     }
515 }