2021-06-25 11:31:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
2021-08-21 11:15:22 +00:00
|
|
|
use Carbon\Carbon;
|
2021-06-25 11:31:57 +00:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2022-11-04 23:35:41 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2021-08-21 11:15:22 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2021-06-25 11:31:57 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-08-21 11:15:22 +00:00
|
|
|
use Illuminate\Support\Str;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
use App\Models\{Address,Domain,File,Mailer,Nodelist,System,Zone};
|
2021-06-25 11:31:57 +00:00
|
|
|
use App\Traits\Import as ImportTrait;
|
|
|
|
|
2021-06-26 00:55:02 +00:00
|
|
|
class NodelistImport implements ShouldQueue
|
2021-06-25 11:31:57 +00:00
|
|
|
{
|
2024-06-15 04:23:05 +00:00
|
|
|
use Dispatchable,InteractsWithQueue,Queueable,SerializesModels,ImportTrait;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
2021-09-11 14:07:02 +00:00
|
|
|
protected const LOGKEY = 'JNI';
|
2021-06-25 11:31:57 +00:00
|
|
|
private const importkey = 'nodelist';
|
2021-08-21 11:15:22 +00:00
|
|
|
|
2023-09-05 07:42:41 +00:00
|
|
|
public const QUEUE = 'nodelist';
|
|
|
|
|
2023-07-09 12:03:15 +00:00
|
|
|
private File|string $file;
|
2022-11-04 23:35:41 +00:00
|
|
|
private ?string $domain;
|
2021-08-21 11:15:22 +00:00
|
|
|
private bool $delete_file;
|
|
|
|
private bool $delete_recs;
|
2023-09-07 02:24:01 +00:00
|
|
|
private bool $testmode;
|
2023-09-14 13:05:05 +00:00
|
|
|
private bool $ignore_crc;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
/**
|
2024-06-15 04:23:05 +00:00
|
|
|
* Import Nodelist from a file.
|
|
|
|
*
|
|
|
|
* A nodelist is treated as authoritative (it will add/update/delete details of existing entries), except where:
|
|
|
|
* + The system has a user owner (the user provides the authoritative information)
|
|
|
|
* + The system is a node of this instance (the admin provides the authoritative information)
|
2021-06-25 11:31:57 +00:00
|
|
|
*
|
2023-07-09 12:03:15 +00:00
|
|
|
* @param File|string $file
|
2022-11-04 23:35:41 +00:00
|
|
|
* @param string|null $domain
|
2021-08-21 11:15:22 +00:00
|
|
|
* @param bool $delete_recs
|
|
|
|
* @param bool $delete_file
|
2023-09-07 02:24:01 +00:00
|
|
|
* @param bool $test
|
2023-09-14 13:05:05 +00:00
|
|
|
* @param bool $ignore_crc
|
2021-06-25 11:31:57 +00:00
|
|
|
*/
|
2023-09-14 13:05:05 +00:00
|
|
|
public function __construct(File|string $file,string $domain=NULL,bool $delete_recs=FALSE,bool $delete_file=TRUE,bool $test=FALSE,bool $ignore_crc = FALSE)
|
2021-06-25 11:31:57 +00:00
|
|
|
{
|
|
|
|
$this->file = $file;
|
2022-11-04 23:35:41 +00:00
|
|
|
$this->domain = $domain;
|
2021-08-21 11:15:22 +00:00
|
|
|
$this->delete_file = $delete_file;
|
|
|
|
$this->delete_recs = $delete_recs;
|
2023-09-07 02:24:01 +00:00
|
|
|
$this->testmode = $test;
|
2023-09-14 13:05:05 +00:00
|
|
|
$this->ignore_crc = $ignore_crc;
|
2023-09-05 07:42:41 +00:00
|
|
|
|
|
|
|
$this->onQueue(self::QUEUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __get($key): mixed
|
|
|
|
{
|
|
|
|
switch ($key) {
|
2024-05-21 11:10:58 +00:00
|
|
|
case 'jobname':
|
2023-09-05 07:42:41 +00:00
|
|
|
return sprintf('%s-%s',$this->domain?->name,is_object($this->file) ? $this->file->name : $this->file);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-06-25 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
// Get the file from the host
|
2022-11-04 11:26:08 +00:00
|
|
|
$file = $this->getFileFromHost(self::importkey,$this->file);
|
2024-06-15 04:23:05 +00:00
|
|
|
Log::debug(sprintf('%s:+ Loading file [%s].',static::LOGKEY,$file));
|
2024-06-15 05:12:09 +00:00
|
|
|
$z = $this->openFile($file);
|
2021-06-25 11:31:57 +00:00
|
|
|
|
2024-06-15 05:12:09 +00:00
|
|
|
$c = 0;
|
2022-11-04 11:26:08 +00:00
|
|
|
$fh = NULL;
|
2024-06-15 05:12:09 +00:00
|
|
|
while ($c < $z->count()) {
|
|
|
|
// Nodelist files have an extension of numbers, between 1-365
|
|
|
|
if (preg_match('/^.+\.[0-3][0-9][0-9]$/',$z->getNameIndex($c))) {
|
|
|
|
$fh = $z->getStreamIndex($c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($fh))
|
|
|
|
throw new \Exception('Couldnt find nodelist in file');
|
|
|
|
|
|
|
|
$lines = $this->getFileLines($fh);
|
|
|
|
Log::debug(sprintf('%s:- Processing [%d] lines.',static::LOGKEY,$lines));
|
|
|
|
// Rewind
|
|
|
|
$fh = $z->getStreamIndex($c);
|
2021-08-21 11:15:22 +00:00
|
|
|
|
|
|
|
// Line 1 tells us the nodelist and the CRC
|
2022-11-04 11:26:08 +00:00
|
|
|
$line = stream_get_line($fh,0,"\r\n");
|
2021-08-21 11:15:22 +00:00
|
|
|
|
|
|
|
$matches = [];
|
2022-11-04 11:26:08 +00:00
|
|
|
if ((! preg_match('/^;A\ /',$line)) || (! preg_match('/^;A\ (.*)\ Nodelist for ([MTWFS][a-z]+,\ [JFMASOND][a-z]+\ [0-9]{1,2},\ [0-9]{4})\ --\ Day\ number\ ([0-9]+)\ :\ ([0-9a-f]+)$/',$line,$matches))) {
|
2023-09-07 02:24:01 +00:00
|
|
|
Log::error(sprintf('%s:! Nodelist file [%d] is not valid?',self::LOGKEY,$this->file->id),['m'=>$matches,'l'=>$line]);
|
|
|
|
|
2022-11-04 23:35:41 +00:00
|
|
|
throw new \Exception('Invalid nodelist for file: '.$this->file->id);
|
2022-11-04 11:26:08 +00:00
|
|
|
}
|
2021-08-21 11:15:22 +00:00
|
|
|
|
2023-07-09 12:03:15 +00:00
|
|
|
$file_crc = (int)$matches[4];
|
2022-11-04 23:35:41 +00:00
|
|
|
$do = Domain::where('name',strtolower($matches[1] ?: $this->domain))->single();
|
2021-08-21 11:15:22 +00:00
|
|
|
|
|
|
|
if (! $do) {
|
2022-11-04 23:35:41 +00:00
|
|
|
Log::error(sprintf('%s:! Domain not found [%s].',static::LOGKEY,strtolower($matches[1] ?: $this->domain)));
|
2023-09-07 02:24:01 +00:00
|
|
|
|
2023-09-14 13:05:05 +00:00
|
|
|
throw new \Exception('Nodelist Domain not found: '.$this->file);
|
2021-08-21 11:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$date = Carbon::createFromFormat('D, M d, Y H:i',$matches[2].'0:00');
|
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
if ($date->dayOfYear !== (int)$matches[3]) {
|
2021-08-21 11:15:22 +00:00
|
|
|
Log::error(sprintf('%s:! Nodelist date doesnt match [%d] (%d:%s).',static::LOGKEY,$matches[3],$date->dayOfYear,$date->format('Y-m-d')));
|
2023-09-07 02:24:01 +00:00
|
|
|
|
2022-11-04 23:35:41 +00:00
|
|
|
throw new \Exception('Nodelist date doesnt match for file: '.$this->file->id);
|
2021-08-21 11:15:22 +00:00
|
|
|
}
|
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
Log::info(sprintf('%s:- Importing nodelist for [%s] dated [%s].',static::LOGKEY,$do->name,$date->format('Y-m-d')));
|
2021-08-21 11:15:22 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
// We'll only commit this if there were no errors
|
2021-08-21 11:15:22 +00:00
|
|
|
DB::beginTransaction();
|
|
|
|
$no = Nodelist::firstOrCreate(['date'=>$date,'domain_id'=>$do->id]);
|
|
|
|
|
|
|
|
if ($this->delete_recs)
|
|
|
|
$no->addresses()->detach();
|
2024-06-15 04:23:05 +00:00
|
|
|
|
2023-10-04 12:42:58 +00:00
|
|
|
elseif ($no->addresses->count()) {
|
2024-06-15 05:12:09 +00:00
|
|
|
Log::error(sprintf('%s:! Nodelist [%s] for [%s] has existing records [%d]',self::LOGKEY,$date,$do->name,$no->addresses->count()));
|
2023-10-04 12:42:58 +00:00
|
|
|
|
2024-06-15 05:12:09 +00:00
|
|
|
return;
|
2023-10-04 12:42:58 +00:00
|
|
|
}
|
2021-08-21 11:15:22 +00:00
|
|
|
|
2024-11-08 12:31:21 +00:00
|
|
|
$mailer_binkp = Mailer::where('name','BINKP')->sole();
|
|
|
|
$mailer_emsi = Mailer::where('name','EMSI')->sole();
|
2024-06-15 04:23:05 +00:00
|
|
|
|
2021-08-27 14:10:42 +00:00
|
|
|
$p = $c = 0;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
$region = NULL;
|
|
|
|
$host = NULL;
|
2024-06-15 04:23:05 +00:00
|
|
|
$ishub = FALSE;
|
2021-06-26 00:34:49 +00:00
|
|
|
$zo = NULL;
|
2024-06-15 04:23:05 +00:00
|
|
|
$ho = NULL;
|
|
|
|
$crc_check = '';
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
while (! feof($fh)) {
|
2022-11-04 11:26:08 +00:00
|
|
|
$line = stream_get_line($fh,0,"\r\n");
|
2024-06-15 04:23:05 +00:00
|
|
|
$crc_check .= $line."\r\n";
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
// Lines beginning with a semicolon(;) are comments
|
2023-06-27 07:39:11 +00:00
|
|
|
if ((! $line) OR preg_match('/^;/',$line) OR ($line === chr(0x1a)))
|
2021-06-25 11:31:57 +00:00
|
|
|
continue;
|
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
// Remove any embedded CR and UTF-8 BOM
|
2021-06-25 11:31:57 +00:00
|
|
|
$line = str_replace("\r",'',$line);
|
|
|
|
$line = preg_replace('/^\x{feff}/u','',$line);
|
|
|
|
$c++;
|
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
Log::debug(sprintf('%s:| %s',self::LOGKEY,$line));
|
2023-09-07 02:24:01 +00:00
|
|
|
|
2021-06-25 11:31:57 +00:00
|
|
|
$fields = str_getcsv(trim($line));
|
|
|
|
|
|
|
|
// First field is either zone,region,host,hub,down,pvt (or blank)
|
|
|
|
if ($fields[0] AND ! in_array($fields[0],Nodelist::definitions)) {
|
2023-09-07 02:24:01 +00:00
|
|
|
Log::error(sprintf('%s:! Invalid field zero [%s] - IGNORING record (%s)',self::LOGKEY,$fields[0],$line));
|
2021-06-25 11:31:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$node = 0;
|
2024-05-09 11:22:30 +00:00
|
|
|
$role = NULL;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
switch ($fields[0]) {
|
2022-11-06 03:40:03 +00:00
|
|
|
case 'Zone':
|
2021-06-26 00:34:49 +00:00
|
|
|
Zone::unguard();
|
|
|
|
$zo = Zone::firstOrNew([
|
2024-06-15 04:23:05 +00:00
|
|
|
'zone_id'=>(int)$fields[1],
|
2021-08-21 11:15:22 +00:00
|
|
|
'domain_id'=>$do->id,
|
2021-06-26 00:34:49 +00:00
|
|
|
'active'=>TRUE,
|
|
|
|
]);
|
|
|
|
Zone::reguard();
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
$region = 0;
|
2021-07-26 11:21:58 +00:00
|
|
|
$host = 0;
|
2024-06-15 04:23:05 +00:00
|
|
|
$ishub = FALSE;
|
2024-06-15 05:12:09 +00:00
|
|
|
$ho = NULL;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
2021-06-26 00:34:49 +00:00
|
|
|
break;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
case 'Region':
|
2023-07-09 12:03:15 +00:00
|
|
|
$region = (int)$fields[1];
|
|
|
|
$host = (int)$fields[1];
|
2024-06-15 04:23:05 +00:00
|
|
|
$ishub = FALSE;
|
2024-06-15 05:12:09 +00:00
|
|
|
$ho = NULL;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Host':
|
2023-07-09 12:03:15 +00:00
|
|
|
$host = (int)$fields[1];
|
2024-06-15 04:23:05 +00:00
|
|
|
$ishub = FALSE;
|
2024-06-15 05:12:09 +00:00
|
|
|
$ho = NULL;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
2021-07-26 11:21:58 +00:00
|
|
|
break;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
case 'Hub':
|
2023-07-09 12:03:15 +00:00
|
|
|
$node = (int)$fields[1];
|
2024-06-15 04:23:05 +00:00
|
|
|
$ishub = TRUE;
|
2024-06-15 05:12:09 +00:00
|
|
|
$ho = NULL;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Pvt':
|
2023-07-09 12:03:15 +00:00
|
|
|
$node = (int)$fields[1];
|
2022-01-24 11:56:13 +00:00
|
|
|
$role = Address::NODE_PVT;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2021-06-26 00:34:49 +00:00
|
|
|
case 'Hold':
|
2023-07-09 12:03:15 +00:00
|
|
|
$node = (int)$fields[1];
|
2022-01-24 11:56:13 +00:00
|
|
|
$role = Address::NODE_HOLD;
|
2021-06-26 00:34:49 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2021-06-25 11:31:57 +00:00
|
|
|
case 'Down':
|
2023-07-09 12:03:15 +00:00
|
|
|
$node = (int)$fields[1];
|
2022-01-24 11:56:13 +00:00
|
|
|
$role = Address::NODE_DOWN;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
// Normal Node
|
2021-06-25 11:31:57 +00:00
|
|
|
case '':
|
|
|
|
$node = $fields[1];
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-09-07 02:24:01 +00:00
|
|
|
Log::error(sprintf('%s:! Unhandled first field [%s]',self::LOGKEY,$fields[0]));
|
2021-06-25 11:31:57 +00:00
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
if (! $zo) {
|
2023-09-07 02:24:01 +00:00
|
|
|
Log::error(sprintf('%s:! Zone NOT set, ignoring record...',self::LOGKEY));
|
2021-06-25 11:31:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
// Find or load an existing entry
|
2021-06-25 11:31:57 +00:00
|
|
|
Address::unguard();
|
|
|
|
$ao = Address::firstOrNew([
|
2024-06-15 04:23:05 +00:00
|
|
|
'zone_id' => $zo?->id,
|
2021-06-25 11:31:57 +00:00
|
|
|
'host_id' => $host,
|
|
|
|
'node_id' => $node,
|
|
|
|
'point_id' => 0,
|
2023-07-09 12:03:15 +00:00
|
|
|
'active' => TRUE,
|
2021-06-25 11:31:57 +00:00
|
|
|
]);
|
|
|
|
Address::reguard();
|
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
// If the address doesnt exist, we'll need to add in the region
|
|
|
|
if (! $ao->exists)
|
|
|
|
$ao->region_id = $region;
|
|
|
|
|
|
|
|
// Address moved regions
|
|
|
|
if ($ao->region_id && ($ao->region_id !== $region)) {
|
2023-09-07 02:24:01 +00:00
|
|
|
Log::alert(sprintf('%s:%% Address [%s] changing regions [%d->%d]',self::LOGKEY,$ao->ftn,$ao->region_id,$region));
|
2024-06-15 04:23:05 +00:00
|
|
|
$ao->region_id = $region;
|
|
|
|
}
|
2023-07-09 12:03:15 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
// Hub details changed
|
|
|
|
if ($ao->hub_id && ($ao->hub_id !== $ho?->id)) {
|
|
|
|
Log::alert(sprintf('%s:%% Address [%s] changing hubs [%d->%s]',self::LOGKEY,$ao->ftn,$ao->hub_id,$ho?->id));
|
|
|
|
$ao->hub_id = $ho?->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sysop = trim(str_replace('_',' ',$fields[4]));
|
|
|
|
$system = trim(str_replace('_',' ',$fields[2]));
|
2021-06-25 11:31:57 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
$protect = FALSE;
|
2023-10-04 12:42:58 +00:00
|
|
|
if ($ao->exists) {
|
|
|
|
Log::info(sprintf('%s:- Processing existing address [%s] (%d)',self::LOGKEY,$ao->ftn,$ao->id));
|
|
|
|
|
|
|
|
// If the address is linked to a user's system, or our system, we'll not process it any further
|
2024-06-15 04:23:05 +00:00
|
|
|
if (our_address()->contains($ao->id)) {
|
|
|
|
Log::info(sprintf('%s:! Limiting update to an address belonging to me',self::LOGKEY));
|
|
|
|
$protect = TRUE;
|
2023-10-04 12:42:58 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
} elseif ($ao->is_hosted) {
|
|
|
|
Log::info(sprintf('%s:! Limiting update to a system managed by this site',self::LOGKEY));
|
|
|
|
$protect = TRUE;
|
2023-10-04 12:42:58 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
} elseif ($ao->is_owned) {
|
|
|
|
Log::info(sprintf('%s:! Limiting update to a system managed by a user',self::LOGKEY));
|
|
|
|
$protect = TRUE;
|
2023-10-04 12:42:58 +00:00
|
|
|
}
|
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
$so = $ao->system;
|
2023-10-04 12:42:58 +00:00
|
|
|
}
|
2021-06-26 00:34:49 +00:00
|
|
|
|
2021-08-21 11:15:22 +00:00
|
|
|
// Flags
|
2023-07-09 12:03:15 +00:00
|
|
|
$methods = collect();
|
2021-08-21 11:15:22 +00:00
|
|
|
$address = '';
|
|
|
|
for ($i=7;$i<count($fields);$i++) {
|
|
|
|
$x = Str::of($fields[$i])->split('/:/');
|
|
|
|
|
|
|
|
switch ($x->first()) {
|
2023-07-09 12:03:15 +00:00
|
|
|
// Address
|
2021-08-21 11:15:22 +00:00
|
|
|
case 'INA':
|
|
|
|
$address = $x->get(1);
|
|
|
|
break;
|
|
|
|
|
2023-07-09 12:03:15 +00:00
|
|
|
// BINKP
|
2021-08-21 11:15:22 +00:00
|
|
|
case 'IBN':
|
2023-07-09 12:03:15 +00:00
|
|
|
case 'ITN':
|
|
|
|
if ($x->first() === 'IBN') {
|
|
|
|
$dport = 24554;
|
|
|
|
$method = $mailer_binkp->id;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$dport = 60179;
|
|
|
|
$method = $mailer_emsi->id;
|
|
|
|
}
|
2021-08-21 11:15:22 +00:00
|
|
|
|
|
|
|
switch ($x->count()) {
|
|
|
|
case 1:
|
2023-07-09 12:03:15 +00:00
|
|
|
$methods->put($method,['port'=>$dport]);
|
2021-08-21 11:15:22 +00:00
|
|
|
break;
|
2023-07-09 12:03:15 +00:00
|
|
|
|
2021-08-21 11:15:22 +00:00
|
|
|
case 2:
|
2023-07-09 12:03:15 +00:00
|
|
|
$mp = is_numeric($x->get(1)) ? (int)$x->get(1) : $dport;
|
|
|
|
$ma = is_numeric($xx=$x->get(1)) ? NULL : $xx;
|
|
|
|
|
|
|
|
if ($ma && ($ma !== $address))
|
|
|
|
$methods->put($method,['address'=>$ma,'port'=>$mp]);
|
|
|
|
else
|
|
|
|
$methods->put($method,['port'=>$mp]);
|
|
|
|
|
2021-08-21 11:15:22 +00:00
|
|
|
break;
|
2023-07-09 12:03:15 +00:00
|
|
|
|
2021-08-21 11:15:22 +00:00
|
|
|
case 3:
|
2023-07-09 12:03:15 +00:00
|
|
|
$mp = (int)$x->get(2);
|
|
|
|
$ma = $x->get(1);
|
2021-08-21 11:15:22 +00:00
|
|
|
|
2023-07-09 12:03:15 +00:00
|
|
|
if ($ma && ($ma !== $address))
|
|
|
|
$methods->put($method,['address'=>$ma,'port'=>$mp]);
|
|
|
|
else
|
|
|
|
$methods->put($method,['port'=>$mp]);
|
2021-08-21 11:15:22 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2023-07-09 12:03:15 +00:00
|
|
|
|
2021-08-21 11:15:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Ignore
|
|
|
|
case 'ZEC':
|
|
|
|
case 'REC':
|
|
|
|
case 'MO':
|
|
|
|
case 'CM':
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-09-07 02:24:01 +00:00
|
|
|
Log::debug(sprintf('%s:! Not configured to handle flag [%s]',self::LOGKEY,$x->first()));
|
2021-08-21 11:15:22 +00:00
|
|
|
continue 2;
|
|
|
|
}
|
2021-08-28 03:36:42 +00:00
|
|
|
}
|
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
// If we are a zone/region/host record, then the system servicing that address may change
|
|
|
|
switch ($ao->role_id) {
|
2023-07-09 12:03:15 +00:00
|
|
|
case Address::NODE_ZC:
|
|
|
|
case Address::NODE_RC:
|
|
|
|
case Address::NODE_NC:
|
2024-06-15 04:23:05 +00:00
|
|
|
// For new address, we'll need to add/link to a system
|
|
|
|
if ($ao->exists) {
|
|
|
|
if (($ao->system->address !== $address) || ($ao->system->name !== $system) || ($ao->system->sysop !== $sysop)) {
|
|
|
|
Log::alert(sprintf('%s:! System has changed for [%s], no longer [%s]',self::LOGKEY,$ao->ftn,$ao->system->name));
|
2023-07-09 12:03:15 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
$ao->active = FALSE;
|
|
|
|
$ao->save();
|
2023-07-09 12:03:15 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
$ao = $ao->replicate();
|
|
|
|
$ao->active = TRUE;
|
|
|
|
$ao->system_id = NULL;
|
|
|
|
}
|
2023-07-09 12:03:15 +00:00
|
|
|
}
|
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
if (! $ao->system_id) {
|
|
|
|
// When searching for a system, we prioritise in this order:
|
|
|
|
// - the mailer address is correct
|
|
|
|
// - the system name is correct
|
|
|
|
$so = System::select('systems.*')
|
|
|
|
->join('mailer_system',['mailer_system.system_id'=>'systems.id'])
|
|
|
|
->when($address,
|
|
|
|
fn($query)=>$query->where(
|
|
|
|
fn($query)=>$query
|
|
|
|
->where('systems.address',$address)
|
|
|
|
->orWhere('mailer_system.address',$address)
|
|
|
|
),
|
|
|
|
fn($query)=>$query->where('systems.name',$system))
|
|
|
|
->single();
|
|
|
|
|
|
|
|
// If no system, we'll need to create one
|
|
|
|
if (! $so) {
|
|
|
|
Log::info(sprintf('%s:= New System for ZC/RC/NC [%s] - System [%s] Sysop [%s]',self::LOGKEY,$ao->ftn,$system,$sysop));
|
|
|
|
$so = new System;
|
|
|
|
$so->sysop = $sysop;
|
|
|
|
$so->name = $system;
|
|
|
|
$so->address = $address;
|
|
|
|
$so->location = 'TBA';
|
|
|
|
$so->notes = sprintf('Created by Nodelist Import: %d',$no->id);
|
|
|
|
$so->active = TRUE;
|
|
|
|
|
|
|
|
$so->save();
|
|
|
|
}
|
2023-07-09 12:03:15 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
$ao->system_id = $so->id;
|
2023-07-09 12:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$ao->save();
|
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
$no->addresses()->attach($ao,['role'=>$ao->role_id]);
|
|
|
|
continue 2;
|
2023-07-09 12:03:15 +00:00
|
|
|
}
|
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
if (! $protect) {
|
|
|
|
// Normal Node
|
|
|
|
if ($ao->system_id && (
|
|
|
|
(($ao->system->sysop === $sysop) || ($ao->system->name === $system))
|
|
|
|
&& (($ao->system->address === $address) || $methods->pluck('address')->contains($address))))
|
|
|
|
{
|
|
|
|
Log::info(sprintf('%s:= Matched [%s] to existing system [%s] with address [%s]',self::LOGKEY,$ao->ftn,$ao->system->name,$ao->system->address));
|
|
|
|
$so = $ao->system;
|
2021-08-28 03:36:42 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
// If the sysop name is different
|
|
|
|
if ($so->sysop !== $sysop) {
|
|
|
|
Log::alert(sprintf('%s:! Sysop Name changed for BBS [%s:%s] from [%s] to [%s]',self::LOGKEY,$so->id,$so->name,$so->sysop,$sysop));
|
2021-08-21 11:15:22 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
$so->sysop = $sysop;
|
2021-08-28 03:36:42 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
// We have the same name has changed (except for ZC/RC addresses)
|
|
|
|
} elseif ($so->name !== $system) {
|
|
|
|
Log::alert(sprintf('%s:! System Name changed for BBS [%s:%s] to [%s]',self::LOGKEY,$so->id,$so->name,$system));
|
2021-08-28 03:36:42 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
$so->name = $system;
|
|
|
|
}
|
2021-10-07 12:32:05 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
// We'll search and see if we already have that system
|
|
|
|
} else {
|
|
|
|
Log::debug(sprintf('%s:- Looking for existing system [%s] with address [%s]',self::LOGKEY,$system,$address));
|
|
|
|
// When searching for a system, we prioritise in this order:
|
|
|
|
// - the mailer address is correct
|
|
|
|
// - the system name is correct
|
2023-07-09 12:03:15 +00:00
|
|
|
$so = System::select('systems.*')
|
|
|
|
->join('mailer_system',['mailer_system.system_id'=>'systems.id'])
|
2024-06-15 04:23:05 +00:00
|
|
|
->when($address,
|
|
|
|
fn($query)=>$query->where(
|
|
|
|
fn($query)=>$query
|
|
|
|
->where('systems.address',$address)
|
|
|
|
->orWhere('mailer_system.address',$address)
|
|
|
|
),
|
|
|
|
fn($query)=>$query->where('systems.name',$system))
|
2021-10-07 12:32:05 +00:00
|
|
|
->single();
|
2023-09-07 02:24:01 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
if (! $so) {
|
|
|
|
Log::debug(sprintf('%s:- Didnt match on system address, looking for System [%s] AND Sysop [%s]',self::LOGKEY,$system,$sysop));
|
2021-09-11 08:00:39 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
$so = System::where('name',$system)
|
|
|
|
->where('sysop',$sysop)
|
|
|
|
->firstOrNew();
|
2021-08-28 03:36:42 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
} else {
|
|
|
|
Log::debug(sprintf('%s:- Matched on system [%d] address',self::LOGKEY,$so->id));
|
|
|
|
}
|
2023-09-07 02:24:01 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
if ($so->exists)
|
|
|
|
Log::info(sprintf('%s:= Linking address [%d:%d/%d] to [%s:%s]',self::LOGKEY,$zo->zone_id,$ao->host_id,$ao->node_id,$so->id,$so->name));
|
|
|
|
else
|
|
|
|
Log::info(sprintf('%s:= New System [%s] with FTN [%d:%d/%d]',self::LOGKEY,$system,$zo->zone_id,$ao->host_id,$ao->node_id));
|
2021-08-28 03:36:42 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
$so->name = $system;
|
|
|
|
$so->sysop = $sysop;
|
|
|
|
$so->active = TRUE;
|
2021-08-28 03:36:42 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
if (! $so->exists)
|
|
|
|
$so->notes = sprintf('Created by Nodelist Import: %d',$no->id);
|
|
|
|
}
|
2021-08-28 03:36:42 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
$so->phone = $fields[5] != '-Unpublished-' ? $fields[5] : NULL;
|
|
|
|
$so->location = trim(str_replace('_',' ',$fields[3]));
|
2021-08-28 03:36:42 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
// Save the system record
|
|
|
|
try {
|
|
|
|
$so->save();
|
2021-08-28 03:36:42 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error(sprintf('%s:! Error with line [%s] (%s)',self::LOGKEY,$line,$e->getMessage()),['fields'=>$fields]);
|
2022-11-11 22:32:39 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
DB::rollBack();
|
|
|
|
throw new \Exception($e->getMessage());
|
|
|
|
}
|
2023-07-09 12:03:15 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
$ao->system_id = $so->id;
|
2022-11-11 22:32:39 +00:00
|
|
|
}
|
2021-06-25 11:31:57 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
if ($methods->count() && (! $ao->is_private)) {
|
|
|
|
$methods->transform(function($item) {
|
|
|
|
$item['active'] = Arr::get($item,'active',TRUE);
|
|
|
|
return $item;
|
|
|
|
});
|
|
|
|
|
2023-07-09 12:03:15 +00:00
|
|
|
$so->mailers()->sync($methods);
|
|
|
|
}
|
|
|
|
|
2021-06-26 00:34:49 +00:00
|
|
|
// If our zone didnt exist, we'll create it with this system
|
|
|
|
if (! $zo->exists) {
|
2024-06-15 04:23:05 +00:00
|
|
|
$zo->system_id = $ao->system_id;
|
2021-06-26 00:34:49 +00:00
|
|
|
$zo->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$ao->zone_id = $zo->id;
|
2024-06-15 04:23:05 +00:00
|
|
|
$ao->role = $role;
|
2021-06-26 00:34:49 +00:00
|
|
|
|
2021-06-26 00:55:02 +00:00
|
|
|
if ($ao->getDirty())
|
2021-06-26 00:34:49 +00:00
|
|
|
$p++;
|
|
|
|
|
2021-06-25 11:31:57 +00:00
|
|
|
try {
|
|
|
|
$so->addresses()->save($ao);
|
2023-07-09 12:03:15 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
// If we were the hub
|
|
|
|
if ($ishub) {
|
|
|
|
$ho = $ao;
|
|
|
|
$ishub = FALSE;
|
|
|
|
}
|
2021-06-25 11:31:57 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
$no->addresses()->attach($ao,['role'=>($ao->role_id & $role)]);
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
2023-09-07 02:24:01 +00:00
|
|
|
Log::error(sprintf('%s:! Error with line [%s] (%s)',self::LOGKEY,$line,$e->getMessage()),['fields'=>$fields]);
|
2023-07-09 12:03:15 +00:00
|
|
|
|
2022-11-11 22:32:39 +00:00
|
|
|
DB::rollBack();
|
|
|
|
throw new \Exception($e->getMessage());
|
2021-06-25 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (! ($c % 100)) {
|
2023-09-07 02:24:01 +00:00
|
|
|
Log::notice(sprintf('%s:= Processed [%s] records',self::LOGKEY,$c),['memory'=>memory_get_usage(TRUE)]);
|
2021-06-25 11:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 11:15:22 +00:00
|
|
|
// Remove addresses not recorded;
|
2023-10-04 12:42:58 +00:00
|
|
|
$no->load('addresses');
|
2021-08-21 11:15:22 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
$remove = $zo
|
|
|
|
->addresses->pluck('id')
|
|
|
|
->diff($no->addresses->pluck('id'))
|
|
|
|
->diff(our_address($do)->pluck('id'))
|
|
|
|
->diff(our_nodes($do)->pluck('id'));
|
|
|
|
|
|
|
|
$remove = Address::whereIn('id',$remove)->get();
|
2024-06-15 05:12:09 +00:00
|
|
|
Log::alert(sprintf('%s:%% Deleting [%d] addresses [%s]',self::LOGKEY,$remove->count(),$remove->pluck('ftn2d')->join(',')));
|
2024-06-15 04:23:05 +00:00
|
|
|
|
|
|
|
Address::whereIN('id',$remove->pluck('id'))->update(['active'=>FALSE]);
|
|
|
|
Address::whereIN('id',$remove->pluck('id'))->delete();
|
|
|
|
|
|
|
|
Log::info(sprintf('%s:= Updated %d AKA records from %d Systems',self::LOGKEY,$p,$c));
|
|
|
|
$crc = crc16(substr($crc_check,0,-3));
|
2023-10-04 12:42:58 +00:00
|
|
|
|
|
|
|
if ((! $this->testmode) && ($this->ignore_crc || ($crc === $file_crc))) {
|
2023-09-07 02:24:01 +00:00
|
|
|
Log::info(sprintf('%s:= Committing nodelist',self::LOGKEY));
|
2021-08-21 11:15:22 +00:00
|
|
|
DB::commit();
|
2023-10-04 12:42:58 +00:00
|
|
|
|
2024-06-15 04:23:05 +00:00
|
|
|
if ($this->delete_file and $c)
|
|
|
|
unlink($file);
|
|
|
|
|
2022-11-11 13:15:45 +00:00
|
|
|
} else {
|
2024-06-15 04:23:05 +00:00
|
|
|
Log::error(sprintf('%s:! Rolling back nodelist, CRC doesnt match [%s != %s] or TEST mode',self::LOGKEY,$crc,$file_crc));
|
2021-08-21 11:15:22 +00:00
|
|
|
DB::rollBack();
|
2022-11-11 13:15:45 +00:00
|
|
|
}
|
2021-08-21 11:15:22 +00:00
|
|
|
|
2021-06-25 11:31:57 +00:00
|
|
|
fclose($fh);
|
|
|
|
}
|
2022-01-24 11:56:13 +00:00
|
|
|
}
|