from PyQt5.QtCore import QUrl, QThread, pyqtSignal, QObject, pyqtSlot, Qt import irc.client import IrcHandler class ircConnectThread(QThread): sig = pyqtSignal(str, str, str) nick_sig = pyqtSignal(str, str) part_sig = pyqtSignal(str, str) join_sig = pyqtSignal(str) topic_sig = pyqtSignal(str, str) namreply_sig = pyqtSignal(str, str) quit_sig = pyqtSignal(str, str) kick_sig = pyqtSignal(str, str, str, str) user_mode_sig = pyqtSignal(str, str, str, str) def __init__(self, handler): QThread.__init__(self) self.reactor = irc.client.Reactor() self.server = self.reactor.server() self.handler = handler def __del__(self): self.wait() def run(self): try: self.c = self.server.connect("irc.blatech.net", 6667, self.handler.get_nick()) except irc.client.ServerConnectionError: print(sys.exc_info()[1]) raise SystemExit(1) # self.sig.connect(IrcWindow.updateTextArea) print("start") self.c.add_global_handler("welcome", self.on_connect) self.c.add_global_handler("pubmsg", self.on_pubmsg) self.c.add_global_handler("privmsg", self.on_privmsg) self.c.add_global_handler("join", self.on_join) self.c.add_global_handler("namreply", self.on_names) self.c.add_global_handler("part", self.on_part) self.c.add_global_handler("ctcp", self.on_ctcp) self.c.add_global_handler("quit", self.on_quit) self.c.add_global_handler("kick", self.on_kick) self.c.add_global_handler("mode", self.on_mode) self.c.add_global_handler("currenttopic", self.on_cur_topic) self.c.add_global_handler("topic", self.on_topic) self.c.add_global_handler("topicinfo", self.on_topic) self.c.add_global_handler("motd", self.on_motd) self.reactor.process_forever() def on_connect(self, connection, event): if irc.client.is_channel("#wtest"): self.join_sig.emit("#wtest") return def on_pubmsg(self, connection, event): print(event) self.sig.emit(event.target, event.source.split("!")[0], event.arguments[0]) def on_privmsg(self, connection, event): self.sig.emit(event.source, "", event.arguments[0]) def on_join(self, connection, event): print(event) nick = event.source.split("!")[0] self.nick_sig.emit(event.target, event.source.split("!")[0]) def on_part(self, connection, event): nick = event.source.split("!")[0] self.part_sig.emit(event.target, event.source.split("!")[0]) def on_names(self, connection, event): print(event) self.namreply_sig.emit(event.arguments[1], event.arguments[2]) def on_ctcp(self, connection, event): source = event.source.split("!")[0] if (event.arguments[0] == "VERSION"): connection.ctcp_reply(source, "VERSION wclient 0.2.0") print(event) def on_quit(self, connection, event): nick = event.source.split("!")[0] print(event) self.quit_sig.emit(nick, event.arguments[0]) def on_kick(self, connection, event): source_nick = event.source.split("!")[0] self.kick_sig.emit(event.arguments[0], event.target, source_nick, event.arguments[1]) def on_mode(self, connection, event): print(event) source_nick = event.source.split("!")[0] self.user_mode_sig.emit(event.target, event.arguments[1], event.arguments[0], source_nick) def on_cur_topic(self, connection, event): self.topic_sig.emit(event.arguments[0], event.arguments[1]) print(event) def on_topic_info(self, connection, event): print(event) def on_topic(self, connection, event): self.topic_sig.emit(event.target, event.arguments[0]) print(event) def on_motd(self, connection, event): print(event) @pyqtSlot(str, str) def send_msg(self, channel, message): if (message.startswith("/join")): self.handle_join(message) else: self.server.privmsg(channel, message) print(message) @pyqtSlot(str) def join_channel(self, channel): print(channel) self.c.join(channel) #TODO: handle errors such as invalid/passworded channel return True