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, $t_anchor);
79 $anchor_idx = anchor_for($irt);
80 $anchor = $seen->{$anchor_idx};
86 $subj = "<a\nhref=\"${path}t/$href/#u\">$subj</a>";
88 if ($root_anchor && $root_anchor eq $id) {
89 $subj = "<u\nid=\"u\">$subj</u>";
92 my $ts = $mime->header('X-PI-TS');
93 unless (defined $ts) {
94 $ts = msg_timestamp($mime);
96 $ts = POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
98 my $rv = "<table\nsummary=l$level><tr>";
100 $rv .= '<td><pre>' . (' ' x $level) . '</pre></td>';
102 $rv .= "<td\nid=s$midx>" . PRE_WRAP;
103 $rv .= "<b\nid=\"$id\">$subj</b>\n";
104 $rv .= "- by $from @ $ts UTC - ";
105 $rv .= "<a\nhref=\"#s$next\">next</a>";
107 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
109 $fh->write($rv .= "\n\n");
111 my ($fhref, $more_ref);
112 my $mhref = "${path}m/$href/";
114 $fhref = "${path}f/$href/";
117 # scan through all parts, looking for displayable text
118 $mime->walk_parts(sub {
119 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
123 my $txt = "${path}m/$href/raw";
124 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
125 $rv .= html_footer($mime, 0, undef, $ctx);
128 unless (defined $anchor) {
129 my $v = PublicInbox::Hval->new_msgid($irt);
131 $anchor = "${path}m/$v/";
132 $seen->{$anchor_idx} = $anchor;
134 $rv .= " <a\nhref=\"$anchor\">parent</a>";
138 $rv .= " <a\nhref=\"${path}t/$href/$t_anchor\">" .
142 $fh->write($rv .= '</pre></td></tr></table>');
146 my ($ctx, $foot, $srch) = @_;
147 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
150 # only private functions below.
152 sub emit_thread_html {
153 my ($cb, $ctx, $foot, $srch) = @_;
154 my $mid = mid_compress($ctx->{mid});
155 my $res = $srch->get_thread($mid);
156 my $msgs = load_results($res);
157 my $nr = scalar @$msgs;
158 return missing_thread($cb) if $nr == 0;
159 my $fh = $cb->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
160 my $th = thread_results($msgs);
164 root_anchor => anchor_for($mid),
168 require PublicInbox::GitCatFile;
169 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
170 thread_entry($fh, $git, $state, $_, 0) for $th->rootset;
172 my $final_anchor = $state->{anchor_idx};
173 my $next = "<a\nid=\"s$final_anchor\">";
174 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
175 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>\n";
176 $next .= "download: <a\nhref=\"mbox.gz\">mbox.gz</a>\n\n";
177 $fh->write("<hr />" . PRE_WRAP . $next . $foot .
178 "</pre></body></html>");
183 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
184 my $s = add_text_body($enc, $part, $part_nr, $fhref);
187 # drop the remainder of git patches, they're usually better
188 # to review when the full message is viewed
189 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
192 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
195 # kill any leading or trailing whitespace lines
200 # kill per-line trailing whitespace
201 $s =~ s/[ \t]+$//sgm;
202 $s .= "\n" unless $s =~ /\n\z/s;
208 my ($ct, $default) = @_;
209 $default ||= $enc_utf8;
210 defined $ct or return $default;
211 my $ct_parsed = parse_content_type($ct);
213 if (my $charset = $ct_parsed->{attributes}->{charset}) {
214 my $enc = find_encoding($charset);
221 sub multipart_text_as_html {
222 my ($mime, $full_pfx, $srch) = @_;
225 my $enc = enc_for($mime->header("Content-Type"));
227 # scan through all parts, looking for displayable text
228 $mime->walk_parts(sub {
230 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx);
236 sub add_filename_line {
240 $fn = $enc->decode($fn);
242 $pad x= ($len/2) if ($len > 0);
243 "$pad " . ascii_html($fn) . " $pad\n";
246 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://
248 ?[\@\w\+\&\?\.\%\;/#=-]*)!x;
251 # no newlines added here since it'd break the splitting we do
253 $_[0] =~ s!$LINK_RE!<a\nhref="$1">$1</a>!g;
257 my ($quot, $n, $part_nr, $full_pfx, $final) = @_;
260 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
262 my $rv = join('', map { linkify($_); $_ } @$quot);
267 # show a short snippet of quoted text and link to full version:
268 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
269 my $cur = join(' ', @$quot);
270 @$quot = split(/\s+/, $cur);
273 my $tmp = shift(@$quot);
274 my $len = length($tmp) + length($cur);
275 if ($len > MAX_TRUNC_LEN) {
280 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
282 $cur =~ s/ \z/ .../s;
284 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
286 # show everything in the full version with anchor from
287 # short version (see above)
289 my $rv = "<a\nid=q${part_nr}_$nr></a>";
290 $rv .= join('', map { linkify($_); $_ } @$quot);
297 my ($enc_msg, $part, $part_nr, $full_pfx) = @_;
298 return '' if $part->subparts;
300 my $ct = $part->content_type;
301 # account for filter bugs...
302 if (defined $ct && $ct =~ m!\btext/[xh]+tml\b!i) {
306 my $enc = enc_for($ct, $enc_msg);
311 $s = $enc->decode($s);
313 my @lines = split(/^/m, $s);
317 my $fn = $part->filename;
318 defined($fn) or $fn = "part #" . ($$part_nr + 1);
319 $s .= add_filename_line($enc, $fn);
323 while (defined(my $cur = shift @lines)) {
324 if ($cur !~ /^>/) {
325 # show the previously buffered quote inline
327 $s .= flush_quote(\@quot, \$n, $$part_nr,
338 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1) if scalar @quot;
339 $s .= "\n" unless $s =~ /\n\z/s;
344 sub headers_to_html_header {
345 my ($mime, $full_pfx, $ctx) = @_;
346 my $srch = $ctx->{srch} if $ctx;
349 my $header_obj = $mime->header_obj;
350 my $mid = $header_obj->header('Message-ID');
351 $mid = PublicInbox::Hval->new_msgid($mid);
352 my $mid_href = $mid->as_href;
353 foreach my $h (qw(From To Cc Subject Date)) {
354 my $v = $mime->header($h);
355 defined($v) && ($v ne '') or next;
356 $v = PublicInbox::Hval->new_oneline($v);
359 my @from = Email::Address->parse($v->raw);
360 $title[1] = ascii_html($from[0]->name);
361 } elsif ($h eq 'Subject') {
362 $title[0] = $v->as_html;
364 $rv .= "$h: <a\nid=\"t\"\n" .
365 "href=\"../../t/$mid_href/\">";
366 $rv .= $v->as_html . "</a>\n";
370 $rv .= "$h: " . $v->as_html . "\n";
373 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
374 my $raw_ref = $full_pfx ? 'raw' : "../../m/$mid_href/raw";
375 $rv .= "(<a\nhref=\"$raw_ref\">raw</a>)\n";
377 $rv .= "<a\nhref=\"#r\">References: [see below]</a>\n";
379 $rv .= _parent_headers_nosrch($header_obj);
383 ("<html><head><title>". join(' - ', @title) .
384 '</title></head><body>' . PRE_WRAP . $rv);
388 my ($dst, $ctx, $cur) = @_;
389 my $srch = $ctx->{srch};
390 my $mid = mid_compress(mid_clean($cur->header('Message-ID')));
391 my $res = $srch->get_thread($mid);
392 my $nr = $res->{total};
394 $$dst .= "[only message in thread]\n";
398 $$dst .= "roughly $nr messages in thread:\n";
399 my $subj = $srch->subject_path($cur->header('Subject'));
401 seen => { $subj => 1 },
405 for (thread_results(load_results($res))->rootset) {
406 inline_dump($dst, $state, $_, 0);
411 sub _parent_headers_nosrch {
412 my ($header_obj) = @_;
415 my $irt = $header_obj->header('In-Reply-To');
417 my $v = PublicInbox::Hval->new_msgid($irt);
418 my $html = $v->as_html;
419 my $href = $v->as_href;
420 $rv .= "In-Reply-To: <";
421 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
424 my $refs = $header_obj->header('References');
426 # avoid redundant URLs wasting bandwidth
428 $seen{mid_clean($irt)} = 1 if defined $irt;
430 my @raw_refs = ($refs =~ /<([^>]+)>/g);
431 foreach my $ref (@raw_refs) {
434 push @refs, linkify_ref($ref);
438 $rv .= 'References: '. join(' ', @refs) . "\n";
445 my ($mime, $standalone, $full_pfx, $ctx) = @_;
446 my %cc; # everyone else
447 my $to; # this is the From address
449 foreach my $h (qw(From To Cc)) {
450 my $v = $mime->header($h);
451 defined($v) && ($v ne '') or next;
452 my @addrs = Email::Address->parse($v);
453 foreach my $recip (@addrs) {
454 my $address = $recip->address;
455 my $dst = lc($address);
456 $cc{$dst} ||= $address;
460 Email::Address->purge_cache if $standalone;
462 my $subj = $mime->header('Subject') || '';
463 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
464 my $mid = $mime->header('Message-ID');
465 my $irt = uri_escape_utf8($mid);
467 $to = uri_escape_utf8($to);
468 $subj = uri_escape_utf8($subj);
470 my $cc = uri_escape_utf8(join(',', sort values %cc));
471 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
473 my $srch = $ctx->{srch} if $ctx;
474 my $idx = $standalone ? " <a\nhref=\"../../\">index</a>" : '';
476 $mid = mid_compress(mid_clean($mid));
477 my $t_anchor = defined $irt ? T_ANCHOR : '';
478 $irt = $mime->header('In-Reply-To');
479 $idx = " <a\nhref=\"../../t/$mid/$t_anchor\">".
480 "threadlink</a>$idx\n\n";
481 my $next = thread_inline(\$idx, $ctx, $mime);
483 $irt = PublicInbox::Hval->new_msgid($irt);
484 $irt = $irt->as_href;
485 $irt = "<a\nhref=\"../$irt/\">parent</a> ";
487 $irt = ' ' x length('parent ');
490 $irt .= "<a\nhref=\"../$next/\">next</a> ";
498 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
502 my $v = PublicInbox::Hval->new_msgid($_[0]);
503 my $html = $v->as_html;
504 my $href = $v->as_href;
505 "<<a\nhref=\"../$href/\">$html</a>>";
511 if ($id !~ /\A[a-f0-9]{40}\z/) {
512 $id = mid_compress(mid_clean($id), 1);
517 sub thread_html_head {
519 my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
521 "<html><head><title>$s</title></head><body>";
525 my ($fh, $git, $state, $node, $level) = @_;
527 if (my $mime = $node->message) {
529 # lazy load the full message from mini_mime:
530 my $path = mid2path(mid_clean($mime->header('Message-ID')));
531 $mime = eval { Email::MIME->new($git->cat_file("HEAD:$path")) };
533 if ($state->{anchor_idx} == 0) {
534 $fh->write(thread_html_head($mime));
536 index_entry($fh, $mime, $level, $state);
539 thread_entry($fh, $git, $state, $node->child, $level + 1);
540 thread_entry($fh, $git, $state, $node->next, $level);
546 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
551 my $ts = eval { str2time($mime->header('Date')) };
552 defined($ts) ? $ts : 0;
557 require PublicInbox::Thread;
558 my $th = PublicInbox::Thread->new(@$msgs);
561 $th->order(*PublicInbox::Thread::sort_ts);
567 my $title = 'Thread does not exist';
568 $cb->([404, ['Content-Type' => 'text/html']])->write(<<EOF);
569 <html><head><title>$title</title></head><body><pre>$title
570 <a href="../../">Return to index</a></pre></body></html>
575 my ($dst, $state, $mime, $level) = @_;
576 my $pfx = ' ' x $level;
578 my $cur = $state->{cur};
579 my $mid = $mime->header('Message-ID');
580 my $f = $mime->header('X-PI-From');
581 my $d = $mime->header('X-PI-Date');
582 $f = PublicInbox::Hval->new($f);
583 $d = PublicInbox::Hval->new($d);
585 $d = $d->as_html . ' UTC';
586 my $midc = mid_compress(mid_clean($mid));
589 delete $state->{cur};
590 $$dst .= "$pfx` <b><a\nid=\"r\"\nhref=\"#t\">".
591 "[this message]</a></b> by $f @ $d\n";
596 $state->{next_msg} ||= $midc;
599 # Subject is never undef, this mail was loaded from
600 # our Xapian which would've resulted in '' if it were
601 # really missing (and Filter rejects empty subjects)
602 my $s = $mime->header('Subject');
603 my $h = $state->{srch}->subject_path($s);
604 if ($state->{seen}->{$h}) {
607 $state->{seen}->{$h} = 1;
608 $s = PublicInbox::Hval->new($s);
611 my $m = PublicInbox::Hval->new_msgid($mid);
612 $m = '../' . $m->as_href . '/';
614 $$dst .= "$pfx` <a\nhref=\"$m\">$s</a>\n" .
617 $$dst .= "$pfx` <a\nhref=\"$m\">$f @ $d</a>\n";
622 my ($dst, $state, $node, $level) = @_;
624 return if $state->{stopped};
625 if (my $mime = $node->message) {
626 _inline_header($dst, $state, $mime, $level);
628 inline_dump($dst, $state, $node->child, $level+1);
629 inline_dump($dst, $state, $node->next, $level);