From cb5d49ff5a1ebdd6886c85fce3e22a5c18814669 Mon Sep 17 00:00:00 2001 From: mio Date: Sat, 13 Oct 2018 17:18:47 +0000 Subject: [PATCH] Check for ping timeout and exit --- itte.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/itte.py b/itte.py index 5b06959..6c62a07 100644 --- a/itte.py +++ b/itte.py @@ -1,3 +1,4 @@ +import select import socket import yaml from time import sleep @@ -45,7 +46,7 @@ class IRC: while 1: sleep(1) data = self.receive() - self.keepalive(data) + self.keepalive(data, self.bot_nick) self.msg = self.parse(data, self.req_prefix) for c in self.channels: # Pass in a context dict for handlers @@ -57,7 +58,7 @@ class IRC: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect(server) except ConnectionError as err: - exit("[debug] " + str(err)) + exit("[debug][err] " + str(err)) self.send("USER", bot_nick + " " + bot_nick + " " + \ bot_nick + " " + bot_nick) self.send("NICK", bot_nick) @@ -70,13 +71,17 @@ class IRC: # disconnect also exits the app exit("Shutting down ...") - def keepalive(self, line): + def keepalive(self, line, bot_nick): """Stay connected to a server by responding to server pings.""" - if line.split(" ")[0]) == "PING": + if line.split(" ")[0] == "PING": resp = line.replace("PING", "PONG", 1) if self.debug: print("[debug][send] " + resp) self.sock.sendall(bytes(resp + "\r\n", "utf-8")) + # Fallback to ensure process exits if timeout occurs + elif (bot_nick in line.split(" ")[0]) and \ + ("QUIT :Ping timeout:" in line): + exit("[debug][err] Ping timeout, exited.") def join_channels(self, channels): """Join channels given a list of channel names."""