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