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_utf8/;
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) .
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 ($mime, $full_pfx) = @_;
145 foreach my $h (qw(From To Cc Subject Date)) {
146 my $v = $mime->header($h);
147 defined($v) && length($v) or next;
148 $v = PublicInbox::Hval->new_oneline($v);
149 $rv .= "$h: " . $v->as_html . "\n";
152 my @from = Email::Address->parse($v->raw);
154 unless (defined($v) && length($v)) {
155 $v = '<' . $from[0]->address . '>';
157 $title[1] = ascii_html($v);
158 } elsif ($h eq 'Subject') {
159 $title[0] = $v->as_html;
163 my $header_obj = $mime->header_obj;
164 my $mid = $header_obj->header_raw('Message-ID');
166 $mid = PublicInbox::Hval->new_msgid($mid);
167 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
168 my $href = $mid->as_href;
169 $href = "../m/$href" unless $full_pfx;
170 $rv .= "(<a href=\"$href.txt\">original</a>)\n";
173 my $irp = $header_obj->header_raw('In-Reply-To');
175 my $v = PublicInbox::Hval->new_msgid(my $tmp = $irp);
176 my $html = $v->as_html;
177 my $href = $v->as_href;
178 $rv .= "In-Reply-To: <";
179 $rv .= "<a href=\"$href.html\">$html</a>>\n";
182 my $refs = $header_obj->header_raw('References');
184 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
185 my @refs = ($refs =~ /<([^>]+)>/g);
187 $rv .= 'References: '. linkify_refs(@refs) . "\n";
193 ("<html><head><title>". join(' - ', @title) .
194 '</title></head><body><pre style="white-space:pre-wrap">' . $rv);
199 my %cc; # everyone else
200 my $to; # this is the From address
202 foreach my $h (qw(From To Cc)) {
203 my $v = $mime->header($h);
204 defined($v) && length($v) or next;
205 my @addrs = Email::Address->parse($v);
206 foreach my $recip (@addrs) {
207 my $address = $recip->address;
208 my $dst = lc($address);
209 $cc{$dst} ||= $address;
213 Email::Address->purge_cache;
215 my $subj = $mime->header('Subject') || '';
216 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
217 my $irp = uri_escape_utf8(
218 $mime->header_obj->header_raw('Message-ID') || '');
220 $to = uri_escape_utf8($to);
221 $subj = uri_escape_utf8($subj);
223 my $cc = uri_escape_utf8(join(',', values %cc));
224 my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
226 '<a href="' . ascii_html($href) . '">reply</a>';
231 my $v = PublicInbox::Hval->new_msgid($_);
232 my $html = $v->as_html;
233 my $href = $v->as_href;
234 "<<a href=\"$href.html\">$html</a>>";