2019-05-06 12:29:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
use Carbon\Carbon;
|
2022-01-01 05:59:35 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2023-07-15 00:46:19 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2023-06-18 13:33:26 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2021-08-15 09:25:04 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-07-15 14:54:23 +00:00
|
|
|
|
2023-06-18 13:33:26 +00:00
|
|
|
use App\Casts\CompressedString;
|
2021-07-15 14:54:23 +00:00
|
|
|
use App\Classes\FTN\Message;
|
2021-07-30 14:35:52 +00:00
|
|
|
use App\Interfaces\Packet;
|
2022-03-14 11:28:54 +00:00
|
|
|
use App\Traits\{EncodeUTF8,MsgID};
|
2019-05-06 12:29:29 +00:00
|
|
|
|
2021-08-15 09:25:04 +00:00
|
|
|
final class Netmail extends Model implements Packet
|
2019-05-06 12:29:29 +00:00
|
|
|
{
|
2021-08-15 09:25:04 +00:00
|
|
|
private const LOGKEY = 'MN-';
|
|
|
|
|
2022-03-14 11:28:54 +00:00
|
|
|
use SoftDeletes,EncodeUTF8,MsgID;
|
2021-08-26 12:32:32 +00:00
|
|
|
|
2023-06-18 13:33:26 +00:00
|
|
|
private Collection $set_path;
|
2023-07-15 00:46:19 +00:00
|
|
|
private Address $set_sender;
|
2023-08-05 11:32:45 +00:00
|
|
|
private Carbon $set_recvtime;
|
2023-07-15 00:46:19 +00:00
|
|
|
private string $set_pkt;
|
2023-06-18 13:33:26 +00:00
|
|
|
|
2021-08-26 12:32:32 +00:00
|
|
|
private const cast_utf8 = [
|
2021-12-01 11:45:51 +00:00
|
|
|
'to',
|
|
|
|
'from',
|
2021-08-26 12:32:32 +00:00
|
|
|
'subject',
|
|
|
|
'msg',
|
2022-02-19 05:33:14 +00:00
|
|
|
'msg_src',
|
2021-09-12 13:06:17 +00:00
|
|
|
'origin',
|
|
|
|
'tearline',
|
|
|
|
'tagline',
|
2021-08-26 12:32:32 +00:00
|
|
|
];
|
2021-07-15 14:54:23 +00:00
|
|
|
|
2023-06-18 13:33:26 +00:00
|
|
|
protected $casts = [
|
2023-06-26 00:32:38 +00:00
|
|
|
'datetime' => 'datetime:Y-m-d H:i:s',
|
2023-06-18 13:33:26 +00:00
|
|
|
'msg' => CompressedString::class,
|
|
|
|
'msg_src' => CompressedString::class,
|
2023-06-26 00:32:38 +00:00
|
|
|
'sent_at' => 'datetime:Y-m-d H:i:s',
|
2023-06-18 13:33:26 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
public function __set($key,$value)
|
|
|
|
{
|
|
|
|
switch ($key) {
|
|
|
|
case 'set_path':
|
2023-07-15 00:46:19 +00:00
|
|
|
case 'set_pkt':
|
2023-08-05 11:32:45 +00:00
|
|
|
case 'set_recvtime':
|
2023-07-15 00:46:19 +00:00
|
|
|
case 'set_sender':
|
2023-06-18 13:33:26 +00:00
|
|
|
$this->{$key} = $value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
parent::__set($key,$value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::created(function($model) {
|
2023-09-20 10:29:23 +00:00
|
|
|
$nodes = collect();
|
2023-06-23 11:28:29 +00:00
|
|
|
|
2023-09-20 10:29:23 +00:00
|
|
|
// Parse PATH
|
|
|
|
// <FTN Address> @YYYYMMDD.HHMMSS[.Precise][.Time Zone] <Program Name> <Version> [Serial Number]
|
2023-07-15 00:46:19 +00:00
|
|
|
if (isset($model->set_path)) {
|
2023-09-20 10:29:23 +00:00
|
|
|
if ($model->set_path->count()) {
|
|
|
|
foreach ($model->set_path as $line) {
|
|
|
|
$m = [];
|
|
|
|
|
|
|
|
if (preg_match('/^([0-9]+:[0-9]+\/[0-9]+(\..*)?)\s+@([0-9.a-zA-Z]+)\s+(.*)$/',$line,$m)) {
|
|
|
|
// Address
|
|
|
|
$ao = Address::findFTN($m[1]);
|
|
|
|
|
|
|
|
// Time
|
|
|
|
$t = [];
|
|
|
|
$datetime = '';
|
|
|
|
|
|
|
|
if (! preg_match('/^([0-9]+\.[0-9]+)(\.?(.*))?$/',$m[3],$t))
|
|
|
|
Log::alert(sprintf('%s:! Unable to determine time from [%s]',self::LOGKEY,$m[3]));
|
|
|
|
else
|
|
|
|
$datetime = Carbon::createFromFormat('Ymd.His',$t[1],$t[3] ?? '');
|
|
|
|
|
|
|
|
if (! $ao) {
|
|
|
|
Log::alert(sprintf('%s:! Undefined Node [%s] for Netmail.',self::LOGKEY,$m[1]));
|
|
|
|
//$rogue->push(['node'=>$m[1],'datetime'=>$datetime,'program'=>$m[4]]);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$nodes->push(['node'=>$ao,'datetime'=>$datetime,'program'=>$m[4]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 00:46:19 +00:00
|
|
|
// If there are no details (Mystic), we'll create a blank
|
2023-09-20 10:29:23 +00:00
|
|
|
} else {
|
|
|
|
$nodes->push(['node'=>$model->set_sender,'datetime'=>Carbon::now(),'program'=>'Unknown']);
|
2023-07-15 00:46:19 +00:00
|
|
|
}
|
2023-09-20 10:29:23 +00:00
|
|
|
}
|
2023-07-15 00:46:19 +00:00
|
|
|
|
2023-09-20 10:29:23 +00:00
|
|
|
// Save the Path
|
|
|
|
$ppoid = NULL;
|
2023-06-23 11:28:29 +00:00
|
|
|
|
2023-09-20 10:29:23 +00:00
|
|
|
foreach ($nodes as $path) {
|
|
|
|
$po = DB::select('INSERT INTO netmail_path (netmail_id,address_id,parent_id,datetime,program) VALUES (?,?,?,?,?) RETURNING id',[
|
|
|
|
$model->id,
|
|
|
|
$path['node']->id,
|
|
|
|
$ppoid,
|
|
|
|
(string)$path['datetime'],
|
|
|
|
$path['program'],
|
|
|
|
]);
|
2023-07-15 00:46:19 +00:00
|
|
|
|
2023-09-20 10:29:23 +00:00
|
|
|
$ppoid = $po[0]->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our last node in the path is our sender
|
|
|
|
if ($nodes->count() && isset($model->set_pkt) && isset($model->set_sender) && isset($model->set_recvtime)) {
|
|
|
|
DB::update('UPDATE netmail_path set recv_pkt=?,recv_at=?,recv_id=? where address_id=? and netmail_id=?',[
|
|
|
|
$model->set_pkt,
|
|
|
|
$model->set_recvtime,
|
|
|
|
$model->set_sender->id,
|
|
|
|
Arr::get($nodes->last(),'node')->id,
|
|
|
|
$model->id,
|
|
|
|
]);
|
2023-07-15 00:46:19 +00:00
|
|
|
}
|
2023-06-18 13:33:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
/* RELATIONS */
|
2019-05-06 12:29:29 +00:00
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
public function fftn()
|
2019-05-06 12:29:29 +00:00
|
|
|
{
|
2021-07-15 14:54:23 +00:00
|
|
|
return $this
|
2021-07-17 05:48:07 +00:00
|
|
|
->belongsTo(Address::class)
|
|
|
|
->withTrashed();
|
2019-05-06 12:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-06-18 13:33:26 +00:00
|
|
|
public function path()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Address::class,'netmail_path')
|
2023-07-15 00:46:19 +00:00
|
|
|
->withPivot(['id','parent_id','datetime','program','recv_pkt','recv_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function received()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Address::class,'netmail_path','netmail_id','recv_id')
|
|
|
|
->withPivot(['id','parent_id','datetime','program','recv_pkt','recv_id']);
|
2023-06-18 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
public function tftn()
|
2019-05-06 12:29:29 +00:00
|
|
|
{
|
2021-07-15 14:54:23 +00:00
|
|
|
return $this
|
|
|
|
->belongsTo(Address::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* METHODS */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return this model as a packet
|
|
|
|
*/
|
2023-07-23 07:27:52 +00:00
|
|
|
public function packet(Address $ao,string $strippass=NULL): Message
|
2021-07-15 14:54:23 +00:00
|
|
|
{
|
2023-07-15 12:10:05 +00:00
|
|
|
Log::debug(sprintf('%s:+ Bundling [%s]',self::LOGKEY,$this->id));
|
2021-08-15 09:25:04 +00:00
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
// @todo Dont bundle mail to nodes that have been disabled, or addresses that have been deleted
|
2021-07-30 14:35:52 +00:00
|
|
|
$o = new Message;
|
2021-07-17 05:48:07 +00:00
|
|
|
|
2021-08-15 09:25:04 +00:00
|
|
|
try {
|
|
|
|
$o->header = [
|
|
|
|
'onode' => $this->fftn->node_id,
|
|
|
|
'dnode' => $ao->node_id,
|
|
|
|
'onet' => $this->fftn->host_id,
|
|
|
|
'dnet' => $ao->host_id,
|
2023-07-20 10:04:41 +00:00
|
|
|
'opoint' => $this->fftn->point_id,
|
|
|
|
'dpoint' => $ao->point_id,
|
|
|
|
'flags' => 0,
|
2021-08-15 09:25:04 +00:00
|
|
|
'cost' => 0,
|
|
|
|
'date'=>$this->datetime->format('d M y H:i:s'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$o->tzutc = $this->datetime->utcOffset($this->tzoffset)->getOffsetString('');
|
|
|
|
$o->user_to = $this->to;
|
|
|
|
$o->user_from = $this->from;
|
2023-07-23 07:27:52 +00:00
|
|
|
$o->subject = (! is_null($strippass)) ? preg_replace('/^'.$strippass.':/','',$this->subject) : $this->subject;
|
2021-08-15 09:25:04 +00:00
|
|
|
|
|
|
|
// INTL kludge
|
|
|
|
$o->intl = sprintf('%s %s',$this->tftn->ftn3d,$this->fftn->ftn3d);
|
|
|
|
$o->flags = $this->flags;
|
|
|
|
|
2023-11-15 00:19:14 +00:00
|
|
|
$o->msgid = $this->msgid
|
|
|
|
? $this->msgid
|
|
|
|
: sprintf('%s %08x',$this->fftn->ftn4d,timew($this->datetime));
|
|
|
|
|
2022-01-01 05:59:35 +00:00
|
|
|
if ($this->replyid)
|
|
|
|
$o->replyid = $this->replyid;
|
2021-08-15 09:25:04 +00:00
|
|
|
|
2022-02-16 12:01:55 +00:00
|
|
|
$o->kludge->put('dbid',$this->id);
|
|
|
|
|
2021-08-15 09:25:04 +00:00
|
|
|
$o->message = $this->msg;
|
2021-08-19 07:24:56 +00:00
|
|
|
$o->tagline = $this->tagline;
|
|
|
|
$o->tearline = $this->tearline;
|
2021-08-15 09:25:04 +00:00
|
|
|
|
|
|
|
// VIA kludge
|
2022-02-16 12:01:55 +00:00
|
|
|
$sysaddress = Setup::findOrFail(config('app.id'))->system->match($this->fftn->zone)->first();
|
2021-08-15 09:25:04 +00:00
|
|
|
$via = $this->via ?: collect();
|
2023-07-15 12:10:05 +00:00
|
|
|
// Add our address to the VIA line
|
2021-08-15 09:25:04 +00:00
|
|
|
$via->push(
|
|
|
|
sprintf('%s @%s.UTC %s %d.%d/%s %s',
|
2022-02-16 12:01:55 +00:00
|
|
|
$sysaddress->ftn3d,
|
2021-08-15 09:25:04 +00:00
|
|
|
Carbon::now()->utc()->format('Ymd.His'),
|
2022-02-16 12:01:55 +00:00
|
|
|
str_replace(' ','_',Setup::PRODUCT_NAME),
|
2021-08-15 09:25:04 +00:00
|
|
|
Setup::PRODUCT_VERSION_MAJ,
|
|
|
|
Setup::PRODUCT_VERSION_MIN,
|
|
|
|
(new Setup)->version,
|
|
|
|
Carbon::now()->format('Y-m-d'),
|
|
|
|
));
|
|
|
|
|
|
|
|
$o->via = $via;
|
|
|
|
|
2021-08-16 12:30:34 +00:00
|
|
|
$o->packed = TRUE;
|
|
|
|
|
2021-08-15 09:25:04 +00:00
|
|
|
} catch (\Exception $e) {
|
2023-07-15 12:10:05 +00:00
|
|
|
Log::error(sprintf('%s:! Error converting netmail [%s] to a message (%d:%s)',self::LOGKEY,$this->id,$e->getLine(),$e->getMessage()));
|
2021-08-15 09:25:04 +00:00
|
|
|
dump($this);
|
|
|
|
}
|
2021-07-15 14:54:23 +00:00
|
|
|
|
|
|
|
return $o;
|
2019-05-06 12:29:29 +00:00
|
|
|
}
|
2023-06-18 13:33:26 +00:00
|
|
|
|
|
|
|
public function pathorder(string $display='ftn2d',int $start=NULL): Collection
|
|
|
|
{
|
|
|
|
$result = collect();
|
|
|
|
|
|
|
|
if ($x=$this->path->firstWhere('pivot.parent_id',$start)) {
|
|
|
|
$result->push($x->$display);
|
|
|
|
$result->push($this->pathorder($display,$x->pivot->id));
|
2023-06-27 07:39:11 +00:00
|
|
|
}
|
2023-06-18 13:33:26 +00:00
|
|
|
|
|
|
|
return $result->flatten()->filter();
|
|
|
|
}
|
2019-05-06 12:29:29 +00:00
|
|
|
}
|