Clean up core and add support for TLS client certs

This commit is contained in:
mutantmonkey
2017-04-09 22:50:00 -07:00
parent e4d28c0990
commit 538fec692d
3 changed files with 200 additions and 155 deletions

121
phenny
View File

@@ -11,19 +11,23 @@ Run ./phenny, then edit ~/.phenny/default.py
Then run ./phenny again
"""
import sys, os, imp
import argparse
import imp
import os
import sys
from textwrap import dedent as trim
dotdir = os.path.expanduser('~/.phenny')
def check_python_version():
if sys.version_info < (3, 0):
error = 'Error: Requires Python 3.0 or later, from www.python.org'
def check_python_version():
if sys.version_info < (3, 4):
error = 'Error: Requires Python 3.4 or later, from www.python.org'
print(error, file=sys.stderr)
sys.exit(1)
def create_default_config(fn):
def create_default_config(fn):
f = open(fn, 'w')
print(trim("""\
nick = 'phenny'
@@ -51,7 +55,7 @@ def create_default_config(fn):
# If you want to enumerate a list of modules rather than disabling
# some, use "enable = ['example']", which takes precedent over exclude
#
#
# enable = []
# Directories to load user modules from
@@ -59,7 +63,7 @@ def create_default_config(fn):
extra = []
# Services to load: maps channel names to white or black lists
external = {
external = {
'#liberal': ['!'], # allow all
'#conservative': [], # allow none
'*': ['!'] # default whitelist, allow all
@@ -69,6 +73,7 @@ def create_default_config(fn):
"""), file=f)
f.close()
def create_default_config_file(dotdir):
print('Creating a default config file at ~/.phenny/default.py...')
default = os.path.join(dotdir, 'default.py')
@@ -77,10 +82,12 @@ def create_default_config_file(dotdir):
print('Done; now you can edit default.py, and run phenny! Enjoy.')
sys.exit(0)
def create_dotdir(dotdir):
def create_dotdir(dotdir):
print('Creating a config directory at ~/.phenny...')
try: os.mkdir(dotdir)
except Exception as e:
try:
os.mkdir(dotdir)
except Exception as e:
print('There was a problem creating %s:' % dotdir, file=sys.stderr)
print(e.__class__, str(e), file=sys.stderr)
print('Please fix this and then run phenny again.', file=sys.stderr)
@@ -88,87 +95,96 @@ def create_dotdir(dotdir):
create_default_config_file(dotdir)
def check_dotdir():
def check_dotdir():
default = os.path.join(dotdir, 'default.py')
if not os.path.isdir(dotdir):
if not os.path.isdir(dotdir):
create_dotdir(dotdir)
elif not os.path.isfile(default):
elif not os.path.isfile(default):
create_default_config_file(dotdir)
def config_names(config):
def config_names(config):
config = config or 'default'
def files(d):
def files(d):
names = os.listdir(d)
return list(os.path.join(d, fn) for fn in names if fn.endswith('.py'))
here = os.path.join('.', config)
if os.path.isfile(here):
if os.path.isfile(here):
return [here]
if os.path.isfile(here + '.py'):
if os.path.isfile(here + '.py'):
return [here + '.py']
if os.path.isdir(here):
if os.path.isdir(here):
return files(here)
there = os.path.join(dotdir, config)
if os.path.isfile(there):
if os.path.isfile(there):
return [there]
if os.path.isfile(there + '.py'):
if os.path.isfile(there + '.py'):
return [there + '.py']
if os.path.isdir(there):
if os.path.isdir(there):
return files(there)
print("Error: Couldn't find a config file!", file=sys.stderr)
print('What happened to ~/.phenny/default.py?', file=sys.stderr)
sys.exit(1)
def main(argv=None):
def main(argv=None):
# Step One: Parse The Command Line
parser = argparse.ArgumentParser(description="A Python IRC bot.")
parser.add_argument('-c', '--config', metavar='fn',
help='use this configuration file or directory')
parser.add_argument('-c', '--config', metavar='fn',
help='use this configuration file or directory')
args = parser.parse_args(argv)
# Step Two: Check Dependencies
check_python_version() # require python2.4 or later
check_python_version()
if not args.config:
check_dotdir() # require ~/.phenny, or make it and exit
check_dotdir() # require ~/.phenny, or make it and exit
# Step Three: Load The Configurations
config_modules = []
for config_name in config_names(args.config):
for config_name in config_names(args.config):
name = os.path.basename(config_name).split('.')[0] + '_config'
module = imp.load_source(name, config_name)
module.filename = config_name
if not hasattr(module, 'prefix'):
if not hasattr(module, 'prefix'):
module.prefix = r'\.'
if not hasattr(module, 'name'):
if not hasattr(module, 'name'):
module.name = 'Phenny Palmersbot, http://inamidst.com/phenny/'
if not hasattr(module, 'port'):
if not hasattr(module, 'port'):
module.port = 6667
if not hasattr(module, 'ssl'):
module.ssl = False
if not hasattr(module, 'ca_certs'):
module.ca_certs = None
if not hasattr(module, 'ssl_cert'):
module.ssl_cert = None
if not hasattr(module, 'ssl_key'):
module.ssl_key = None
if not hasattr(module, 'ipv6'):
module.ipv6 = False
if not hasattr(module, 'password'):
if not hasattr(module, 'password'):
module.password = None
if module.host == 'irc.example.net':
error = ('Error: you must edit the config file first!\n' +
"You're currently using %s" % module.filename)
if module.host == 'irc.example.net':
error = ('Error: you must edit the config file first!\n' +
"You're currently using %s" % module.filename)
print(error, file=sys.stderr)
sys.exit(1)
@@ -176,18 +192,21 @@ def main(argv=None):
# Step Four: Load Phenny
try: from __init__ import run
except ImportError:
try: from phenny import run
except ImportError:
try:
from __init__ import run
except ImportError:
try:
from phenny import run
except ImportError:
print("Error: Couldn't find phenny to import", file=sys.stderr)
sys.exit(1)
# Step Five: Initialise And Run The Phennies
# @@ ignore SIGHUP
for config_module in config_modules:
run(config_module) # @@ thread this
for config_module in config_modules:
run(config_module) # @@ thread this
if __name__ == '__main__':
if __name__ == '__main__':
main()