2012-12-16 17:09:43 -05:00
|
|
|
#!/usr/bin/env python3
|
2008-02-21 07:06:33 -05:00
|
|
|
"""
|
|
|
|
__init__.py - Phenny Init Module
|
|
|
|
Copyright 2008, Sean B. Palmer, inamidst.com
|
|
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
|
|
|
|
http://inamidst.com/phenny/
|
|
|
|
"""
|
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
import os
|
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
import time
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
|
|
|
|
class Watcher(object):
|
2012-05-31 02:24:21 -04:00
|
|
|
# Cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496735
|
|
|
|
def __init__(self):
|
|
|
|
self.child = os.fork()
|
|
|
|
if self.child != 0:
|
|
|
|
signal.signal(signal.SIGTERM, self.sig_term)
|
|
|
|
self.watch()
|
2008-03-02 06:16:25 -05:00
|
|
|
|
2012-05-31 02:24:21 -04:00
|
|
|
def watch(self):
|
2017-04-10 01:50:00 -04:00
|
|
|
try:
|
|
|
|
os.wait()
|
2012-05-31 02:24:21 -04:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
self.kill()
|
|
|
|
sys.exit()
|
2008-03-02 06:16:25 -05:00
|
|
|
|
2012-05-31 02:24:21 -04:00
|
|
|
def kill(self):
|
2017-04-10 01:50:00 -04:00
|
|
|
try:
|
|
|
|
os.kill(self.child, signal.SIGKILL)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2008-03-02 06:16:25 -05:00
|
|
|
|
2012-05-31 02:24:21 -04:00
|
|
|
def sig_term(self, signum, frame):
|
|
|
|
self.kill()
|
|
|
|
sys.exit()
|
2011-10-07 16:09:01 -04:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
|
|
|
|
def run_phenny(config):
|
|
|
|
if hasattr(config, 'delay'):
|
2012-05-31 02:24:21 -04:00
|
|
|
delay = config.delay
|
2017-04-10 01:50:00 -04:00
|
|
|
else:
|
|
|
|
delay = 20
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
def connect(config):
|
2012-05-31 02:24:21 -04:00
|
|
|
import bot
|
|
|
|
p = bot.Phenny(config)
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
ssl_context = p.get_ssl_context(config.ca_certs)
|
|
|
|
if config.ssl_cert and config.ssl_key:
|
|
|
|
ssl_context.load_cert_chain(config.ssl_cert, config.ssl_key)
|
|
|
|
p.run(config.host, config.port, config.ssl, config.ipv6, None,
|
|
|
|
ssl_context)
|
|
|
|
|
|
|
|
try:
|
|
|
|
Watcher()
|
2012-05-31 02:24:21 -04:00
|
|
|
except Exception as e:
|
|
|
|
print('Warning:', e, '(in __init__.py)', file=sys.stderr)
|
2008-05-31 06:46:48 -04:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
connect(config)
|
2012-05-31 02:24:21 -04:00
|
|
|
except KeyboardInterrupt:
|
2017-04-10 01:50:00 -04:00
|
|
|
sys.exit()
|
2008-03-02 10:50:05 -05:00
|
|
|
|
2012-05-31 02:24:21 -04:00
|
|
|
if not isinstance(delay, int):
|
|
|
|
break
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
msg = "Warning: Disconnected. Reconnecting in {0} seconds..."
|
|
|
|
print(msg.format(delay), file=sys.stderr)
|
2012-05-31 02:24:21 -04:00
|
|
|
time.sleep(delay)
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
|
|
|
|
def run(config):
|
2012-05-31 02:24:21 -04:00
|
|
|
t = threading.Thread(target=run_phenny, args=(config,))
|
|
|
|
if hasattr(t, 'run'):
|
|
|
|
t.run()
|
2017-04-10 01:50:00 -04:00
|
|
|
else:
|
|
|
|
t.start()
|
|
|
|
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2012-05-31 02:24:21 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
print(__doc__)
|