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;
|
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
|
|
|
|
|
|
|
use App\Http\Controllers\DomainController;
|
2021-08-21 11:15:22 +00:00
|
|
|
use App\Models\{Address,Domain,Nodelist,Setup,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
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
use ImportTrait;
|
|
|
|
|
|
|
|
protected const LOGKEY = 'JIN';
|
|
|
|
private const importkey = 'nodelist';
|
2021-08-21 11:15:22 +00:00
|
|
|
|
2021-06-25 11:31:57 +00:00
|
|
|
private string $file;
|
2021-08-21 11:15:22 +00:00
|
|
|
private bool $delete_file;
|
|
|
|
private bool $delete_recs;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Import Nodelist constructor.
|
|
|
|
*
|
|
|
|
* @param string $file
|
2021-08-21 11:15:22 +00:00
|
|
|
* @param bool $delete_recs
|
|
|
|
* @param bool $delete_file
|
2021-06-25 11:31:57 +00:00
|
|
|
*/
|
2021-08-21 11:15:22 +00:00
|
|
|
public function __construct(string $file,bool $delete_recs=FALSE,bool $delete_file=TRUE)
|
2021-06-25 11:31:57 +00:00
|
|
|
{
|
|
|
|
$this->file = $file;
|
2021-08-21 11:15:22 +00:00
|
|
|
$this->delete_file = $delete_file;
|
|
|
|
$this->delete_recs = $delete_recs;
|
2021-06-25 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2021-08-21 11:15:22 +00:00
|
|
|
$us = Setup::findOrFail(config('app.id'));
|
|
|
|
|
2021-06-25 11:31:57 +00:00
|
|
|
// Get the file from the host
|
|
|
|
$file = $this->getFileFromHost('',self::importkey,$this->file);
|
2021-08-28 03:36:42 +00:00
|
|
|
Log::debug(sprintf('%s:Loading file [%s] (%s).',static::LOGKEY,$file,getcwd()));
|
2021-06-25 11:31:57 +00:00
|
|
|
$lines = $this->getFileLines($file);
|
|
|
|
Log::debug(sprintf('%s:Processing [%d] lines.',static::LOGKEY,$lines));
|
|
|
|
|
|
|
|
$fh = fopen($file,'r');
|
2021-08-21 11:15:22 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2021-08-28 03:36:42 +00:00
|
|
|
Log::debug(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
|
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
$no = Nodelist::firstOrCreate(['date'=>$date,'domain_id'=>$do->id]);
|
|
|
|
|
|
|
|
if ($this->delete_recs)
|
|
|
|
$no->addresses()->detach();
|
|
|
|
|
2021-08-27 14:10:42 +00:00
|
|
|
$p = $c = 0;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
$region = NULL;
|
|
|
|
$host = NULL;
|
|
|
|
$hub_id = NULL;
|
2021-06-26 00:34:49 +00:00
|
|
|
$zo = NULL;
|
2021-08-21 11:15:22 +00:00
|
|
|
$tocrc = '';
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
while (! feof($fh)) {
|
|
|
|
$line = stream_get_line($fh, 0, "\r\n");
|
2021-08-21 11:15:22 +00:00
|
|
|
$tocrc .= $line."\r\n";
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
// Lines beginning with a semicolon(;) are comments
|
2021-09-11 09:26:11 +00:00
|
|
|
if ((! $line) OR preg_match('/^;/',$line) OR ($line == chr(0x1a)))
|
2021-06-25 11:31:57 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Remove any embedded CR and BOM
|
|
|
|
$line = str_replace("\r",'',$line);
|
|
|
|
$line = preg_replace('/^\x{feff}/u','',$line);
|
|
|
|
$c++;
|
|
|
|
|
|
|
|
$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)) {
|
|
|
|
Log::error(sprintf('%s:Invalid field zero [%s] - IGNORING record (%s)',self::LOGKEY,$fields[0],$line));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$node = 0;
|
|
|
|
|
|
|
|
switch ($fields[0]) {
|
|
|
|
case 'Zone': $zone = $fields[1];
|
2021-06-26 00:34:49 +00:00
|
|
|
Zone::unguard();
|
|
|
|
$zo = Zone::firstOrNew([
|
|
|
|
'zone_id'=>$zone,
|
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;
|
2021-06-25 11:31:57 +00:00
|
|
|
$hub_id = NULL;
|
|
|
|
$role = DomainController::NODE_ZC;
|
|
|
|
|
2021-06-26 00:34:49 +00:00
|
|
|
break;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
case 'Region':
|
|
|
|
$region = $fields[1];
|
|
|
|
$role = DomainController::NODE_RC;
|
|
|
|
$host = 0;
|
|
|
|
$hub_id = NULL;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Host':
|
|
|
|
$host = $fields[1];
|
|
|
|
$hub_id = NULL;
|
|
|
|
$role = DomainController::NODE_NC;
|
|
|
|
|
2021-07-26 11:21:58 +00:00
|
|
|
break;
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
case 'Hub':
|
2021-06-26 00:34:49 +00:00
|
|
|
$node = $fields[1];
|
2021-06-25 11:31:57 +00:00
|
|
|
$role = DomainController::NODE_HC;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Pvt':
|
|
|
|
$node = $fields[1];
|
|
|
|
$role = DomainController::NODE_PVT;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2021-06-26 00:34:49 +00:00
|
|
|
case 'Hold':
|
|
|
|
$node = $fields[1];
|
|
|
|
$role = DomainController::NODE_HOLD;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2021-06-25 11:31:57 +00:00
|
|
|
case 'Down':
|
|
|
|
$node = $fields[1];
|
|
|
|
$role = DomainController::NODE_DOWN;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '':
|
|
|
|
$node = $fields[1];
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Log::error(sprintf('%s:Unhandled first field [%s]',self::LOGKEY,$fields[0]));
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $zone) {
|
|
|
|
Log::error(sprintf('%s:Zone NOT set, ignoring record...',self::LOGKEY));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Address::unguard();
|
|
|
|
$ao = Address::firstOrNew([
|
2021-08-21 11:15:22 +00:00
|
|
|
'zone_id' => $zo->id,
|
2021-06-25 11:31:57 +00:00
|
|
|
'region_id' => $region,
|
|
|
|
'host_id' => $host,
|
|
|
|
'node_id' => $node,
|
|
|
|
'point_id' => 0,
|
|
|
|
]);
|
|
|
|
Address::reguard();
|
|
|
|
|
|
|
|
$ao->active = TRUE;
|
|
|
|
$ao->role = $role;
|
|
|
|
$ao->hub_id = $hub_id;
|
|
|
|
|
|
|
|
$role = NULL;
|
|
|
|
|
2021-06-26 00:34:49 +00:00
|
|
|
if ($ao->exists)
|
|
|
|
Log::debug(sprintf('%s:Processing existing address [%s]',self::LOGKEY,$ao->ftn));
|
|
|
|
|
|
|
|
$sysop = trim(str_replace('_',' ',$fields[4]));
|
|
|
|
$system = trim(str_replace('_',' ',$fields[2]));
|
|
|
|
|
2021-08-21 11:15:22 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2021-08-28 03:36:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the System
|
|
|
|
if ($ao->system_id && (($ao->system->sysop === $sysop) || ($ao->system->name === $system))) {
|
|
|
|
$so = $ao->system;
|
|
|
|
|
|
|
|
// 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));
|
2021-08-21 11:15:22 +00:00
|
|
|
|
2021-08-28 03:36:42 +00:00
|
|
|
$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));
|
|
|
|
|
|
|
|
$so->name = $system;
|
|
|
|
}
|
2021-08-21 11:15:22 +00:00
|
|
|
}
|
2021-08-28 03:36:42 +00:00
|
|
|
|
|
|
|
// We'll search and see if we already have that system
|
|
|
|
} else {
|
2021-09-11 08:00:39 +00:00
|
|
|
$so = System::where('mailer_address',$address)
|
|
|
|
->where('mailer_port',$port)
|
|
|
|
->single();
|
|
|
|
|
|
|
|
if (! $so)
|
|
|
|
$so = System::where('name',$system)
|
|
|
|
->where('sysop',$sysop)
|
|
|
|
->firstOrNew();
|
2021-08-28 03:36:42 +00:00
|
|
|
|
|
|
|
if ($so->exists)
|
|
|
|
Log::debug(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::debug(sprintf('%s:New System [%s] with address [%d:%d/%d]',self::LOGKEY,$system,$zo->zone_id,$ao->host_id,$ao->node_id));
|
|
|
|
|
|
|
|
$so->name = $system;
|
|
|
|
$so->sysop = $sysop;
|
|
|
|
$so->active = TRUE;
|
|
|
|
|
|
|
|
if (! $so->exists)
|
|
|
|
$so->notes = sprintf('Created by Nodelist Import: %d',$no->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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];
|
|
|
|
|
|
|
|
$so->baud = $fields[6];
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ($method && ($ao->role != DomainController::NODE_PVT)) {
|
|
|
|
$so->mailer_type = $method;
|
|
|
|
$so->mailer_address = $address;
|
|
|
|
$so->mailer_port = $port;
|
2021-08-21 11:15:22 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 11:31:57 +00:00
|
|
|
// Save the system record
|
|
|
|
$so->save();
|
|
|
|
|
2021-06-26 00:34:49 +00:00
|
|
|
// If our zone didnt exist, we'll create it with this system
|
|
|
|
if (! $zo->exists) {
|
|
|
|
$zo->system_id = $so->id;
|
|
|
|
$zo->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$ao->zone_id = $zo->id;
|
|
|
|
|
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);
|
2021-07-26 11:21:58 +00:00
|
|
|
if ($ao->role == DomainController::NODE_HC)
|
2021-06-25 11:31:57 +00:00
|
|
|
$hub_id = $ao->id;
|
|
|
|
|
2021-08-21 11:15:22 +00:00
|
|
|
$no->addresses()->attach($ao,['role'=>$ao->role]);
|
2021-06-25 11:31:57 +00:00
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error(sprintf('%s:Error with line [%s] (%s)',self::LOGKEY,$line,$e->getMessage()),['fields'=>$fields]);
|
|
|
|
throw new \Exception($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! ($c % 100)) {
|
|
|
|
Log::notice(sprintf('%s:Processed [%s] records',self::LOGKEY,$c),['memory'=>memory_get_usage(TRUE)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 11:15:22 +00:00
|
|
|
// 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();
|
|
|
|
|
2021-06-25 11:31:57 +00:00
|
|
|
fclose($fh);
|
|
|
|
|
2021-08-21 11:15:22 +00:00
|
|
|
if ($this->delete_file and $c)
|
2021-06-25 11:31:57 +00:00
|
|
|
unlink($file);
|
|
|
|
|
2021-06-26 00:34:49 +00:00
|
|
|
Log::info(sprintf('%s:Updated %d records from %d systems',self::LOGKEY,$p,$c));
|
2021-06-25 11:31:57 +00:00
|
|
|
}
|
|
|
|
}
|