Implementation of areafix processing, first subscribe/unsubscribe with scan

This commit is contained in:
2024-10-31 22:40:58 +11:00
parent d792bf8fe3
commit 4796dd9a6e
11 changed files with 1133 additions and 745 deletions

View File

@@ -6,9 +6,9 @@ use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Log;
use App\Classes\FTN\Process;
use App\Classes\FTN\Process\Netmail\Robot\Unknown;
use App\Models\{Echomail,Netmail};
use App\Notifications\Netmails\Areafix as AreafixNotification;
use App\Notifications\Netmails\Areafix\NotConfiguredHere as AreafixNotConfiguredHereNotification;
/**
* Process messages to Ping
@@ -19,18 +19,75 @@ final class Areafix extends Process
{
private const LOGKEY = 'RP-';
private const areafix_commands = 'App\\Classes\\FTN\\Process\\Netmail\\Robot\\Areafix\\';
public static function handle(Echomail|Netmail $mo): bool
{
if (strtolower($mo->to) !== 'areafix')
if (((strtolower($mo->to) !== 'areafix') || (strtolower($mo->to) !== 'filefix')) && (! ($mo instanceof Netmail)))
return FALSE;
Log::info(sprintf('%s:- Processing AREAFIX message from (%s) [%s]',self::LOGKEY,$mo->from,$mo->fftn));
Log::info(sprintf('%s:- Processing AREAFIX message from (%s) [%s]',self::LOGKEY,$mo->from,$mo->fftn->ftn));
// If this is not a node we manage, then respond with a sorry can help you
if ($mo->fftn->system->sessions->count())
Notification::route('netmail',$mo->fftn)->notify(new AreafixNotification($mo));
else
Notification::route('netmail',$mo->fftn)->notify(new AreafixNotConfiguredHereNotification($mo));
if (! $mo->fftn->system->sessions->count()) {
Notification::route('netmail',$mo->fftn)->notify(new AreafixNotification\NotConfiguredHere($mo));
return TRUE;
}
// If this nodes password is not correct
if ($mo->fftn->pass_fix !== strtoupper($mo->subject)) {
Notification::route('netmail',$mo->fftn)->notify(new AreafixNotification\InvalidPassword($mo));
return TRUE;
}
$result = collect();
$result->push('--> BEGIN <--');
foreach ($mo->body_lines as $command) {
// Skip empty lines
if (! $command)
continue;
$command = explode(' ',strtoupper(trim($command)));
// If command starts with '...' or '---', its a tear/tag line, and we have reached the end
if (str_starts_with($command[0],'...') || str_starts_with($command[0],'---')) {
Log::debug(sprintf('%s:= We got a tearline/tagline, end of processing',self::LOGKEY));
$result->push('--> END OF PROCESSING <--');
break;
// If command doesnt start with %, its an area
} elseif (! str_starts_with($command[0],'%')) {
Log::debug(sprintf('%s:= Assuming command [%s] is an AREA command',self::LOGKEY,$command[0]));
array_unshift($command,'%AREA');
}
// Parse the message body and pluck out the commands on each line
$class = self::areafix_commands.ucfirst(strtolower(substr($command[0],1)));
if (! class_exists($class)) {
$result->push(sprintf('%-25s <-- **COMMAND UNKNOWN**',join(' ',$command)));
continue;
}
// Drop the command from the array, the rest are arguments
array_shift($command);
// Refresh our echoareas
$mo->fftn->load('echoareas');
$o = new $class($mo->fftn,$command);
$result->push($o->process());
}
// Reply with a confirmation of what commands were processed
Notification::route('netmail',$mo->fftn)->notify(new AreafixNotification\CommandsProcessed($mo,$result));
return TRUE;
}

View File

@@ -0,0 +1,138 @@
<?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';
public function help(): array
{
return [
'%AREA [-|+]<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',
];
}
public function process(): string
{
// 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);
if ($ea=$this->ao->domain->echoareas->where('name',$area)->pop()) {
// If already subscribed
if ($nea=$this->ao->echoareas->where('name',$area)->pop()) {
// requesting to subscribe "You already are since..., arguments ignored
if ($sub) {
Log::debug(sprintf('%s:- FTN [%s] ALREADY subscribed to [%s] since [%s]',self::LOGKEY,$this->ao->ftn,$area,$nea->pivot->subscribed->format('Y-m-d H:i')));
return sprintf('%-25s <-- ALREADY subscribed since %s',$area,$nea->pivot->subscribed->format('Y-m-d H:i'));
// requesting to unsubscribe
} else {
$this->ao->echoareas()->detach($ea->id);
// Remove sub, clear queue
$x = DB::table('echomail_seenby')
->where('address_id',$this->ao->id)
->join('echomails',['echomails.id'=>'echomail_seenby.echomail_id'])
->where('echoarea_id',$nea->id)
->whereNotNull('export_at')
->whereNull('sent_at')
->orderBy('echomails.datetime')
->skip($this->ao->system->pkt_msgs)
->delete();
Log::debug(sprintf('%s:- FTN [%s] UNSUBSCRIBED from [%s] clearing [%s]',self::LOGKEY,$this->ao->ftn,$area,$x));
return sprintf('%-25s <-- UNSUBSCRIBED, cleared [%d] items from queue',$area,$x);
}
// If not subscribed
} else {
// requesting to subscribe, subsubsribe and rescan if arguments
if ($sub) {
$this->ao->echoareas()->attach([$ea->id=>['subscribed'=>Carbon::now()]]);
// 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':
AreafixRescan::dispatch($this->ao,$ea,$m[2])
->onQueue('mail');
return sprintf('%-25s <-- AREA SUBSCRIBED, RESCAN [%d] DAYS queued',$area,$m[2]);
// Scan
case 'R':
AreafixRescan::dispatch($this->ao,$ea,$m[2],TRUE)
->onQueue('mail');
return sprintf('%-25s <-- AREA SUBSCRIBED, FORCE RESCAN [%d] DAYS queued',$area,$m[2]);
}
}
return sprintf('%-25s <-- AREA SUBSCRIBED, INVALID OPTIONS',$area);
} elseif (count($this->arguments) > 1) {
Log::debug(sprintf('%s:- FTN [%s] subscribed to [%s], extra commands [%s] ignored',self::LOGKEY,$this->ao->ftn,$area,implode('|',$this->arguments)));
return sprintf('%-25s <-- AREA SUBSCRIBED, OPTIONS IGNORED',$area);
} else {
Log::debug(sprintf('%s:- FTN [%s] subscribed to [%s]',self::LOGKEY,$this->ao->ftn,$area));
return sprintf('%-25s <-- AREA SUBSCRIBED',$area);
}
// If not subscribed, "you arent subscribed, arguments ignored"
} else {
Log::debug(sprintf('%s:- FTN [%s] is NOT subscribed to [%s], NO ACTION taken',self::LOGKEY,$this->ao->ftn,$area));
return sprintf('%-25s <-- NOT subscribed, NO ACTION taken',$area);
}
}
} else {
Log::debug(sprintf('%s:- FTN [%s] area UNKNOWN [%s], NO ACTION taken',self::LOGKEY,$this->ao->ftn,$area));
return sprintf('%-25s <-- AREA UNKNOWN, NO ACTION TAKEN',$area);
}
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Classes\FTN\Process\Netmail\Robot\Areafix;
use Illuminate\Support\Facades\Log;
use App\Models\Address;
// Our base areafix commands class
abstract class Base
{
private const LOGKEY = 'AB-';
protected Address $ao;
protected array $arguments;
public function __construct(Address $ao,array $arguments) {
Log::debug(sprintf('%s:- Areafix [%s] command with arguments [%s] for [%s]',self::LOGKEY,get_class($this),implode('|',$arguments),$ao->ftn));
$this->ao = $ao;
$this->arguments = $arguments;
}
abstract public function help(): array;
abstract public function process(): string;
}