]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/commandinterpreter.php
Updated .form_settings fieldset fieldset styles
[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 'whois':
173             if (!$arg) {
174                 return null;
175             }
176             list($other, $extra) = $this->split_arg($arg);
177             if ($extra) {
178                 return null;
179             } else {
180                 return new WhoisCommand($user, $other);
181             }
182          case 'fav':
183             if (!$arg) {
184                 return null;
185             }
186             list($other, $extra) = $this->split_arg($arg);
187             if ($extra) {
188                 return null;
189             } else {
190                 return new FavCommand($user, $other);
191             }
192          case 'nudge':
193             if (!$arg) {
194                 return null;
195             }
196             list($other, $extra) = $this->split_arg($arg);
197             if ($extra) {
198                 return null;
199             } else {
200                 return new NudgeCommand($user, $other);
201             }
202          case 'stats':
203             if ($arg) {
204                 return null;
205             }
206             return new StatsCommand($user);
207          case 'invite':
208             if (!$arg) {
209                 return null;
210             }
211             list($other, $extra) = $this->split_arg($arg);
212             if ($extra) {
213                 return null;
214             } else {
215                 return new InviteCommand($user, $other);
216             }
217          case 'track':
218             if (!$arg) {
219                 return null;
220             }
221             list($word, $extra) = $this->split_arg($arg);
222             if ($extra) {
223                 return null;
224             } else if ($word == 'off') {
225                 return new TrackOffCommand($user);
226             } else {
227                 return new TrackCommand($user, $word);
228             }
229          case 'untrack':
230             if (!$arg) {
231                 return null;
232             }
233             list($word, $extra) = $this->split_arg($arg);
234             if ($extra) {
235                 return null;
236             } else if ($word == 'all') {
237                 return new TrackOffCommand($user);
238             } else {
239                 return new UntrackCommand($user, $word);
240             }
241          case 'tracks':
242          case 'tracking':
243             if ($arg) {
244                 return null;
245             }
246             return new TrackingCommand($user);
247          default:
248             return false;
249         }
250     }
251     
252     /**
253      * Split arguments without triggering a PHP notice warning
254      */
255     function split_arg($text)
256     {
257         $pieces = explode(' ', $text, 2);
258         if (count($pieces) == 1) {
259             $pieces[] = null;
260         }
261         return $pieces;
262     }
263 }
264