diff options
-rw-r--r-- | climl.py | 94 | ||||
-rw-r--r-- | climl/__init__.py | 7 | ||||
-rw-r--r-- | climl/imap.py | 94 |
3 files changed, 104 insertions, 91 deletions
@@ -1,93 +1,5 @@ #!/usr/bin/python3 -import os -import subprocess -import confparser -import imapclient -import contextlib -import backports.ssl -import socket - -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()} - -@contextlib.contextmanager -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): - # don't check if certificate hostname doesn't match target hostname - ssl_context.check_hostname = False - - 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), - ssl_context=ssl_context) - 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) - print('successfuly logged') - - # TODO: replace shutdown by logouts when it will not timeout through ssl - try: - yield connection - #connection.logout() - except (connection.AbortError, socket.error, socket.timeout, - backports.ssl.SSLError, backports.ssl.CertificateError): - raise - else: - pass - #connection.logout() - finally: - connection.shutdown() - -def main(): - 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: - password = subprocess.check_output(password_command, shell=True) - password = password.rstrip().decode('utf8') - print('got pasword:', password) - - #connection = connect_to_imap(conf, password) - with connect_to_imap(conf, password) as connection: - print('selecting folder') - connection.select_folder(conf.get('imap.mailbox')) - print('entering idle mode') - connection.idle() - print('waiting for an event') - while True: - try: - result = connection.idle_check(timeout=10) - except KeyboardInterrupt: - break - print('result:', result) - print('end of connection') - - -main() # dev mode for now +if __name__ == '__main__': + import climl + climl.main() diff --git a/climl/__init__.py b/climl/__init__.py new file mode 100644 index 0000000..d577ace --- /dev/null +++ b/climl/__init__.py @@ -0,0 +1,7 @@ +from . import imap + +def on_email(content): + print('on_email:', content) + +def main(): + imap.main(on_email) diff --git a/climl/imap.py b/climl/imap.py new file mode 100644 index 0000000..208f9cc --- /dev/null +++ b/climl/imap.py @@ -0,0 +1,94 @@ +import os +import subprocess +import confparser +import imapclient +import contextlib +import backports.ssl +import socket + +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()} + +@contextlib.contextmanager +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): + # don't check if certificate hostname doesn't match target hostname + ssl_context.check_hostname = False + + 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), + ssl_context=ssl_context) + 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) + print('successfuly logged') + + # TODO: replace shutdown by logouts when it will not timeout through ssl + try: + yield connection + #connection.logout() + except (connection.AbortError, socket.error, socket.timeout, + backports.ssl.SSLError, backports.ssl.CertificateError): + raise + else: + pass + #connection.logout() + finally: + connection.shutdown() + +def main(callback=None): + assert callback + 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: + password = subprocess.check_output(password_command, shell=True) + password = password.rstrip().decode('utf8') + print('got pasword:', password) + + #connection = connect_to_imap(conf, password) + with connect_to_imap(conf, password) as connection: + print('selecting folder') + connection.select_folder(conf.get('imap.mailbox')) + print('entering idle mode') + connection.idle() + print('waiting for an event') + while True: + try: + result = connection.idle_check(timeout=10) + except KeyboardInterrupt: + break + print('result:', result) + print('calling callback') + callback('mail') + print('end of connection') + + +#main() # dev mode for now |