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