Added filefix %FILELIST

This commit is contained in:
Deon George 2024-11-26 23:48:14 +11:00
parent 08375f4995
commit d9817e1c2e
2 changed files with 170 additions and 0 deletions

View File

@ -0,0 +1,67 @@
<?php
namespace App\Classes\FTN\Process\Netmail\Robot\Filefix;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
use App\Classes\FTN\Process\Netmail\Robot\Areafix\Base;
use App\Notifications\Netmails\Filefix\Filelist as FilelistNotification;
// FILELIST - List files in an area
class Filelist extends Base
{
private const LOGKEY = 'AFR';
private const command = '%FILELIST';
public static function help(): array
{
return [
self::command.' [-|+]<FILEAREA> [<DAYS>]',
' Use the filelist command to list files from an filearea.',
' This is will resend files again, even if you have received them in the',
' past.',
' Arguments:',
' - FILEAREA (required) name of area',
' - DAYS (optional) number of days to resend mail from this area that you',
' If DAYS is omitted, the default is 30. The maximum is 365.',
];
}
public function process(): string
{
Log::debug(sprintf('%s:- Filefix [%s] for [%s] for [%s]',self::LOGKEY,self::command,$this->mo->fftn->ftn,join('|',$this->arguments)));
$command = self::command.' '.join(' ',$this->arguments);
if (! is_numeric($this->arguments[1]))
return sprintf('%-25s <-- INVALID, DAYS [%s] NOT NUMERIC',$command,$this->arguments[1]);
if ($this->arguments[1] > 365)
$this->arguments[1] = 365;
// Area exists
if ($fa=$this->mo->fftn->domain->fileareas->where('name',$this->arguments[0])->pop()) {
// If already subscribed
if ($this->mo->fftn->fileareas->pluck('name')->contains($this->arguments[0])) {
Notification::route('netmail',$this->mo->fftn)
->notify(new FileListNotification($this->mo,$fa,$this->arguments[1]));
Log::debug(sprintf('%s:- FTN [%s] FILELIST [%s] DAYS [%d]',self::LOGKEY,$this->mo->fftn->ftn,$this->arguments[0],$this->arguments[1]));
return sprintf('%-25s <-- FILELIST [%d] DAYS',$command,$this->arguments[1]);
// If not subscribed
} else {
Log::debug(sprintf('%s:- FTN [%s] is NOT subscribed to [%s], NO ACTION taken',self::LOGKEY,$this->mo->fftn->ftn,$this->arguments[0]));
return sprintf('%-25s <-- NOT subscribed, NO ACTION taken',$command);
}
} else {
Log::debug(sprintf('%s:- FTN [%s] area UNKNOWN [%s], NO ACTION taken',self::LOGKEY,$this->mo->fftn->ftn,$this->arguments[0]));
return sprintf('%-25s <-- AREA UNKNOWN, NO ACTION TAKEN',$command);
}
}
}

View File

@ -0,0 +1,103 @@
<?php
namespace App\Notifications\Netmails\Filefix;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use App\Notifications\Netmails;
use App\Models\{Filearea,Netmail};
use Illuminate\Support\Str;
use App\Traits\{MessagePath,PageTemplate};
class Filelist extends Netmails
{
use MessagePath,PageTemplate;
private const LOGKEY = 'FCL';
private Filearea $fa;
private int $days;
/**
* Reply to a filefix FILELIST commands.
*
* @param Filearea $fa
* @param int $days
*/
public function __construct(Netmail $mo,Filearea $fa,int $days=30)
{
parent::__construct();
$this->mo = $mo->withoutRelations();
$this->fa = $fa->withoutRelations();
$this->days = $days;
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return Netmail
* @throws \Exception
*/
public function toNetmail(object $notifiable): Netmail
{
$o = $this->setupNetmail($notifiable);
$ao = $notifiable->routeNotificationFor(static::via);
Log::info(sprintf('%s:+ Responding to filefix [%s] FILE LIST processed',self::LOGKEY,$ao->ftn));
$o->to = $this->mo->from;
$o->replyid = $this->mo->msgid;
$o->subject = 'Filefix - File List';
// Message
$msg = $this->page(FALSE,'Filefix');
$msg->addText(sprintf("Filearea [%s] has the following files in the last %d days:\r\r",$this->fa->name,$this->days));
$files = $this->fa
->files()
->orderBy('datetime')
->where('datetime','>',Carbon::now()->subDays($this->days)->startOfDay());
if ($files->count()) {
$msg->addText(sprintf(":-%s-:-%s-:-%s-:\r",
str_repeat('-',15),
str_repeat('-',44),
str_repeat('-',8),
));
$msg->addText(sprintf(": %-15s : %-44s : %8s :\r",'FILE','DESCRIPTION','SIZE(mb)'));
$msg->addText(sprintf(":-%s-:-%s-:-%s-:\r",
str_repeat('-',15),
str_repeat('-',44),
str_repeat('-',8),
));
foreach ($files->get() as $fo) {
$msg->addText(sprintf(": %-15s : %-44s : %8s :\r",
$fo->name,
Str::limit($fo->desc,44-3),
number_format($fo->size/1024/1024,3),
));
}
$msg->addText(sprintf(":-%s-:-%s-:-%s-:\r",
str_repeat('-',15),
str_repeat('-',44),
str_repeat('-',8),
));
} else {
$msg->addText(sprintf('No files in [%s]',$this->fa->name));
}
$o->msg = $msg->render();
$o->set_tagline = 'Why did the robot cross the road? The chicken programmed it.';
$o->save();
return $o;
}
}