From 0457b3df25399ad57bf8a3651681188d426e0a95 Mon Sep 17 00:00:00 2001 From: Deon George Date: Mon, 13 May 2024 18:55:39 +1000 Subject: [PATCH] Resorting methods, no functional changes --- app/Classes/FTN.php | 14 --- app/Classes/FTN/Message.php | 188 ++++++++++++++++++++---------------- 2 files changed, 104 insertions(+), 98 deletions(-) diff --git a/app/Classes/FTN.php b/app/Classes/FTN.php index 509a390..c38d051 100644 --- a/app/Classes/FTN.php +++ b/app/Classes/FTN.php @@ -37,20 +37,6 @@ abstract class FTN } } - /** - * Determine if a line is a kludge line. - * - * @param string $kludge - * @param string $string - * @return string - */ - protected function kludge(string $kludge,string $string) - { - return (preg_match("/^{$kludge}/",$string)) - ? chop(preg_replace("/^{$kludge}/",'',$string),"\r") - : FALSE; - } - /** * This function creates our unpack header * diff --git a/app/Classes/FTN/Message.php b/app/Classes/FTN/Message.php index 5fad2e0..92e39c3 100644 --- a/app/Classes/FTN/Message.php +++ b/app/Classes/FTN/Message.php @@ -141,6 +141,7 @@ class Message extends FTNBase private Collection $via; // The path the message has gone using Via lines (Netmail) private Collection $unknown; // Temporarily hold attributes we have no logic for. + /** @deprecated Not sure why this is needed? */ public bool $packed = FALSE; // Has the message been packed successfully // Convert characters into printable chars @@ -189,6 +190,94 @@ class Message extends FTNBase 0xfc => 0x207f, 0xfd => 0x00b2, 0xfe => 0x25a0, 0xff => 0x00a0, ]; + /** + * Parse a message from a packet + * + * @param string $msg + * @param Zone|null $zone + * @return Message + * @throws \Exception + */ + public static function parseMessage(string $msg,Zone $zone=NULL): self + { + Log::info(sprintf('%s:= Processing message [%d] bytes from zone [%d]',self::LOGKEY,strlen($msg),$zone?->zone_id)); + + $o = new self($zone); + $o->dump = $msg; + + try { + $o->header = unpack(self::unpackheader(self::header),substr($msg,0,self::HEADER_LEN)); + + } catch (\Exception $e) { + Log::error(sprintf('%s:! Error bad packet header',self::LOGKEY),['e'=>$e->getMessage(),'header'=>substr($msg,0,self::HEADER_LEN)]); + $validator = Validator::make([ + 'header' => substr($msg,0,self::HEADER_LEN), + ],[ + 'header' => [function ($attribute,$value,$fail) use ($e) { return $fail($e->getMessage()); }] + ]); + + if ($validator->fails()) + $o->errors = $validator; + + return $o; + } + + $ptr = 0; + + // To User + $o->user_to = strstr(substr($msg,self::HEADER_LEN+$ptr),"\x00",TRUE); + $ptr += strlen($o->user_to)+1; + + // From User + $o->user_from = strstr(substr($msg,self::HEADER_LEN+$ptr),"\x00",TRUE); + $ptr += strlen($o->user_from)+1; + + // Subject + $o->subject = strstr(substr($msg,self::HEADER_LEN+$ptr),"\x00",TRUE); + $ptr += strlen($o->subject)+1; + + // Check if this is an Echomail + if (! strncmp(substr($msg,self::HEADER_LEN+$ptr),'AREA:',5)) { + $o->echoarea = strtoupper(substr($msg,self::HEADER_LEN+$ptr+5,strpos($msg,"\r",self::HEADER_LEN+$ptr+5)-(self::HEADER_LEN+$ptr+5))); + $ptr += strlen($o->echoarea)+5+1; + } + + $o->unpackMessage(substr($msg,self::HEADER_LEN+$ptr)); + + if (($x=$o->validate())->fails()) { + Log::debug(sprintf('%s:! Message fails validation (%s@%s->%s@%s)',self::LOGKEY,$o->user_from,$o->fftn,$o->user_to,$o->tftn),['result'=>$x->errors()]); + //throw new \Exception('Message validation fails:'.join(' ',$x->errors()->all())); + } + + return $o; + } + + /** + * Translate the string into something printable via the web + * + * @param string $string + * @param array $skip + * @return string + */ + public static function tr(string $string,array $skip=[0x0a,0x0d]): string + { + $tr = []; + + foreach (self::CP437 as $k=>$v) { + if (in_array($k,$skip)) + continue; + + $tr[chr($k)] = '&#'.$v; + } + + return strtr($string,$tr); + } + + /** + * Packets have no concept of a zone, so we add it for any calculations that could use it + * + * @param Zone|NULL $zone + */ public function __construct(Zone $zone=NULL) { $this->zone = $zone; @@ -508,6 +597,20 @@ class Message extends FTNBase $this->decode($values); } + /** + * Determine if a line is a kludge line. + * + * @param string $kludge + * @param string $string + * @return string + */ + private function kludge(string $kludge,string $string) + { + return (preg_match("/^{$kludge}/",$string)) + ? chop(preg_replace("/^{$kludge}/",'',$string),"\r") + : FALSE; + } + /** * Reduce our PATH/SEEN-BY for messages as per FSC-0068 * @@ -517,7 +620,7 @@ class Message extends FTNBase * @param string $delim * @return string */ - function optimise_path(Collection $path,string $prefix,int $len=79,string $delim="\r"): string + private function optimise_path(Collection $path,string $prefix,int $len=79,string $delim="\r"): string { $cur = NULL; $result = $prefix; @@ -546,89 +649,6 @@ class Message extends FTNBase return $result; } - /** - * Parse a message from a packet - * - * @param string $msg - * @param Zone|null $zone - * @return Message - * @throws \Exception - */ - public static function parseMessage(string $msg,Zone $zone=NULL): self - { - Log::info(sprintf('%s:= Processing message [%d] bytes from zone [%d]',self::LOGKEY,strlen($msg),$zone?->zone_id)); - - $o = new self($zone); - $o->dump = $msg; - - try { - $o->header = unpack(self::unpackheader(self::header),substr($msg,0,self::HEADER_LEN)); - - } catch (\Exception $e) { - Log::error(sprintf('%s:! Error bad packet header',self::LOGKEY),['e'=>$e->getMessage(),'header'=>substr($msg,0,self::HEADER_LEN)]); - $validator = Validator::make([ - 'header' => substr($msg,0,self::HEADER_LEN), - ],[ - 'header' => [function ($attribute,$value,$fail) use ($e) { return $fail($e->getMessage()); }] - ]); - - if ($validator->fails()) - $o->errors = $validator; - - return $o; - } - - $ptr = 0; - - // To User - $o->user_to = strstr(substr($msg,self::HEADER_LEN+$ptr),"\x00",TRUE); - $ptr += strlen($o->user_to)+1; - - // From User - $o->user_from = strstr(substr($msg,self::HEADER_LEN+$ptr),"\x00",TRUE); - $ptr += strlen($o->user_from)+1; - - // Subject - $o->subject = strstr(substr($msg,self::HEADER_LEN+$ptr),"\x00",TRUE); - $ptr += strlen($o->subject)+1; - - // Check if this is an Echomail - if (! strncmp(substr($msg,self::HEADER_LEN+$ptr),'AREA:',5)) { - $o->echoarea = strtoupper(substr($msg,self::HEADER_LEN+$ptr+5,strpos($msg,"\r",self::HEADER_LEN+$ptr+5)-(self::HEADER_LEN+$ptr+5))); - $ptr += strlen($o->echoarea)+5+1; - } - - $o->unpackMessage(substr($msg,self::HEADER_LEN+$ptr)); - - if (($x=$o->validate())->fails()) { - Log::debug(sprintf('%s:! Message fails validation (%s@%s->%s@%s)',self::LOGKEY,$o->user_from,$o->fftn,$o->user_to,$o->tftn),['result'=>$x->errors()]); - //throw new \Exception('Message validation fails:'.join(' ',$x->errors()->all())); - } - - return $o; - } - - /** - * Translate the string into something printable via the web - * - * @param string $string - * @param array $skip - * @return string - */ - public static function tr(string $string,array $skip=[0x0a,0x0d]): string - { - $tr = []; - - foreach (self::CP437 as $k=>$v) { - if (in_array($k,$skip)) - continue; - - $tr[chr($k)] = '&#'.$v; - } - - return strtr($string,$tr); - } - /** * If this message doesnt have an AREATAG, then its a netmail. *