]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Command.php
1a6d7cc0661600716fc0267555df945540a68e58
[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 FaveCommand 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                 mail_notify_fave($recipient, $this->user, $notice);
137                 $this->user->blowFavesCache();
138                 
139                 $channel->output($this->user, _('Message marked as fave.'));
140         }
141 }
142
143 class WhoisCommand extends Command {
144         var $other = NULL;
145         function __construct($user, $other) {
146                 parent::__construct($user);
147                 $this->other = $other;
148         }
149         
150         function execute($channel) {
151                 $recipient = 
152                   common_relative_profile($this->user, common_canonical_nickname($this->other));
153                 
154                 if (!$recipient) {
155                         $channel->error($this->user, _('No such user.'));
156                         return;
157                 }
158                 
159                 $whois = sprintf(_("%1\$s (%2\$s)"), $recipient->nickname,
160                                                  $recipient->profileurl);
161                 if ($recipient->fullname) {
162                         $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname);
163                 }
164                 if ($recipient->location) {
165                         $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location);
166                 }
167                 if ($recipient->homepage) {
168                         $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage);
169                 }
170                 if ($recipient->bio) {
171                         $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio);
172                 }
173                 $channel->output($this->user, $whois);
174         }
175 }
176
177 class MessageCommand extends Command {
178         var $other = NULL;
179         var $text = NULL;
180         function __construct($user, $other, $text) {
181                 parent::__construct($user);
182                 $this->other = $other;
183                 $this->text = $other;           
184         }
185         
186         function execute($channel) {
187                 $other = User::staticGet('nickname', common_canonical_nickname($this->other));
188                 $len = mb_strlen($this->text);
189                 if ($len == 0) {
190                         $channel->error($this->user, _('No content!'));
191                         return;
192                 } else if ($len > 140) {
193                         $channel->error($this->user,sprintf(_('Message too long - maximum is 140 characters, you sent %d'), $len));
194                         return;
195                 } else if (!$other) {
196                         $channel->error($this->user, _('No such user.'));
197                         return;
198                 } else if (!$user->mutuallySubscribed($other)) {
199                         $channel->error($this->user, _('You can\'t send a message to this user.'));
200                         return;
201                 } else if ($user->id == $other->id) {
202                         $channel->error($this->user, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
203                         return;
204                 }
205                 $message = Message::saveNew($user->id, $other->id, $body, 'xmpp');
206                 if ($message) {
207                         $channel->output($this->user, sprintf(_('Direct message to %s sent'), $this->other));
208                 } else {
209                         $channel->error($this->user, _('Error sending direct message.'));
210                 }
211         }
212 }
213
214 class GetCommand extends Command {
215         
216         var $other = NULL;
217         
218         function __construct($user, $other) {
219                 parent::__construct($user);
220                 $this->other = $other;
221         }
222         
223         function execute($channel) {
224                 $target = 
225                   common_relative_profile($this->user, common_canonical_nickname($this->other));
226
227                 if (!$target) {
228                         $channel->error($this->user, _('No such user.'));
229                         return;
230                 }
231                 $notice = $target->getCurrentNotice();
232                 if (!$notice) {
233                         $channel->error($this->user, _('User has no last notice'));
234                         return;
235                 }
236                 $notice_content = $notice->content;
237                 $channel->output($this->user, $target_nickname . ": " . $notice_content);
238         }
239 }
240
241 class SubCommand extends Command {
242         
243         var $other = NULL;
244         
245         function __construct($user, $other) {
246                 parent::__construct($user);
247                 $this->other = $other;
248         }
249         
250         function execute($channel) {
251                 
252                 if (!$this->other) {
253                         $channel->error($this->user, _('Specify the name of the user to subscribe to'));
254                         return;
255                 }
256                 
257                 $result = subs_subscribe_user($user, $this->other);
258                 
259                 if ($result == 'true') {
260                         $channel->output($this->user, sprintf(_('Subscribed to %s'), $this->other));
261                 } else {
262                         $channel->error($this->user, $result);
263                 }
264         }
265 }
266
267 class UnsubCommand extends Command {
268
269         var $other = NULL;
270         
271         function __construct($user, $other) {
272                 parent::__construct($user);
273                 $this->other = $other;
274         }
275
276         function execute($channel) {
277                 if(!$this->other) {
278                         $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
279                         return;
280                 }
281                 
282                 $result=subs_unsubscribe_user($user, $this->other);
283                 
284                 if ($result) {
285                         $channel->output($this->user, sprintf(_('Unsubscribed from %s'), $this->other));
286                 } else {
287                         $channel->error($this->user, $result);
288                 }
289         }
290 }
291
292 class OffCommand extends Command {
293         var $other = NULL;
294         function __construct($user, $other=NULL) {
295                 parent::__construct($user);
296                 $this->other = $other;
297         }
298         function execute($channel) {
299                 if ($other) {
300                         $channel->error($this->user, _("Command not yet implemented."));
301                 } else {
302                         $channel->off($this->user);
303                 }
304         }
305 }
306
307 class OnCommand extends Command {
308         var $other = NULL;
309         function __construct($user, $other=NULL) {
310                 parent::__construct($user);
311                 $this->other = $other;
312         }
313         
314         function execute($channel) {
315                 if ($other) {
316                         $channel->error($this->user, _("Command not yet implemented."));
317                 } else {
318                         $channel->on($this->user);
319                 }
320         }
321 }
322
323 class HelpCommand extends Command {
324         function execute($channel) {
325                 $channel->output($this->user,
326                                                  _("Commands:\n".
327                                                    "on - turn on notifications\n".
328                                                    "off - turn off notifications\n".
329                                                    "help - show this help\n".
330                                                    "follow <nickname> - subscribe to user\n".
331                                                    "leave <nickname> - unsubscribe from user\n".
332                                                    "d <nickname> <text> - direct message to user\n".
333                                                    "get <nickname> - get last notice from user\n".
334                                                    "whois <nickname> - get profile info on user\n".
335                                                    "fav <nickname> - add user's last notice as a 'fave'\n".
336                                                    "stats - get your stats\n".
337                                                    "stop - same as 'off'\n".
338                                                    "quit - same as 'off'\n".
339                                                    "sub <nickname> - same as 'follow'\n".
340                                                    "unsub <nickname> - same as 'leave'\n".
341                                                    "last <nickname> - same as 'get'\n".
342                                                    "on <nickname> - not yet implemented.\n".
343                                                    "off <nickname> - not yet implemented.\n".                                              
344                                                    "nudge <nickname> - not yet implemented.\n".
345                                                    "invite <phone number> - not yet implemented.\n".
346                                                    "track <word> - not yet implemented.\n".
347                                                    "untrack <word> - not yet implemented.\n".
348                                                    "track off - not yet implemented.\n".
349                                                    "untrack all - not yet implemented.\n".
350                                                    "tracks - not yet implemented.\n".
351                                                    "tracking - not yet implemented.\n"));
352         }
353 }