clrghouz/app/Models/Netmail.php

115 lines
2.4 KiB
PHP
Raw Normal View History

2019-05-06 12:29:29 +00:00
<?php
namespace App\Models;
use Carbon\Carbon;
use Jenssegers\Mongodb\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\SoftDeletes;
use App\Classes\FTN\Message;
2019-05-06 12:29:29 +00:00
class Netmail extends Model
{
use SoftDeletes;
//protected $connection = 'mongodb';
protected $dates = ['datetime','sent_at'];
/**
* Resolve a connection instance.
* We need to do this, because our relations are in pgsql and somehow loading a relation changes this models
* connection information. Using protected $connection is not enough.
*
* @param string|null $connection
*/
public static function resolveConnection($connection = null)
{
return static::$resolver->connection('mongodb');
}
/* RELATIONS */
2019-05-06 12:29:29 +00:00
public function fftn()
2019-05-06 12:29:29 +00:00
{
return $this
->setConnection('pgsql')
2021-07-17 05:48:07 +00:00
->belongsTo(Address::class)
->withTrashed();
2019-05-06 12:29:29 +00:00
}
public function tftn()
2019-05-06 12:29:29 +00:00
{
return $this
->setConnection('pgsql')
->belongsTo(Address::class);
}
/* ATTRIBUTES */
public function getMsgAttribute($value): string
{
return utf8_decode($value);
}
public function setMsgAttribute($value): void
{
$this->attributes['msg'] = utf8_encode($value);
}
/* METHODS */
/**
* Return this model as a packet
*/
public function packet(): Message
{
$o = new Message;
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->header = [
'onode' => $this->fftn->node_id,
'dnode' => $this->tftn->node_id,
'onet' => $this->fftn->host_id,
'dnet' => $this->tftn->host_id,
'flags' => 0, // @todo?
'cost' => 0,
'date'=>$this->created_at->format('d M y H:i:s'),
];
$o->user_to = $this->to;
$o->user_from = $this->from;
$o->subject = $this->subject;
$o->message = $this->msg;
if ($this->tagline)
$o->message .= "\r... ".$this->tagline."\r";
$o->tearline .= $this->tearline;
$o->msgid = sprintf('%s %08x',$this->fftn->ftn3d,crc32($this->id));
// VIA kludge
$via = $this->via ?: collect();
$via->push(
sprintf('%s @%s.UTC %s %d.%d/%s %s',
$this->fftn->ftn3d,
Carbon::now()->utc()->format('Ymd.His'),
Setup::PRODUCT_NAME,
Setup::PRODUCT_VERSION_MAJ,
Setup::PRODUCT_VERSION_MIN,
(new Setup)->version,
Carbon::now()->format('Y-m-d'),
));
$o->via = $via;
// INTL kludge
// @todo Point handling FMPT/TOPT
$o->intl = sprintf('%s %s',$this->tftn->ftn3d,$this->fftn->ftn3d);
2021-07-17 05:48:07 +00:00
// TZUTC
$o->kludge->put('tzutc',str_replace('+','',$this->created_at->getOffsetString('')));
return $o;
2019-05-06 12:29:29 +00:00
}
}