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