diff options
author | Joe Robinson <joe@lc8n.com> | 2017-04-14 19:07:16 +0200 |
---|---|---|
committer | Joe Robinson <joe@lc8n.com> | 2017-04-14 19:07:16 +0200 |
commit | a75725425aaa29ad26d7f1225ec7e4e6c55a640d (patch) | |
tree | 27f3f2ca1b8c44bef27b30a278f6f83c37996abe /IrcConnection.py |
Initial commit, basic connection and message handling, some broken old code
Diffstat (limited to 'IrcConnection.py')
-rw-r--r-- | IrcConnection.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/IrcConnection.py b/IrcConnection.py new file mode 100644 index 0000000..e51e4e3 --- /dev/null +++ b/IrcConnection.py @@ -0,0 +1,65 @@ +from PyQt5.QtCore import QUrl, QThread, pyqtSignal, QObject, pyqtSlot, Qt +import irc.client + +class ircConnectThread(QThread): + sig = pyqtSignal(str) + nick_sig = pyqtSignal(str, str) + join_sig = pyqtSignal(str) + def __init__(self): + QThread.__init__(self) + self.reactor = irc.client.Reactor() + self.server = self.reactor.server() + + def __del__(self): + self.wait() + + def run(self): + try: + self.c = self.server.connect("irc.blatech.net", 6667, "wtest") + 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.reactor.process_forever() + + def on_connect(self, connection, event): + if irc.client.is_channel("#wtest"): + connection.join("#wtest") + return + def on_pubmsg(self, connection, event): + self.sig.emit(event.source + ": " + 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]) + self.sig.emit(event.source + " joined " + event.target) + + def on_names(self, connection, event): + print(event) + for nick in event.arguments[2].split(" "): + if (nick.strip() != ""): + self.nick_sig.emit(event.arguments[1], nick) + + @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) + + def handle_join(self, message): + print(message) + self.c.join(message.split(" ")[1]) + self.join_sig.emit(message.split(" ")[1]) |