clrghouz/app/Models/Netmail.php

179 lines
3.9 KiB
PHP
Raw Normal View History

2019-05-06 12:29:29 +00:00
<?php
namespace App\Models;
use Carbon\Carbon;
2022-01-01 05:59:35 +00:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Casts\CompressedString;
use App\Classes\FTN\Message;
use App\Interfaces\Packet;
use App\Traits\{EncodeUTF8,MsgID};
2019-05-06 12:29:29 +00:00
final class Netmail extends Model implements Packet
2019-05-06 12:29:29 +00:00
{
private const LOGKEY = 'MN-';
use SoftDeletes,EncodeUTF8,MsgID;
2021-08-26 12:32:32 +00:00
private Collection $set_path;
2021-08-26 12:32:32 +00:00
private const cast_utf8 = [
'to',
'from',
2021-08-26 12:32:32 +00:00
'subject',
'msg',
2022-02-19 05:33:14 +00:00
'msg_src',
'origin',
'tearline',
'tagline',
2021-08-26 12:32:32 +00:00
];
protected $casts = [
'datetime' => 'datetime:Y-m-d H:i:s',
'msg' => CompressedString::class,
'msg_src' => CompressedString::class,
'sent_at' => 'datetime:Y-m-d H:i:s',
];
public function __set($key,$value)
{
switch ($key) {
case 'set_path':
$this->{$key} = $value;
break;
default:
parent::__set($key,$value);
}
}
public static function boot()
{
parent::boot();
static::created(function($model) {
// Save the Path
$ppoid = NULL;
if (isset($model->set_path))
foreach ($model->set_path 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'],
]);
$ppoid = $po[0]->id;
}
});
}
/* RELATIONS */
2019-05-06 12:29:29 +00:00
public function fftn()
2019-05-06 12:29:29 +00:00
{
return $this
2021-07-17 05:48:07 +00:00
->belongsTo(Address::class)
->withTrashed();
2019-05-06 12:29:29 +00:00
}
public function path()
{
return $this->belongsToMany(Address::class,'netmail_path')
->withPivot(['id','parent_id','datetime','program']);
}
public function tftn()
2019-05-06 12:29:29 +00:00
{
return $this
->belongsTo(Address::class);
}
/* METHODS */
/**
* Return this model as a packet
*/
public function packet(Address $ao): Message
{
Log::debug(sprintf('%s:Bundling [%s]',self::LOGKEY,$this->id));
2021-07-17 05:48:07 +00:00
// @todo Dont bundle mail to nodes that have been disabled, or addresses that have been deleted
$o = new Message;
2021-07-17 05:48:07 +00:00
try {
$o->header = [
'onode' => $this->fftn->node_id,
'dnode' => $ao->node_id,
'onet' => $this->fftn->host_id,
'dnet' => $ao->host_id,
'flags' => 0, // @todo?
'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;
$o->subject = $this->subject;
// INTL kludge
// @todo Point handling FMPT/TOPT
$o->intl = sprintf('%s %s',$this->tftn->ftn3d,$this->fftn->ftn3d);
$o->flags = $this->flags;
$o->msgid = sprintf('%s %08x',$this->fftn->ftn3d,crc32($this->id));
2022-01-01 05:59:35 +00:00
if ($this->replyid)
$o->replyid = $this->replyid;
$o->kludge->put('dbid',$this->id);
$o->message = $this->msg;
$o->tagline = $this->tagline;
$o->tearline = $this->tearline;
// VIA kludge
$sysaddress = Setup::findOrFail(config('app.id'))->system->match($this->fftn->zone)->first();
$via = $this->via ?: collect();
$via->push(
sprintf('%s @%s.UTC %s %d.%d/%s %s',
$sysaddress->ftn3d,
Carbon::now()->utc()->format('Ymd.His'),
str_replace(' ','_',Setup::PRODUCT_NAME),
Setup::PRODUCT_VERSION_MAJ,
Setup::PRODUCT_VERSION_MIN,
(new Setup)->version,
Carbon::now()->format('Y-m-d'),
));
$o->via = $via;
$o->packed = TRUE;
} catch (\Exception $e) {
Log::error(sprintf('%s:Error converting netmail [%s] to a message (%d:%s)',self::LOGKEY,$this->id,$e->getLine(),$e->getMessage()));
dump($this);
}
return $o;
2019-05-06 12:29:29 +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
}
return $result->flatten()->filter();
}
2019-05-06 12:29:29 +00:00
}