2024-10-31 11:40:58 +00:00
< ? php
namespace App\Classes\FTN\Process\Netmail\Robot\Areafix ;
use Carbon\Carbon ;
use Illuminate\Support\Facades\DB ;
use Illuminate\Support\Facades\Log ;
use App\Jobs\AreafixRescan ;
// Echoarea Processing Command
class Area extends Base
{
private const LOGKEY = 'AFA' ;
2024-10-31 12:53:43 +00:00
private const command = '%AREA' ;
public static function help () : array
2024-10-31 11:40:58 +00:00
{
return [
2024-10-31 12:53:43 +00:00
self :: command . ' [-|+]<ECHOAREA> [R|D=<DAYS>]' ,
' Use the area command to subscribe (+) or unsubscribe (-) to an ECHOAREA' ,
' Arguments:' ,
' - ECHOAREA (required) name of area to subscribe or unsubscribe' ,
' - D=DAYS (optional) number of days to resend mail from this area that you' ,
' havent already received (useful if you are resubscribing to an area and' ,
' have received mail in the past)' ,
' - R=DAYS (optional) number of days to resend mail from this area (even if' ,
' it was sent to you previously)' ,
' Notes:' ,
' * "+" is optional, and is implied if "-" is not used' ,
' * "R" and "D" options only apply to subscribing' ,
2024-10-31 11:40:58 +00:00
];
}
public function process () : string
{
2024-10-31 21:15:32 +00:00
$command = self :: command . ' ' . join ( ' ' , $this -> arguments );
2024-10-31 11:40:58 +00:00
// If command starts with '-', its to unsubscribe
if ( str_starts_with ( $this -> arguments [ 0 ], '-' )) {
$sub = FALSE ;
$area = substr ( $this -> arguments [ 0 ], 1 );
} elseif ( str_starts_with ( $this -> arguments [ 0 ], '+' )) {
$sub = TRUE ;
$area = substr ( $this -> arguments [ 0 ], 1 );
} else {
$sub = TRUE ;
$area = $this -> arguments [ 0 ];
}
Log :: debug ( sprintf ( '%s:- Processing [%s] for [%s]' , self :: LOGKEY , $sub ? 'ADD' : 'REMOVE' , $area ));
// Drop the area from the arguments, the rest are options
array_shift ( $this -> arguments );
2024-10-31 23:04:12 +00:00
// Area exists
2024-10-31 12:53:43 +00:00
if ( $ea = $this -> mo -> fftn -> domain -> echoareas -> where ( 'name' , $area ) -> pop ()) {
2024-10-31 11:40:58 +00:00
// If already subscribed
2024-10-31 12:53:43 +00:00
if ( $nea = $this -> mo -> fftn -> echoareas -> where ( 'name' , $area ) -> pop ()) {
2024-10-31 11:40:58 +00:00
// requesting to subscribe "You already are since..., arguments ignored
if ( $sub ) {
2024-10-31 12:53:43 +00:00
Log :: debug ( sprintf ( '%s:- FTN [%s] ALREADY subscribed to [%s] since [%s]' , self :: LOGKEY , $this -> mo -> fftn -> ftn , $area , $nea -> pivot -> subscribed -> format ( 'Y-m-d H:i' )));
2024-10-31 11:40:58 +00:00
2024-10-31 21:15:32 +00:00
return sprintf ( '%-25s <-- ALREADY subscribed since %s' , $command , $nea -> pivot -> subscribed -> format ( 'Y-m-d H:i' ));
2024-10-31 11:40:58 +00:00
// requesting to unsubscribe
} else {
2024-10-31 12:53:43 +00:00
$this -> mo -> fftn -> echoareas () -> detach ( $ea -> id );
2024-10-31 11:40:58 +00:00
// Remove sub, clear queue
$x = DB :: table ( 'echomail_seenby' )
2024-10-31 12:53:43 +00:00
-> where ( 'address_id' , $this -> mo -> fftn -> id )
2024-10-31 11:40:58 +00:00
-> join ( 'echomails' ,[ 'echomails.id' => 'echomail_seenby.echomail_id' ])
-> where ( 'echoarea_id' , $nea -> id )
-> whereNotNull ( 'export_at' )
-> whereNull ( 'sent_at' )
-> orderBy ( 'echomails.datetime' )
2024-10-31 12:53:43 +00:00
-> skip ( $this -> mo -> fftn -> system -> pkt_msgs )
2024-10-31 11:40:58 +00:00
-> delete ();
2024-10-31 12:53:43 +00:00
Log :: debug ( sprintf ( '%s:- FTN [%s] UNSUBSCRIBED from [%s] clearing [%s]' , self :: LOGKEY , $this -> mo -> fftn -> ftn , $area , $x ));
2024-10-31 11:40:58 +00:00
2024-10-31 21:15:32 +00:00
return sprintf ( '%-25s <-- UNSUBSCRIBED, CLEARED [%d] MSGS from queue' , $command , $x );
2024-10-31 11:40:58 +00:00
}
// If not subscribed
} else {
// requesting to subscribe, subsubsribe and rescan if arguments
if ( $sub ) {
2024-10-31 12:53:43 +00:00
$this -> mo -> fftn -> echoareas () -> attach ([ $ea -> id => [ 'subscribed' => Carbon :: now ()]]);
2024-10-31 11:40:58 +00:00
// If we have arguments, they are to rescan
if ( count ( $this -> arguments ) === 1 ) {
$m = [];
if ( preg_match ( '/^([DR])=([0-9]+)$/' , $this -> arguments [ 0 ], $m )) {
switch ( $m [ 1 ]) {
// Scan
case 'D' :
2024-10-31 12:53:43 +00:00
AreafixRescan :: dispatch ( $this -> mo -> fftn , $ea , $m [ 2 ])
2024-10-31 11:40:58 +00:00
-> onQueue ( 'mail' );
2024-10-31 21:15:32 +00:00
return sprintf ( '%-25s <-- SUBSCRIBED, RESCAN [%d] DAYS queued' , $command , $m [ 2 ]);
2024-10-31 11:40:58 +00:00
// Scan
case 'R' :
2024-10-31 12:53:43 +00:00
AreafixRescan :: dispatch ( $this -> mo -> fftn , $ea , $m [ 2 ], TRUE )
2024-10-31 11:40:58 +00:00
-> onQueue ( 'mail' );
2024-10-31 21:15:32 +00:00
return sprintf ( '%-25s <-- SUBSCRIBED, FORCE RESCAN [%d] DAYS queued' , $command , $m [ 2 ]);
2024-10-31 11:40:58 +00:00
}
}
2024-10-31 21:15:32 +00:00
return sprintf ( '%-25s <-- SUBSCRIBED, INVALID OPTIONS' , $command );
2024-10-31 11:40:58 +00:00
} elseif ( count ( $this -> arguments ) > 1 ) {
2024-10-31 21:15:32 +00:00
Log :: debug ( sprintf ( '%s:- FTN [%s] subscribed to [%s], extra commands [%s] ignored' , self :: LOGKEY , $this -> mo -> fftn -> ftn , $area , join ( '|' , $this -> arguments )));
2024-10-31 11:40:58 +00:00
2024-10-31 21:15:32 +00:00
return sprintf ( '%-25s <-- SUBSCRIBED, OPTIONS IGNORED' , $command );
2024-10-31 11:40:58 +00:00
} else {
2024-10-31 12:53:43 +00:00
Log :: debug ( sprintf ( '%s:- FTN [%s] subscribed to [%s]' , self :: LOGKEY , $this -> mo -> fftn -> ftn , $area ));
2024-10-31 11:40:58 +00:00
2024-10-31 21:15:32 +00:00
return sprintf ( '%-25s <-- SUBSCRIBED' , $command );
2024-10-31 11:40:58 +00:00
}
// If not subscribed, "you arent subscribed, arguments ignored"
} else {
2024-10-31 12:53:43 +00:00
Log :: debug ( sprintf ( '%s:- FTN [%s] is NOT subscribed to [%s], NO ACTION taken' , self :: LOGKEY , $this -> mo -> fftn -> ftn , $area ));
2024-10-31 11:40:58 +00:00
2024-10-31 21:15:32 +00:00
return sprintf ( '%-25s <-- NOT subscribed, NO ACTION taken' , $command );
2024-10-31 11:40:58 +00:00
}
}
} else {
2024-10-31 12:53:43 +00:00
Log :: debug ( sprintf ( '%s:- FTN [%s] area UNKNOWN [%s], NO ACTION taken' , self :: LOGKEY , $this -> mo -> fftn -> ftn , $area ));
2024-10-31 11:40:58 +00:00
2024-10-31 21:15:32 +00:00
return sprintf ( '%-25s <-- AREA UNKNOWN, NO ACTION TAKEN' , $command );
2024-10-31 11:40:58 +00:00
}
}
}