2019-04-27 13:57:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
2019-05-20 07:18:18 +00:00
|
|
|
use App\Traits\{GetNode,ParseNodes};
|
2019-04-27 13:57:39 +00:00
|
|
|
use App\Classes\FTNPacket;
|
2019-05-06 12:29:29 +00:00
|
|
|
use App\Models\{Echomail,Netmail,Zone};
|
2019-04-27 13:57:39 +00:00
|
|
|
|
|
|
|
class ImportPacket extends Command
|
|
|
|
{
|
2019-05-20 07:18:18 +00:00
|
|
|
use GetNode,ParseNodes;
|
2019-04-27 13:57:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-05-20 07:18:18 +00:00
|
|
|
protected $signature = 'import:pkt {file : Packet File} {--f|force : Force import of duplicates}';
|
2019-04-27 13:57:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2019-05-06 12:29:29 +00:00
|
|
|
* @throws \Exception
|
2019-04-27 13:57:39 +00:00
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$pkt = new FTNPacket($this->argument('file'));
|
|
|
|
|
|
|
|
foreach ($pkt->messages as $o)
|
|
|
|
{
|
2019-05-20 07:18:18 +00:00
|
|
|
$o->date->setTimezone(($o->tzutc >= 0 ? '+' : '').substr_replace($o->tzutc,':',2,0));
|
|
|
|
|
|
|
|
switch ($o->type)
|
2019-04-27 13:57:39 +00:00
|
|
|
{
|
2019-05-06 12:29:29 +00:00
|
|
|
case 'echomail':
|
|
|
|
// See if we already have this message.
|
2019-05-20 07:18:18 +00:00
|
|
|
$oo = Echomail::firstOrNew([
|
|
|
|
'date'=>$o->date,
|
|
|
|
'from_ftn'=>$this->get_node(['z'=>$o->fz,'n'=>$o->fn,'f'=>$o->ff,'p'=>$o->fp])->id,
|
2019-05-06 12:29:29 +00:00
|
|
|
'msgid'=>$o->msgid,
|
|
|
|
]);
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
if (md5(utf8_decode($eo->message)) == md5($o->message))
|
|
|
|
{
|
|
|
|
$this->warn(sprintf('Duplicate message: %s@%s with id: %s',$o->from,$o->fqfa,$o->msgid));
|
|
|
|
break 2;
|
|
|
|
}
|
2019-05-06 12:29:29 +00:00
|
|
|
|
2019-05-20 07:18:18 +00:00
|
|
|
break;
|
2019-05-06 12:29:29 +00:00
|
|
|
|
2019-05-20 07:18:18 +00:00
|
|
|
case 'netmail':
|
|
|
|
// See if we already have this message.
|
|
|
|
$oo = Netmail::firstOrNew([
|
|
|
|
'date'=>$o->date,
|
|
|
|
'from_ftn'=>$this->get_node(['z'=>$o->fz,'n'=>$o->fn,'f'=>$o->ff,'p'=>$o->fp])->id,
|
|
|
|
'msgid'=>$o->msgid,
|
|
|
|
]);
|
2019-05-06 12:29:29 +00:00
|
|
|
|
2019-05-20 07:18:18 +00:00
|
|
|
$oo->to_ftn = $this->get_node(['z'=>$o->tz,'n'=>$o->tn,'f'=>$o->tf,'p'=>$o->tp])->id;
|
2019-05-06 12:29:29 +00:00
|
|
|
|
2019-05-20 07:18:18 +00:00
|
|
|
break;
|
2019-05-06 12:29:29 +00:00
|
|
|
|
2019-05-20 07:18:18 +00:00
|
|
|
default:
|
|
|
|
abort(500,'Unknown type: '.$o->type);
|
|
|
|
}
|
2019-05-06 12:29:29 +00:00
|
|
|
|
2019-05-20 07:18:18 +00:00
|
|
|
if (md5(utf8_decode($oo->message)) == md5($o->message))
|
|
|
|
{
|
|
|
|
$this->warn(sprintf('Duplicate message: %s@%s with id: %s',$o->from,$o->fqfa,$o->msgid));
|
|
|
|
if (! $this->option('force'))
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-06 12:29:29 +00:00
|
|
|
|
2019-05-20 07:18:18 +00:00
|
|
|
$oo->pkt_from = $this->get_node(['z'=>$pkt->sz,'n'=>$pkt->sn,'f'=>$pkt->sf,'p'=>$pkt->sp])->id;
|
|
|
|
$oo->pkt_to = $this->get_node(['z'=>$pkt->dz,'n'=>$pkt->dn,'f'=>$pkt->df,'p'=>$pkt->dp])->id;
|
|
|
|
$oo->pkt = $pkt->filename;
|
|
|
|
$oo->pkt_date = $pkt->date;
|
|
|
|
$oo->flags = $o->flags;
|
|
|
|
$oo->cost = $o->cost;
|
|
|
|
$oo->from_user = utf8_encode($o->from);
|
|
|
|
$oo->to_user = utf8_encode($o->to);
|
|
|
|
$oo->subject = utf8_encode($o->subject);
|
|
|
|
$oo->tz = $o->tzutc;
|
|
|
|
$oo->replyid = $o->replyid;
|
|
|
|
$oo->message = utf8_encode($o->message);
|
|
|
|
$oo->origin = utf8_encode($o->origin);
|
|
|
|
$oo->save();
|
|
|
|
|
|
|
|
foreach ($o->kludge as $k=>$v)
|
|
|
|
{
|
|
|
|
$oo->kludges()->attach($k,['value'=>json_encode($v)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($o->unknown as $v)
|
|
|
|
{
|
|
|
|
$oo->kludges()->attach('UNKNOWN',['value'=>json_encode($v)]);
|
|
|
|
}
|
2019-05-06 12:29:29 +00:00
|
|
|
|
2019-05-20 07:18:18 +00:00
|
|
|
// Finish off the import
|
|
|
|
switch($o->type) {
|
|
|
|
case 'echomail':
|
2019-05-06 12:29:29 +00:00
|
|
|
foreach ($o->seenby as $v)
|
|
|
|
{
|
|
|
|
foreach ($this->parse_nodes(Zone::findOrFail($pkt->sz),$v) as $no)
|
|
|
|
{
|
2019-05-20 07:18:18 +00:00
|
|
|
$oo->seenbys()->attach($no->id);
|
2019-05-06 12:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$seq = 0;
|
|
|
|
foreach ($o->path as $v)
|
|
|
|
{
|
|
|
|
foreach ($this->parse_nodes(Zone::findOrFail($pkt->sz),$v) as $no)
|
|
|
|
{
|
2019-05-20 07:18:18 +00:00
|
|
|
$oo->paths()->attach($no->id,['sequence'=>$seq++]);
|
2019-05-06 12:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 07:18:18 +00:00
|
|
|
break;
|
2019-05-06 12:29:29 +00:00
|
|
|
|
2019-05-20 07:18:18 +00:00
|
|
|
case 'netmail':
|
2019-05-06 12:29:29 +00:00
|
|
|
$seq = 0;
|
2019-05-20 07:18:18 +00:00
|
|
|
|
2019-05-06 12:29:29 +00:00
|
|
|
foreach ($o->via as $v)
|
|
|
|
{
|
|
|
|
$data = preg_split('/\s/',$v);
|
2019-05-20 07:18:18 +00:00
|
|
|
$ftno = $this->get_node(ftn_address_split($data[0]));
|
2019-05-06 12:29:29 +00:00
|
|
|
unset($data[0]);
|
|
|
|
|
2019-05-20 07:18:18 +00:00
|
|
|
$oo->paths()->attach($ftno->id,['sequence'=>$seq++,'value'=>json_encode($data)]);
|
2019-05-06 12:29:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 07:18:18 +00:00
|
|
|
break;
|
2019-04-27 13:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-01 10:59:15 +00:00
|
|
|
}
|