]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/command.php
507990a0b3ac6a6ba6d5315a9c3c605f0050b957
[quix0rs-gnu-social.git] / lib / command.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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('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
101         $subs = new Subscription();
102         $subs->subscriber = $this->user->id;
103         $subs_count = (int) $subs->count() - 1;
104
105         $subbed = new Subscription();
106         $subbed->subscribed = $this->user->id;
107         $subbed_count = (int) $subbed->count() - 1;
108
109         $notices = new Notice();
110         $notices->profile_id = $this->user->id;
111         $notice_count = (int) $notices->count();
112
113         $channel->output($this->user, sprintf(_("Subscriptions: %1\$s\n".
114                                    "Subscribers: %2\$s\n".
115                                    "Notices: %3\$s"),
116                                  $subs_count,
117                                  $subbed_count,
118                                  $notice_count));
119     }
120 }
121
122 class FavCommand extends Command
123 {
124
125     var $other = null;
126
127     function __construct($user, $other)
128     {
129         parent::__construct($user);
130         $this->other = $other;
131     }
132
133     function execute($channel)
134     {
135
136         $recipient =
137           common_relative_profile($this->user, common_canonical_nickname($this->other));
138
139         if (!$recipient) {
140             $channel->error($this->user, _('No such user.'));
141             return;
142         }
143         $notice = $recipient->getCurrentNotice();
144         if (!$notice) {
145             $channel->error($this->user, _('User has no last notice'));
146             return;
147         }
148
149         $fave = Fave::addNew($this->user, $notice);
150
151         if (!$fave) {
152             $channel->error($this->user, _('Could not create favorite.'));
153             return;
154         }
155
156         $other = User::staticGet('id', $recipient->id);
157
158         if ($other && $other->id != $user->id) {
159             if ($other->email && $other->emailnotifyfav) {
160                 mail_notify_fave($other, $this->user, $notice);
161             }
162         }
163
164         $this->user->blowFavesCache();
165
166         $channel->output($this->user, _('Notice marked as fave.'));
167     }
168 }
169
170 class WhoisCommand extends Command
171 {
172     var $other = null;
173     function __construct($user, $other)
174     {
175         parent::__construct($user);
176         $this->other = $other;
177     }
178
179     function execute($channel)
180     {
181         $recipient =
182           common_relative_profile($this->user, common_canonical_nickname($this->other));
183
184         if (!$recipient) {
185             $channel->error($this->user, _('No such user.'));
186             return;
187         }
188
189         $whois = sprintf(_("%1\$s (%2\$s)"), $recipient->nickname,
190                          $recipient->profileurl);
191         if ($recipient->fullname) {
192             $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname);
193         }
194         if ($recipient->location) {
195             $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location);
196         }
197         if ($recipient->homepage) {
198             $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage);
199         }
200         if ($recipient->bio) {
201             $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio);
202         }
203         $channel->output($this->user, $whois);
204     }
205 }
206
207 class MessageCommand extends Command
208 {
209     var $other = null;
210     var $text = null;
211     function __construct($user, $other, $text)
212     {
213         parent::__construct($user);
214         $this->other = $other;
215         $this->text = $text;
216     }
217
218     function execute($channel)
219     {
220         $other = User::staticGet('nickname', common_canonical_nickname($this->other));
221         $len = mb_strlen($this->text);
222         if ($len == 0) {
223             $channel->error($this->user, _('No content!'));
224             return;
225         } else if ($len > 140) {
226             $content = common_shorten_links($content);
227             if (mb_strlen($content) > 140) {
228                 $channel->error($this->user, sprintf(_('Message too long - maximum is 140 characters, you sent %d'), $len));
229                 return;
230             }
231         }
232
233         if (!$other) {
234             $channel->error($this->user, _('No such user.'));
235             return;
236         } else if (!$this->user->mutuallySubscribed($other)) {
237             $channel->error($this->user, _('You can\'t send a message to this user.'));
238             return;
239         } else if ($this->user->id == $other->id) {
240             $channel->error($this->user, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
241             return;
242         }
243         $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
244         if ($message) {
245             $channel->output($this->user, sprintf(_('Direct message to %s sent'), $this->other));
246         } else {
247             $channel->error($this->user, _('Error sending direct message.'));
248         }
249     }
250 }
251
252 class GetCommand extends Command
253 {
254
255     var $other = null;
256
257     function __construct($user, $other)
258     {
259         parent::__construct($user);
260         $this->other = $other;
261     }
262
263     function execute($channel)
264     {
265         $target_nickname = common_canonical_nickname($this->other);
266
267         $target =
268           common_relative_profile($this->user, $target_nickname);
269
270         if (!$target) {
271             $channel->error($this->user, _('No such user.'));
272             return;
273         }
274         $notice = $target->getCurrentNotice();
275         if (!$notice) {
276             $channel->error($this->user, _('User has no last notice'));
277             return;
278         }
279         $notice_content = $notice->content;
280
281         $channel->output($this->user, $target_nickname . ": " . $notice_content);
282     }
283 }
284
285 class SubCommand extends Command
286 {
287
288     var $other = null;
289
290     function __construct($user, $other)
291     {
292         parent::__construct($user);
293         $this->other = $other;
294     }
295
296     function execute($channel)
297     {
298
299         if (!$this->other) {
300             $channel->error($this->user, _('Specify the name of the user to subscribe to'));
301             return;
302         }
303
304         $result = subs_subscribe_user($this->user, $this->other);
305
306         if ($result == 'true') {
307             $channel->output($this->user, sprintf(_('Subscribed to %s'), $this->other));
308         } else {
309             $channel->error($this->user, $result);
310         }
311     }
312 }
313
314 class UnsubCommand extends Command
315 {
316
317     var $other = null;
318
319     function __construct($user, $other)
320     {
321         parent::__construct($user);
322         $this->other = $other;
323     }
324
325     function execute($channel)
326     {
327         if(!$this->other) {
328             $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
329             return;
330         }
331
332         $result=subs_unsubscribe_user($this->user, $this->other);
333
334         if ($result) {
335             $channel->output($this->user, sprintf(_('Unsubscribed from %s'), $this->other));
336         } else {
337             $channel->error($this->user, $result);
338         }
339     }
340 }
341
342 class OffCommand extends Command
343 {
344     var $other = null;
345     function __construct($user, $other=null)
346     {
347         parent::__construct($user);
348         $this->other = $other;
349     }
350     function execute($channel)
351     {
352         if ($other) {
353             $channel->error($this->user, _("Command not yet implemented."));
354         } else {
355             if ($channel->off($this->user)) {
356                 $channel->output($this->user, _('Notification off.'));
357             } else {
358                 $channel->error($this->user, _('Can\'t turn off notification.'));
359             }
360         }
361     }
362 }
363
364 class OnCommand extends Command
365 {
366     var $other = null;
367     function __construct($user, $other=null)
368     {
369         parent::__construct($user);
370         $this->other = $other;
371     }
372
373     function execute($channel)
374     {
375         if ($other) {
376             $channel->error($this->user, _("Command not yet implemented."));
377         } else {
378             if ($channel->on($this->user)) {
379                 $channel->output($this->user, _('Notification on.'));
380             } else {
381                 $channel->error($this->user, _('Can\'t turn on notification.'));
382             }
383         }
384     }
385 }
386
387 class HelpCommand extends Command
388 {
389     function execute($channel)
390     {
391         $channel->output($this->user,
392                          _("Commands:\n".
393                            "on - turn on notifications\n".
394                            "off - turn off notifications\n".
395                            "help - show this help\n".
396                            "follow <nickname> - subscribe to user\n".
397                            "leave <nickname> - unsubscribe from user\n".
398                            "d <nickname> <text> - direct message to user\n".
399                            "get <nickname> - get last notice from user\n".
400                            "whois <nickname> - get profile info on user\n".
401                            "fav <nickname> - add user's last notice as a 'fave'\n".
402                            "stats - get your stats\n".
403                            "stop - same as 'off'\n".
404                            "quit - same as 'off'\n".
405                            "sub <nickname> - same as 'follow'\n".
406                            "unsub <nickname> - same as 'leave'\n".
407                            "last <nickname> - same as 'get'\n".
408                            "on <nickname> - not yet implemented.\n".
409                            "off <nickname> - not yet implemented.\n".
410                            "nudge <nickname> - not yet implemented.\n".
411                            "invite <phone number> - not yet implemented.\n".
412                            "track <word> - not yet implemented.\n".
413                            "untrack <word> - not yet implemented.\n".
414                            "track off - not yet implemented.\n".
415                            "untrack all - not yet implemented.\n".
416                            "tracks - not yet implemented.\n".
417                            "tracking - not yet implemented.\n"));
418     }
419 }