Added /ban, /ban-ip, /pardon, /pardon-ip

This commit is contained in:
Shoghi Cervantes 2014-03-31 05:51:42 +02:00
parent 1ff381ebf4
commit befe1c606f
9 changed files with 364 additions and 4 deletions

View File

@ -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();

View File

@ -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"));
}

View File

@ -0,0 +1,63 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace PocketMine\Command\Defaults;
use PocketMine\Command\Command;
use PocketMine\Command\CommandSender;
use PocketMine\Player;
use PocketMine\Server;
use PocketMine\Utils\TextFormat;
class BanCommand extends VanillaCommand{
public function __construct($name){
parent::__construct(
$name,
"Prevents the specified player from using this server",
"/ban <player> [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;
}
}

View File

@ -0,0 +1,80 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace PocketMine\Command\Defaults;
use PocketMine\Command\Command;
use PocketMine\Command\CommandSender;
use PocketMine\Player;
use PocketMine\Server;
use PocketMine\Utils\TextFormat;
class BanIpCommand extends VanillaCommand{
public function __construct($name){
parent::__construct(
$name,
"Prevents the specified IP address from using this server",
"/ban <address|player> [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);
}
}

View File

@ -0,0 +1,65 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace PocketMine\Command\Defaults;
use PocketMine\Command\Command;
use PocketMine\Command\CommandSender;
use PocketMine\Player;
use PocketMine\Server;
use PocketMine\Utils\TextFormat;
class BanListCommand extends VanillaCommand{
public function __construct($name){
parent::__construct(
$name,
"View all players banned from this server",
"/banlist [ips|players]"
);
$this->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;
}
}

View File

@ -0,0 +1,56 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace PocketMine\Command\Defaults;
use PocketMine\Command\Command;
use PocketMine\Command\CommandSender;
use PocketMine\Player;
use PocketMine\Server;
use PocketMine\Utils\TextFormat;
class PardonCommand extends VanillaCommand{
public function __construct($name){
parent::__construct(
$name,
"Allows the specified player to use this server",
"/pardon <player>"
);
$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;
}
}

View File

@ -0,0 +1,60 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace PocketMine\Command\Defaults;
use PocketMine\Command\Command;
use PocketMine\Command\CommandSender;
use PocketMine\Player;
use PocketMine\Server;
use PocketMine\Utils\TextFormat;
class PardonIpCommand extends VanillaCommand{
public function __construct($name){
parent::__construct(
$name,
"Allows the specified IP address to use this server",
"/pardon-ip <address>"
);
$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;
}
}

View File

@ -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;
}

View File

@ -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
*/