From ee8e536d83e7fd38a0ad9f88a172fae956c15754 Mon Sep 17 00:00:00 2001 From: VG Date: Wed, 16 Dec 2015 23:06:24 +0100 Subject: add conf processing (boolean) --- climl_imap_bridge.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/climl_imap_bridge.py b/climl_imap_bridge.py index 8070b86..c0ddb82 100644 --- a/climl_imap_bridge.py +++ b/climl_imap_bridge.py @@ -7,6 +7,22 @@ import imapclient import backports.ssl +def conf_boolean_postprocess(src): + src = src.lower().strip() + if src == 'true' or src == 'on' or src == '1': + return True + return False + +def conf_postprocess(conf): + d = { + 'imap.tls': conf_boolean_postprocess, + 'imap.start_tls': conf_boolean_postprocess, + 'imap.tls_nocheck_hostname': conf_boolean_postprocess, + 'imap.tls_nocheck_ca': conf_boolean_postprocess, + } + return {key: d[key](value) if key in d else value + for key, value in conf.items()} + def connect_to_imap(conf, password): cafile = conf.get('imap.tls_ca', None) @@ -16,18 +32,18 @@ def connect_to_imap(conf, password): ssl_context = imapclient.create_default_context(cafile=cafile) - if conf.get('imap.tls_nocheck_hostname', 'false').lower() == 'true': + if conf.get('imap.tls_nocheck_hostname', False): # don't check if certificate hostname doesn't match target hostname ssl_context.check_hostname = False - if conf.get('imap.tls_nocheck_ca', 'false').lower() == 'true': + if conf.get('imap.tls_nocheck_ca', False): # don't check if the certificate is trusted by a certificate authority ssl_context.verify_mode = backports.ssl.CERT_NONE connection = imapclient.IMAPClient(host=conf.get('imap.server'), - ssl=conf.get('imap.tls', 'true').lower() == 'true', + ssl=conf.get('imap.tls', True), ssl_context=ssl_context) - if conf.get('imap.start_tls', 'false').lower() == 'true': + if conf.get('imap.start_tls', False): connection.start_tls(ssl_context=ssl_context) print('connection succeed') connection.login(username=conf.get('imap.username'), password=password) @@ -35,8 +51,8 @@ def connect_to_imap(conf, password): return connection def main(): - confpath = os.path.expanduser('~') + '/temp/conf.cfg' - conf = confparser.read_conf(confpath) + confpath = os.path.expanduser('~/temp/conf.cfg') + conf = conf_postprocess(confparser.read_conf(confpath)) print('Read conf:', conf) password_command = conf.get('imap.password_command', None) if password_command: -- cgit v1.2.3