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)));
92 # this is already inside a <pre>
94 my ($mime, $level, $state) = @_;
95 my $midx = $state->{anchor_idx}++;
96 my $ctx = $state->{ctx};
97 my $srch = $ctx->{srch};
98 my $hdr = $mime->header_obj;
99 my $subj = $hdr->header('Subject');
101 my $mid_raw = mid_clean(mid_mime($mime));
102 my $id = anchor_for($mid_raw);
103 my $seen = $state->{seen};
104 $seen->{$id} = "#$id"; # save the anchor for children, later
106 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
108 my $root_anchor = $state->{root_anchor} || '';
109 my $path = $root_anchor ? '../../' : '';
110 my $href = $mid->as_href;
111 my $irt = in_reply_to($hdr);
112 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
114 $subj = ascii_html($subj);
115 $subj = "<a\nhref=\"${path}$href/\">$subj</a>";
116 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id;
118 my $ts = _msg_date($hdr);
119 my $rv = "<pre\nid=s$midx>";
120 $rv .= "<b\nid=$id>$subj</b>\n";
121 my $txt = "${path}$href/raw";
122 my $fh = $state->{fh};
123 my $from = _hdr_names($hdr, 'From');
124 $rv .= "- $from @ $ts UTC (<a\nhref=\"$txt\">raw</a>)\n";
126 foreach my $f (qw(To Cc)) {
127 my $dst = _hdr_names($hdr, $f);
128 push @tocc, "$f: $dst" if $dst ne '';
130 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
131 $fh->write($rv .= "\n");
133 my $mhref = "${path}$href/";
135 # scan through all parts, looking for displayable text
136 msg_iter($mime, sub { index_walk($fh, $mhref, $_[0]) });
137 $rv = "\n" . html_footer($hdr, 0, $ctx, "$path$href/#R");
140 unless (defined $parent_anchor) {
141 my $v = PublicInbox::Hval->new_msgid($irt, 1);
143 $parent_anchor = "${path}$v/";
145 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
147 if (my $pct = $state->{pct}) { # used by SearchView.pm
148 $rv .= " [relevance $pct->{$mid_raw}%]";
150 my $threaded = 'threaded';
154 $flat = "<b>$flat</b>";
155 $end = "\n"; # for lynx
157 $threaded = "<b>$threaded</b>";
159 $rv .= " [<a\nhref=\"${path}$href/t/#u\">$threaded</a>";
160 $rv .= "|<a\nhref=\"${path}$href/T/#u\">$flat</a>]$end";
162 $fh->write($rv .= '</pre>');
166 my ($ctx, $foot, $srch) = @_;
167 # $_[0] in sub is the Plack callback
168 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
172 my ($th, $state, $cb) = @_;
173 my @q = map { (0, $_) } $th->rootset;
175 my $level = shift @q;
176 my $node = shift @q or next;
177 $cb->($state, $level, $node);
178 unshift @q, $level+1, $node->child, $level, $node->next;
182 # only private functions below.
184 sub emit_thread_html {
185 my ($res, $ctx, $foot, $srch) = @_;
186 my $mid = $ctx->{mid};
187 my $flat = $ctx->{flat};
188 my $msgs = load_results($srch->get_thread($mid, { asc => $flat }));
189 my $nr = scalar @$msgs;
190 return missing_thread($res, $ctx) if $nr == 0;
196 root_anchor => anchor_for($mid),
201 require PublicInbox::Git;
202 $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
204 pre_anchor_entry($seen, $_) for (@$msgs);
205 __thread_entry($state, $_, 0) for (@$msgs);
207 walk_thread(thread_results($msgs), $state, *thread_entry);
208 if (my $max = $state->{cur_level}) {
210 ('</ul></li>' x ($max - 1)) . '</ul>');
214 # there could be a race due to a message being deleted in git
215 # but still being in the Xapian index:
216 my $fh = delete $state->{fh} or return missing_thread($res, $ctx);
218 my $final_anchor = $state->{anchor_idx};
219 my $next = "<a\nid=s$final_anchor>";
220 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
221 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
222 $next .= "\ndownload thread: ";
223 $next .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
224 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
225 $fh->write('<hr /><pre>' . $next . "\n\n".
226 $foot . '</pre></body></html>');
231 my ($fh, $upfx, $p) = @_;
232 my $s = add_text_body($upfx, $p);
239 sub multipart_text_as_html {
240 my ($mime, $upfx) = @_;
243 # scan through all parts, looking for displayable text
244 msg_iter($mime, sub {
246 $rv .= add_text_body($upfx, $p);
252 my ($s, $l, $quot) = @_;
254 # show everything in the full version with anchor from
255 # short version (see above)
256 my $rv = $l->linkify_1(join('', @$quot));
259 # we use a <span> here to allow users to specify their own
260 # color for quoted text
261 $rv = $l->linkify_2(ascii_html($rv));
262 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
265 sub attach_link ($$$$) {
266 my ($upfx, $ct, $p, $fn) = @_;
267 my ($part, $depth, @idx) = @$p;
268 my $nl = $idx[-1] > 1 ? "\n" : '';
269 my $idx = join('.', @idx);
270 my $size = bytes::length($part->body);
271 $ct ||= 'text/plain';
272 $ct =~ s/;.*//; # no attributes
273 $ct = ascii_html($ct);
274 my $desc = $part->header('Content-Description');
275 $desc = $fn unless defined $desc;
276 $desc = '' unless defined $desc;
278 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
280 } elsif ($ct eq 'text/plain') {
285 my @ret = qq($nl<a\nhref="$upfx$idx-$sfn">[-- Attachment #$idx: );
286 my $ts = "Type: $ct, Size: $size bytes";
287 push(@ret, ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]");
288 join('', @ret, "</a>\n");
292 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
293 my ($part, $depth, @idx) = @$p;
294 my $ct = $part->content_type;
295 my $fn = $part->filename;
297 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
298 return attach_link($upfx, $ct, $p, $fn);
301 my $s = eval { $part->body_str };
303 # badly-encoded message? tell the world about it!
304 return attach_link($upfx, $ct, $p, $fn) if $@;
306 my @lines = split(/^/m, $s);
308 if (defined($fn) || $depth > 0) {
309 $s .= attach_link($upfx, $ct, $p, $fn);
313 my $l = PublicInbox::Linkify->new;
314 while (defined(my $cur = shift @lines)) {
316 # show the previously buffered quote inline
317 flush_quote(\$s, $l, \@quot) if @quot;
320 $cur = $l->linkify_1($cur);
321 $cur = ascii_html($cur);
322 $s .= $l->linkify_2($cur);
331 flush_quote(\$s, $l, \@quot);
333 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
334 $s =~ s/\A\n+//s; # kill leading blank lines
335 $s =~ s/\s+\z//s; # kill all trailing spaces
339 sub _msg_html_prepare {
340 my ($hdr, $ctx) = @_;
341 my $srch = $ctx->{srch} if $ctx;
343 my $rv = "<pre\nid=b>"; # anchor for body start
346 $ctx->{-upfx} = '../';
349 my $mid = $hdr->header_raw('Message-ID');
350 $mid = PublicInbox::Hval->new_msgid($mid);
351 foreach my $h (qw(From To Cc Subject Date)) {
352 my $v = $hdr->header($h);
353 defined($v) && ($v ne '') or next;
354 $v = PublicInbox::Hval->new($v);
357 my @n = PublicInbox::Address::names($v->raw);
358 $title[1] = ascii_html(join(', ', @n));
359 } elsif ($h eq 'Subject') {
360 $title[0] = $v->as_html;
362 $rv .= qq($h: <a\nhref="#r"\nid=t>);
363 $rv .= $v->as_html . "</a>\n";
368 $v =~ s/(\@[^,]+,) /$1\n\t/g if ($h eq 'Cc' || $h eq 'To');
372 $title[0] ||= '(no subject)';
373 $ctx->{-title_html} = join(' - ', @title);
374 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
375 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
376 $rv .= _parent_headers($hdr, $srch);
381 my ($dst, $ctx, $hdr, $tpfx) = @_;
382 my $srch = $ctx->{srch};
383 my $mid = mid_clean($hdr->header_raw('Message-ID'));
384 my $sres = $srch->get_thread($mid);
385 my $nr = $sres->{total};
386 my $expand = qq(<a\nhref="${tpfx}t/#u">expand</a> ) .
387 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
388 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
390 my $parent = in_reply_to($hdr);
392 if (defined $parent) {
393 $$dst .= "($expand)\n ";
394 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
396 $$dst .= "[no followups, yet] ($expand)\n";
398 $ctx->{next_msg} = undef;
399 $ctx->{parent_msg} = $parent;
403 $$dst .= "$nr+ messages in thread ($expand";
404 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
406 my $subj = $srch->subject_path($hdr->header('Subject'));
408 seen => { $subj => 1 },
416 walk_thread(thread_results(load_results($sres)), $state, *skel_dump);
417 $ctx->{next_msg} = $state->{next_msg};
418 $ctx->{parent_msg} = $parent;
421 sub _parent_headers {
422 my ($hdr, $srch) = @_;
425 my $irt = in_reply_to($hdr);
427 my $v = PublicInbox::Hval->new_msgid($irt, 1);
428 my $html = $v->as_html;
429 my $href = $v->as_href;
430 $rv .= "In-Reply-To: <";
431 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
434 # do not display References: if search is present,
435 # we show the thread skeleton at the bottom, instead.
438 my $refs = $hdr->header_raw('References');
440 # avoid redundant URLs wasting bandwidth
442 $seen{$irt} = 1 if defined $irt;
444 my @raw_refs = ($refs =~ /<([^>]+)>/g);
445 foreach my $ref (@raw_refs) {
448 push @refs, linkify_ref_nosrch($ref);
452 $rv .= 'References: '. join("\n\t", @refs) . "\n";
458 sub squote_maybe ($) {
460 if ($val =~ m{([^\w@\./,\%\+\-])}) {
461 $val =~ s/(['!])/'\\$1'/g; # '!' for csh
467 sub mailto_arg_link {
469 my %cc; # everyone else
470 my $to; # this is the From address
472 foreach my $h (qw(From To Cc)) {
473 my $v = $hdr->header($h);
474 defined($v) && ($v ne '') or next;
475 my @addrs = PublicInbox::Address::emails($v);
476 foreach my $address (@addrs) {
477 my $dst = lc($address);
478 $cc{$dst} ||= $address;
484 my $subj = $hdr->header('Subject') || '';
485 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
486 my $mid = $hdr->header_raw('Message-ID');
487 push @arg, '--in-reply-to='.ascii_html(squote_maybe(mid_clean($mid)));
488 my $irt = uri_escape_utf8($mid);
490 push @arg, '--to=' . ascii_html($to);
491 $to = uri_escape_utf8($to);
492 $subj = uri_escape_utf8($subj);
493 my $cc = join(',', sort values %cc);
494 push @arg, '--cc=' . ascii_html($cc);
495 $cc = uri_escape_utf8($cc);
496 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
503 my ($hdr, $standalone, $ctx, $rhref) = @_;
505 my $srch = $ctx->{srch} if $ctx;
508 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
512 thread_skel(\$idx, $ctx, $hdr, $tpfx);
513 my $p = $ctx->{parent_msg};
514 my $next = $ctx->{next_msg};
516 $p = PublicInbox::Hval->new_msgid($p);
518 $irt = "<a\nhref=\"$upfx$p/\"\nrel=prev>parent</a> ";
520 $irt = ' ' x length('parent ');
523 my $n = PublicInbox::Hval->new_msgid($next)->as_href;
524 $irt .= "<a\nhref=\"$upfx$n/\"\nrel=next>next</a> ";
526 $irt .= ' ' x length('next ');
532 $irt .= qq(<a\nhref="$rhref">reply</a>);
536 sub linkify_ref_nosrch {
537 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
538 my $html = $v->as_html;
539 my $href = $v->as_href;
540 "<<a\nhref=\"../$href/\">$html</a>>";
546 if ($id !~ /\A[a-f0-9]{40}\z/) {
547 $id = id_compress(mid_clean($id), 1);
552 sub thread_html_head {
553 my ($hdr, $state) = @_;
554 my $res = delete $state->{res} or die "BUG: no Plack callback in {res}";
555 my $fh = $res->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
558 my $s = ascii_html($hdr->header('Subject'));
559 $fh->write("<html><head><title>$s</title>".
560 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
561 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
562 PublicInbox::Hval::STYLE .
566 sub pre_anchor_entry {
567 my ($seen, $mime) = @_;
568 my $id = anchor_for(mid_mime($mime));
569 $seen->{$id} = "#$id"; # save the anchor for children, later
573 my ($upfx, $mid) = @_;
574 # 'subject dummy' is used internally by Mail::Thread
575 return '[no common parent]' if ($mid eq 'subject dummy');
577 $mid = PublicInbox::Hval->new_msgid($mid);
578 my $href = $mid->as_href;
579 my $html = $mid->as_html;
580 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
583 sub thread_adj_level {
584 my ($state, $level) = @_;
586 my $max = $state->{cur_level};
588 return '' if $max == 0; # flat output
590 # reset existing lists
591 my $x = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
592 $state->{fh}->write($x . '</ul>');
593 $state->{cur_level} = 0;
596 if ($level == $max) { # continue existing list
597 $state->{fh}->write('<li>');
598 } elsif ($level < $max) {
599 my $x = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
600 $state->{fh}->write($x .= '<li>');
601 $state->{cur_level} = $level;
602 } else { # ($level > $max) # start a new level
603 $state->{cur_level} = $level;
604 $state->{fh}->write(($max ? '<li>' : '') . '<ul><li>');
610 my ($state, $upfx, $mid, $level) = @_;
611 my $end = '<pre>'. ghost_parent($upfx, $mid) . '</pre>';
612 $state->{fh}->write($end .= thread_adj_level($state, $level));
616 my ($state, $mime, $level) = @_;
618 # lazy load the full message from mini_mime:
620 my $mid = mid_clean(mid_mime($mime));
621 $state->{ctx}->{-inbox}->msg_by_mid($mid);
623 $mime = Email::MIME->new($mime);
625 thread_html_head($mime, $state) if $state->{anchor_idx} == 0;
626 if (my $ghost = delete $state->{ghost}) {
627 # n.b. ghost messages may only be parents, not children
628 foreach my $g (@$ghost) {
629 ghost_flush($state, '../../', @$g);
632 my $end = thread_adj_level($state, $level);
633 index_entry($mime, $level, $state);
634 $state->{fh}->write($end) if $end;
641 INDENT x ($level - 1);
644 sub __ghost_prepare {
645 my ($state, $node, $level) = @_;
646 my $ghost = $state->{ghost} ||= [];
647 push @$ghost, [ $node->messageid, $level ];
651 my ($state, $level, $node) = @_;
652 if (my $mime = $node->message) {
653 unless (__thread_entry($state, $mime, $level)) {
654 __ghost_prepare($state, $node, $level);
657 __ghost_prepare($state, $node, $level);
664 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
669 my $ts = eval { str2time($hdr->header('Date')) };
670 defined($ts) ? $ts : 0;
675 require PublicInbox::Thread;
676 my $th = PublicInbox::Thread->new(@$msgs);
678 $th->order(*sort_ts);
683 my ($res, $ctx) = @_;
684 require PublicInbox::ExtMsg;
686 $res->(PublicInbox::ExtMsg::ext_msg($ctx))
691 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
695 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
698 my ($state, $hdr, $level) = @_;
700 my $dst = $state->{dst};
701 my $cur = $state->{cur};
702 my $mid = mid_clean($hdr->header_raw('Message-ID'));
703 my $f = ascii_html($hdr->header('X-PI-From'));
704 my $d = _msg_date($hdr);
705 my $pfx = "$d " . indent_for($level) . th_pfx($level);
707 $state->{first_level} ||= $level;
709 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
710 $state->{prev_attr} = $attr;
714 $state->{prev_level} = $level;
718 delete $state->{cur};
719 $$dst .= "$pfx<b><a\nid=r\nhref=\"#t\">".
720 "$attr [this message]</a></b>\n";
725 $state->{next_msg} ||= $mid;
728 # Subject is never undef, this mail was loaded from
729 # our Xapian which would've resulted in '' if it were
730 # really missing (and Filter rejects empty subjects)
731 my $s = $hdr->header('Subject');
732 my $h = $state->{srch}->subject_path($s);
733 if ($state->{seen}->{$h}) {
736 $state->{seen}->{$h} = 1;
737 $s = PublicInbox::Hval->new($s);
740 my $m = PublicInbox::Hval->new_msgid($mid);
741 $m = $state->{upfx} . $m->as_href . '/';
742 $$dst .= "$pfx<a\nhref=\"$m\">";
743 $$dst .= defined($s) ? "$s</a> $f\n" : "$f</a>\n";
747 my ($state, $level, $node) = @_;
748 if (my $mime = $node->message) {
749 my $hdr = $mime->header_obj;
750 my $mid = mid_clean($hdr->header_raw('Message-ID'));
751 _skel_header($state, $hdr, $level);
753 my $mid = $node->messageid;
754 my $dst = $state->{dst};
755 if ($mid eq 'subject dummy') {
756 $$dst .= "\t[no common parent]\n";
758 $$dst .= ' [not found] ';
759 $$dst .= indent_for($level) . th_pfx($level);
760 $mid = PublicInbox::Hval->new_msgid($mid);
761 my $href = $state->{upfx} . $mid->as_href . '/';
762 my $html = $mid->as_html;
763 $$dst .= qq{<<a\nhref="$href">$html</a>>\n};
770 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
771 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
775 sub _tryload_ghost ($$) {
776 my ($srch, $mid) = @_;
777 my $smsg = $srch->lookup_mail($mid) or return;
781 # accumulate recent topics if search is supported
782 # returns 1 if done, undef if not
784 my ($state, $level, $node) = @_;
785 my $srch = $state->{srch};
786 my $mid = $node->messageid;
787 my $x = $node->message || _tryload_ghost($srch, $mid);
791 $subj = $x->header('Subject');
792 $subj = $srch->subject_normalized($subj);
793 $ts = $x->header('X-PI-TS');
794 } else { # ghost message, do not bump level
798 if (++$state->{subjs}->{$subj} == 1) {
799 push @{$state->{order}}, [ $level, $subj ];
801 my $exist = $state->{latest}->{$subj};
802 if (!$exist || $exist->[1] < $ts) {
803 $state->{latest}->{$subj} = [ $mid, $ts ];
809 my $order = $state->{order};
810 my $subjs = $state->{subjs};
811 my $latest = $state->{latest};
812 my $fh = $state->{fh};
813 return $fh->write("\n[No topics in range]</pre>") unless scalar @$order;
819 while (defined(my $info = shift @$order)) {
820 my ($level, $subj) = @$info;
821 my $n = delete $subjs->{$subj};
822 my ($mid, $ts) = @{delete $latest->{$subj}};
823 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
824 $pfx = indent_for($level);
825 my $nl = $level == $prev ? "\n" : '';
830 $cur ||= [ $ts, '' ];
831 $cur->[0] = $ts if $ts > $cur->[0];
832 $cur->[1] .= $nl . $pfx . th_pfx($level);
833 if ($ts == -666) { # ghost
834 $cur->[1] .= ghost_parent('', $mid) . "\n";
835 next; # child will have mbox / atom link
838 $subj = PublicInbox::Hval->new($subj)->as_html;
839 $cur->[1] .= "<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
841 my $attr = " $ts UTC";
843 # $n isn't the total number of posts on the topic,
844 # just the number of posts in the current results window
845 $n = $n == 1 ? '' : " ($n+ messages)";
847 if ($level == 0 || $attr ne $prev_attr) {
848 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
849 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
850 $pfx .= INDENT if $level > 0;
851 $cur->[1] .= $pfx . $attr . $n . " - $mbox / $atom\n";
855 push @recent, $cur if $cur;
856 @recent = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @recent;
857 $fh->write(join('', @recent) . '</pre>');
860 sub emit_index_topics {
862 my ($off) = (($state->{ctx}->{cgi}->param('o') || '0') =~ /(\d+)/);
863 $state->{order} = [];
864 $state->{subjs} = {};
865 $state->{latest} = {};
867 my %opts = ( offset => $off, limit => $max * 4 );
868 while (scalar @{$state->{order}} < $max) {
869 my $sres = $state->{srch}->query('', \%opts);
870 my $nr = scalar @{$sres->{msgs}} or last;
871 $sres = load_results($sres);
872 walk_thread(thread_results($sres), $state, *add_topic);
873 $opts{offset} += $nr;