2021-07-17 05:48:07 +00:00
< ? php
namespace App\Classes\File ;
use Carbon\Carbon ;
2024-09-09 01:50:41 +00:00
use Illuminate\Support\Arr ;
2023-07-19 02:32:41 +00:00
use Illuminate\Support\Facades\DB ;
2024-05-12 04:56:18 +00:00
use Illuminate\Support\Facades\Log ;
2023-07-19 02:32:41 +00:00
use App\Classes\Node ;
use App\Classes\FTN\ { Message , Packet };
2021-07-17 05:48:07 +00:00
2023-07-17 06:36:53 +00:00
final class Mail extends Send
2021-07-17 05:48:07 +00:00
{
2024-05-12 04:56:18 +00:00
private const LOGKEY = 'IFM' ;
2023-07-17 06:36:53 +00:00
/** @var int Our internal position counter */
private int $readpos ;
2024-05-23 07:36:47 +00:00
private ? string $content ;
2021-07-17 05:48:07 +00:00
/**
* @ throws \Exception
*/
2023-07-17 06:36:53 +00:00
public function __construct ( Packet $mail , int $type )
2021-07-17 05:48:07 +00:00
{
2023-07-17 06:36:53 +00:00
parent :: __construct ();
2021-07-17 05:48:07 +00:00
2023-07-17 06:36:53 +00:00
$this -> f = $mail ;
$this -> ftype = ((( $type & 0xff ) << 8 ) | self :: IS_PKT );
$this -> readpos = 0 ;
2021-07-17 05:48:07 +00:00
}
2023-07-14 10:03:09 +00:00
public function __get ( $key ) {
switch ( $key ) {
2023-07-17 06:36:53 +00:00
case 'dbids' :
2024-05-19 13:28:45 +00:00
return $this -> f -> messages -> pluck ( 'id' );
2023-07-17 06:36:53 +00:00
case 'name' :
2024-09-09 01:50:41 +00:00
return sprintf ( '%08x' , $this -> youngest_id ());
2023-07-17 06:36:53 +00:00
case 'nameas' :
return sprintf ( '%s.pkt' , $this -> name );
case 'mtime' :
2024-09-09 01:50:41 +00:00
return $this -> youngest_date () -> timestamp ;
2023-07-17 06:36:53 +00:00
case 'type' :
return ( $this -> ftype & 0xff00 ) >> 8 ;
2023-07-14 10:03:09 +00:00
default :
return parent :: __get ( $key );
}
}
2023-07-19 02:32:41 +00:00
public function close ( bool $successful , Node $node ) : void
2023-07-17 06:36:53 +00:00
{
2023-07-19 02:32:41 +00:00
if ( $successful ) {
2023-07-17 06:36:53 +00:00
$this -> complete = TRUE ;
2023-07-19 02:32:41 +00:00
2024-11-27 07:41:27 +00:00
Log :: info ( 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' )]);
2024-05-12 04:56:18 +00:00
2023-07-19 02:32:41 +00:00
// 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 ,
]);
2024-05-23 07:36:47 +00:00
$this -> content = NULL ;
2024-06-07 01:12:06 +00:00
$this -> f = NULL ;
2023-07-19 02:32:41 +00:00
}
2023-07-17 06:36:53 +00:00
}
public function feof () : bool
{
return ( $this -> readpos === $this -> size );
}
public function open ( string $compress = '' ) : bool
{
2024-05-23 07:36:47 +00:00
$this -> content = ( string ) $this -> f ;
2024-06-17 09:03:48 +00:00
$this -> size = strlen ( $this -> content );
2023-07-17 06:36:53 +00:00
return TRUE ;
}
public function read ( int $length ) : string
{
2024-05-23 07:36:47 +00:00
$result = substr ( $this -> content , $this -> readpos , $length );
2023-07-17 06:36:53 +00:00
$this -> readpos += strlen ( $result );
return $result ;
}
public function seek ( int $pos ) : bool
2021-07-17 05:48:07 +00:00
{
2023-07-17 06:36:53 +00:00
$this -> readpos = ( $pos < $this -> size ) ? $pos : $this -> size ;
2024-05-23 07:36:47 +00:00
return TRUE ;
2021-07-17 05:48:07 +00:00
}
2023-07-14 10:03:09 +00:00
2024-09-09 01:50:41 +00:00
private function youngest () : array
2023-07-14 10:03:09 +00:00
{
2024-09-09 01:50:41 +00:00
return $this -> f -> messages -> sortBy ( fn ( $item ) => Arr :: get ( $item , 'datetime' )) -> first ();
}
private function youngest_id () : int
{
return Arr :: get ( $this -> youngest (), 'id' , 0 );
}
private function youngest_date () : Carbon
{
return Arr :: get ( $this -> youngest (), 'datetime' , Carbon :: now ());
2023-07-14 10:03:09 +00:00
}
2021-07-17 05:48:07 +00:00
}