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;
6 use URI::Escape qw/uri_escape_utf8/;
7 use Date::Parse qw/str2time/;
8 use Encode qw/find_encoding/;
9 use Encode::MIME::Header;
10 use Email::MIME::ContentType qw/parse_content_type/;
11 use PublicInbox::Hval;
12 use PublicInbox::MID qw/mid_clean mid_compress mid2path/;
13 use Digest::SHA qw/sha1_hex/;
17 # TODO: make these constants tunable
18 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
19 use constant MAX_TRUNC_LEN => 72;
20 use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
21 use constant T_ANCHOR => '#u';
23 *ascii_html = *PublicInbox::Hval::ascii_html;
25 my $enc_utf8 = find_encoding('UTF-8');
29 my ($ctx, $mime, $full_pfx, $footer) = @_;
30 if (defined $footer) {
31 $footer = "\n" . $footer;
35 headers_to_html_header($mime, $full_pfx, $ctx) .
36 multipart_text_as_html($mime, $full_pfx) .
37 '</pre><hr />' . PRE_WRAP .
38 html_footer($mime, 1, $full_pfx, $ctx) .
40 '</pre></body></html>';
44 my ($class, $mime, $full_pfx) = @_;
46 PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
49 # this is already inside a <pre>
51 my ($fh, $mime, $level, $state) = @_;
52 my $midx = $state->{anchor_idx}++;
53 my $ctx = $state->{ctx};
54 my $srch = $ctx->{srch};
55 my ($prev, $next) = ($midx - 1, $midx + 1);
57 my $enc = enc_for($mime->header("Content-Type"));
58 my $subj = $mime->header('Subject');
59 my $header_obj = $mime->header_obj;
61 my $mid_raw = $header_obj->header('Message-ID');
62 my $id = anchor_for($mid_raw);
63 my $seen = $state->{seen};
64 $seen->{$id} = "#$id"; # save the anchor for later
66 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
67 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
68 my @from = Email::Address->parse($from);
69 $from = $from[0]->name;
71 $from = PublicInbox::Hval->new_oneline($from)->as_html;
72 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
73 my $more = 'permalink';
74 my $root_anchor = $state->{root_anchor};
75 my $path = $root_anchor ? '../../' : '';
76 my $href = $mid->as_href;
77 my $irt = $header_obj->header('In-Reply-To');
78 my ($anchor_idx, $anchor);
80 $anchor_idx = anchor_for($irt);
81 $anchor = $seen->{$anchor_idx};
84 $subj = "<a\nhref=\"${path}$href/t/#u\">$subj</a>";
86 if ($root_anchor && $root_anchor eq $id) {
87 $subj = "<u\nid=\"u\">$subj</u>";
90 my $ts = _msg_date($mime);
91 my $rv = "<table\nsummary=l$level><tr>";
93 $rv .= '<td><pre>' . (' ' x $level) . '</pre></td>';
95 $rv .= "<td\nid=s$midx>" . PRE_WRAP;
96 $rv .= "<b\nid=\"$id\">$subj</b>\n";
97 $rv .= "- by $from @ $ts UTC - ";
98 $rv .= "<a\nhref=\"#s$next\">next</a>";
100 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
102 $fh->write($rv .= "\n\n");
104 my ($fhref, $more_ref);
105 my $mhref = "${path}$href/";
107 $fhref = "${path}$href/f/";
110 # scan through all parts, looking for displayable text
111 $mime->walk_parts(sub {
112 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
116 my $txt = "${path}$href/raw";
117 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
118 $rv .= html_footer($mime, 0, undef, $ctx);
121 unless (defined $anchor) {
122 my $v = PublicInbox::Hval->new_msgid($irt);
124 $anchor = "${path}$v/";
125 $seen->{$anchor_idx} = $anchor;
127 $rv .= " <a\nhref=\"$anchor\">parent</a>";
130 $fh->write($rv .= '</pre></td></tr></table>');
134 my ($ctx, $foot, $srch) = @_;
135 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
138 # only private functions below.
140 sub emit_thread_html {
141 my ($cb, $ctx, $foot, $srch) = @_;
142 my $mid = mid_compress($ctx->{mid});
143 my $res = $srch->get_thread($mid);
144 my $msgs = load_results($res);
145 my $nr = scalar @$msgs;
146 return missing_thread($cb) if $nr == 0;
147 my $fh = $cb->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
148 my $th = thread_results($msgs);
152 root_anchor => anchor_for($mid),
156 require PublicInbox::GitCatFile;
157 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
158 thread_entry($fh, $git, $state, $_, 0) for $th->rootset;
160 Email::Address->purge_cache;
161 my $final_anchor = $state->{anchor_idx};
162 my $next = "<a\nid=\"s$final_anchor\">";
163 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
164 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>\n";
165 $next .= "download thread: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
166 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>\n\n";
167 $fh->write("<hr />" . PRE_WRAP . $next . $foot .
168 "</pre></body></html>");
173 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
174 my $s = add_text_body($enc, $part, $part_nr, $fhref);
177 # drop the remainder of git patches, they're usually better
178 # to review when the full message is viewed
179 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
182 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
185 # kill any leading or trailing whitespace lines
190 # kill per-line trailing whitespace
191 $s =~ s/[ \t]+$//sgm;
192 $s .= "\n" unless $s =~ /\n\z/s;
198 my ($ct, $default) = @_;
199 $default ||= $enc_utf8;
200 defined $ct or return $default;
201 my $ct_parsed = parse_content_type($ct);
203 if (my $charset = $ct_parsed->{attributes}->{charset}) {
204 my $enc = find_encoding($charset);
211 sub multipart_text_as_html {
212 my ($mime, $full_pfx, $srch) = @_;
215 my $enc = enc_for($mime->header("Content-Type"));
217 # scan through all parts, looking for displayable text
218 $mime->walk_parts(sub {
220 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx);
226 sub add_filename_line {
230 $fn = $enc->decode($fn);
232 $pad x= ($len/2) if ($len > 0);
233 "$pad " . ascii_html($fn) . " $pad\n";
236 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://
238 ?[\@\w\+\&\?\.\%\;/#=-]*)!x;
241 my ($link_map, $s) = @_;
244 # salt this, as this could be exploited to show
245 # links in the HTML which don't show up in the raw mail.
246 my $key = sha1_hex($url . $SALT);
247 $link_map->{$key} = $url;
254 my ($link_map, $s) = @_;
256 # Added "PI-LINK-" prefix to avoid false-positives on git commits
257 $s =~ s!\bPI-LINK-([a-f0-9]{40})\b!
259 my $url = $link_map->{$key};
261 $url = ascii_html($url);
262 "<a\nhref=\"$url\">$url</a>";
264 # false positive or somebody tried to mess with us
272 my ($quot, $n, $part_nr, $full_pfx, $final) = @_;
275 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
278 my $rv = join('', map { linkify_1(\%l, $_) } @$quot);
280 $rv = ascii_html($rv);
281 return linkify_2(\%l, $rv);
284 # show a short snippet of quoted text and link to full version:
285 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
286 my $cur = join(' ', @$quot);
287 @$quot = split(/\s+/, $cur);
290 my $tmp = shift(@$quot);
291 my $len = length($tmp) + length($cur);
292 if ($len > MAX_TRUNC_LEN) {
297 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
299 $cur =~ s/ \z/ .../s;
300 $cur = ascii_html($cur);
302 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
304 # show everything in the full version with anchor from
305 # short version (see above)
309 $rv .= join('', map { linkify_1(\%l, $_) } @$quot);
311 $rv = ascii_html($rv);
312 "<a\nid=q${part_nr}_$nr></a>" . linkify_2(\%l, $rv);
317 my ($enc_msg, $part, $part_nr, $full_pfx) = @_;
318 return '' if $part->subparts;
320 my $ct = $part->content_type;
321 # account for filter bugs...
322 if (defined $ct && $ct =~ m!\btext/[xh]+tml\b!i) {
326 my $enc = enc_for($ct, $enc_msg);
331 $s = $enc->decode($s);
332 my @lines = split(/^/m, $s);
336 my $fn = $part->filename;
337 defined($fn) or $fn = "part #" . ($$part_nr + 1);
338 $s .= add_filename_line($enc, $fn);
342 while (defined(my $cur = shift @lines)) {
344 # show the previously buffered quote inline
346 $s .= flush_quote(\@quot, \$n, $$part_nr,
352 $cur = linkify_1(\%l, $cur);
353 $cur = ascii_html($cur);
354 $s .= linkify_2(\%l, $cur);
359 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1) if scalar @quot;
360 $s .= "\n" unless $s =~ /\n\z/s;
365 sub headers_to_html_header {
366 my ($mime, $full_pfx, $ctx) = @_;
367 my $srch = $ctx->{srch} if $ctx;
370 my $header_obj = $mime->header_obj;
371 my $mid = $header_obj->header('Message-ID');
372 $mid = PublicInbox::Hval->new_msgid($mid);
373 my $mid_href = $mid->as_href;
374 foreach my $h (qw(From To Cc Subject Date)) {
375 my $v = $mime->header($h);
376 defined($v) && ($v ne '') or next;
377 $v = PublicInbox::Hval->new_oneline($v);
380 my @from = Email::Address->parse($v->raw);
381 $title[1] = ascii_html($from[0]->name);
382 } elsif ($h eq 'Subject') {
383 $title[0] = $v->as_html;
385 my $p = $full_pfx ? '' : '../';
386 $rv .= "$h: <a\nid=\"t\"\nhref=\"${p}t/#u\">";
387 $rv .= $v->as_html . "</a>\n";
391 $rv .= "$h: " . $v->as_html . "\n";
394 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
395 my $raw_ref = $full_pfx ? 'raw' : '../raw';
396 $rv .= "(<a\nhref=\"$raw_ref\">raw</a>)\n";
398 $rv .= "<a\nhref=\"#r\">References: [see below]</a>\n";
400 $rv .= _parent_headers_nosrch($header_obj);
404 ("<html><head><title>". join(' - ', @title) .
405 '</title></head><body>' . PRE_WRAP . $rv);
409 my ($dst, $ctx, $cur, $full_pfx) = @_;
410 my $srch = $ctx->{srch};
411 my $mid = mid_compress(mid_clean($cur->header('Message-ID')));
412 my $res = $srch->get_thread($mid);
413 my $nr = $res->{total};
416 $$dst .= "\n[no followups, yet]\n";
419 my $upfx = $full_pfx ? '' : '../';
421 $$dst .= "\n\n~$nr messages in thread: ".
422 "(<a\nhref=\"${upfx}t/#u\">expand</a>)\n";
423 my $subj = $srch->subject_path($cur->header('Subject'));
425 seen => { $subj => 1 },
429 for (thread_results(load_results($res))->rootset) {
430 inline_dump($dst, $state, $upfx, $_, 0);
435 sub _parent_headers_nosrch {
436 my ($header_obj) = @_;
439 my $irt = $header_obj->header('In-Reply-To');
441 my $v = PublicInbox::Hval->new_msgid($irt);
442 my $html = $v->as_html;
443 my $href = $v->as_href;
444 $rv .= "In-Reply-To: <";
445 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
448 my $refs = $header_obj->header('References');
450 # avoid redundant URLs wasting bandwidth
452 $seen{mid_clean($irt)} = 1 if defined $irt;
454 my @raw_refs = ($refs =~ /<([^>]+)>/g);
455 foreach my $ref (@raw_refs) {
458 push @refs, linkify_ref($ref);
462 $rv .= 'References: '. join(' ', @refs) . "\n";
469 my ($mime, $standalone, $full_pfx, $ctx) = @_;
470 my %cc; # everyone else
471 my $to; # this is the From address
473 foreach my $h (qw(From To Cc)) {
474 my $v = $mime->header($h);
475 defined($v) && ($v ne '') or next;
476 my @addrs = Email::Address->parse($v);
477 foreach my $recip (@addrs) {
478 my $address = $recip->address;
479 my $dst = lc($address);
480 $cc{$dst} ||= $address;
484 Email::Address->purge_cache if $standalone;
486 my $subj = $mime->header('Subject') || '';
487 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
488 my $mid = $mime->header('Message-ID');
489 my $irt = uri_escape_utf8($mid);
491 $to = uri_escape_utf8($to);
492 $subj = uri_escape_utf8($subj);
494 my $cc = uri_escape_utf8(join(',', sort values %cc));
495 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
497 my $srch = $ctx->{srch} if $ctx;
498 my $upfx = $full_pfx ? '../' : '../../';
499 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
501 my $next = thread_inline(\$idx, $ctx, $mime, $full_pfx);
502 $irt = $mime->header('In-Reply-To');
504 $irt = PublicInbox::Hval->new_msgid($irt);
505 $irt = $irt->as_href;
506 $irt = "<a\nhref=\"$upfx$irt/\">parent</a> ";
508 $irt = ' ' x length('parent ');
511 $irt .= "<a\nhref=\"$upfx$next/\">next</a> ";
519 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
523 my $v = PublicInbox::Hval->new_msgid($_[0]);
524 my $html = $v->as_html;
525 my $href = $v->as_href;
526 "<<a\nhref=\"../$href/\">$html</a>>";
532 if ($id !~ /\A[a-f0-9]{40}\z/) {
533 $id = mid_compress(mid_clean($id), 1);
538 sub thread_html_head {
540 my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
542 "<html><head><title>$s</title></head><body>";
546 my ($fh, $git, $state, $node, $level) = @_;
548 if (my $mime = $node->message) {
550 # lazy load the full message from mini_mime:
551 my $path = mid2path(mid_clean($mime->header('Message-ID')));
552 $mime = eval { Email::MIME->new($git->cat_file("HEAD:$path")) };
554 if ($state->{anchor_idx} == 0) {
555 $fh->write(thread_html_head($mime));
557 index_entry($fh, $mime, $level, $state);
560 thread_entry($fh, $git, $state, $node->child, $level + 1);
561 thread_entry($fh, $git, $state, $node->next, $level);
567 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
572 my $ts = eval { str2time($mime->header('Date')) };
573 defined($ts) ? $ts : 0;
578 require PublicInbox::Thread;
579 my $th = PublicInbox::Thread->new(@$msgs);
582 $th->order(*PublicInbox::Thread::sort_ts);
588 my $title = 'Thread does not exist';
589 $cb->([404, ['Content-Type' => 'text/html']])->write(<<EOF);
590 <html><head><title>$title</title></head><body><pre>$title
591 <a href="../../">Return to index</a></pre></body></html>
597 my $ts = $mime->header('X-PI-TS') || msg_timestamp($mime);
598 POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
602 my ($dst, $state, $upfx, $mime, $level) = @_;
603 my $pfx = ' ' x $level;
605 my $cur = $state->{cur};
606 my $mid = $mime->header('Message-ID');
607 my $f = $mime->header('X-PI-From');
608 my $d = _msg_date($mime);
609 $f = PublicInbox::Hval->new($f);
610 $d = PublicInbox::Hval->new($d);
612 $d = $d->as_html . ' UTC';
613 my $midc = mid_compress(mid_clean($mid));
616 delete $state->{cur};
617 $$dst .= "$pfx` <b><a\nid=\"r\"\nhref=\"#t\">".
618 "[this message]</a></b> by $f @ $d\n";
623 $state->{next_msg} ||= $midc;
626 # Subject is never undef, this mail was loaded from
627 # our Xapian which would've resulted in '' if it were
628 # really missing (and Filter rejects empty subjects)
629 my $s = $mime->header('Subject');
630 my $h = $state->{srch}->subject_path($s);
631 if ($state->{seen}->{$h}) {
634 $state->{seen}->{$h} = 1;
635 $s = PublicInbox::Hval->new($s);
638 my $m = PublicInbox::Hval->new_msgid($mid);
639 $m = $upfx . '../' . $m->as_href . '/';
641 $$dst .= "$pfx` <a\nhref=\"$m\">$s</a>\n" .
644 $$dst .= "$pfx` <a\nhref=\"$m\">$f @ $d</a>\n";
649 my ($dst, $state, $upfx, $node, $level) = @_;
651 return if $state->{stopped};
652 if (my $mime = $node->message) {
653 _inline_header($dst, $state, $upfx, $mime, $level);
655 inline_dump($dst, $state, $upfx, $node->child, $level+1);
656 inline_dump($dst, $state, $upfx, $node->next, $level);