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