Be nicer about zero-length strings in Messages

This commit is contained in:
Salad Dais
2023-12-31 15:52:15 +00:00
parent 5ad8ee986f
commit 167673aa08
3 changed files with 18 additions and 6 deletions

View File

@@ -19,7 +19,7 @@ async def amain():
return
if message["ChatData"]["SourceType"] != ChatSourceType.AGENT:
return
if "hello" not in str(message["ChatData"]["Message"]).lower():
if "hello" not in message["ChatData"]["Message"].lower():
return
await client.send_chat(f'Hello {message["ChatData"]["FromName"]}!', chat_type=ChatType.SHOUT)

View File

@@ -317,6 +317,12 @@ class JankStringyBytes(bytes):
return item in str(self)
return item in bytes(self)
def lower(self):
return str(self).lower()
def upper(self):
return str(self).upper()
class RawBytes(bytes):
__slots__ = ()

View File

@@ -220,11 +220,17 @@ class UDPMessageDeserializer:
if tmpl_variable.probably_binary:
return unpacked_data
# Truncated strings need to be treated carefully
if tmpl_variable.probably_text and unpacked_data.endswith(b"\x00"):
try:
return unpacked_data.decode("utf8").rstrip("\x00")
except UnicodeDecodeError:
return JankStringyBytes(unpacked_data)
if tmpl_variable.probably_text:
# If it has a null terminator, let's try to decode it first.
# We don't want to do this if there isn't one, because that may change
# the meaning of the data.
if unpacked_data.endswith(b"\x00"):
try:
return unpacked_data.decode("utf8").rstrip("\x00")
except UnicodeDecodeError:
pass
# Failed, return jank stringy bytes
return JankStringyBytes(unpacked_data)
elif tmpl_variable.type in {MsgType.MVT_FIXED, MsgType.MVT_VARIABLE}:
# No idea if this should be bytes or a string... make an object that's sort of both.
return JankStringyBytes(unpacked_data)