Check for ping timeout and exit

trunk
mio 2018-10-13 17:18:47 +00:00
parent 143e5b91af
commit cb5d49ff5a
1 changed files with 9 additions and 4 deletions

13
itte.py
View File

@ -1,3 +1,4 @@
import select
import socket import socket
import yaml import yaml
from time import sleep from time import sleep
@ -45,7 +46,7 @@ class IRC:
while 1: while 1:
sleep(1) sleep(1)
data = self.receive() data = self.receive()
self.keepalive(data) self.keepalive(data, self.bot_nick)
self.msg = self.parse(data, self.req_prefix) self.msg = self.parse(data, self.req_prefix)
for c in self.channels: for c in self.channels:
# Pass in a context dict for handlers # 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 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(server) self.sock.connect(server)
except ConnectionError as err: except ConnectionError as err:
exit("[debug] " + str(err)) exit("[debug][err] " + str(err))
self.send("USER", bot_nick + " " + bot_nick + " " + \ self.send("USER", bot_nick + " " + bot_nick + " " + \
bot_nick + " " + bot_nick) bot_nick + " " + bot_nick)
self.send("NICK", bot_nick) self.send("NICK", bot_nick)
@ -70,13 +71,17 @@ class IRC:
# disconnect also exits the app # disconnect also exits the app
exit("Shutting down ...") exit("Shutting down ...")
def keepalive(self, line): def keepalive(self, line, bot_nick):
"""Stay connected to a server by responding to server pings.""" """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) resp = line.replace("PING", "PONG", 1)
if self.debug: if self.debug:
print("[debug][send] " + resp) print("[debug][send] " + resp)
self.sock.sendall(bytes(resp + "\r\n", "utf-8")) 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): def join_channels(self, channels):
"""Join channels given a list of channel names.""" """Join channels given a list of channel names."""