From bba6f93fbcc86b2bf4d58358395d7d8ea3a2910e Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 21 Apr 2024 21:40:55 +1000 Subject: [PATCH] Code improvement to our_address(), reducing arguments --- app/Classes/Dynamic/HubStats.php | 2 +- app/Classes/FTN/Tic.php | 2 +- app/Models/Address.php | 2 +- app/Models/Echomail.php | 2 +- app/Models/Netmail.php | 2 +- app/Notifications/Echomails.php | 2 +- app/Notifications/Netmails.php | 3 ++- app/Traits/MsgID.php | 2 +- app/helpers.php | 42 +++++++++++++++++++++++--------- 9 files changed, 39 insertions(+), 20 deletions(-) diff --git a/app/Classes/Dynamic/HubStats.php b/app/Classes/Dynamic/HubStats.php index baf68ce..5eb9a78 100644 --- a/app/Classes/Dynamic/HubStats.php +++ b/app/Classes/Dynamic/HubStats.php @@ -70,7 +70,7 @@ class HubStats extends Dynamic $header = "| %-12s | %4d | %3d | %3d | %16s | %5s | %5s |\r\n"; - $output = sprintf("Hub Status for [%s] as at [%s]\r\n",our_address($this->ao->zone->domain,$this->ao)->ftn,$date); + $output = sprintf("Hub Status for [%s] as at [%s]\r\n",our_address($this->ao)->ftn,$date); $output .= "\r"; $output .= "+--------------+------+-----+-----+------------------+-------+-------+\r\n"; $output .= "| FTN | ECHO | NET |FILES| LAST SESSION | MODE |AUTOHLD|\r\n"; diff --git a/app/Classes/FTN/Tic.php b/app/Classes/FTN/Tic.php index 51dacc5..abd56c7 100644 --- a/app/Classes/FTN/Tic.php +++ b/app/Classes/FTN/Tic.php @@ -90,7 +90,7 @@ class Tic extends FTNBase if (! $this->to) throw new \Exception('No to address defined'); - $sysaddress = our_address($this->to->zone->domain,$this->to); + $sysaddress = our_address($this->to); $result = collect(); diff --git a/app/Models/Address.php b/app/Models/Address.php index a205959..b5962fc 100644 --- a/app/Models/Address.php +++ b/app/Models/Address.php @@ -829,7 +829,7 @@ class Address extends Model public function getPacket(Collection $msgs,string $passwd=NULL): ?Packet { $s = Setup::findOrFail(config('app.id')); - $ao = our_address($this->zone->domain,$this); + $ao = our_address($this); // If we dont match on the address, we cannot pack mail for that system if (! $ao) { diff --git a/app/Models/Echomail.php b/app/Models/Echomail.php index 6e3ab46..83d092a 100644 --- a/app/Models/Echomail.php +++ b/app/Models/Echomail.php @@ -214,7 +214,7 @@ final class Echomail extends Model implements Packet { Log::info(sprintf('%s:+ Bundling [%s]',self::LOGKEY,$this->id)); - $sysaddress = our_address($this->fftn->zone->domain,$this->fftn); + $sysaddress = our_address($this->fftn); if (! $sysaddress) throw new \Exception(sprintf('%s:! We dont have an address in this network? (%s)',self::LOGKEY,$this->fftn->zone->domain->name)); diff --git a/app/Models/Netmail.php b/app/Models/Netmail.php index 313b33a..ddd4431 100644 --- a/app/Models/Netmail.php +++ b/app/Models/Netmail.php @@ -210,7 +210,7 @@ final class Netmail extends Model implements Packet // Add our address to the VIA line $via->push( sprintf('%s @%s.UTC %s %d.%d/%s %s', - our_address($this->fftn->zone->domain,$this->fftn)->ftn3d, + our_address($this->fftn)->ftn3d, Carbon::now()->utc()->format('Ymd.His'), str_replace(' ','_',Setup::PRODUCT_NAME), Setup::PRODUCT_VERSION_MAJ, diff --git a/app/Notifications/Echomails.php b/app/Notifications/Echomails.php index e5ba88d..58c1a07 100644 --- a/app/Notifications/Echomails.php +++ b/app/Notifications/Echomails.php @@ -60,7 +60,7 @@ abstract class Echomails extends Notification //implements ShouldQueue $o->datetime = Carbon::now(); $o->tzoffset = $o->datetime->utcOffset(); - $o->fftn_id = ($x=our_address($mo->fboss_o->zone->domain,$mo->fboss_o))->id; + $o->fftn_id = ($x=our_address($mo->fboss_o))->id; $o->flags = (Message::FLAG_LOCAL); $o->tearline = sprintf('%s (%04X)',Setup::PRODUCT_NAME,Setup::PRODUCT_ID); diff --git a/app/Notifications/Netmails.php b/app/Notifications/Netmails.php index 217cf5d..3276680 100644 --- a/app/Notifications/Netmails.php +++ b/app/Notifications/Netmails.php @@ -48,6 +48,7 @@ abstract class Netmails extends Notification //implements ShouldQueue protected function setupNetmail(object $notifiable): Netmail { + // @todo Redirect netmails to Hubs or higher to the admin $ao = $notifiable->routeNotificationFor(static::via); $o = new Netmail; @@ -57,7 +58,7 @@ abstract class Netmails extends Notification //implements ShouldQueue $o->datetime = Carbon::now(); $o->tzoffset = $o->datetime->utcOffset(); - $o->fftn_id = our_address($ao->zone->domain,$ao)->id; + $o->fftn_id = our_address($ao)->id; $o->tftn_id = $ao->id; $o->flags = (Message::FLAG_LOCAL|Message::FLAG_PRIVATE); $o->cost = 0; diff --git a/app/Traits/MsgID.php b/app/Traits/MsgID.php index 16f0609..b8464b3 100644 --- a/app/Traits/MsgID.php +++ b/app/Traits/MsgID.php @@ -16,7 +16,7 @@ trait MsgID { // Only create a MSGID for locally generated content if ((! $this->exists) && ($this->flags & Message::FLAG_LOCAL) && (is_null(Arr::get($this->attributes,'msgid')))) { - $ftn = our_address($this->fftn->zone->domain,$this->fftn); + $ftn = our_address($this->fftn); $this->attributes['msgid'] = sprintf('%s %08x',$ftn->ftn4d,timew()); } diff --git a/app/helpers.php b/app/helpers.php index fced2fe..ac24063 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -3,7 +3,7 @@ use Carbon\Carbon; use Illuminate\Support\Collection; -use App\Models\{Address,Domain,Setup,Zone}; +use App\Models\{Address,Domain,Setup}; /** * Calculate CCITT-CRC16 checksum @@ -83,13 +83,14 @@ if (! function_exists('hexstr')) { /** * Return our addresses. - * If zone provided, limit the list to those within the zone + * If domain provided, limit the list to those within the domain - returning a Collection::class + * If address provided, return our address that would be used for the provided address - return Address::class * - * @param Domain|NULL $do Limit the addresses for the specific domain - * @param Address|null $ao If address is presented, show the address we use when talking to that address + * @param Domain|Address|null $o - Domain or Address * @return Collection|Address|NULL + * @throws Exception */ -function our_address(Domain $do=NULL,Address $ao=NULL): Collection|Address|NULL +function our_address(Domain|Address $o=NULL): Collection|Address|NULL { static $so = NULL; static $our = NULL; @@ -102,15 +103,32 @@ function our_address(Domain $do=NULL,Address $ao=NULL): Collection|Address|NULL $our = $so->system->akas; } - $filter = $our; - if ($do) - $filter = $our->filter(function($item) use ($do) { return $item->zone->domain_id === $do->id; })->sortBy('role'); + // If we dont have any addresses + if ($our->count() === 0) + return NULL; - // If we are looking for a specific address, and there is only 1 result, return it, otherwise return what we have - if ($ao && config('fido.strict') && ($x=$filter->filter(function($item) use ($ao) { return $item->role <= $ao->role; })->sortBy('role'))->count()) - $filter = $x; + // We havent asked for an address/domain, so we'll return them all. + if (is_null($o)) + return $our; - return $ao ? $filter->last() : ($do ? $filter : $our); + // We are requesting a list of addresses for a Domain, or a specific Address, and we have more than 1 + switch (get_class($o)) { + case Address::class: + $filter = $our->filter(function($item) use ($o) { return $item->zone->domain_id === $o->zone->domain_id; })->sortBy('role'); + + // If we are looking for a specific address, and there is only 1 result, return it, otherwise return what we have + if (config('fido.strict') && ($x=$filter->filter(function($item) use ($o) { return $item->role <= $o->role; })->sortBy('role'))->count()) + $filter = $x; + + return $filter->last(); + + case Domain::class: + return $our->filter(function($item) use ($o) { return $item->zone->domain_id === $o->id; })->sortBy('role'); + + // We shouldnt get here + default: + throw new Exception('Unhandled class: '.get_class($o)); + } } /**