clrghouz/app/Classes/FTN/Tic.php

334 lines
8.6 KiB
PHP
Raw Normal View History

2022-11-01 11:24:36 +00:00
<?php
namespace App\Classes\FTN;
use Carbon\Carbon;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
2022-11-01 11:24:36 +00:00
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\UnableToReadFile;
2022-11-01 11:24:36 +00:00
use App\Classes\FTN as FTNBase;
2023-09-15 12:57:32 +00:00
use App\Models\{Address,File,Filearea,Setup,System};
2022-11-01 11:24:36 +00:00
use App\Traits\EncodeUTF8;
/**
* Class TIC
* Used create the structure of TIC files
2022-11-01 11:24:36 +00:00
*
* @package App\Classes
*/
class Tic extends FTNBase
{
use EncodeUTF8;
private const LOGKEY = 'FT-';
2022-11-01 11:24:36 +00:00
private const cast_utf8 = [
];
// Single value kludge items and whether they are required
// http://ftsc.org/docs/fts-5006.001
private array $_kludge = [
'AREA' => TRUE,
'areadesc' => FALSE,
'ORIGIN' => TRUE,
'FROM' => TRUE,
'to' => FALSE,
'FILE' => TRUE, // 8.3 DOS format
'lfile' => FALSE, // alias fullname
'fullname' => FALSE,
'size' => FALSE,
'date' => FALSE, // File creation date
'desc' => FALSE, // One line description of file
'ldesc' => FALSE, // Can have multiple
'created' => FALSE,
'magic' => FALSE,
'replaces' => FALSE, // ? and * are wildcards, as per DOS
'CRC' => TRUE, // crc-32
'PATH' => TRUE, // can have multiple: [FTN] [unix timestamp] [datetime human readable] [signature]
'SEENBY' => TRUE,
'pw' => FALSE, // Password
];
private File $fo;
2022-11-01 11:24:36 +00:00
private Filearea $area;
private Collection $values;
2022-11-01 11:24:36 +00:00
private Address $origin; // Should be first address in Path
private Address $from; // Should be last address in Path
private Address $to; // Should be me
public function __construct()
{
$this->fo = new File;
$this->fo->kludges = collect();
$this->fo->set_path = collect();
$this->fo->set_seenby = collect();
$this->fo->rogue_seenby = collect();
$this->values = collect();
}
2023-09-05 09:57:34 +00:00
public function __get(string $key): mixed
{
switch ($key) {
case 'fo':
return $this->{$key};
default:
return parent::__get($key);
}
}
/**
* Generate a TIC file for an address
*
2023-09-05 09:57:34 +00:00
* @param Address $ao
* @param File $fo
* @return string
*/
public static function generate(Address $ao,File $fo): string
{
$sysaddress = Setup::findOrFail(config('app.id'))->system->match($ao->zone)->first();
$result = collect();
// Origin is the first address in our path
$result->put('ORIGIN',$fo->path->first()->ftn3d);
$result->put('FROM',$sysaddress->ftn3d);
$result->put('TO',$ao->ftn3d);
$result->put('FILE',$fo->name);
$result->put('SIZE',$fo->size);
if ($fo->description)
$result->put('DESC',$fo->description);
if ($fo->replaces)
$result->put('REPLACES',$fo->replaces);
$result->put('AREA',$fo->filearea->name);
$result->put('AREADESC',$fo->filearea->description);
if ($x=$ao->session('ticpass'))
$result->put('PW',$x);
$result->put('CRC',sprintf("%X",$fo->crc));
$out = '';
foreach ($result as $key=>$value)
$out .= sprintf("%s %s\r\n",$key,$value);
foreach ($fo->path as $o)
$out .= sprintf("PATH %s %s %s\r\n",$o->ftn3d,$o->pivot->datetime,$o->pivot->extra);
// Add ourself to the path:
$out .= sprintf("PATH %s %s\r\n",$sysaddress->ftn3d,Carbon::now());
foreach ($fo->seenby as $o)
$out .= sprintf("SEENBY %s\r\n",$o->ftn3d);
$out .= sprintf("SEENBY %s\r\n",$sysaddress->ftn3d);
return $out;
}
2022-11-01 11:24:36 +00:00
/**
* Does this TIC file bring us a nodelist
*
* @return bool
*/
public function isNodelist(): bool
{
Log::critical(sprintf('%s:D fo_nodelist_file_area [%d], fo_filearea_domain_filearea_id [%d], regex [%s] name [%s]',
self::LOGKEY,
$this->fo->nodelist_filearea_id,
$this->fo->filearea->domain->filearea_id,
str_replace(['.','?'],['\.','.'],'#^'.$this->fo->filearea->domain->nodelist_filename.'$#i'),
$this->fo->name,
));
return (($this->fo->nodelist_filearea_id === $this->fo->filearea->domain->filearea_id)
&& (preg_match(str_replace(['.','?'],['\.','.'],'#^'.$this->fo->filearea->domain->nodelist_filename.'$#i'),$this->fo->name)));
}
/**
* Load a TIC file from an existing filename
*
* @param string $filename Relative to filesystem
* @return void
* @throws FileNotFoundException
*/
public function load(string $filename): void
{
Log::info(sprintf('%s:+ Processing TIC file [%s]',self::LOGKEY,$filename));
$fs = Storage::disk(config('fido.local_disk'));
2022-11-01 11:24:36 +00:00
if (str_contains($filename,'-')) {
list($hex,$name) = explode('-',$filename);
$hex = basename($hex);
} else {
$hex = '';
}
2022-11-01 11:24:36 +00:00
if (! $fs->exists($filename))
throw new FileNotFoundException(sprintf('File [%s] doesnt exist',$fs->path($filename)));
2022-11-01 11:24:36 +00:00
if (! is_readable($fs->path($filename)))
throw new UnableToReadFile(sprintf('File [%s] is not readable',realpath($filename)));
2022-11-01 11:24:36 +00:00
$f = $fs->readStream($filename);
2022-11-01 11:24:36 +00:00
if (! $f) {
Log::error(sprintf('%s:! Unable to open file [%s] for reading',self::LOGKEY,$filename));
2022-11-01 11:24:36 +00:00
return;
}
$ldesc = '';
2022-11-01 11:24:36 +00:00
while (! feof($f)) {
$line = chop(fgets($f));
$matches = [];
if (! $line)
continue;
preg_match('/([a-zA-Z]+)\ ?(.*)?/',$line,$matches);
2022-11-01 11:24:36 +00:00
if (in_array(strtolower(Arr::get($matches,1,'-')),$this->_kludge)) {
2022-11-01 11:24:36 +00:00
switch ($k=strtolower($matches[1])) {
case 'area':
$this->{$k} = Filearea::singleOrNew(['name'=>strtoupper($matches[2])]);
2022-11-01 11:24:36 +00:00
break;
case 'origin':
case 'from':
case 'to':
$this->{$k} = Address::findFTN($matches[2]);
if (! $this->{$k})
Log::alert(sprintf('%s:! Unable to find an FTN for [%s] for the (%s)',self::LOGKEY,$matches[2],$k));
2022-11-01 11:24:36 +00:00
break;
case 'file':
$this->fo->name = $matches[2];
$this->fo->prefix = $hex;
if (! $fs->exists($this->fo->recvd_rel_name)) {
// @todo Fail this, so that it is rescheduled to try again in 1-24hrs.
throw new FileNotFoundException(sprintf('File not found? [%s]',$fs->path($this->fo->recvd_rel_name)));
}
2022-11-01 11:24:36 +00:00
break;
case 'areadesc':
$areadesc = $matches[2];
break;
2022-11-01 11:24:36 +00:00
case 'created':
// ignored
2022-11-01 11:24:36 +00:00
break;
case 'pw':
$pw = $matches[2];
2023-09-05 09:57:34 +00:00
break;
case 'lfile':
$this->fo->lname = $matches[2];
2023-09-05 09:57:34 +00:00
break;
case 'desc':
2022-11-01 11:24:36 +00:00
case 'magic':
case 'replaces':
case 'size':
$this->fo->{$k} = $matches[2];
2022-11-01 11:24:36 +00:00
break;
case 'fullname':
$this->fo->lfile = $matches[2];
2022-11-01 11:24:36 +00:00
break;
case 'date':
$this->fo->datetime = Carbon::createFromTimestamp($matches[2]);
2022-11-01 11:24:36 +00:00
break;
case 'ldesc':
$ldesc .= ($ldesc ? "\r" : '').$matches[2];
2022-11-01 11:24:36 +00:00
break;
case 'crc':
$this->fo->{$k} = hexdec($matches[2]);
2022-11-01 11:24:36 +00:00
break;
case 'path':
$this->fo->set_path->push($matches[2]);
2022-11-01 11:24:36 +00:00
break;
case 'seenby':
$this->fo->set_seenby->push($matches[2]);
2022-11-01 11:24:36 +00:00
break;
}
} else {
$this->fo->kludges->push($line);
2022-11-01 11:24:36 +00:00
}
}
if ($ldesc)
$this->fo->ldesc = $ldesc;
2022-11-01 11:24:36 +00:00
fclose($f);
2023-07-29 03:17:59 +00:00
// @todo Add notifictions back to the system
if ($this->fo->replaces && (! preg_match('/^'.$this->fo->replaces.'$/',$this->fo->name))) {
Log::alert(sprintf('%s:! Regex [%s] doesnt match file name [%s]',self::LOGKEY,$this->fo->replaces,$this->fo->name));
$this->fo->replaces = NULL;
}
2022-11-01 11:24:36 +00:00
// Validate Size
if ($this->fo->size !== ($y=$fs->size($this->fo->recvd_rel_name)))
throw new \Exception(sprintf('TIC file size [%d] doesnt match file [%s] (%d)',$this->fo->size,$this->fo->recvd_rel_name,$y));
2022-11-01 11:24:36 +00:00
// Validate CRC
if (sprintf('%08x',$this->fo->crc) !== ($y=$fs->checksum($this->fo->recvd_rel_name,['checksum_algo'=>'crc32b'])))
throw new \Exception(sprintf('TIC file CRC [%08x] doesnt match file [%s] (%s)',$this->fo->crc,$this->fo->recvd_rel_name,$y));
2022-11-01 11:24:36 +00:00
// Validate Password
if ($pw !== ($y=$this->from->session('ticpass')))
throw new \Exception(sprintf('TIC file PASSWORD [%s] doesnt match system [%s] (%s)',$pw,$this->from->ftn,$y));
2022-11-01 11:24:36 +00:00
// Validate Sender is linked (and permitted to send)
if ($this->from->fileareas->search(function($item) { return $item->id === $this->area->id; }) === FALSE)
throw new \Exception(sprintf('Node [%s] is not subscribed to [%s]',$this->from->ftn,$this->area->name));
// If the filearea is to be autocreated, create it
if (! $this->area->exists) {
$this->area->description = $areadesc;
2022-11-01 11:24:36 +00:00
$this->area->active = TRUE;
$this->area->show = FALSE;
2022-11-01 11:24:36 +00:00
$this->area->notes = 'Autocreated';
$this->area->domain_id = $this->from->zone->domain_id;
$this->area->save();
}
$this->fo->filearea_id = $this->area->id;
$this->fo->fftn_id = $this->origin->id;
2022-11-01 11:24:36 +00:00
// If the file create time is blank, we'll take the files
if (! $this->fo->datetime)
$this->fo->datetime = Carbon::createFromTimestamp($fs->lastModified($this->fo->recvd_rel_name));
2022-11-01 11:24:36 +00:00
$this->fo->save();
2022-11-01 11:24:36 +00:00
}
}