1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::View;
7 use URI::Escape qw/uri_escape/;
8 use Encode qw/find_encoding/;
9 use Encode::MIME::Header;
10 use Email::MIME::ContentType qw/parse_content_type/;
11 use constant MAX_INLINE_QUOTED => 5;
12 use constant MAX_TRUNC_LEN => 72;
13 *ascii_html = *PublicInbox::Hval::ascii_html;
15 my $enc_utf8 = find_encoding('UTF-8');
16 my $enc_mime = find_encoding('MIME-Header');
20 my ($class, $mime, $full_pfx) = @_;
22 headers_to_html_header($mime, $full_pfx) .
23 multipart_text_as_html($mime, $full_pfx) .
24 '</pre></body></html>';
28 my ($class, $mime, $full_pfx) = @_;
30 "<pre>" . multipart_text_as_html($mime, $full_pfx) . "</pre>";
34 # only private functions below.
38 defined $ct or return $enc_utf8;
39 my $ct_parsed = parse_content_type($ct);
41 if (my $charset = $ct_parsed->{attributes}->{charset}) {
42 my $enc = find_encoding($charset);
49 sub multipart_text_as_html {
50 my ($mime, $full_pfx) = @_;
53 my $enc_msg = enc_for($mime->header("Content-Type"));
55 # scan through all parts, looking for displayable text
56 $mime->walk_parts(sub {
58 return if $part->subparts; # walk_parts already recurses
59 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
62 my $fn = $part->filename;
63 defined($fn) or $fn = "part #" . ($part_nr + 1);
64 $rv .= add_filename_line($enc->decode($fn));
67 if (defined $full_pfx) {
68 $rv .= add_text_body_short($enc, $part, $part_nr,
71 $rv .= add_text_body_full($enc, $part, $part_nr);
73 $rv .= "\n" unless $rv =~ /\n\z/s;
79 sub add_filename_line {
85 $pad x= ($len/2) if ($len > 0);
86 "$pad " . ascii_html($fn) . " $pad\n";
89 sub add_text_body_short {
90 my ($enc, $part, $part_nr, $full_pfx) = @_;
92 my $s = ascii_html($enc->decode($part->body));
93 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
95 my @lines = split(/\n/, $cur);
96 if (@lines > MAX_INLINE_QUOTED) {
97 # show a short snippet of quoted text
98 $cur = join(' ', @lines);
101 my @sum = split(/\s+/, $cur);
104 my $tmp = shift(@sum);
105 my $len = length($tmp) + length($cur);
106 if ($len > MAX_TRUNC_LEN) {
111 } while (@sum && length($cur) < MAX_TRUNC_LEN);
113 "> <<a href=\"${full_pfx}#q${part_nr}_" . $n++ .
114 "\">$cur<\/a>>\n";
122 sub add_text_body_full {
123 my ($enc, $part, $part_nr) = @_;
125 my $s = ascii_html($enc->decode($part->body));
126 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
128 my @lines = split(/\n/, $cur);
129 if (@lines > MAX_INLINE_QUOTED) {
130 "<a name=q${part_nr}_" . $n++ . ">$cur</a>";
138 sub headers_to_html_header {
139 my ($mime, $full_pfx) = @_;
143 foreach my $h (qw(From To Cc Subject Date)) {
144 my $v = $mime->header($h);
145 defined($v) && length($v) or next;
146 $v = PublicInbox::Hval->new_oneline($v);
147 $rv .= "$h: " . $v->as_html . "\n";
150 my @from = Email::Address->parse($v->raw);
152 unless (defined($v) && length($v)) {
153 $v = '<' . $from[0]->address . '>';
155 $title[1] = ascii_html($v);
156 } elsif ($h eq 'Subject') {
157 $title[0] = $v->as_html;
161 my $header_obj = $mime->header_obj;
162 my $mid = $header_obj->header_raw('Message-ID');
164 $mid = PublicInbox::Hval->new_msgid($mid);
165 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
166 my $href = $mid->as_href;
167 $href = "../m/$href" unless $full_pfx;
168 $rv .= "(<a href=\"$href.txt\">original</a>)\n";
171 my $irp = $header_obj->header_raw('In-Reply-To');
173 $irp = PublicInbox::Hval->new_msgid($irp);
174 my $html = $irp->as_html;
175 my $href = $irp->as_href;
176 $rv .= "In-Reply-To: <";
177 $rv .= "<a href=\"$href.html\">$html</a>>\n";
181 ("<html><head><title>". join(' - ', @title) .
182 '</title></head><body><pre style="white-space:pre-wrap">' . $rv);