Check for ping timeout and exit
parent
143e5b91af
commit
cb5d49ff5a
13
itte.py
13
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."""
|
||||
|
|
Loading…
Reference in New Issue