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;
18 use Time::Local qw(timegm);
20 use constant COLS => 72;
21 use constant INDENT => ' ';
22 use constant TCHILD => '` ';
23 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
25 # public functions: (unstable)
28 my ($ctx, $mime, $more, $smsg) = @_;
29 my $hdr = $mime->header_obj;
30 my $ibx = $ctx->{-inbox};
31 my $obfs_ibx = $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
32 my $tip = _msg_html_prepare($hdr, $ctx, $more, 0);
34 PublicInbox::WwwStream->response($ctx, 200, sub {
37 # $more cannot be true w/o $smsg being defined:
38 my $upfx = $more ? '../'.mid_escape($smsg->mid).'/' : '';
39 $tip . multipart_text_as_html($mime, $upfx, $obfs_ibx) .
41 } elsif ($more && @$more) {
43 msg_html_more($ctx, $more, $nr);
44 } elsif ($nr == $end) {
45 # fake an EOF if generating the footer fails;
46 # we want to at least show the message if something
49 '<pre>' . html_footer($hdr, 1, $ctx) .
50 '</pre>' . msg_reply($ctx, $hdr)
60 my $mid = $ctx->{mid};
61 my $ibx = $ctx->{-inbox};
64 if (my $srch = $ibx->search) {
66 $smsg = $srch->next_by_mid($mid, \$id, \$prev);
67 $first = $ibx->msg_by_smsg($smsg) if $smsg;
69 my $next = $srch->next_by_mid($mid, \$id, \$prev);
70 $more = [ $id, $prev, $next ] if $next;
74 $first = $ibx->msg_by_mid($mid) or return;
76 msg_html($ctx, PublicInbox::MIME->new($first), $more, $smsg);
80 my ($ctx, $more, $nr) = @_;
82 my ($id, $prev, $smsg) = @$more;
83 my $mid = $ctx->{mid};
84 $smsg = $ctx->{-inbox}->smsg_mime($smsg);
85 my $next = $ctx->{srch}->next_by_mid($mid, \$id, \$prev);
86 @$more = $next ? ($id, $prev, $next) : ();
88 my $mime = $smsg->{mime};
89 my $upfx = '../' . mid_escape($smsg->mid) . '/';
90 _msg_html_prepare($mime->header_obj, $ctx, $more, $nr) .
91 multipart_text_as_html($mime, $upfx,
99 warn "Error lookup up additional messages: $@\n";
100 $str = '<pre>Error looking up additional messages</pre>';
105 # /$INBOX/$MESSAGE_ID/#R
107 my ($ctx, $hdr) = @_;
109 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
111 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
114 my $ibx = $ctx->{-inbox};
115 if (my $url = $ibx->{infourl}) {
116 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
117 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
120 my ($arg, $link, $reply_to_all) =
121 PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
122 if (ref($arg) eq 'SCALAR') {
123 return '<pre id=R>'.ascii_html($$arg).'</pre>';
126 # mailto: link only works if address obfuscation is disabled
130 * If your mail client supports setting the <b>In-Reply-To</b> header
131 via mailto: links, try the <a
132 href="$link">mailto: link</a>
136 push @$arg, '/path/to/YOUR_REPLY';
137 $arg = ascii_html(join(" \\\n ", '', @$arg));
140 id=R><b>Reply instructions:</b>
142 You may reply publically to <a
143 href=#t>this message</a> via plain-text email
144 using any one of the following methods:
146 * Save the following mbox file, import it into your mail client,
147 and $reply_to_all from there: <a
150 Avoid top-posting and favor interleaved quoting:
152 href="$p_url">$p_url</a>
154 * Reply using the <b>--to</b>, <b>--cc</b>, and <b>--in-reply-to</b>
155 switches of git-send-email(1):
160 href="$se_url">$se_url</a>
167 my $refs = references($hdr);
171 sub fold_addresses ($) {
172 return $_[0] if length($_[0]) <= COLS;
173 # try to fold on commas after non-word chars before $lim chars,
174 # Try to get the "," preceeded by ">" or ")", but avoid folding
175 # on the comma where somebody uses "Lastname, Firstname".
176 # We also try to keep the last and penultimate addresses in
177 # the list on the same line if possible, hence the extra \z
178 # Fall back to folding on spaces at $lim + 1 chars
179 my $lim = COLS - 8; # 8 = "\t" display width
180 my $too_long = $lim + 1;
181 $_[0] =~ s/\s*\z//s; # Email::Simple doesn't strip trailing spaces
183 ($_[0] =~ /(.{0,$lim}\W(?:,|\z)|
186 .{$too_long,}?)(?:\s|\z)/xgo));
189 sub _hdr_names_html ($$) {
190 my ($hdr, $field) = @_;
191 my $val = $hdr->header($field) or return '';
192 ascii_html(join(', ', PublicInbox::Address::names($val)));
196 my ($nr, $singular, $plural) = @_;
197 return "0 $plural" if $nr == 0;
198 $nr == 1 ? "$nr $singular" : "$nr $plural";
201 # this is already inside a <pre>
203 my ($smsg, $ctx, $more) = @_;
204 my $srch = $ctx->{srch};
205 my $subj = $smsg->subject;
206 my $mid_raw = $smsg->mid;
207 my $id = id_compress($mid_raw, 1);
210 my $root_anchor = $ctx->{root_anchor} || '';
212 my $obfs_ibx = $ctx->{-obfs_ibx};
214 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
215 $subj = '<b>'.ascii_html($subj).'</b>';
216 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
217 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
219 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
221 my $mime = $smsg->{mime};
222 my $hdr = $mime->header_obj;
223 my $from = _hdr_names_html($hdr, 'From');
224 obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
225 $rv .= "From: $from @ ".fmt_ts($smsg->ds)." UTC";
226 my $upfx = $ctx->{-upfx};
227 my $mhref = $upfx . mid_escape($mid_raw) . '/';
228 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
229 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
230 my $to = fold_addresses(_hdr_names_html($hdr, 'To'));
231 my $cc = fold_addresses(_hdr_names_html($hdr, 'Cc'));
232 my ($tlen, $clen) = (length($to), length($cc));
234 if (($tlen + $clen) > COLS) {
235 $to_cc .= ' To: '.$to."\n" if $tlen;
236 $to_cc .= ' Cc: '.$cc."\n" if $clen;
239 $to_cc .= ' To: '.$to;
240 $to_cc .= '; <b>+Cc:</b> '.$cc if $clen;
242 $to_cc .= ' Cc: '.$cc if $clen;
246 obfuscate_addrs($obfs_ibx, $to_cc) if $obfs_ibx;
249 my $mapping = $ctx->{mapping};
250 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
251 my $mirt = PublicInbox::Hval->new_msgid($irt);
252 my $href = $upfx . $mirt->{href}. '/';
253 my $html = $mirt->as_html;
254 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
258 # scan through all parts, looking for displayable text
259 msg_iter($mime, sub { $rv .= add_text_body($mhref, $obfs_ibx, $_[0]) });
262 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
263 "<a\nhref=\"$mhref\">permalink</a>" .
264 " <a\nhref=\"${mhref}raw\">raw</a>" .
265 " <a\nhref=\"${mhref}#R\">reply</a>";
268 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
269 $rv .= "\t[relevance $pct->{$mid_raw}%]";
272 my $nested = 'nested';
277 $flat = "<b>$flat</b>";
279 $nested = "<b>$nested</b>";
281 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
282 $rv .= "|<a\nhref=\"${mhref}t/#u\">$nested</a>]";
283 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
288 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
292 sub pad_link ($$;$) {
293 my ($mid, $level, $s) = @_;
295 my $id = id_compress($mid, 1);
296 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
300 my ($mid_raw, $irt, $id, $ctx) = @_;
302 my $mapping = $ctx->{mapping} or return $rv;
304 my $mid_map = $mapping->{$mid_raw};
306 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
307 my ($attr, $node, $idx, $level) = @$mid_map;
308 my $children = $node->{children};
309 my $nr_c = scalar @$children;
312 if (my $smsg = $node->{smsg}) {
313 ($$irt) = (($smsg->{references} || '') =~ m/<([^>]+)>\z/);
315 my $irt_map = $mapping->{$$irt} if defined $$irt;
316 if (defined $irt_map) {
317 $siblings = $irt_map->[1]->{children};
318 $nr_s = scalar(@$siblings) - 1;
319 $rv .= $pad . $irt_map->[0];
321 my $prev = $siblings->[$idx - 1];
322 my $pmid = $prev->{id};
324 my $s = ($idx - 1). ' preceding siblings ...';
325 $rv .= pad_link($pmid, $level, $s);
326 } elsif ($idx == 2) {
327 my $ppmid = $siblings->[0]->{id};
328 $rv .= $pad . $mapping->{$ppmid}->[0];
330 $rv .= $pad . $mapping->{$pmid}->[0];
333 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
334 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
335 $attr =~ s!\n\z!</b>\n!s;
336 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
337 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
340 my $cmid = $children->[0]->{id};
341 $rv .= $pad . $mapping->{$cmid}->[0];
343 my $s = ($nr_c - 1). ' more replies';
344 $rv .= pad_link($cmid, $level + 1, $s);
345 } elsif (my $cn = $children->[1]) {
346 $rv .= $pad . $mapping->{$cn->{id}}->[0];
350 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
352 my $nmid = $next->{id};
353 $rv .= $pad . $mapping->{$nmid}->[0];
354 my $nnext = $nr_s - $idx;
356 my $s = ($nnext - 1).' subsequent siblings';
357 $rv .= pad_link($nmid, $level, $s);
358 } elsif (my $nn = $siblings->[$idx + 2]) {
359 $rv .= $pad . $mapping->{$nn->{id}}->[0];
362 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
366 my ($rootset, $ctx, $cb) = @_;
367 my @q = map { (0, $_, -1) } @$rootset;
369 my ($level, $node, $i) = splice(@q, 0, 3);
370 defined $node or next;
371 $cb->($ctx, $level, $node, $i) or return;
374 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
379 my ($ctx, $level, $node, $idx) = @_;
380 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
381 skel_dump($ctx, $level, $node);
384 sub thread_index_entry {
385 my ($ctx, $level, $smsg) = @_;
386 my ($beg, $end) = thread_adj_level($ctx, $level);
387 $beg . '<pre>' . index_entry($smsg, $ctx, 0) . '</pre>' . $end;
390 sub stream_thread ($$) {
391 my ($rootset, $ctx) = @_;
392 my $inbox = $ctx->{-inbox};
393 my @q = map { (0, $_) } @$rootset;
398 my $node = shift @q or next;
400 unshift @q, map { ($cl, $_) } @{$node->{children}};
401 $smsg = $inbox->smsg_mime($node->{smsg}) and last;
403 return missing_thread($ctx) unless $smsg;
405 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
406 $ctx->{-title_html} = ascii_html($smsg->subject);
407 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg);
409 PublicInbox::WwwStream->response($ctx, 200, sub {
413 my $node = shift @q or next;
415 unshift @q, map { ($cl, $_) } @{$node->{children}};
416 if ($smsg = $inbox->smsg_mime($node->{smsg})) {
417 return thread_index_entry($ctx, $level, $smsg);
419 return ghost_index_entry($ctx, $level, $node);
422 my $ret = join('', thread_adj_level($ctx, 0));
423 $ret .= ${$ctx->{dst}}; # skel
431 my $mid = $ctx->{mid};
432 my $srch = $ctx->{srch};
433 my ($nr, $msgs) = $srch->get_thread($mid);
434 return missing_thread($ctx) if $nr == 0;
435 my $skel = '<hr><pre>';
436 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
437 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
438 $skel .= "<b\nid=t>Thread overview:</b> ";
439 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
440 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
441 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
442 $skel .= "-- links below jump to the message on this page --\n";
443 $ctx->{-upfx} = '../../';
444 $ctx->{cur_level} = 0;
445 $ctx->{dst} = \$skel;
446 $ctx->{prev_attr} = '';
447 $ctx->{prev_level} = 0;
448 $ctx->{root_anchor} = anchor_for($mid);
449 $ctx->{mapping} = {};
450 $ctx->{s_nr} = "$nr+ messages in thread";
452 my $rootset = thread_results($ctx, $msgs);
454 # reduce hash lookups in pre_thread->skel_dump
455 my $inbox = $ctx->{-inbox};
456 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
457 walk_thread($rootset, $ctx, *pre_thread);
460 return stream_thread($rootset, $ctx) unless $ctx->{flat};
462 # flat display: lazy load the full message from smsg
464 while (my $m = shift @$msgs) {
465 $smsg = $inbox->smsg_mime($m) and last;
467 return missing_thread($ctx) unless $smsg;
468 $ctx->{-title_html} = ascii_html($smsg->subject);
469 $ctx->{-html_tip} = '<pre>'.index_entry($smsg, $ctx, scalar @$msgs);
471 PublicInbox::WwwStream->response($ctx, 200, sub {
474 while (my $m = shift @$msgs) {
475 $smsg = $inbox->smsg_mime($m) and last;
477 return index_entry($smsg, $ctx, scalar @$msgs) if $smsg;
483 sub multipart_text_as_html {
484 my ($mime, $upfx, $obfs_ibx) = @_;
487 # scan through all parts, looking for displayable text
488 msg_iter($mime, sub { $rv .= add_text_body($upfx, $obfs_ibx, $_[0]) });
493 my ($s, $l, $quot) = @_;
495 # show everything in the full version with anchor from
496 # short version (see above)
497 my $rv = $l->linkify_1(join('', @$quot));
500 # we use a <span> here to allow users to specify their own
501 # color for quoted text
502 $rv = $l->linkify_2(ascii_html($rv));
503 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
506 sub attach_link ($$$$;$) {
507 my ($upfx, $ct, $p, $fn, $err) = @_;
508 my ($part, $depth, @idx) = @$p;
509 my $nl = $idx[-1] > 1 ? "\n" : '';
510 my $idx = join('.', @idx);
511 my $size = bytes::length($part->body);
513 # hide attributes normally, unless we want to aid users in
514 # spotting MUA problems:
515 $ct =~ s/;.*// unless $err;
516 $ct = ascii_html($ct);
517 my $desc = $part->header('Content-Description');
518 $desc = $fn unless defined $desc;
519 $desc = '' unless defined $desc;
521 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
523 } elsif ($ct eq 'text/plain') {
528 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
531 "[-- Warning: decoded text below may be mangled --]\n";
533 $ret .= "[-- Attachment #$idx: ";
534 my $ts = "Type: $ct, Size: $size bytes";
535 $desc = ascii_html($desc);
536 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
541 my ($upfx, $obfs_ibx, $p) = @_;
542 # $p - from msg_iter: [ Email::MIME, depth, @idx ]
543 my ($part, $depth) = @$p; # attachment @idx is unused
544 my $ct = $part->content_type || 'text/plain';
545 my $fn = $part->filename;
546 my ($s, $err) = msg_part_text($part, $ct);
548 return attach_link($upfx, $ct, $p, $fn) unless defined $s;
550 my @lines = split(/^/m, $s);
552 if (defined($fn) || $depth > 0 || $err) {
553 # badly-encoded message with $err? tell the world about it!
554 $s .= attach_link($upfx, $ct, $p, $fn, $err);
558 my $l = PublicInbox::Linkify->new;
559 foreach my $cur (@lines) {
561 # show the previously buffered quote inline
562 flush_quote(\$s, $l, \@quot) if @quot;
566 $s .= $l->linkify_2(ascii_html($cur));
572 if (@quot) { # ugh, top posted
573 flush_quote(\$s, $l, \@quot);
574 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
577 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
578 if ($s =~ /\n\z/s) { # common, last line ends with a newline
580 } else { # some editors don't do newlines...
586 sub _msg_html_prepare {
587 my ($hdr, $ctx, $more, $nr) = @_;
588 my $srch = $ctx->{srch} if $ctx;
590 my $obfs_ibx = $ctx->{-obfs_ibx};
592 my $mids = mids($hdr);
593 my $multiple = scalar(@$mids) > 1; # zero, one, infinity
597 "<pre>WARNING: multiple messages refer to this Message-ID\n</pre>";
599 $rv .= "<pre\nid=b>"; # anchor for body start
604 $ctx->{-upfx} = '../';
608 if (defined($v = $hdr->header('From'))) {
609 $v = PublicInbox::Hval->new($v);
610 my @n = PublicInbox::Address::names($v->raw);
611 $title[1] = ascii_html(join(', ', @n));
614 obfuscate_addrs($obfs_ibx, $v);
615 obfuscate_addrs($obfs_ibx, $title[1]);
617 $rv .= "From: $v\n" if $v ne '';
619 foreach my $h (qw(To Cc)) {
620 defined($v = $hdr->header($h)) or next;
623 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
624 $rv .= "$h: $v\n" if $v ne '';
626 if (defined($v = $hdr->header('Subject')) && ($v ne '')) {
628 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
630 $rv .= qq(Subject: <a\nhref="#r"\nid=t>$v</a>\n);
632 $rv .= "Subject: $v\n";
635 } else { # dummy anchor for thread skeleton at bottom of page
636 $rv .= qq(<a\nhref="#r"\nid=t></a>) if $srch;
637 $title[0] = '(no subject)';
639 if (defined($v = $hdr->header('Date'))) {
641 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; # possible :P
644 $ctx->{-title_html} = join(' - ', @title);
646 my $mid = PublicInbox::Hval->new_msgid($_) ;
647 my $mhtml = $mid->as_html;
649 my $href = $mid->{href};
650 $rv .= "Message-ID: ";
651 $rv .= "<a\nhref=\"../$href/\">";
652 $rv .= "<$mhtml></a> ";
653 $rv .= "(<a\nhref=\"../$href/raw\">raw</a>)\n";
655 $rv .= "Message-ID: <$mhtml> ";
656 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
659 $rv .= _parent_headers($hdr, $srch);
664 my ($dst, $ctx, $hdr, $tpfx) = @_;
665 my $srch = $ctx->{srch};
666 my $mid = mids($hdr)->[0];
667 my ($nr, $msgs) = $srch->get_thread($mid);
668 my $expand = qq(expand[<a\nhref="${tpfx}T/#u">flat</a>) .
669 qq(|<a\nhref="${tpfx}t/#u">nested</a>] ) .
670 qq(<a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
671 qq(<a\nhref="${tpfx}t.atom">Atom feed</a>);
673 my $parent = in_reply_to($hdr);
674 $$dst .= "\n<b>Thread overview: </b>";
676 if (defined $parent) {
677 $$dst .= "$expand\n ";
678 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
680 $$dst .= "[no followups] $expand\n";
682 $ctx->{next_msg} = undef;
683 $ctx->{parent_msg} = $parent;
687 $$dst .= "$nr+ messages / $expand";
688 $$dst .= qq! <a\nhref="#b">top</a>\n!;
690 my $subj = $hdr->header('Subject');
691 defined $subj or $subj = '';
692 $ctx->{prev_subj} = [ split(/ /, $srch->subject_normalized($subj)) ];
694 $ctx->{prev_attr} = '';
695 $ctx->{prev_level} = 0;
698 # reduce hash lookups in skel_dump
699 my $ibx = $ctx->{-inbox};
700 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
701 walk_thread(thread_results($ctx, $msgs), $ctx, *skel_dump);
703 $ctx->{parent_msg} = $parent;
706 sub _parent_headers {
707 my ($hdr, $srch) = @_;
710 my $refs = references($hdr);
711 my $irt = pop @$refs;
713 my $v = PublicInbox::Hval->new_msgid($irt);
714 my $html = $v->as_html;
715 my $href = $v->{href};
716 $rv .= "In-Reply-To: <";
717 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
720 # do not display References: if search is present,
721 # we show the thread skeleton at the bottom, instead.
725 @$refs = map { linkify_ref_nosrch($_) } @$refs;
726 $rv .= 'References: '. join("\n\t", @$refs) . "\n";
732 my ($hdr, $standalone, $ctx, $rhref) = @_;
734 my $srch = $ctx->{srch} if $ctx;
737 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
741 thread_skel(\$idx, $ctx, $hdr, $tpfx);
746 if (my $n = $ctx->{next_msg}) {
747 $n = PublicInbox::Hval->new_msgid($n)->{href};
748 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
751 my $par = $ctx->{parent_msg};
753 $u = PublicInbox::Hval->new_msgid($par)->{href};
756 if (my $p = $ctx->{prev_msg}) {
757 $prev = PublicInbox::Hval->new_msgid($p)->{href};
758 if ($p && $par && $p eq $par) {
759 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
760 'rel=prev>prev parent</a>';
763 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
765 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
767 } elsif ($u) { # unlikely
768 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
770 $irt = "$next $prev$parent ";
775 $irt .= qq(<a\nhref="$rhref">reply</a>);
779 sub linkify_ref_nosrch {
780 my $v = PublicInbox::Hval->new_msgid($_[0]);
781 my $html = $v->as_html;
782 my $href = $v->{href};
783 "<<a\nhref=\"../$href/\">$html</a>>";
788 'm' . id_compress($msgid, 1);
792 my ($upfx, $mid) = @_;
794 $mid = PublicInbox::Hval->new_msgid($mid);
795 my $href = $mid->{href};
796 my $html = $mid->as_html;
797 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
802 $level ? INDENT x ($level - 1) : '';
806 my ($ctx, $level, $node, $idx) = @_;
807 ++$ctx->{root_idx} if $level == 0;
808 if ($node->{id} eq $ctx->{mid}) {
809 $ctx->{found_mid_at} = $ctx->{root_idx};
815 sub strict_loose_note ($) {
818 " -- strict thread matches above, loose matches on Subject: below --\n";
820 if ($nr > PublicInbox::Over::DEFAULT_LIMIT()) {
822 " -- use mbox.gz link to download all $nr messages --\n";
828 my ($ctx, $msgs) = @_;
829 require PublicInbox::SearchThread;
830 my $ibx = $ctx->{-inbox};
831 my $rootset = PublicInbox::SearchThread::thread($msgs, *sort_ds, $ibx);
833 # FIXME: `tid' is broken on --reindex, so that needs to be fixed
834 # and preserved in the future. This bug is hidden by `sid' matches
835 # in get_thread, so we never noticed it until now. And even when
836 # reindexing is fixed, we'll keep this code until a SCHEMA_VERSION
837 # bump since reindexing is expensive and users may not do it
839 # loose threading could've returned too many results,
840 # put the root the message we care about at the top:
841 my $mid = $ctx->{mid};
842 if (defined($mid) && scalar(@$rootset) > 1) {
843 $ctx->{root_idx} = -1;
844 my $nr = scalar @$msgs;
845 walk_thread($rootset, $ctx, *find_mid_root);
846 my $idx = $ctx->{found_mid_at};
847 if (defined($idx) && $idx != 0) {
848 my $tip = splice(@$rootset, $idx, 1);
849 @$rootset = reverse @$rootset;
850 unshift @$rootset, $tip;
851 $ctx->{sl_note} = strict_loose_note($nr);
859 require PublicInbox::ExtMsg;
860 PublicInbox::ExtMsg::ext_msg($ctx);
865 fmt_ts(msg_datestamp($hdr));
868 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
871 my ($prev_subj, $subj, $val) = @_;
873 my $omit = ''; # '"' denotes identical text omitted
874 my (@prev_pop, @curr_pop);
875 while (@$prev_subj && @$subj && $subj->[-1] eq $prev_subj->[-1]) {
876 push(@prev_pop, pop(@$prev_subj));
877 push(@curr_pop, pop(@$subj));
880 pop @$subj if @$subj && $subj->[-1] =~ /^re:\s*/i;
881 if (scalar(@curr_pop) == 1) {
883 push @$prev_subj, @prev_pop;
884 push @$subj, @curr_pop;
890 my ($ctx, $level, $node) = @_;
891 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
893 my $dst = $ctx->{dst};
894 my $cur = $ctx->{cur};
895 my $mid = $smsg->{mid};
897 if ($level == 0 && $ctx->{skel_dump_roots}++) {
898 $$dst .= delete $ctx->{sl_note} || '';
901 my $f = ascii_html($smsg->from_name);
902 my $obfs_ibx = $ctx->{-obfs_ibx};
903 obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
905 my $d = fmt_ts($smsg->{ds}) . ' ' . indent_for($level) . th_pfx($level);
907 $ctx->{first_level} ||= $level;
909 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
910 $ctx->{prev_attr} = $attr;
912 $ctx->{prev_level} = $level;
917 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
918 "$attr [this message]</a></b>\n";
921 $ctx->{prev_msg} = $mid;
924 $ctx->{next_msg} ||= $mid;
927 # Subject is never undef, this mail was loaded from
928 # our Xapian which would've resulted in '' if it were
929 # really missing (and Filter rejects empty subjects)
930 my @subj = split(/ /, $ctx->{srch}->subject_normalized($smsg->subject));
932 # remove common suffixes from the subject if it matches the previous,
933 # so we do not show redundant text at the end.
934 my $prev_subj = $ctx->{prev_subj} || [];
935 $ctx->{prev_subj} = [ @subj ];
936 my $omit = dedupe_subject($prev_subj, \@subj, '" ');
939 my $subj = join(' ', @subj);
940 $subj = ascii_html($subj);
941 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
942 $end = "$subj</a> $omit$f\n"
948 my $mapping = $ctx->{mapping};
950 my $map = $mapping->{$mid};
951 $id = id_compress($mid, 1);
953 $map->[0] = "$d<a\nhref=\"$m\">$end";
956 $m = $ctx->{-upfx}.mid_escape($mid).'/';
958 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
963 my ($ctx, $level, $node) = @_;
965 my $mid = $node->{id};
966 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
968 $d .= indent_for($level) . th_pfx($level);
969 my $upfx = $ctx->{-upfx};
970 my $m = PublicInbox::Hval->new_msgid($mid);
971 my $href = $upfx . $m->{href} . '/';
972 my $html = $m->as_html;
974 my $mapping = $ctx->{mapping};
975 my $map = $mapping->{$mid} if $mapping;
977 my $id = id_compress($mid, 1);
978 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
979 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
981 $d .= qq{<<a\nhref="$href">$html</a>>\n};
983 my $dst = $ctx->{dst};
990 (eval { $a->topmost->{smsg}->ds } || 0) <=>
991 (eval { $b->topmost->{smsg}->ds } || 0)
995 # accumulate recent topics if search is supported
996 # returns 200 if done, 404 if not
998 my ($ctx, $level, $node) = @_;
999 my $srch = $ctx->{srch};
1000 my $mid = $node->{id};
1001 my $x = $node->{smsg} || $ctx->{-inbox}->smsg_by_mid($mid);
1005 $subj = $x->subject;
1006 $subj = $srch->subject_normalized($subj);
1009 $topic = [ $ds, 1, { $subj => $mid }, $subj ];
1010 $ctx->{-cur_topic} = $topic;
1011 push @{$ctx->{order}}, $topic;
1015 $topic = $ctx->{-cur_topic}; # should never be undef
1016 $topic->[0] = $ds if $ds > $topic->[0];
1018 my $seen = $topic->[2];
1019 if (scalar(@$topic) == 3) { # parent was a ghost
1020 push @$topic, $subj;
1021 } elsif (!$seen->{$subj}) {
1022 push @$topic, $level, $subj;
1024 $seen->{$subj} = $mid; # latest for subject
1025 } else { # ghost message
1026 return 1 if $level != 0; # ignore child ghosts
1027 $topic = [ -666, 0, {} ];
1028 $ctx->{-cur_topic} = $topic;
1029 push @{$ctx->{order}}, $topic;
1036 my $order = delete $ctx->{order}; # [ ds, subj1, subj2, subj3, ... ]
1038 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
1043 my $ibx = $ctx->{-inbox};
1044 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
1045 my $srch = $ctx->{srch};
1047 # sort by recency, this allows new posts to "bump" old topics...
1048 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
1049 my ($ds, $n, $seen, $top, @ex) = @$topic;
1051 next unless defined $top; # ghost topic
1052 my $mid = delete $seen->{$top};
1053 my $href = mid_escape($mid);
1054 my $prev_subj = [ split(/ /, $top) ];
1055 $top = PublicInbox::Hval->new($top)->as_html;
1058 # $n isn't the total number of posts on the topic,
1059 # just the number of posts in the current results window
1063 $anchor = '#u'; # top of only message
1065 $n = " ($n+ messages)";
1066 $anchor = '#t'; # thread skeleton
1069 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
1070 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
1071 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
1072 " $ds UTC $n - $mbox / $atom\n";
1073 for (my $i = 0; $i < scalar(@ex); $i += 2) {
1074 my $level = $ex[$i];
1075 my $subj = $ex[$i + 1];
1076 $mid = delete $seen->{$subj};
1077 my @subj = split(/ /, $srch->subject_normalized($subj));
1078 my @next_prev = @subj; # full copy
1079 my $omit = dedupe_subject($prev_subj, \@subj, ' "');
1080 $prev_subj = \@next_prev;
1081 $subj = ascii_html(join(' ', @subj));
1082 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
1083 $href = mid_escape($mid);
1084 $s .= indent_for($level) . TCHILD;
1085 $s .= "<a\nhref=\"$href/T/#u\">$subj</a>$omit\n";
1089 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
1095 POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts));
1099 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]);
1100 timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
1103 sub pagination_footer ($$) {
1104 my ($ctx, $latest) = @_;
1105 delete $ctx->{qp} or return;
1106 my $next = $ctx->{next_page} || '';
1107 my $prev = $ctx->{prev_page} || '';
1109 $next = $next ? "$next " : ' ';
1110 $prev .= qq! <a\nhref='$latest'>latest</a>!;
1112 "<hr><pre>page: $next$prev</pre>";
1115 sub index_nav { # callback for WwwStream
1116 my (undef, $ctx) = @_;
1117 pagination_footer($ctx, '.')
1120 sub paginate_recent ($$) {
1121 my ($ctx, $lim) = @_;
1122 my $t = $ctx->{qp}->{t} || '';
1123 my $opts = { limit => $lim };
1124 my ($after, $before);
1126 # Xapian uses '..' but '-' is perhaps friendier to URL linkifiers
1127 # if only $after exists "YYYYMMDD.." because "." could be skipped
1128 # if interpreted as an end-of-sentence
1129 $t =~ s/\A(\d{8,14})-// and $after = str2ts($1);
1130 $t =~ /\A(\d{8,14})\z/ and $before = str2ts($1);
1132 my $ibx = $ctx->{-inbox};
1133 my $msgs = $ibx->recent($opts, $after, $before);
1134 my $nr = scalar @$msgs;
1135 if ($nr < $lim && defined($after)) {
1136 $after = $before = undef;
1137 $msgs = $ibx->recent($opts);
1138 $nr = scalar @$msgs;
1140 my $more = $nr == $lim;
1141 my ($newest, $oldest);
1143 $newest = $msgs->[0]->{ts};
1144 $oldest = $msgs->[-1]->{ts};
1145 # if we only had $after, our SQL query in ->recent ordered
1146 if ($newest < $oldest) {
1147 ($oldest, $newest) = ($newest, $oldest);
1148 $more = 0 if defined($after) && $after < $oldest;
1151 if (defined($oldest) && $more) {
1152 my $s = ts2str($oldest);
1153 $ctx->{next_page} = qq!<a\nhref="?t=$s"\nrel=next>next</a>!;
1155 if (defined($newest) && (defined($before) || defined($after))) {
1156 my $s = ts2str($newest);
1157 $ctx->{prev_page} = qq!<a\nhref="?t=$s-"\nrel=prev>prev</a>!;
1164 my $msgs = paginate_recent($ctx, 200); # 200 is our window
1166 walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic);
1168 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
1171 sub thread_adj_level {
1172 my ($ctx, $level) = @_;
1174 my $max = $ctx->{cur_level};
1176 return ('', '') if $max == 0; # flat output
1178 # reset existing lists
1179 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
1180 $ctx->{cur_level} = 0;
1182 } elsif ($level == $max) { # continue existing list
1184 } elsif ($level < $max) {
1185 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1186 $ctx->{cur_level} = $level;
1187 ("$beg<li>", '</li>');
1188 } else { # ($level > $max) # start a new level
1189 $ctx->{cur_level} = $level;
1190 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1195 sub ghost_index_entry {
1196 my ($ctx, $level, $node) = @_;
1197 my ($beg, $end) = thread_adj_level($ctx, $level);
1198 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})