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};
395 $$dst .= "\n[no followups, yet]</a>\n";
399 $$dst .= "\n\n~$nr messages in thread: ".
400 "(<a\nhref=\"../../t/$mid/#u\">expand</a>)\n";
401 my $subj = $srch->subject_path($cur->header('Subject'));
403 seen => { $subj => 1 },
407 for (thread_results(load_results($res))->rootset) {
408 inline_dump($dst, $state, $_, 0);
413 sub _parent_headers_nosrch {
414 my ($header_obj) = @_;
417 my $irt = $header_obj->header('In-Reply-To');
419 my $v = PublicInbox::Hval->new_msgid($irt);
420 my $html = $v->as_html;
421 my $href = $v->as_href;
422 $rv .= "In-Reply-To: <";
423 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
426 my $refs = $header_obj->header('References');
428 # avoid redundant URLs wasting bandwidth
430 $seen{mid_clean($irt)} = 1 if defined $irt;
432 my @raw_refs = ($refs =~ /<([^>]+)>/g);
433 foreach my $ref (@raw_refs) {
436 push @refs, linkify_ref($ref);
440 $rv .= 'References: '. join(' ', @refs) . "\n";
447 my ($mime, $standalone, $full_pfx, $ctx) = @_;
448 my %cc; # everyone else
449 my $to; # this is the From address
451 foreach my $h (qw(From To Cc)) {
452 my $v = $mime->header($h);
453 defined($v) && ($v ne '') or next;
454 my @addrs = Email::Address->parse($v);
455 foreach my $recip (@addrs) {
456 my $address = $recip->address;
457 my $dst = lc($address);
458 $cc{$dst} ||= $address;
462 Email::Address->purge_cache if $standalone;
464 my $subj = $mime->header('Subject') || '';
465 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
466 my $mid = $mime->header('Message-ID');
467 my $irt = uri_escape_utf8($mid);
469 $to = uri_escape_utf8($to);
470 $subj = uri_escape_utf8($subj);
472 my $cc = uri_escape_utf8(join(',', sort values %cc));
473 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
475 my $srch = $ctx->{srch} if $ctx;
476 my $idx = $standalone ? " <a\nhref=\"../../\">index</a>" : '';
478 my $next = thread_inline(\$idx, $ctx, $mime);
480 $irt = PublicInbox::Hval->new_msgid($irt);
481 $irt = $irt->as_href;
482 $irt = "<a\nhref=\"../$irt/\">parent</a> ";
484 $irt = ' ' x length('parent ');
487 $irt .= "<a\nhref=\"../$next/\">next</a> ";
495 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
499 my $v = PublicInbox::Hval->new_msgid($_[0]);
500 my $html = $v->as_html;
501 my $href = $v->as_href;
502 "<<a\nhref=\"../$href/\">$html</a>>";
508 if ($id !~ /\A[a-f0-9]{40}\z/) {
509 $id = mid_compress(mid_clean($id), 1);
514 sub thread_html_head {
516 my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
518 "<html><head><title>$s</title></head><body>";
522 my ($fh, $git, $state, $node, $level) = @_;
524 if (my $mime = $node->message) {
526 # lazy load the full message from mini_mime:
527 my $path = mid2path(mid_clean($mime->header('Message-ID')));
528 $mime = eval { Email::MIME->new($git->cat_file("HEAD:$path")) };
530 if ($state->{anchor_idx} == 0) {
531 $fh->write(thread_html_head($mime));
533 index_entry($fh, $mime, $level, $state);
536 thread_entry($fh, $git, $state, $node->child, $level + 1);
537 thread_entry($fh, $git, $state, $node->next, $level);
543 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
548 my $ts = eval { str2time($mime->header('Date')) };
549 defined($ts) ? $ts : 0;
554 require PublicInbox::Thread;
555 my $th = PublicInbox::Thread->new(@$msgs);
558 $th->order(*PublicInbox::Thread::sort_ts);
564 my $title = 'Thread does not exist';
565 $cb->([404, ['Content-Type' => 'text/html']])->write(<<EOF);
566 <html><head><title>$title</title></head><body><pre>$title
567 <a href="../../">Return to index</a></pre></body></html>
572 my ($dst, $state, $mime, $level) = @_;
573 my $pfx = ' ' x $level;
575 my $cur = $state->{cur};
576 my $mid = $mime->header('Message-ID');
577 my $f = $mime->header('X-PI-From');
578 my $d = $mime->header('X-PI-Date');
579 $f = PublicInbox::Hval->new($f);
580 $d = PublicInbox::Hval->new($d);
582 $d = $d->as_html . ' UTC';
583 my $midc = mid_compress(mid_clean($mid));
586 delete $state->{cur};
587 $$dst .= "$pfx` <b><a\nid=\"r\"\nhref=\"#t\">".
588 "[this message]</a></b> by $f @ $d\n";
593 $state->{next_msg} ||= $midc;
596 # Subject is never undef, this mail was loaded from
597 # our Xapian which would've resulted in '' if it were
598 # really missing (and Filter rejects empty subjects)
599 my $s = $mime->header('Subject');
600 my $h = $state->{srch}->subject_path($s);
601 if ($state->{seen}->{$h}) {
604 $state->{seen}->{$h} = 1;
605 $s = PublicInbox::Hval->new($s);
608 my $m = PublicInbox::Hval->new_msgid($mid);
609 $m = '../' . $m->as_href . '/';
611 $$dst .= "$pfx` <a\nhref=\"$m\">$s</a>\n" .
614 $$dst .= "$pfx` <a\nhref=\"$m\">$f @ $d</a>\n";
619 my ($dst, $state, $node, $level) = @_;
621 return if $state->{stopped};
622 if (my $mime = $node->message) {
623 _inline_header($dst, $state, $mime, $level);
625 inline_dump($dst, $state, $node->child, $level+1);
626 inline_dump($dst, $state, $node->next, $level);