Enhance nodelist import
This commit is contained in:
@@ -2,16 +2,18 @@
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
use App\Http\Controllers\DomainController;
|
||||
use App\Models\{Address,Domain,Nodelist,System,Zone};
|
||||
use App\Models\{Address,Domain,Nodelist,Setup,System,Zone};
|
||||
use App\Traits\Import as ImportTrait;
|
||||
|
||||
class NodelistImport implements ShouldQueue
|
||||
@@ -21,27 +23,23 @@ class NodelistImport implements ShouldQueue
|
||||
|
||||
protected const LOGKEY = 'JIN';
|
||||
private const importkey = 'nodelist';
|
||||
|
||||
private string $file;
|
||||
private Domain $do;
|
||||
private Nodelist $no;
|
||||
private bool $deletefile = FALSE;
|
||||
private bool $delete_file;
|
||||
private bool $delete_recs;
|
||||
|
||||
/**
|
||||
* Import Nodelist constructor.
|
||||
*
|
||||
* @param Domain $do
|
||||
* @param Nodelist $no
|
||||
* @param string $file
|
||||
* @param bool $delete
|
||||
* @param bool $delete_recs
|
||||
* @param bool $delete_file
|
||||
*/
|
||||
public function __construct(Domain $do,Nodelist $no,string $file,bool $delete=TRUE)
|
||||
public function __construct(string $file,bool $delete_recs=FALSE,bool $delete_file=TRUE)
|
||||
{
|
||||
$this->do = $do;
|
||||
$this->no = $no;
|
||||
$this->file = $file;
|
||||
|
||||
if ($delete)
|
||||
$no->addresses()->detach();
|
||||
$this->delete_file = $delete_file;
|
||||
$this->delete_recs = $delete_recs;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,21 +50,56 @@ class NodelistImport implements ShouldQueue
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$us = Setup::findOrFail(config('app.id'));
|
||||
|
||||
// Get the file from the host
|
||||
$file = $this->getFileFromHost('',self::importkey,$this->file);
|
||||
$lines = $this->getFileLines($file);
|
||||
Log::debug(sprintf('%s:Processing [%d] lines.',static::LOGKEY,$lines));
|
||||
|
||||
$fh = fopen($file,'r');
|
||||
|
||||
// Line 1 tells us the nodelist and the CRC
|
||||
$line = stream_get_line($fh, 0, "\r\n");
|
||||
|
||||
$matches = [];
|
||||
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)))
|
||||
abort(500,'Nodelist invalid');
|
||||
|
||||
$file_crc = $matches[4];
|
||||
$do = Domain::where('name',strtolower($matches[1]))->single();
|
||||
|
||||
if (! $do) {
|
||||
Log::error(sprintf('%s:! Domain not found [%s].',static::LOGKEY,strtolower($matches[1])));
|
||||
return;
|
||||
}
|
||||
|
||||
$date = Carbon::createFromFormat('D, M d, Y H:i',$matches[2].'0:00');
|
||||
|
||||
if ($date->dayOfYear != $matches[3]) {
|
||||
Log::error(sprintf('%s:! Nodelist date doesnt match [%d] (%d:%s).',static::LOGKEY,$matches[3],$date->dayOfYear,$date->format('Y-m-d')));
|
||||
return;
|
||||
}
|
||||
|
||||
Log::debug(sprintf('%s:Importing nodelist for [%s] dated [%s] lines.',static::LOGKEY,$do->name,$date->format('Y-m-d')));
|
||||
|
||||
DB::beginTransaction();
|
||||
$no = Nodelist::firstOrCreate(['date'=>$date,'domain_id'=>$do->id]);
|
||||
|
||||
if ($this->delete_recs)
|
||||
$no->addresses()->detach();
|
||||
|
||||
$p = $c =0;
|
||||
|
||||
$region = NULL;
|
||||
$host = NULL;
|
||||
$hub_id = NULL;
|
||||
$zo = NULL;
|
||||
$tocrc = '';
|
||||
|
||||
while (! feof($fh)) {
|
||||
$line = stream_get_line($fh, 0, "\r\n");
|
||||
$tocrc .= $line."\r\n";
|
||||
|
||||
// Lines beginning with a semicolon(;) are comments
|
||||
if (preg_match('/^;/',$line) OR ($line == chr(0x1a)))
|
||||
@@ -92,7 +125,7 @@ class NodelistImport implements ShouldQueue
|
||||
Zone::unguard();
|
||||
$zo = Zone::firstOrNew([
|
||||
'zone_id'=>$zone,
|
||||
'domain_id'=>$this->do->id,
|
||||
'domain_id'=>$do->id,
|
||||
'active'=>TRUE,
|
||||
]);
|
||||
Zone::reguard();
|
||||
@@ -159,6 +192,7 @@ class NodelistImport implements ShouldQueue
|
||||
|
||||
Address::unguard();
|
||||
$ao = Address::firstOrNew([
|
||||
'zone_id' => $zo->id,
|
||||
'region_id' => $region,
|
||||
'host_id' => $host,
|
||||
'node_id' => $node,
|
||||
@@ -177,25 +211,27 @@ class NodelistImport implements ShouldQueue
|
||||
|
||||
$sysop = trim(str_replace('_',' ',$fields[4]));
|
||||
$system = trim(str_replace('_',' ',$fields[2]));
|
||||
$location = trim(str_replace('_',' ',$fields[3]));
|
||||
|
||||
// Get the System
|
||||
if ($ao->system_id && (($ao->system->sysop === $sysop) || ($ao->system->name === $system))) {
|
||||
$so = $ao->system;
|
||||
|
||||
// If the sysop name is different
|
||||
if ($so->sysop !== $sysop) {
|
||||
Log::debug(sprintf('%s:Sysop Name changed for BBS [%s:%s] from [%s] to [%s]',
|
||||
self::LOGKEY,$so->id,$so->name,$so->sysop,$sysop));
|
||||
// Dont change the system details if a user exists here, or its us
|
||||
if ((! $so->users->count()) && ($so->id != $us->system_id)) {
|
||||
// If the sysop name is different
|
||||
if ($so->sysop !== $sysop) {
|
||||
Log::debug(sprintf('%s:Sysop Name changed for BBS [%s:%s] from [%s] to [%s]',
|
||||
self::LOGKEY,$so->id,$so->name,$so->sysop,$sysop));
|
||||
|
||||
$so->sysop = $sysop;
|
||||
$so->sysop = $sysop;
|
||||
|
||||
// We have the same name has changed.
|
||||
} elseif ($so->name !== $system) {
|
||||
Log::debug(sprintf('%s:System Name changed for BBS [%s:%s] to [%s]',
|
||||
self::LOGKEY,$so->id,$so->name,$system));
|
||||
// We have the same name has changed.
|
||||
} elseif ($so->name !== $system) {
|
||||
Log::debug(sprintf('%s:System Name changed for BBS [%s:%s] to [%s]',
|
||||
self::LOGKEY,$so->id,$so->name,$system));
|
||||
|
||||
$so->name = $system;
|
||||
$so->name = $system;
|
||||
}
|
||||
}
|
||||
|
||||
// We'll search and see if we already have that system
|
||||
@@ -214,10 +250,11 @@ class NodelistImport implements ShouldQueue
|
||||
$so->active = TRUE;
|
||||
|
||||
if (! $so->exists)
|
||||
$so->notes = sprintf('Created by Nodelist Import: %d',$this->no->id);
|
||||
$so->notes = sprintf('Created by Nodelist Import: %d',$no->id);
|
||||
}
|
||||
|
||||
$so->location = $location;
|
||||
$so->phone = $fields[5] != '-Unpublished-' ? $fields[5] : NULL;
|
||||
$so->location = trim(str_replace('_',' ',$fields[3]));
|
||||
/*
|
||||
if (! in_array($fields[5],['-Unpublished-']))
|
||||
$so->phone = $fields[5];
|
||||
@@ -225,6 +262,70 @@ class NodelistImport implements ShouldQueue
|
||||
$so->baud = $fields[6];
|
||||
*/
|
||||
|
||||
// Flags
|
||||
$method = NULL;
|
||||
$address = '';
|
||||
$port = '';
|
||||
for ($i=7;$i<count($fields);$i++) {
|
||||
$x = Str::of($fields[$i])->split('/:/');
|
||||
|
||||
switch ($x->first()) {
|
||||
// address
|
||||
case 'INA':
|
||||
$address = $x->get(1);
|
||||
break;
|
||||
|
||||
case 'IBN':
|
||||
$method = Setup::O_BINKP;
|
||||
|
||||
switch ($x->count()) {
|
||||
case 1:
|
||||
$port = 24554;
|
||||
break;
|
||||
case 2:
|
||||
$port = $x->get(1);
|
||||
break;
|
||||
case 3:
|
||||
$address = $x->get(1);
|
||||
$port = $x->get(2);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'ITN':
|
||||
$method = Setup::O_EMSI;
|
||||
|
||||
switch ($x->count()) {
|
||||
case 1:
|
||||
$port = 60179;
|
||||
break;
|
||||
case 2:
|
||||
$port = $x->get(1);
|
||||
break;
|
||||
case 3:
|
||||
$address = $x->get(1);
|
||||
$port = $x->get(2);
|
||||
}
|
||||
break;
|
||||
|
||||
// Ignore
|
||||
case 'ZEC':
|
||||
case 'REC':
|
||||
case 'MO':
|
||||
case 'CM':
|
||||
break;
|
||||
|
||||
default:
|
||||
Log::debug(sprintf('%s: - Not configured to handle flag [%s]',self::LOGKEY,$x->first()));
|
||||
continue 2;
|
||||
}
|
||||
|
||||
if ($method && ($ao->role != DomainController::NODE_PVT)) {
|
||||
$so->mailer_type = $method;
|
||||
$so->mailer_address = $address;
|
||||
$so->mailer_port = $port;
|
||||
}
|
||||
}
|
||||
|
||||
// Save the system record
|
||||
$so->save();
|
||||
|
||||
@@ -244,7 +345,7 @@ class NodelistImport implements ShouldQueue
|
||||
if ($ao->role == DomainController::NODE_HC)
|
||||
$hub_id = $ao->id;
|
||||
|
||||
$this->no->addresses()->attach($ao,['role'=>$ao->role]);
|
||||
$no->addresses()->attach($ao,['role'=>$ao->role]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error(sprintf('%s:Error with line [%s] (%s)',self::LOGKEY,$line,$e->getMessage()),['fields'=>$fields]);
|
||||
@@ -256,9 +357,17 @@ class NodelistImport implements ShouldQueue
|
||||
}
|
||||
}
|
||||
|
||||
// Remove addresses not recorded;
|
||||
Address::whereIN('id',$zo->addresses->except($us->system->addresses->pluck('id')->toArray())->diff($no->addresses)->pluck('id')->toArray())->delete();
|
||||
|
||||
if (crc16(substr($tocrc,0,-3)) == $file_crc)
|
||||
DB::commit();
|
||||
else
|
||||
DB::rollBack();
|
||||
|
||||
fclose($fh);
|
||||
|
||||
if ($this->deletefile and $c)
|
||||
if ($this->delete_file and $c)
|
||||
unlink($file);
|
||||
|
||||
Log::info(sprintf('%s:Updated %d records from %d systems',self::LOGKEY,$p,$c));
|
||||
|
Reference in New Issue
Block a user