Awesome IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.9 KiB

#!/usr/bin/env python
.. module:: PingResponder
:synopsis: Module to repsond to irc server PING requests
.. moduleauthor:: Dave Pedu <>
from time import time, sleep
from threading import Thread
from pyircbot.modulebase import ModuleBase, ModuleHook
class PingResponder(ModuleBase):
def __init__(self, bot, moduleName):
ModuleBase.__init__(self, bot, moduleName)
self.timer = PingRespondTimer(self)
self.hooks = [
ModuleHook("PING", self.pingrespond),
ModuleHook("_RECV", self.resettimer),
ModuleHook("_SEND", self.resettimer)
def pingrespond(self, args, prefix, trailing):
"""Respond to the PING command"""
# got a ping? send it right back"%s Responded to a ping: %s" % (, trailing))
def resettimer(self, msg):
"""Resets the connection failure timer"""
def ondisable(self):
class PingRespondTimer(Thread):
"Tracks last ping from server, and reconnects if over a threshold"
def __init__(self, master):
self.daemon = True
self.alive = True
self.master = master
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:
if time() - self.lastping > self.master.config.get("activity_timeout", 300):"No activity in %s seconds. Reconnecting" % str(time() - self.lastping))"Ping timeout", forever=False)