]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/commandinterpreter.php
Merge branch 'master' into social-master
[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) = self::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('StartInterpretCommand', 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) = self::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) = self::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) = self::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) = self::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) = self::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) = self::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) = self::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) = self::split_arg($arg);
177                 if ($extra) {
178                     $result = null;
179                 } else {
180                     $result = new GetCommand($user, $other);
181                 }
182                 break;
183             case 'r':
184             case 'reply':
185                 if (!$arg) {
186                     $result = null;
187                 }
188                 list($other, $extra) = self::split_arg($arg);
189                 if (!$extra) {
190                     $result = null;
191                 } else {
192                     $result = new ReplyCommand($user, $other, $extra);
193                 }
194                 break;
195             case 'repeat':
196             case 'rp':
197             case 'rt':
198             case 'rd':
199                 if (!$arg) {
200                     $result = null;
201                 } else {
202                     list($other, $extra) = self::split_arg($arg);
203                     if ($extra) {
204                         $result = null;
205                     } else {
206                         $result = new RepeatCommand($user, $other);
207                     }
208                 }
209                 break;
210             case 'whois':
211                 if (!$arg) {
212                     $result = null;
213                 } else {
214                     list($other, $extra) = self::split_arg($arg);
215                     if ($extra) {
216                         $result = null;
217                     } else {
218                         $result = new WhoisCommand($user, $other);
219                     }
220                 }
221                 break;
222             case 'nudge':
223                 if (!$arg) {
224                     $result = null;
225                 } else {
226                     list($other, $extra) = self::split_arg($arg);
227                     if ($extra) {
228                         $result = null;
229                     } else {
230                         $result = new NudgeCommand($user, $other);
231                     }
232                 }
233                 break;
234             case 'stats':
235                 if ($arg) {
236                     $result = null;
237                 } else {
238                     $result = new StatsCommand($user);
239                 }
240                 break;
241             case 'invite':
242                 if (!$arg) {
243                     $result = null;
244                 } else {
245                     list($other, $extra) = self::split_arg($arg);
246                     if ($extra) {
247                         $result = null;
248                     } else {
249                         $result = new InviteCommand($user, $other);
250                     }
251                 }
252                 break;
253              case 'list':
254              case 'tag':
255                 if (!$arg) {
256                     $result = null;
257                     break;
258                 }
259                 list($other, $tags) = self::split_arg($arg);
260                 if (!$tags) {
261                     $result = null;
262                 } else {
263                     $result = new TagCommand($user, $other, $tags);
264                 }
265                 break;
266              case 'unlist':
267              case 'untag':
268                 if (!$arg) {
269                     $result = null;
270                     break;
271                 }
272                 list($other, $tags) = self::split_arg($arg);
273                 if (!$tags) {
274                     $result = null;
275                 } else {
276                     $result = new UntagCommand($user, $other, $tags);
277                 }
278                 break;
279             case 'track':
280                 if (!$arg) {
281                     $result = null;
282                 } else {
283                     list($word, $extra) = self::split_arg($arg);
284                     if ($extra) {
285                         $result = null;
286                     } else if ($word == 'off') {
287                         $result = new TrackOffCommand($user);
288                     } else {
289                         $result = new TrackCommand($user, $word);
290                     }
291                 }
292                 break;
293             case 'untrack':
294                 if (!$arg) {
295                     $result = null;
296                 } else {
297                     list($word, $extra) = self::split_arg($arg);
298                     if ($extra) {
299                         $result = null;
300                     } else if ($word == 'all') {
301                         $result = new TrackOffCommand($user);
302                     } else {
303                         $result = new UntrackCommand($user, $word);
304                     }
305                 }
306                 break;
307             case 'tracks':
308             case 'tracking':
309                 if ($arg) {
310                     $result = null;
311                 } else {
312                     $result = new TrackingCommand($user);
313                 }
314                 break;
315             default:
316                 $result = false;
317             }
318
319             Event::handle('EndInterpretCommand', array($cmd, $arg, $user, &$result));
320         }
321
322         return $result;
323     }
324
325     /**
326      * Split arguments without triggering a PHP notice warning
327      */
328     static function split_arg($text)
329     {
330         $pieces = explode(' ', $text, 2);
331         if (count($pieces) == 1) {
332             $pieces[] = null;
333         }
334         return $pieces;
335     }
336 }