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