Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "src/ARTulloss/FilterX/libs/pasvl"]
path = src/ARTulloss/FilterX/libs/pasvl
url = https://github.com/lezhnev74/pasvl
3 changes: 2 additions & 1 deletion .poggit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ projects:
path: ""
libs:
- src: poggit/libasynql/libasynql
version: ^3.2.0
version: ^3.3.0
branch: "4.0"
...
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: FilterX
main: ARTulloss\FilterX\Main
version: 1.0.0
api: 3.0.0
api: 4.0.0
author: ARTulloss
description: The next generation chat filter
website: https://github.com/artulloss
41 changes: 41 additions & 0 deletions src/ARTulloss/FilterX/Autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace ARTulloss\FilterX;

class Autoloader {

/**
* This autoloader checks the lib directory
*/
public function __construct() {
spl_autoload_register([self::class, 'autoload']);
}

private static function autoload($class) {
// project-specific namespace prefix
$prefix = 'PASVL\\';

// base directory for the namespace prefix
$base_dir = __DIR__ . '/libs/pasvl/src/';

// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}

// get the relative class name
$relative_class = substr($class, $len);

// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

// if the file exists, require it
if (file_exists($file)) {
require $file;
}
}
}
2 changes: 1 addition & 1 deletion src/ARTulloss/FilterX/Events/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
use pocketmine\event\player\PlayerQuitEvent;
use pocketmine\plugin\Plugin;
use pocketmine\utils\Config;
use Exception;
use pocketmine\utils\TextFormat;
use Exception;
use function time;
use const PHP_INT_MAX;

Expand Down
49 changes: 27 additions & 22 deletions src/ARTulloss/FilterX/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@

namespace ARTulloss\FilterX;

use function arsort;
use ARTulloss\FilterX\{Events\Listener,
libs\PASVL\Traverser\FailReport,
libs\PASVL\Traverser\VO\Traverser,
libs\PASVL\ValidatorLocator\ValidatorLocator,
use ARTulloss\FilterX\{
Events\Listener,
Queries\Queries,
Session\SessionHandler};
Session\SessionHandler
};
use PASVL\Validation\{
ValidatorBuilder,
Problems\ArrayFailedValidation,
};
use pocketmine\plugin\PluginBase;
use pocketmine\utils\Config;
use poggit\libasynql\DataConnector;
use poggit\libasynql\libasynql;
use RuntimeException;
use function strtotime;
use function arsort;

class Main extends PluginBase {

Expand All @@ -40,32 +43,35 @@ class Main extends PluginBase {
'soft_mute' => ':bool'
],
'Infraction' => [
'Mode' => ':int :between(1,2)',
'Reset Every' => ':int', # seconds
'Mode' => ':number :int :between(1,2)',
'Reset Every' => ':number :int', # seconds
'Punishments' => [
'*' => ':string :regexp(/^(?:[0-9]+ \)(?:seconds?|minutes?|hours?|days?|weeks?|months?|years?\)$/i)'
// '*' => ':string :regexp(\'^([0-9]+ )(seconds?|minutes?|hours?|days?|weeks?|months?|years?)$\i\')',
// ':number :int' => ':string :regexp(\'^[0-9]+ seconds?|minutes?|hours?|days?|weeks?|months?|years?$\')',
'*' => ':string',
]
]
];

