From 40d38f30461863f7378df01819aa1002bd6d760a Mon Sep 17 00:00:00 2001 From: Firepup Sixfifty Date: Fri, 3 Nov 2023 16:40:57 -0500 Subject: [PATCH] add message queue, fix a bug in joining channels, and don't respond to fake PINGs. --- ircbot.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/ircbot.py b/ircbot.py index 0a9b6f8..085ba45 100644 --- a/ircbot.py +++ b/ircbot.py @@ -89,6 +89,7 @@ if __name__ == "__main__": np = re.compile( npbase.replace("MAX", f"{nicklen}") ) + queue = [] log(f"Start init for {server}") npallowed = ["FireBitBot"] ESCAPE_SEQUENCE_RE = re.compile( @@ -119,7 +120,14 @@ def send(command: str, encoding: str = "UTF-8") -> int: def recv() -> bytes: - return bytes(ircsock.recv(2048).strip(b"\r\n")) + global queue + if queue: + return bytes(queue.pop(0)) + data = bytes(ircsock.recv(2048).strip(b"\r\n")) + if b'\r\n' in data: + queue.extend(data.split(b'\r\n')) + return bytes(queue.pop(0)) + return data def ping(ircmsg: str) -> int: @@ -206,7 +214,7 @@ def joinserver(): botnick = f"{botnick}{r.randint(0,1000)}" send(f"NICK {botnick}\n") log(f"botnick is now {botnick}") - if ircmsg.find("PING :") != -1: + if ircmsg.startswith("PING "): # pong = "PONG :" + input("Ping?:") + "\n" # pong = pong.replace("\\\\", "\\") ping(ircmsg) @@ -247,8 +255,8 @@ def joinchan(chan: str, origin: str, chanList: dict, lock: bool = True): ircmsg = recv().decode() if ircmsg != "": print(bytes(ircmsg).lazy_decode()) - if ircmsg.find("PING :") != -1: - ping() + if ircmsg.startswith("PING "): + ping(ircmsg) if len(ircmsg.split("\x01")) == 3: CTCPHandler(ircmsg, isRaw=True) if ircmsg.find("No such channel") != -1: