mince/tools/sync

45 lines
1.5 KiB
PHP
Executable file

#!/usr/bin/env php
<?php
use Mince\Whitelist;
require_once __DIR__ . '/../mince.php';
echo 'Syncing server whitelists...' . PHP_EOL;
$rInfo = $remote->getInfo();
if(empty($rInfo->servers)) {
echo 'There are no active servers.' . PHP_EOL;
return;
}
echo 'Fetching master list from database...' . PHP_EOL;
$myNames = (new Whitelist($db))->getNames();
foreach($rInfo->servers as $serverId) {
try {
echo "[{$serverId}] Fetching list on server..." . PHP_EOL;
$rWhitelist = $remote->getWhitelist($serverId);
$rNames = $rWhitelist->list ?? [];
echo "[{$serverId}] Filtering..." . PHP_EOL;
$addNames = array_values(array_udiff($myNames, $rNames, 'strcasecmp'));
$removeNames = array_values(array_udiff($rNames, $myNames, $addNames, 'strcasecmp'));
if(!empty($addNames)) {
echo "[{$serverId}] Adding names..." . PHP_EOL;
$addResult = $remote->addToWhitelist($serverId, $addNames);
foreach($addResult->results as $name => $result)
echo "[{$serverId}] [{$name}] {$result->message}" . PHP_EOL;
}
if(!empty($removeNames)) {
echo "[{$serverId}] Removing names..." . PHP_EOL;
$removeResult = $remote->removeFromWhitelist($serverId, $removeNames);
foreach($removeResult->results as $name => $result)
echo "[{$serverId}] [{$name}] {$result->message}" . PHP_EOL;
}
} catch(RuntimeException $ex) {
var_dump((string)$ex);
echo PHP_EOL;
}
}