summaryrefslogtreecommitdiff
path: root/src/jwebmail/render_mail.py
blob: c36150d7bb035fe5c9a51484e1bc47f417f48a2c (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from flask import current_app, request, url_for
from flask_babel import gettext
from markupsafe import Markup, escape


def render_text_plain(_subtype, content, _path):
    return f'<div class="jwm-mail-body-text-plain"><pre>{escape(content)}</pre></div>\n'


def render_text_html(_subtype, _content, path):
    if path:
        url = url_for(
            "read",
            folder=request.view_args.get("folder", ""),
            msgid=request.view_args["msgid"],
            format="raw",
            path=".".join(map(str, path)),
        )
    else:
        url = url_for(
            "read",
            folder=request.view_args.get("folder", ""),
            msgid=request.view_args["msgid"],
            format="raw",
        )

    return f'<iframe src="{url}" class="jwm-mail-body-text-html" ></iframe>\n'


def render_image(subtype, content, _path):
    return f'<img src="data:image/{subtype};base64,{escape(content)}" />\n'


def render_multipart_alternative(_subtype, content, path):
    parts = content["parts"]
    T = "<div class=tabs><ul>\n"
    C = "<div class=jwm-mail-body-multipart-alternative-bodies>"
    init, *rest = reversed(parts)

    T += f"<li class=is-active data=0><a>{to_mime_type(init['head'])}</a></li>"

    C += "<div class=jwm-mail-body-multipart-alternative-body>\n"
    C += mime_render(
        *to_mime_types(init["head"]), init["body"], path + (len(parts) - 1,)
    )
    C += "</div>\n"

    for i, r in enumerate(rest, 1):
        T += f"<li data={i}><a>{to_mime_type(r['head'])}</a></li>\n"

        C += '<div class="jwm-mail-body-multipart-alternative-body is-hidden">\n'
        C += mime_render(
            *to_mime_types(r["head"]), r["body"], path + (len(parts) - 1 - i,)
        )
        C += "</div>\n"

    C += "</div>"
    T += "</ul></div>"
    return f'<div class="jwm-mail-body jwm-mail-body-multipart-alternative">\n{T}\n{C}\n</div>\n'


def render_multipart(_subtype, content, path):
    parts = content["parts"]
    R = '<div class="jwm-mail-body jwm-mail-body-multipart">\n'

    for i, p in enumerate(parts):
        R += "<div class=media><div class=media-content>\n"
        if (
            not p["head"]["content_disposition"]
            or p["head"]["content_disposition"].lower() == "none"
            or p["head"]["content_disposition"].lower() == "inline"
        ):
            R += mime_render(*to_mime_types(p["head"]), p["body"], path + (i,))
        elif p["head"]["content_disposition"].lower() == "attachment":
            link_text = gettext("Attachment {filename} of type {filetype}").format(
                filename=p["head"]["filename"], filetype=to_mime_type(p["head"])
            )

            ref_url = url_for(
                "read",
                folder=request.view_args.get("folder", ""),
                msgid=request.view_args["msgid"],
                path=".".join(map(str, [*path, i])),
                format="raw",
            )

            R += "<p>"
            R += f'<a href="{ref_url}" download="{escape(p["head"]["filename"])}">\n'
            R += f"{escape(link_text)}</a>\n"
            R += "</p>\n"
        else:
            current_app.log.warning(
                "unknown Content-Disposition %s", p["head"]["content_disposition"]
            )
            R += f"<p>unknown Content-Disposition {p['head']['content_disposition']}</p>\n"

        R += "</div></div>\n"

    return R + "</div>\n"


def _format_header(category, value):
    R = ""

    if isinstance(value, list) and value:
        R += f"<dt>{escape(category)}</dt>\n"
        for v in value:
            value = (
                f'"{v["display_name"]}" <{v["address"]}>'
                if v["display_name"]
                else v["address"]
            )
            R += f"<dd>{escape(value)}<dd>\n"

    return R


def render_message(subtype, msg, path):
    if subtype != "rfc822":
        current_app.log.warning("unknown message mime-subtype %s", subtype)

    R = '<div class="jwm-mail">'

    R += '<dl class="jwm-mail-header">'
    R += f"<dt>{escape(gettext('Subject'))}</dt>"
    R += f"<dd>{escape(msg['head']['subject'])}</dd>\n"
    R += _format_header(gettext("From"), msg["head"].get("from"))
    R += _format_header(gettext("To"), msg["head"].get("to"))
    R += _format_header(gettext("CC"), msg["head"].get("cc"))
    R += _format_header(gettext("BCC"), msg["head"].get("bcc"))
    R += f"<dt>{escape(gettext('Date'))}</dt>"
    date = escape(msg["head"]["date"])
    R += f"<dd><time datetime='{date}' class=jwm-tolocaltime>{date}</time></dd>\n"
    R += f"<dt>{escape(gettext('Content-Type'))}</dt>"
    R += f"<dd>{to_mime_type(msg['head']['mime'])}</dd>\n"
    R += "</dl>\n"

    R += mime_render(*to_mime_types(msg["head"]["mime"]), msg["body"], path + (0,))

    return R + "</div>\n"


MIMERenderSubs = {
    ("text", "plain"): render_text_plain,
    ("text", "html"): render_text_html,
    ("multipart", "alternative"): render_multipart_alternative,
    "multipart": render_multipart,
    "message": render_message,
    "image": render_image,
}


def mime_render(maintype, subtype, content, path):
    renderer = MIMERenderSubs.get((maintype, subtype)) or MIMERenderSubs.get(maintype)

    if not renderer:
        typ = f"<code>{maintype}/{subtype}</code>"
        msg = gettext("Unsupported MIME type of {mime_type}.").format(mime_type=typ)
        return f'<p class="jwm-body-unsupported">{msg}</p>\n'

    return renderer(subtype, content, path)


def to_mime_type(mime):
    return escape(f"{mime['content_maintype']}/{mime['content_subtype']}".lower())


def to_mime_types(mime):
    return escape(mime["content_maintype"]), escape(mime["content_subtype"])


def format_mail(mail):
    return Markup(mime_render("message", "rfc822", mail, tuple()))