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 CGI qw/escapeHTML/;
9 use Encode qw/find_encoding/;
10 use Encode::MIME::Header;
11 use Email::MIME::ContentType qw/parse_content_type/;
12 use constant MAX_INLINE_QUOTED => 5;
13 use constant MAX_TRUNC_LEN => 72;
14 *ascii_html = *PublicInbox::Hval::ascii_html;
16 my $enc_utf8 = find_encoding('utf8');
17 my $enc_ascii = find_encoding('us-ascii');
18 my $enc_mime = find_encoding('MIME-Header');
22 my ($class, $mime, $full_pfx) = @_;
24 headers_to_html_header($mime, $full_pfx) .
25 multipart_text_as_html($mime, $full_pfx) .
26 '</pre></body></html>';
30 my ($class, $mime, $full_pfx) = @_;
32 "<pre>" . multipart_text_as_html($mime, $full_pfx) . "</pre>";
36 # only private functions below.
40 defined $ct or return $enc_utf8;
41 my $ct_parsed = parse_content_type($ct);
43 if (my $charset = $ct_parsed->{attributes}->{charset}) {
44 my $enc = find_encoding($charset);
51 sub multipart_text_as_html {
52 my ($mime, $full_pfx) = @_;
55 my $enc_msg = enc_for($mime->header("Content-Type"));
57 # scan through all parts, looking for displayable text
58 $mime->walk_parts(sub {
60 return if $part->subparts; # walk_parts already recurses
61 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
64 my $fn = $part->filename;
65 defined($fn) or $fn = "part #" . ($part_nr + 1);
66 $rv .= add_filename_line($enc->decode($fn));
69 if (defined $full_pfx) {
70 $rv .= add_text_body_short($enc, $part, $part_nr,
73 $rv .= add_text_body_full($enc, $part, $part_nr);
75 $rv .= "\n" unless $rv =~ /\n\z/s;
81 sub add_filename_line {
87 $pad x= ($len/2) if ($len > 0);
88 "$pad " . ascii_html($fn) . " $pad\n";
91 sub add_text_body_short {
92 my ($enc, $part, $part_nr, $full_pfx) = @_;
94 my $s = ascii_html($enc->decode($part->body));
95 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
97 my @lines = split(/\n/, $cur);
98 if (@lines > MAX_INLINE_QUOTED) {
99 # show a short snippet of quoted text
100 $cur = join(' ', @lines);
101 $cur =~ s/^>\s*//;
103 my @sum = split(/\s+/, $cur);
106 my $tmp = shift(@sum);
107 my $len = length($tmp) + length($cur);
108 if ($len > MAX_TRUNC_LEN) {
113 } while (@sum && length($cur) < MAX_TRUNC_LEN);
115 "> <<a href=\"${full_pfx}#q${part_nr}_" . $n++ .
116 "\">$cur<\/a>>\n";
124 sub add_text_body_full {
125 my ($enc, $part, $part_nr) = @_;
127 my $s = ascii_html($enc->decode($part->body));
128 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
130 my @lines = split(/\n/, $cur);
131 if (@lines > MAX_INLINE_QUOTED) {
132 "<a name=q${part_nr}_" . $n++ . ">$cur</a>";
140 sub headers_to_html_header {
141 my ($simple, $full_pfx) = @_;
145 foreach my $h (qw(From To Cc Subject Date)) {
146 my $v = $simple->header($h);
150 my $raw = $enc_mime->decode($v);
151 $v = ascii_html($raw);
155 my @from = Email::Address->parse($raw);
156 $raw = $from[0]->name;
157 unless (defined($raw) && length($raw)) {
158 $raw = '<' . $from[0]->address . '>';
160 $title[1] = ascii_html($raw);
162 } elsif ($h eq 'Subject') {
167 my $mid = $simple->header('Message-ID');
169 $mid = PublicInbox::Hval->new_msgid($mid);
170 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
171 my $href = $mid->as_href;
172 $href = "../m/$href" unless $full_pfx;
173 $rv .= "(<a href=\"$href.txt\">original</a>)\n";
176 my $irp = $simple->header('In-Reply-To');
178 $irp = PublicInbox::Hval->new_msgid($irp);
179 my $html = $irp->as_html;
180 my $href = $irp->as_href;
181 $rv .= "In-Reply-To: <";
182 $rv .= "<a href=\"$href.html\">$html</a>>\n";
186 ("<html><head><title>". join(' - ', @title) .
187 '</title></head><body><pre style="white-space:pre-wrap">' . $rv);