| 
									
										
										
										
											2008-10-03 22:14:42 -04:00
										 |  |  | <?php | 
					
						
							|  |  |  | /* | 
					
						
							| 
									
										
										
										
											2009-08-25 18:14:12 -04:00
										 |  |  |  * StatusNet - the distributed open-source microblogging tool | 
					
						
							| 
									
										
										
										
											2009-08-25 18:12:20 -04:00
										 |  |  |  * Copyright (C) 2008, 2009, StatusNet, Inc. | 
					
						
							| 
									
										
										
										
											2008-10-03 22:14:42 -04:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-26 10:41:36 -04:00
										 |  |  | if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } | 
					
						
							| 
									
										
										
										
											2008-10-03 22:14:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-11 15:48:30 -05:00
										 |  |  | require_once INSTALLDIR.'/lib/command.php'; | 
					
						
							| 
									
										
										
										
											2008-10-04 11:48:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-23 14:49:23 -05:00
										 |  |  | class CommandInterpreter | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-23 14:33:23 -05:00
										 |  |  |     function handle_command($user, $text) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |         // XXX: localise
 | 
					
						
							| 
									
										
										
										
											2008-10-03 22:14:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |         $text = preg_replace('/\s+/', ' ', trim($text)); | 
					
						
							| 
									
										
										
										
											2009-09-20 21:39:57 -07:00
										 |  |  |         list($cmd, $arg) = $this->split_arg($text); | 
					
						
							| 
									
										
										
										
											2008-10-03 22:14:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |         // 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
 | 
					
						
							| 
									
										
										
										
											2009-01-06 17:09:39 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-03 17:06:15 -05:00
										 |  |  |         $cmd = strtolower($cmd); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |         if (Event::handle('StartIntepretCommand', array($cmd, $arg, $user, &$result))) { | 
					
						
							| 
									
										
										
										
											2011-02-03 17:06:15 -05:00
										 |  |  |             switch($cmd) { | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |             case 'help': | 
					
						
							|  |  |  |                 if ($arg) { | 
					
						
							|  |  |  |                     $result = null; | 
					
						
							| 
									
										
										
										
											2011-02-21 11:42:32 -08:00
										 |  |  |                 } else { | 
					
						
							|  |  |  |                     $result = new HelpCommand($user); | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 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; | 
					
						
							| 
									
										
										
										
											2010-02-22 22:57:16 -05:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2011-02-21 11:42:32 -08:00
										 |  |  |                     list($other, $extra) = $this->split_arg($arg); | 
					
						
							|  |  |  |                     if ($extra) { | 
					
						
							|  |  |  |                         $result = null; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         $result = new JoinCommand($user, $other); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'drop': | 
					
						
							|  |  |  |                 if (!$arg) { | 
					
						
							|  |  |  |                     $result = null; | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2011-02-21 11:42:32 -08:00
										 |  |  |                     list($other, $extra) = $this->split_arg($arg); | 
					
						
							|  |  |  |                     if ($extra) { | 
					
						
							|  |  |  |                         $result = null; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         $result = new DropCommand($user, $other); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case 'follow': | 
					
						
							|  |  |  |             case 'sub': | 
					
						
							|  |  |  |                 if (!$arg) { | 
					
						
							|  |  |  |                     $result = null; | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2011-02-21 11:42:32 -08:00
										 |  |  |                     list($other, $extra) = $this->split_arg($arg); | 
					
						
							|  |  |  |                     if ($extra) { | 
					
						
							|  |  |  |                         $result = null; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         $result = new SubCommand($user, $other); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case 'leave': | 
					
						
							|  |  |  |             case 'unsub': | 
					
						
							|  |  |  |                 if (!$arg) { | 
					
						
							|  |  |  |                     $result = null; | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2011-02-21 11:42:32 -08:00
										 |  |  |                     list($other, $extra) = $this->split_arg($arg); | 
					
						
							|  |  |  |                     if ($extra) { | 
					
						
							|  |  |  |                         $result = null; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         $result = new UnsubCommand($user, $other); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 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 { | 
					
						
							| 
									
										
										
										
											2011-02-21 11:42:32 -08:00
										 |  |  |                     list($other, $extra) = $this->split_arg($arg); | 
					
						
							|  |  |  |                     if ($extra) { | 
					
						
							|  |  |  |                         $result = null; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         $result = new RepeatCommand($user, $other); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'whois': | 
					
						
							|  |  |  |                 if (!$arg) { | 
					
						
							|  |  |  |                     $result = null; | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2011-02-21 11:42:32 -08:00
										 |  |  |                     list($other, $extra) = $this->split_arg($arg); | 
					
						
							|  |  |  |                     if ($extra) { | 
					
						
							|  |  |  |                         $result = null; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         $result = new WhoisCommand($user, $other); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'fav': | 
					
						
							|  |  |  |                 if (!$arg) { | 
					
						
							|  |  |  |                     $result = null; | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2011-02-21 11:42:32 -08:00
										 |  |  |                     list($other, $extra) = $this->split_arg($arg); | 
					
						
							|  |  |  |                     if ($extra) { | 
					
						
							|  |  |  |                         $result = null; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         $result = new FavCommand($user, $other); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'nudge': | 
					
						
							|  |  |  |                 if (!$arg) { | 
					
						
							|  |  |  |                     $result = null; | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2011-02-21 11:42:32 -08:00
										 |  |  |                     list($other, $extra) = $this->split_arg($arg); | 
					
						
							|  |  |  |                     if ($extra) { | 
					
						
							|  |  |  |                         $result = null; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         $result = new NudgeCommand($user, $other); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'stats': | 
					
						
							|  |  |  |                 if ($arg) { | 
					
						
							|  |  |  |                     $result = null; | 
					
						
							| 
									
										
										
										
											2011-02-21 11:42:32 -08:00
										 |  |  |                 } else { | 
					
						
							|  |  |  |                     $result = new StatsCommand($user); | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'invite': | 
					
						
							|  |  |  |                 if (!$arg) { | 
					
						
							|  |  |  |                     $result = null; | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2011-02-21 11:42:32 -08:00
										 |  |  |                     list($other, $extra) = $this->split_arg($arg); | 
					
						
							|  |  |  |                     if ($extra) { | 
					
						
							|  |  |  |                         $result = null; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         $result = new InviteCommand($user, $other); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'track': | 
					
						
							|  |  |  |                 if (!$arg) { | 
					
						
							|  |  |  |                     $result = null; | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2011-02-21 11:42:32 -08:00
										 |  |  |                     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); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'untrack': | 
					
						
							|  |  |  |                 if (!$arg) { | 
					
						
							|  |  |  |                     $result = null; | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2011-02-21 11:42:32 -08:00
										 |  |  |                     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); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'tracks': | 
					
						
							|  |  |  |             case 'tracking': | 
					
						
							|  |  |  |                 if ($arg) { | 
					
						
							|  |  |  |                     $result = null; | 
					
						
							| 
									
										
										
										
											2011-02-21 11:42:32 -08:00
										 |  |  |                 } else { | 
					
						
							|  |  |  |                     $result = new TrackingCommand($user); | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 $result = false; | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  |                  | 
					
						
							| 
									
										
										
										
											2011-03-16 16:52:37 -07:00
										 |  |  |             Event::handle('EndInterpretCommand', array($cmd, $arg, $user, &$result)); | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-02-03 17:04:16 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-09-29 00:39:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-20 21:39:57 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Split arguments without triggering a PHP notice warning | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function split_arg($text) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $pieces = explode(' ', $text, 2); | 
					
						
							|  |  |  |         if (count($pieces) == 1) { | 
					
						
							|  |  |  |             $pieces[] = null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $pieces; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-10-03 22:14:42 -04:00
										 |  |  | } |