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/;
16 # TODO: make these constants tunable
17 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
18 use constant MAX_TRUNC_LEN => 72;
19 use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
20 use constant T_ANCHOR => '#u';
22 *ascii_html = *PublicInbox::Hval::ascii_html;
24 my $enc_utf8 = find_encoding('UTF-8');
28 my ($ctx, $mime, $full_pfx, $footer) = @_;
29 if (defined $footer) {
30 $footer = "\n" . $footer;
34 headers_to_html_header($mime, $full_pfx, $ctx) .
35 multipart_text_as_html($mime, $full_pfx) .
36 '</pre><hr />' . PRE_WRAP .
37 html_footer($mime, 1, $full_pfx, $ctx) .
39 '</pre></body></html>';
43 my ($class, $mime, $full_pfx) = @_;
45 PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
48 # this is already inside a <pre>
50 my ($fh, $mime, $level, $state) = @_;
51 my $midx = $state->{anchor_idx}++;
52 my $ctx = $state->{ctx};
53 my $srch = $ctx->{srch};
54 my ($prev, $next) = ($midx - 1, $midx + 1);
56 my $enc = enc_for($mime->header("Content-Type"));
57 my $subj = $mime->header('Subject');
58 my $header_obj = $mime->header_obj;
60 my $mid_raw = $header_obj->header('Message-ID');
61 my $id = anchor_for($mid_raw);
62 my $seen = $state->{seen};
63 $seen->{$id} = "#$id"; # save the anchor for later
65 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
66 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
67 my @from = Email::Address->parse($from);
68 $from = $from[0]->name;
70 $from = PublicInbox::Hval->new_oneline($from)->as_html;
71 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
72 my $more = 'permalink';
73 my $root_anchor = $state->{root_anchor};
74 my $path = $root_anchor ? '../../' : '';
75 my $href = $mid->as_href;
76 my $irt = $header_obj->header('In-Reply-To');
77 my ($anchor_idx, $anchor);
79 $anchor_idx = anchor_for($irt);
80 $anchor = $seen->{$anchor_idx};
83 $subj = "<a\nhref=\"${path}t/$href/#u\">$subj</a>";
85 if ($root_anchor && $root_anchor eq $id) {
86 $subj = "<u\nid=\"u\">$subj</u>";
89 my $ts = $mime->header('X-PI-TS');
90 unless (defined $ts) {
91 $ts = msg_timestamp($mime);
93 $ts = POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
95 my $rv = "<table\nsummary=l$level><tr>";
97 $rv .= '<td><pre>' . (' ' x $level) . '</pre></td>';
99 $rv .= "<td\nid=s$midx>" . PRE_WRAP;
100 $rv .= "<b\nid=\"$id\">$subj</b>\n";
101 $rv .= "- by $from @ $ts UTC - ";
102 $rv .= "<a\nhref=\"#s$next\">next</a>";
104 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
106 $fh->write($rv .= "\n\n");
108 my ($fhref, $more_ref);
109 my $mhref = "${path}m/$href/";
111 $fhref = "${path}f/$href/";
114 # scan through all parts, looking for displayable text
115 $mime->walk_parts(sub {
116 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
120 my $txt = "${path}m/$href/raw";
121 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
122 $rv .= html_footer($mime, 0, undef, $ctx);
125 unless (defined $anchor) {
126 my $v = PublicInbox::Hval->new_msgid($irt);
128 $anchor = "${path}m/$v/";
129 $seen->{$anchor_idx} = $anchor;
131 $rv .= " <a\nhref=\"$anchor\">parent</a>";
134 $fh->write($rv .= '</pre></td></tr></table>');
138 my ($ctx, $foot, $srch) = @_;
139 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
142 # only private functions below.
144 sub emit_thread_html {
145 my ($cb, $ctx, $foot, $srch) = @_;
146 my $mid = mid_compress($ctx->{mid});
147 my $res = $srch->get_thread($mid);
148 my $msgs = load_results($res);
149 my $nr = scalar @$msgs;
150 return missing_thread($cb) if $nr == 0;
151 my $fh = $cb->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
152 my $th = thread_results($msgs);
156 root_anchor => anchor_for($mid),
160 require PublicInbox::GitCatFile;
161 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
162 thread_entry($fh, $git, $state, $_, 0) for $th->rootset;
164 my $final_anchor = $state->{anchor_idx};
165 my $next = "<a\nid=\"s$final_anchor\">";
166 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
167 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>\n";
168 $next .= "download: <a\nhref=\"mbox.gz\">mbox.gz</a>\n\n";
169 $fh->write("<hr />" . PRE_WRAP . $next . $foot .
170 "</pre></body></html>");
175 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
176 my $s = add_text_body($enc, $part, $part_nr, $fhref);
179 # drop the remainder of git patches, they're usually better
180 # to review when the full message is viewed
181 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
184 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
187 # kill any leading or trailing whitespace lines
192 # kill per-line trailing whitespace
193 $s =~ s/[ \t]+$//sgm;
194 $s .= "\n" unless $s =~ /\n\z/s;
200 my ($ct, $default) = @_;
201 $default ||= $enc_utf8;
202 defined $ct or return $default;
203 my $ct_parsed = parse_content_type($ct);
205 if (my $charset = $ct_parsed->{attributes}->{charset}) {
206 my $enc = find_encoding($charset);
213 sub multipart_text_as_html {
214 my ($mime, $full_pfx, $srch) = @_;
217 my $enc = enc_for($mime->header("Content-Type"));
219 # scan through all parts, looking for displayable text
220 $mime->walk_parts(sub {
222 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx);
228 sub add_filename_line {
232 $fn = $enc->decode($fn);
234 $pad x= ($len/2) if ($len > 0);
235 "$pad " . ascii_html($fn) . " $pad\n";
238 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://
240 ?[\@\w\+\&\?\.\%\;/#=-]*)!x;
243 # no newlines added here since it'd break the splitting we do
245 $_[0] =~ s!$LINK_RE!<a\nhref="$1">$1</a>!g;
249 my ($quot, $n, $part_nr, $full_pfx, $final) = @_;
252 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
254 my $rv = join('', map { linkify($_); $_ } @$quot);
259 # show a short snippet of quoted text and link to full version:
260 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
261 my $cur = join(' ', @$quot);
262 @$quot = split(/\s+/, $cur);
265 my $tmp = shift(@$quot);
266 my $len = length($tmp) + length($cur);
267 if ($len > MAX_TRUNC_LEN) {
272 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
274 $cur =~ s/ \z/ .../s;
276 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
278 # show everything in the full version with anchor from
279 # short version (see above)
281 my $rv = "<a\nid=q${part_nr}_$nr></a>";
282 $rv .= join('', map { linkify($_); $_ } @$quot);
289 my ($enc_msg, $part, $part_nr, $full_pfx) = @_;
290 return '' if $part->subparts;
292 my $ct = $part->content_type;
293 # account for filter bugs...
294 if (defined $ct && $ct =~ m!\btext/[xh]+tml\b!i) {
298 my $enc = enc_for($ct, $enc_msg);
303 $s = $enc->decode($s);
305 my @lines = split(/^/m, $s);
309 my $fn = $part->filename;
310 defined($fn) or $fn = "part #" . ($$part_nr + 1);
311 $s .= add_filename_line($enc, $fn);
315 while (defined(my $cur = shift @lines)) {
316 if ($cur !~ /^>/) {
317 # show the previously buffered quote inline
319 $s .= flush_quote(\@quot, \$n, $$part_nr,
330 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1) if scalar @quot;
331 $s .= "\n" unless $s =~ /\n\z/s;
336 sub headers_to_html_header {
337 my ($mime, $full_pfx, $ctx) = @_;
338 my $srch = $ctx->{srch} if $ctx;
341 my $header_obj = $mime->header_obj;
342 my $mid = $header_obj->header('Message-ID');
343 $mid = PublicInbox::Hval->new_msgid($mid);
344 my $mid_href = $mid->as_href;
345 foreach my $h (qw(From To Cc Subject Date)) {
346 my $v = $mime->header($h);
347 defined($v) && ($v ne '') or next;
348 $v = PublicInbox::Hval->new_oneline($v);
351 my @from = Email::Address->parse($v->raw);
352 $title[1] = ascii_html($from[0]->name);
353 } elsif ($h eq 'Subject') {
354 $title[0] = $v->as_html;
356 $rv .= "$h: <a\nid=\"t\"\n" .
357 "href=\"../../t/$mid_href/\">";
358 $rv .= $v->as_html . "</a>\n";
362 $rv .= "$h: " . $v->as_html . "\n";
365 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
366 my $raw_ref = $full_pfx ? 'raw' : "../../m/$mid_href/raw";
367 $rv .= "(<a\nhref=\"$raw_ref\">raw</a>)\n";
369 $rv .= "<a\nhref=\"#r\">References: [see below]</a>\n";
371 $rv .= _parent_headers_nosrch($header_obj);
375 ("<html><head><title>". join(' - ', @title) .
376 '</title></head><body>' . PRE_WRAP . $rv);
380 my ($dst, $ctx, $cur) = @_;
381 my $srch = $ctx->{srch};
382 my $mid = mid_compress(mid_clean($cur->header('Message-ID')));
383 my $res = $srch->get_thread($mid);
384 my $nr = $res->{total};
387 $$dst .= "\n[no followups, yet]</a>\n";
391 $$dst .= "\n\n~$nr messages in thread: ".
392 "(<a\nhref=\"../../t/$mid/#u\">expand</a>)\n";
393 my $subj = $srch->subject_path($cur->header('Subject'));
395 seen => { $subj => 1 },
399 for (thread_results(load_results($res))->rootset) {
400 inline_dump($dst, $state, $_, 0);
405 sub _parent_headers_nosrch {
406 my ($header_obj) = @_;
409 my $irt = $header_obj->header('In-Reply-To');
411 my $v = PublicInbox::Hval->new_msgid($irt);
412 my $html = $v->as_html;
413 my $href = $v->as_href;
414 $rv .= "In-Reply-To: <";
415 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
418 my $refs = $header_obj->header('References');
420 # avoid redundant URLs wasting bandwidth
422 $seen{mid_clean($irt)} = 1 if defined $irt;
424 my @raw_refs = ($refs =~ /<([^>]+)>/g);
425 foreach my $ref (@raw_refs) {
428 push @refs, linkify_ref($ref);
432 $rv .= 'References: '. join(' ', @refs) . "\n";
439 my ($mime, $standalone, $full_pfx, $ctx) = @_;
440 my %cc; # everyone else
441 my $to; # this is the From address
443 foreach my $h (qw(From To Cc)) {
444 my $v = $mime->header($h);
445 defined($v) && ($v ne '') or next;
446 my @addrs = Email::Address->parse($v);
447 foreach my $recip (@addrs) {
448 my $address = $recip->address;
449 my $dst = lc($address);
450 $cc{$dst} ||= $address;
454 Email::Address->purge_cache if $standalone;
456 my $subj = $mime->header('Subject') || '';
457 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
458 my $mid = $mime->header('Message-ID');
459 my $irt = uri_escape_utf8($mid);
461 $to = uri_escape_utf8($to);
462 $subj = uri_escape_utf8($subj);
464 my $cc = uri_escape_utf8(join(',', sort values %cc));
465 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
467 my $srch = $ctx->{srch} if $ctx;
468 my $idx = $standalone ? " <a\nhref=\"../../\">index</a>" : '';
470 my $next = thread_inline(\$idx, $ctx, $mime);
472 $irt = PublicInbox::Hval->new_msgid($irt);
473 $irt = $irt->as_href;
474 $irt = "<a\nhref=\"../$irt/\">parent</a> ";
476 $irt = ' ' x length('parent ');
479 $irt .= "<a\nhref=\"../$next/\">next</a> ";
487 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
491 my $v = PublicInbox::Hval->new_msgid($_[0]);
492 my $html = $v->as_html;
493 my $href = $v->as_href;
494 "<<a\nhref=\"../$href/\">$html</a>>";
500 if ($id !~ /\A[a-f0-9]{40}\z/) {
501 $id = mid_compress(mid_clean($id), 1);
506 sub thread_html_head {
508 my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
510 "<html><head><title>$s</title></head><body>";
514 my ($fh, $git, $state, $node, $level) = @_;
516 if (my $mime = $node->message) {
518 # lazy load the full message from mini_mime:
519 my $path = mid2path(mid_clean($mime->header('Message-ID')));
520 $mime = eval { Email::MIME->new($git->cat_file("HEAD:$path")) };
522 if ($state->{anchor_idx} == 0) {
523 $fh->write(thread_html_head($mime));
525 index_entry($fh, $mime, $level, $state);
528 thread_entry($fh, $git, $state, $node->child, $level + 1);
529 thread_entry($fh, $git, $state, $node->next, $level);
535 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
540 my $ts = eval { str2time($mime->header('Date')) };
541 defined($ts) ? $ts : 0;
546 require PublicInbox::Thread;
547 my $th = PublicInbox::Thread->new(@$msgs);
550 $th->order(*PublicInbox::Thread::sort_ts);
556 my $title = 'Thread does not exist';
557 $cb->([404, ['Content-Type' => 'text/html']])->write(<<EOF);
558 <html><head><title>$title</title></head><body><pre>$title
559 <a href="../../">Return to index</a></pre></body></html>
564 my ($dst, $state, $mime, $level) = @_;
565 my $pfx = ' ' x $level;
567 my $cur = $state->{cur};
568 my $mid = $mime->header('Message-ID');
569 my $f = $mime->header('X-PI-From');
570 my $d = $mime->header('X-PI-Date');
571 $f = PublicInbox::Hval->new($f);
572 $d = PublicInbox::Hval->new($d);
574 $d = $d->as_html . ' UTC';
575 my $midc = mid_compress(mid_clean($mid));
578 delete $state->{cur};
579 $$dst .= "$pfx` <b><a\nid=\"r\"\nhref=\"#t\">".
580 "[this message]</a></b> by $f @ $d\n";
585 $state->{next_msg} ||= $midc;
588 # Subject is never undef, this mail was loaded from
589 # our Xapian which would've resulted in '' if it were
590 # really missing (and Filter rejects empty subjects)
591 my $s = $mime->header('Subject');
592 my $h = $state->{srch}->subject_path($s);
593 if ($state->{seen}->{$h}) {
596 $state->{seen}->{$h} = 1;
597 $s = PublicInbox::Hval->new($s);
600 my $m = PublicInbox::Hval->new_msgid($mid);
601 $m = '../' . $m->as_href . '/';
603 $$dst .= "$pfx` <a\nhref=\"$m\">$s</a>\n" .
606 $$dst .= "$pfx` <a\nhref=\"$m\">$f @ $d</a>\n";
611 my ($dst, $state, $node, $level) = @_;
613 return if $state->{stopped};
614 if (my $mime = $node->message) {
615 _inline_header($dst, $state, $mime, $level);
617 inline_dump($dst, $state, $node->child, $level+1);
618 inline_dump($dst, $state, $node->next, $level);