#!/usr/bin/python3 import os import subprocess import confparser import imapclient import backports.ssl def connect_to_imap(conf, password): cafile = conf.get('imap.tls_ca', None) if cafile: cafile = os.path.expanduser(cafile) ssl_context = imapclient.create_default_context(cafile=cafile) if conf.get('imap.tls_nocheck_hostname', 'false').lower() == 'true': # 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': # 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_context=ssl_context) if conf.get('imap.start_tls', 'false').lower() == 'true': connection.start_tls(ssl_context=ssl_context) print('connection succeed') connection.login(username=conf.get('imap.username'), password=password) print('successfuly logged') return connection def main(): confpath = os.path.expanduser('~') + '/temp/conf.cfg' conf = confparser.read_conf(confpath) print('Read conf:', conf) password_command = conf.get('imap.password_command', None) if password_command: password = subprocess.check_output(password_command, shell=True) password = password.rstrip().decode('utf8') print('got pasword:', password) connection = connect_to_imap(conf, password) main() # dev mode for now