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
|
|
|
|
2024-06-01 00:46:02 +00:00
|
|
|
use App\Casts\{CollectionOrNull,CompressedStringOrNull,UTF8StringOrNull};
|
2021-07-30 14:35:52 +00:00
|
|
|
use App\Interfaces\Packet;
|
2024-05-17 12:10:54 +00:00
|
|
|
use App\Pivots\ViaPivot;
|
|
|
|
use App\Traits\{MessageAttributes,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
|
|
|
{
|
2024-05-17 12:10:54 +00:00
|
|
|
use SoftDeletes,MsgID,MessageAttributes;
|
|
|
|
|
2021-08-15 09:25:04 +00:00
|
|
|
private const LOGKEY = 'MN-';
|
2024-06-06 22:42:10 +00:00
|
|
|
public const UPDATED_AT = NULL;
|
2024-05-17 12:10:54 +00:00
|
|
|
private const PATH_REGEX = '/^([0-9]+:[0-9]+\/[0-9]+(\..*)?)\s+@([0-9.a-zA-Z]+)\s+(.*)$/';
|
2021-08-15 09:25:04 +00:00
|
|
|
|
2024-05-17 12:10:54 +00:00
|
|
|
/**
|
|
|
|
* Kludges that we absorb in this model
|
|
|
|
*/
|
|
|
|
private const kludges = [
|
|
|
|
'MSGID:'=>'msgid',
|
|
|
|
'REPLY:'=>'replyid',
|
|
|
|
'Via' => 'set_path',
|
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 = [
|
2024-06-01 00:46:02 +00:00
|
|
|
'to' => UTF8StringOrNull::class,
|
|
|
|
'from' => UTF8StringOrNull::class,
|
|
|
|
'subject' => UTF8StringOrNull::class,
|
2023-06-26 00:32:38 +00:00
|
|
|
'datetime' => 'datetime:Y-m-d H:i:s',
|
2024-05-17 12:10:54 +00:00
|
|
|
'kludges' => CollectionOrNull::class,
|
2024-06-01 00:46:02 +00:00
|
|
|
'msg' => CompressedStringOrNull::class,
|
|
|
|
'msg_src' => CompressedStringOrNull::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
|
|
|
];
|
|
|
|
|
2024-06-03 09:08:40 +00:00
|
|
|
public function __get($key)
|
|
|
|
{
|
|
|
|
switch ($key) {
|
|
|
|
case 'set_fftn':
|
|
|
|
case 'set_tftn':
|
|
|
|
case 'set_path':
|
|
|
|
case 'set_pkt':
|
|
|
|
case 'set_recvtime':
|
|
|
|
case 'set_seenby':
|
|
|
|
case 'set_sender':
|
|
|
|
|
|
|
|
case 'set_tagline':
|
|
|
|
case 'set_tearline':
|
|
|
|
case 'set_origin':
|
|
|
|
return $this->set->get($key);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return parent::__get($key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-18 13:33:26 +00:00
|
|
|
public function __set($key,$value)
|
|
|
|
{
|
|
|
|
switch ($key) {
|
2024-05-17 12:10:54 +00:00
|
|
|
case 'kludges':
|
|
|
|
if (! count($value))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (array_key_exists($value[0],self::kludges)) {
|
|
|
|
$this->{self::kludges[$value[0]]} = $value[1];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->kludges->put($value[0],$value[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2024-05-20 11:31:21 +00:00
|
|
|
case 'set_fftn':
|
|
|
|
case 'set_tftn':
|
2024-05-17 12:10:54 +00:00
|
|
|
// Values that we pass to boot() to record how we got this netmail
|
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':
|
2024-06-03 09:08:40 +00:00
|
|
|
|
2024-05-17 12:10:54 +00:00
|
|
|
case 'set_tagline':
|
|
|
|
case 'set_tearline':
|
|
|
|
case 'set_origin':
|
|
|
|
$this->set->put($key,$value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// The path the netmail went through to get here
|
|
|
|
case 'set_path':
|
|
|
|
if (! $this->set->has($key))
|
|
|
|
$this->set->put($key,collect());
|
|
|
|
|
|
|
|
$this->set->get($key)->push($value);
|
2023-06-18 13:33:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
parent::__set($key,$value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
2024-05-17 12:10:54 +00:00
|
|
|
static::creating(function($model) {
|
2024-05-21 13:30:48 +00:00
|
|
|
if (isset($model->errors) && $model->errors->count())
|
2024-05-17 12:10:54 +00:00
|
|
|
throw new \Exception('Cannot save, validation errors exist');
|
2024-06-03 09:08:40 +00:00
|
|
|
|
2024-06-05 11:57:16 +00:00
|
|
|
if ($model->set->has('set_tagline')) {
|
|
|
|
$x = Tagline::where('value',utf8_encode($model->set_tagline))->single();
|
2024-06-03 09:08:40 +00:00
|
|
|
|
2024-06-05 11:57:16 +00:00
|
|
|
if (! $x) {
|
|
|
|
$x = new Tagline;
|
|
|
|
$x->value = $model->set_tagline;
|
|
|
|
$x->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->tagline_id = $x->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($model->set->has('set_tearline')) {
|
|
|
|
$x = Tearline::where('value',utf8_encode($model->set_tearline))->single();
|
|
|
|
|
|
|
|
if (! $x) {
|
|
|
|
$x = new Tearline;
|
|
|
|
$x->value = $model->set_tearline;
|
|
|
|
$x->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->tearline_id = $x->id;
|
|
|
|
}
|
2024-06-03 09:08:40 +00:00
|
|
|
|
|
|
|
if ($model->set->has('set_origin')) {
|
|
|
|
// Make sure our origin contains our FTN
|
|
|
|
$m = [];
|
|
|
|
if ((preg_match('#^(.*)\s+\(([0-9]+:[0-9]+/[0-9]+.*)\)+\s*$#',$model->set_origin,$m))
|
2024-06-05 11:57:16 +00:00
|
|
|
&& (Address::findFTN(sprintf('%s@%s',$m[2],$model->fftn->domain->name))?->id === $model->fftn_id))
|
|
|
|
{
|
|
|
|
$x = Origin::where('value',utf8_encode($m[1]))->single();
|
|
|
|
|
|
|
|
if (! $x) {
|
|
|
|
$x = new Origin;
|
|
|
|
$x->value = $m[1];
|
|
|
|
$x->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->origin_id = $x->id;
|
|
|
|
}
|
2024-06-03 09:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we can rebuild the message content, then we can do away with msg_src
|
|
|
|
if (md5($model->rebuildMessage()) === $model->msg_crc) {
|
|
|
|
Log::debug(sprintf('%s:- Pruning message source, since we can rebuild the message [%s]',self::LOGKEY,$model->msgid));
|
|
|
|
$model->msg_src = NULL;
|
|
|
|
}
|
2024-05-17 12:10:54 +00:00
|
|
|
});
|
|
|
|
|
2023-06-18 13:33:26 +00:00
|
|
|
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]
|
2024-05-17 12:10:54 +00:00
|
|
|
if ($model->set->has('set_path')) {
|
|
|
|
foreach ($model->set->get('set_path') as $line) {
|
|
|
|
$m = [];
|
|
|
|
|
|
|
|
if (preg_match(self::PATH_REGEX,$line,$m)) {
|
|
|
|
// Address
|
|
|
|
// @todo Do we need to add a domain here, since the path line may not include one
|
|
|
|
$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] in netmail path.',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-09-20 10:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-15 00:46:19 +00:00
|
|
|
}
|
2024-05-17 12:10:54 +00:00
|
|
|
|
|
|
|
// If there are no details (Mystic), we'll create a blank
|
2024-05-19 13:28:45 +00:00
|
|
|
} elseif ($model->set->has('set_sender')) {
|
|
|
|
$nodes->push(['node'=>$model->set->get('set_sender'),'datetime'=>Carbon::now(),'program'=>'Unknown']);
|
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
|
2024-05-17 12:10:54 +00:00
|
|
|
if ($nodes->count() && $model->set->has('set_pkt') && $model->set->has('set_sender') && $model->set->has('set_recvtime')) {
|
2023-09-20 10:29:23 +00:00
|
|
|
DB::update('UPDATE netmail_path set recv_pkt=?,recv_at=?,recv_id=? where address_id=? and netmail_id=?',[
|
2024-05-17 12:10:54 +00:00
|
|
|
$model->set->get('set_pkt'),
|
|
|
|
$model->set->get('set_recvtime'),
|
|
|
|
$model->set->get('set_sender')->id,
|
2023-09-20 10:29:23 +00:00
|
|
|
Arr::get($nodes->last(),'node')->id,
|
|
|
|
$model->id,
|
|
|
|
]);
|
2023-07-15 00:46:19 +00:00
|
|
|
}
|
2024-05-17 12:10:54 +00:00
|
|
|
|
|
|
|
$model->save();
|
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
|
|
|
|
2023-06-18 13:33:26 +00:00
|
|
|
public function path()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Address::class,'netmail_path')
|
2024-05-17 12:10:54 +00:00
|
|
|
->withPivot(['id','parent_id','datetime','program','recv_pkt','recv_id'])
|
2024-05-23 05:16:47 +00:00
|
|
|
->orderBy('netmail_path.id')
|
2024-05-17 12:10:54 +00:00
|
|
|
->using(ViaPivot::class);
|
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
|
2023-11-23 10:55:39 +00:00
|
|
|
->belongsTo(Address::class)
|
|
|
|
->withTrashed();
|
2021-07-15 14:54:23 +00:00
|
|
|
}
|
|
|
|
|
2024-05-17 12:10:54 +00:00
|
|
|
/* ATTRIBUTES */
|
2021-07-15 14:54:23 +00:00
|
|
|
|
|
|
|
/**
|
2024-05-17 12:10:54 +00:00
|
|
|
* Enable rendering the path even if the model hasnt been saved
|
|
|
|
*
|
|
|
|
* @return Collection
|
2021-07-15 14:54:23 +00:00
|
|
|
*/
|
2024-05-17 12:10:54 +00:00
|
|
|
public function getPathAttribute(): Collection
|
2021-07-15 14:54:23 +00:00
|
|
|
{
|
2024-05-17 12:10:54 +00:00
|
|
|
return ((! $this->exists) && $this->set->has('set_path'))
|
|
|
|
? $this->set->get('set_path')->map(function($item) {
|
|
|
|
$m = [];
|
|
|
|
preg_match(self::PATH_REGEX,$item,$m);
|
|
|
|
return $m[1];
|
|
|
|
})
|
|
|
|
: $this->getRelationValue('path');
|
2019-05-06 12:29:29 +00:00
|
|
|
}
|
2023-06-18 13:33:26 +00:00
|
|
|
|
2024-05-17 12:10:54 +00:00
|
|
|
/* METHODS */
|
2023-06-18 13:33:26 +00:00
|
|
|
|
2024-05-17 12:10:54 +00:00
|
|
|
/**
|
|
|
|
* Render the via line
|
|
|
|
*
|
|
|
|
* @param Address $ao
|
|
|
|
* @return string
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function via(Address $ao): string
|
|
|
|
{
|
|
|
|
if (! $ao->pivot)
|
|
|
|
throw new \Exception('Cannot render the via line without an address record without a path pivot');
|
2023-06-18 13:33:26 +00:00
|
|
|
|
2024-05-17 12:10:54 +00:00
|
|
|
return sprintf('%s @%s.UTC %s',
|
|
|
|
$ao->ftn3d,
|
|
|
|
$ao->pivot->datetime->format('Ymd.His'),
|
|
|
|
$ao->pivot->program);
|
2023-06-18 13:33:26 +00:00
|
|
|
}
|
2019-05-06 12:29:29 +00:00
|
|
|
}
|