aboutsummaryrefslogtreecommitdiffstats
path: root/climl/hooks.py
blob: 660d1dbe6ce592f35ab6e387f43f58dfa12102a1 (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
import smtplib
from . import interface
import email


def hook_extract_email_from_content(context):
    context['email'] = email.message_from_bytes(context['content'])


def hook_extract_listname(context):
    email = context['email']
    context['listaddress'] = email['To']
    context['listname'] = context['listaddress'].split('@')[0]
    print('found listname: {}'.format(context['listname']))


def hook_check_ml_restrictions(context):
    pass


def hook_validate_poster(context):
    pass


def hook_process_subscribe_queries(context):
    pass


def hook_modify_reply_to(context):
    email = context['email']
    email['Reply-To'] = context['listaddress']


def hook_generate_rcpt_list(context):
    listname = context.get('listname', None)
    if not listname:
        raise interface.HookStopIteration('list name not found')
    recipients_filename = ''.join([listname, '-recipients.txt'])
    try:
        with open(recipients_filename, encoding='utf8') as fi:
            recipients = [recipient for recipient in fi]
            context['recipients'] = recipients
    except FileNotFoundError as e:
        n = interface.HookAbortError('recipients list not found')
        n.args += ({'listname': listname,
                       'recipients_filename': recipients_filename}, )
        raise n from e


def hook_send_all(context):
    listaddress = context.get('listaddress', None)
    if not listaddress:
        raise interface.HookStopIteration('list address not found')
    recipients = context.get('recipients', None)
    if not recipients:
        raise interface.HookStopIteration()

    print('server_smtp:', context['conf']['smtp.server'])
    with smtplib.SMTP('devys.org') as smtp_server:
        #smtp_server.set_debuglevel(1)
        smtp_server.send_message(context['email'],
                            from_addr=listaddress,
                            to_addrs=context['recipients'])