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