From 67cbfa4c64d9de72f38a44911b3fafc545f0e917 Mon Sep 17 00:00:00 2001 From: Deon George Date: Tue, 26 Nov 2024 12:23:07 +1100 Subject: [PATCH] Split out areafix command processing, implemented start of filefix --- app/Classes/FTN/Process/Netmail/Robot.php | 50 ++++++++++ .../Process/Netmail/{ => Robot}/Areafix.php | 53 +++-------- .../Process/Netmail/Robot/Areafix/Help.php | 4 +- .../FTN/Process/Netmail/Robot/Filefix.php | 93 +++++++++++++++++++ .../Process/Netmail/Robot/Filefix/Help.php | 50 ++++++++++ app/Notifications/Netmails/Areafix/Help.php | 2 +- app/Notifications/Netmails/Filefix/Help.php | 63 +++++++++++++ config/process.php | 3 +- 8 files changed, 273 insertions(+), 45 deletions(-) create mode 100644 app/Classes/FTN/Process/Netmail/Robot.php rename app/Classes/FTN/Process/Netmail/{ => Robot}/Areafix.php (55%) create mode 100644 app/Classes/FTN/Process/Netmail/Robot/Filefix.php create mode 100644 app/Classes/FTN/Process/Netmail/Robot/Filefix/Help.php create mode 100644 app/Notifications/Netmails/Filefix/Help.php diff --git a/app/Classes/FTN/Process/Netmail/Robot.php b/app/Classes/FTN/Process/Netmail/Robot.php new file mode 100644 index 0000000..daa550d --- /dev/null +++ b/app/Classes/FTN/Process/Netmail/Robot.php @@ -0,0 +1,50 @@ +to) !== 'areafix') && (strtolower($mo->to) !== 'filefix')) || (! ($mo instanceof Netmail))) + return FALSE; + + Log::info(sprintf('%s:- Processing *FIX [%s] message from (%s) [%s]',self::LOGKEY,$mo->to,$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 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 InvalidPassword($mo)); + + return TRUE; + } + + if ((strtolower($mo->to) === 'areafix')) + return static::areafix($mo); + + if ((strtolower($mo->to) === 'filefix')) + return static::filefix($mo); + + return FALSE; + } +} \ No newline at end of file diff --git a/app/Classes/FTN/Process/Netmail/Areafix.php b/app/Classes/FTN/Process/Netmail/Robot/Areafix.php similarity index 55% rename from app/Classes/FTN/Process/Netmail/Areafix.php rename to app/Classes/FTN/Process/Netmail/Robot/Areafix.php index 677bbcf..9cd7133 100644 --- a/app/Classes/FTN/Process/Netmail/Areafix.php +++ b/app/Classes/FTN/Process/Netmail/Robot/Areafix.php @@ -1,55 +1,33 @@ to) !== 'areafix') && (strtolower($mo->to) !== 'filefix')) || (! ($mo instanceof Netmail))) + if ((strtolower($mo->to) !== 'areafix') || (! ($mo instanceof Netmail))) return FALSE; - Log::info(sprintf('%s:- Processing *FIX [%s] message from (%s) [%s]',self::LOGKEY,$mo->to,$mo->from,$mo->fftn->ftn)); + Log::info(sprintf('%s:- Processing AREAFIX [%s] message from (%s) [%s]', self::LOGKEY, $mo->to, $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 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 InvalidPassword($mo)); - - return TRUE; - } - - if ((strtolower($mo->to) === 'areafix')) - return self::areafix($mo); - - if ((strtolower($mo->to) === 'filefix')) - return self::filefix($mo); - - return FALSE; + return parent::handle($mo); } public static function areafix(Netmail $mo): bool @@ -72,7 +50,7 @@ final class Areafix extends Process break; - // If command doesnt start with %, its an area + // If command doesnt start with %, its an area } elseif (! str_starts_with($command[0],'%')) { Log::info(sprintf('%s:= Assuming command [%s] is an AREA command',self::LOGKEY,$command[0])); @@ -82,12 +60,12 @@ final class Areafix extends Process // Some commands are reserved words switch ($x=strtolower(substr($command[0],1))) { case 'list': - $class = self::areafix_commands.'AreaList'; + $class = self::commands.'AreaList'; break; default: // Parse the message body and pluck out the commands on each line - $class = self::areafix_commands.ucfirst($x); + $class = self::commands.ucfirst($x); } if (! class_exists($class)) { @@ -112,11 +90,4 @@ final class Areafix extends Process return TRUE; } - - public static function filefix(Netmail $mo): bool - { - Notification::route('netmail',$mo->fftn)->notify(new FixCantHandle($mo)); - - return TRUE; - } } \ No newline at end of file diff --git a/app/Classes/FTN/Process/Netmail/Robot/Areafix/Help.php b/app/Classes/FTN/Process/Netmail/Robot/Areafix/Help.php index 28b398a..b999195 100644 --- a/app/Classes/FTN/Process/Netmail/Robot/Areafix/Help.php +++ b/app/Classes/FTN/Process/Netmail/Robot/Areafix/Help.php @@ -5,7 +5,7 @@ namespace App\Classes\FTN\Process\Netmail\Robot\Areafix; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Notification; -use App\Classes\FTN\Process\Netmail\Areafix; +use App\Classes\FTN\Process\Netmail\Robot\Areafix; use App\Notifications\Netmails\Areafix\Help as HelpNotification; // A Help Index Command @@ -33,7 +33,7 @@ class Help extends Base if (($file === 'Base.php') || (! str_ends_with(strtolower($file),'.php'))) continue; - $class = Areafix::areafix_commands.preg_replace('/\.php$/','',$file); + $class = Areafix::commands.preg_replace('/\.php$/','',$file); if ($result->count()) $result->push(''); diff --git a/app/Classes/FTN/Process/Netmail/Robot/Filefix.php b/app/Classes/FTN/Process/Netmail/Robot/Filefix.php new file mode 100644 index 0000000..78b0b85 --- /dev/null +++ b/app/Classes/FTN/Process/Netmail/Robot/Filefix.php @@ -0,0 +1,93 @@ +to) !== 'filefix') || (! ($mo instanceof Netmail))) + return FALSE; + + Log::info(sprintf('%s:- Processing FILEFIX [%s] message from (%s) [%s]', self::LOGKEY, $mo->to, $mo->from, $mo->fftn->ftn)); + + return parent::handle($mo); + } + + public static function filefix(Netmail $mo): bool + { + $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::info(sprintf('%s:= Assuming command [%s] is an AREA command',self::LOGKEY,$command[0])); + + array_unshift($command,'%AREA'); + } + + // Some commands are reserved words + switch ($x=strtolower(substr($command[0],1))) { + case 'list': + $class = self::commands.'FileList'; + break; + + default: + // Parse the message body and pluck out the commands on each line + $class = self::commands.ucfirst($x); + } + + if (! class_exists($class)) { + $result->push(sprintf('%-25s <-- **COMMAND UNKNOWN**',join(' ',$command))); + Log::info(sprintf('%s:! Command UNKNOWN [%s] ',self::LOGKEY,join('|',$command)),['class'=>$class]); + + continue; + } + + // Drop the command from the array, the rest are arguments + array_shift($command); + + // Refresh our echoareas + $mo->fftn->load('fileareas'); + + $o = new $class($mo,$command); + $result->push($o->process()); + } + + // Reply with a confirmation of what commands were processed + Notification::route('netmail',$mo->fftn)->notify(new CommandsProcessed($mo,$result)); + + return TRUE; + } +} \ No newline at end of file diff --git a/app/Classes/FTN/Process/Netmail/Robot/Filefix/Help.php b/app/Classes/FTN/Process/Netmail/Robot/Filefix/Help.php new file mode 100644 index 0000000..79db660 --- /dev/null +++ b/app/Classes/FTN/Process/Netmail/Robot/Filefix/Help.php @@ -0,0 +1,50 @@ +mo->fftn->ftn)); + + $result = collect(); + + foreach (preg_grep('/^([^.])/',scandir(self::filefix_classes)) as $file) { + if (($file === 'Base.php') || (! str_ends_with(strtolower($file),'.php'))) + continue; + + $class = Filefix::commands.preg_replace('/\.php$/','',$file); + if ($result->count()) + $result->push(''); + + $result = $result + ->merge($class::help()); + } + + Notification::route('netmail',$this->mo->fftn)->notify(new HelpNotification($this->mo,$result)); + + return sprintf('%-25s <-- COMMAND PROCESSED',self::command); + } +} \ No newline at end of file diff --git a/app/Notifications/Netmails/Areafix/Help.php b/app/Notifications/Netmails/Areafix/Help.php index ad000fc..c561573 100644 --- a/app/Notifications/Netmails/Areafix/Help.php +++ b/app/Notifications/Netmails/Areafix/Help.php @@ -16,7 +16,7 @@ class Help extends Netmails private const LOGKEY = 'ACH'; /** - * Reply to a areafix, commands unknown. + * Reply to an areafix HELP commands. * * @param Netmail $mo * @param Collection $commands diff --git a/app/Notifications/Netmails/Filefix/Help.php b/app/Notifications/Netmails/Filefix/Help.php new file mode 100644 index 0000000..9f337e7 --- /dev/null +++ b/app/Notifications/Netmails/Filefix/Help.php @@ -0,0 +1,63 @@ +setupNetmail($notifiable); + $ao = $notifiable->routeNotificationFor(static::via); + + Log::info(sprintf('%s:+ Responding to filefix for a node [%s] HELP processed',self::LOGKEY,$ao->ftn)); + + $o->to = $this->mo->from; + $o->replyid = $this->mo->msgid; + $o->subject = 'Filefix - Help'; + + // Message + $msg = $this->page(FALSE,'Filefix'); + + $msg->addText("Here are the list of commands available to you:\r\r\r\r"); + + foreach ($this->commands as $command) { + $msg->addText("$command\r"); + } + + $o->msg = $msg->render(); + $o->set_tagline = 'Why did the chicken cross the road? The robot programmed it.'; + + $o->save(); + + return $o; + } +} \ No newline at end of file diff --git a/config/process.php b/config/process.php index 00a0e96..b3d22d7 100644 --- a/config/process.php +++ b/config/process.php @@ -9,6 +9,7 @@ return [ // Netmail 'robots' => [ \App\Classes\FTN\Process\Netmail\Ping::class, - \App\Classes\FTN\Process\Netmail\Areafix::class, + \App\Classes\FTN\Process\Netmail\Robot\Areafix::class, + \App\Classes\FTN\Process\Netmail\Robot\Filefix::class, ], ]; \ No newline at end of file