Process packet seenby/path/via lines when saving echomail/netmail

This commit is contained in:
2023-09-20 20:29:23 +10:00
parent 7fedf88d8c
commit 612efda945
11 changed files with 337 additions and 230 deletions

View File

@@ -11,8 +11,7 @@ use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Validator as ValidatorResult;
use App\Classes\FTN as FTNBase;
use App\Http\Controllers\DomainController;
use App\Models\{Address,Domain,System,Zone};
use App\Models\{Address,Domain,Zone};
use App\Rules\{TwoByteInteger,TwoByteIntegerWithZero};
use App\Traits\EncodeUTF8;
@@ -138,10 +137,7 @@ class Message extends FTNBase
private Collection $rescanned; // Message was created as a result of a rescan
private Collection $path; // FTS-0004.001 The message PATH lines
private Collection $pathaddress; // Collection of Addresses after parsing seenby
private Collection $rogue_seenby; // Collection of FTNs in the Seen-by that are not defined
private Collection $seenby; // FTS-0004.001 The message SEEN-BY lines
private Collection $seenaddress; // Collection of Addresses after parsing seenby
private Collection $via; // The path the message has gone using Via lines (Netmail)
private Collection $unknown; // Temporarily hold attributes we have no logic for.
@@ -224,10 +220,7 @@ class Message extends FTNBase
$this->kludge = collect();
$this->rescanned = collect();
$this->path = collect();
$this->rogue_seenby = collect();
$this->seenby = collect();
$this->seenaddress = collect();
$this->pathaddress = collect();
$this->via = collect();
$this->unknown = collect();
}
@@ -364,9 +357,6 @@ class Message extends FTNBase
case 'rescanned':
case 'path':
case 'seenby':
case 'pathaddress':
case 'rogue_seenby':
case 'seenaddress':
case 'unknown':
case 'via':
@@ -556,6 +546,7 @@ class Message extends FTNBase
}
$ptr = 0;
// To User
$o->user_to = strstr(substr($msg,self::HEADER_LEN+$ptr),"\x00",TRUE);
$ptr += strlen($o->user_to)+1;
@@ -670,77 +661,6 @@ class Message extends FTNBase
return ($this->flags & $flag);
}
/**
* Parse the Seenby/path lines and return a collection of addresses
*
* @param string $type Type of address, ie: seenby/path
* @param Collection $addresses
* @param Collection $rogue
* @return Collection
* @throws \Exception
*/
private function parseAddresses(string $type,Collection $addresses,Collection &$rogue): Collection
{
$nodes = collect();
$net = NULL;
foreach ($addresses as $line) {
foreach (explode(' ',$line) as $item) {
if (($x=strpos($item,'/')) !== FALSE) {
$net = (int)substr($item,0,$x);
$node = (int)substr($item,$x+1);
} else {
$node = (int)$item;
}
$aoid = NULL;
// If domain should be flattened, look for node regardless of zone (within the list of zones for the domain)
if ($this->fdomain && $this->fdomain->flatten) {
$ao = Address::findZone($this->fdomain,$net&DomainController::NUMBER_MAX,$node&DomainController::NUMBER_MAX,0);
$ftn = sprintf('%d:%d/%d@%s',$this->fz,$net&DomainController::NUMBER_MAX,$node&DomainController::NUMBER_MAX,$this->fdomain->name);
$aoid = $ao?->id;
} elseif ($this->fdomain) {
$ftn = sprintf('%d:%d/%d@%s',$this->fz,$net&DomainController::NUMBER_MAX,$node&DomainController::NUMBER_MAX,$this->fdomain->name);
} else {
$ftn = sprintf('%d:%d/%d',$this->fz,$net&DomainController::NUMBER_MAX,$node&DomainController::NUMBER_MAX);
}
if (! $aoid) {
if (! ($ao=Address::findFTN($ftn))) {
Log::alert(sprintf('%s:! Undefined Node [%s] in [%s] - auto created.',self::LOGKEY,$ftn,$type));
$ao = Address::createFTN($ftn,System::createUnknownSystem());
}
$aoid = $ao?->id;
}
switch ($type) {
case 'seenby':
if (! $ao) {
$rogue->push(sprintf('%d:%d/%d',$this->fz,$net&DomainController::NUMBER_MAX,$node&DomainController::NUMBER_MAX));
continue 2;
}
case 'path':
if (! $aoid)
throw new \Exception(sprintf('Didnt get an address for [%s]',$ftn));
break;
}
$nodes->push($aoid);
}
}
return $nodes;
}
/**
* Process the data after the ORIGIN
* There may be kludge lines after the origin - notably SEEN-BY
@@ -785,48 +705,6 @@ class Message extends FTNBase
}
}
/**
* Parse the via address and return a collection of addresses
*
* <FTN Address> @YYYYMMDD.HHMMSS[.Precise][.Time Zone] <Program Name> <Version> [Serial Number]
*
* @param Collection $via
* @return Collection
* @throws \Exception
*/
private function parseVia(Collection $via): Collection
{
$nodes = collect();
foreach ($via as $line) {
$m = [];
if (preg_match('/^([0-9]+:[0-9]+\/[0-9]+(\..*)?)\s+@([0-9.a-zA-Z]+)\s+(.*)$/',$line,$m)) {
// Address
$ao = Address::findFTN($m[1]);
// Time
$t = [];
$datetime = '';
if (! preg_match('/^([0-9]+\.[0-9]+)(\.?(.*))?$/',$m[3],$t))
Log::alert(sprintf('%s:! Unable to determine time from [%s]',self::LOGKEY,$m[3]));
else
$datetime = Carbon::createFromFormat('Ymd.His',$t[1],$t[3] ?? '');
if (! $ao) {
Log::alert(sprintf('%s:! Undefined Node [%s] for Netmail.',self::LOGKEY,$m[1]));
//$rogue->push(['node'=>$m[1],'datetime'=>$datetime,'program'=>$m[4]]);
} else {
$nodes->push(['node'=>$ao,'datetime'=>$datetime,'program'=>$m[4]]);
}
}
}
return $nodes;
}
/**
* Extract information out of the message text.
*
@@ -1009,19 +887,6 @@ class Message extends FTNBase
} elseif ($this->zone) {
$this->src = Address::parseFTN(sprintf('%d:%d/%d.%d@%s',$this->zone->zone_id,$this->fn,$this->ff,$this->fp,$this->zone->domain->name));
}
// Parse SEEN-BY
if ($this->seenby->count())
$this->seenaddress = $this->parseAddresses('seenby',$this->seenby,$this->rogue_seenby);
$dummy = collect();
// Parse PATH
if ($this->path->count())
$this->pathaddress = $this->parseAddresses('path',$this->path,$dummy);
// Parse VIA
if ($this->via->count())
$this->pathaddress = $this->parseVia($this->via);
}
/**
@@ -1061,6 +926,15 @@ class Message extends FTNBase
]);
$validator->after(function($validator) {
if ($this->zone->domain->flatten) {
if (! $this->zone->domain->zones->pluck('zone_id')->contains($this->fz))
$validator->errors()->add('invalid-zone',sprintf('Message zone [%d] doesnt match any zone in domain for packet zone [%d].',$this->fz,$this->zone->zone_id));
} else {
if ($this->zone->zone_id !== $this->fz)
$validator->errors()->add('invalid-zone',sprintf('Message zone [%d] doesnt match packet zone [%d].',$this->fz,$this->zone->zone_id));
}
if (! $this->fboss_o)
$validator->errors()->add('from',sprintf('Undefined Node [%s] sent message.',$this->fboss));
if (! $this->tboss_o)