Fix invalid UTF-8 bytes in output crashing the script

This commit is contained in:
a-bad-dev 2026-02-20 23:28:56 -04:00
commit 216afac3f2

23
main.py
View file

@ -106,6 +106,7 @@ class IRC2BASH:
# #
command_list = [] command_list = []
command_noescapes = command
for letter in command: for letter in command:
match letter: match letter:
case "\\": case "\\":
@ -126,15 +127,19 @@ class IRC2BASH:
print(f"{channel}: <{username}> {command}") print(f"{channel}: <{username}> {command}")
os.system(f"/bin/bash -c \"{command}\" >/tmp/.command 2>&1") os.system(f"/bin/bash -c \"{command}\" >/tmp/.command 2>&1")
with open("/tmp/.command", "r") as f: try:
output = f.read() with open("/tmp/.command", "r") as f:
output = f.read()
output = output.strip() output = output.strip()
print(output) print(output)
output = output.split("\n") output = output.split("\n")
for line in output: for line in output:
IRC2BASH.send(f"PRIVMSG {IRC2BASH.chan} :{line}") IRC2BASH.send(f"PRIVMSG {IRC2BASH.chan} :{line}")
time.sleep(0.75) time.sleep(0.75)
except UnicodeDecodeError:
print(f"UnicodeDecodeError: invalid bytes, caused by: {command_noescapes}")
IRC2BASH.send(f"PRIVMSG {IRC2BASH.chan} :Invalid bytes in response.")
if __name__ == "__main__": if __name__ == "__main__":
IRC2BASH.main() IRC2BASH.main()