diff --git a/app/Console/Commands/NodelistImport.php b/app/Console/Commands/NodelistImport.php index 38e1abb..801e79a 100644 --- a/app/Console/Commands/NodelistImport.php +++ b/app/Console/Commands/NodelistImport.php @@ -36,13 +36,20 @@ class NodelistImport extends Command */ public function handle() { - return Job::dispatchSync( - is_numeric($x=$this->argument('file')) ? File::findOrFail($x) : $x, - $this->argument('domain'), - $this->option('delete'), - $this->option('unlink'), - $this->option('test'), - $this->option('ignorecrc'), - ); + try { + return Job::dispatchSync( + is_numeric($x=$this->argument('file')) + ? File::findOrFail($x) + : sprintf('%s/%s',config('fido.dir'),$this->argument('file')), + $this->argument('domain'), + $this->option('delete'), + $this->option('unlink'), + $this->option('test'), + $this->option('ignorecrc'), + ); + + } catch (\Exception $e) { + $this->error($e->getMessage()); + } } } \ No newline at end of file diff --git a/app/Jobs/NodelistImport.php b/app/Jobs/NodelistImport.php index d08b0a0..3852857 100644 --- a/app/Jobs/NodelistImport.php +++ b/app/Jobs/NodelistImport.php @@ -13,7 +13,7 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; -use App\Models\{Address, Domain, File, Mailer, Nodelist, Setup, System, Zone}; +use App\Models\{Address,Domain,File,Mailer,Nodelist,Setup,System,SystemZone,User,Zone}; use App\Traits\Import as ImportTrait; class NodelistImport implements ShouldQueue @@ -75,6 +75,26 @@ class NodelistImport implements ShouldQueue public function handle() { $us = Setup::findOrFail(config('app.id')); + $our_systems = SystemZone::select('system_id') + ->get() + ->pluck('system') + ->flatten() + ->pluck('addresses') + ->flatten() + ->filter(function($item) { return $item->active; }) + ->pluck('id'); + + $our_users = User::with(['systems.addresses']) + ->get() + ->pluck('systems') + ->flatten() + ->pluck('addresses') + ->flatten() + ->filter(function($item) { return $item->active; }) + ->pluck('id') + ->diff($our_systems); + + $our_addresses = $us->system->addresses->pluck('id'); // Get the file from the host $file = $this->getFileFromHost(self::importkey,$this->file); @@ -119,6 +139,11 @@ class NodelistImport implements ShouldQueue if ($this->delete_recs) $no->addresses()->detach(); + elseif ($no->addresses->count()) { + Log::error($x=sprintf('%s:! Nodelist [%s] for [%s] has existing records [%d]',self::LOGKEY,$date,$do->name,$no->addresses->count())); + + throw new \Exception($x); + } $p = $c = 0; @@ -244,8 +269,31 @@ class NodelistImport implements ShouldQueue $ao->role = $role; $ao->hub_id = $hub_id; - if ($ao->exists) - Log::debug(sprintf('%s:- Processing existing address [%s]',self::LOGKEY,$ao->ftn)); + 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 + $skip = FALSE; + if ($our_addresses->contains($ao->id)) { + Log::info(sprintf('%s:! Ignoring updating an address belonging to me',self::LOGKEY)); + $skip = TRUE; + } + + if ($our_systems->contains($ao->id)) { + Log::info(sprintf('%s:! Ignoring a system managed by this site',self::LOGKEY)); + $skip = TRUE; + } + + if ($our_users->contains($ao->id)) { + Log::info(sprintf('%s:! Ignoring a system managed by a user',self::LOGKEY)); + $skip = TRUE; + } + + if ($skip) { + $no->addresses()->attach($ao,['role'=>$ao->role]); + continue; + } + } $sysop = trim(str_replace('_',' ',$fields[4])); $system = trim(str_replace('_',' ',$fields[2])); @@ -376,22 +424,19 @@ class NodelistImport implements ShouldQueue 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; - // 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::alert(sprintf('%s:! Sysop Name changed for BBS [%s:%s] from [%s] to [%s]', - self::LOGKEY,$so->id,$so->name,$so->sysop,$sysop)); + // 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)); - $so->sysop = $sysop; + $so->sysop = $sysop; - // We have the same name has changed (except for ZC/RC addresses) - } elseif (($so->name !== $system) && (! ((Address::NODE_ZC|Address::NODE_RC|Address::NODE_NC) & $ao->role))) { - Log::alert(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 (except for ZC/RC addresses) + } elseif (($so->name !== $system) && (! ((Address::NODE_ZC|Address::NODE_RC|Address::NODE_NC) & $ao->role))) { + Log::alert(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 @@ -444,18 +489,6 @@ class NodelistImport implements ShouldQueue $so->baud = $fields[6]; */ - if ($so->getDirty()) { - if ($so->users->count()) { - Log::alert(sprintf('%s:! Refusing to update a system managed by a user',self::LOGKEY),['dirty'=>$so->getDirty()]); - continue; - } - - if ($so->sessions()->count()) { - Log::alert(sprintf('%s:! Refusing to update a system configured here',self::LOGKEY),['dirty'=>$so->getDirty()]); - continue; - } - } - // Save the system record try { $so->save(); @@ -505,16 +538,20 @@ class NodelistImport implements ShouldQueue } // Remove addresses not recorded; - $remove = $zo->addresses->except($us->system->addresses->pluck('id')->toArray())->diff($no->addresses); + $no->load('addresses'); + $remove = $zo->addresses->diff($no->addresses)->except($our_systems->toArray())->except($our_users->toArray()); Log::alert(sprintf('%s:%% Deleting [%d] addresses [%s]',self::LOGKEY,$remove->count(),$remove->pluck('ftn2d')->join(','))); Address::whereIN('id',$remove->pluck('id')->toArray())->update(['active'=>FALSE]); Address::whereIN('id',$remove->pluck('id')->toArray())->delete(); - if ((! $this->testmode) && ($this->ignore_crc || ($x=crc16(substr($tocrc,0,-3))) === $file_crc)) { + $crc = crc16(substr($tocrc,0,-3)); + + if ((! $this->testmode) && ($this->ignore_crc || ($crc === $file_crc))) { Log::info(sprintf('%s:= Committing nodelist',self::LOGKEY)); DB::commit(); + } else { - Log::error(sprintf('%s:! Rolling back nodelist, CRC doesnt match [%s](%s) or test mode',self::LOGKEY,$x,$file_crc)); + Log::error(sprintf('%s:! Rolling back nodelist, CRC doesnt match [%s](%s) or test mode',self::LOGKEY,$crc,$file_crc)); DB::rollBack(); }