package JWebmail::Plugin::RenderMail;
use Mojo::Base 'Mojolicious::Plugin';
use Mojo::ByteStream;
use Mojo::Util 'xml_escape';
sub render_text_plain {
my ($_c, $_subtype, $content, $_path) = @_;
$content = xml_escape($content);
$content =~ s/\n/
/g;
return qq'
';
my $i = 0;
my $end;
for (reverse @$parts) {
if (!$end) {
my $x = mime_render($c, to_mime_types($_->{head}), $_->{body}, [@$path, $#$parts-$i]);
if ($x) {
$R .= $x;
$end = 1;
}
}
else {
$R .= '';
$R .= '';
$R .= to_mime_type($_->{head});
$R .= "
\n";
$R .= mime_render($c, to_mime_types($_->{head}), $_->{body}, [@$path, $#$parts-$i]);
$R .= " \n";
}
++$i;
}
return $R . "
\n";
}
sub render_multipart {
my ($c, $_subtype, $content, $path) = @_;
my $parts = $content->{parts};
my $R = qq'';
my $i = 0;
for (@$parts) {
if ( !$_->{head}{content_disposition}
|| lc $_->{head}{content_disposition} eq 'none'
|| lc $_->{head}{content_disposition} eq 'inline') {
$R .= mime_render($c, to_mime_types($_->{head}), $_->{body}, [@$path, $i]);
}
elsif (lc $_->{head}{content_disposition} eq 'attachment') {
$R .= '
';
$R .= $c->link_to($c->url_for(raw => id => $c->stash('id'))->query(path => join('.', @$path, $i)), (download => $_->{head}{filename}) => sub {
'Attachment ' . xml_escape($_->{head}{filename}) . ' of type ' . to_mime_type($_->{head});
});
$R .= "
\n";
}
else {
warn "unknown Content-Disposition '$_->{head}{content_disposition}'";
$R .= "
unknown Content-Disposition '$_->{head}{content_disposition}'
\n";
}
++$i;
}
return $R . "
\n";
}
sub _format_header {
my ($c, $category, $value) = @_;
my $R = '';
if (ref $value eq 'ARRAY' && $value->@*) {
$R .= '