diff --git a/src/PocketMine/Server.php b/src/PocketMine/Server.php index 37c526c81..0dcc0a1bd 100644 --- a/src/PocketMine/Server.php +++ b/src/PocketMine/Server.php @@ -608,8 +608,12 @@ class Server{ if(file_exists($this->dataPath . "banned.txt") and !file_exists($this->dataPath . "banned-players.txt")){ @rename($this->dataPath . "banned.txt", $this->dataPath . "banned-players.txt"); } + @touch($this->dataPath . "banned-players.txt"); $this->banByName = new BanList($this->dataPath . "banned-players.txt"); + $this->banByName->load(); + @touch($this->dataPath . "banned-ips.txt"); $this->banByIP = new BanList($this->dataPath . "banned-ips.txt"); + $this->banByIP->load(); $this->tickScheduler = new TickScheduler(20); $this->scheduler = new ServerScheduler(); diff --git a/src/PocketMine/command/SimpleCommandMap.php b/src/PocketMine/command/SimpleCommandMap.php index f2694c034..9a9ec900b 100644 --- a/src/PocketMine/command/SimpleCommandMap.php +++ b/src/PocketMine/command/SimpleCommandMap.php @@ -21,14 +21,20 @@ namespace PocketMine\Command; +use PocketMine\Command\Defaults\BanCommand; +use PocketMine\Command\Defaults\BanIpCommand; +use PocketMine\Command\Defaults\BanListCommand; use PocketMine\Command\Defaults\DefaultGamemodeCommand; use PocketMine\Command\Defaults\HelpCommand; +use PocketMine\Command\Defaults\PardonCommand; +use PocketMine\Command\Defaults\PardonIpCommand; use PocketMine\Command\Defaults\PluginsCommand; use PocketMine\Command\Defaults\SeedCommand; use PocketMine\Command\Defaults\StopCommand; use PocketMine\Command\Defaults\TellCommand; use PocketMine\Command\Defaults\VanillaCommand; use PocketMine\Command\Defaults\VersionCommand; +use PocketMine\Permission\BanList; use PocketMine\Server; class SimpleCommandMap implements CommandMap{ @@ -55,6 +61,11 @@ class SimpleCommandMap implements CommandMap{ $this->register("pocketmine", new StopCommand("stop")); $this->register("pocketmine", new TellCommand("tell")); $this->register("pocketmine", new DefaultGamemodeCommand("defaultgamemode")); + $this->register("pocketmine", new BanCommand("ban")); + $this->register("pocketmine", new BanIpCommand("ban-ip")); + $this->register("pocketmine", new BanListCommand("banlist")); + $this->register("pocketmine", new PardonCommand("pardon")); + $this->register("pocketmine", new PardonIpCommand("pardon-ip")); } diff --git a/src/PocketMine/command/defaults/BanCommand.php b/src/PocketMine/command/defaults/BanCommand.php new file mode 100644 index 000000000..4eb79c74c --- /dev/null +++ b/src/PocketMine/command/defaults/BanCommand.php @@ -0,0 +1,63 @@ + [reason...]" + ); + $this->setPermission("pocketmine.command.ban.player"); + } + + public function execute(CommandSender $sender, $currentAlias, array $args){ + if(!$this->testPermission($sender)){ + return true; + } + + if(count($args) === 0){ + $sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage); + return false; + } + + $name = array_shift($args); + $reason = implode(" ", $args); + + Server::getInstance()->getNameBans()->addBan($name, $reason, null, $sender->getName()); + + if(($player = Player::get($name, true)) instanceof Player){ + $player->kick("Banned by admin."); + } + + Command::broadcastCommandMessage($sender, "Banned player ". $name); + return true; + } +} \ No newline at end of file diff --git a/src/PocketMine/command/defaults/BanIpCommand.php b/src/PocketMine/command/defaults/BanIpCommand.php new file mode 100644 index 000000000..8dfb0ae6b --- /dev/null +++ b/src/PocketMine/command/defaults/BanIpCommand.php @@ -0,0 +1,80 @@ + [reason...]" + ); + $this->setPermission("pocketmine.command.ban.ip"); + } + + public function execute(CommandSender $sender, $currentAlias, array $args){ + if(!$this->testPermission($sender)){ + return true; + } + + if(count($args) === 0){ + $sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage); + return false; + } + + $value = array_shift($args); + $reason = implode(" ", $args); + + if(preg_match("/^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$/", $value)){ + $this->processIPBan($value, $sender, $reason); + }else{ + if(($player = Player::get($name, true)) instanceof Player){ + $this->processIPBan($player->getIP(), $sender, $reason); + }else{ + $sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage); + return false; + } + } + + Command::broadcastCommandMessage($sender, "Banned player ". $name); + return true; + } + + private function processIPBan($ip, CommandSender $sender, $reason){ + Server::getInstance()->getIPBans()->addBan($ip, $reason, null, $sender->getName()); + + foreach(Player::getAll() as $player){ + if($player->getIP() === $ip){ + $player->kick("You have been IP banned."); + } + } + + Command::broadcastCommandMessage($sender, "Banned IP Address ". $ip); + } +} \ No newline at end of file diff --git a/src/PocketMine/command/defaults/BanListCommand.php b/src/PocketMine/command/defaults/BanListCommand.php new file mode 100644 index 000000000..314b34f68 --- /dev/null +++ b/src/PocketMine/command/defaults/BanListCommand.php @@ -0,0 +1,65 @@ +setPermission("pocketmine.command.ban.list"); + } + + public function execute(CommandSender $sender, $currentAlias, array $args){ + if(!$this->testPermission($sender)){ + return true; + } + $list = Server::getInstance()->getNameBans(); + if(isset($args[0])){ + $args[0] = strtolower($args[0]); + if($args[0] === "ips"){ + $list = Server::getInstance()->getIPBans(); + }elseif($args[0] === "players"){ + $list = Server::getInstance()->getNameBans(); + } + } + + $message = ""; + $list = $list->getEntries(); + foreach($list as $entry){ + $message .= $entry->getName() . ", "; + } + + $sender->sendMessage("There are ". count($list)." total banned players:"); + $sender->sendMessage(substr($message, 0, -2)); + return true; + } +} \ No newline at end of file diff --git a/src/PocketMine/command/defaults/PardonCommand.php b/src/PocketMine/command/defaults/PardonCommand.php new file mode 100644 index 000000000..2d751bf1f --- /dev/null +++ b/src/PocketMine/command/defaults/PardonCommand.php @@ -0,0 +1,56 @@ +" + ); + $this->setPermission("pocketmine.command.unban.player"); + } + + public function execute(CommandSender $sender, $currentAlias, array $args){ + if(!$this->testPermission($sender)){ + return true; + } + + if(count($args) !== 1){ + $sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage); + return false; + } + + Server::getInstance()->getNameBans()->remove($args[0]); + + Command::broadcastCommandMessage($sender, "Pardoned ". $name); + return true; + } +} \ No newline at end of file diff --git a/src/PocketMine/command/defaults/PardonIpCommand.php b/src/PocketMine/command/defaults/PardonIpCommand.php new file mode 100644 index 000000000..4f561665b --- /dev/null +++ b/src/PocketMine/command/defaults/PardonIpCommand.php @@ -0,0 +1,60 @@ +" + ); + $this->setPermission("pocketmine.command.unban.ip"); + } + + public function execute(CommandSender $sender, $currentAlias, array $args){ + if(!$this->testPermission($sender)){ + return true; + } + + if(count($args) !== 1){ + $sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage); + return false; + } + + if(preg_match("/^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$/", $args[0])){ + Server::getInstance()->getIPBans()->remove($args[0]); + Command::broadcastCommandMessage($sender, "Pardoned IP ". $name); + }else{ + $sender->sendMessage("Invalid IP"); + } + + return true; + } +} \ No newline at end of file diff --git a/src/PocketMine/permission/BanEntry.php b/src/PocketMine/permission/BanEntry.php index a6140ec79..bcdc66ae5 100644 --- a/src/PocketMine/permission/BanEntry.php +++ b/src/PocketMine/permission/BanEntry.php @@ -61,7 +61,10 @@ class BanEntry{ return $this->expirationDate; } - public function setExpires(\DateTime $date){ + /** + * @param \DateTime $date + */ + public function setExpires($date){ $this->expirationDate = $date; } @@ -89,7 +92,6 @@ class BanEntry{ $str .= $this->getExpires() === null ? "Forever" : $this->getExpires()->format(self::$format); $str .= "|"; $str .= $this->getReason(); - $str .= "|"; return $str; } diff --git a/src/PocketMine/permission/BanList.php b/src/PocketMine/permission/BanList.php index 5456aff8e..0b7df8ddd 100644 --- a/src/PocketMine/permission/BanList.php +++ b/src/PocketMine/permission/BanList.php @@ -25,7 +25,7 @@ use PocketMine\Server; class BanList{ - /** @var string[] */ + /** @var BanEntry[] */ private $list = array(); /** @var string */ @@ -56,7 +56,7 @@ class BanList{ } /** - * @return string[] + * @return BanEntry[] */ public function getEntries(){ $this->removeExpired(); @@ -86,6 +86,25 @@ class BanList{ $this->save(); } + /** + * @param string $target + * @param string $reason + * @param \DateTime $expires + * @param string $source + * + * @return BanEntry + */ + public function addBan($target, $reason = null, $expires = null, $source = null){ + $entry = new BanEntry($target); + $entry->setSource($source != null ? $source : $entry->getSource()); + $entry->setExpires($expires); + $entry->setReason($reason != null ? $reason : $entry->getReason()); + + $this->list[$entry->getName()] = $entry; + $this->save(); + return $entry; + } + /** * @param string $name */