]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/commandinterpreter.php
Merge branch 'testing' 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 'lose':
51             if ($arg) {
52                 list($other, $extra) = $this->split_arg($arg);
53                 if ($extra) {
54                     return null;
55                 } else {
56                     return new LoseCommand($user, $other);
57                 }
58             } else {
59               return null;
60             }
61          case 'subscribers':
62             if ($arg) {
63                 return null;
64             } else {
65                 return new SubscribersCommand($user);
66             }
67          case 'subscriptions':
68             if ($arg) {
69                 return null;
70             } else {
71                 return new SubscriptionsCommand($user);
72             }
73          case 'groups':
74             if ($arg) {
75                 return null;
76             } else {
77                 return new GroupsCommand($user);
78             }
79          case 'on':
80             if ($arg) {
81                 list($other, $extra) = $this->split_arg($arg);
82                 if ($extra) {
83                     return null;
84                 } else {
85                     return new OnCommand($user, $other);
86                 }
87             } else {
88                 return new OnCommand($user);
89             }
90          case 'off':
91             if ($arg) {
92                 list($other, $extra) = $this->split_arg($arg);
93                 if ($extra) {
94                     return null;
95                 } else {
96                     return new OffCommand($user, $other);
97                 }
98             } else {
99                 return new OffCommand($user);
100             }
101          case 'stop':
102          case 'quit':
103             if ($arg) {
104                 return null;
105             } else {
106                 return new OffCommand($user);
107             }
108          case 'join':
109              if (!$arg) {
110                 return null;
111             }
112             list($other, $extra) = $this->split_arg($arg);
113             if ($extra) {
114                 return null;
115             } else {
116                 return new JoinCommand($user, $other);
117             }
118          case 'drop':
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 DropCommand($user, $other);
127             }
128          case 'follow':
129          case 'sub':
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 SubCommand($user, $other);
138             }
139          case 'leave':
140          case 'unsub':
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 UnsubCommand($user, $other);
149             }
150          case 'get':
151          case 'last':
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 GetCommand($user, $other);
160             }
161          case 'd':
162          case 'dm':
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 MessageCommand($user, $other, $extra);
171             }
172          case 'r':
173          case 'reply':
174             if (!$arg) {
175                 return null;
176             }
177             list($other, $extra) = $this->split_arg($arg);
178             if (!$extra) {
179                 return null;
180             } else {
181                 return new ReplyCommand($user, $other, $extra);
182             }
183          case 'repeat':
184          case 'rp':
185          case 'rt':
186          case 'rd':
187             if (!$arg) {
188                 return null;
189             }
190             list($other, $extra) = $this->split_arg($arg);
191             if ($extra) {
192                 return null;
193             } else {
194                 return new RepeatCommand($user, $other);
195             }
196          case 'whois':
197             if (!$arg) {
198                 return null;
199             }
200             list($other, $extra) = $this->split_arg($arg);
201             if ($extra) {
202                 return null;
203             } else {
204                 return new WhoisCommand($user, $other);
205             }
206          case 'fav':
207             if (!$arg) {
208                 return null;
209             }
210             list($other, $extra) = $this->split_arg($arg);
211             if ($extra) {
212                 return null;
213             } else {
214                 return new FavCommand($user, $other);
215             }
216          case 'nudge':
217             if (!$arg) {
218                 return null;
219             }
220             list($other, $extra) = $this->split_arg($arg);
221             if ($extra) {
222                 return null;
223             } else {
224                 return new NudgeCommand($user, $other);
225             }
226          case 'stats':
227             if ($arg) {
228                 return null;
229             }
230             return new StatsCommand($user);
231          case 'invite':
232             if (!$arg) {
233                 return null;
234             }
235             list($other, $extra) = $this->split_arg($arg);
236             if ($extra) {
237                 return null;
238             } else {
239                 return new InviteCommand($user, $other);
240             }
241          case 'track':
242             if (!$arg) {
243                 return null;
244             }
245             list($word, $extra) = $this->split_arg($arg);
246             if ($extra) {
247                 return null;
248             } else if ($word == 'off') {
249                 return new TrackOffCommand($user);
250             } else {
251                 return new TrackCommand($user, $word);
252             }
253          case 'untrack':
254             if (!$arg) {
255                 return null;
256             }
257             list($word, $extra) = $this->split_arg($arg);
258             if ($extra) {
259                 return null;
260             } else if ($word == 'all') {
261                 return new TrackOffCommand($user);
262             } else {
263                 return new UntrackCommand($user, $word);
264             }
265          case 'tracks':
266          case 'tracking':
267             if ($arg) {
268                 return null;
269             }
270             return new TrackingCommand($user);
271          default:
272             return false;
273         }
274     }
275     
276     /**
277      * Split arguments without triggering a PHP notice warning
278      */
279     function split_arg($text)
280     {
281         $pieces = explode(' ', $text, 2);
282         if (count($pieces) == 1) {
283             $pieces[] = null;
284         }
285         return $pieces;
286     }
287 }
288