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 my $srch = $ctx->{srch} if $ctx;
35 headers_to_html_header($mime, $full_pfx, $srch) .
36 multipart_text_as_html($mime, $full_pfx) .
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, $t_anchor);
80 $anchor_idx = anchor_for($irt);
81 $anchor = $seen->{$anchor_idx};
87 $subj = "<a\nhref=\"${path}t/$href/#u\">$subj</a>";
89 if ($root_anchor && $root_anchor eq $id) {
90 $subj = "<u\nid=\"u\">$subj</u>";
93 my $ts = $mime->header('X-PI-TS');
94 unless (defined $ts) {
95 $ts = msg_timestamp($mime);
97 $ts = POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
99 my $rv = "<table\nsummary=l$level><tr>";
101 $rv .= '<td><pre>' . (' ' x $level) . '</pre></td>';
103 $rv .= "<td\nid=s$midx>" . PRE_WRAP;
104 $rv .= "<b\nid=\"$id\">$subj</b>\n";
105 $rv .= "- by $from @ $ts UTC - ";
106 $rv .= "<a\nhref=\"#s$next\">next</a>";
108 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
110 $fh->write($rv .= "\n\n");
112 my ($fhref, $more_ref);
113 my $mhref = "${path}m/$href/";
115 $fhref = "${path}f/$href/";
118 # scan through all parts, looking for displayable text
119 $mime->walk_parts(sub {
120 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
124 my $txt = "${path}m/$href/raw";
125 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
126 $rv .= html_footer($mime, 0, undef, $ctx);
129 unless (defined $anchor) {
130 my $v = PublicInbox::Hval->new_msgid($irt);
132 $anchor = "${path}m/$v/";
133 $seen->{$anchor_idx} = $anchor;
135 $rv .= " <a\nhref=\"$anchor\">parent</a>";
139 $rv .= " <a\nhref=\"${path}t/$href/$t_anchor\">" .
143 $fh->write($rv .= '</pre></td></tr></table>');
147 my ($ctx, $foot, $srch) = @_;
148 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
151 # only private functions below.
153 sub emit_thread_html {
154 my ($cb, $ctx, $foot, $srch) = @_;
155 my $mid = mid_compress($ctx->{mid});
156 my $res = $srch->get_thread($mid);
157 my $msgs = load_results($res);
158 my $nr = scalar @$msgs;
159 return missing_thread($cb) if $nr == 0;
160 my $fh = $cb->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
161 my $th = thread_results($msgs);
165 root_anchor => anchor_for($mid),
169 require PublicInbox::GitCatFile;
170 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
171 thread_entry($fh, $git, $state, $_, 0) for $th->rootset;
173 my $final_anchor = $state->{anchor_idx};
174 my $next = "<a\nid=\"s$final_anchor\">";
175 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
176 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>\n";
177 $next .= "download: <a\nhref=\"mbox.gz\">mbox.gz</a>\n\n";
178 $fh->write("<hr />" . PRE_WRAP . $next . $foot .
179 "</pre></body></html>");
184 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
185 my $s = add_text_body($enc, $part, $part_nr, $fhref);
188 # drop the remainder of git patches, they're usually better
189 # to review when the full message is viewed
190 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
193 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
196 # kill any leading or trailing whitespace lines
201 # kill per-line trailing whitespace
202 $s =~ s/[ \t]+$//sgm;
203 $s .= "\n" unless $s =~ /\n\z/s;
209 my ($ct, $default) = @_;
210 $default ||= $enc_utf8;
211 defined $ct or return $default;
212 my $ct_parsed = parse_content_type($ct);
214 if (my $charset = $ct_parsed->{attributes}->{charset}) {
215 my $enc = find_encoding($charset);
222 sub multipart_text_as_html {
223 my ($mime, $full_pfx, $srch) = @_;
226 my $enc = enc_for($mime->header("Content-Type"));
228 # scan through all parts, looking for displayable text
229 $mime->walk_parts(sub {
231 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx);
237 sub add_filename_line {
241 $fn = $enc->decode($fn);
243 $pad x= ($len/2) if ($len > 0);
244 "$pad " . ascii_html($fn) . " $pad\n";
247 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!;
250 # no newlines added here since it'd break the splitting we do
252 $_[0] =~ s!$LINK_RE!<a\nhref="$1">$1</a>!g;
256 my ($quot, $n, $part_nr, $full_pfx, $final) = @_;
259 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
261 my $rv = join('', map { linkify($_); $_ } @$quot);
266 # show a short snippet of quoted text and link to full version:
267 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
268 my $cur = join(' ', @$quot);
269 @$quot = split(/\s+/, $cur);
272 my $tmp = shift(@$quot);
273 my $len = length($tmp) + length($cur);
274 if ($len > MAX_TRUNC_LEN) {
279 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
281 $cur =~ s/ \z/ .../s;
283 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
285 # show everything in the full version with anchor from
286 # short version (see above)
288 my $rv = "<a\nid=q${part_nr}_$nr></a>";
289 $rv .= join('', map { linkify($_); $_ } @$quot);
296 my ($enc_msg, $part, $part_nr, $full_pfx) = @_;
297 return '' if $part->subparts;
299 my $ct = $part->content_type;
300 # account for filter bugs...
301 if (defined $ct && $ct =~ m!\btext/[xh]+tml\b!i) {
305 my $enc = enc_for($ct, $enc_msg);
310 $s = $enc->decode($s);
312 my @lines = split(/^/m, $s);
316 my $fn = $part->filename;
317 defined($fn) or $fn = "part #" . ($$part_nr + 1);
318 $s .= add_filename_line($enc, $fn);
322 while (defined(my $cur = shift @lines)) {
323 if ($cur !~ /^>/) {
324 # show the previously buffered quote inline
326 $s .= flush_quote(\@quot, \$n, $$part_nr,
337 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1) if scalar @quot;
338 $s .= "\n" unless $s =~ /\n\z/s;
343 sub headers_to_html_header {
344 my ($mime, $full_pfx, $srch) = @_;
348 my $header_obj = $mime->header_obj;
349 my $mid = $header_obj->header('Message-ID');
350 $mid = PublicInbox::Hval->new_msgid($mid);
351 my $mid_href = $mid->as_href;
352 foreach my $h (qw(From To Cc Subject Date)) {
353 my $v = $mime->header($h);
354 defined($v) && ($v ne '') or next;
355 $v = PublicInbox::Hval->new_oneline($v);
358 my @from = Email::Address->parse($v->raw);
359 $title[1] = ascii_html($from[0]->name);
360 } elsif ($h eq 'Subject') {
361 $title[0] = $v->as_html;
363 $rv .= "$h: <a\nhref=\"../../t/$mid_href/\">";
364 $rv .= $v->as_html . "</a>\n";
368 $rv .= "$h: " . $v->as_html . "\n";
372 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
373 my $raw_ref = $full_pfx ? 'raw' : "../../m/$mid_href/raw";
374 $rv .= "(<a\nhref=\"$raw_ref\">raw</a>)\n";
376 my $irt = $header_obj->header('In-Reply-To');
378 my $v = PublicInbox::Hval->new_msgid($irt);
379 my $html = $v->as_html;
380 my $href = $v->as_href;
381 $rv .= "In-Reply-To: <";
382 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
385 my $refs = $header_obj->header('References');
387 # avoid redundant URLs wasting bandwidth
389 $seen{mid_clean($irt)} = 1 if defined $irt;
391 my @raw_refs = ($refs =~ /<([^>]+)>/g);
392 foreach my $ref (@raw_refs) {
395 push @refs, linkify_ref($ref);
399 $rv .= 'References: '. join(' ', @refs) . "\n";
405 ("<html><head><title>". join(' - ', @title) .
406 '</title></head><body>' . PRE_WRAP . $rv);
410 my ($mime, $standalone, $full_pfx, $ctx) = @_;
411 my %cc; # everyone else
412 my $to; # this is the From address
414 foreach my $h (qw(From To Cc)) {
415 my $v = $mime->header($h);
416 defined($v) && ($v ne '') or next;
417 my @addrs = Email::Address->parse($v);
418 foreach my $recip (@addrs) {
419 my $address = $recip->address;
420 my $dst = lc($address);
421 $cc{$dst} ||= $address;
425 Email::Address->purge_cache if $standalone;
427 my $subj = $mime->header('Subject') || '';
428 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
429 my $mid = $mime->header('Message-ID');
430 my $irt = uri_escape_utf8($mid);
432 $to = uri_escape_utf8($to);
433 $subj = uri_escape_utf8($subj);
435 my $cc = uri_escape_utf8(join(',', sort values %cc));
436 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
438 my $srch = $ctx->{srch} if $ctx;
439 my $idx = $standalone ? " <a\nhref=\"../../\">index</a>" : '';
441 $mid = mid_compress(mid_clean($mid));
442 my $t_anchor = defined $irt ? T_ANCHOR : '';
443 $irt = $mime->header('In-Reply-To');
444 $idx = " <a\nhref=\"../../t/$mid/$t_anchor\">".
445 "threadlink</a>$idx";
446 my $res = $srch->get_followups($mid);
447 if (my $c = $res->{total}) {
448 my $nr = scalar @{$res->{msgs}};
450 $c = "$nr of $c followups";
452 $c = $c == 1 ? '1 followup' : "$c followups";
455 $res->{srch} = $srch;
456 thread_followups(\$idx, $mime, $res);
458 $idx .= "\n(no followups, yet)\n";
461 $irt = PublicInbox::Hval->new_msgid($irt);
462 $irt = $irt->as_href;
463 $irt = "<a\nhref=\"../$irt/\">parent</a> ";
465 $irt = ' ' x length('parent ');
471 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
475 my $v = PublicInbox::Hval->new_msgid($_[0]);
476 my $html = $v->as_html;
477 my $href = $v->as_href;
478 "<<a\nhref=\"../$href/\">$html</a>>";
484 if ($id !~ /\A[a-f0-9]{40}\z/) {
485 $id = mid_compress(mid_clean($id), 1);
491 my ($dst, $root, $node, $level) = @_;
493 # $root = [ undef, \%seen, $srch ];
494 if (my $x = $node->message) {
495 my $f = $x->header('X-PI-From');
496 my $d = $x->header('X-PI-Date');
497 if (defined $f && defined $d) {
498 my $mid = $x->header('Message-ID');
499 my $pfx = ' ' x $level;
502 # Subject is never undef, this mail was loaded from
503 # our Xapian which would've resulted in '' if it were
504 # really missing (and Filter rejects empty subjects)
505 my $s = $x->header('Subject');
506 my $h = $root->[2]->subject_path($s);
507 if ($root->[1]->{$h}) {
510 $root->[1]->{$h} = 1;
511 $s = PublicInbox::Hval->new($s);
514 my $m = PublicInbox::Hval->new_msgid($mid);
515 $f = PublicInbox::Hval->new($f);
516 $d = PublicInbox::Hval->new($d);
517 $m = '../' . $m->as_href . '/';
519 $d = $d->as_html . ' UTC';
521 $$dst .= "` <a\nhref=\"$m\">$s</a>\n" .
524 $$dst .= "` <a\nhref=\"$m\">$f @ $d</a>\n";
528 simple_dump($dst, $root, $node->child, $level+1);
529 simple_dump($dst, $root, $node->next, $level);
532 sub thread_followups {
533 my ($dst, $root, $res) = @_;
534 $root->header_set('X-PI-TS', '0');
535 my $msgs = load_results($res);
537 my $th = thread_results($msgs);
538 my $srch = $res->{srch};
539 my $subj = $srch->subject_path($root->header('Subject'));
540 my %seen = ($subj => 1);
541 $root = [ undef, \%seen, $srch ];
542 simple_dump($dst, $root, $_, 0) for $th->rootset;
545 sub thread_html_head {
547 my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
549 "<html><head><title>$s</title></head><body>";
553 my ($fh, $git, $state, $node, $level) = @_;
555 if (my $mime = $node->message) {
557 # lazy load the full message from mini_mime:
558 my $path = mid2path(mid_clean($mime->header('Message-ID')));
559 $mime = eval { Email::MIME->new($git->cat_file("HEAD:$path")) };
561 if ($state->{anchor_idx} == 0) {
562 $fh->write(thread_html_head($mime));
564 index_entry($fh, $mime, $level, $state);
567 thread_entry($fh, $git, $state, $node->child, $level + 1);
568 thread_entry($fh, $git, $state, $node->next, $level);
574 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
579 my $ts = eval { str2time($mime->header('Date')) };
580 defined($ts) ? $ts : 0;
585 require PublicInbox::Thread;
586 my $th = PublicInbox::Thread->new(@$msgs);
589 $th->order(*PublicInbox::Thread::sort_ts);
595 my $title = 'Thread does not exist';
596 $cb->([404, ['Content-Type' => 'text/html']])->write(<<EOF);
597 <html><head><title>$title</title></head><body><pre>$title
598 <a href="../../">Return to index</a></pre></body></html>