Work with netmail creation

This commit is contained in:
Deon George
2019-05-11 11:17:56 +10:00
parent 188fd1a2cf
commit 9317f78a3a
6 changed files with 389 additions and 129 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Classes;
use App\Exceptions\InvalidFidoPacketException;
use Carbon\Carbon;
class FTNPacket extends FTN
{
@@ -11,18 +12,141 @@ class FTNPacket extends FTN
private $pktver = NULL;
public $date = NULL;
private $baud = NULL;
private $software = [];
private $cap = [];
private $proddata = NULL;
private $password = NULL;
private $sz = NULL;
private $dz = NULL;
private $sn = NULL;
private $dn = NULL;
private $sf = NULL;
private $df = NULL;
private $sp = NULL;
private $dp = NULL;
public $filename = NULL;
public $messages = [];
public $messages = NULL;
public function __construct(string $file)
// First part of header
private $pack1 = [
'onode'=>[0x00,'v',2],
'dnode'=>[0x02,'v',2],
'y'=>[0x04,'v',2],
'm'=>[0x06,'v',2],
'd'=>[0x08,'v',2],
'H'=>[0x0a,'v',2],
'M'=>[0x0c,'v',2],
'S'=>[0x0e,'v',2],
'baud'=>[0x10,'v',2],
'pktver'=>[0x12,'v',2],
'onet'=>[0x14,'v',2],
'dnet'=>[0x16,'v',2],
'prodcode-lo'=>[0x18,'C',1],
'prodrev-maj'=>[0x19,'C',1],
];
// Second part of header
private $pack2 = [
'qozone'=>[0x22,'v',2],
'qdzone'=>[0x24,'v',2],
'filler'=>[0x26,'v',2],
'capvalid'=>[0x28,'v',2],
'prodcode-hi'=>[0x2a,'C',1],
'prodrev-min'=>[0x2b,'C',1],
'capword'=>[0x2c,'v',1],
'ozone'=>[0x2e,'v',2],
'dzone'=>[0x30,'v',2],
'opoint'=>[0x32,'v',2],
'dpoint'=>[0x34,'v',2],
];
public function __construct(string $file=NULL)
{
$this->filename = $file;
$this->messages = collect();
// @todo - is this appropriate to set here
$this->date = now();
$this->software['prodcode-lo'] = 0;
$this->software['prodcode-hi'] = 1;
$this->software['rev-maj'] = 2;
$this->software['rev-min'] = 3;
$this->cap['valid'] = 0; // @todo this is wrong
$this->cap['word'] = 0; // @todo this is wrong
$this->pktver = 0x02;
if ($file) {
$this->filename = $file;
if ($file)
return $this->OpenFile($file);
}
}
public function __toString(): string
{
$return = $this->createHeader();
foreach ($this->messages as $o)
$return .= "\02\00".(string)$o;
$return .= "\00\00";
return $return;
}
/**
* Create our message packet header
*/
private function createHeader(): string
{
try {
$a = pack(join('',collect($this->pack1)->pluck(1)->toArray()),
$this->sf,
$this->df,
$this->date->year,
$this->date->month,
$this->date->day,
$this->date->hour,
$this->date->minute,
$this->date->second,
$this->baud,
$this->pktver,
$this->sn,
$this->dn,
$this->software['prodcode-lo'], // @todo change to this software
$this->software['rev-maj'] // @todo change to this software
);
$b = pack(join('',collect($this->pack2)->pluck(1)->toArray()),
$this->sz,
$this->dz,
0x00, // Baud
$this->cap['valid'], // @todo to check
$this->software['prodcode-hi'], // @todo change to this software
$this->software['rev-min'], // @todo change to this software
$this->cap['word'], // @todo to check
$this->sz,
$this->dz,
$this->sp,
$this->dp
);
return $a.pack('a8',$this->password).$b."mbse"; // @todo change to this software
} catch (\Exception $e) {
return $e->getMessage();
}
}
public function addMessage(FTNMessage $o)
{
$this->messages->push($o);
}
public function dump()
{
return hex_dump((string)$this);
}
/**
@@ -45,10 +169,9 @@ class FTNPacket extends FTN
// Not a type 2 packet
if (array_get(unpack('vv',substr($header,0x12)),'v') != 2)
throw new InvalidFidoPacketException('Not a type 2 packet:'. $file);
throw new InvalidFidoPacketException('Not a type 2 packet in file: '. $file);
$this->setHeader($header);
$this->messages = collect();
$this->parseHeader($header);
while (! feof($f))
{
@@ -69,11 +192,12 @@ class FTNPacket extends FTN
break;
$message = new FTNMessage(fread($f,0xc));
$message->date = $this->readnullfield($f);
$message->date = Carbon::createFromFormat('d M y H:i:s',$this->readnullfield($f));
$message->to = $this->readnullfield($f);
$message->from = $this->readnullfield($f);
$message->subject = $this->readnullfield($f);
$message->message = $this->readnullfield($f);
$message->parsemessage($this->readnullfield($f));
$this->messages->push($message);
}
@@ -91,41 +215,12 @@ class FTNPacket extends FTN
return $result;
}
public function setHeader(string $header)
private function parseHeader(string $header)
{
$pack1 = [
'onode'=>[0x00,'v',2],
'dnode'=>[0x02,'v',2],
'y'=>[0x04,'v',2],
'm'=>[0x06,'v',2],
'd'=>[0x08,'v',2],
'H'=>[0x0a,'v',2],
'M'=>[0x0c,'v',2],
'S'=>[0x0e,'v',2],
'baud'=>[0x10,'v',2],
'pktver'=>[0x12,'v',2],
'onet'=>[0x14,'v',2],
'dnet'=>[0x16,'v',2],
'prodcode-lo'=>[0x18,'C',1],
'prodrev-maj'=>[0x19,'C',1],
];
$pack2 = [
'qozone'=>[0x22,'v',2],
'qdzone'=>[0x24,'v',2],
'filler'=>[0x26,'v',2],
'capvalid'=>[0x28,'v',2],
'prodcode-hi'=>[0x2a,'C',1],
'prodrev-min'=>[0x2b,'C',1],
'capword'=>[0x2c,'v',1],
'ozone'=>[0x2e,'v',2],
'dzone'=>[0x30,'v',2],
'opoint'=>[0x32,'v',2],
'dpoint'=>[0x34,'v',2],
];
$result1 = unpack($this->unpackheader($pack1),substr($header,0,0x1a));
$result2 = unpack($this->unpackheader($pack2),substr($header,0x22,0x14));
$result1 = unpack($this->unpackheader($this->pack1),substr($header,0,0x1a));
$this->password = array_get(unpack('a*p',substr($header,0x1a,8)),'p');
$result2 = unpack($this->unpackheader($this->pack2),substr($header,0x22,0x14));
$this->proddata = array_get(unpack('A*p',substr($header,0x36,4)),'p');
$this->sz = array_get($result2,'ozone');
$this->sn = array_get($result1,'onet');
@@ -149,7 +244,7 @@ class FTNPacket extends FTN
$this->dp
);
$this->date = sprintf ('%d-%d-%d %d:%d:%d',
$this->date = Carbon::create(
array_get($result1,'y'),
array_get($result1,'m'),
array_get($result1,'d'),
@@ -160,8 +255,12 @@ class FTNPacket extends FTN
$this->baud = array_get($result1,'baud');
$this->pktver = array_get($result1,'pktver');
$this->password = array_get(unpack('A*p',substr($header,0x1a,8)),'p');
$this->proddata = array_get(unpack('A*p',substr($header,0x36,4)),'p');
$this->software['prodcode-lo'] = array_get($result1,'prodcode-lo');
$this->software['prodcode-hi'] = array_get($result2,'prodcode-hi');
$this->software['rev-maj'] = array_get($result1,'prodrev-maj');
$this->software['rev-min'] = array_get($result2,'prodrev-min');
$this->cap['valid'] = array_get($result2,'capvalid');
$this->cap['word'] = array_get($result2,'capword');
// @todo filler
}
}