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/;
12 # TODO: make these constants tunable
13 use constant MAX_INLINE_QUOTED => 5;
14 use constant MAX_TRUNC_LEN => 72;
16 *ascii_html = *PublicInbox::Hval::ascii_html;
18 my $enc_utf8 = find_encoding('UTF-8');
19 my $enc_mime = find_encoding('MIME-Header');
23 my ($class, $mime, $full_pfx, $footer) = @_;
24 if (defined $footer) {
25 $footer = "\n" . $footer;
29 headers_to_html_header($mime, $full_pfx) .
30 multipart_text_as_html($mime, $full_pfx) .
32 html_footer($mime) . $footer .
33 '</pre></body></html>';
37 my ($class, $mime, $full_pfx) = @_;
39 '<pre>' . multipart_text_as_html($mime, $full_pfx) . '</pre>';
43 # only private functions below.
47 defined $ct or return $enc_utf8;
48 my $ct_parsed = parse_content_type($ct);
50 if (my $charset = $ct_parsed->{attributes}->{charset}) {
51 my $enc = find_encoding($charset);
58 sub multipart_text_as_html {
59 my ($mime, $full_pfx) = @_;
62 my $enc_msg = enc_for($mime->header("Content-Type"));
64 # scan through all parts, looking for displayable text
65 $mime->walk_parts(sub {
67 return if $part->subparts; # walk_parts already recurses
68 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
71 my $fn = $part->filename;
72 defined($fn) or $fn = "part #" . ($part_nr + 1);
73 $rv .= add_filename_line($enc->decode($fn));
76 if (defined $full_pfx) {
77 $rv .= add_text_body_short($enc, $part, $part_nr,
80 $rv .= add_text_body_full($enc, $part, $part_nr);
82 $rv .= "\n" unless $rv =~ /\n\z/s;
88 sub add_filename_line {
94 $pad x= ($len/2) if ($len > 0);
95 "$pad " . ascii_html($fn) . " $pad\n";
98 sub add_text_body_short {
99 my ($enc, $part, $part_nr, $full_pfx) = @_;
101 my $s = ascii_html($enc->decode($part->body));
102 # TODO: fold the "so-and-so wrote:" attribute line here, too:
103 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
105 my @lines = split(/\n/, $cur);
106 if (@lines > MAX_INLINE_QUOTED) {
107 # show a short snippet of quoted text
108 $cur = join(' ', @lines);
109 $cur =~ s/^>\s*//;
111 my @sum = split(/\s+/, $cur);
114 my $tmp = shift(@sum);
115 my $len = length($tmp) + length($cur);
116 if ($len > MAX_TRUNC_LEN) {
121 } while (@sum && length($cur) < MAX_TRUNC_LEN);
123 "> <<a href=\"${full_pfx}#q${part_nr}_" . $n++ .
124 "\">$cur<\/a>>\n";
132 sub add_text_body_full {
133 my ($enc, $part, $part_nr) = @_;
135 my $s = ascii_html($enc->decode($part->body));
136 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
138 my @lines = split(/\n/, $cur);
139 if (@lines > MAX_INLINE_QUOTED) {
140 "<a name=q${part_nr}_" . $n++ . ">$cur</a>";
148 sub headers_to_html_header {
149 my ($mime, $full_pfx) = @_;
153 foreach my $h (qw(From To Cc Subject Date)) {
154 my $v = $mime->header($h);
155 defined($v) && length($v) or next;
156 $v = PublicInbox::Hval->new_oneline($v);
157 $rv .= "$h: " . $v->as_html . "\n";
160 my @from = Email::Address->parse($v->raw);
162 unless (defined($v) && length($v)) {
163 $v = '<' . $from[0]->address . '>';
165 $title[1] = ascii_html($v);
166 } elsif ($h eq 'Subject') {
167 $title[0] = $v->as_html;
171 my $header_obj = $mime->header_obj;
172 my $mid = $header_obj->header_raw('Message-ID');
174 $mid = PublicInbox::Hval->new_msgid($mid);
175 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
176 my $href = $mid->as_href;
177 $href = "../m/$href" unless $full_pfx;
178 $rv .= "(<a href=\"$href.txt\">original</a>)\n";
181 my $irp = $header_obj->header_raw('In-Reply-To');
183 my $v = PublicInbox::Hval->new_msgid(my $tmp = $irp);
184 my $html = $v->as_html;
185 my $href = $v->as_href;
186 $rv .= "In-Reply-To: <";
187 $rv .= "<a href=\"$href.html\">$html</a>>\n";
190 my $refs = $header_obj->header_raw('References');
192 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
193 my @refs = ($refs =~ /<([^>]+)>/g);
195 $rv .= 'References: '. linkify_refs(@refs) . "\n";
201 ("<html><head><title>". join(' - ', @title) .
202 '</title></head><body><pre style="white-space:pre-wrap">' . $rv);
207 my %cc; # everyone else
208 my $to; # this is the From address
210 foreach my $h (qw(From To Cc)) {
211 my $v = $mime->header($h);
212 defined($v) && length($v) or next;
213 my @addrs = Email::Address->parse($v);
214 foreach my $recip (@addrs) {
215 my $address = $recip->address;
216 my $dst = lc($address);
217 $cc{$dst} ||= $address;
221 Email::Address->purge_cache;
223 my $subj = $mime->header('Subject') || '';
224 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
225 my $irp = uri_escape_utf8(
226 $mime->header_obj->header_raw('Message-ID') || '');
228 $to = uri_escape_utf8($to);
229 $subj = uri_escape_utf8($subj);
231 my $cc = uri_escape_utf8(join(',', values %cc));
232 my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
234 '<a href="' . ascii_html($href) . '">reply</a>';
239 my $v = PublicInbox::Hval->new_msgid($_);
240 my $html = $v->as_html;
241 my $href = $v->as_href;
242 "<<a href=\"$href.html\">$html</a>>";