From 9ba790e72c0c4d088495464dd2bdbb17fbcf9785 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sat, 27 Apr 2019 23:57:39 +1000 Subject: [PATCH] Initial echomail import --- app/Classes/FTNMessage.php | 21 +++- app/Classes/FTNPacket.php | 24 ++-- ...{NodelistImport.php => ImportNodelist.php} | 4 +- app/Console/Commands/ImportPacket.php | 105 ++++++++++++++++++ app/Models/Echomail.php | 25 +++++ app/Models/Kludge.php | 10 ++ app/Models/Node.php | 7 +- app/Models/Path.php | 10 ++ app/Models/Seenby.php | 10 ++ app/Traits/GetNode.php | 32 ++++++ app/Traits/ParseNodes.php | 31 ++++++ .../2019_04_16_105254_create_nodes.php | 10 +- .../2019_04_26_112227_create_echomail.php | 56 ++++++++++ ...19_04_26_112228_create_echomail_tables.php | 62 +++++++++++ 14 files changed, 387 insertions(+), 20 deletions(-) rename app/Console/Commands/{NodelistImport.php => ImportNodelist.php} (96%) create mode 100644 app/Console/Commands/ImportPacket.php create mode 100644 app/Models/Echomail.php create mode 100644 app/Models/Kludge.php create mode 100644 app/Models/Path.php create mode 100644 app/Models/Seenby.php create mode 100644 app/Traits/GetNode.php create mode 100644 app/Traits/ParseNodes.php create mode 100644 database/migrations/2019_04_26_112227_create_echomail.php create mode 100644 database/migrations/2019_04_26_112228_create_echomail_tables.php diff --git a/app/Classes/FTNMessage.php b/app/Classes/FTNMessage.php index f5efba5..1226188 100644 --- a/app/Classes/FTNMessage.php +++ b/app/Classes/FTNMessage.php @@ -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; } diff --git a/app/Classes/FTNPacket.php b/app/Classes/FTNPacket.php index 09298e2..377ff07 100644 --- a/app/Classes/FTNPacket.php +++ b/app/Classes/FTNPacket.php @@ -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', diff --git a/app/Console/Commands/NodelistImport.php b/app/Console/Commands/ImportNodelist.php similarity index 96% rename from app/Console/Commands/NodelistImport.php rename to app/Console/Commands/ImportNodelist.php index dfef8fe..6d9770d 100644 --- a/app/Console/Commands/NodelistImport.php +++ b/app/Console/Commands/ImportNodelist.php @@ -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. diff --git a/app/Console/Commands/ImportPacket.php b/app/Console/Commands/ImportPacket.php new file mode 100644 index 0000000..43aa2a8 --- /dev/null +++ b/app/Console/Commands/ImportPacket.php @@ -0,0 +1,105 @@ +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++]); + } + } + } + } +} \ No newline at end of file diff --git a/app/Models/Echomail.php b/app/Models/Echomail.php new file mode 100644 index 0000000..90112df --- /dev/null +++ b/app/Models/Echomail.php @@ -0,0 +1,25 @@ +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'); + } +} \ No newline at end of file diff --git a/app/Models/Kludge.php b/app/Models/Kludge.php new file mode 100644 index 0000000..6f64384 --- /dev/null +++ b/app/Models/Kludge.php @@ -0,0 +1,10 @@ +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}() diff --git a/app/Models/Path.php b/app/Models/Path.php new file mode 100644 index 0000000..2d70c7b --- /dev/null +++ b/app/Models/Path.php @@ -0,0 +1,10 @@ +$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; + } +} \ No newline at end of file diff --git a/app/Traits/ParseNodes.php b/app/Traits/ParseNodes.php new file mode 100644 index 0000000..832d97a --- /dev/null +++ b/app/Traits/ParseNodes.php @@ -0,0 +1,31 @@ +push($this->get_node($zone->id,$net,$node,0),$create); + } + + return $result; + } +} \ No newline at end of file diff --git a/database/migrations/2019_04_16_105254_create_nodes.php b/database/migrations/2019_04_16_105254_create_nodes.php index eb81cb1..c46b445 100644 --- a/database/migrations/2019_04_16_105254_create_nodes.php +++ b/database/migrations/2019_04_16_105254_create_nodes.php @@ -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'); diff --git a/database/migrations/2019_04_26_112227_create_echomail.php b/database/migrations/2019_04_26_112227_create_echomail.php new file mode 100644 index 0000000..df4b164 --- /dev/null +++ b/database/migrations/2019_04_26_112227_create_echomail.php @@ -0,0 +1,56 @@ +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'); + } +} diff --git a/database/migrations/2019_04_26_112228_create_echomail_tables.php b/database/migrations/2019_04_26_112228_create_echomail_tables.php new file mode 100644 index 0000000..7c17b8f --- /dev/null +++ b/database/migrations/2019_04_26_112228_create_echomail_tables.php @@ -0,0 +1,62 @@ +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'); + } +}