2008-02-21 07:06:33 -05:00
|
|
|
#!/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/
|
|
|
|
"""
|
|
|
|
|
2008-03-02 06:16:25 -05:00
|
|
|
import sys, os, time, threading, signal
|
2008-02-21 07:06:33 -05:00
|
|
|
import bot
|
|
|
|
|
2008-03-02 06:16:25 -05:00
|
|
|
class Watcher(object):
|
|
|
|
# Cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496735
|
|
|
|
def __init__(self):
|
|
|
|
self.child = os.fork()
|
|
|
|
if self.child != 0:
|
|
|
|
self.watch()
|
|
|
|
|
|
|
|
def watch(self):
|
|
|
|
try: os.wait()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
self.kill()
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
def kill(self):
|
|
|
|
try: os.kill(self.child, signal.SIGKILL)
|
|
|
|
except OSError: pass
|
|
|
|
|
2008-02-21 07:06:33 -05:00
|
|
|
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)
|
|
|
|
|
2008-03-02 06:16:25 -05:00
|
|
|
Watcher()
|
2008-02-21 07:06:33 -05:00
|
|
|
while True:
|
2008-03-02 10:50:05 -05:00
|
|
|
try: connect(config)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
if not isinstance(delay, int):
|
|
|
|
break
|
2008-02-21 07:06:33 -05:00
|
|
|
|
|
|
|
warning = 'Warning: Disconnected. Reconnecting in %s seconds...' % delay
|
|
|
|
print >> sys.stderr, warning
|
|
|
|
time.sleep(delay)
|
|
|
|
|
|
|
|
def run(config):
|
|
|
|
t = threading.Thread(target=run_phenny, args=(config,))
|
2008-03-02 10:50:05 -05:00
|
|
|
if hasattr(t, 'run'):
|
|
|
|
t.run()
|
|
|
|
else: t.start()
|
2008-02-21 07:06:33 -05:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print __doc__
|