#!/usr/bin/env python """ __init__.py - Phenny Init Module Copyright 2008, Sean B. Palmer, inamidst.com Licensed under the Eiffel Forum License 2. http://inamidst.com/phenny/ """ import sys, os, time, threading, signal import bot class Watcher(object): # Cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496735 def __init__(self): self.children = [] signal.signal(signal.SIGTERM, self.sig_term) def add(self, config): child = os.fork() if child != 0: self.children.append(child) else: run_phenny(config) def watch(self): try: os.wait() except KeyboardInterrupt: self.kill() sys.exit() def kill(self): for child in self.children: try: os.kill(child, signal.SIGKILL) except OSError: pass def sig_term(self, signum, frame): self.kill() sys.exit() def run_phenny(config): if hasattr(config, 'delay'): delay = config.delay else: delay = 20 def connect(config): p = bot.Phenny(config) p.run(config.host, config.port, config.ssl) while True: try: connect(config) except KeyboardInterrupt: sys.exit() if not isinstance(delay, int): break warning = 'Warning: Disconnected. Reconnecting in %s seconds...' % delay print >> sys.stderr, warning time.sleep(delay) def run(configs): w = Watcher() for config in configs: w.add(config) w.watch() if __name__ == '__main__': print __doc__