Code improvement to our_address(), reducing arguments

This commit is contained in:
Deon George 2024-04-21 21:40:55 +10:00
parent 1c270025cf
commit bba6f93fbc
9 changed files with 39 additions and 20 deletions

View File

@ -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";

View File

@ -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();

View File

@ -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) {

View File

@ -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));

View File

@ -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,

View File

@ -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);

View File

@ -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;

View File

@ -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());
}

View File

@ -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));
}
}
/**