Minor cosmetic changes, show hex_dump when data purged from protocol stream

This commit is contained in:
Deon George
2022-02-13 10:24:31 +11:00
parent f142284a2b
commit f216f42917
3 changed files with 109 additions and 111 deletions

View File

@@ -211,87 +211,6 @@ class Message extends FTNBase
$this->unknown = collect();
}
/**
* 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);
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));
$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 = 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);
}
public function __get($key)
{
switch ($key) {
@@ -403,6 +322,16 @@ class Message extends FTNBase
}
}
/**
* When we serialise this object, we'll need to utf8_encode some values
*
* @return array
*/
public function __serialize(): array
{
return $this->encode();
}
public function __set($key,$value)
{
switch ($key) {
@@ -438,26 +367,6 @@ class Message extends FTNBase
}
}
/**
* When we serialise this object, we'll need to utf8_encode some values
*
* @return array
*/
public function __serialize(): array
{
return $this->encode();
}
/**
* When we unserialize, we'll restore (utf8_decode) some values
*
* @param array $values
*/
public function __unserialize(array $values): void
{
$this->decode($values);
}
/**
* Export an FTN message, ready for sending.
*
@@ -530,6 +439,97 @@ class Message extends FTNBase
return $return;
}
/**
* When we unserialize, we'll restore (utf8_decode) some values
*
* @param array $values
*/
public function __unserialize(array $values): void
{
$this->decode($values);
}
/**
* 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);
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));
$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 = 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.
*