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;
15 use constant PRE_WRAP => '<pre style="white-space:pre-wrap">';
17 *ascii_html = *PublicInbox::Hval::ascii_html;
19 my $enc_utf8 = find_encoding('UTF-8');
20 my $enc_mime = find_encoding('MIME-Header');
24 my ($class, $mime, $full_pfx, $footer) = @_;
25 if (defined $footer) {
26 $footer = "\n" . $footer;
30 headers_to_html_header($mime, $full_pfx) .
31 multipart_text_as_html($mime, $full_pfx) .
32 '</pre><hr />' . PRE_WRAP .
33 html_footer($mime, 1) . $footer .
34 '</pre></body></html>';
38 my ($class, $mime, $full_pfx) = @_;
40 PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
44 # only private functions below.
48 defined $ct or return $enc_utf8;
49 my $ct_parsed = parse_content_type($ct);
51 if (my $charset = $ct_parsed->{attributes}->{charset}) {
52 my $enc = find_encoding($charset);
59 sub multipart_text_as_html {
60 my ($mime, $full_pfx) = @_;
63 my $enc_msg = enc_for($mime->header("Content-Type"));
65 # scan through all parts, looking for displayable text
66 $mime->walk_parts(sub {
68 return if $part->subparts; # walk_parts already recurses
69 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
72 my $fn = $part->filename;
73 defined($fn) or $fn = "part #" . ($part_nr + 1);
74 $rv .= add_filename_line($enc->decode($fn));
77 if (defined $full_pfx) {
78 $rv .= add_text_body_short($enc, $part, $part_nr,
81 $rv .= add_text_body_full($enc, $part, $part_nr);
83 $rv .= "\n" unless $rv =~ /\n\z/s;
89 sub add_filename_line {
95 $pad x= ($len/2) if ($len > 0);
96 "$pad " . ascii_html($fn) . " $pad\n";
99 sub add_text_body_short {
100 my ($enc, $part, $part_nr, $full_pfx) = @_;
102 my $s = ascii_html($enc->decode($part->body));
103 # TODO: fold the "so-and-so wrote:" attribute line here, too:
104 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
106 my @lines = split(/\n/, $cur);
107 if (@lines > MAX_INLINE_QUOTED) {
108 # show a short snippet of quoted text
109 $cur = join(' ', @lines);
110 $cur =~ s/^>\s*//;
112 my @sum = split(/\s+/, $cur);
115 my $tmp = shift(@sum);
116 my $len = length($tmp) + length($cur);
117 if ($len > MAX_TRUNC_LEN) {
122 } while (@sum && length($cur) < MAX_TRUNC_LEN);
124 "> <<a href=\"${full_pfx}#q${part_nr}_" . $n++ .
125 "\">$cur<\/a>>\n";
133 sub add_text_body_full {
134 my ($enc, $part, $part_nr) = @_;
136 my $s = ascii_html($enc->decode($part->body));
137 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
139 my @lines = split(/\n/, $cur);
140 if (@lines > MAX_INLINE_QUOTED) {
141 "<a name=q${part_nr}_" . $n++ . ">$cur</a>";
149 sub headers_to_html_header {
150 my ($mime, $full_pfx) = @_;
154 foreach my $h (qw(From To Cc Subject Date)) {
155 my $v = $mime->header($h);
156 defined($v) && length($v) or next;
157 $v = PublicInbox::Hval->new_oneline($v);
158 $rv .= "$h: " . $v->as_html . "\n";
161 my @from = Email::Address->parse($v->raw);
163 unless (defined($v) && length($v)) {
164 $v = '<' . $from[0]->address . '>';
166 $title[1] = ascii_html($v);
167 } elsif ($h eq 'Subject') {
168 $title[0] = $v->as_html;
172 my $header_obj = $mime->header_obj;
173 my $mid = $header_obj->header_raw('Message-ID');
175 $mid = PublicInbox::Hval->new_msgid($mid);
176 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
177 my $href = $mid->as_href;
178 $href = "../m/$href" unless $full_pfx;
179 $rv .= "(<a href=\"$href.txt\">original</a>)\n";
182 my $irp = $header_obj->header_raw('In-Reply-To');
184 my $v = PublicInbox::Hval->new_msgid(my $tmp = $irp);
185 my $html = $v->as_html;
186 my $href = $v->as_href;
187 $rv .= "In-Reply-To: <";
188 $rv .= "<a href=\"$href.html\">$html</a>>\n";
191 my $refs = $header_obj->header_raw('References');
193 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
194 my @refs = ($refs =~ /<([^>]+)>/g);
196 $rv .= 'References: '. linkify_refs(@refs) . "\n";
202 ("<html><head><title>". join(' - ', @title) .
203 '</title></head><body>' . PRE_WRAP . $rv);
207 my ($mime, $purge) = @_;
208 my %cc; # everyone else
209 my $to; # this is the From address
211 foreach my $h (qw(From To Cc)) {
212 my $v = $mime->header($h);
213 defined($v) && length($v) or next;
214 my @addrs = Email::Address->parse($v);
215 foreach my $recip (@addrs) {
216 my $address = $recip->address;
217 my $dst = lc($address);
218 $cc{$dst} ||= $address;
222 Email::Address->purge_cache if $purge;
224 my $subj = $mime->header('Subject') || '';
225 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
226 my $irp = uri_escape_utf8(
227 $mime->header_obj->header_raw('Message-ID') || '');
229 $to = uri_escape_utf8($to);
230 $subj = uri_escape_utf8($subj);
232 my $cc = uri_escape_utf8(join(',', values %cc));
233 my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
235 '<a href="' . ascii_html($href) . '">reply</a>';
240 my $v = PublicInbox::Hval->new_msgid($_);
241 my $html = $v->as_html;
242 my $href = $v->as_href;
243 "<<a href=\"$href.html\">$html</a>>";