1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Used for displaying the HTML web interface.
5 # See Documentation/design_www.txt for this.
6 package PublicInbox::View;
9 use PublicInbox::MsgTime qw(msg_datestamp);
10 use PublicInbox::Hval qw/ascii_html obfuscate_addrs/;
11 use PublicInbox::Linkify;
12 use PublicInbox::MID qw/id_compress mid_escape mids references/;
13 use PublicInbox::MsgIter;
14 use PublicInbox::Address;
15 use PublicInbox::WwwStream;
16 use PublicInbox::Reply;
17 use PublicInbox::ViewDiff qw(flush_diff);
19 use Time::Local qw(timegm);
21 use constant COLS => 72;
22 use constant INDENT => ' ';
23 use constant TCHILD => '` ';
24 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
26 # public functions: (unstable)
29 my ($ctx, $mime, $more, $smsg) = @_;
30 my $hdr = $mime->header_obj;
31 my $ibx = $ctx->{-inbox};
32 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
33 my $tip = _msg_html_prepare($hdr, $ctx, $more, 0);
35 PublicInbox::WwwStream->response($ctx, 200, sub {
38 # $more cannot be true w/o $smsg being defined:
39 my $upfx = $more ? '../'.mid_escape($smsg->mid).'/' : '';
40 $tip . multipart_text_as_html($mime, $upfx, $ibx) .
42 } elsif ($more && @$more) {
44 msg_html_more($ctx, $more, $nr);
45 } elsif ($nr == $end) {
46 # fake an EOF if generating the footer fails;
47 # we want to at least show the message if something
50 '<pre>' . html_footer($hdr, 1, $ctx) .
51 '</pre>' . msg_reply($ctx, $hdr)
61 my $mid = $ctx->{mid};
62 my $ibx = $ctx->{-inbox};
65 if (my $srch = $ibx->search) {
67 $smsg = $srch->next_by_mid($mid, \$id, \$prev);
68 $first = $ibx->msg_by_smsg($smsg) if $smsg;
70 my $next = $srch->next_by_mid($mid, \$id, \$prev);
71 $more = [ $id, $prev, $next ] if $next;
75 $first = $ibx->msg_by_mid($mid) or return;
77 msg_html($ctx, PublicInbox::MIME->new($first), $more, $smsg);
81 my ($ctx, $more, $nr) = @_;
83 my ($id, $prev, $smsg) = @$more;
84 my $mid = $ctx->{mid};
85 my $ibx = $ctx->{-inbox};
86 $smsg = $ibx->smsg_mime($smsg);
87 my $next = $ctx->{srch}->next_by_mid($mid, \$id, \$prev);
88 @$more = $next ? ($id, $prev, $next) : ();
90 my $mime = $smsg->{mime};
91 my $upfx = '../' . mid_escape($smsg->mid) . '/';
92 _msg_html_prepare($mime->header_obj, $ctx, $more, $nr) .
93 multipart_text_as_html($mime, $upfx, $ibx) .
100 warn "Error lookup up additional messages: $@\n";
101 $str = '<pre>Error looking up additional messages</pre>';
106 # /$INBOX/$MESSAGE_ID/#R
108 my ($ctx, $hdr) = @_;
110 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
112 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
115 my $ibx = $ctx->{-inbox};
116 if (my $url = $ibx->{infourl}) {
117 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
118 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
121 my ($arg, $link, $reply_to_all) =
122 PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
123 if (ref($arg) eq 'SCALAR') {
124 return '<pre id=R>'.ascii_html($$arg).'</pre>';
127 # mailto: link only works if address obfuscation is disabled
131 * If your mail client supports setting the <b>In-Reply-To</b> header
132 via mailto: links, try the <a
133 href="$link">mailto: link</a>
137 push @$arg, '/path/to/YOUR_REPLY';
138 $arg = ascii_html(join(" \\\n ", '', @$arg));
141 id=R><b>Reply instructions:</b>
143 You may reply publically to <a
144 href=#t>this message</a> via plain-text email
145 using any one of the following methods:
147 * Save the following mbox file, import it into your mail client,
148 and $reply_to_all from there: <a
151 Avoid top-posting and favor interleaved quoting:
153 href="$p_url">$p_url</a>
155 * Reply using the <b>--to</b>, <b>--cc</b>, and <b>--in-reply-to</b>
156 switches of git-send-email(1):
161 href="$se_url">$se_url</a>
168 my $refs = references($hdr);
172 sub fold_addresses ($) {
173 return $_[0] if length($_[0]) <= COLS;
174 # try to fold on commas after non-word chars before $lim chars,
175 # Try to get the "," preceeded by ">" or ")", but avoid folding
176 # on the comma where somebody uses "Lastname, Firstname".
177 # We also try to keep the last and penultimate addresses in
178 # the list on the same line if possible, hence the extra \z
179 # Fall back to folding on spaces at $lim + 1 chars
180 my $lim = COLS - 8; # 8 = "\t" display width
181 my $too_long = $lim + 1;
182 $_[0] =~ s/\s*\z//s; # Email::Simple doesn't strip trailing spaces
184 ($_[0] =~ /(.{0,$lim}\W(?:,|\z)|
187 .{$too_long,}?)(?:\s|\z)/xgo));
190 sub _hdr_names_html ($$) {
191 my ($hdr, $field) = @_;
192 my $val = $hdr->header($field) or return '';
193 ascii_html(join(', ', PublicInbox::Address::names($val)));
197 my ($nr, $singular, $plural) = @_;
198 return "0 $plural" if $nr == 0;
199 $nr == 1 ? "$nr $singular" : "$nr $plural";
202 # this is already inside a <pre>
204 my ($smsg, $ctx, $more) = @_;
205 my $srch = $ctx->{srch};
206 my $subj = $smsg->subject;
207 my $mid_raw = $smsg->mid;
208 my $id = id_compress($mid_raw, 1);
211 my $root_anchor = $ctx->{root_anchor} || '';
213 my $obfs_ibx = $ctx->{-obfs_ibx};
215 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
216 $subj = '<b>'.ascii_html($subj).'</b>';
217 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
218 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
220 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
222 my $ds = $smsg->ds; # for v1 non-Xapian/SQLite users
223 # deleting {mime} is critical to memory use,
224 # the rest of the fields saves about 400K as we iterate across 1K msgs
225 my ($mime) = delete @$smsg{qw(mime ds ts blob subject)};
227 my $hdr = $mime->header_obj;
228 my $from = _hdr_names_html($hdr, 'From');
229 obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
230 $rv .= "From: $from @ ".fmt_ts($ds)." UTC";
231 my $upfx = $ctx->{-upfx};
232 my $mhref = $upfx . mid_escape($mid_raw) . '/';
233 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
234 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
235 my $to = fold_addresses(_hdr_names_html($hdr, 'To'));
236 my $cc = fold_addresses(_hdr_names_html($hdr, 'Cc'));
237 my ($tlen, $clen) = (length($to), length($cc));
239 if (($tlen + $clen) > COLS) {
240 $to_cc .= ' To: '.$to."\n" if $tlen;
241 $to_cc .= ' Cc: '.$cc."\n" if $clen;
244 $to_cc .= ' To: '.$to;
245 $to_cc .= '; <b>+Cc:</b> '.$cc if $clen;
247 $to_cc .= ' Cc: '.$cc if $clen;
251 obfuscate_addrs($obfs_ibx, $to_cc) if $obfs_ibx;
254 my $mapping = $ctx->{mapping};
255 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
256 my $mirt = PublicInbox::Hval->new_msgid($irt);
257 my $href = $upfx . $mirt->{href}. '/';
258 my $html = $mirt->as_html;
259 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
263 # scan through all parts, looking for displayable text
264 my $ibx = $ctx->{-inbox};
265 msg_iter($mime, sub { $rv .= add_text_body($mhref, $ibx, $_[0]) });
268 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
269 "<a\nhref=\"$mhref\">permalink</a>" .
270 " <a\nhref=\"${mhref}raw\">raw</a>" .
271 " <a\nhref=\"${mhref}#R\">reply</a>";
274 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
275 $rv .= "\t[relevance $pct->{$mid_raw}%]";
278 my $nested = 'nested';
283 $flat = "<b>$flat</b>";
285 $nested = "<b>$nested</b>";
287 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
288 $rv .= "|<a\nhref=\"${mhref}t/#u\">$nested</a>]";
289 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
294 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
298 sub pad_link ($$;$) {
299 my ($mid, $level, $s) = @_;
301 my $id = id_compress($mid, 1);
302 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
306 my ($mid_raw, $irt, $id, $ctx) = @_;
308 my $mapping = $ctx->{mapping} or return $rv;
310 my $mid_map = $mapping->{$mid_raw};
312 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
313 my ($attr, $node, $idx, $level) = @$mid_map;
314 my $children = $node->{children};
315 my $nr_c = scalar @$children;
318 if (my $smsg = $node->{smsg}) {
319 # delete saves about 200KB on a 1K message thread
320 if (my $refs = delete $smsg->{references}) {
321 ($$irt) = ($refs =~ m/<([^>]+)>\z/);
324 my $irt_map = $mapping->{$$irt} if defined $$irt;
325 if (defined $irt_map) {
326 $siblings = $irt_map->[1]->{children};
327 $nr_s = scalar(@$siblings) - 1;
328 $rv .= $pad . $irt_map->[0];
330 my $prev = $siblings->[$idx - 1];
331 my $pmid = $prev->{id};
333 my $s = ($idx - 1). ' preceding siblings ...';
334 $rv .= pad_link($pmid, $level, $s);
335 } elsif ($idx == 2) {
336 my $ppmid = $siblings->[0]->{id};
337 $rv .= $pad . $mapping->{$ppmid}->[0];
339 $rv .= $pad . $mapping->{$pmid}->[0];
342 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
343 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
344 $attr =~ s!\n\z!</b>\n!s;
345 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
346 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
349 my $cmid = $children->[0]->{id};
350 $rv .= $pad . $mapping->{$cmid}->[0];
352 my $s = ($nr_c - 1). ' more replies';
353 $rv .= pad_link($cmid, $level + 1, $s);
354 } elsif (my $cn = $children->[1]) {
355 $rv .= $pad . $mapping->{$cn->{id}}->[0];
359 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
361 my $nmid = $next->{id};
362 $rv .= $pad . $mapping->{$nmid}->[0];
363 my $nnext = $nr_s - $idx;
365 my $s = ($nnext - 1).' subsequent siblings';
366 $rv .= pad_link($nmid, $level, $s);
367 } elsif (my $nn = $siblings->[$idx + 2]) {
368 $rv .= $pad . $mapping->{$nn->{id}}->[0];
371 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
375 my ($rootset, $ctx, $cb) = @_;
376 my @q = map { (0, $_, -1) } @$rootset;
378 my ($level, $node, $i) = splice(@q, 0, 3);
379 defined $node or next;
380 $cb->($ctx, $level, $node, $i) or return;
383 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
388 my ($ctx, $level, $node, $idx) = @_;
389 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
390 skel_dump($ctx, $level, $node);
393 sub thread_index_entry {
394 my ($ctx, $level, $smsg) = @_;
395 my ($beg, $end) = thread_adj_level($ctx, $level);
396 $beg . '<pre>' . index_entry($smsg, $ctx, 0) . '</pre>' . $end;
399 sub stream_thread ($$) {
400 my ($rootset, $ctx) = @_;
401 my $inbox = $ctx->{-inbox};
402 my @q = map { (0, $_) } @$rootset;
407 my $node = shift @q or next;
409 unshift @q, map { ($cl, $_) } @{$node->{children}};
410 $smsg = $inbox->smsg_mime($node->{smsg}) and last;
412 return missing_thread($ctx) unless $smsg;
414 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
415 $ctx->{-title_html} = ascii_html($smsg->subject);
416 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg);
418 PublicInbox::WwwStream->response($ctx, 200, sub {
422 my $node = shift @q or next;
424 unshift @q, map { ($cl, $_) } @{$node->{children}};
425 if ($smsg = $inbox->smsg_mime($node->{smsg})) {
426 return thread_index_entry($ctx, $level, $smsg);
428 return ghost_index_entry($ctx, $level, $node);
431 my $ret = join('', thread_adj_level($ctx, 0));
432 $ret .= ${$ctx->{dst}}; # skel
440 my $mid = $ctx->{mid};
441 my $srch = $ctx->{srch};
442 my ($nr, $msgs) = $srch->get_thread($mid);
443 return missing_thread($ctx) if $nr == 0;
444 my $skel = '<hr><pre>';
445 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
446 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
447 $skel .= "<b\nid=t>Thread overview:</b> ";
448 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
449 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
450 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
451 $skel .= "-- links below jump to the message on this page --\n";
452 $ctx->{-upfx} = '../../';
453 $ctx->{cur_level} = 0;
454 $ctx->{dst} = \$skel;
455 $ctx->{prev_attr} = '';
456 $ctx->{prev_level} = 0;
457 $ctx->{root_anchor} = anchor_for($mid);
458 $ctx->{mapping} = {};
459 $ctx->{s_nr} = "$nr+ messages in thread";
461 my $rootset = thread_results($ctx, $msgs);
463 # reduce hash lookups in pre_thread->skel_dump
464 my $inbox = $ctx->{-inbox};
465 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
466 walk_thread($rootset, $ctx, *pre_thread);
469 return stream_thread($rootset, $ctx) unless $ctx->{flat};
471 # flat display: lazy load the full message from smsg
473 while (my $m = shift @$msgs) {
474 $smsg = $inbox->smsg_mime($m) and last;
476 return missing_thread($ctx) unless $smsg;
477 $ctx->{-title_html} = ascii_html($smsg->subject);
478 $ctx->{-html_tip} = '<pre>'.index_entry($smsg, $ctx, scalar @$msgs);
480 PublicInbox::WwwStream->response($ctx, 200, sub {
483 while (my $m = shift @$msgs) {
484 $smsg = $inbox->smsg_mime($m) and last;
486 return index_entry($smsg, $ctx, scalar @$msgs) if $smsg;
492 sub multipart_text_as_html {
493 my ($mime, $upfx, $ibx) = @_;
496 # scan through all parts, looking for displayable text
497 msg_iter($mime, sub { $rv .= add_text_body($upfx, $ibx, $_[0]) });
502 my ($s, $l, $quot) = @_;
504 # show everything in the full version with anchor from
505 # short version (see above)
506 my $rv = $l->linkify_1(join('', @$quot));
509 # we use a <span> here to allow users to specify their own
510 # color for quoted text
511 $rv = $l->linkify_2(ascii_html($rv));
512 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
515 sub attach_link ($$$$;$) {
516 my ($upfx, $ct, $p, $fn, $err) = @_;
517 my ($part, $depth, @idx) = @$p;
518 my $nl = $idx[-1] > 1 ? "\n" : '';
519 my $idx = join('.', @idx);
520 my $size = bytes::length($part->body);
522 # hide attributes normally, unless we want to aid users in
523 # spotting MUA problems:
524 $ct =~ s/;.*// unless $err;
525 $ct = ascii_html($ct);
526 my $desc = $part->header('Content-Description');
527 $desc = $fn unless defined $desc;
528 $desc = '' unless defined $desc;
530 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
532 } elsif ($ct eq 'text/plain') {
537 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
540 "[-- Warning: decoded text below may be mangled --]\n";
542 $ret .= "[-- Attachment #$idx: ";
543 my $ts = "Type: $ct, Size: $size bytes";
544 $desc = ascii_html($desc);
545 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
550 my ($upfx, $ibx, $p) = @_;
551 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
552 # $p - from msg_iter: [ Email::MIME, depth, @idx ]
553 my ($part, $depth) = @$p; # attachment @idx is unused
554 my $ct = $part->content_type || 'text/plain';
555 my $fn = $part->filename;
556 my ($s, $err) = msg_part_text($part, $ct);
558 return attach_link($upfx, $ct, $p, $fn) unless defined $s;
561 if ($ibx->{-repo_objs} && $s =~ /^(?:diff|---|\+{3}) /ms) {
563 my $n_slash = $upfx =~ tr!/!/!;
566 } elsif ($n_slash == 1) {
568 } else { # nslash == 2
573 my @lines = split(/^/m, $s);
575 if (defined($fn) || $depth > 0 || $err) {
576 # badly-encoded message with $err? tell the world about it!
577 $s .= attach_link($upfx, $ct, $p, $fn, $err);
581 my $l = PublicInbox::Linkify->new;
582 foreach my $cur (@lines) {
584 # show the previously buffered quote inline
585 flush_quote(\$s, $l, \@quot) if @quot;
592 $s .= $l->linkify_2(ascii_html($cur));
595 flush_diff(\$s, $spfx, $l, $diff) if $diff && @$diff;
600 if (@quot) { # ugh, top posted
601 flush_quote(\$s, $l, \@quot);
602 flush_diff(\$s, $spfx, $l, $diff) if $diff && @$diff;
603 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
606 flush_diff(\$s, $spfx, $l, $diff) if $diff && @$diff;
607 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
608 if ($s =~ /\n\z/s) { # common, last line ends with a newline
610 } else { # some editors don't do newlines...
616 sub _msg_html_prepare {
617 my ($hdr, $ctx, $more, $nr) = @_;
618 my $srch = $ctx->{srch} if $ctx;
620 my $obfs_ibx = $ctx->{-obfs_ibx};
622 my $mids = mids($hdr);
623 my $multiple = scalar(@$mids) > 1; # zero, one, infinity
627 "<pre>WARNING: multiple messages refer to this Message-ID\n</pre>";
629 $rv .= "<pre\nid=b>"; # anchor for body start
634 $ctx->{-upfx} = '../';
638 if (defined($v = $hdr->header('From'))) {
639 $v = PublicInbox::Hval->new($v);
640 my @n = PublicInbox::Address::names($v->raw);
641 $title[1] = ascii_html(join(', ', @n));
644 obfuscate_addrs($obfs_ibx, $v);
645 obfuscate_addrs($obfs_ibx, $title[1]);
647 $rv .= "From: $v\n" if $v ne '';
649 foreach my $h (qw(To Cc)) {
650 defined($v = $hdr->header($h)) or next;
653 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
654 $rv .= "$h: $v\n" if $v ne '';
656 if (defined($v = $hdr->header('Subject')) && ($v ne '')) {
658 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
660 $rv .= qq(Subject: <a\nhref="#r"\nid=t>$v</a>\n);
662 $rv .= "Subject: $v\n";
665 } else { # dummy anchor for thread skeleton at bottom of page
666 $rv .= qq(<a\nhref="#r"\nid=t></a>) if $srch;
667 $title[0] = '(no subject)';
669 if (defined($v = $hdr->header('Date'))) {
671 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; # possible :P
674 $ctx->{-title_html} = join(' - ', @title);
676 my $mid = PublicInbox::Hval->new_msgid($_) ;
677 my $mhtml = $mid->as_html;
679 my $href = $mid->{href};
680 $rv .= "Message-ID: ";
681 $rv .= "<a\nhref=\"../$href/\">";
682 $rv .= "<$mhtml></a> ";
683 $rv .= "(<a\nhref=\"../$href/raw\">raw</a>)\n";
685 $rv .= "Message-ID: <$mhtml> ";
686 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
689 $rv .= _parent_headers($hdr, $srch);
694 my ($dst, $ctx, $hdr, $tpfx) = @_;
695 my $srch = $ctx->{srch};
696 my $mid = mids($hdr)->[0];
697 my ($nr, $msgs) = $srch->get_thread($mid);
698 my $expand = qq(expand[<a\nhref="${tpfx}T/#u">flat</a>) .
699 qq(|<a\nhref="${tpfx}t/#u">nested</a>] ) .
700 qq(<a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
701 qq(<a\nhref="${tpfx}t.atom">Atom feed</a>);
703 my $parent = in_reply_to($hdr);
704 $$dst .= "\n<b>Thread overview: </b>";
706 if (defined $parent) {
707 $$dst .= "$expand\n ";
708 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
710 $$dst .= "[no followups] $expand\n";
712 $ctx->{next_msg} = undef;
713 $ctx->{parent_msg} = $parent;
717 $$dst .= "$nr+ messages / $expand";
718 $$dst .= qq! <a\nhref="#b">top</a>\n!;
720 my $subj = $hdr->header('Subject');
721 defined $subj or $subj = '';
722 $ctx->{prev_subj} = [ split(/ /, $srch->subject_normalized($subj)) ];
724 $ctx->{prev_attr} = '';
725 $ctx->{prev_level} = 0;
728 # reduce hash lookups in skel_dump
729 my $ibx = $ctx->{-inbox};
730 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
731 walk_thread(thread_results($ctx, $msgs), $ctx, *skel_dump);
733 $ctx->{parent_msg} = $parent;
736 sub _parent_headers {
737 my ($hdr, $srch) = @_;
740 my $refs = references($hdr);
741 my $irt = pop @$refs;
743 my $v = PublicInbox::Hval->new_msgid($irt);
744 my $html = $v->as_html;
745 my $href = $v->{href};
746 $rv .= "In-Reply-To: <";
747 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
750 # do not display References: if search is present,
751 # we show the thread skeleton at the bottom, instead.
755 @$refs = map { linkify_ref_nosrch($_) } @$refs;
756 $rv .= 'References: '. join("\n\t", @$refs) . "\n";
762 my ($hdr, $standalone, $ctx, $rhref) = @_;
764 my $srch = $ctx->{srch} if $ctx;
767 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
771 thread_skel(\$idx, $ctx, $hdr, $tpfx);
776 if (my $n = $ctx->{next_msg}) {
777 $n = PublicInbox::Hval->new_msgid($n)->{href};
778 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
781 my $par = $ctx->{parent_msg};
783 $u = PublicInbox::Hval->new_msgid($par)->{href};
786 if (my $p = $ctx->{prev_msg}) {
787 $prev = PublicInbox::Hval->new_msgid($p)->{href};
788 if ($p && $par && $p eq $par) {
789 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
790 'rel=prev>prev parent</a>';
793 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
795 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
797 } elsif ($u) { # unlikely
798 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
800 $irt = "$next $prev$parent ";
805 $irt .= qq(<a\nhref="$rhref">reply</a>);
809 sub linkify_ref_nosrch {
810 my $v = PublicInbox::Hval->new_msgid($_[0]);
811 my $html = $v->as_html;
812 my $href = $v->{href};
813 "<<a\nhref=\"../$href/\">$html</a>>";
818 'm' . id_compress($msgid, 1);
822 my ($upfx, $mid) = @_;
824 $mid = PublicInbox::Hval->new_msgid($mid);
825 my $href = $mid->{href};
826 my $html = $mid->as_html;
827 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
832 $level ? INDENT x ($level - 1) : '';
836 my ($ctx, $level, $node, $idx) = @_;
837 ++$ctx->{root_idx} if $level == 0;
838 if ($node->{id} eq $ctx->{mid}) {
839 $ctx->{found_mid_at} = $ctx->{root_idx};
845 sub strict_loose_note ($) {
848 " -- strict thread matches above, loose matches on Subject: below --\n";
850 if ($nr > PublicInbox::Over::DEFAULT_LIMIT()) {
852 " -- use mbox.gz link to download all $nr messages --\n";
858 my ($ctx, $msgs) = @_;
859 require PublicInbox::SearchThread;
860 my $ibx = $ctx->{-inbox};
861 my $rootset = PublicInbox::SearchThread::thread($msgs, *sort_ds, $ibx);
863 # FIXME: `tid' is broken on --reindex, so that needs to be fixed
864 # and preserved in the future. This bug is hidden by `sid' matches
865 # in get_thread, so we never noticed it until now. And even when
866 # reindexing is fixed, we'll keep this code until a SCHEMA_VERSION
867 # bump since reindexing is expensive and users may not do it
869 # loose threading could've returned too many results,
870 # put the root the message we care about at the top:
871 my $mid = $ctx->{mid};
872 if (defined($mid) && scalar(@$rootset) > 1) {
873 $ctx->{root_idx} = -1;
874 my $nr = scalar @$msgs;
875 walk_thread($rootset, $ctx, *find_mid_root);
876 my $idx = $ctx->{found_mid_at};
877 if (defined($idx) && $idx != 0) {
878 my $tip = splice(@$rootset, $idx, 1);
879 @$rootset = reverse @$rootset;
880 unshift @$rootset, $tip;
881 $ctx->{sl_note} = strict_loose_note($nr);
889 require PublicInbox::ExtMsg;
890 PublicInbox::ExtMsg::ext_msg($ctx);
895 fmt_ts(msg_datestamp($hdr));
898 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
901 my ($prev_subj, $subj, $val) = @_;
903 my $omit = ''; # '"' denotes identical text omitted
904 my (@prev_pop, @curr_pop);
905 while (@$prev_subj && @$subj && $subj->[-1] eq $prev_subj->[-1]) {
906 push(@prev_pop, pop(@$prev_subj));
907 push(@curr_pop, pop(@$subj));
910 pop @$subj if @$subj && $subj->[-1] =~ /^re:\s*/i;
911 if (scalar(@curr_pop) == 1) {
913 push @$prev_subj, @prev_pop;
914 push @$subj, @curr_pop;
920 my ($ctx, $level, $node) = @_;
921 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
923 my $dst = $ctx->{dst};
924 my $cur = $ctx->{cur};
925 my $mid = $smsg->{mid};
927 if ($level == 0 && $ctx->{skel_dump_roots}++) {
928 $$dst .= delete $ctx->{sl_note} || '';
931 my $f = ascii_html($smsg->from_name);
932 my $obfs_ibx = $ctx->{-obfs_ibx};
933 obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
935 my $d = fmt_ts($smsg->{ds}) . ' ' . indent_for($level) . th_pfx($level);
937 $ctx->{first_level} ||= $level;
939 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
940 $ctx->{prev_attr} = $attr;
942 $ctx->{prev_level} = $level;
947 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
948 "$attr [this message]</a></b>\n";
951 $ctx->{prev_msg} = $mid;
954 $ctx->{next_msg} ||= $mid;
957 # Subject is never undef, this mail was loaded from
958 # our Xapian which would've resulted in '' if it were
959 # really missing (and Filter rejects empty subjects)
960 my @subj = split(/ /, $ctx->{srch}->subject_normalized($smsg->subject));
962 # remove common suffixes from the subject if it matches the previous,
963 # so we do not show redundant text at the end.
964 my $prev_subj = $ctx->{prev_subj} || [];
965 $ctx->{prev_subj} = [ @subj ];
966 my $omit = dedupe_subject($prev_subj, \@subj, '" ');
969 my $subj = join(' ', @subj);
970 $subj = ascii_html($subj);
971 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
972 $end = "$subj</a> $omit$f\n"
978 my $mapping = $ctx->{mapping};
980 my $map = $mapping->{$mid};
981 $id = id_compress($mid, 1);
983 $map->[0] = "$d<a\nhref=\"$m\">$end";
986 $m = $ctx->{-upfx}.mid_escape($mid).'/';
988 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
993 my ($ctx, $level, $node) = @_;
995 my $mid = $node->{id};
996 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
998 $d .= indent_for($level) . th_pfx($level);
999 my $upfx = $ctx->{-upfx};
1000 my $m = PublicInbox::Hval->new_msgid($mid);
1001 my $href = $upfx . $m->{href} . '/';
1002 my $html = $m->as_html;
1004 my $mapping = $ctx->{mapping};
1005 my $map = $mapping->{$mid} if $mapping;
1007 my $id = id_compress($mid, 1);
1008 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
1009 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
1011 $d .= qq{<<a\nhref="$href">$html</a>>\n};
1013 my $dst = $ctx->{dst};
1020 (eval { $a->topmost->{smsg}->ds } || 0) <=>
1021 (eval { $b->topmost->{smsg}->ds } || 0)
1025 # accumulate recent topics if search is supported
1026 # returns 200 if done, 404 if not
1028 my ($ctx, $level, $node) = @_;
1029 my $srch = $ctx->{srch};
1030 my $mid = $node->{id};
1031 my $x = $node->{smsg} || $ctx->{-inbox}->smsg_by_mid($mid);
1035 $subj = $x->subject;
1036 $subj = $srch->subject_normalized($subj);
1039 $topic = [ $ds, 1, { $subj => $mid }, $subj ];
1040 $ctx->{-cur_topic} = $topic;
1041 push @{$ctx->{order}}, $topic;
1045 $topic = $ctx->{-cur_topic}; # should never be undef
1046 $topic->[0] = $ds if $ds > $topic->[0];
1048 my $seen = $topic->[2];
1049 if (scalar(@$topic) == 3) { # parent was a ghost
1050 push @$topic, $subj;
1051 } elsif (!$seen->{$subj}) {
1052 push @$topic, $level, $subj;
1054 $seen->{$subj} = $mid; # latest for subject
1055 } else { # ghost message
1056 return 1 if $level != 0; # ignore child ghosts
1057 $topic = [ -666, 0, {} ];
1058 $ctx->{-cur_topic} = $topic;
1059 push @{$ctx->{order}}, $topic;
1066 my $order = delete $ctx->{order}; # [ ds, subj1, subj2, subj3, ... ]
1068 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
1073 my $ibx = $ctx->{-inbox};
1074 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
1075 my $srch = $ctx->{srch};
1077 # sort by recency, this allows new posts to "bump" old topics...
1078 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
1079 my ($ds, $n, $seen, $top, @ex) = @$topic;
1081 next unless defined $top; # ghost topic
1082 my $mid = delete $seen->{$top};
1083 my $href = mid_escape($mid);
1084 my $prev_subj = [ split(/ /, $top) ];
1085 $top = PublicInbox::Hval->new($top)->as_html;
1088 # $n isn't the total number of posts on the topic,
1089 # just the number of posts in the current results window
1093 $anchor = '#u'; # top of only message
1095 $n = " ($n+ messages)";
1096 $anchor = '#t'; # thread skeleton
1099 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
1100 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
1101 my $s = "<a\nhref=\"$href/T/$anchor\">$top</a>\n" .
1102 " $ds UTC $n - $mbox / $atom\n";
1103 for (my $i = 0; $i < scalar(@ex); $i += 2) {
1104 my $level = $ex[$i];
1105 my $subj = $ex[$i + 1];
1106 $mid = delete $seen->{$subj};
1107 my @subj = split(/ /, $srch->subject_normalized($subj));
1108 my @next_prev = @subj; # full copy
1109 my $omit = dedupe_subject($prev_subj, \@subj, ' "');
1110 $prev_subj = \@next_prev;
1111 $subj = ascii_html(join(' ', @subj));
1112 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
1113 $href = mid_escape($mid);
1114 $s .= indent_for($level) . TCHILD;
1115 $s .= "<a\nhref=\"$href/T/#u\">$subj</a>$omit\n";
1119 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
1125 POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts));
1129 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]);
1130 timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
1133 sub pagination_footer ($$) {
1134 my ($ctx, $latest) = @_;
1135 delete $ctx->{qp} or return;
1136 my $next = $ctx->{next_page} || '';
1137 my $prev = $ctx->{prev_page} || '';
1139 $next = $next ? "$next " : ' ';
1140 $prev .= qq! <a\nhref='$latest'>latest</a>!;
1142 "<hr><pre>page: $next$prev</pre>";
1145 sub index_nav { # callback for WwwStream
1146 my (undef, $ctx) = @_;
1147 pagination_footer($ctx, '.')
1150 sub paginate_recent ($$) {
1151 my ($ctx, $lim) = @_;
1152 my $t = $ctx->{qp}->{t} || '';
1153 my $opts = { limit => $lim };
1154 my ($after, $before);
1156 # Xapian uses '..' but '-' is perhaps friendier to URL linkifiers
1157 # if only $after exists "YYYYMMDD.." because "." could be skipped
1158 # if interpreted as an end-of-sentence
1159 $t =~ s/\A(\d{8,14})-// and $after = str2ts($1);
1160 $t =~ /\A(\d{8,14})\z/ and $before = str2ts($1);
1162 my $ibx = $ctx->{-inbox};
1163 my $msgs = $ibx->recent($opts, $after, $before);
1164 my $nr = scalar @$msgs;
1165 if ($nr < $lim && defined($after)) {
1166 $after = $before = undef;
1167 $msgs = $ibx->recent($opts);
1168 $nr = scalar @$msgs;
1170 my $more = $nr == $lim;
1171 my ($newest, $oldest);
1173 $newest = $msgs->[0]->{ts};
1174 $oldest = $msgs->[-1]->{ts};
1175 # if we only had $after, our SQL query in ->recent ordered
1176 if ($newest < $oldest) {
1177 ($oldest, $newest) = ($newest, $oldest);
1178 $more = 0 if defined($after) && $after < $oldest;
1181 if (defined($oldest) && $more) {
1182 my $s = ts2str($oldest);
1183 $ctx->{next_page} = qq!<a\nhref="?t=$s"\nrel=next>next</a>!;
1185 if (defined($newest) && (defined($before) || defined($after))) {
1186 my $s = ts2str($newest);
1187 $ctx->{prev_page} = qq!<a\nhref="?t=$s-"\nrel=prev>prev</a>!;
1194 my $msgs = paginate_recent($ctx, 200); # 200 is our window
1196 walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic);
1198 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
1201 sub thread_adj_level {
1202 my ($ctx, $level) = @_;
1204 my $max = $ctx->{cur_level};
1206 return ('', '') if $max == 0; # flat output
1208 # reset existing lists
1209 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
1210 $ctx->{cur_level} = 0;
1212 } elsif ($level == $max) { # continue existing list
1214 } elsif ($level < $max) {
1215 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1216 $ctx->{cur_level} = $level;
1217 ("$beg<li>", '</li>');
1218 } else { # ($level > $max) # start a new level
1219 $ctx->{cur_level} = $level;
1220 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1225 sub ghost_index_entry {
1226 my ($ctx, $level, $node) = @_;
1227 my ($beg, $end) = thread_adj_level($ctx, $level);
1228 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})