clrghouz/app/Classes/File/Mail.php
Deon George 14c505c15b
All checks were successful
Create Docker Image / Build Docker Image (x86_64) (push) Successful in 41s
Create Docker Image / Build Docker Image (arm64) (push) Successful in 1m42s
Create Docker Image / Final Docker Image Manifest (push) Successful in 12s
Must not cache the mail waiting queries, otherwise mail/files will be resent in a query loop
2024-05-12 14:56:18 +10:00

118 lines
2.5 KiB
PHP

<?php
namespace App\Classes\File;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Classes\Node;
use App\Classes\FTN\{Message,Packet};
final class Mail extends Send
{
private const LOGKEY = 'IFM';
/** @var int Our internal position counter */
private int $readpos;
/**
* @throws \Exception
*/
public function __construct(Packet $mail,int $type)
{
parent::__construct();
$this->f = $mail;
$this->ftype = ((($type&0xff)<<8)|self::IS_PKT);
$this->readpos = 0;
}
public function __get($key) {
switch ($key) {
case 'dbids':
return $this->f->messages->pluck('dbid');
case 'name':
return sprintf('%08x',timew($this->youngest()));
case 'nameas':
return sprintf('%s.pkt',$this->name);
case 'mtime':
return $this->youngest()->timestamp;
case 'size':
return strlen($this->f);
case 'type':
return ($this->ftype&0xff00)>>8;
default:
return parent::__get($key);
}
}
public function close(bool $successful,Node $node): void
{
if ($successful) {
$this->complete = TRUE;
Log::debug(sprintf('%s:- Successful close for [%d] - updating [%d] records.',self::LOGKEY,$this->type,$this->dbids->count()),['dbids'=>$this->dbids,'authd'=>$node->aka_remote_authed->pluck('id')]);
// Update netmail table
if (($this->type === Send::T_NETMAIL)
&& ($x=$this->dbids)->count())
DB::table('netmails')
->whereIn('id',$x)
->update([
'sent_at'=>Carbon::now(),
'sent_pkt'=>$this->name,
'sent_id'=>$node->address->id,
'flags'=>DB::raw('flags | '.Message::FLAG_SENT),
]);
// Update echomails table
elseif (($this->type === Send::T_ECHOMAIL)
&& ($x=$this->dbids)->count()
&& $node->aka_remote_authed->count())
DB::table('echomail_seenby')
->whereIn('echomail_id',$x)
->whereIn('address_id',$node->aka_remote_authed->pluck('id'))
->update([
'sent_at'=>Carbon::now(),
'sent_pkt'=>$this->name,
]);
}
}
public function feof(): bool
{
return ($this->readpos === $this->size);
}
public function open(string $compress=''): bool
{
return TRUE;
}
public function read(int $length): string
{
$result = substr((string)$this->f,$this->readpos,$length);
$this->readpos += strlen($result);
return $result;
}
public function seek(int $pos): bool
{
$this->readpos = ($pos < $this->size) ? $pos : $this->size;
return TRUE;
}
public function youngest(): Carbon
{
return $this->f->messages->pluck('date')->sort()->last();
}
}