Initial echomail import

This commit is contained in:
Deon George 2019-04-27 23:57:39 +10:00
parent 6515c91270
commit 9ba790e72c
14 changed files with 387 additions and 20 deletions

View File

@ -65,15 +65,28 @@ class FTNMessage extends FTN
$result = unpack($this->unpackheader($struct),$header);
$this->src = sprintf('%s/%s',array_get($result,'onet'),array_get($result,'onode'));
$this->dst = sprintf('%s/%s',array_get($result,'dnet'),array_get($result,'dnode'));
$this->fn = array_get($result,'onet');
$this->ff = array_get($result,'onode');
$this->src = sprintf('%s/%s',
$this->fn,
$this->ff
);
$this->tn = array_get($result,'dnet');
$this->tf = array_get($result,'dnode');
$this->dst = sprintf('%s/%s',
$this->tn,
$this->tf
);
$this->flags = array_get($result,'flags');
$this->cost = array_get($result,'cost');
}
public function __get($k)
{
return $this->{$k};
return isset($this->{$k}) ? $this->{$k} : NULL;
}
public function __set($k,$v)
@ -130,7 +143,7 @@ class FTNMessage extends FTN
}
foreach ($this->_kludge as $a => $b) {
if ($t = $this->kludge($b, $v)) {
if ($t = $this->kludge($b,$v)) {
$this->kludge->put($a,$t);
break;
}

View File

@ -127,18 +127,26 @@ class FTNPacket extends FTN
$result1 = unpack($this->unpackheader($pack1),substr($header,0,0x1a));
$result2 = unpack($this->unpackheader($pack2),substr($header,0x22,0x14));
$this->sz = array_get($result2,'ozone');
$this->sn = array_get($result1,'onet');
$this->sf = array_get($result1,'onode');
$this->sp = array_get($result2,'dpoint');
$this->pktsrc = sprintf('%s:%s/%s.%s',
array_get($result2,'ozone'),
array_get($result1,'onet'),
array_get($result1,'onode'),
array_get($result2,'dpoint')
$this->sz,
$this->sn,
$this->sf,
$this->sp
);
$this->dz = array_get($result2,'dzone');
$this->dn = array_get($result1,'dnet');
$this->df = array_get($result1,'dnode');
$this->dp = array_get($result2,'dpoint');
$this->pktdst = sprintf('%s:%s/%s.%s',
array_get($result2,'dzone'),
array_get($result1,'dnet'),
array_get($result1,'dnode'),
array_get($result2,'dpoint')
$this->dz,
$this->dn,
$this->df,
$this->dp
);
$this->date = sprintf ('%d-%d-%d %d:%d:%d',

View File

@ -5,14 +5,14 @@ namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\{Flag,Node,Zone};
class NodelistImport extends Command
class ImportNodelist extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'nodelist:import {file : Nodelist File}';
protected $signature = 'import:nodelist {file : Nodelist File}';
/**
* The console command description.

View File

@ -0,0 +1,105 @@
<?php
namespace App\Console\Commands;
use Carbon\Carbon;
use Illuminate\Console\Command;
use App\Traits\{GetNode,ParseNodes};
use App\Classes\FTNPacket;
use App\Models\{Echomail,Kludge,Seenby,Zone};
class ImportPacket extends Command
{
use GetNode,ParseNodes;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:pkt {file : Packet File}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import Mail Packet';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$pkt = new FTNPacket($this->argument('file'));
foreach ($pkt->messages as $o)
{
$eo = new Echomail;
$eo->pkt_from = $this->get_node($pkt->sz,$pkt->sn,$pkt->sf,$pkt->sp)->id;
$eo->pkt_to = $this->get_node($pkt->dz,$pkt->dn,$pkt->df,$pkt->dp)->id;
$eo->pkt = $pkt->filename;
$eo->pkt_date = $pkt->date;
$eo->flags = $o->flags;
$eo->cost = $o->cost;
$eo->from_user = $o->from;
$eo->from_ftn = $this->get_node($pkt->sz,$o->fn,$o->ff,$pkt->sp)->id;
$eo->to_user = $o->to;
$eo->subject = $o->subject;
$eo->date = Carbon::createFromFormat('d M y H:i:s',$o->date,($o->tzutc >= 0 ? '+' : '').substr_replace($o->tzutc,':',2,0));
$eo->tz = $o->tzutc;
$eo->area = $o->echoarea;
$eo->msgid = $o->msgid;
$eo->replyid = $o->replyid;
$eo->message = $o->message;
$eo->origin = $o->origin;
//$eo->original = (string)$o;
$eo->save();
foreach ($o->kludge as $k=>$v)
{
$eo->kludges()->attach($k,['value'=>json_encode($v)]);
}
foreach ($o->unknown as $v)
{
$eo->kludges()->attach('UNKNOWN',['value'=>json_encode($v)]);
}
foreach ($o->seenby as $v)
{
foreach ($this->parse_nodes(Zone::findOrFail($pkt->sz),$v) as $no)
{
$eo->seenbys()->attach($no->id);
}
}
$seq = 0;
foreach ($o->path as $v)
{
foreach ($this->parse_nodes(Zone::findOrFail($pkt->sz),$v) as $no)
{
$eo->paths()->attach($no->id,['sequence'=>$seq++]);
}
}
}
}
}

25
app/Models/Echomail.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Echomail extends Model
{
protected $dates = ['date'];
public function kludges()
{
return $this->belongsToMany(Kludge::class);
}
public function seenbys()
{
return $this->belongsToMany(Seenby::class,NULL,NULL,'node_id');
}
public function paths()
{
return $this->belongsToMany(Path::class,NULL,NULL,'node_id');
}
}

10
app/Models/Kludge.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Kludge extends Model
{
//
}

View File

@ -6,12 +6,17 @@ use Illuminate\Database\Eloquent\Model;
class Node extends Model
{
protected $fillable = ['zone_id','host_id','node_id'];
protected $fillable = ['zone_id','host_id','node_id','point_id'];
public function flags() {
return $this->belongsToMany(Flag::class);
}
public function getFTNAttribute()
{
return sprintf('%s:%s/%s.%s',$this->zone_id,$this->host_id,$this->node_id,$this->point_id);
}
public function hasFlag($relation, $model)
{
return (bool) $this->{$relation}()

10
app/Models/Path.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Path extends Model
{
//
}

10
app/Models/Seenby.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Seenby extends Model
{
//
}

32
app/Traits/GetNode.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace App\Traits;
use App\Models\{Node,Zone};
trait GetNode
{
/**
* Get an FTN record
* If the record doesnt exist, we'll create it
*/
protected function get_node($z,$n,$f,$p=0,$create=TRUE)
{
$zo = Zone::firstOrCreate(['id'=>$z]);
$no = Node::firstOrNew(['zone_id'=>$zo->id,'host_id'=>$n,'node_id'=>$f,'point_id'=>$p]);
if (! $no->exists AND $create)
{
$no->active = FALSE;
$no->system = 'AUTO DISCOVERED';
$no->sysop = 'UNKNOWN';
$no->location = '';
$no->baud = 0;
$no->save();
}
return ($no->exists) ? $no : NULL;
}
}

31
app/Traits/ParseNodes.php Normal file
View File

@ -0,0 +1,31 @@
<?php
namespace App\Traits;
use App\Models\Zone;
trait ParseNodes
{
/**
* Parse a seenby or path list and return node models
*/
protected function parse_nodes(Zone $zone,string $line,$create=TRUE)
{
$net = FALSE;
$result = collect();
foreach (explode(' ',$line) as $node)
{
if (preg_match('#/#',$node))
{
list($net,$node) = preg_split('#/#',$node);
}
if (! $net)
throw new \Exception('Missing Net?',$node);
$result->push($this->get_node($zone->id,$net,$node,0),$create);
}
return $result;
}
}

View File

@ -17,12 +17,13 @@ class CreateNodes extends Migration
$table->increments('id');
$table->timestamps();
$table->integer('zone_id')->index();
$table->integer('region_id');
$table->integer('region_id')->nullable();
$table->integer('host_id')->index();
$table->integer('hub_id')->nullable()->index();
$table->integer('node_id')->index();
$table->integer('point_id')->default(0);
$table->binary('active');
$table->boolean('active');
$table->string('status')->nullable();
$table->string('system');
$table->string('sysop');
@ -31,13 +32,12 @@ class CreateNodes extends Migration
$table->string('phone')->nullable();
$table->binary('zt',10)->nullable();
$table->unique(['zone_id','region_id','id']);
$table->unique(['zone_id','host_id','node_id','point_id']);
$table->unique(['zone_id','zt']);
// $table->index('zone_id');
$table->foreign('zone_id')->references('id')->on('zones');
$table->unique(['zone_id','host_id','id']);
// $table->unique(['zone_id','host_id','id']);
// $table->index(['zone_id','host_id']);
// $table->index(['zone_id','id']);
// $table->foreign(['zone_id','host_id'])->references(['zone_id','id'])->on('nodes');

View File

@ -0,0 +1,56 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateEchomail extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('echomails', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('pkt_from');
$table->integer('pkt_to');
$table->datetime('pkt_date');
$table->string('pkt');
$table->string('flags');
$table->integer('cost');
$table->string('from_user');
$table->integer('from_ftn');
$table->string('to_user');
$table->string('subject');
$table->datetime('date');
$table->string('tz')->nullable();
$table->string('area');
$table->string('msgid')->nullable();
$table->string('replyid')->nullable();
$table->text('message');
$table->string('origin');
$table->text('original')->nullable();
$table->index('pkt_from');
$table->index('pkt_to');
$table->index('from_ftn');
$table->foreign('pkt_from')->references('id')->on('nodes');
$table->foreign('pkt_to')->references('id')->on('nodes');
$table->foreign('from_ftn')->references('id')->on('nodes');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('echomails');
}
}

View File

@ -0,0 +1,62 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateEchomailTables extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('echomail_kludge', function (Blueprint $table) {
$table->integer('echomail_id');
$table->string('kludge_id')->nullable();
$table->json('value');
$table->index('echomail_id');
$table->foreign('echomail_id')->references('id')->on('echomails');
});
Schema::create('echomail_seenby', function (Blueprint $table) {
$table->integer('echomail_id');
$table->integer('node_id');
$table->unique(['echomail_id','node_id']);
$table->index('echomail_id');
$table->foreign('echomail_id')->references('id')->on('echomails');
$table->index('node_id');
$table->foreign('node_id')->references('id')->on('nodes');
});
Schema::create('echomail_path', function (Blueprint $table) {
$table->integer('echomail_id');
$table->integer('node_id');
$table->integer('sequence');
$table->unique(['echomail_id','sequence']);
$table->index('echomail_id');
$table->foreign('echomail_id')->references('id')->on('echomails');
$table->index('node_id');
$table->foreign('node_id')->references('id')->on('nodes');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('echomail_path');
Schema::dropIfExists('echomail_seenby');
Schema::dropIfExists('echomail_kludge');
}
}