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