summaryrefslogtreecommitdiff
path: root/lib/JWebmail/View/RenderMail.pm
blob: 63d3bdb95f17e5c1908ac003fd0e15394f5d5dcc (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package JWebmail::View::RenderMail;

use Mojo::Base -base;

use Mojo::ByteStream;
use Mojo::Util 'xml_escape';


has 'c';


sub render_text_plain {
    my ($_self, $_subtype, $content, $_path) = @_;

    $content = xml_escape $content;

    return qq'<div class="jwm-mail-body-text-plain"><pre>$content</pre></div>\n';
}

sub render_text_html {
    my ($self, $_subtype, $_content, $path) = @_;

    my $url = $self->c->url_for('raw', id => $self->c->stash('id'));
    $url = $url->query(path => join('.', @$path)) if @$path;

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

sub render_image {
    my ($_self, $subtype, $content, $_path) = @_;

    return qq'<img src="data:image/$subtype;base64,$content" />';
}

sub render_multipart_alternative {
    my ($self, $_subtype, $content, $path) = @_;

    my $parts = $content->{parts};
    my $T = "<div class=tabs><ul>\n";
    my $C = '<div class=jwm-mail-body-multipart-alternative-bodies>';
    my $i = 1;
    my ($init, @rest) = reverse @$parts;

    $T .= '<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 .= $self->mime_render(to_mime_types($init->{head}), $init->{body}, [@$path, $#$parts]);
    $C .= "</div>\n";

    for (@rest) {
        $T .= "<li  data=$i><a>" . to_mime_type($_->{head}) . "</a></li>\n";

        $C .= qq'<div class="jwm-mail-body-multipart-alternative-body is-hidden">\n';
        $C .= $self->mime_render(to_mime_types($_->{head}), $_->{body}, [@$path, $#$parts-$i]);
        $C .= "</div>\n";

        ++$i;
    }
    $C .= qq'</div>';
    $T .= qq'</ul></div>';
    return qq'<script src=/src/rendermail.js></script><div class="jwm-mail-body jwm-mail-body-multipart-alternative">\n$T\n$C\n</div>\n';
}

sub render_multipart {
    my ($self, $_subtype, $content, $path) = @_;

    my $parts = $content->{parts};
    my $R = qq'<div class="jwm-mail-body jwm-mail-body-multipart"\n>';
    my $i = 0;

    for (@$parts) {
        $R .= "<div class=media><div class=media-content>\n";
        if (     !$_->{head}{content_disposition}
            || lc $_->{head}{content_disposition} eq 'none'
            || lc $_->{head}{content_disposition} eq 'inline') {

            $R .= $self->mime_render(to_mime_types($_->{head}), $_->{body}, [@$path, $i]);
        }
        elsif (lc $_->{head}{content_disposition} eq 'attachment') {
            $R .= '<p>';
            $R .= $self->c->link_to($self->c->url_for(raw => id => $self->c->stash('id'))->query(path => join('.', @$path, $i)), (download => $_->{head}{filename}) => sub {
                'Attachment ' . xml_escape($_->{head}{filename}) . ' of type ' . to_mime_type($_->{head});
            });
            $R .= "</p>\n";
        }
        else {
            warn "unknown Content-Disposition '$_->{head}{content_disposition}'";
            $R .= "<p>unknown Content-Disposition '$_->{head}{content_disposition}'</p>\n";
        }
        $R .= "</div></div>\n";
        ++$i;
    }
    return $R . "</div>\n";
}

sub _format_header {
    my ($self, $category, $value) = @_;

    my $R = '';

    if (ref $value eq 'ARRAY' && $value->@*) {
        $R .= '<dt>' . xml_escape(uc $self->c->l($category)) . "</dt>\n";
        for ($value->@*) {
            $R .= '<dd>';
            $R .= xml_escape($_->{display_name} ? qq("$_->{display_name}" <$_->{address}>) : "$_->{address}");
            $R .= "<dd>\n";
        }
    }
    return $R;
}

sub render_message {
    my ($self, $subtype, $msg, $path) = @_;

    warn "unkown mime-subtype $subtype" unless $subtype eq 'rfc822';

    my $R .= '<div class="jwm-mail">';

    $R .= '<dl class="jwm-mail-header">';
    $R .= '<dt>' . xml_escape(uc $self->c->l('subject')) . '</dt>';
    $R .= '<dd>' . xml_escape($msg->{head}{subject}) . "</dd>\n";
    $R .= $self->_format_header(from => $msg->{head}{from});
    $R .= $self->_format_header(to   => $msg->{head}{to});
    $R .= $self->_format_header(cc   => $msg->{head}{cc});
    $R .= $self->_format_header(bcc  => $msg->{head}{bcc});
    $R .= '<dt>' . xml_escape(uc $self->c->l('date')) . '</dt>';
    $R .= '<dd>' . xml_escape($msg->{head}{date}) . "</dd>\n";
    $R .= '<dt>' . xml_escape(uc $self->c->l('content-type')) . '</dt>';
    $R .= '<dd>' . to_mime_type($msg->{head}{mime}) . "</dd>\n";
    $R .= "</dl>\n";

    $R .= $self->mime_render(to_mime_types($msg->{head}{mime}), $msg->{body}, [@$path, 0]);

    return $R . "</div>\n";
}

our %MIME_Render_Subs = (
    'text/plain'            => \&render_text_plain,
    'text/html'             => \&render_text_html,
    'multipart/alternative' => \&render_multipart_alternative,
    'multipart'             => \&render_multipart,
    'message'               => \&render_message,
    'image'                 => \&render_image,
);

sub mime_render {
    my ($self, $maintype, $subtype, $content, $path) = @_;

    my $renderer = $MIME_Render_Subs{"$maintype/$subtype"} || $MIME_Render_Subs{$maintype};

    unless ($renderer) {
        return qq(<p class="jwm-body-unsupported">Unsupported MIME type of <code>$maintype/$subtype</code>.</p>\n);
    }

    return $renderer->($self, $subtype, $content, $path);
}


sub to_mime_type  { lc xml_escape("$_[0]->{content_maintype}/$_[0]->{content_subtype}") }
sub to_mime_types { return xml_escape($_[0]->{content_maintype}), xml_escape($_[0]->{content_subtype}) }


sub format_mail {
    my ($self, $mail) = @_;

    return Mojo::ByteStream->new($self->mime_render('message', 'rfc822', $mail, []));
}

1

__END__

=encoding utf-8

=head1 NAME

JWebmail::View::RenderMail - Does the heavy lifting of converting an E-Mail to HTML

=head1 FUNCTIONS

=head2 to_mime_type

Combines the content_maintype and content_subtype attributes into the regular MIME description.
These attributes are found in a mail head mime section or as head for multipart messages.

=head1 METHODS

=head2 format_mail

Renders a mail to html recursively.