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 my ($arg, $link) = mailto_arg_link($hdr);
56 push @$arg, '/path/to/YOUR_REPLY';
57 $arg = join(" \\\n ", '', @$arg);
60 id=R>You may reply publically to <a
61 href=#t>this message</a> via
62 plain-text email using any one of the following methods:
64 * Save the following mbox file, import it into your mail client,
65 and reply-to-all from there: <a
68 * Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,
69 and <b>--in-reply-to</b> switches of git-send-email(1):
74 href="$se_url">$se_url</a>
76 * If your mail client supports setting the <b>In-Reply-To</b> header
77 via mailto: links, try the <a
78 href="$link">mailto: link</a></pre>
84 my $irt = $hdr->header_raw('In-Reply-To');
86 return mid_clean($irt) if (defined $irt);
88 my $refs = $hdr->header_raw('References');
89 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
96 my ($hdr, $field) = @_;
97 my $val = $hdr->header($field) or return '';
98 ascii_html(join(', ', PublicInbox::Address::names($val)));
102 my ($nr, $singular, $plural) = @_;
103 return "0 $plural" if $nr == 0;
104 $nr == 1 ? "$nr $singular" : "$nr $plural";
107 # this is already inside a <pre>
109 my ($mime, $ctx, $more) = @_;
110 my $srch = $ctx->{srch};
111 my $hdr = $mime->header_obj;
112 my $subj = $hdr->header('Subject');
114 my $mid_raw = mid_clean(mid_mime($mime));
115 my $id = id_compress($mid_raw, 1);
117 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
119 my $root_anchor = $ctx->{root_anchor} || '';
120 my $irt = in_reply_to($hdr);
122 my $rv = '<b>'.ascii_html($subj).'</b>';
123 $rv = "<u\nid=u>$rv</u>" if $root_anchor eq $id_m;
125 $rv .= _th_index_lite($mid_raw, $irt, $id, $ctx);
127 foreach my $f (qw(To Cc)) {
128 my $dst = _hdr_names($hdr, $f);
129 push @tocc, "$f: $dst" if $dst ne '';
131 $rv .= "From: "._hdr_names($hdr, 'From').' @ '._msg_date($hdr)." UTC";
132 my $upfx = $ctx->{-upfx};
133 my $mhref = $upfx . $mid->as_href . '/';
134 $rv .= qq{ (<a\nhref="$mhref/">permalink</a> / };
135 $rv .= qq{<a\nhref="$mhref/raw">raw</a>)\n};
136 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
138 my $mapping = $ctx->{mapping};
139 if (!$mapping && $irt) {
140 my $mirt = PublicInbox::Hval->new_msgid($irt);
141 my $href = $upfx . $mirt->as_href . '/';
142 my $html = $mirt->as_html;
143 $rv .= qq(In-Reply-To: <<a\nhref="$href/">$html</a>>\n)
147 # scan through all parts, looking for displayable text
148 msg_iter($mime, sub { $rv .= add_text_body($mhref, $_[0]) });
151 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
152 "<a\nhref=\"$mhref\">permalink</a>" .
153 " / <a\nhref=\"${mhref}raw\">raw</a>" .
154 " / <a\nhref=\"${mhref}#R\">reply</a>";
155 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
156 $rv .= " [relevance $pct->{$mid_raw}%]";
158 my $threaded = 'threaded';
162 $flat = "<b>$flat</b>";
164 $threaded = "<b>$threaded</b>";
166 $rv .= " / [<a\nhref=\"${mhref}T/#u\">$flat</a>";
167 $rv .= "|<a\nhref=\"${mhref}t/#u\">$threaded</a>]";
168 $rv .= " / <a\nhref=#r$id>$ctx->{s_nr}</a>";
171 $rv .= $more ? "\n\n" : "\n";
174 sub pad_link ($$;$) {
175 my ($mid, $level, $s) = @_;
177 my $id = id_compress($mid, 1);
178 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
182 my ($mid_raw, $irt, $id, $ctx) = @_;
184 my $mapping = $ctx->{mapping} or return $rv;
186 # map = [children, attr, node, idx, level]
187 my $map = $mapping->{$mid_raw};
188 my $nr_c = scalar @{$map->[0]};
190 my $level = $map->[4];
193 my $irt_map = $mapping->{$irt};
194 my $siblings = $irt_map->[0];
195 $nr_s = scalar(@$siblings) - 1;
196 $rv .= $pad . $irt_map->[1];
198 my $prev = $siblings->[$idx - 1];
199 my $pmid = $prev->messageid;
201 my $s = ($idx - 1). ' preceding siblings ...';
202 $rv .= pad_link($pmid, $level, $s);
203 } elsif ($idx == 2) {
204 my $ppmid = $siblings->[0]->messageid;
205 $rv .= $pad . $mapping->{$ppmid}->[1];
207 $rv .= $pad . $mapping->{$pmid}->[1];
210 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
211 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
212 my $this = $map->[1];
213 $this =~ s!\n\z!</b>\n!s;
214 $this =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
216 my $node = $map->[2];
217 if (my $child = $node->child) {
218 my $cmid = $child->messageid;
219 $rv .= $pad . $mapping->{$cmid}->[1];
221 my $s = ($nr_c - 1). ' more replies';
222 $rv .= pad_link($cmid, $level + 1, $s);
223 } elsif (my $cn = $child->next) {
224 $rv .= $pad . $mapping->{$cn->messageid}->[1];
227 if (my $next = $node->next) {
228 my $nmid = $next->messageid;
229 $rv .= $pad . $mapping->{$nmid}->[1];
230 my $nnext = $nr_s - $idx;
232 my $s = ($nnext - 1).' subsequent siblings';
233 $rv .= pad_link($nmid, $level, $s);
234 } elsif (my $nn = $next->next) {
235 $rv .= $pad . $mapping->{$nn->messageid}->[1];
238 $rv .= "<a\nhref=#e$id\nid=m$id>_</a> ";
239 $rv .= "<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
243 my ($th, $ctx, $cb) = @_;
244 my @q = map { (0, $_) } $th->rootset;
246 my $level = shift @q;
247 my $node = shift @q or next;
248 $cb->($ctx, $level, $node);
249 unshift @q, $level+1, $node->child, $level, $node->next;
254 my ($ctx, $level, $node) = @_;
255 my $mapping = $ctx->{mapping};
257 if (my $parent = $node->parent) {
258 my $m = $mapping->{$parent->messageid}->[0];
262 $mapping->{$node->messageid} = [ [], '', $node, $idx, $level ];
263 skel_dump($ctx, $level, $node);
266 sub thread_index_entry {
267 my ($ctx, $level, $mime) = @_;
268 my ($beg, $end) = thread_adj_level($ctx, $level);
269 $beg . '<pre>' . index_entry($mime, $ctx, 0) . '</pre>' . $end;
272 sub stream_thread ($$) {
274 my $inbox = $ctx->{-inbox};
276 my @q = map { (0, $_) } $th->rootset;
280 my $node = shift @q or next;
281 unshift @q, $level+1, $node->child, $level, $node->next;
282 $mime = $inbox->msg_by_mid($node->messageid) and last;
284 return missing_thread($ctx) unless $mime;
286 $mime = Email::MIME->new($mime);
287 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
288 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
289 PublicInbox::WwwStream->response($ctx, 200, sub {
293 my $node = shift @q or next;
294 unshift @q, $level+1, $node->child, $level, $node->next;
295 my $mid = $node->messageid;
296 if ($mime = $inbox->msg_by_mid($mid)) {
297 $mime = Email::MIME->new($mime);
298 return thread_index_entry($ctx, $level, $mime);
300 return ghost_index_entry($ctx, $level, $mid);
303 my $ret = join('', thread_adj_level($ctx, 0));
304 $ret .= ${$ctx->{dst}}; # skel
312 my $mid = $ctx->{mid};
313 my $sres = $ctx->{srch}->get_thread($mid, { asc => 1 });
314 my $msgs = load_results($sres);
315 my $nr = $sres->{total};
316 return missing_thread($ctx) if $nr == 0;
317 my $skel = '<hr /><pre>';
318 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
319 $skel .= ", back to <a\nhref=\"../../\">index</a>";
320 $skel .= "\n<a\nid=t>$nr+ messages in thread:</a> (download: ";
321 $skel .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
322 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
323 $ctx->{-upfx} = '../../';
324 $ctx->{cur_level} = 0;
325 $ctx->{dst} = \$skel;
326 $ctx->{prev_attr} = '';
327 $ctx->{prev_level} = 0;
328 $ctx->{root_anchor} = anchor_for($mid);
330 $ctx->{mapping} = {};
331 $ctx->{s_nr} = "$nr+ messages in thread";
333 my $th = thread_results($msgs);
334 walk_thread($th, $ctx, *pre_thread);
336 return stream_thread($th, $ctx) unless $ctx->{flat};
338 # flat display: lazy load the full message from mini_mime:
339 my $inbox = $ctx->{-inbox};
341 while ($mime = shift @$msgs) {
342 $mime = $inbox->msg_by_mid(mid_clean(mid_mime($mime))) and last;
344 return missing_thread($ctx) unless $mime;
345 $mime = Email::MIME->new($mime);
346 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
347 $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
349 PublicInbox::WwwStream->response($ctx, 200, sub {
351 while ($mime = shift @$msgs) {
352 $mid = mid_clean(mid_mime($mime));
353 $mime = $inbox->msg_by_mid($mid) and last;
356 $mime = Email::MIME->new($mime);
357 return index_entry($mime, $ctx, scalar @$msgs);
364 sub multipart_text_as_html {
365 my ($mime, $upfx) = @_;
368 # scan through all parts, looking for displayable text
369 msg_iter($mime, sub {
371 $rv .= add_text_body($upfx, $p);
377 my ($s, $l, $quot) = @_;
379 # show everything in the full version with anchor from
380 # short version (see above)
381 my $rv = $l->linkify_1(join('', @$quot));
384 # we use a <span> here to allow users to specify their own
385 # color for quoted text
386 $rv = $l->linkify_2(ascii_html($rv));
387 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
390 sub attach_link ($$$$) {
391 my ($upfx, $ct, $p, $fn) = @_;
392 my ($part, $depth, @idx) = @$p;
393 my $nl = $idx[-1] > 1 ? "\n" : '';
394 my $idx = join('.', @idx);
395 my $size = bytes::length($part->body);
396 $ct ||= 'text/plain';
397 $ct =~ s/;.*//; # no attributes
398 $ct = ascii_html($ct);
399 my $desc = $part->header('Content-Description');
400 $desc = $fn unless defined $desc;
401 $desc = '' unless defined $desc;
403 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
405 } elsif ($ct eq 'text/plain') {
410 my @ret = qq($nl<a\nhref="$upfx$idx-$sfn">[-- Attachment #$idx: );
411 my $ts = "Type: $ct, Size: $size bytes";
412 push(@ret, ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]");
413 join('', @ret, "</a>\n");
417 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
418 my ($part, $depth, @idx) = @$p;
419 my $ct = $part->content_type;
420 my $fn = $part->filename;
422 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
423 return attach_link($upfx, $ct, $p, $fn);
426 my $s = eval { $part->body_str };
428 # badly-encoded message? tell the world about it!
429 return attach_link($upfx, $ct, $p, $fn) if $@;
431 my @lines = split(/^/m, $s);
433 if (defined($fn) || $depth > 0) {
434 $s .= attach_link($upfx, $ct, $p, $fn);
438 my $l = PublicInbox::Linkify->new;
439 while (defined(my $cur = shift @lines)) {
441 # show the previously buffered quote inline
442 flush_quote(\$s, $l, \@quot) if @quot;
445 $cur = $l->linkify_1($cur);
446 $cur = ascii_html($cur);
447 $s .= $l->linkify_2($cur);
456 flush_quote(\$s, $l, \@quot);
458 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
459 $s =~ s/\A\n+//s; # kill leading blank lines
460 $s =~ s/\s+\z//s; # kill all trailing spaces
464 sub _msg_html_prepare {
465 my ($hdr, $ctx) = @_;
466 my $srch = $ctx->{srch} if $ctx;
468 my $rv = "<pre\nid=b>"; # anchor for body start
471 $ctx->{-upfx} = '../';
474 my $mid = $hdr->header_raw('Message-ID');
475 $mid = PublicInbox::Hval->new_msgid($mid);
476 foreach my $h (qw(From To Cc Subject Date)) {
477 my $v = $hdr->header($h);
478 defined($v) && ($v ne '') or next;
479 $v = PublicInbox::Hval->new($v);
482 my @n = PublicInbox::Address::names($v->raw);
483 $title[1] = ascii_html(join(', ', @n));
484 } elsif ($h eq 'Subject') {
485 $title[0] = $v->as_html;
487 $rv .= qq($h: <a\nhref="#r"\nid=t>);
488 $rv .= $v->as_html . "</a>\n";
493 $v =~ s/(\@[^,]+,) /$1\n\t/g if ($h eq 'Cc' || $h eq 'To');
497 $title[0] ||= '(no subject)';
498 $ctx->{-title_html} = join(' - ', @title);
499 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
500 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
501 $rv .= _parent_headers($hdr, $srch);
506 my ($dst, $ctx, $hdr, $tpfx) = @_;
507 my $srch = $ctx->{srch};
508 my $mid = mid_clean($hdr->header_raw('Message-ID'));
509 my $sres = $srch->get_thread($mid);
510 my $nr = $sres->{total};
511 my $expand = qq(<a\nhref="${tpfx}T/#u">expand</a> ) .
512 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
513 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
515 my $parent = in_reply_to($hdr);
517 if (defined $parent) {
518 $$dst .= "($expand)\n ";
519 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
521 $$dst .= "[no followups, yet] ($expand)\n";
523 $ctx->{next_msg} = undef;
524 $ctx->{parent_msg} = $parent;
528 $$dst .= "$nr+ messages in thread ($expand";
529 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
531 my $subj = $srch->subject_path($hdr->header('Subject'));
532 $ctx->{seen} = { $subj => 1 };
534 $ctx->{prev_attr} = '';
535 $ctx->{prev_level} = 0;
537 walk_thread(thread_results(load_results($sres)), $ctx, *skel_dump);
538 $ctx->{parent_msg} = $parent;
541 sub _parent_headers {
542 my ($hdr, $srch) = @_;
545 my $irt = in_reply_to($hdr);
547 my $v = PublicInbox::Hval->new_msgid($irt, 1);
548 my $html = $v->as_html;
549 my $href = $v->as_href;
550 $rv .= "In-Reply-To: <";
551 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
554 # do not display References: if search is present,
555 # we show the thread skeleton at the bottom, instead.
558 my $refs = $hdr->header_raw('References');
560 # avoid redundant URLs wasting bandwidth
562 $seen{$irt} = 1 if defined $irt;
564 my @raw_refs = ($refs =~ /<([^>]+)>/g);
565 foreach my $ref (@raw_refs) {
568 push @refs, linkify_ref_nosrch($ref);
572 $rv .= 'References: '. join("\n\t", @refs) . "\n";
578 sub squote_maybe ($) {
580 if ($val =~ m{([^\w@\./,\%\+\-])}) {
581 $val =~ s/(['!])/'\\$1'/g; # '!' for csh
587 sub mailto_arg_link {
589 my %cc; # everyone else
590 my $to; # this is the From address
592 foreach my $h (qw(From To Cc)) {
593 my $v = $hdr->header($h);
594 defined($v) && ($v ne '') or next;
595 my @addrs = PublicInbox::Address::emails($v);
596 foreach my $address (@addrs) {
597 my $dst = lc($address);
598 $cc{$dst} ||= $address;
604 my $subj = $hdr->header('Subject') || '';
605 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
606 my $mid = $hdr->header_raw('Message-ID');
607 push @arg, '--in-reply-to='.ascii_html(squote_maybe(mid_clean($mid)));
608 my $irt = uri_escape_utf8($mid);
610 push @arg, '--to=' . ascii_html($to);
611 $to = uri_escape_utf8($to);
612 $subj = uri_escape_utf8($subj);
613 my $cc = join(',', sort values %cc);
614 push @arg, '--cc=' . ascii_html($cc);
615 $cc = uri_escape_utf8($cc);
616 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
619 (\@arg, ascii_html($href));
623 my ($hdr, $standalone, $ctx, $rhref) = @_;
625 my $srch = $ctx->{srch} if $ctx;
628 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
632 thread_skel(\$idx, $ctx, $hdr, $tpfx);
633 my $p = $ctx->{parent_msg};
634 my $next = $ctx->{next_msg};
636 $p = PublicInbox::Hval->new_msgid($p);
638 $irt = "<a\nhref=\"$upfx$p/\"\nrel=prev>parent</a> ";
640 $irt = ' ' x length('parent ');
643 my $n = PublicInbox::Hval->new_msgid($next)->as_href;
644 $irt .= "<a\nhref=\"$upfx$n/\"\nrel=next>next</a> ";
646 $irt .= ' ' x length('next ');
652 $irt .= qq(<a\nhref="$rhref">reply</a>);
656 sub linkify_ref_nosrch {
657 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
658 my $html = $v->as_html;
659 my $href = $v->as_href;
660 "<<a\nhref=\"../$href/\">$html</a>>";
665 'm' . id_compress($msgid, 1);
669 my ($upfx, $mid) = @_;
670 # 'subject dummy' is used internally by Mail::Thread
671 return '[no common parent]' if ($mid eq 'subject dummy');
673 $mid = PublicInbox::Hval->new_msgid($mid);
674 my $href = $mid->as_href;
675 my $html = $mid->as_html;
676 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
681 INDENT x ($level - 1);
687 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
692 my $ts = eval { str2time($hdr->header('Date')) };
693 defined($ts) ? $ts : 0;
698 require PublicInbox::Thread;
699 my $th = PublicInbox::Thread->new(@$msgs);
701 $th->order(*sort_ts);
707 require PublicInbox::ExtMsg;
708 PublicInbox::ExtMsg::ext_msg($ctx);
713 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
717 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
720 my ($ctx, $hdr, $level) = @_;
722 my $dst = $ctx->{dst};
723 my $cur = $ctx->{cur};
724 my $mid = mid_clean($hdr->header_raw('Message-ID'));
725 my $f = ascii_html($hdr->header('X-PI-From'));
726 my $d = _msg_date($hdr) . ' ' . indent_for($level) . th_pfx($level);
728 $ctx->{first_level} ||= $level;
730 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
731 $ctx->{prev_attr} = $attr;
735 $ctx->{prev_level} = $level;
741 $$dst .= "<b><a\nid=r\nhref=\"#t\">".
742 "$attr [this message]</a></b>\n";
746 $ctx->{next_msg} ||= $mid;
749 # Subject is never undef, this mail was loaded from
750 # our Xapian which would've resulted in '' if it were
751 # really missing (and Filter rejects empty subjects)
752 my $s = $hdr->header('Subject');
753 my $h = $ctx->{srch}->subject_path($s);
754 if ($ctx->{seen}->{$h}) {
757 $ctx->{seen}->{$h} = 1;
758 $s = PublicInbox::Hval->new($s);
761 my $m = PublicInbox::Hval->new_msgid($mid);
763 my $mapping = $ctx->{mapping};
764 my $end = defined($s) ? "$s</a> $f\n" : "$f</a>\n";
766 my $map = $mapping->{$mid};
767 $id = id_compress($mid, 1);
769 $map->[1] = "$d<a\nhref=\"$m\">$end";
772 $m = $ctx->{-upfx}.$m->as_href.'/';
774 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
778 my ($ctx, $level, $node) = @_;
779 if (my $mime = $node->message) {
780 _skel_header($ctx, $mime->header_obj, $level);
782 my $mid = $node->messageid;
783 my $dst = $ctx->{dst};
784 my $mapping = $ctx->{mapping};
785 my $map = $mapping->{$mid} if $mapping;
786 if ($mid eq 'subject dummy') {
787 my $ncp = "\t[no common parent]\n";
788 $map->[1] = $ncp if $map;
792 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
794 $d .= indent_for($level) . th_pfx($level);
795 my $upfx = $ctx->{-upfx};
796 my $m = PublicInbox::Hval->new_msgid($mid);
797 my $href = $upfx . $m->as_href . '/';
798 my $html = $m->as_html;
801 my $id = id_compress($mid, 1);
802 $map->[1] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
803 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
805 $d .= qq{<<a\nhref="$href">$html</a>>\n};
813 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
814 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
818 sub _tryload_ghost ($$) {
819 my ($srch, $mid) = @_;
820 my $smsg = $srch->lookup_mail($mid) or return;
824 # accumulate recent topics if search is supported
825 # returns 1 if done, undef if not
827 my ($ctx, $level, $node) = @_;
828 my $srch = $ctx->{srch};
829 my $mid = $node->messageid;
830 my $x = $node->message || _tryload_ghost($srch, $mid);
834 $subj = $x->header('Subject');
835 $subj = $srch->subject_normalized($subj);
836 $ts = $x->header('X-PI-TS');
837 } else { # ghost message, do not bump level
841 if (++$ctx->{subjs}->{$subj} == 1) {
842 push @{$ctx->{order}}, [ $level, $subj ];
844 my $exist = $ctx->{latest}->{$subj};
845 if (!$exist || $exist->[1] < $ts) {
846 $ctx->{latest}->{$subj} = [ $mid, $ts ];
852 my $order = $ctx->{order};
853 my $subjs = $ctx->{subjs};
854 my $latest = $ctx->{latest};
856 return $fh->write("\n[No topics in range]</pre>") unless scalar @$order;
862 while (defined(my $info = shift @$order)) {
863 my ($level, $subj) = @$info;
864 my $n = delete $subjs->{$subj};
865 my ($mid, $ts) = @{delete $latest->{$subj}};
866 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
867 $pfx = indent_for($level);
868 my $nl = $level == $prev ? "\n" : '';
873 $cur ||= [ $ts, '' ];
874 $cur->[0] = $ts if $ts > $cur->[0];
875 $cur->[1] .= $nl . $pfx . th_pfx($level);
876 if ($ts == -666) { # ghost
877 $cur->[1] .= ghost_parent('', $mid) . "\n";
878 next; # child will have mbox / atom link
881 $subj = PublicInbox::Hval->new($subj)->as_html;
882 $cur->[1] .= "<a\nhref=\"$mid/T/#u\"><b>$subj</b></a>\n";
884 my $attr = " $ts UTC";
886 # $n isn't the total number of posts on the topic,
887 # just the number of posts in the current results window
888 $n = $n == 1 ? '' : " ($n+ messages)";
890 if ($level == 0 || $attr ne $prev_attr) {
891 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
892 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
893 $pfx .= INDENT if $level > 0;
894 $cur->[1] .= $pfx . $attr . $n . " - $mbox / $atom\n";
898 push @recent, $cur if $cur;
899 @recent = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @recent;
900 $fh->write(join('', @recent) . '</pre>');
903 sub emit_index_topics {
905 my ($off) = (($ctx->{cgi}->param('o') || '0') =~ /(\d+)/);
910 my %opts = ( offset => $off, limit => $max * 4 );
911 while (scalar @{$ctx->{order}} < $max) {
912 my $sres = $ctx->{srch}->query('', \%opts);
913 my $nr = scalar @{$sres->{msgs}} or last;
914 $sres = load_results($sres);
915 walk_thread(thread_results($sres), $ctx, *add_topic);
916 $opts{offset} += $nr;
923 sub thread_adj_level {
924 my ($ctx, $level) = @_;
926 my $max = $ctx->{cur_level};
928 return ('', '') if $max == 0; # flat output
930 # reset existing lists
931 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
932 $ctx->{cur_level} = 0;
934 } elsif ($level == $max) { # continue existing list
936 } elsif ($level < $max) {
937 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
938 $ctx->{cur_level} = $level;
939 ("$beg<li>", '</li>');
940 } else { # ($level > $max) # start a new level
941 $ctx->{cur_level} = $level;
942 my $beg = ($max ? '<li>' : '') . '<ul><li>';
947 sub ghost_index_entry {
948 my ($ctx, $level, $mid) = @_;
949 my ($beg, $end) = thread_adj_level($ctx, $level);
950 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $mid) . '</pre>' . $end;