From 630aa83084e1b78e2b07a1acb81edc58b70ca2d0 Mon Sep 17 00:00:00 2001 From: James Lu Date: Mon, 28 Sep 2015 11:30:51 -0700 Subject: [PATCH] core: add some rudimentary config file validation --- conf.py | 33 +++++++++++++++++++++++++++------ pylink | 3 --- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/conf.py b/conf.py index 93067a4..d1f7b1a 100644 --- a/conf.py +++ b/conf.py @@ -31,6 +31,32 @@ testconf = {'bot': 'sidrange': '8##' }) } + +def validateConf(conf): + """Validates a parsed configuration dict.""" + assert type(conf) == dict, "Invalid configuration given: should be type dict, not %s." % type(conf).__name__ + for section in ('bot', 'servers', 'login'): + assert conf.get(section), "Missing %r section in config." % section + for netname, serverblock in conf['servers'].items(): + for section in ('ip', 'port', 'recvpass', 'sendpass', 'hostname', + 'sid', 'sidrange', 'channels', 'protocol', 'maxnicklen'): + assert serverblock.get(section), "Missing %r in server block for %r." % (section, netname) + assert type(serverblock['channels']) == list, "'channels' option in " \ + "server block for %s must be a list, not %s." % (netname, type(serverblock['channels']).__name__) + assert type(conf['login'].get('password')) == type(conf['login'].get('user')) == str and \ + conf['login']['password'] != "changeme", "You have not set the login details correctly!" + return conf + +def loadConf(fname): + """Loads a PyLink configuration file from the filename given.""" + with open(fname, 'r') as f: + try: + conf = yaml.load(f) + except Exception as e: + print('ERROR: Failed to load config from %r: %s: %s' % (fname, type(e).__name__, e)) + sys.exit(4) + return conf + if world.testing: conf = testconf confname = 'testconf' @@ -46,9 +72,4 @@ else: # we load. confname = 'pylink' fname = 'config.yml' - with open(fname, 'r') as f: - try: - conf = yaml.load(f) - except Exception as e: - print('ERROR: Failed to load config from %r: %s: %s' % (fname, type(e).__name__, e)) - sys.exit(4) + conf = validateConf(loadConf(fname)) diff --git a/pylink b/pylink index e79a388..8e558f0 100755 --- a/pylink +++ b/pylink @@ -16,9 +16,6 @@ import coreplugin if __name__ == '__main__': log.info('PyLink %s starting...', world.version) - if conf.conf['login']['password'] == 'changeme': - log.critical("You have not set the login details correctly! Exiting...") - sys.exit(2) protocols_folder = [os.path.join(os.getcwd(), 'protocols')] # Write a PID file.