private const DATABASE_PATTERN = [
'type' => ':string :regexp(/^(sqlite|mysql\)$/)',
'sqlite' => [
'file' => ':string :regexp(/^.*\.sqlite$/)'
'type' => ':string :in("sqlite","mysql")',
'sqlite?' => [
'file' => ':string :ends(".sqlite")'
],
'mysql' => [
'host' => ':string :regexp(/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\)\.\){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\)$/)',
'host' => ':string',
'username' => ':string',
'password' => ':string',
'schema' => ':string'
],
'worker-limit' => ':int :min(1)'
'worker-limit' => ':number :int :min(1)'
];

/**
* @throws \Exception
*/
public function onEnable(): void{
new Autoloader();
$this->saveDefaultConfigs();
$this->startDatabase();
$this->checkConfigsValid();
Expand All @@ -80,7 +86,6 @@ public function onEnable(): void{
}
$this->timer = new Timer($resetEverySeconds);
}

}
public function onDisable(): void{
if(isset($this->database))
Expand All @@ -97,15 +102,15 @@ public function saveDefaultConfigs(): void{
* Check if the config is valid
*/
public function checkConfigsValid(): void{
$traverser = new Traverser(new ValidatorLocator());
$cfgBuilder = ValidatorBuilder::forArray(self::CONFIG_PATTERN);
$dbBuilder = ValidatorBuilder::forArray(self::DATABASE_PATTERN);
try {
# TODO Maybe do this by file so it's easier to know which file is broken- but the configs are rather small...
$traverser->match(self::CONFIG_PATTERN, $this->getConfig()->getAll());
$traverser->match(self::DATABASE_PATTERN, $this->databaseConfig->getAll());
} catch (FailReport $report) {
$cfgBuilder->build()->validate($this->getConfig()->getAll());
$dbBuilder->build()->validate($this->databaseConfig->getAll());
} catch (ArrayFailedValidation $e) {
$logger = $this->getLogger();
$logger->error('Invalid config detected! Reason:');
Utils::outputFailReasons($this, $report);
$logger->error('Invalid config detected! Reason: ' . $e->getMessage());
$logger->error('Disabling...');
$this->getServer()->getPluginManager()->disablePlugin($this);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ARTulloss/FilterX/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace ARTulloss\FilterX\Session;

use Exception;
use pocketmine\Player;
use pocketmine\Player\Player;
use function time;

/**
Expand Down
6 changes: 3 additions & 3 deletions src/ARTulloss/FilterX/Session/SessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use ARTulloss\FilterX\Main;
use ARTulloss\FilterX\Queries\Queries;
use ARTulloss\FilterX\Utils;
use pocketmine\Player;
use pocketmine\Player\Player;
use pocketmine\utils\Utils as PMUtils;
use Closure;
use function time;
Expand Down Expand Up @@ -66,7 +66,7 @@ public function passToNewSession(Closure $passTo, Player $player, string $lastMe
// var_dump($result);
if($result !== [])
$session->setSoftMutedFor($result[0]['until'] > time() ? $result[0]['until'] : null);
$this->sessions[$player->getLowerCaseName()] = $session;
$this->sessions[$player->getName()] = $session;
$passTo($session);
}, $onError);
};
Expand Down Expand Up @@ -112,7 +112,7 @@ public function deleteSession(Player $player): void{
* @return Session|null
*/
public function getSession(Player $player): ?Session{
return $this->sessions[$player->getLowerCaseName()] ?? null;
return $this->sessions[$player->getName()] ?? null;
}
/**
* @return Session[]|null
Expand Down
13 changes: 1 addition & 12 deletions src/ARTulloss/FilterX/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

namespace ARTulloss\FilterX;

use ARTulloss\FilterX\libs\PASVL\Traverser\FailReport;
use pocketmine\plugin\Plugin;
use pocketmine\Server;
use function strtolower;
use Throwable;
use Closure;
use function strtolower;

/**
* Class Utils
Expand Down Expand Up @@ -93,14 +92,4 @@ static function getOnError(Plugin $plugin = null): Closure{
$hasLogger->getLogger()->logException($error);
};
}
static function outputFailReasons(Plugin $plugin, FailReport $report): void{
$logger = $plugin->getLogger();
$reason = $report->getReason();
if($reason->isKeyQuantityType())
$logger->error('Invalid key quantity found!');
if($reason->isKeyType())
$logger->error('Invalid key type found!');
if($reason->isValueType())
$logger->error('Invalid value type found!');
}
}
13 changes: 0 additions & 13 deletions src/ARTulloss/FilterX/libs/PASVL/Pattern/InvalidPattern.php

This file was deleted.

Loading