2011-12-27 12:58:28 +00:00
|
|
|
<?php
|
2013-01-26 21:46:54 +00:00
|
|
|
namespace chat\system\command;
|
2011-12-27 12:58:28 +00:00
|
|
|
use \wcf\util\StringUtil;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles commands
|
|
|
|
*
|
|
|
|
* @author Tim Düsterhus
|
2013-01-19 19:36:40 +00:00
|
|
|
* @copyright 2010-2013 Tim Düsterhus
|
2011-12-27 12:58:28 +00:00
|
|
|
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
2013-01-19 19:36:40 +00:00
|
|
|
* @package be.bastelstu.chat
|
2012-03-24 21:37:47 +00:00
|
|
|
* @subpackage system.chat.command
|
2011-12-27 12:58:28 +00:00
|
|
|
*/
|
2012-02-04 11:32:56 +00:00
|
|
|
final class CommandHandler {
|
2012-10-20 14:23:00 +00:00
|
|
|
/**
|
|
|
|
* char that indicates a command
|
2013-06-17 21:48:59 +00:00
|
|
|
* @var string
|
2012-10-20 14:23:00 +00:00
|
|
|
*/
|
2011-12-27 12:58:28 +00:00
|
|
|
const COMMAND_CHAR = '/';
|
2012-10-20 14:23:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* message text
|
2013-06-17 21:48:59 +00:00
|
|
|
* @var string
|
2012-10-20 14:23:00 +00:00
|
|
|
*/
|
2012-02-04 11:32:56 +00:00
|
|
|
private $text = '';
|
2011-12-27 12:58:28 +00:00
|
|
|
|
2013-03-11 16:17:58 +00:00
|
|
|
/**
|
|
|
|
* current room
|
2013-06-17 21:48:59 +00:00
|
|
|
* @var \chat\data\room\Room
|
2013-03-11 16:17:58 +00:00
|
|
|
*/
|
|
|
|
private $room = null;
|
|
|
|
|
2011-12-27 12:58:28 +00:00
|
|
|
/**
|
|
|
|
* Initialises the CommandHandler
|
|
|
|
*
|
2013-03-11 16:17:58 +00:00
|
|
|
* @param string $text
|
|
|
|
* @param \chat\data\room\Room $room
|
2011-12-27 12:58:28 +00:00
|
|
|
*/
|
2013-03-11 16:17:58 +00:00
|
|
|
public function __construct($text, \chat\data\room\Room $room = null) {
|
2011-12-27 12:58:28 +00:00
|
|
|
$this->text = $text;
|
2013-03-11 16:17:58 +00:00
|
|
|
$this->room = $room;
|
2013-06-17 21:56:28 +00:00
|
|
|
|
|
|
|
$aliases = self::getAliasMap();
|
|
|
|
foreach ($aliases as $search => $replace) {
|
2013-06-17 22:13:24 +00:00
|
|
|
$this->text = \wcf\system\Regex::compile('^'.preg_quote(self::COMMAND_CHAR.$search).'( |$)')->replace($this->text, self::COMMAND_CHAR.$replace.' ');
|
2013-06-17 21:56:28 +00:00
|
|
|
}
|
2013-06-17 22:13:24 +00:00
|
|
|
|
|
|
|
$this->text = \wcf\system\Regex::compile('^//')->replace($this->text, '/plain ');
|
2011-12-27 12:58:28 +00:00
|
|
|
}
|
|
|
|
|
2013-06-17 21:48:59 +00:00
|
|
|
/**
|
|
|
|
* Returns the alias map. Key is the alias, value is the target.
|
|
|
|
*
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
|
|
|
public static function getAliasMap() {
|
2013-09-30 22:16:19 +00:00
|
|
|
try {
|
|
|
|
$result = array();
|
|
|
|
foreach (explode("\n", StringUtil::unifyNewlines(StringUtil::toLowerCase(CHAT_COMMAND_ALIASES))) as $line) {
|
|
|
|
list($key, $val) = explode(':', $line, 2);
|
|
|
|
|
|
|
|
$result[$key] = $val;
|
|
|
|
}
|
2013-06-17 21:48:59 +00:00
|
|
|
|
2013-09-30 22:16:19 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
catch (\wcf\system\exception\SystemException $e) {
|
|
|
|
throw new \wcf\system\exception\SystemException("Invalid alias specified: '".$line."'");
|
2013-06-17 21:48:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-27 12:58:28 +00:00
|
|
|
/**
|
|
|
|
* Checks whether the given text is a command.
|
|
|
|
*/
|
|
|
|
public function isCommand($text = null) {
|
|
|
|
if ($text === null) $text = $this->text;
|
2012-10-20 14:23:00 +00:00
|
|
|
|
|
|
|
return StringUtil::startsWith($text, static::COMMAND_CHAR);
|
2011-12-27 12:58:28 +00:00
|
|
|
}
|
|
|
|
|
2012-02-04 11:32:56 +00:00
|
|
|
/**
|
|
|
|
* Returns the whole message.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getText() {
|
|
|
|
return $this->text;
|
|
|
|
}
|
|
|
|
|
2013-03-11 16:17:58 +00:00
|
|
|
/**
|
|
|
|
* Returns the current room.
|
|
|
|
*
|
|
|
|
* @return \chat\data\room\Room
|
|
|
|
*/
|
|
|
|
public function getRoom() {
|
|
|
|
return $this->room;
|
|
|
|
}
|
|
|
|
|
2011-12-27 13:32:28 +00:00
|
|
|
/**
|
|
|
|
* Returns the parameter-string.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getParameters() {
|
2013-08-19 21:23:26 +00:00
|
|
|
$parts = explode(' ', mb_substr($this->text, mb_strlen(static::COMMAND_CHAR)), 2);
|
2011-12-27 13:32:28 +00:00
|
|
|
|
2012-02-04 11:32:56 +00:00
|
|
|
if (!isset($parts[1])) return '';
|
2011-12-27 13:32:28 +00:00
|
|
|
return $parts[1];
|
|
|
|
}
|
|
|
|
|
2011-12-27 12:58:28 +00:00
|
|
|
/**
|
|
|
|
* Loads the command.
|
|
|
|
*/
|
|
|
|
public function loadCommand() {
|
2013-08-19 21:23:26 +00:00
|
|
|
$parts = explode(' ', mb_substr($this->text, mb_strlen(static::COMMAND_CHAR)), 2);
|
2011-12-27 12:58:28 +00:00
|
|
|
|
2013-01-26 21:46:54 +00:00
|
|
|
$class = '\chat\system\command\commands\\'.ucfirst(strtolower($parts[0])).'Command';
|
2011-12-27 12:58:28 +00:00
|
|
|
if (!class_exists($class)) {
|
2013-06-17 22:28:56 +00:00
|
|
|
throw new NotFoundException($parts[0]);
|
2011-12-27 12:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new $class($this);
|
|
|
|
}
|
|
|
|
}
|