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/;
13 # TODO: make these constants tunable
14 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
15 use constant MAX_TRUNC_LEN => 72;
16 use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
18 *ascii_html = *PublicInbox::Hval::ascii_html;
20 my $enc_utf8 = find_encoding('UTF-8');
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>';
43 # this is already inside a <pre>
45 my ($class, $mime, $level, $state) = @_;
46 my ($now, $seen, $first) = @$state;
47 my $midx = $state->[3]++;
48 my ($prev, $next) = ($midx - 1, $midx + 1);
51 my $enc_msg = enc_for($mime->header("Content-Type"));
52 my $subj = $mime->header('Subject');
53 my $header_obj = $mime->header_obj;
55 my $mid_raw = $header_obj->header_raw('Message-ID');
56 my $id = anchor_for($mid_raw);
57 $seen->{$id} = "#$id"; # save the anchor for later
59 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
60 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
61 my @from = Email::Address->parse($from);
62 $from = $from[0]->name;
63 (defined($from) && length($from)) or $from = $from[0]->address;
65 $from = PublicInbox::Hval->new_oneline($from)->as_html;
66 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
67 my $pfx = (' ' x $level);
69 my $ts = $mime->header('X-PI-Date');
70 my $fmt = '%Y-%m-%d %H:%M UTC';
71 $ts = POSIX::strftime($fmt, gmtime($ts));
73 $rv .= "$pfx<b\nid=\"$id\">$subj</b>\n$pfx";
74 $rv .= "- by $from @ $ts - ";
75 $rv .= "<a\nid=\"s$midx\"\nhref=\"#s$next\">next</a>";
77 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
81 my $irp = $header_obj->header_raw('In-Reply-To');
82 my ($anchor_idx, $anchor);
84 $anchor_idx = anchor_for($irp);
85 $anchor = $seen->{$anchor_idx};
87 my $href = $mid->as_href;
88 my $mhref = "m/$href.html";
89 my $fhref = "f/$href.html";
91 # scan through all parts, looking for displayable text
92 $mime->walk_parts(sub {
93 $rv .= index_walk($_[0], $pfx, $enc_msg, $part_nr, $fhref,
98 $rv .= "\n$pfx<a\nhref=\"$mhref\">$more</a> ";
99 my $txt = "m/$href.txt";
100 $rv .= "<a\nhref=\"$txt\">raw</a> ";
101 $rv .= html_footer($mime, 0);
104 unless (defined $anchor) {
105 my $v = PublicInbox::Hval->new_msgid($irp);
106 my $html = $v->as_html;
107 $anchor = 'm/' . $v->as_href . '.html';
108 $seen->{$anchor_idx} = $anchor;
110 $rv .= " <a\nhref=\"$anchor\">parent</a>";
112 $rv .= " <a\nhref=\"?r=$first#$id\">threadlink</a>";
117 # only private functions below.
120 my ($part, $pfx, $enc_msg, $part_nr, $fhref, $more) = @_;
122 return $rv if $part->subparts; # walk_parts already recurses
123 my $ct = $part->content_type;
125 # account for filter bugs...
126 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
128 my $enc = enc_for($ct, $enc_msg);
131 my $fn = $part->filename;
132 defined($fn) or $fn = "part #" . ($part_nr + 1);
133 $rv .= $pfx . add_filename_line($enc->decode($fn));
136 my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
138 # drop the remainder of git patches, they're usually better
139 # to review when the full message is viewed
140 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
143 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
145 # kill any leading or trailing whitespace lines
153 # kill per-line trailing whitespace
154 $s =~ s/[ \t]+$//sgm;
162 my ($ct, $default) = @_;
163 $default ||= $enc_utf8;
164 defined $ct or return $default;
165 my $ct_parsed = parse_content_type($ct);
167 if (my $charset = $ct_parsed->{attributes}->{charset}) {
168 my $enc = find_encoding($charset);
175 sub multipart_text_as_html {
176 my ($mime, $full_pfx) = @_;
179 my $enc_msg = enc_for($mime->header("Content-Type"));
181 # scan through all parts, looking for displayable text
182 $mime->walk_parts(sub {
184 return if $part->subparts; # walk_parts already recurses
185 my $ct = $part->content_type;
187 # account for filter bugs...
188 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
190 my $enc = enc_for($ct, $enc_msg);
193 my $fn = $part->filename;
194 defined($fn) or $fn = "part #" . ($part_nr + 1);
195 $rv .= add_filename_line($enc->decode($fn));
198 if (defined $full_pfx) {
199 $rv .= add_text_body_short($enc, $part, $part_nr,
202 $rv .= add_text_body_full($enc, $part, $part_nr);
204 $rv .= "\n" unless $rv =~ /\n\z/s;
210 sub add_filename_line {
216 $pad x= ($len/2) if ($len > 0);
217 "$pad " . ascii_html($fn) . " $pad\n";
220 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!;
223 # no newlines added here since it'd break the splitting we do
225 $_[0] =~ s!$LINK_RE!<a href="$1">$1</a>!g;
228 sub add_text_body_short {
229 my ($enc, $part, $part_nr, $full_pfx) = @_;
231 my $s = ascii_html($enc->decode($part->body));
233 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
235 my @lines = split(/\n/, $cur);
236 if (@lines > MAX_INLINE_QUOTED) {
237 # show a short snippet of quoted text
238 $cur = join(' ', @lines);
239 $cur =~ s/^>\s*//;
241 my @sum = split(/\s+/, $cur);
244 my $tmp = shift(@sum);
245 my $len = length($tmp) + length($cur);
246 if ($len > MAX_TRUNC_LEN) {
251 } while (@sum && length($cur) < MAX_TRUNC_LEN);
253 "> <<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
254 "\">$cur<\/a>>\n";
262 sub add_text_body_full {
263 my ($enc, $part, $part_nr) = @_;
265 my $s = ascii_html($enc->decode($part->body));
267 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
269 my @lines = split(/\n/, $cur);
270 if (@lines > MAX_INLINE_QUOTED) {
271 "<a\nid=q${part_nr}_" . $n++ . ">$cur</a>";
279 sub headers_to_html_header {
280 my ($mime, $full_pfx) = @_;
284 foreach my $h (qw(From To Cc Subject Date)) {
285 my $v = $mime->header($h);
286 defined($v) && length($v) or next;
287 $v = PublicInbox::Hval->new_oneline($v);
288 $rv .= "$h: " . $v->as_html . "\n";
291 my @from = Email::Address->parse($v->raw);
293 unless (defined($v) && length($v)) {
294 $v = '<' . $from[0]->address . '>';
296 $title[1] = ascii_html($v);
297 } elsif ($h eq 'Subject') {
298 $title[0] = $v->as_html;
302 my $header_obj = $mime->header_obj;
303 my $mid = $header_obj->header_raw('Message-ID');
305 $mid = PublicInbox::Hval->new_msgid($mid);
306 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
307 my $href = $mid->as_href;
308 $href = "../m/$href" unless $full_pfx;
309 $rv .= "(<a\nhref=\"$href.txt\">raw</a>)\n";
312 my $irp = $header_obj->header_raw('In-Reply-To');
314 my $v = PublicInbox::Hval->new_msgid($irp);
315 my $html = $v->as_html;
316 my $href = $v->as_href;
317 $rv .= "In-Reply-To: <";
318 $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
321 my $refs = $header_obj->header_raw('References');
323 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
324 my @refs = ($refs =~ /<([^>]+)>/g);
326 $rv .= 'References: '. linkify_refs(@refs) . "\n";
332 ("<html><head><title>". join(' - ', @title) .
333 '</title></head><body>' . PRE_WRAP . $rv);
337 my ($mime, $standalone) = @_;
338 my %cc; # everyone else
339 my $to; # this is the From address
341 foreach my $h (qw(From To Cc)) {
342 my $v = $mime->header($h);
343 defined($v) && length($v) or next;
344 my @addrs = Email::Address->parse($v);
345 foreach my $recip (@addrs) {
346 my $address = $recip->address;
347 my $dst = lc($address);
348 $cc{$dst} ||= $address;
352 Email::Address->purge_cache if $standalone;
354 my $subj = $mime->header('Subject') || '';
355 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
356 my $irp = uri_escape_utf8(
357 $mime->header_obj->header_raw('Message-ID') || '');
359 $to = uri_escape_utf8($to);
360 $subj = uri_escape_utf8($subj);
362 my $cc = uri_escape_utf8(join(',', sort values %cc));
363 my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
365 my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
367 "<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
372 my $v = PublicInbox::Hval->new_msgid($_);
373 my $html = $v->as_html;
374 my $href = $v->as_href;
375 "<<a\nhref=\"$href.html\">$html</a>>";
382 $msgid =~ s/\A\s*<?//;
383 $msgid =~ s/>?\s*\z//;
384 'm' . Digest::SHA::sha1_hex($msgid);