From 8d1597b10cb49cd54677f7a049afa637cece6134 Mon Sep 17 00:00:00 2001 From: VG Date: Wed, 8 Jun 2016 23:33:32 +0200 Subject: move conf and start a new hook --- climl/__init__.py | 53 ++++++++++++++++++++++++++++++++++++++++++----------- climl/hooks.py | 7 ++++++- climl/imap.py | 22 +++------------------- 3 files changed, 51 insertions(+), 31 deletions(-) diff --git a/climl/__init__.py b/climl/__init__.py index e865254..bfbe4fc 100644 --- a/climl/__init__.py +++ b/climl/__init__.py @@ -1,34 +1,65 @@ from . import imap from . import interface from . import hooks +import confparser +import os -def on_email(content, hooks): - print('on_email:', content) - context = {} +def on_email(content, hooks, conf): + #print('on_email:', content) + + class Context(dict): + def __init__(self): + self.conf = conf + + context = Context() try: for hook in hooks: + print('on_email, calling hook {}'.format(hook.__name__)) context, content = hook(context, content) - except interface.HookStopIteration(): + except interface.HookStopIteration: pass print('now, raising exception') raise interface.HookAbortError() +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 main(): + confpath = os.path.expanduser('~/.config/climl/climl.cfg') + conf = conf_postprocess(confparser.read_conf(confpath)) + # TODO: how to load hooks with a dependency tree and sort them accordingly # to generate hooks list ? # now try a fixed list ? - hooks = ( - hook_check_ml_restrictions, # open/private ml - hook_validate_poster, - hook_process_subscribe_queries, - hook_generate_rcpt_list, - hook_send_all, + hooksfuncs = ( + hooks.hook_extract_listname, + hooks.hook_check_ml_restrictions, # open/private ml + hooks.hook_validate_poster, + hooks.hook_process_subscribe_queries, + hooks.hook_generate_rcpt_list, + hooks.hook_send_all, ) print('starting imap.main loop') try: - imap.main(lambda content: on_mail(content, hooks)) + imap.main(lambda content: on_email(content, hooksfuncs, conf), + conf=conf) except KeyboardInterrupt: pass print('end of main imap loop') diff --git a/climl/hooks.py b/climl/hooks.py index 8311da4..cf8ae9d 100644 --- a/climl/hooks.py +++ b/climl/hooks.py @@ -2,6 +2,11 @@ import smtplib from . import interface +def hook_extract_listname(context, content): + print('hook_extract_listname:') + print(content) + + def hook_check_ml_restrictions(context, content): pass @@ -39,7 +44,7 @@ def hook_send_all(context, content): raise interface.HookStopIteration() # TODO: how to send emails here ? - smtp_server = smtplib.SMTP(context.get('smtp.server')) + smtp_server = smtplib.SMTP(context.conf.get('smtp.server')) #smtp_server.set_debuglevel(1) smtp_server.sendmail(listaddress, context['recipients'], content) smtp_server.quit() diff --git a/climl/imap.py b/climl/imap.py index f15ae05..b21a8f6 100644 --- a/climl/imap.py +++ b/climl/imap.py @@ -1,29 +1,14 @@ import os import subprocess -import confparser import imapclient import contextlib import backports.ssl as ssl import socket import time + from . import interface -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): @@ -144,10 +129,9 @@ def process_emails(connection, callback=None, maxsize=None): data = None -def main(callback=None): +def main(callback=None, conf=None): assert callback - confpath = os.path.expanduser('~/.config/climl/climl.cfg') - conf = conf_postprocess(confparser.read_conf(confpath)) + assert conf print('Read conf:', conf) password_command = conf.get('imap.password_command', None) if password_command: -- cgit v1.2.3