From 2c1ab88bbdbf4c67d679b6aed6025a4787fd6f4b Mon Sep 17 00:00:00 2001 From: Deon George Date: Tue, 7 Jan 2025 14:55:57 +1100 Subject: [PATCH] Improvements to working out region for new addresses, Bounce netmails to a invalid address (that we would host) --- app/Classes/FTN/Packet.php | 25 +++++-- app/Console/Commands/PacketInfo.php | 2 +- app/Jobs/PacketProcess.php | 20 ++++-- app/Models/Address.php | 5 +- app/Notifications/Netmails.php | 2 +- .../Netmails/NetmailBadAddress.php | 2 +- .../Netmails/NetmailNoDestination.php | 65 +++++++++++++++++++ resources/views/pkt.blade.php | 2 +- 8 files changed, 107 insertions(+), 16 deletions(-) create mode 100644 app/Notifications/Netmails/NetmailNoDestination.php diff --git a/app/Classes/FTN/Packet.php b/app/Classes/FTN/Packet.php index a730208..3798bd2 100644 --- a/app/Classes/FTN/Packet.php +++ b/app/Classes/FTN/Packet.php @@ -189,7 +189,7 @@ abstract class Packet extends FTNBase implements \Iterator, \Countable $msgbuf = substr($msgbuf,$end+3); continue; - // If we have more to read + // If we have more to read } elseif ($read_ptr < $size) { continue; } @@ -430,10 +430,27 @@ abstract class Packet extends FTNBase implements \Iterator, \Countable // If the $msg->tftn doesnt exist, we'll need to create it if ($msg->errors->has('to') && $this->tftn && $this->tftn->zone_id) { - Log::debug(sprintf('%s:^ To address [%s] doesnt exist, it needs to be created',self::LOGKEY,$msg->set->get('set_tftn'))); - $ao = Address::findFTN($msg->set->get('set_tftn'),TRUE,TRUE); + // If this is a netmail message, to a non existant address, we need to bounce it + if (($msg instanceof Netmail)) { + if ((! $ao) && our_address()->contains(Address::newFTN($msg->set_tftn)?->parent())) { + Log::alert(sprintf('%s:^ To address [%s] doesnt exist, netmail will be bounced',self::LOGKEY,$msg->set->get('set_tftn'))); + + $this->messages->push($msg); + return; + + // If this is a netmail message, to a non existant address, we need to bounce it + } elseif ($ao && (! $ao->active) && our_address()->contains($ao->parent())) { + Log::alert(sprintf('%s:^ To address [%s] isnt active, netmail will be bounced',self::LOGKEY,$msg->set->get('set_tftn'))); + + $this->messages->push($msg); + return; + } + } + + Log::debug(sprintf('%s:^ To address [%s] doesnt exist, it needs to be created',self::LOGKEY,$msg->set->get('set_tftn'))); + if ($ao?->exists && ($ao->zone?->domain_id !== $this->tftn->zone->domain_id)) { Log::alert(sprintf('%s:! To address [%s] domain [%d] doesnt match packet domain [%d]?',self::LOGKEY,$msg->set->get('set_tftn'),$ao->zone?->domain_id,$this->fftn->zone->domain_id)); @@ -442,7 +459,7 @@ abstract class Packet extends FTNBase implements \Iterator, \Countable if (! $ao) { $so = System::createUnknownSystem(); - $ao = Address::createFTN($msg->set->get('set_fftn'),$so); + $ao = Address::createFTN($msg->set->get('set_tftn'),$so); Log::alert(sprintf('%s:- To FTN [%s] is not defined, created new entry for (%d)',self::LOGKEY,$msg->set->get('set_tftn'),$ao->id)); } diff --git a/app/Console/Commands/PacketInfo.php b/app/Console/Commands/PacketInfo.php index a1babd8..eca8926 100644 --- a/app/Console/Commands/PacketInfo.php +++ b/app/Console/Commands/PacketInfo.php @@ -77,7 +77,7 @@ class PacketInfo extends Command if ($msg instanceof Echomail) $this->warn(sprintf(' - To : %s',$msg->to)); else - $this->warn(sprintf(' - To : %s (%s)',$msg->to,$msg->tftn->ftn)); + $this->warn(sprintf(' - To : %s (%s)',$msg->to,$msg->tftn?->ftn ?: $msg->set_tftn)); $this->warn(sprintf(' - Subject: %s',$msg->subject)); if ($msg instanceof Echomail) $this->warn(sprintf(' - Area : %s',$msg->echoarea->name)); diff --git a/app/Jobs/PacketProcess.php b/app/Jobs/PacketProcess.php index b9e3e50..ba185a5 100644 --- a/app/Jobs/PacketProcess.php +++ b/app/Jobs/PacketProcess.php @@ -17,8 +17,8 @@ use League\Flysystem\UnableToMoveFile; use App\Classes\File; use App\Classes\FTN\Packet; use App\Exceptions\InvalidPacketException; -use App\Models\{Echomail,Netmail,System}; -use App\Notifications\Netmails\{PacketPasswordInvalid,UnexpectedPacketFromYou}; +use App\Models\{Address,Echomail,Netmail,System}; +use App\Notifications\Netmails\{NetmailNoDestination,PacketPasswordInvalid,UnexpectedPacketFromYou}; class PacketProcess implements ShouldQueue { @@ -119,10 +119,20 @@ class PacketProcess implements ShouldQueue $count = 0; foreach ($pkt as $msg) { - if ($msg instanceof Netmail) - Log::info(sprintf('%s:- Netmail from [%s] to [%s]',self::LOGKEY,$msg->fftn->ftn,$msg->tftn->ftn)); + if ($msg instanceof Netmail) { + Log::info(sprintf('%s:- Netmail from [%s] to [%s]',self::LOGKEY,$msg->fftn->ftn,$msg->tftn?->ftn ?: $msg->set_tftn)); - elseif ($msg instanceof Echomail) { + // If we dont have a destination, we need to bounce it, if we would be the parent of the address + if ((! $msg->tftn) && our_address()->contains(Address::newFTN($msg->set_tftn)?->parent())) { + Log::alert(sprintf('%s:! Netmail destination [%s] doesnt exist, bouncing back to [%s]',self::LOGKEY,$msg->set_tftn,$pkt->fftn->ftn)); + + Notification::route('netmail',$msg->fftn)->notify(new NetmailNoDestination($msg)); + $count++; + + continue; + } + + } elseif ($msg instanceof Echomail) { Log::info(sprintf('%s:- Echomail from [%s]',self::LOGKEY,$msg->fftn->ftn)); if ($netmail_only) { diff --git a/app/Models/Address.php b/app/Models/Address.php index a1d5728..4605919 100644 --- a/app/Models/Address.php +++ b/app/Models/Address.php @@ -355,7 +355,6 @@ class Address extends Model // Work out region $region_id = NULL; $zone_id = NULL; - // We can only work out region/zone if we have a domain - this is for 2D parsing if ($matches[5] ?? NULL) { $o = new self; @@ -1357,7 +1356,7 @@ class Address extends Model case self::NODE_HC: // NC return self::active() ->where('zone_id',$this->zone_id) - ->where('region_id',$this->region_id) + ->when($this->region_id,fn($q)=>$q->where('region_id',$this->region_id)) ->where('host_id',$this->host_id) ->where('node_id',0) ->where('point_id',0) @@ -1393,7 +1392,7 @@ class Address extends Model */ private function session(string $type): ?string { - return ($this->exists && ($x=$this->system->sessions->where('id',$this->zone_id)->first())) ? ($x->pivot->{$type} ?: '') : NULL; + return ($x=$this->system?->sessions->where('id',$this->zone_id)->first()) ? ($x->pivot->{$type} ?: '') : NULL; } /** diff --git a/app/Notifications/Netmails.php b/app/Notifications/Netmails.php index 6d6184f..9cda225 100644 --- a/app/Notifications/Netmails.php +++ b/app/Notifications/Netmails.php @@ -72,7 +72,7 @@ abstract class Netmails extends Notification //implements ShouldQueue protected function sourceSummary(Echomail|Netmail $o,string $item=NULL): string { - return sprintf("The %s was received here on [%s] and it looks like your system sent it on [%s].", + return sprintf("The %s was processed here on [%s] and it looks like your system sent it on [%s].", $item ?: sprintf('%s with ID [%s] to [%s]',$o instanceof Netmail ? 'Netmail' : 'Echomail',$o->msgid,$o->to), Carbon::now()->utc()->toDateTimeString(), $o->date->utc()->toDateTimeString(), diff --git a/app/Notifications/Netmails/NetmailBadAddress.php b/app/Notifications/Netmails/NetmailBadAddress.php index ea781b3..78a2bc6 100644 --- a/app/Notifications/Netmails/NetmailBadAddress.php +++ b/app/Notifications/Netmails/NetmailBadAddress.php @@ -5,7 +5,7 @@ namespace App\Notifications\Netmails; use Illuminate\Support\Facades\Log; use App\Notifications\Netmails; -use App\Models\{Echomail,Netmail}; +use App\Models\Netmail; use App\Traits\{MessagePath,PageTemplate}; class NetmailBadAddress extends Netmails diff --git a/app/Notifications/Netmails/NetmailNoDestination.php b/app/Notifications/Netmails/NetmailNoDestination.php new file mode 100644 index 0000000..3836e07 --- /dev/null +++ b/app/Notifications/Netmails/NetmailNoDestination.php @@ -0,0 +1,65 @@ +mo = $mo; + } + + /** + * 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:+ Creating NETMAIL NO DESTINATION netmail to [%s]',self::LOGKEY,$ao->ftn)); + + $o->subject = sprintf('Bad destination for netmail [%s]',$this->mo->msgid); + + // Message + $msg = $this->page(FALSE,'nodest'); + + $msg->addText($this->sourceSummary($this->mo)."\r\r"); + + $msg->addText(sprintf("The TO address [%s] in this netmail doesnt exist for the domain [%s].\r\r",$this->mo->get_tftn,$ao->zone->domain->name)); + + $msg->addText("This netmail has been rejected and not stored here - so no downstream node will receive it. If you think this is a mistake, please let me know.\r\r"); + + $msg->addText($this->message_path($this->mo)); + + $o->msg = $msg->render(); + $o->set_tagline = 'I enjoyed reading your message, even though nobody else will get it :)'; + + $o->save(); + + return $o; + } +} \ No newline at end of file diff --git a/resources/views/pkt.blade.php b/resources/views/pkt.blade.php index 62d91b2..95fe4d1 100644 --- a/resources/views/pkt.blade.php +++ b/resources/views/pkt.blade.php @@ -115,7 +115,7 @@ FROM: {!! Message::tr($msg->from) !!} ({{ $msg->fftn ? $msg->fftn->ftn : $msg->fftn_t}})
- TO: {!! Message::tr($msg->to) !!}@if($msg instanceof Netmail) ({{ $msg->tftn ? $msg->tftn->ftn : $msg->tftn_t }}) @endif + TO: {!! Message::tr($msg->to) !!}@if($msg instanceof Netmail) ({{ $msg->tftn ? $msg->tftn->ftn : ($msg->tftn_t ?: $msg->set_tftn)}}) @endif