]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/commandinterpreter.php
documentation for people tags
[quix0rs-gnu-social.git] / lib / commandinterpreter.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/lib/command.php';
23
24 class CommandInterpreter
25 {
26     function handle_command($user, $text)
27     {
28         // XXX: localise
29
30         $text = preg_replace('/\s+/', ' ', trim($text));
31         list($cmd, $arg) = $this->split_arg($text);
32
33         // We try to support all the same commands as Twitter, see
34         // http://getsatisfaction.com/twitter/topics/what_are_the_twitter_commands
35         // There are a few compatibility commands from earlier versions of
36         // StatusNet
37
38         $cmd = strtolower($cmd);
39
40         if (Event::handle('StartIntepretCommand', array($cmd, $arg, $user, &$result))) {
41             switch($cmd) {
42             case 'help':
43                 if ($arg) {
44                     $result = null;
45                 } else {
46                     $result = new HelpCommand($user);
47                 }
48                 break;
49             case 'login':
50                 if ($arg) {
51                     $result = null;
52                 } else {
53                     $result = new LoginCommand($user);
54                 }
55                 break;
56             case 'lose':
57                 if ($arg) {
58                     list($other, $extra) = $this->split_arg($arg);
59                     if ($extra) {
60                         $result = null;
61                     } else {
62                         $result = new LoseCommand($user, $other);
63                     }
64                 } else {
65                     $result = null;
66                 }
67                 break;
68             case 'subscribers':
69                 if ($arg) {
70                     $result = null;
71                 } else {
72                     $result = new SubscribersCommand($user);
73                 }
74                 break;
75             case 'subscriptions':
76                 if ($arg) {
77                     $result = null;
78                 } else {
79                     $result = new SubscriptionsCommand($user);
80                 }
81                 break;
82             case 'groups':
83                 if ($arg) {
84                     $result = null;
85                 } else {
86                     $result = new GroupsCommand($user);
87                 }
88                 break;
89             case 'on':
90                 if ($arg) {
91                     list($other, $extra) = $this->split_arg($arg);
92                     if ($extra) {
93                         $result = null;
94                     } else {
95                         $result = new OnCommand($user, $other);
96                     }
97                 } else {
98                     $result = new OnCommand($user);
99                 }
100                 break;
101             case 'off':
102                 if ($arg) {
103                     list($other, $extra) = $this->split_arg($arg);
104                     if ($extra) {
105                         $result = null;
106                     } else {
107                         $result = new OffCommand($user, $other);
108                     }
109                 } else {
110                     $result = new OffCommand($user);
111                 }
112                 break;
113             case 'stop':
114             case 'quit':
115                 if ($arg) {
116                     $result = null;
117                 } else {
118                     $result = new OffCommand($user);
119                 }
120                 break;
121             case 'join':
122                 if (!$arg) {
123                     $result = null;
124                 } else {
125                     list($other, $extra) = $this->split_arg($arg);
126                     if ($extra) {
127                         $result = null;
128                     } else {
129                         $result = new JoinCommand($user, $other);
130                     }
131                 }
132                 break;
133             case 'drop':
134                 if (!$arg) {
135                     $result = null;
136                 } else {
137                     list($other, $extra) = $this->split_arg($arg);
138                     if ($extra) {
139                         $result = null;
140                     } else {
141                         $result = new DropCommand($user, $other);
142                     }
143                 }
144                 break;
145             case 'follow':
146             case 'sub':
147                 if (!$arg) {
148                     $result = null;
149                 } else {
150                     list($other, $extra) = $this->split_arg($arg);
151                     if ($extra) {
152                         $result = null;
153                     } else {
154                         $result = new SubCommand($user, $other);
155                     }
156                 }
157                 break;
158             case 'leave':
159             case 'unsub':
160                 if (!$arg) {
161                     $result = null;
162                 } else {
163                     list($other, $extra) = $this->split_arg($arg);
164                     if ($extra) {
165                         $result = null;
166                     } else {
167                         $result = new UnsubCommand($user, $other);
168                     }
169                 }
170                 break;
171             case 'get':
172             case 'last':
173                 if (!$arg) {
174                     $result = null;
175                 }
176                 list($other, $extra) = $this->split_arg($arg);
177                 if ($extra) {
178                     $result = null;
179                 } else {
180                     $result = new GetCommand($user, $other);
181                 }
182                 break;
183             case 'd':
184             case 'dm':
185                 if (!$arg) {
186                     $result = null;
187                 }
188                 list($other, $extra) = $this->split_arg($arg);
189                 if (!$extra) {
190                     $result = null;
191                 } else {
192                     $result = new MessageCommand($user, $other, $extra);
193                 }
194                 break;
195             case 'r':
196             case 'reply':
197                 if (!$arg) {
198                     $result = null;
199                 }
200                 list($other, $extra) = $this->split_arg($arg);
201                 if (!$extra) {
202                     $result = null;
203                 } else {
204                     $result = new ReplyCommand($user, $other, $extra);
205                 }
206                 break;
207             case 'repeat':
208             case 'rp':
209             case 'rt':
210             case 'rd':
211                 if (!$arg) {
212                     $result = null;
213                 } else {
214                     list($other, $extra) = $this->split_arg($arg);
215                     if ($extra) {
216                         $result = null;
217                     } else {
218                         $result = new RepeatCommand($user, $other);
219                     }
220                 }
221                 break;
222             case 'whois':
223                 if (!$arg) {
224                     $result = null;
225                 } else {
226                     list($other, $extra) = $this->split_arg($arg);
227                     if ($extra) {
228                         $result = null;
229                     } else {
230                         $result = new WhoisCommand($user, $other);
231                     }
232                 }
233                 break;
234             case 'fav':
235                 if (!$arg) {
236                     $result = null;
237                 } else {
238                     list($other, $extra) = $this->split_arg($arg);
239                     if ($extra) {
240                         $result = null;
241                     } else {
242                         $result = new FavCommand($user, $other);
243                     }
244                 }
245                 break;
246             case 'nudge':
247                 if (!$arg) {
248                     $result = null;
249                 } else {
250                     list($other, $extra) = $this->split_arg($arg);
251                     if ($extra) {
252                         $result = null;
253                     } else {
254                         $result = new NudgeCommand($user, $other);
255                     }
256                 }
257                 break;
258             case 'stats':
259                 if ($arg) {
260                     $result = null;
261                 } else {
262                     $result = new StatsCommand($user);
263                 }
264                 break;
265             case 'invite':
266                 if (!$arg) {
267                     $result = null;
268                 } else {
269                     list($other, $extra) = $this->split_arg($arg);
270                     if ($extra) {
271                         $result = null;
272                     } else {
273                         $result = new InviteCommand($user, $other);
274                     }
275                 }
276                 break;
277              case 'list':
278              case 'tag':
279                 if (!$arg) {
280                     $result = null;
281                     break;
282                 }
283                 list($other, $tags) = $this->split_arg($arg);
284                 if (!$tags) {
285                     $result = null;
286                 } else {
287                     $result = new TagCommand($user, $other, $tags);
288                 }
289                 break;
290              case 'unlist':
291              case 'untag':
292                 if (!$arg) {
293                     $result = null;
294                     break;
295                 }
296                 list($other, $tags) = $this->split_arg($arg);
297                 if (!$tags) {
298                     $result = null;
299                 } else {
300                     $result = new UntagCommand($user, $other, $tags);
301                 }
302                 break;
303             case 'track':
304                 if (!$arg) {
305                     $result = null;
306                 } else {
307                     list($word, $extra) = $this->split_arg($arg);
308                     if ($extra) {
309                         $result = null;
310                     } else if ($word == 'off') {
311                         $result = new TrackOffCommand($user);
312                     } else {
313                         $result = new TrackCommand($user, $word);
314                     }
315                 }
316                 break;
317             case 'untrack':
318                 if (!$arg) {
319                     $result = null;
320                 } else {
321                     list($word, $extra) = $this->split_arg($arg);
322                     if ($extra) {
323                         $result = null;
324                     } else if ($word == 'all') {
325                         $result = new TrackOffCommand($user);
326                     } else {
327                         $result = new UntrackCommand($user, $word);
328                     }
329                 }
330                 break;
331             case 'tracks':
332             case 'tracking':
333                 if ($arg) {
334                     $result = null;
335                 } else {
336                     $result = new TrackingCommand($user);
337                 }
338                 break;
339             default:
340                 $result = false;
341             }
342                 
343             Event::handle('EndInterpretCommand', array($cmd, $arg, $user, $result));
344         }
345
346         return $result;
347     }
348
349     /**
350      * Split arguments without triggering a PHP notice warning
351      */
352     function split_arg($text)
353     {
354         $pieces = explode(' ', $text, 2);
355         if (count($pieces) == 1) {
356             $pieces[] = null;
357         }
358         return $pieces;
359     }
360 }