summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/mail/smtp/bounces.py
blob: 7a4674b9cc7336ce920c0f475d78f050092b94c3 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-
# bounces.py
# Copyright (C) 2016 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Deliver bounces to the user Inbox.
"""
import time
from email.message import Message
from email.utils import formatdate

from leap.mail.constants import INBOX_NAME
from leap.mail.mail import Account


# TODO implement localization for this template.

BOUNCE_TEMPLATE = """This is your local Bitmask Mail Agent running at localhost.

I'm sorry to have to inform you that your message could not be delivered to one
or more recipients.

The reasons I got for the error are:

{raw_error}

If the problem persists and it's not a network connectivity issue, you might
want to contact your provider ({provider}) with this information (remove any
sensitive data before).

--- Original message (*before* it was encrypted by bitmask) below ----:

{orig}"""


class Bouncer(object):
    """
    Implements a mechanism to deliver bounces to user inbox.
    """
    # TODO this should follow RFC 6522, and compose a correct multipart
    # attaching the report and the original message. Leaving this for a future
    # iteration.

    def __init__(self, inbox_collection):
        self._inbox_collection = inbox_collection

    def bounce_message(self, error_data, to, date=None, orig=''):
        if not date:
            date = formatdate(time.time())

        raw_data = self._format_msg(error_data, to, date, orig)
        d = self._inbox_collection.add_msg(
            raw_data, ('\\Recent',), date=date)
        return d

    def _format_msg(self, error_data, to, date, orig):
        provider = to.split('@')[1]

        msg = Message()
        msg.add_header(
            'From', 'bitmask-bouncer@localhost (Bitmask Local Agent)')
        msg.add_header('To', to)
        msg.add_header('Subject', 'Undelivered Message')
        msg.add_header('Date', date)
        msg.set_payload(BOUNCE_TEMPLATE.format(
            raw_error=error_data,
            provider=provider,
            orig=orig))

        return msg.as_string()


def bouncerFactory(soledad):
    user_id = soledad.uuid
    acc = Account(soledad, user_id)
    d = acc.callWhenReady(lambda _: acc.get_collection_by_mailbox(INBOX_NAME))
    d.addCallback(lambda inbox: Bouncer(inbox))
    return d