From 586cf1ad9007e5f8572a0f00f662a7406bd05918 Mon Sep 17 00:00:00 2001 From: a-bad-dev <244852891+a-bad-dev@users.noreply.github.com> Date: Thu, 19 Feb 2026 23:29:04 -0400 Subject: [PATCH] Rewrite most of the script (especially the message parser) --- main.py | 180 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 125 insertions(+), 55 deletions(-) diff --git a/main.py b/main.py index 2d3db70..a7712f5 100644 --- a/main.py +++ b/main.py @@ -1,69 +1,139 @@ import os, socket, _thread, time -def main() -> None: - ip = "" - port = 6667 - name = "username_here" - nick = "nick_here" - global chan - chan = "#channel_name_here" - +class IRC2BASH: + ip: str = "" # up to the user to set these values + port: int = 0 + name: str = "" + nick: str = "" + chan: str = "" + + send = lambda string: IRC2BASH.sock.send(f"{string}\r\n".encode("utf-8")) + server = (ip, port) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - - sock.connect((ip, port)) - time.sleep(5) # wait for server to connect - - sock.send(f"USER {name} * * :{nick}\r\n".encode("utf-8")) - time.sleep(1) # wait for server to process - - sock.send(f"NICK {nick}\r\n".encode("utf-8")) - time.sleep(5) # wait for server to process - - sock.send(f"JOIN {chan}\r\n".encode("utf-8")) - time.sleep(1) - _thread.start_new_thread(receive_messages, (sock,)) - while True: - time.sleep(60) - sock.send(f"PING :{ip}\r\n".encode("utf-8")) -def receive_messages(sock) -> None: - while True: - data = sock.recv(1024) - if not data: - break + def main() -> None: + IRC2BASH.sock.connect(IRC2BASH.server) + + time.sleep(5) # wait for server to connect + + IRC2BASH.send(f"USER {IRC2BASH.nick} * * :{IRC2BASH.name}") + time.sleep(1) # wait for server to process + + IRC2BASH.send(f"NICK {IRC2BASH.nick}") + time.sleep(5) # wait for server to process + + IRC2BASH.send(f"JOIN {IRC2BASH.chan}") + time.sleep(1) + + _thread.start_new_thread(IRC2BASH.receive_messages, (IRC2BASH.sock,)) + + while True: # ping the server every minute so the connection stays alive + time.sleep(60) + IRC2BASH.send(f"PING :{IRC2BASH.ip}") + + def receive_messages(sock) -> None: + while True: + data = sock.recv(1024) + if not data: + break + + response = data.decode("utf-8") + response = response.strip() + response = response[1:] + response = response.split(" ") + + # + # attempt to find a username in part #1 + # - response = data.decode("utf-8") - print(response) - if "PRIVMSG" in response and chan in response: try: - command = response.split(" ", 3)[3][1:] + username = response[0] + username = username.split("!") + username = username[0] + except Exception: + username = "" + + # + # attempt to find the channel name + # + + try: + if response[1] == "PRIVMSG" and response[2] == IRC2BASH.chan: # correct channel + channel = response[2] + else: + channel = "" + except Exception: + channel = "" + + # + # attempt to parse the actual message + # + + try: + message = response[3:] + parsed_message = "" + for word in message: + parsed_message = f"{parsed_message} {word}" + + message = parsed_message + parsed_message = None + + message = message.strip() + message = message[1:] + except Exception: + message = "" + + # + # see if the message is a command + # + + try: + if message.startswith("$ "): + command = message[2:] + else: + command = "" except Exception: command = "" - if command.startswith("$"): - try: - command = command.split(" ", 1)[1] - except Exception: - command = "" - - command = f"/bin/bash -c \"{command.strip()}\" >/tmp/.command 2>&1" - os.system(command) + # + # we now (probably) have the command :D, time to run it + # + + if username != "" and channel != "" and message != "" and command != "": + + # + # escape quotation marks and backslashes + # + + command_list = [] + for letter in command: + match letter: + case "\\": + command_list.append("\\\\") + + case "\"": + command_list.append("\\\"") + + case _: + command_list.append(letter) + + command = "" + for letter in command_list: + command = f"{command}{letter}" + + command_list = None + + print(f"{channel}: <{username}> {command}") + os.system(f"/bin/bash -c \"{command}\" >/tmp/.command 2>&1") + with open("/tmp/.command", "r") as f: output = f.read() - + + output = output.strip().split("\n") print(output) - for line in output.split("\n"): - sock.send(f"PRIVMSG {chan} :{line}\r\n".encode("utf-8")) - time.sleep(0.5) - - else: - print("message not ok") - -def send_ping(sock, server) -> None: - while True: - time.sleep(60) - sock.send(f"PING :{server}\r\n".encode("utf-8")) - + for line in output: + IRC2BASH.send(f"PRIVMSG {IRC2BASH.chan} :{line}") + time.sleep(0.75) if __name__ == "__main__": - main() + IRC2BASH.main()