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->new($ctx, 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
52 my $se_url = 'https://git-htmldocs.bogomips.org/git-send-email.html';
54 my ($arg, $link) = mailto_arg_link($hdr);
55 push @$arg, '/path/to/YOUR_REPLY';
58 "You may reply publically to <a\nhref=#t>this message</a> via\n".
59 "plain-text email using any one of the following methods:\n\n" .
60 "* Save the following mbox file, import it into your mail client,\n" .
61 " and reply-to-all from there: <a\nhref=raw>mbox</a>\n\n" .
62 "* Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,\n" .
63 " and <b>--in-reply-to</b> switches of git-send-email(1):\n\n" .
64 " git send-email \\\n " .
65 join(" \\\n ", @$arg ). "\n\n" .
66 qq( <a\nhref="$se_url">$se_url</a>\n\n) .
67 "* If your mail client supports setting the <b>In-Reply-To</b>" .
68 " header\n via mailto: links, try the " .
69 qq(<a\nhref="$link">mailto: link</a>\n) .
75 my $irt = $hdr->header_raw('In-Reply-To');
77 return mid_clean($irt) if (defined $irt);
79 my $refs = $hdr->header_raw('References');
80 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
87 my ($hdr, $field) = @_;
88 my $val = $hdr->header($field) or return '';
89 ascii_html(join(', ', PublicInbox::Address::names($val)));
93 my ($nr, $singular, $plural) = @_;
94 return "0 $plural" if $nr == 0;
95 $nr == 1 ? "$nr $singular" : "$nr $plural";
98 # this is already inside a <pre>
100 my ($mime, $ctx, $more) = @_;
101 my $srch = $ctx->{srch};
102 my $hdr = $mime->header_obj;
103 my $subj = $hdr->header('Subject');
105 my $mid_raw = mid_clean(mid_mime($mime));
106 my $id = id_compress($mid_raw, 1);
108 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
110 my $root_anchor = $ctx->{root_anchor} || '';
111 my $irt = in_reply_to($hdr);
113 my $rv = '<b>'.ascii_html($subj).'</b>';
114 $rv = "<u\nid=u>$rv</u>" if $root_anchor eq $id_m;
116 $rv .= _th_index_lite($mid_raw, $irt, $id, $ctx);
118 foreach my $f (qw(To Cc)) {
119 my $dst = _hdr_names($hdr, $f);
120 push @tocc, "$f: $dst" if $dst ne '';
122 $rv .= "From: "._hdr_names($hdr, 'From').' @ '._msg_date($hdr)." UTC";
123 my $upfx = $ctx->{-upfx};
124 my $mhref = $upfx . $mid->as_href . '/';
125 $rv .= qq{ (<a\nhref="$mhref/">permalink</a> / };
126 $rv .= qq{<a\nhref="$mhref/raw">raw</a>)\n};
127 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
129 my $mapping = $ctx->{mapping};
130 if (!$mapping && $irt) {
131 my $mirt = PublicInbox::Hval->msgid($irt);
132 my $href = $upfx . $mirt->as_href . '/';
133 my $html = $mirt->as_html;
134 $rv .= qq(In-Reply-To: <<a\nhref="$href/">$html</a>>\n)
138 # scan through all parts, looking for displayable text
139 msg_iter($mime, sub { $rv .= add_text_body($mhref, $_[0]) });
142 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
143 "<a\nhref=\"$mhref\">permalink</a>" .
144 " / <a\nhref=\"${mhref}raw\">raw</a>" .
145 " / <a\nhref=\"${mhref}#R\">reply</a>";
146 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
147 $rv .= " [relevance $pct->{$mid_raw}%]";
149 my $threaded = 'threaded';
153 $flat = "<b>$flat</b>";
155 $threaded = "<b>$threaded</b>";
157 $rv .= " / [<a\nhref=\"${mhref}T/#u\">$flat</a>";
158 $rv .= "|<a\nhref=\"${mhref}t/#u\">$threaded</a>]";
159 $rv .= " / <a\nhref=#r$id>thread overview</a>";
162 $rv .= $more ? "\n\n" : "\n";
165 sub pad_link ($$;$) {
166 my ($mid, $level, $s) = @_;
168 my $id = id_compress($mid, 1);
169 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
173 my ($mid_raw, $irt, $id, $ctx) = @_;
175 my $mapping = $ctx->{mapping} or return $rv;
177 # map = [children, attr, node, idx, level]
178 my $map = $mapping->{$mid_raw};
179 my $nr_c = scalar @{$map->[0]};
181 my $level = $map->[4];
184 my $irt_map = $mapping->{$irt};
185 my $siblings = $irt_map->[0];
186 $nr_s = scalar(@$siblings) - 1;
187 $rv .= $pad . $irt_map->[1];
189 my $prev = $siblings->[$idx - 1];
190 my $pmid = $prev->messageid;
192 my $s = ($idx - 1). ' preceding siblings ...';
193 $rv .= pad_link($pmid, $level, $s);
194 } elsif ($idx == 2) {
195 my $ppmid = $siblings->[0]->messageid;
196 $rv .= $pad . $mapping->{$ppmid}->[1];
198 $rv .= $pad . $mapping->{$pmid}->[1];
201 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
202 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
203 my $this = $map->[1];
204 $this =~ s!\n\z!</b>\n!s;
205 $this =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
207 my $node = $map->[2];
208 if (my $child = $node->child) {
209 my $cmid = $child->messageid;
210 $rv .= $pad . $mapping->{$cmid}->[1];
212 my $s = ($nr_c - 1). ' more replies';
213 $rv .= pad_link($cmid, $level + 1, $s);
214 } elsif (my $cn = $child->next) {
215 $rv .= $pad . $mapping->{$cn->messageid}->[1];
218 if (my $next = $node->next) {
219 my $nmid = $next->messageid;
220 $rv .= $pad . $mapping->{$nmid}->[1];
221 my $nnext = $nr_s - $idx;
223 my $s = ($nnext - 1).' subsequent siblings';
224 $rv .= pad_link($nmid, $level, $s);
225 } elsif (my $nn = $next->next) {
226 $rv .= $pad . $mapping->{$nn->messageid}->[1];
229 $rv .= "<a\nhref=#e$id\nid=m$id>_</a> <a\nhref=#r$id>$s_s, $s_c</a>\n";
233 my ($th, $ctx, $cb) = @_;
234 my @q = map { (0, $_) } $th->rootset;
236 my $level = shift @q;
237 my $node = shift @q or next;
238 $cb->($ctx, $level, $node);
239 unshift @q, $level+1, $node->child, $level, $node->next;
244 my ($ctx, $level, $node) = @_;
245 my $mapping = $ctx->{mapping};
247 if (my $parent = $node->parent) {
248 my $m = $mapping->{$parent->messageid}->[0];
252 $mapping->{$node->messageid} = [ [], '', $node, $idx, $level ];
253 skel_dump($ctx, $level, $node);
256 sub thread_index_entry {
257 my ($ctx, $level, $mime) = @_;
258 my ($beg, $end) = thread_adj_level($ctx, $level);
259 $beg . '<pre>' . index_entry($mime, $ctx, 0) . '</pre>' . $end;
262 sub stream_thread ($$) {
264 my $inbox = $ctx->{-inbox};
266 my @q = map { (0, $_) } $th->rootset;
270 my $node = shift @q or next;
271 unshift @q, $level+1, $node->child, $level, $node->next;
272 $mime = $inbox->msg_by_mid($node->messageid) and last;
274 return missing_thread($ctx) unless $mime;
276 $mime = Email::MIME->new($mime);
277 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
278 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
279 my $body = PublicInbox::WwwStream->new($ctx, sub {
283 my $node = shift @q or next;
284 unshift @q, $level+1, $node->child, $level, $node->next;
285 my $mid = $node->messageid;
286 if ($mime = $inbox->msg_by_mid($mid)) {
287 $mime = Email::MIME->new($mime);
288 return thread_index_entry($ctx, $level, $mime);
290 return ghost_index_entry($ctx, $level, $mid);
293 my $ret = join('', thread_adj_level($ctx, 0));
294 $ret .= ${$ctx->{dst}}; # skel
298 [ 200, ['Content-Type', 'text/html; charset=UTF-8'], $body ];
303 my $mid = $ctx->{mid};
304 my $sres = $ctx->{srch}->get_thread($mid, { asc => 1 });
305 my $msgs = load_results($sres);
306 my $nr = $sres->{total};
307 return missing_thread($ctx) if $nr == 0;
308 my $skel = '<hr /><pre>';
309 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
310 $skel .= ", back to <a\nhref=\"../../\">index</a>";
311 $skel .= "\n<a\nid=t>$nr+ messages in thread:</a> (download: ";
312 $skel .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
313 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
314 $ctx->{-upfx} = '../../';
315 $ctx->{cur_level} = 0;
316 $ctx->{dst} = \$skel;
317 $ctx->{prev_attr} = '';
318 $ctx->{prev_level} = 0;
319 $ctx->{root_anchor} = anchor_for($mid);
321 $ctx->{mapping} = {};
323 my $th = thread_results($msgs);
324 walk_thread($th, $ctx, *pre_thread);
326 return stream_thread($th, $ctx) unless $ctx->{flat};
328 # flat display: lazy load the full message from mini_mime:
329 my $inbox = $ctx->{-inbox};
331 while ($mime = shift @$msgs) {
332 $mime = $inbox->msg_by_mid(mid_clean(mid_mime($mime))) and last;
334 return missing_thread($ctx) unless $mime;
335 $mime = Email::MIME->new($mime);
336 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
337 $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
339 my $body = PublicInbox::WwwStream->new($ctx, sub {
341 while ($mime = shift @$msgs) {
342 $mid = mid_clean(mid_mime($mime));
343 $mime = $inbox->msg_by_mid($mid) and last;
346 $mime = Email::MIME->new($mime);
347 return index_entry($mime, $ctx, scalar @$msgs);
352 [ 200, ['Content-Type', 'text/html; charset=UTF-8'], $body ];
355 sub multipart_text_as_html {
356 my ($mime, $upfx) = @_;
359 # scan through all parts, looking for displayable text
360 msg_iter($mime, sub {
362 $rv .= add_text_body($upfx, $p);
368 my ($s, $l, $quot) = @_;
370 # show everything in the full version with anchor from
371 # short version (see above)
372 my $rv = $l->linkify_1(join('', @$quot));
375 # we use a <span> here to allow users to specify their own
376 # color for quoted text
377 $rv = $l->linkify_2(ascii_html($rv));
378 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
381 sub attach_link ($$$$) {
382 my ($upfx, $ct, $p, $fn) = @_;
383 my ($part, $depth, @idx) = @$p;
384 my $nl = $idx[-1] > 1 ? "\n" : '';
385 my $idx = join('.', @idx);
386 my $size = bytes::length($part->body);
387 $ct ||= 'text/plain';
388 $ct =~ s/;.*//; # no attributes
389 $ct = ascii_html($ct);
390 my $desc = $part->header('Content-Description');
391 $desc = $fn unless defined $desc;
392 $desc = '' unless defined $desc;
394 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
396 } elsif ($ct eq 'text/plain') {
401 my @ret = qq($nl<a\nhref="$upfx$idx-$sfn">[-- Attachment #$idx: );
402 my $ts = "Type: $ct, Size: $size bytes";
403 push(@ret, ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]");
404 join('', @ret, "</a>\n");
408 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
409 my ($part, $depth, @idx) = @$p;
410 my $ct = $part->content_type;
411 my $fn = $part->filename;
413 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
414 return attach_link($upfx, $ct, $p, $fn);
417 my $s = eval { $part->body_str };
419 # badly-encoded message? tell the world about it!
420 return attach_link($upfx, $ct, $p, $fn) if $@;
422 my @lines = split(/^/m, $s);
424 if (defined($fn) || $depth > 0) {
425 $s .= attach_link($upfx, $ct, $p, $fn);
429 my $l = PublicInbox::Linkify->new;
430 while (defined(my $cur = shift @lines)) {
432 # show the previously buffered quote inline
433 flush_quote(\$s, $l, \@quot) if @quot;
436 $cur = $l->linkify_1($cur);
437 $cur = ascii_html($cur);
438 $s .= $l->linkify_2($cur);
447 flush_quote(\$s, $l, \@quot);
449 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
450 $s =~ s/\A\n+//s; # kill leading blank lines
451 $s =~ s/\s+\z//s; # kill all trailing spaces
455 sub _msg_html_prepare {
456 my ($hdr, $ctx) = @_;
457 my $srch = $ctx->{srch} if $ctx;
459 my $rv = "<pre\nid=b>"; # anchor for body start
462 $ctx->{-upfx} = '../';
465 my $mid = $hdr->header_raw('Message-ID');
466 $mid = PublicInbox::Hval->new_msgid($mid);
467 foreach my $h (qw(From To Cc Subject Date)) {
468 my $v = $hdr->header($h);
469 defined($v) && ($v ne '') or next;
470 $v = PublicInbox::Hval->new($v);
473 my @n = PublicInbox::Address::names($v->raw);
474 $title[1] = ascii_html(join(', ', @n));
475 } elsif ($h eq 'Subject') {
476 $title[0] = $v->as_html;
478 $rv .= qq($h: <a\nhref="#r"\nid=t>);
479 $rv .= $v->as_html . "</a>\n";
484 $v =~ s/(\@[^,]+,) /$1\n\t/g if ($h eq 'Cc' || $h eq 'To');
488 $title[0] ||= '(no subject)';
489 $ctx->{-title_html} = join(' - ', @title);
490 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
491 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
492 $rv .= _parent_headers($hdr, $srch);
497 my ($dst, $ctx, $hdr, $tpfx) = @_;
498 my $srch = $ctx->{srch};
499 my $mid = mid_clean($hdr->header_raw('Message-ID'));
500 my $sres = $srch->get_thread($mid);
501 my $nr = $sres->{total};
502 my $expand = qq(<a\nhref="${tpfx}T/#u">expand</a> ) .
503 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
504 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
506 my $parent = in_reply_to($hdr);
508 if (defined $parent) {
509 $$dst .= "($expand)\n ";
510 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
512 $$dst .= "[no followups, yet] ($expand)\n";
514 $ctx->{next_msg} = undef;
515 $ctx->{parent_msg} = $parent;
519 $$dst .= "$nr+ messages in thread ($expand";
520 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
522 my $subj = $srch->subject_path($hdr->header('Subject'));
523 $ctx->{seen} = { $subj => 1 };
525 $ctx->{prev_attr} = '';
526 $ctx->{prev_level} = 0;
528 walk_thread(thread_results(load_results($sres)), $ctx, *skel_dump);
529 $ctx->{parent_msg} = $parent;
532 sub _parent_headers {
533 my ($hdr, $srch) = @_;
536 my $irt = in_reply_to($hdr);
538 my $v = PublicInbox::Hval->new_msgid($irt, 1);
539 my $html = $v->as_html;
540 my $href = $v->as_href;
541 $rv .= "In-Reply-To: <";
542 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
545 # do not display References: if search is present,
546 # we show the thread skeleton at the bottom, instead.
549 my $refs = $hdr->header_raw('References');
551 # avoid redundant URLs wasting bandwidth
553 $seen{$irt} = 1 if defined $irt;
555 my @raw_refs = ($refs =~ /<([^>]+)>/g);
556 foreach my $ref (@raw_refs) {
559 push @refs, linkify_ref_nosrch($ref);
563 $rv .= 'References: '. join("\n\t", @refs) . "\n";
569 sub squote_maybe ($) {
571 if ($val =~ m{([^\w@\./,\%\+\-])}) {
572 $val =~ s/(['!])/'\\$1'/g; # '!' for csh
578 sub mailto_arg_link {
580 my %cc; # everyone else
581 my $to; # this is the From address
583 foreach my $h (qw(From To Cc)) {
584 my $v = $hdr->header($h);
585 defined($v) && ($v ne '') or next;
586 my @addrs = PublicInbox::Address::emails($v);
587 foreach my $address (@addrs) {
588 my $dst = lc($address);
589 $cc{$dst} ||= $address;
595 my $subj = $hdr->header('Subject') || '';
596 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
597 my $mid = $hdr->header_raw('Message-ID');
598 push @arg, '--in-reply-to='.ascii_html(squote_maybe(mid_clean($mid)));
599 my $irt = uri_escape_utf8($mid);
601 push @arg, '--to=' . ascii_html($to);
602 $to = uri_escape_utf8($to);
603 $subj = uri_escape_utf8($subj);
604 my $cc = join(',', sort values %cc);
605 push @arg, '--cc=' . ascii_html($cc);
606 $cc = uri_escape_utf8($cc);
607 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
610 (\@arg, ascii_html($href));
614 my ($hdr, $standalone, $ctx, $rhref) = @_;
616 my $srch = $ctx->{srch} if $ctx;
619 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
623 thread_skel(\$idx, $ctx, $hdr, $tpfx);
624 my $p = $ctx->{parent_msg};
625 my $next = $ctx->{next_msg};
627 $p = PublicInbox::Hval->new_msgid($p);
629 $irt = "<a\nhref=\"$upfx$p/\"\nrel=prev>parent</a> ";
631 $irt = ' ' x length('parent ');
634 my $n = PublicInbox::Hval->new_msgid($next)->as_href;
635 $irt .= "<a\nhref=\"$upfx$n/\"\nrel=next>next</a> ";
637 $irt .= ' ' x length('next ');
643 $irt .= qq(<a\nhref="$rhref">reply</a>);
647 sub linkify_ref_nosrch {
648 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
649 my $html = $v->as_html;
650 my $href = $v->as_href;
651 "<<a\nhref=\"../$href/\">$html</a>>";
656 'm' . id_compress($msgid, 1);
660 my ($upfx, $mid) = @_;
661 # 'subject dummy' is used internally by Mail::Thread
662 return '[no common parent]' if ($mid eq 'subject dummy');
664 $mid = PublicInbox::Hval->new_msgid($mid);
665 my $href = $mid->as_href;
666 my $html = $mid->as_html;
667 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
672 INDENT x ($level - 1);
678 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
683 my $ts = eval { str2time($hdr->header('Date')) };
684 defined($ts) ? $ts : 0;
689 require PublicInbox::Thread;
690 my $th = PublicInbox::Thread->new(@$msgs);
692 $th->order(*sort_ts);
698 require PublicInbox::ExtMsg;
699 PublicInbox::ExtMsg::ext_msg($ctx);
704 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
708 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
711 my ($ctx, $hdr, $level) = @_;
713 my $dst = $ctx->{dst};
714 my $cur = $ctx->{cur};
715 my $mid = mid_clean($hdr->header_raw('Message-ID'));
716 my $f = ascii_html($hdr->header('X-PI-From'));
717 my $d = _msg_date($hdr) . ' ' . indent_for($level) . th_pfx($level);
719 $ctx->{first_level} ||= $level;
721 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
722 $ctx->{prev_attr} = $attr;
726 $ctx->{prev_level} = $level;
732 $$dst .= "<b><a\nid=r\nhref=\"#t\">".
733 "$attr [this message]</a></b>\n";
737 $ctx->{next_msg} ||= $mid;
740 # Subject is never undef, this mail was loaded from
741 # our Xapian which would've resulted in '' if it were
742 # really missing (and Filter rejects empty subjects)
743 my $s = $hdr->header('Subject');
744 my $h = $ctx->{srch}->subject_path($s);
745 if ($ctx->{seen}->{$h}) {
748 $ctx->{seen}->{$h} = 1;
749 $s = PublicInbox::Hval->new($s);
752 my $m = PublicInbox::Hval->new_msgid($mid);
754 my $mapping = $ctx->{mapping};
755 my $end = defined($s) ? "$s</a> $f\n" : "$f</a>\n";
757 my $map = $mapping->{$mid};
758 $id = id_compress($mid, 1);
760 $map->[1] = "$d<a\nhref=\"$m\">$end";
763 $m = $ctx->{-upfx}.$m->as_href.'/';
765 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
769 my ($ctx, $level, $node) = @_;
770 if (my $mime = $node->message) {
771 _skel_header($ctx, $mime->header_obj, $level);
773 my $mid = $node->messageid;
774 my $dst = $ctx->{dst};
775 my $mapping = $ctx->{mapping};
776 my $map = $mapping->{$mid} if $mapping;
777 if ($mid eq 'subject dummy') {
778 my $ncp = "\t[no common parent]\n";
779 $map->[1] = $ncp if $map;
783 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
785 $d .= indent_for($level) . th_pfx($level);
786 my $upfx = $ctx->{-upfx};
787 my $m = PublicInbox::Hval->new_msgid($mid);
788 my $href = $upfx . $m->as_href . '/';
789 my $html = $m->as_html;
792 my $id = id_compress($mid, 1);
793 $map->[1] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
794 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
796 $d .= qq{<<a\nhref="$href">$html</a>>\n};
804 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
805 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
809 sub _tryload_ghost ($$) {
810 my ($srch, $mid) = @_;
811 my $smsg = $srch->lookup_mail($mid) or return;
815 # accumulate recent topics if search is supported
816 # returns 1 if done, undef if not
818 my ($ctx, $level, $node) = @_;
819 my $srch = $ctx->{srch};
820 my $mid = $node->messageid;
821 my $x = $node->message || _tryload_ghost($srch, $mid);
825 $subj = $x->header('Subject');
826 $subj = $srch->subject_normalized($subj);
827 $ts = $x->header('X-PI-TS');
828 } else { # ghost message, do not bump level
832 if (++$ctx->{subjs}->{$subj} == 1) {
833 push @{$ctx->{order}}, [ $level, $subj ];
835 my $exist = $ctx->{latest}->{$subj};
836 if (!$exist || $exist->[1] < $ts) {
837 $ctx->{latest}->{$subj} = [ $mid, $ts ];
843 my $order = $ctx->{order};
844 my $subjs = $ctx->{subjs};
845 my $latest = $ctx->{latest};
847 return $fh->write("\n[No topics in range]</pre>") unless scalar @$order;
853 while (defined(my $info = shift @$order)) {
854 my ($level, $subj) = @$info;
855 my $n = delete $subjs->{$subj};
856 my ($mid, $ts) = @{delete $latest->{$subj}};
857 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
858 $pfx = indent_for($level);
859 my $nl = $level == $prev ? "\n" : '';
864 $cur ||= [ $ts, '' ];
865 $cur->[0] = $ts if $ts > $cur->[0];
866 $cur->[1] .= $nl . $pfx . th_pfx($level);
867 if ($ts == -666) { # ghost
868 $cur->[1] .= ghost_parent('', $mid) . "\n";
869 next; # child will have mbox / atom link
872 $subj = PublicInbox::Hval->new($subj)->as_html;
873 $cur->[1] .= "<a\nhref=\"$mid/T/#u\"><b>$subj</b></a>\n";
875 my $attr = " $ts UTC";
877 # $n isn't the total number of posts on the topic,
878 # just the number of posts in the current results window
879 $n = $n == 1 ? '' : " ($n+ messages)";
881 if ($level == 0 || $attr ne $prev_attr) {
882 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
883 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
884 $pfx .= INDENT if $level > 0;
885 $cur->[1] .= $pfx . $attr . $n . " - $mbox / $atom\n";
889 push @recent, $cur if $cur;
890 @recent = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @recent;
891 $fh->write(join('', @recent) . '</pre>');
894 sub emit_index_topics {
896 my ($off) = (($ctx->{cgi}->param('o') || '0') =~ /(\d+)/);
901 my %opts = ( offset => $off, limit => $max * 4 );
902 while (scalar @{$ctx->{order}} < $max) {
903 my $sres = $ctx->{srch}->query('', \%opts);
904 my $nr = scalar @{$sres->{msgs}} or last;
905 $sres = load_results($sres);
906 walk_thread(thread_results($sres), $ctx, *add_topic);
907 $opts{offset} += $nr;
914 sub thread_adj_level {
915 my ($ctx, $level) = @_;
917 my $max = $ctx->{cur_level};
919 return ('', '') if $max == 0; # flat output
921 # reset existing lists
922 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
923 $ctx->{cur_level} = 0;
925 } elsif ($level == $max) { # continue existing list
927 } elsif ($level < $max) {
928 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
929 $ctx->{cur_level} = $level;
930 ("$beg<li>", '</li>');
931 } else { # ($level > $max) # start a new level
932 $ctx->{cur_level} = $level;
933 my $beg = ($max ? '<li>' : '') . '<ul><li>';
938 sub ghost_index_entry {
939 my ($ctx, $level, $mid) = @_;
940 my ($beg, $end) = thread_adj_level($ctx, $level);
941 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $mid) . '</pre>' . $end;