commandinterpreter.php 11.7 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) = $this->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 39
        $cmd = strtolower($cmd);

Evan Prodromou's avatar
Evan Prodromou committed
40
        if (Event::handle('StartIntepretCommand', array($cmd, $arg, $user, &$result))) {
41
            switch($cmd) {
Evan Prodromou's avatar
Evan Prodromou committed
42 43 44
            case 'help':
                if ($arg) {
                    $result = null;
45 46
                } else {
                    $result = new HelpCommand($user);
Evan Prodromou's avatar
Evan Prodromou committed
47 48 49 50 51 52 53 54 55 56 57 58 59 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
                }
                break;
            case 'login':
                if ($arg) {
                    $result = null;
                } else {
                    $result = new LoginCommand($user);
                }
                break;
            case 'lose':
                if ($arg) {
                    list($other, $extra) = $this->split_arg($arg);
                    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) {
                    list($other, $extra) = $this->split_arg($arg);
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new OnCommand($user, $other);
                    }
                } else {
                    $result = new OnCommand($user);
                }
                break;
            case 'off':
                if ($arg) {
                    list($other, $extra) = $this->split_arg($arg);
                    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;
124
                } else {
125 126 127 128 129 130
                    list($other, $extra) = $this->split_arg($arg);
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new JoinCommand($user, $other);
                    }
Evan Prodromou's avatar
Evan Prodromou committed
131 132 133 134 135
                }
                break;
            case 'drop':
                if (!$arg) {
                    $result = null;
136
                } else {
137 138 139 140 141 142
                    list($other, $extra) = $this->split_arg($arg);
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new DropCommand($user, $other);
                    }
143
                }
Evan Prodromou's avatar
Evan Prodromou committed
144 145 146 147 148
                break;
            case 'follow':
            case 'sub':
                if (!$arg) {
                    $result = null;
149
                } else {
150 151 152 153 154 155
                    list($other, $extra) = $this->split_arg($arg);
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new SubCommand($user, $other);
                    }
156
                }
Evan Prodromou's avatar
Evan Prodromou committed
157 158 159 160 161 162
                break;
            case 'leave':
            case 'unsub':
                if (!$arg) {
                    $result = null;
                } else {
163 164 165 166 167 168
                    list($other, $extra) = $this->split_arg($arg);
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new UnsubCommand($user, $other);
                    }
Evan Prodromou's avatar
Evan Prodromou committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
                }
                break;
            case 'get':
            case 'last':
                if (!$arg) {
                    $result = null;
                }
                list($other, $extra) = $this->split_arg($arg);
                if ($extra) {
                    $result = null;
                } else {
                    $result = new GetCommand($user, $other);
                }
                break;
            case 'd':
            case 'dm':
                if (!$arg) {
                    $result = null;
                }
                list($other, $extra) = $this->split_arg($arg);
                if (!$extra) {
                    $result = null;
                } else {
                    $result = new MessageCommand($user, $other, $extra);
                }
                break;
            case 'r':
            case 'reply':
                if (!$arg) {
                    $result = null;
                }
                list($other, $extra) = $this->split_arg($arg);
                if (!$extra) {
                    $result = null;
                } else {
                    $result = new ReplyCommand($user, $other, $extra);
                }
                break;
            case 'repeat':
            case 'rp':
            case 'rt':
            case 'rd':
                if (!$arg) {
                    $result = null;
                } else {
214 215 216 217 218 219
                    list($other, $extra) = $this->split_arg($arg);
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new RepeatCommand($user, $other);
                    }
Evan Prodromou's avatar
Evan Prodromou committed
220 221 222 223 224 225
                }
                break;
            case 'whois':
                if (!$arg) {
                    $result = null;
                } else {
226 227 228 229 230 231
                    list($other, $extra) = $this->split_arg($arg);
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new WhoisCommand($user, $other);
                    }
Evan Prodromou's avatar
Evan Prodromou committed
232 233 234 235 236 237
                }
                break;
            case 'fav':
                if (!$arg) {
                    $result = null;
                } else {
238 239 240 241 242 243
                    list($other, $extra) = $this->split_arg($arg);
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new FavCommand($user, $other);
                    }
Evan Prodromou's avatar
Evan Prodromou committed
244 245 246 247 248 249
                }
                break;
            case 'nudge':
                if (!$arg) {
                    $result = null;
                } else {
250 251 252 253 254 255
                    list($other, $extra) = $this->split_arg($arg);
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new NudgeCommand($user, $other);
                    }
Evan Prodromou's avatar
Evan Prodromou committed
256 257 258 259 260
                }
                break;
            case 'stats':
                if ($arg) {
                    $result = null;
261 262
                } else {
                    $result = new StatsCommand($user);
Evan Prodromou's avatar
Evan Prodromou committed
263 264 265 266 267 268
                }
                break;
            case 'invite':
                if (!$arg) {
                    $result = null;
                } else {
269 270 271 272 273 274
                    list($other, $extra) = $this->split_arg($arg);
                    if ($extra) {
                        $result = null;
                    } else {
                        $result = new InviteCommand($user, $other);
                    }
Evan Prodromou's avatar
Evan Prodromou committed
275 276
                }
                break;
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
             case 'list':
             case 'tag':
                if (!$arg) {
                    $result = null;
                    break;
                }
                list($other, $tags) = $this->split_arg($arg);
                if (!$tags) {
                    $result = null;
                } else {
                    $result = new TagCommand($user, $other, $tags);
                }
                break;
             case 'unlist':
             case 'untag':
                if (!$arg) {
                    $result = null;
                    break;
                }
                list($other, $tags) = $this->split_arg($arg);
                if (!$tags) {
                    $result = null;
                } else {
                    $result = new UntagCommand($user, $other, $tags);
                }
                break;
Evan Prodromou's avatar
Evan Prodromou committed
303 304 305 306
            case 'track':
                if (!$arg) {
                    $result = null;
                } else {
307 308 309 310 311 312 313 314
                    list($word, $extra) = $this->split_arg($arg);
                    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
315 316 317 318 319 320
                }
                break;
            case 'untrack':
                if (!$arg) {
                    $result = null;
                } else {
321 322 323 324 325 326 327 328
                    list($word, $extra) = $this->split_arg($arg);
                    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
329 330 331 332 333 334
                }
                break;
            case 'tracks':
            case 'tracking':
                if ($arg) {
                    $result = null;
335 336
                } else {
                    $result = new TrackingCommand($user);
Evan Prodromou's avatar
Evan Prodromou committed
337 338 339 340
                }
                break;
            default:
                $result = false;
341
            }
342

343
            Event::handle('EndInterpretCommand', array($cmd, $arg, $user, &$result));
344
        }
Evan Prodromou's avatar
Evan Prodromou committed
345 346

        return $result;
347
    }
348

349 350 351 352 353 354 355 356 357 358 359
    /**
     * Split arguments without triggering a PHP notice warning
     */
    function split_arg($text)
    {
        $pieces = explode(' ', $text, 2);
        if (count($pieces) == 1) {
            $pieces[] = null;
        }
        return $pieces;
    }
360
}