From 393600f96f99f025a91f31ac6ae68c39e8bbf99c Mon Sep 17 00:00:00 2001 From: Deon George Date: Thu, 12 Aug 2021 21:59:48 +1000 Subject: [PATCH] Dont process incomplete files --- app/Classes/File/Item.php | 1 + app/Classes/File/Receive.php | 71 +++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/app/Classes/File/Item.php b/app/Classes/File/Item.php index d57af77..5cb7836 100644 --- a/app/Classes/File/Item.php +++ b/app/Classes/File/Item.php @@ -32,6 +32,7 @@ class Item public bool $sent = FALSE; public bool $received = FALSE; + public bool $incomplete = FALSE; /** * @throws FileNotFoundException diff --git a/app/Classes/File/Receive.php b/app/Classes/File/Receive.php index e14b647..3c17149 100644 --- a/app/Classes/File/Receive.php +++ b/app/Classes/File/Receive.php @@ -84,8 +84,10 @@ final class Receive extends Item if (! $this->f) throw new Exception('No file to close'); - if ($this->file_pos != $this->receiving->file_size) + if ($this->file_pos != $this->receiving->file_size) { Log::warning(sprintf('%s: - Closing [%s], but missing [%d] bytes',__METHOD__,$this->receiving->file_name,$this->receiving->file_size-$this->file_pos)); + $this->receiving->incomplete = TRUE; + } $this->receiving->received = TRUE; @@ -96,44 +98,47 @@ final class Receive extends Item $this->file_pos = 0; $this->f = NULL; + // If the packet has been received but not the right size, dont process it any more. + // If we received a packet, we'll dispatch a job to process it - switch ($this->receiving->file_type) { - case self::IS_PKT: - Log::info(sprintf('%s: - Processing mail packet [%s]',__METHOD__,$this->file)); + if (! $this->receiving->incomplete) + switch ($this->receiving->file_type) { + case self::IS_PKT: + Log::info(sprintf('%s: - Processing mail packet [%s]',__METHOD__,$this->file)); - try { - $po = Packet::open(new File($this->file),$this->ao->zone->domain); + try { + $po = Packet::open(new File($this->file),$this->ao->zone->domain); - } catch (InvalidPacketException $e) { - Log::error(sprintf('%s: - Not deleting packet [%s], as it generated an exception',__METHOD__,$this->file)); + } catch (InvalidPacketException $e) { + Log::error(sprintf('%s: - Not deleting packet [%s], as it generated an exception',__METHOD__,$this->file)); + + break; + } + + foreach ($po->messages as $msg) { + Log::info(sprintf('%s: - Mail from [%s] to [%s]',__METHOD__,$msg->fftn,$msg->tftn)); + + // @todo Quick check that the packet should be processed by us. + // @todo validate that the packet's zone is in the domain. + + // Dispatch job. + ProcessPacket::dispatchSync($msg); + } + + if ($po->hasErrors) { + Log::info(sprintf('%s: - Not deleting packet [%s], as it has validation errors',__METHOD__,$this->file)); + + // If we want to keep the packet, we could do that logic here + } elseif (! config('app.packet_keep')) { + Log::debug(sprintf('%s: - Deleting processed packet [%s]',__METHOD__,$this->file)); + unlink($this->file); + } break; - } - foreach ($po->messages as $msg) { - Log::info(sprintf('%s: - Mail from [%s] to [%s]',__METHOD__,$msg->fftn,$msg->tftn)); - - // @todo Quick check that the packet should be processed by us. - // @todo validate that the packet's zone is in the domain. - - // Dispatch job. - ProcessPacket::dispatchSync($msg); - } - - if ($po->hasErrors) { - Log::info(sprintf('%s: - Not deleting packet [%s], as it has validation errors',__METHOD__,$this->file)); - - // If we want to keep the packet, we could do that logic here - } elseif (! config('app.packet_keep')) { - Log::debug(sprintf('%s: - Deleting processed packet [%s]',__METHOD__,$this->file)); - unlink($this->file); - } - - break; - - default: - Log::debug(sprintf('%s: - Leaving file [%s] in the inbound dir',__METHOD__,$this->file)); - } + default: + Log::debug(sprintf('%s: - Leaving file [%s] in the inbound dir',__METHOD__,$this->file)); + } $this->receiving = NULL; }