from PyQt5.QtCore import QUrl, QThread, pyqtSignal, QObject, pyqtSlot, Qt import irc.client import IrcHandler class ircConnectThread(QThread): sig = pyqtSignal(str, str, str) join_sig = pyqtSignal(str, str) part_sig = pyqtSignal(str, str) nick_sig = pyqtSignal(str, 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) error_sig = pyqtSignal(str, str) status_sig = pyqtSignal(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()) self.c.connection_complete = False 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("nick", self.on_nick) self.c.add_global_handler("motd", self.on_motd) self.c.add_global_handler("motdstart", self.on_motd) self.c.add_global_handler("nicknameinuse", self.on_nicknameinuse) self.c.add_global_handler("welcome", self.on_connectmsg) self.c.add_global_handler("yourhost", self.on_connectmsg) self.c.add_global_handler("created", self.on_connectmsg) self.c.add_global_handler("myinfo", self.on_connectmsg) self.c.add_global_handler("featurelist", self.on_connectmsg) self.c.add_global_handler("luserconns", self.on_connectmsg) self.c.add_global_handler("luserclient", self.on_connectmsg) self.c.add_global_handler("luserop", self.on_connectmsg) self.c.add_global_handler("luserunknown", self.on_connectmsg) self.c.add_global_handler("luserchannels", self.on_connectmsg) self.c.add_global_handler("luserme", self.on_connectmsg) self.reactor.process_forever() def on_connect(self, connection, event): print("connected") self.c.connection_complete = True if irc.client.is_channel("#wtest"): # self.join_sig.emit("#wtest") return def on_connectmsg(self, connection, event): print(event) status_msg = "* " for status in event.arguments: status_msg = status_msg + status + " " self.status_sig.emit(status_msg) 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.join_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_nick(self, connection, event): source_nick = event.source.split("!")[0] self.nick_sig.emit(source_nick, event.target) print(event) def on_motd(self, connection, event): self.status_sig.emit(event.arguments[0]) print(event) def on_nicknameinuse(self, connection, event): print(event) if (self.c.connection_complete): self.error_sig.emit("Nick '" + event.arguments[0] + "' is already in use", "") self.handler.set_nick(event.target) else: self.error_sig.emit("Nick '" + event.arguments[0] + "' is already in use, changing to " + connection.get_nickname() + "_", "") self.handler.set_nick(connection.get_nickname() + "_") connection.nick(connection.get_nickname() + "_") @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 change_nick(self, new_nick): self.handler.set_nick(new_nick) self.c.nick(new_nick) #TODO: handle errors such as invalid/passworded channel return True @pyqtSlot(str) def join_channel(self, channel): print(channel) self.c.join(channel) #TODO: handle errors such as invalid/passworded channel return True @pyqtSlot(str) def part_channel(self, channel): print(channel) self.c.part(channel) #TODO: handle errors such as invalid/passworded channel return True