commandinterpreter.php 10.4 KB
Newer Older
1 2
<?php
/*
3
 * StatusNet - the distributed open-source microblogging tool
4
 * Copyright (C) 2008, 2009, StatusNet, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21

Evan Prodromou's avatar
Evan Prodromou committed
22
require_once INSTALLDIR.'/lib/command.php';
23

24 25
class CommandInterpreter
{
26 27
    function handle_command($user, $text)
    {
Evan Prodromou's avatar
Evan Prodromou committed
28
        // XXX: localise
29

30
        $text = preg_replace('/\s+/', ' ', trim($text));
31
        list($cmd, $arg) = self::split_arg($text);
32

Evan Prodromou's avatar
Evan Prodromou committed
33 34 35 36
        // We try to support all the same commands as Twitter, see
        // http://getsatisfaction.com/twitter/topics/what_are_the_twitter_commands
        // There are a few compatibility commands from earlier versions of
        // StatusNet
37

38
        $cmd = strtolower($cmd);
39
        $result = false;
40

41
        if (Event::handle('StartInterpretCommand', array($cmd, $arg, $user, &$result))) {
42
            switch($cmd) {
Evan Prodromou's avatar
Evan Prodromou committed
43 44 45
            case 'help':
                if ($arg) {
                    $result = null;
46 47
                } else {
                    $result = new HelpCommand($user);
Evan Prodromou's avatar
Evan Prodromou committed
48 49 50 51 52 53 54 55 56 57 58
                }
                break;
            case 'login':
                if ($arg) {
                    $result = null;
                } else {
                    $result = new LoginCommand($user);
                }
                break;
            case 'lose':
                if ($arg) {
59
                    list($other, $extra) = self::split_arg($arg);
Evan Prodromou's avatar
Evan Prodromou committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new LoseCommand($user, $other);
                    }
                } else {
                    $result = null;
                }
                break;
            case 'subscribers':
                if ($arg) {
                    $result = null;
                } else {
                    $result = new SubscribersCommand($user);
                }
                break;
            case 'subscriptions':
                if ($arg) {
                    $result = null;
                } else {
                    $result = new SubscriptionsCommand($user);
                }
                break;
            case 'groups':
                if ($arg) {
                    $result = null;
                } else {
                    $result = new GroupsCommand($user);
                }
                break;
            case 'on':
                if ($arg) {
92
                    list($other, $extra) = self::split_arg($arg);
Evan Prodromou's avatar
Evan Prodromou committed
93 94 95 96 97 98 99 100 101 102 103
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new OnCommand($user, $other);
                    }
                } else {
                    $result = new OnCommand($user);
                }
                break;
            case 'off':
                if ($arg) {
104
                    list($other, $extra) = self::split_arg($arg);
Evan Prodromou's avatar
Evan Prodromou committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new OffCommand($user, $other);
                    }
                } else {
                    $result = new OffCommand($user);
                }
                break;
            case 'stop':
            case 'quit':
                if ($arg) {
                    $result = null;
                } else {
                    $result = new OffCommand($user);
                }
                break;
            case 'join':
                if (!$arg) {
                    $result = null;
125
                } else {
126
                    list($other, $extra) = self::split_arg($arg);
127 128 129 130 131
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new JoinCommand($user, $other);
                    }
Evan Prodromou's avatar
Evan Prodromou committed
132 133 134 135 136
                }
                break;
            case 'drop':
                if (!$arg) {
                    $result = null;
137
                } else {
138
                    list($other, $extra) = self::split_arg($arg);
139 140 141 142 143
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new DropCommand($user, $other);
                    }
144
                }
Evan Prodromou's avatar
Evan Prodromou committed
145 146 147 148 149
                break;
            case 'follow':
            case 'sub':
                if (!$arg) {
                    $result = null;
150
                } else {
151
                    list($other, $extra) = self::split_arg($arg);
152 153 154 155 156
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new SubCommand($user, $other);
                    }
157
                }
Evan Prodromou's avatar
Evan Prodromou committed
158 159 160 161 162 163
                break;
            case 'leave':
            case 'unsub':
                if (!$arg) {
                    $result = null;
                } else {
164
                    list($other, $extra) = self::split_arg($arg);
165 166 167 168 169
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new UnsubCommand($user, $other);
                    }
Evan Prodromou's avatar
Evan Prodromou committed
170 171 172 173 174 175 176
                }
                break;
            case 'get':
            case 'last':
                if (!$arg) {
                    $result = null;
                }
177
                list($other, $extra) = self::split_arg($arg);
Evan Prodromou's avatar
Evan Prodromou committed
178 179 180 181 182 183 184 185 186 187 188
                if ($extra) {
                    $result = null;
                } else {
                    $result = new GetCommand($user, $other);
                }
                break;
            case 'r':
            case 'reply':
                if (!$arg) {
                    $result = null;
                }
189
                list($other, $extra) = self::split_arg($arg);
Evan Prodromou's avatar
Evan Prodromou committed
190 191 192 193 194 195 196 197 198 199
                if (!$extra) {
                    $result = null;
                } else {
                    $result = new ReplyCommand($user, $other, $extra);
                }
                break;
            case 'whois':
                if (!$arg) {
                    $result = null;
                } else {
200
                    list($other, $extra) = self::split_arg($arg);
201 202 203 204 205
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new WhoisCommand($user, $other);
                    }
Evan Prodromou's avatar
Evan Prodromou committed
206 207 208 209 210 211
                }
                break;
            case 'nudge':
                if (!$arg) {
                    $result = null;
                } else {
212
                    list($other, $extra) = self::split_arg($arg);
213 214 215 216 217
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new NudgeCommand($user, $other);
                    }
Evan Prodromou's avatar
Evan Prodromou committed
218 219 220 221 222
                }
                break;
            case 'stats':
                if ($arg) {
                    $result = null;
223 224
                } else {
                    $result = new StatsCommand($user);
Evan Prodromou's avatar
Evan Prodromou committed
225 226 227 228 229 230
                }
                break;
            case 'invite':
                if (!$arg) {
                    $result = null;
                } else {
231
                    list($other, $extra) = self::split_arg($arg);
232 233 234 235 236
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new InviteCommand($user, $other);
                    }
Evan Prodromou's avatar
Evan Prodromou committed
237 238
                }
                break;
239 240 241 242 243 244
             case 'list':
             case 'tag':
                if (!$arg) {
                    $result = null;
                    break;
                }
245
                list($other, $tags) = self::split_arg($arg);
246 247 248 249 250 251 252 253 254 255 256 257
                if (!$tags) {
                    $result = null;
                } else {
                    $result = new TagCommand($user, $other, $tags);
                }
                break;
             case 'unlist':
             case 'untag':
                if (!$arg) {
                    $result = null;
                    break;
                }
258
                list($other, $tags) = self::split_arg($arg);
259 260 261 262 263 264
                if (!$tags) {
                    $result = null;
                } else {
                    $result = new UntagCommand($user, $other, $tags);
                }
                break;
Evan Prodromou's avatar
Evan Prodromou committed
265 266 267 268
            case 'track':
                if (!$arg) {
                    $result = null;
                } else {
269
                    list($word, $extra) = self::split_arg($arg);
270 271 272 273 274 275 276
                    if ($extra) {
                        $result = null;
                    } else if ($word == 'off') {
                        $result = new TrackOffCommand($user);
                    } else {
                        $result = new TrackCommand($user, $word);
                    }
Evan Prodromou's avatar
Evan Prodromou committed
277 278 279 280 281 282
                }
                break;
            case 'untrack':
                if (!$arg) {
                    $result = null;
                } else {
283
                    list($word, $extra) = self::split_arg($arg);
284 285 286 287 288 289 290
                    if ($extra) {
                        $result = null;
                    } else if ($word == 'all') {
                        $result = new TrackOffCommand($user);
                    } else {
                        $result = new UntrackCommand($user, $word);
                    }
Evan Prodromou's avatar
Evan Prodromou committed
291 292 293 294 295 296
                }
                break;
            case 'tracks':
            case 'tracking':
                if ($arg) {
                    $result = null;
297 298
                } else {
                    $result = new TrackingCommand($user);
Evan Prodromou's avatar
Evan Prodromou committed
299 300
                }
                break;
301
            }
302

303
            Event::handle('EndInterpretCommand', array($cmd, $arg, $user, &$result));
304
        }
Evan Prodromou's avatar
Evan Prodromou committed
305 306

        return $result;
307
    }
308

309 310 311
    /**
     * Split arguments without triggering a PHP notice warning
     */
312
    static function split_arg($text)
313 314 315 316 317 318 319
    {
        $pieces = explode(' ', $text, 2);
        if (count($pieces) == 1) {
            $pieces[] = null;
        }
        return $pieces;
    }
320
}