]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - _darcs/pristine/classes/Command.php
replace all tabs with four spaces
[quix0rs-gnu-social.git] / _darcs / pristine / 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             $content = common_shorten_links($content);
201             if (mb_strlen($content) > 140) {
202                 $channel->error($this->user, sprintf(_('Message too long - maximum is 140 characters, you sent %d'), $len));
203                 return;
204             }
205         }
206         
207         if (!$other) {
208             $channel->error($this->user, _('No such user.'));
209             return;
210         } else if (!$this->user->mutuallySubscribed($other)) {
211             $channel->error($this->user, _('You can\'t send a message to this user.'));
212             return;
213         } else if ($this->user->id == $other->id) {
214             $channel->error($this->user, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
215             return;
216         }
217         $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
218         if ($message) {
219             $channel->output($this->user, sprintf(_('Direct message to %s sent'), $this->other));
220         } else {
221             $channel->error($this->user, _('Error sending direct message.'));
222         }
223     }
224 }
225
226 class GetCommand extends Command {
227     
228     var $other = NULL;
229     
230     function __construct($user, $other) {
231         parent::__construct($user);
232         $this->other = $other;
233     }
234     
235     function execute($channel) {
236         $target_nickname = common_canonical_nickname($this->other);
237         
238         $target =
239           common_relative_profile($this->user, $target_nickname);
240
241         if (!$target) {
242             $channel->error($this->user, _('No such user.'));
243             return;
244         }
245         $notice = $target->getCurrentNotice();
246         if (!$notice) {
247             $channel->error($this->user, _('User has no last notice'));
248             return;
249         }
250         $notice_content = $notice->content;
251         
252         $channel->output($this->user, $target_nickname . ": " . $notice_content);
253     }
254 }
255
256 class SubCommand extends Command {
257     
258     var $other = NULL;
259     
260     function __construct($user, $other) {
261         parent::__construct($user);
262         $this->other = $other;
263     }
264     
265     function execute($channel) {
266         
267         if (!$this->other) {
268             $channel->error($this->user, _('Specify the name of the user to subscribe to'));
269             return;
270         }
271         
272         $result = subs_subscribe_user($this->user, $this->other);
273         
274         if ($result == 'true') {
275             $channel->output($this->user, sprintf(_('Subscribed to %s'), $this->other));
276         } else {
277             $channel->error($this->user, $result);
278         }
279     }
280 }
281
282 class UnsubCommand extends Command {
283
284     var $other = NULL;
285     
286     function __construct($user, $other) {
287         parent::__construct($user);
288         $this->other = $other;
289     }
290
291     function execute($channel) {
292         if(!$this->other) {
293             $channel->error($this->user, _('Specify the name of the user to unsubscribe from'));
294             return;
295         }
296         
297         $result=subs_unsubscribe_user($this->user, $this->other);
298         
299         if ($result) {
300             $channel->output($this->user, sprintf(_('Unsubscribed from %s'), $this->other));
301         } else {
302             $channel->error($this->user, $result);
303         }
304     }
305 }
306
307 class OffCommand extends Command {
308     var $other = NULL;
309     function __construct($user, $other=NULL) {
310         parent::__construct($user);
311         $this->other = $other;
312     }
313     function execute($channel) {
314         if ($other) {
315             $channel->error($this->user, _("Command not yet implemented."));
316         } else {
317             if ($channel->off($this->user)) {
318                 $channel->output($this->user, _('Notification off.'));
319             } else {
320                 $channel->error($this->user, _('Can\'t turn off notification.'));
321             }
322         }
323     }
324 }
325
326 class OnCommand extends Command {
327     var $other = NULL;
328     function __construct($user, $other=NULL) {
329         parent::__construct($user);
330         $this->other = $other;
331     }
332     
333     function execute($channel) {
334         if ($other) {
335             $channel->error($this->user, _("Command not yet implemented."));
336         } else {
337             if ($channel->on($this->user)) {
338                 $channel->output($this->user, _('Notification on.'));
339             } else {
340                 $channel->error($this->user, _('Can\'t turn on notification.'));
341             }
342         }
343     }
344 }
345
346 class HelpCommand extends Command {
347     function execute($channel) {
348         $channel->output($this->user,
349                          _("Commands:\n".
350                            "on - turn on notifications\n".
351                            "off - turn off notifications\n".
352                            "help - show this help\n".
353                            "follow <nickname> - subscribe to user\n".
354                            "leave <nickname> - unsubscribe from user\n".
355                            "d <nickname> <text> - direct message to user\n".
356                            "get <nickname> - get last notice from user\n".
357                            "whois <nickname> - get profile info on user\n".
358                            "fav <nickname> - add user's last notice as a 'fave'\n".
359                            "stats - get your stats\n".
360                            "stop - same as 'off'\n".
361                            "quit - same as 'off'\n".
362                            "sub <nickname> - same as 'follow'\n".
363                            "unsub <nickname> - same as 'leave'\n".
364                            "last <nickname> - same as 'get'\n".
365                            "on <nickname> - not yet implemented.\n".
366                            "off <nickname> - not yet implemented.\n".                           
367                            "nudge <nickname> - not yet implemented.\n".
368                            "invite <phone number> - not yet implemented.\n".
369                            "track <word> - not yet implemented.\n".
370                            "untrack <word> - not yet implemented.\n".
371                            "track off - not yet implemented.\n".
372                            "untrack all - not yet implemented.\n".
373                            "tracks - not yet implemented.\n".
374                            "tracking - not yet implemented.\n"));
375     }
376 }