]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/commandinterpreter.php
Last type-hint is an array, added.
[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         $result = false;
40
41         if (Event::handle('StartInterpretCommand', array($cmd, $arg, $user, &$result))) {
42             switch($cmd) {
43             case 'help':
44                 if ($arg) {
45                     $result = null;
46                 } else {
47                     $result = new HelpCommand($user);
48                 }
49                 break;
50             case 'login':
51                 if ($arg) {
52                     $result = null;
53                 } else {
54                     $result = new LoginCommand($user);
55                 }
56                 break;
57             case 'lose':
58                 if ($arg) {
59                     list($other, $extra) = self::split_arg($arg);
60                     if ($extra) {
61                         $result = null;
62                     } else {
63                         $result = new LoseCommand($user, $other);
64                     }
65                 } else {
66                     $result = null;
67                 }
68                 break;
69             case 'subscribers':
70                 if ($arg) {
71                     $result = null;
72                 } else {
73                     $result = new SubscribersCommand($user);
74                 }
75                 break;
76             case 'subscriptions':
77                 if ($arg) {
78                     $result = null;
79                 } else {
80                     $result = new SubscriptionsCommand($user);
81                 }
82                 break;
83             case 'groups':
84                 if ($arg) {
85                     $result = null;
86                 } else {
87                     $result = new GroupsCommand($user);
88                 }
89                 break;
90             case 'on':
91                 if ($arg) {
92                     list($other, $extra) = self::split_arg($arg);
93                     if ($extra) {
94                         $result = null;
95                     } else {
96                         $result = new OnCommand($user, $other);
97                     }
98                 } else {
99                     $result = new OnCommand($user);
100                 }
101                 break;
102             case 'off':
103                 if ($arg) {
104                     list($other, $extra) = self::split_arg($arg);
105                     if ($extra) {
106                         $result = null;
107                     } else {
108                         $result = new OffCommand($user, $other);
109                     }
110                 } else {
111                     $result = new OffCommand($user);
112                 }
113                 break;
114             case 'stop':
115             case 'quit':
116                 if ($arg) {
117                     $result = null;
118                 } else {
119                     $result = new OffCommand($user);
120                 }
121                 break;
122             case 'join':
123                 if (!$arg) {
124                     $result = null;
125                 } else {
126                     list($other, $extra) = self::split_arg($arg);
127                     if ($extra) {
128                         $result = null;
129                     } else {
130                         $result = new JoinCommand($user, $other);
131                     }
132                 }
133                 break;
134             case 'drop':
135                 if (!$arg) {
136                     $result = null;
137                 } else {
138                     list($other, $extra) = self::split_arg($arg);
139                     if ($extra) {
140                         $result = null;
141                     } else {
142                         $result = new DropCommand($user, $other);
143                     }
144                 }
145                 break;
146             case 'follow':
147             case 'sub':
148                 if (!$arg) {
149                     $result = null;
150                 } else {
151                     list($other, $extra) = self::split_arg($arg);
152                     if ($extra) {
153                         $result = null;
154                     } else {
155                         $result = new SubCommand($user, $other);
156                     }
157                 }
158                 break;
159             case 'leave':
160             case 'unsub':
161                 if (!$arg) {
162                     $result = null;
163                 } else {
164                     list($other, $extra) = self::split_arg($arg);
165                     if ($extra) {
166                         $result = null;
167                     } else {
168                         $result = new UnsubCommand($user, $other);
169                     }
170                 }
171                 break;
172             case 'get':
173             case 'last':
174                 if (!$arg) {
175                     $result = null;
176                 }
177                 list($other, $extra) = self::split_arg($arg);
178                 if ($extra) {
179                     $result = null;
180                 } else {
181                     $result = new GetCommand($user, $other);
182                 }
183                 break;
184             case 'r':
185             case 'reply':
186                 if (!$arg) {
187                     $result = null;
188                 }
189                 list($other, $extra) = self::split_arg($arg);
190                 if (!$extra) {
191                     $result = null;
192                 } else {
193                     $result = new ReplyCommand($user, $other, $extra);
194                 }
195                 break;
196             case 'whois':
197                 if (!$arg) {
198                     $result = null;
199                 } else {
200                     list($other, $extra) = self::split_arg($arg);
201                     if ($extra) {
202                         $result = null;
203                     } else {
204                         $result = new WhoisCommand($user, $other);
205                     }
206                 }
207                 break;
208             case 'nudge':
209                 if (!$arg) {
210                     $result = null;
211                 } else {
212                     list($other, $extra) = self::split_arg($arg);
213                     if ($extra) {
214                         $result = null;
215                     } else {
216                         $result = new NudgeCommand($user, $other);
217                     }
218                 }
219                 break;
220             case 'stats':
221                 if ($arg) {
222                     $result = null;
223                 } else {
224                     $result = new StatsCommand($user);
225                 }
226                 break;
227             case 'invite':
228                 if (!$arg) {
229                     $result = null;
230                 } else {
231                     list($other, $extra) = self::split_arg($arg);
232                     if ($extra) {
233                         $result = null;
234                     } else {
235                         $result = new InviteCommand($user, $other);
236                     }
237                 }
238                 break;
239              case 'list':
240              case 'tag':
241                 if (!$arg) {
242                     $result = null;
243                     break;
244                 }
245                 list($other, $tags) = self::split_arg($arg);
246                 if (!$tags) {
247                     $result = null;
248                 } else {
249                     $result = new TagCommand($user, $other, $tags);
250                 }
251                 break;
252              case 'unlist':
253              case 'untag':
254                 if (!$arg) {
255                     $result = null;
256                     break;
257                 }
258                 list($other, $tags) = self::split_arg($arg);
259                 if (!$tags) {
260                     $result = null;
261                 } else {
262                     $result = new UntagCommand($user, $other, $tags);
263                 }
264                 break;
265             case 'track':
266                 if (!$arg) {
267                     $result = null;
268                 } else {
269                     list($word, $extra) = self::split_arg($arg);
270                     if ($extra) {
271                         $result = null;
272                     } else if ($word == 'off') {
273                         $result = new TrackOffCommand($user);
274                     } else {
275                         $result = new TrackCommand($user, $word);
276                     }
277                 }
278                 break;
279             case 'untrack':
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 == 'all') {
287                         $result = new TrackOffCommand($user);
288                     } else {
289                         $result = new UntrackCommand($user, $word);
290                     }
291                 }
292                 break;
293             case 'tracks':
294             case 'tracking':
295                 if ($arg) {
296                     $result = null;
297                 } else {
298                     $result = new TrackingCommand($user);
299                 }
300                 break;
301             }
302
303             Event::handle('EndInterpretCommand', array($cmd, $arg, $user, &$result));
304         }
305
306         return $result;
307     }
308
309     /**
310      * Split arguments without triggering a PHP notice warning
311      */
312     static function split_arg($text)
313     {
314         $pieces = explode(' ', $text, 2);
315         if (count($pieces) == 1) {
316             $pieces[] = null;
317         }
318         return $pieces;
319     }
320 }