aboutsummaryrefslogtreecommitdiffstats
path: root/climl/__init__.py
blob: 056d33a33f6f9dcd321b042fa0c38b94f5ad3eb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from . import imap
from . import interface
from . import hooks
import confparser
import os
import traceback


def on_email(content, hooks, conf):
    print('▬▬▬▬▬▬▬▶')

    context = dict(
        conf=conf,
        content=content,
    )

    try:
        for hook in hooks:
            print('on_email, calling hook {}'.format(hook.__name__))
            hook(context)
            print('on_email, end of hook {}'.format(hook.__name__))
    except interface.HookStopIteration:
        print('hook wanted to stop processing for current email')
        traceback.print_exc()
    print('◀▬▬▬▬▬▬▬')


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))

    hooksfuncs = (
            hooks.hook_extract_email_from_content,
            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_modify_reply_to,
            hooks.hook_send_all,
    )
    print('starting imap.main loop')
    try:
        imap.main(lambda content: on_email(content, hooksfuncs, conf),
                  conf=conf)
    except KeyboardInterrupt:
        pass
    print('end of main imap loop')