pyircbot/pyircbot/modules/PingResponder.py

53 lines
1.6 KiB
Python
Raw Normal View History

2013-12-28 09:58:20 -08:00
#!/usr/bin/env python
2014-10-02 18:14:42 -07:00
"""
.. module:: PingResponder
2015-11-01 18:03:11 -08:00
:synopsis: Module to repsond to irc server PING requests
2014-10-02 18:14:42 -07:00
.. moduleauthor:: Dave Pedu <dave@davepedu.com>
"""
from time import time,sleep
from threading import Thread
2015-06-18 19:29:46 -07:00
from pyircbot.modulebase import ModuleBase,ModuleHook
2013-12-28 09:58:20 -08:00
class PingResponder(ModuleBase):
2015-11-01 18:03:11 -08:00
def __init__(self, bot, moduleName):
ModuleBase.__init__(self, bot, moduleName);
self.timer = PingRespondTimer(self)
2015-11-01 18:03:11 -08:00
self.hooks=[ModuleHook("PING", self.pingrespond)]
def pingrespond(self, args, prefix, trailing):
"""Respond to the PING command"""
# got a ping? send it right back
self.bot.act_PONG(trailing)
self.log.info("%s Responded to a ping: %s" % (self.bot.get_nick(), trailing))
self.timer.reset()
def ondisable(self):
self.timer.disable()
class PingRespondTimer(Thread):
"Tracks last ping from server, and reconnects if over a threshold"
def __init__(self, master):
Thread.__init__(self)
self.daemon = True
self.alive = True
self.master = master
self.reset()
self.start()
def reset(self):
"Reset the internal ping timeout counter"
self.lastping = time()
def disable(self):
"Allow the thread to die"
self.alive = False
def run(self):
while self.alive:
sleep(5)
if time() - self.lastping > 300: #TODO: configurable timeout
self.master.log.info("No pings in %s seconds. Reconnecting" % str(time() - self.lastping))
self.master.bot.disconnect("Reconnecting...")
self.reset()