From a13028808a2800208bf634cffe8ddea9313aaafe Mon Sep 17 00:00:00 2001 From: Deon George Date: Thu, 23 Nov 2023 12:18:20 +1100 Subject: [PATCH] Optimise our address FTN regex --- app/Models/Address.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/app/Models/Address.php b/app/Models/Address.php index bb377c1..6252b74 100644 --- a/app/Models/Address.php +++ b/app/Models/Address.php @@ -23,7 +23,7 @@ class Address extends Model protected $with = ['zone']; // http://ftsc.org/docs/frl-1028.002 - public const ftn_regex = '([0-9]+):([0-9]+)/([0-9]+)(\.([0-9]+))?(@([a-z0-9\-_~]{0,8}))?'; + public const ftn_regex = '(\d+):(\d+)/(\d+)(?:\.(\d+))?(?:@([a-z0-9\-_~]{0,8}))?'; public const NODE_ZC = 1<<0; // Zone public const NODE_RC = 1<<1; // Region @@ -659,6 +659,8 @@ class Address extends Model * @param bool $update * @param Collection|null $echomail * @return Packet|null + * @todo If we export to uplink hubs without our address in the seenby, they should send the message back to + * us with their seenby's. */ public function getEchomail(bool $update=TRUE,Collection $echomail=NULL): ?Packet { @@ -834,23 +836,22 @@ class Address extends Model public static function parseFTN(string $ftn): array { if (! preg_match(sprintf('#^%s$#',self::ftn_regex),strtolower($ftn),$matches)) - throw new InvalidFTNException(sprintf('Invalid FTN: %s - regex failed',$ftn)); + throw new InvalidFTNException(sprintf('Invalid FTN: [%s] - regex failed',serialize($ftn))); // Check our numbers are correct. - foreach ([1,2,3] as $i) { + foreach ([1,2,3] as $i) if ((! is_numeric($matches[$i])) || ($matches[$i] > DomainController::NUMBER_MAX)) - throw new InvalidFTNException(sprintf('Invalid FTN: %s - zone, host or node address invalid',$ftn)); - } + throw new InvalidFTNException(sprintf('Invalid FTN: [%s] - zone, host, or node address invalid [%d]',$ftn,$matches[$i])); - if (isset($matches[5]) AND ((! is_numeric($matches[$i])) || ($matches[5] > DomainController::NUMBER_MAX))) - throw new InvalidFTNException(sprintf('Invalid FTN: %s - point address invalid',$ftn)); + if ((! empty($matches[4])) AND ((! is_numeric($matches[$i])) || ($matches[4] > DomainController::NUMBER_MAX))) + throw new InvalidFTNException(sprintf('Invalid FTN: [%s] - point address invalid [%d]',$ftn,$matches[4])); return [ 'z'=>(int)$matches[1], 'n'=>(int)$matches[2], 'f'=>(int)$matches[3], - 'p'=>isset($matches[5]) && $matches[5] ? (int)$matches[5] : 0, - 'd'=>$matches[7] ?? NULL + 'p'=>empty($matches[4]) ? 0 : (int)$matches[4], + 'd'=>$matches[5] ?? NULL ]; }