1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (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 URI::Escape qw/uri_escape_utf8/;
10 use Date::Parse qw/str2time/;
11 use Encode::MIME::Header;
13 use PublicInbox::Hval qw/ascii_html/;
14 use PublicInbox::Linkify;
15 use PublicInbox::MID qw/mid_clean id_compress mid_mime/;
16 use PublicInbox::MsgIter;
17 use PublicInbox::Address;
18 use PublicInbox::WwwStream;
21 use constant INDENT => ' ';
22 use constant TCHILD => '` ';
23 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
25 # public functions: (unstable)
27 my ($ctx, $mime, $footer) = @_;
28 my $hdr = $mime->header_obj;
29 my $tip = _msg_html_prepare($hdr, $ctx);
30 PublicInbox::WwwStream->response($ctx, 200, sub {
33 $tip . multipart_text_as_html($mime, '') .
36 # fake an EOF if generating the footer fails;
37 # we want to at least show the message if something
40 '<pre>' . html_footer($hdr, 1, $ctx) .
41 '</pre>' . msg_reply($ctx, $hdr)
49 # /$INBOX/$MESSAGE_ID/#R
53 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
55 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
57 my ($arg, $link) = mailto_arg_link($hdr);
58 push @$arg, '/path/to/YOUR_REPLY';
59 $arg = join(" \\\n ", '', @$arg);
62 id=R>You may reply publically to <a
63 href=#t>this message</a> via
64 plain-text email using any one of the following methods:
66 * Save the following mbox file, import it into your mail client,
67 and reply-to-all from there: <a
70 Avoid top-posting and favor interleaved quoting:
72 href="$p_url">$p_url</a>
74 * Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,
75 and <b>--in-reply-to</b> switches of git-send-email(1):
80 href="$se_url">$se_url</a>
82 * If your mail client supports setting the <b>In-Reply-To</b> header
83 via mailto: links, try the <a
84 href="$link">mailto: link</a></pre>
90 my $irt = $hdr->header_raw('In-Reply-To');
92 return mid_clean($irt) if (defined $irt);
94 my $refs = $hdr->header_raw('References');
95 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
101 sub _hdr_names ($$) {
102 my ($hdr, $field) = @_;
103 my $val = $hdr->header($field) or return '';
104 ascii_html(join(', ', PublicInbox::Address::names($val)));
108 my ($nr, $singular, $plural) = @_;
109 return "0 $plural" if $nr == 0;
110 $nr == 1 ? "$nr $singular" : "$nr $plural";
113 # this is already inside a <pre>
115 my ($mime, $ctx, $more) = @_;
116 my $srch = $ctx->{srch};
117 my $hdr = $mime->header_obj;
118 my $subj = $hdr->header('Subject');
120 my $mid_raw = mid_clean(mid_mime($mime));
121 my $id = id_compress($mid_raw, 1);
123 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
125 my $root_anchor = $ctx->{root_anchor} || '';
126 my $irt = in_reply_to($hdr);
128 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
129 $subj = '<b>'.ascii_html($subj).'</b>';
130 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
132 $rv .= _th_index_lite($mid_raw, $irt, $id, $ctx);
134 foreach my $f (qw(To Cc)) {
135 my $dst = _hdr_names($hdr, $f);
136 push @tocc, "$f: $dst" if $dst ne '';
138 $rv .= "From: "._hdr_names($hdr, 'From').' @ '._msg_date($hdr)." UTC";
139 my $upfx = $ctx->{-upfx};
140 my $mhref = $upfx . $mid->as_href . '/';
141 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
142 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
143 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
145 my $mapping = $ctx->{mapping};
146 if (!$mapping && $irt) {
147 my $mirt = PublicInbox::Hval->new_msgid($irt);
148 my $href = $upfx . $mirt->as_href . '/';
149 my $html = $mirt->as_html;
150 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
154 # scan through all parts, looking for displayable text
155 msg_iter($mime, sub { $rv .= add_text_body($mhref, $_[0]) });
158 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
159 "<a\nhref=\"$mhref\">permalink</a>" .
160 " / <a\nhref=\"${mhref}raw\">raw</a>" .
161 " / <a\nhref=\"${mhref}#R\">reply</a>";
162 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
163 $rv .= " [relevance $pct->{$mid_raw}%]";
165 my $threaded = 'threaded';
169 $flat = "<b>$flat</b>";
171 $threaded = "<b>$threaded</b>";
173 $rv .= " / [<a\nhref=\"${mhref}T/#u\">$flat</a>";
174 $rv .= "|<a\nhref=\"${mhref}t/#u\">$threaded</a>]";
175 $rv .= " / <a\nhref=#r$id>$ctx->{s_nr}</a>";
178 $rv .= $more ? "\n\n" : "\n";
181 sub pad_link ($$;$) {
182 my ($mid, $level, $s) = @_;
184 my $id = id_compress($mid, 1);
185 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
189 my ($mid_raw, $irt, $id, $ctx) = @_;
191 my $mapping = $ctx->{mapping} or return $rv;
193 # map = [children, attr, node, idx, level]
194 my $map = $mapping->{$mid_raw};
195 my $nr_c = scalar @{$map->[0]};
197 my $level = $map->[4];
200 my $irt_map = $mapping->{$irt};
201 my $siblings = $irt_map->[0];
202 $nr_s = scalar(@$siblings) - 1;
203 $rv .= $pad . $irt_map->[1];
205 my $prev = $siblings->[$idx - 1];
206 my $pmid = $prev->messageid;
208 my $s = ($idx - 1). ' preceding siblings ...';
209 $rv .= pad_link($pmid, $level, $s);
210 } elsif ($idx == 2) {
211 my $ppmid = $siblings->[0]->messageid;
212 $rv .= $pad . $mapping->{$ppmid}->[1];
214 $rv .= $pad . $mapping->{$pmid}->[1];
217 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
218 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
219 my $this = $map->[1];
220 $this =~ s!\n\z!</b>\n!s;
221 $this =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
222 $this =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
224 my $node = $map->[2];
225 if (my $child = $node->child) {
226 my $cmid = $child->messageid;
227 $rv .= $pad . $mapping->{$cmid}->[1];
229 my $s = ($nr_c - 1). ' more replies';
230 $rv .= pad_link($cmid, $level + 1, $s);
231 } elsif (my $cn = $child->next) {
232 $rv .= $pad . $mapping->{$cn->messageid}->[1];
235 if (my $next = $node->next) {
236 my $nmid = $next->messageid;
237 $rv .= $pad . $mapping->{$nmid}->[1];
238 my $nnext = $nr_s - $idx;
240 my $s = ($nnext - 1).' subsequent siblings';
241 $rv .= pad_link($nmid, $level, $s);
242 } elsif (my $nn = $next->next) {
243 $rv .= $pad . $mapping->{$nn->messageid}->[1];
246 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
250 my ($th, $ctx, $cb) = @_;
251 my @q = map { (0, $_) } $th->rootset;
253 my $level = shift @q;
254 my $node = shift @q or next;
255 $cb->($ctx, $level, $node);
256 unshift @q, $level+1, $node->child, $level, $node->next;
261 my ($ctx, $level, $node) = @_;
262 my $mapping = $ctx->{mapping};
264 if (my $parent = $node->parent) {
265 my $m = $mapping->{$parent->messageid}->[0];
269 $mapping->{$node->messageid} = [ [], '', $node, $idx, $level ];
270 skel_dump($ctx, $level, $node);
273 sub thread_index_entry {
274 my ($ctx, $level, $mime) = @_;
275 my ($beg, $end) = thread_adj_level($ctx, $level);
276 $beg . '<pre>' . index_entry($mime, $ctx, 0) . '</pre>' . $end;
279 sub stream_thread ($$) {
281 my $inbox = $ctx->{-inbox};
283 my @q = map { (0, $_) } $th->rootset;
287 my $node = shift @q or next;
288 unshift @q, $level+1, $node->child, $level, $node->next;
289 $mime = $inbox->msg_by_mid($node->messageid) and last;
291 return missing_thread($ctx) unless $mime;
293 $mime = Email::MIME->new($mime);
294 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
295 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
296 PublicInbox::WwwStream->response($ctx, 200, sub {
300 my $node = shift @q or next;
301 unshift @q, $level+1, $node->child, $level, $node->next;
302 my $mid = $node->messageid;
303 if ($mime = $inbox->msg_by_mid($mid)) {
304 $mime = Email::MIME->new($mime);
305 return thread_index_entry($ctx, $level, $mime);
307 return ghost_index_entry($ctx, $level, $mid);
310 my $ret = join('', thread_adj_level($ctx, 0));
311 $ret .= ${$ctx->{dst}}; # skel
319 my $mid = $ctx->{mid};
320 my $sres = $ctx->{srch}->get_thread($mid, { asc => 1 });
321 my $msgs = load_results($sres);
322 my $nr = $sres->{total};
323 return missing_thread($ctx) if $nr == 0;
324 my $skel = '<hr /><pre>';
325 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
326 $skel .= ", back to <a\nhref=\"../../\">index</a>";
327 $skel .= "\n<a\nid=t>$nr+ messages in thread:</a> (download: ";
328 $skel .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
329 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
330 $ctx->{-upfx} = '../../';
331 $ctx->{cur_level} = 0;
332 $ctx->{dst} = \$skel;
333 $ctx->{prev_attr} = '';
334 $ctx->{prev_level} = 0;
335 $ctx->{root_anchor} = anchor_for($mid);
337 $ctx->{mapping} = {};
338 $ctx->{s_nr} = "$nr+ messages in thread";
340 my $th = thread_results($msgs);
341 walk_thread($th, $ctx, *pre_thread);
343 return stream_thread($th, $ctx) unless $ctx->{flat};
345 # flat display: lazy load the full message from mini_mime:
346 my $inbox = $ctx->{-inbox};
348 while ($mime = shift @$msgs) {
349 $mime = $inbox->msg_by_mid(mid_clean(mid_mime($mime))) and last;
351 return missing_thread($ctx) unless $mime;
352 $mime = Email::MIME->new($mime);
353 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
354 $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
356 PublicInbox::WwwStream->response($ctx, 200, sub {
358 while ($mime = shift @$msgs) {
359 $mid = mid_clean(mid_mime($mime));
360 $mime = $inbox->msg_by_mid($mid) and last;
363 $mime = Email::MIME->new($mime);
364 return index_entry($mime, $ctx, scalar @$msgs);
371 sub multipart_text_as_html {
372 my ($mime, $upfx) = @_;
375 # scan through all parts, looking for displayable text
376 msg_iter($mime, sub {
378 $rv .= add_text_body($upfx, $p);
384 my ($s, $l, $quot) = @_;
386 # show everything in the full version with anchor from
387 # short version (see above)
388 my $rv = $l->linkify_1(join('', @$quot));
391 # we use a <span> here to allow users to specify their own
392 # color for quoted text
393 $rv = $l->linkify_2(ascii_html($rv));
394 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
397 sub attach_link ($$$$) {
398 my ($upfx, $ct, $p, $fn) = @_;
399 my ($part, $depth, @idx) = @$p;
400 my $nl = $idx[-1] > 1 ? "\n" : '';
401 my $idx = join('.', @idx);
402 my $size = bytes::length($part->body);
403 $ct ||= 'text/plain';
404 $ct =~ s/;.*//; # no attributes
405 $ct = ascii_html($ct);
406 my $desc = $part->header('Content-Description');
407 $desc = $fn unless defined $desc;
408 $desc = '' unless defined $desc;
410 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
412 } elsif ($ct eq 'text/plain') {
417 my @ret = qq($nl<a\nhref="$upfx$idx-$sfn">[-- Attachment #$idx: );
418 my $ts = "Type: $ct, Size: $size bytes";
419 push(@ret, ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]");
420 join('', @ret, "</a>\n");
424 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
425 my ($part, $depth, @idx) = @$p;
426 my $ct = $part->content_type;
427 my $fn = $part->filename;
429 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
430 return attach_link($upfx, $ct, $p, $fn);
433 my $s = eval { $part->body_str };
435 # badly-encoded message? tell the world about it!
436 return attach_link($upfx, $ct, $p, $fn) if $@;
438 my @lines = split(/^/m, $s);
440 if (defined($fn) || $depth > 0) {
441 $s .= attach_link($upfx, $ct, $p, $fn);
445 my $l = PublicInbox::Linkify->new;
446 while (defined(my $cur = shift @lines)) {
448 # show the previously buffered quote inline
449 flush_quote(\$s, $l, \@quot) if @quot;
452 $cur = $l->linkify_1($cur);
453 $cur = ascii_html($cur);
454 $s .= $l->linkify_2($cur);
463 flush_quote(\$s, $l, \@quot);
465 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
466 $s =~ s/\A\n+//s; # kill leading blank lines
467 $s =~ s/\s+\z//s; # kill all trailing spaces
471 sub _msg_html_prepare {
472 my ($hdr, $ctx) = @_;
473 my $srch = $ctx->{srch} if $ctx;
475 my $rv = "<pre\nid=b>"; # anchor for body start
478 $ctx->{-upfx} = '../';
481 my $mid = $hdr->header_raw('Message-ID');
482 $mid = PublicInbox::Hval->new_msgid($mid);
483 foreach my $h (qw(From To Cc Subject Date)) {
484 my $v = $hdr->header($h);
485 defined($v) && ($v ne '') or next;
486 $v = PublicInbox::Hval->new($v);
489 my @n = PublicInbox::Address::names($v->raw);
490 $title[1] = ascii_html(join(', ', @n));
491 } elsif ($h eq 'Subject') {
492 $title[0] = $v->as_html;
494 $rv .= qq($h: <a\nhref="#r"\nid=t>);
495 $rv .= $v->as_html . "</a>\n";
500 $v =~ s/(\@[^,]+,) /$1\n\t/g if ($h eq 'Cc' || $h eq 'To');
504 $title[0] ||= '(no subject)';
505 $ctx->{-title_html} = join(' - ', @title);
506 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
507 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
508 $rv .= _parent_headers($hdr, $srch);
513 my ($dst, $ctx, $hdr, $tpfx) = @_;
514 my $srch = $ctx->{srch};
515 my $mid = mid_clean($hdr->header_raw('Message-ID'));
516 my $sres = $srch->get_thread($mid);
517 my $nr = $sres->{total};
518 my $expand = qq(<a\nhref="${tpfx}T/#u">expand</a> ) .
519 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
520 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
522 my $parent = in_reply_to($hdr);
524 if (defined $parent) {
525 $$dst .= "($expand)\n ";
526 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
528 $$dst .= "[no followups, yet] ($expand)\n";
530 $ctx->{next_msg} = undef;
531 $ctx->{parent_msg} = $parent;
535 $$dst .= "$nr+ messages in thread ($expand";
536 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
538 my $subj = $srch->subject_path($hdr->header('Subject'));
539 $ctx->{seen} = { $subj => 1 };
541 $ctx->{prev_attr} = '';
542 $ctx->{prev_level} = 0;
544 walk_thread(thread_results(load_results($sres)), $ctx, *skel_dump);
545 $ctx->{parent_msg} = $parent;
548 sub _parent_headers {
549 my ($hdr, $srch) = @_;
552 my $irt = in_reply_to($hdr);
554 my $v = PublicInbox::Hval->new_msgid($irt);
555 my $html = $v->as_html;
556 my $href = $v->as_href;
557 $rv .= "In-Reply-To: <";
558 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
561 # do not display References: if search is present,
562 # we show the thread skeleton at the bottom, instead.
565 my $refs = $hdr->header_raw('References');
567 # avoid redundant URLs wasting bandwidth
569 $seen{$irt} = 1 if defined $irt;
571 my @raw_refs = ($refs =~ /<([^>]+)>/g);
572 foreach my $ref (@raw_refs) {
575 push @refs, linkify_ref_nosrch($ref);
579 $rv .= 'References: '. join("\n\t", @refs) . "\n";
585 sub squote_maybe ($) {
587 if ($val =~ m{([^\w@\./,\%\+\-])}) {
588 $val =~ s/(['!])/'\\$1'/g; # '!' for csh
594 sub mailto_arg_link {
596 my %cc; # everyone else
597 my $to; # this is the From address
599 foreach my $h (qw(From To Cc)) {
600 my $v = $hdr->header($h);
601 defined($v) && ($v ne '') or next;
602 my @addrs = PublicInbox::Address::emails($v);
603 foreach my $address (@addrs) {
604 my $dst = lc($address);
605 $cc{$dst} ||= $address;
611 my $subj = $hdr->header('Subject') || '';
612 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
613 my $mid = $hdr->header_raw('Message-ID');
614 push @arg, '--in-reply-to='.ascii_html(squote_maybe(mid_clean($mid)));
615 my $irt = uri_escape_utf8($mid);
617 push @arg, '--to=' . ascii_html($to);
618 $to = uri_escape_utf8($to);
619 $subj = uri_escape_utf8($subj);
620 my $cc = join(',', sort values %cc);
621 push @arg, '--cc=' . ascii_html($cc);
622 $cc = uri_escape_utf8($cc);
623 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
626 (\@arg, ascii_html($href));
630 my ($hdr, $standalone, $ctx, $rhref) = @_;
632 my $srch = $ctx->{srch} if $ctx;
635 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
639 thread_skel(\$idx, $ctx, $hdr, $tpfx);
640 my $p = $ctx->{parent_msg};
641 my $next = $ctx->{next_msg};
643 $p = PublicInbox::Hval->new_msgid($p);
645 $irt = "<a\nhref=\"$upfx$p/\"\nrel=prev>parent</a> ";
647 $irt = ' ' x length('parent ');
650 my $n = PublicInbox::Hval->new_msgid($next)->as_href;
651 $irt .= "<a\nhref=\"$upfx$n/\"\nrel=next>next</a> ";
653 $irt .= ' ' x length('next ');
659 $irt .= qq(<a\nhref="$rhref">reply</a>);
663 sub linkify_ref_nosrch {
664 my $v = PublicInbox::Hval->new_msgid($_[0]);
665 my $html = $v->as_html;
666 my $href = $v->as_href;
667 "<<a\nhref=\"../$href/\">$html</a>>";
672 'm' . id_compress($msgid, 1);
676 my ($upfx, $mid) = @_;
677 # 'subject dummy' is used internally by Mail::Thread
678 return '[no common parent]' if ($mid eq 'subject dummy');
680 $mid = PublicInbox::Hval->new_msgid($mid);
681 my $href = $mid->as_href;
682 my $html = $mid->as_html;
683 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
688 INDENT x ($level - 1);
694 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
699 my $ts = eval { str2time($hdr->header('Date')) };
700 defined($ts) ? $ts : 0;
705 require PublicInbox::Thread;
706 my $th = PublicInbox::Thread->new(@$msgs);
708 $th->order(*sort_ts);
714 require PublicInbox::ExtMsg;
715 PublicInbox::ExtMsg::ext_msg($ctx);
720 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
724 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
727 my ($ctx, $hdr, $level) = @_;
729 my $dst = $ctx->{dst};
730 my $cur = $ctx->{cur};
731 my $mid = mid_clean($hdr->header_raw('Message-ID'));
732 my $f = ascii_html($hdr->header('X-PI-From'));
733 my $d = _msg_date($hdr) . ' ' . indent_for($level) . th_pfx($level);
735 $ctx->{first_level} ||= $level;
737 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
738 $ctx->{prev_attr} = $attr;
742 $ctx->{prev_level} = $level;
748 $$dst .= "<b><a\nid=r\nhref=\"#t\">".
749 "$attr [this message]</a></b>\n";
753 $ctx->{next_msg} ||= $mid;
756 # Subject is never undef, this mail was loaded from
757 # our Xapian which would've resulted in '' if it were
758 # really missing (and Filter rejects empty subjects)
759 my $s = $hdr->header('Subject');
760 my $h = $ctx->{srch}->subject_path($s);
761 if ($ctx->{seen}->{$h}) {
764 $ctx->{seen}->{$h} = 1;
765 $s = PublicInbox::Hval->new($s);
768 my $m = PublicInbox::Hval->new_msgid($mid);
770 my $mapping = $ctx->{mapping};
771 my $end = defined($s) ? "$s</a> $f\n" : "$f</a>\n";
773 my $map = $mapping->{$mid};
774 $id = id_compress($mid, 1);
776 $map->[1] = "$d<a\nhref=\"$m\">$end";
779 $m = $ctx->{-upfx}.$m->as_href.'/';
781 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
785 my ($ctx, $level, $node) = @_;
786 if (my $mime = $node->message) {
787 _skel_header($ctx, $mime->header_obj, $level);
789 my $mid = $node->messageid;
790 my $dst = $ctx->{dst};
791 my $mapping = $ctx->{mapping};
792 my $map = $mapping->{$mid} if $mapping;
793 if ($mid eq 'subject dummy') {
794 my $ncp = "\t[no common parent]\n";
795 $map->[1] = $ncp if $map;
799 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
801 $d .= indent_for($level) . th_pfx($level);
802 my $upfx = $ctx->{-upfx};
803 my $m = PublicInbox::Hval->new_msgid($mid);
804 my $href = $upfx . $m->as_href . '/';
805 my $html = $m->as_html;
808 my $id = id_compress($mid, 1);
809 $map->[1] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
810 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
812 $d .= qq{<<a\nhref="$href">$html</a>>\n};
820 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
821 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
825 sub _tryload_ghost ($$) {
826 my ($srch, $mid) = @_;
827 my $smsg = $srch->lookup_mail($mid) or return;
831 # accumulate recent topics if search is supported
832 # returns 1 if done, undef if not
834 my ($ctx, $level, $node) = @_;
835 my $srch = $ctx->{srch};
836 my $mid = $node->messageid;
837 my $x = $node->message || _tryload_ghost($srch, $mid);
841 $subj = $x->header('Subject');
842 $subj = $srch->subject_normalized($subj);
843 $ts = $x->header('X-PI-TS');
844 } else { # ghost message, do not bump level
848 if (++$ctx->{subjs}->{$subj} == 1) {
849 push @{$ctx->{order}}, [ $level, $subj ];
851 my $exist = $ctx->{latest}->{$subj};
852 if (!$exist || $exist->[1] < $ts) {
853 $ctx->{latest}->{$subj} = [ $mid, $ts ];
859 my $order = $ctx->{order};
860 my $subjs = $ctx->{subjs};
861 my $latest = $ctx->{latest};
863 return $fh->write("\n[No topics in range]</pre>") unless scalar @$order;
869 while (defined(my $info = shift @$order)) {
870 my ($level, $subj) = @$info;
871 my $n = delete $subjs->{$subj};
872 my ($mid, $ts) = @{delete $latest->{$subj}};
873 my $href = PublicInbox::Hval->new_msgid($mid)->as_href;
874 $pfx = indent_for($level);
875 my $nl = $level == $prev ? "\n" : '';
880 $cur ||= [ $ts, '' ];
881 $cur->[0] = $ts if $ts > $cur->[0];
882 $cur->[1] .= $nl . $pfx . th_pfx($level);
883 if ($ts == -666) { # ghost
884 $cur->[1] .= ghost_parent('', $mid) . "\n";
885 next; # child will have mbox / atom link
888 $subj = PublicInbox::Hval->new($subj)->as_html;
889 $cur->[1] .= "<a\nhref=\"$href/T/#u\"><b>$subj</b></a>\n";
891 my $attr = " $ts UTC";
893 # $n isn't the total number of posts on the topic,
894 # just the number of posts in the current results window
895 $n = $n == 1 ? '' : " ($n+ messages)";
897 if ($level == 0 || $attr ne $prev_attr) {
898 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
899 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
900 $pfx .= INDENT if $level > 0;
901 $cur->[1] .= $pfx . $attr . $n . " - $mbox / $atom\n";
905 push @recent, $cur if $cur;
906 @recent = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @recent;
907 $fh->write(join('', @recent) . '</pre>');
910 sub emit_index_topics {
912 my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/);
917 my %opts = ( offset => $off, limit => $max * 4 );
918 while (scalar @{$ctx->{order}} < $max) {
919 my $sres = $ctx->{srch}->query('', \%opts);
920 my $nr = scalar @{$sres->{msgs}} or last;
921 $sres = load_results($sres);
922 walk_thread(thread_results($sres), $ctx, *add_topic);
923 $opts{offset} += $nr;
930 sub thread_adj_level {
931 my ($ctx, $level) = @_;
933 my $max = $ctx->{cur_level};
935 return ('', '') if $max == 0; # flat output
937 # reset existing lists
938 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
939 $ctx->{cur_level} = 0;
941 } elsif ($level == $max) { # continue existing list
943 } elsif ($level < $max) {
944 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
945 $ctx->{cur_level} = $level;
946 ("$beg<li>", '</li>');
947 } else { # ($level > $max) # start a new level
948 $ctx->{cur_level} = $level;
949 my $beg = ($max ? '<li>' : '') . '<ul><li>';
954 sub ghost_index_entry {
955 my ($ctx, $level, $mid) = @_;
956 my ($beg, $end) = thread_adj_level($ctx, $level);
957 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $mid) . '</pre>' . $end;