import socket import time import sys class IRC: def __init__(self, channel, code): self.channel = channel self.sock = socket.socket() self.code = code self.sender = {"ip":"", "nickname":""} self.data = {"type":""} def connect(self, host, nick): self.sock.connect(host) self.sock.send(f"USER {nick} 0 0 {nick}\r\n".encode(self.code)) self.sock.send(f"NICK {nick}\r\n".encode(self.code)) time.sleep(7) self.sock.send(f"JOIN {self.channel}\r\n".encode(self.code)) def recv(self, size): try: data = self.sock.recv(size).decode() except UnicodeDecodeError: return None if len(data) == 0: self.sock.close() raise BrokenPipeError("Disconnected") if "PING" in data.split(":")[0]: self.sock.send(f"PONG {data.split()[-1]}\r\n".encode()) return data def parser(self, data): data = data.split(" ", 3) self.sender["ip"] = data[0].split("@", 1)[-1] self.sender["nickname"] = data[0].split("!", 1)[0][1:] if data[1] == "PRIVMSG": self.data["channel"] = data[2] self.data["msg"] = data[-1][1:].strip() self.data["type"] = "PRIVMSG" elif data[1] == "JOIN": self.data["type"] = "JOIN" elif data[1] == "PART": self.data["type"] = "PART" def sendMSG(self, msg, channel): self.sock.send(f"PRIVMSG {channel} :{msg}\r\n".encode(self.code)) def sendRAW(self, msg): self.sock.send(f"{msg}\r\n".encode(self.code))