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