clrghouz/app/Models/Netmail.php
2022-02-19 17:03:12 +11:00

116 lines
2.5 KiB
PHP

<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Log;
use App\Classes\FTN\Message;
use App\Interfaces\Packet;
use App\Traits\EncodeUTF8;
final class Netmail extends Model implements Packet
{
private const LOGKEY = 'MN-';
use SoftDeletes,EncodeUTF8;
private const cast_utf8 = [
'to',
'from',
'subject',
'msg',
'msg_src',
'origin',
'tearline',
'tagline',
];
protected $dates = ['datetime','sent_at'];
/* RELATIONS */
public function fftn()
{
return $this
->belongsTo(Address::class)
->withTrashed();
}
public function tftn()
{
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));
// @todo Dont bundle mail to nodes that have been disabled, or addresses that have been deleted
$o = new Message;
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));
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;
}
}