Optimise our address FTN regex

This commit is contained in:
Deon George 2023-11-23 12:18:20 +11:00
parent e5de4970d1
commit a13028808a

View File

@ -23,7 +23,7 @@ class Address extends Model
protected $with = ['zone']; protected $with = ['zone'];
// http://ftsc.org/docs/frl-1028.002 // 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_ZC = 1<<0; // Zone
public const NODE_RC = 1<<1; // Region public const NODE_RC = 1<<1; // Region
@ -659,6 +659,8 @@ class Address extends Model
* @param bool $update * @param bool $update
* @param Collection|null $echomail * @param Collection|null $echomail
* @return Packet|null * @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 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 public static function parseFTN(string $ftn): array
{ {
if (! preg_match(sprintf('#^%s$#',self::ftn_regex),strtolower($ftn),$matches)) 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. // 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)) 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))) if ((! empty($matches[4])) AND ((! is_numeric($matches[$i])) || ($matches[4] > DomainController::NUMBER_MAX)))
throw new InvalidFTNException(sprintf('Invalid FTN: %s - point address invalid',$ftn)); throw new InvalidFTNException(sprintf('Invalid FTN: [%s] - point address invalid [%d]',$ftn,$matches[4]));
return [ return [
'z'=>(int)$matches[1], 'z'=>(int)$matches[1],
'n'=>(int)$matches[2], 'n'=>(int)$matches[2],
'f'=>(int)$matches[3], 'f'=>(int)$matches[3],
'p'=>isset($matches[5]) && $matches[5] ? (int)$matches[5] : 0, 'p'=>empty($matches[4]) ? 0 : (int)$matches[4],
'd'=>$matches[7] ?? NULL 'd'=>$matches[5] ?? NULL
]; ];
} }