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) = @_;
25 headers_to_html_header($mime, $full_pfx) .
26 multipart_text_as_html($mime, $full_pfx) .
29 '</pre></body></html>';
33 my ($class, $mime, $full_pfx) = @_;
35 '<pre>' . multipart_text_as_html($mime, $full_pfx) . '</pre>';
39 # only private functions below.
43 defined $ct or return $enc_utf8;
44 my $ct_parsed = parse_content_type($ct);
46 if (my $charset = $ct_parsed->{attributes}->{charset}) {
47 my $enc = find_encoding($charset);
54 sub multipart_text_as_html {
55 my ($mime, $full_pfx) = @_;
58 my $enc_msg = enc_for($mime->header("Content-Type"));
60 # scan through all parts, looking for displayable text
61 $mime->walk_parts(sub {
63 return if $part->subparts; # walk_parts already recurses
64 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
67 my $fn = $part->filename;
68 defined($fn) or $fn = "part #" . ($part_nr + 1);
69 $rv .= add_filename_line($enc->decode($fn));
72 if (defined $full_pfx) {
73 $rv .= add_text_body_short($enc, $part, $part_nr,
76 $rv .= add_text_body_full($enc, $part, $part_nr);
78 $rv .= "\n" unless $rv =~ /\n\z/s;
84 sub add_filename_line {
90 $pad x= ($len/2) if ($len > 0);
91 "$pad " . ascii_html($fn) . " $pad\n";
94 sub add_text_body_short {
95 my ($enc, $part, $part_nr, $full_pfx) = @_;
97 my $s = ascii_html($enc->decode($part->body));
98 # TODO: fold the "so-and-so wrote:" attribute line here, too:
99 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
101 my @lines = split(/\n/, $cur);
102 if (@lines > MAX_INLINE_QUOTED) {
103 # show a short snippet of quoted text
104 $cur = join(' ', @lines);
105 $cur =~ s/^>\s*//;
107 my @sum = split(/\s+/, $cur);
110 my $tmp = shift(@sum);
111 my $len = length($tmp) + length($cur);
112 if ($len > MAX_TRUNC_LEN) {
117 } while (@sum && length($cur) < MAX_TRUNC_LEN);
119 "> <<a href=\"${full_pfx}#q${part_nr}_" . $n++ .
120 "\">$cur<\/a>>\n";
128 sub add_text_body_full {
129 my ($enc, $part, $part_nr) = @_;
131 my $s = ascii_html($enc->decode($part->body));
132 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
134 my @lines = split(/\n/, $cur);
135 if (@lines > MAX_INLINE_QUOTED) {
136 "<a name=q${part_nr}_" . $n++ . ">$cur</a>";
144 sub headers_to_html_header {
145 my ($mime, $full_pfx) = @_;
149 foreach my $h (qw(From To Cc Subject Date)) {
150 my $v = $mime->header($h);
151 defined($v) && length($v) or next;
152 $v = PublicInbox::Hval->new_oneline($v);
153 $rv .= "$h: " . $v->as_html . "\n";
156 my @from = Email::Address->parse($v->raw);
158 unless (defined($v) && length($v)) {
159 $v = '<' . $from[0]->address . '>';
161 $title[1] = ascii_html($v);
162 } elsif ($h eq 'Subject') {
163 $title[0] = $v->as_html;
167 my $header_obj = $mime->header_obj;
168 my $mid = $header_obj->header_raw('Message-ID');
170 $mid = PublicInbox::Hval->new_msgid($mid);
171 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
172 my $href = $mid->as_href;
173 $href = "../m/$href" unless $full_pfx;
174 $rv .= "(<a href=\"$href.txt\">original</a>)\n";
177 my $irp = $header_obj->header_raw('In-Reply-To');
179 my $v = PublicInbox::Hval->new_msgid(my $tmp = $irp);
180 my $html = $v->as_html;
181 my $href = $v->as_href;
182 $rv .= "In-Reply-To: <";
183 $rv .= "<a href=\"$href.html\">$html</a>>\n";
186 my $refs = $header_obj->header_raw('References');
188 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
189 my @refs = ($refs =~ /<([^>]+)>/g);
191 $rv .= 'References: '. linkify_refs(@refs) . "\n";
197 ("<html><head><title>". join(' - ', @title) .
198 '</title></head><body><pre style="white-space:pre-wrap">' . $rv);
203 my %cc; # everyone else
204 my $to; # this is the From address
206 foreach my $h (qw(From To Cc)) {
207 my $v = $mime->header($h);
208 defined($v) && length($v) or next;
209 my @addrs = Email::Address->parse($v);
210 foreach my $recip (@addrs) {
211 my $address = $recip->address;
212 my $dst = lc($address);
213 $cc{$dst} ||= $address;
217 Email::Address->purge_cache;
219 my $subj = $mime->header('Subject') || '';
220 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
221 my $irp = uri_escape_utf8(
222 $mime->header_obj->header_raw('Message-ID') || '');
224 $to = uri_escape_utf8($to);
225 $subj = uri_escape_utf8($subj);
227 my $cc = uri_escape_utf8(join(',', values %cc));
228 my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
230 '<a href="' . ascii_html($href) . '">reply</a>';
235 my $v = PublicInbox::Hval->new_msgid($_);
236 my $html = $v->as_html;
237 my $href = $v->as_href;
238 "<<a href=\"$href.html\">$html</a>>";