clrghouz/app/Models/Echomail.php

286 lines
8.5 KiB
PHP
Raw Normal View History

2019-04-27 13:57:39 +00:00
<?php
namespace App\Models;
2021-09-06 13:39:32 +00:00
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;
2021-08-29 14:44:20 +00:00
use Illuminate\Support\Facades\DB;
2021-09-06 13:39:32 +00:00
use Illuminate\Support\Facades\Log;
2019-04-27 13:57:39 +00:00
use App\Casts\{CollectionOrNull,CompressedStringOrNull,UTF8StringOrNull};
use App\Classes\FTN\Message;
use App\Interfaces\Packet;
use App\Traits\{MessageAttributes,MsgID,ParseAddresses,QueryCacheableConfig};
2021-09-06 13:39:32 +00:00
final class Echomail extends Model implements Packet
2019-04-27 13:57:39 +00:00
{
use SoftDeletes,MessageAttributes,MsgID,ParseAddresses,QueryCacheableConfig;
2021-08-11 13:45:30 +00:00
2021-09-06 13:39:32 +00:00
private const LOGKEY = 'ME-';
private bool $no_export = FALSE;
2022-01-01 05:59:35 +00:00
private const kludges = [
'MSGID:'=>'msgid',
'PATH:'=>'set_path',
'REPLY:'=>'replyid',
'SEEN-BY:'=>'set_seenby',
];
// When generating a packet for this echomail, the packet recipient is our tftn
public Address $tftn;
2022-01-01 05:59:35 +00:00
protected $casts = [
'to' => UTF8StringOrNull::class,
'from' => UTF8StringOrNull::class,
'subject' => UTF8StringOrNull::class,
'origin' => UTF8StringOrNull::class,
'tearline' => UTF8StringOrNull::class,
'tagline' => UTF8StringOrNull::class,
'datetime' => 'datetime:Y-m-d H:i:s',
'kludges' => CollectionOrNull::class,
'msg' => CompressedStringOrNull::class,
'msg_src' => CompressedStringOrNull::class,
'rogue_seenby' => CollectionOrNull::class,
'rogue_path' => CollectionOrNull::class, // @deprecated?
2021-08-11 13:45:30 +00:00
];
2019-04-27 13:57:39 +00:00
2024-06-03 09:08:40 +00:00
public function __get($key)
{
switch ($key) {
case 'set_echoarea':
case 'set_fftn':
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);
}
}
2022-11-01 11:24:36 +00:00
public function __set($key,$value)
2022-01-01 05:59:35 +00:00
{
switch ($key) {
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;
case 'no_export':
$this->{$key} = $value;
break;
case 'set_fftn':
// Values that we pass to boot() to record how we got this echomail
case 'set_pkt':
case 'set_recvtime':
case 'set_sender':
2024-06-03 09:08:40 +00:00
case 'set_tagline':
case 'set_tearline':
case 'set_origin':
// For us to record the echoarea the message is for, if the area isnt defined (eg: packet dump)
case 'set_echoarea':
$this->set->put($key,$value);
break;
// The path and seenby the echomail went through to get here
case 'set_path':
2022-01-01 05:59:35 +00:00
case 'set_seenby':
if (! $this->set->has($key))
$this->set->put($key,collect());
$this->set->get($key)->push($value);
2022-01-01 05:59:35 +00:00
break;
default:
parent::__set($key,$value);
}
}
2021-09-06 13:39:32 +00:00
public static function boot()
{
parent::boot();
static::creating(function($model) {
if (isset($model->errors) && $model->errors->count())
throw new \Exception('Cannot save, validation errors exist');
2024-06-03 09:08:40 +00:00
if ($model->set->has('set_tagline'))
$model->tagline_id = Tagline::firstOrCreate(['value'=>$model->set_tagline])->id;
if ($model->set->has('set_tearline'))
$model->tearline_id = Tearline::firstOrCreate(['value'=>$model->set_tearline])->id;
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))
&& (Address::findFTN($m[2])->id === $model->fftn_id))
$model->origin_id = Origin::firstOrCreate(['value'=>$m[1]])->id;
}
// 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;
}
});
// @todo if the message is updated with new SEEN-BY's from another route, we'll delete the pending export for systems (if there is one)
2021-09-06 13:39:32 +00:00
static::created(function($model) {
$rogue = collect();
$seenby = collect();
$path = collect();
2021-09-06 13:39:32 +00:00
// Parse PATH
if ($model->set->has('set_path'))
$path = self::parseAddresses('path',$model->set->get('set_path'),$model->fftn->zone,$rogue);
Log::debug(sprintf('%s:^ Message [%d] from point address is [%d]',self::LOGKEY,$model->id,$model->fftn->point_id));
2021-09-06 13:39:32 +00:00
// Make sure our sender is first in the path
if ((! $model->isFlagSet(Message::FLAG_LOCAL)) && (! $path->contains($model->fftn_id))) {
Log::alert(sprintf('%s:? Echomail adding sender to start of PATH [%s].',self::LOGKEY,$model->fftn_id));
$path->prepend($model->fftn_id);
}
// Make sure our pktsrc is last in the path
if ($model->set->has('set_sender') && (! $path->contains($model->set->get('set_sender')->id))) {
Log::alert(sprintf('%s:? Echomail adding pktsrc to end of PATH [%s].',self::LOGKEY,$model->set->get('set_sender')->ftn));
$path->push($model->set->get('set_sender')->id);
}
2022-01-01 05:59:35 +00:00
// Save the Path
$ppoid = NULL;
foreach ($path as $aoid) {
2022-01-01 05:59:35 +00:00
$po = DB::select('INSERT INTO echomail_path (echomail_id,address_id,parent_id) VALUES (?,?,?) RETURNING id',[
$model->id,
$aoid,
$ppoid,
2021-09-06 13:39:32 +00:00
]);
2022-01-01 05:59:35 +00:00
$ppoid = $po[0]->id;
2021-09-06 13:39:32 +00:00
}
$rogue = collect();
2023-12-01 07:14:51 +00:00
// @todo move the parseAddress processing into Message::class, and our address to the seenby (and thus no need to add it when we export)
// Parse SEEN-BY
if ($model->set->has('set_seenby'))
$seenby = self::parseAddresses('seenby',$model->set->get('set_seenby'),$model->fftn->zone,$rogue);
2023-11-22 02:35:37 +00:00
// Make sure our sender is in the seenby
if ((! $model->isFlagSet(Message::FLAG_LOCAL)) && (! $seenby->contains($model->fftn_id))) {
Log::alert(sprintf('%s:? Echomail adding sender to SEENBY [%s].',self::LOGKEY,$model->fftn_id));
$seenby->push($model->fftn_id);
}
2023-11-22 02:35:37 +00:00
// Make sure our pktsrc is in the seenby
if ($model->set->has('set_sender') && (! $seenby->contains($model->set->get('set_sender')->id))) {
Log::alert(sprintf('%s:? Echomail adding pktsrc to SEENBY [%s].',self::LOGKEY,$model->set->get('set_sender')->ftn));
$seenby->push($model->set->get('set_sender')->id);
}
if (count($rogue)) {
$model->rogue_seenby = $rogue;
$model->save();
}
if ($seenby)
$model->seenby()->sync($seenby);
// Our last node in the path is our sender
if ($model->set->has('set_pkt') && $model->set->has('set_recvtime')) {
if ($path->count()) {
DB::update('UPDATE echomail_path set recv_pkt=?,recv_at=? where address_id=? and echomail_id=?',[
$model->set->get('set_pkt'),
$model->set->get('set_recvtime'),
$path->last(),
$model->id,
]);
} else {
Log::critical(sprintf('%s:! Wasnt able to set packet details for [%d] to [%s] to [%s], no path information',self::LOGKEY,$model->id,$model->set->get('set_pkt'),$model->set->get('set_recvtime')));
}
}
2022-01-01 05:59:35 +00:00
// See if we need to export this message.
2023-07-29 03:17:36 +00:00
if ($model->echoarea->sec_read) {
$exportto = $model
2023-07-29 03:17:36 +00:00
->echoarea
->addresses
->filter(function($item) use ($model) { return $model->echoarea->can_read($item->security); })
2023-07-29 03:17:36 +00:00
->pluck('id')
->diff(our_address($model->fftn->zone->domain)->pluck('id'))
->diff($seenby);
2023-07-29 03:17:36 +00:00
if ($exportto->count()) {
if ($model->no_export) {
Log::alert(sprintf('%s:- NOT processing exporting of message by configuration [%s] to [%s]',self::LOGKEY,$model->id,$exportto->join(',')));
2023-07-29 03:17:36 +00:00
return;
}
Log::info(sprintf('%s:- Exporting message [%s] to [%s]',self::LOGKEY,$model->id,$exportto->join(',')));
2023-07-29 03:17:36 +00:00
// Save the seenby for the exported systems
$model->seenby()->syncWithPivotValues($exportto,['export_at'=>Carbon::now()],FALSE);
}
2022-01-01 05:59:35 +00:00
}
2021-09-06 13:39:32 +00:00
});
}
/* RELATIONS */
2019-04-27 13:57:39 +00:00
2021-09-06 13:39:32 +00:00
public function echoarea()
{
return $this->belongsTo(Echoarea::class);
}
2022-01-01 05:59:35 +00:00
public function seenby()
2021-08-29 01:48:27 +00:00
{
2022-01-01 05:59:35 +00:00
return $this->belongsToMany(Address::class,'echomail_seenby')
2023-12-01 07:14:51 +00:00
->withPivot(['export_at','sent_at','sent_pkt'])
->FTN2DOrder();
2021-08-29 01:48:27 +00:00
}
2022-01-01 05:59:35 +00:00
public function path()
2021-08-29 01:48:27 +00:00
{
return $this->belongsToMany(Address::class,'echomail_path')
->withPivot(['id','parent_id','recv_pkt','recv_at'])
->orderBy('id','DESC');
2021-08-29 01:48:27 +00:00
}
/* ATTRIBUTES */
public function getSeenByAttribute(): Collection
{
return ((! $this->exists) && $this->set->has('set_seenby'))
? $this->set->get('set_seenby')
: $this->getRelationValue('seenby');
}
public function getPathAttribute(): Collection
2021-08-11 13:45:30 +00:00
{
return ((! $this->exists) && $this->set->has('set_path'))
? $this->set->get('set_path')
: $this->getRelationValue('path');
}
2019-04-27 13:57:39 +00:00
}