1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::View;
6 use URI::Escape qw/uri_escape_utf8/;
7 use Date::Parse qw/str2time/;
8 use Encode qw/find_encoding/;
9 use Encode::MIME::Header;
10 use Email::MIME::ContentType qw/parse_content_type/;
11 use PublicInbox::Hval;
12 use PublicInbox::MID qw/mid_clean mid_compress mid2path/;
13 use Digest::SHA qw/sha1_hex/;
17 # TODO: make these constants tunable
18 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
19 use constant MAX_TRUNC_LEN => 72;
20 use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
21 use constant T_ANCHOR => '#u';
22 use constant INDENT => ' ';
24 *ascii_html = *PublicInbox::Hval::ascii_html;
26 my $enc_utf8 = find_encoding('UTF-8');
30 my ($ctx, $mime, $full_pfx, $footer) = @_;
31 if (defined $footer) {
32 $footer = "\n" . $footer;
36 headers_to_html_header($mime, $full_pfx, $ctx) .
37 multipart_text_as_html($mime, $full_pfx) .
38 '</pre><hr />' . PRE_WRAP .
39 html_footer($mime, 1, $full_pfx, $ctx) .
41 '</pre></body></html>';
45 my ($class, $mime, $full_pfx) = @_;
47 PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
51 my ($header_obj) = @_;
52 my $irt = $header_obj->header('In-Reply-To');
54 return mid_clean($irt) if (defined $irt);
56 my $refs = $header_obj->header('References');
57 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
63 # this is already inside a <pre>
65 my ($fh, $mime, $level, $state) = @_;
66 my $midx = $state->{anchor_idx}++;
67 my $ctx = $state->{ctx};
68 my $srch = $ctx->{srch};
69 my ($prev, $next) = ($midx - 1, $midx + 1);
71 my $enc = enc_for($mime->header("Content-Type"));
72 my $subj = $mime->header('Subject');
73 my $header_obj = $mime->header_obj;
75 my $mid_raw = $header_obj->header('Message-ID');
76 my $id = anchor_for($mid_raw);
77 my $seen = $state->{seen};
78 $seen->{$id} = "#$id"; # save the anchor for children, later
80 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
81 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
82 my @from = Email::Address->parse($from);
83 $from = $from[0]->name;
85 $from = PublicInbox::Hval->new_oneline($from)->as_html;
86 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
87 my $more = 'permalink';
88 my $root_anchor = $state->{root_anchor} || '';
89 my $path = $root_anchor ? '../../' : '';
90 my $href = $mid->as_href;
91 my $irt = in_reply_to($header_obj);
92 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
95 my $t = $ctx->{flat} ? 'T' : 't';
96 $subj = "<a\nhref=\"${path}$href/$t/#u\">$subj</a>";
98 if ($root_anchor eq $id) {
99 $subj = "<u\nid=\"u\">$subj</u>";
102 my $ts = _msg_date($mime);
103 my $rv = "<table\nsummary=l$level><tr>";
105 $rv .= '<td><pre>' . (INDENT x $level) . '</pre></td>';
107 $rv .= "<td\nid=s$midx>" . PRE_WRAP;
108 $rv .= "<b\nid=\"$id\">$subj</b>\n";
109 $rv .= "- by $from @ $ts UTC - ";
110 $rv .= "<a\nhref=\"#s$next\">next</a>";
112 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
114 $fh->write($rv .= "\n\n");
116 my ($fhref, $more_ref);
117 my $mhref = "${path}$href/";
119 # show full message if it's our root message
120 if ($root_anchor ne $id) {
121 $fhref = "${path}$href/f/";
124 # scan through all parts, looking for displayable text
125 $mime->walk_parts(sub {
126 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
130 my $txt = "${path}$href/raw";
131 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
132 $rv .= html_footer($mime, 0, undef, $ctx);
135 unless (defined $parent_anchor) {
136 my $v = PublicInbox::Hval->new_msgid($irt, 1);
138 $parent_anchor = "${path}$v/";
140 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
144 $rv .= " [<a\nhref=\"${path}$href/t/#u\">threaded</a>" .
147 $rv .= " [<b>threaded</b>|" .
148 "<a\nhref=\"${path}$href/T/#u\">flat</a>]";
152 $fh->write($rv .= '</pre></td></tr></table>');
156 my ($ctx, $foot, $srch) = @_;
157 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
160 # only private functions below.
162 sub emit_thread_html {
163 my ($cb, $ctx, $foot, $srch) = @_;
164 my $mid = $ctx->{mid};
165 my $res = $srch->get_thread($mid);
166 my $msgs = load_results($res);
167 my $nr = scalar @$msgs;
168 return missing_thread($cb, $ctx) if $nr == 0;
169 my $flat = $ctx->{flat};
175 root_anchor => anchor_for($mid),
179 require PublicInbox::GitCatFile;
180 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
182 pre_anchor_entry($seen, $_) for (@$msgs);
183 __thread_entry(\$cb, $git, $state, $_, 0) for (@$msgs);
185 my $th = thread_results($msgs);
186 thread_entry(\$cb, $git, $state, $_, 0) for $th->rootset;
189 Email::Address->purge_cache;
191 # there could be a race due to a message being deleted in git
192 # but still being in the Xapian index:
193 return missing_thread($cb, $ctx) if ($orig_cb eq $cb);
195 my $final_anchor = $state->{anchor_idx};
196 my $next = "<a\nid=\"s$final_anchor\">";
197 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
198 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
199 $next .= "\ndownload thread: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
200 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
201 $cb->write("<hr />" . PRE_WRAP . $next . "\n\n". $foot .
202 "</pre></body></html>");
207 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
208 my $s = add_text_body($enc, $part, $part_nr, $fhref);
211 # drop the remainder of git patches, they're usually better
212 # to review when the full message is viewed
213 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
216 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
219 # kill any leading or trailing whitespace lines
224 # kill per-line trailing whitespace
225 $s =~ s/[ \t]+$//sgm;
226 $s .= "\n" unless $s =~ /\n\z/s;
232 my ($ct, $default) = @_;
233 $default ||= $enc_utf8;
234 defined $ct or return $default;
235 my $ct_parsed = parse_content_type($ct);
237 if (my $charset = $ct_parsed->{attributes}->{charset}) {
238 my $enc = find_encoding($charset);
245 sub multipart_text_as_html {
246 my ($mime, $full_pfx, $srch) = @_;
249 my $enc = enc_for($mime->header("Content-Type"));
251 # scan through all parts, looking for displayable text
252 $mime->walk_parts(sub {
254 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx, 1);
260 sub add_filename_line {
264 $fn = $enc->decode($fn);
266 $pad x= ($len/2) if ($len > 0);
267 "$pad " . ascii_html($fn) . " $pad\n";
270 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://
272 ?[\@\w\+\&\?\.\%\;/#=-]*)!x;
275 my ($link_map, $s) = @_;
278 # salt this, as this could be exploited to show
279 # links in the HTML which don't show up in the raw mail.
280 my $key = sha1_hex($url . $SALT);
281 $link_map->{$key} = $url;
288 my ($link_map, $s) = @_;
290 # Added "PI-LINK-" prefix to avoid false-positives on git commits
291 $s =~ s!\bPI-LINK-([a-f0-9]{40})\b!
293 my $url = $link_map->{$key};
295 $url = ascii_html($url);
296 "<a\nhref=\"$url\">$url</a>";
298 # false positive or somebody tried to mess with us
306 my ($quot, $n, $part_nr, $full_pfx, $final, $do_anchor) = @_;
309 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
312 my $rv = join('', map { linkify_1(\%l, $_) } @$quot);
314 $rv = ascii_html($rv);
315 return linkify_2(\%l, $rv);
318 # show a short snippet of quoted text and link to full version:
319 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
320 my $cur = join(' ', @$quot);
321 @$quot = split(/\s+/, $cur);
324 my $tmp = shift(@$quot);
325 my $len = length($tmp) + length($cur);
326 if ($len > MAX_TRUNC_LEN) {
331 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
333 $cur =~ s/ \z/ .../s;
334 $cur = ascii_html($cur);
336 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
338 # show everything in the full version with anchor from
339 # short version (see above)
341 my $rv .= join('', map { linkify_1(\%l, $_) } @$quot);
343 $rv = ascii_html($rv);
344 return linkify_2(\%l, $rv) unless $do_anchor;
346 "<a\nid=q${part_nr}_$nr></a>" . linkify_2(\%l, $rv);
351 my ($enc_msg, $part, $part_nr, $full_pfx, $do_anchor) = @_;
352 return '' if $part->subparts;
354 my $ct = $part->content_type;
355 # account for filter bugs...
356 if (defined $ct && $ct =~ m!\btext/[xh]+tml\b!i) {
360 my $enc = enc_for($ct, $enc_msg);
365 $s = $enc->decode($s);
366 my @lines = split(/^/m, $s);
370 my $fn = $part->filename;
371 defined($fn) or $fn = "part #" . ($$part_nr + 1);
372 $s .= add_filename_line($enc, $fn);
376 while (defined(my $cur = shift @lines)) {
378 # show the previously buffered quote inline
380 $s .= flush_quote(\@quot, \$n, $$part_nr,
381 $full_pfx, 0, $do_anchor);
386 $cur = linkify_1(\%l, $cur);
387 $cur = ascii_html($cur);
388 $s .= linkify_2(\%l, $cur);
394 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1,
397 $s .= "\n" unless $s =~ /\n\z/s;
402 sub headers_to_html_header {
403 my ($mime, $full_pfx, $ctx) = @_;
404 my $srch = $ctx->{srch} if $ctx;
407 my $header_obj = $mime->header_obj;
408 my $mid = $header_obj->header('Message-ID');
409 $mid = PublicInbox::Hval->new_msgid($mid);
410 my $mid_href = $mid->as_href;
411 foreach my $h (qw(From To Cc Subject Date)) {
412 my $v = $mime->header($h);
413 defined($v) && ($v ne '') or next;
414 $v = PublicInbox::Hval->new_oneline($v);
417 my @from = Email::Address->parse($v->raw);
418 $title[1] = ascii_html($from[0]->name);
419 } elsif ($h eq 'Subject') {
420 $title[0] = $v->as_html;
422 my $p = $full_pfx ? '' : '../';
423 $rv .= "$h: <a\nid=\"t\"\nhref=\"${p}t/#u\">";
424 $rv .= $v->as_html . "</a>\n";
428 $rv .= "$h: " . $v->as_html . "\n";
431 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
432 my $raw_ref = $full_pfx ? 'raw' : '../raw';
433 $rv .= "(<a\nhref=\"$raw_ref\">raw</a>)\n";
435 $rv .= "<a\nhref=\"#r\">References: [see below]</a>\n";
437 $rv .= _parent_headers_nosrch($header_obj);
441 ("<html><head><title>". join(' - ', @title) .
442 '</title></head><body>' . PRE_WRAP . $rv);
446 my ($dst, $ctx, $cur, $full_pfx) = @_;
447 my $srch = $ctx->{srch};
448 my $mid = mid_clean($cur->header('Message-ID'));
449 my $res = $srch->get_thread($mid);
450 my $nr = $res->{total};
453 $$dst .= "\n[no followups, yet]\n";
454 return (undef, in_reply_to($cur));
456 my $upfx = $full_pfx ? '' : '../';
458 $$dst .= "\n\n~$nr messages in thread: ".
459 "(<a\nhref=\"${upfx}t/#u\">expand</a>)\n";
460 my $subj = $srch->subject_path($cur->header('Subject'));
461 my $parent = in_reply_to($cur);
463 seen => { $subj => 1 },
466 parent_cmp => defined $parent ? $parent : '',
471 for (thread_results(load_results($res))->rootset) {
472 inline_dump($dst, $state, $upfx, $_, 0);
474 ($state->{next_msg}, $state->{parent});
477 sub _parent_headers_nosrch {
478 my ($header_obj) = @_;
481 my $irt = in_reply_to($header_obj);
483 my $v = PublicInbox::Hval->new_msgid($irt, 1);
484 my $html = $v->as_html;
485 my $href = $v->as_href;
486 $rv .= "In-Reply-To: <";
487 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
490 my $refs = $header_obj->header('References');
492 # avoid redundant URLs wasting bandwidth
494 $seen{$irt} = 1 if defined $irt;
496 my @raw_refs = ($refs =~ /<([^>]+)>/g);
497 foreach my $ref (@raw_refs) {
500 push @refs, linkify_ref_nosrch($ref);
504 $rv .= 'References: '. join(' ', @refs) . "\n";
511 my ($mime, $standalone, $full_pfx, $ctx) = @_;
512 my %cc; # everyone else
513 my $to; # this is the From address
515 foreach my $h (qw(From To Cc)) {
516 my $v = $mime->header($h);
517 defined($v) && ($v ne '') or next;
518 my @addrs = Email::Address->parse($v);
519 foreach my $recip (@addrs) {
520 my $address = $recip->address;
521 my $dst = lc($address);
522 $cc{$dst} ||= $address;
526 Email::Address->purge_cache if $standalone;
528 my $subj = $mime->header('Subject') || '';
529 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
530 my $mid = $mime->header('Message-ID');
531 my $irt = uri_escape_utf8($mid);
533 $to = uri_escape_utf8($to);
534 $subj = uri_escape_utf8($subj);
536 my $cc = uri_escape_utf8(join(',', sort values %cc));
537 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
539 my $srch = $ctx->{srch} if $ctx;
540 my $upfx = $full_pfx ? '../' : '../../';
541 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
543 my ($next, $p) = thread_inline(\$idx, $ctx, $mime, $full_pfx);
545 $p = PublicInbox::Hval->new_oneline($p);
547 $irt = "<a\nhref=\"$upfx$p/\">parent</a> ";
549 $irt = ' ' x length('parent ');
552 $irt .= "<a\nhref=\"$upfx$next/\">next</a> ";
560 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
563 sub linkify_ref_nosrch {
564 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
565 my $html = $v->as_html;
566 my $href = $v->as_href;
567 "<<a\nhref=\"../$href/\">$html</a>>";
573 if ($id !~ /\A[a-f0-9]{40}\z/) {
574 $id = mid_compress(mid_clean($id), 1);
579 sub thread_html_head {
580 my ($cb, $mime) = @_;
581 $$cb = $$cb->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
583 my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
585 $$cb->write("<html><head><title>$s</title></head><body>");
588 sub pre_anchor_entry {
589 my ($seen, $mime) = @_;
590 my $id = anchor_for($mime->header('Message-ID'));
591 $seen->{$id} = "#$id"; # save the anchor for children, later
595 my ($upfx, $mid) = @_;
596 # 'subject dummy' is used internally by Mail::Thread
597 return '[no common parent]' if ($mid eq 'subject dummy');
599 $mid = PublicInbox::Hval->new_msgid($mid);
600 my $href = $mid->as_href;
601 my $html = $mid->as_html;
602 qq{[parent not found: <<a\nhref="$upfx../$href/">$html</a>>]};
606 my ($cb, $git, $state, $mime, $level) = @_;
608 # lazy load the full message from mini_mime:
610 my $path = mid2path(mid_clean($mime->header('Message-ID')));
611 Email::MIME->new($git->cat_file('HEAD:'.$path));
614 if ($state->{anchor_idx} == 0) {
615 thread_html_head($cb, $mime, $state);
618 if (my $ghost = delete $state->{ghost}) {
619 # n.b. ghost messages may only be parents, not children
620 foreach my $g (@$ghost) {
621 $$cb->write("<table\nsummary=ghost><tr><td>" .
622 (INDENT x $g->[1]) . "</td><td>" .
623 PRE_WRAP . ghost_parent('../', $g->[0]) .
624 '</pre></td></table>');
627 index_entry($$cb, $mime, $level, $state);
632 my ($state, $node, $level) = @_;
633 my $ghost = $state->{ghost} ||= [];
634 push @$ghost, [ $node->messageid, $level ];
638 my ($cb, $git, $state, $node, $level) = @_;
640 if (my $mime = $node->message) {
641 unless (__thread_entry($cb, $git, $state, $mime, $level)) {
642 __ghost_entry($state, $node, $level);
645 __ghost_entry($state, $node, $level);
648 thread_entry($cb, $git, $state, $node->child, $level + 1);
649 thread_entry($cb, $git, $state, $node->next, $level);
655 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
660 my $ts = eval { str2time($mime->header('Date')) };
661 defined($ts) ? $ts : 0;
665 my ($msgs, $nosubject) = @_;
666 require PublicInbox::Thread;
667 my $th = PublicInbox::Thread->new(@$msgs);
669 $Mail::Thread::nosubject = $nosubject;
671 $th->order(*sort_ts);
677 require PublicInbox::ExtMsg;
679 $cb->(PublicInbox::ExtMsg::ext_msg($ctx))
684 my $ts = $mime->header('X-PI-TS') || msg_timestamp($mime);
685 POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
689 my ($dst, $state, $upfx, $mime, $level) = @_;
690 my $pfx = INDENT x ($level - 1);
691 my $dot = $level == 0 ? '' : '` ';
693 my $cur = $state->{cur};
694 my $mid = mid_clean($mime->header('Message-ID'));
695 my $f = $mime->header('X-PI-From');
696 my $d = _msg_date($mime);
697 $f = PublicInbox::Hval->new($f)->as_html;
698 $d = PublicInbox::Hval->new($d)->as_html;
699 my $attr = "$f @ $d";
700 $state->{first_level} ||= $level;
701 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
702 $state->{prev_attr} = $attr;
703 $attr = ' - ' . $attr;
704 $attr .= ' UTC' if $level >= $state->{first_level};
708 $state->{prev_level} = $level;
712 delete $state->{cur};
713 $$dst .= "$pfx$dot<b><a\nid=\"r\"\nhref=\"#t\">".
714 "[this message]</a></b>$attr\n";
719 $state->{next_msg} ||= $mid;
722 # Subject is never undef, this mail was loaded from
723 # our Xapian which would've resulted in '' if it were
724 # really missing (and Filter rejects empty subjects)
725 my $s = $mime->header('Subject');
726 my $h = $state->{srch}->subject_path($s);
727 if ($state->{seen}->{$h}) {
730 $state->{seen}->{$h} = 1;
731 $s = PublicInbox::Hval->new($s);
734 my $m = PublicInbox::Hval->new_msgid($mid);
735 $m = $upfx . '../' . $m->as_href . '/';
737 $$dst .= "$pfx$dot<a\nhref=\"$m\">$s</a>$attr\n";
739 $$dst .= "$pfx$dot<a\nhref=\"$m\">$f @ $d</a>\n";
744 my ($dst, $state, $upfx, $node, $level) = @_;
746 if (my $mime = $node->message) {
747 my $mid = mid_clean($mime->header('Message-ID'));
748 if ($mid eq $state->{parent_cmp}) {
749 $state->{parent} = $mid;
751 _inline_header($dst, $state, $upfx, $mime, $level);
753 my $dot = $level == 0 ? '' : '` ';
754 my $pfx = (INDENT x $level) . $dot;
755 $$dst .= $pfx . ghost_parent($upfx, $node->messageid) . "\n";
757 inline_dump($dst, $state, $upfx, $node->child, $level+1);
758 inline_dump($dst, $state, $upfx, $node->next, $level);
763 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
764 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
770 (eval { $b->topmost->message->header('X-PI-TS') } || 0) <=>
771 (eval { $a->topmost->message->header('X-PI-TS') } || 0)
775 # accumulate recent topics if search is supported
776 # returns 1 if done, undef if not
778 my ($state, $node, $level) = @_;
780 my $child_adjust = 1;
782 if (my $x = $node->message) {
786 $subj = $x->header('Subject');
787 $subj = $state->{srch}->subject_normalized($subj);
790 # kill "[PATCH v2]" etc. for summarization
791 $topic =~ s/\A\s*\[[^\]]+\]\s*//g;
792 $topic = substr($topic, 0, 30);
794 if (++$state->{subjs}->{$topic} == 1) {
795 push @{$state->{order}}, [ $level, $subj, $topic ];
798 my $mid = mid_clean($x->header('Message-ID'));
800 my $u = $x->header('X-PI-From');
801 my $ts = $x->header('X-PI-TS');
802 $state->{latest}->{$topic} = [ $mid, $u, $ts ];
804 # ghost message, do not bump level
808 add_topic($state, $node->child, $level + $child_adjust);
809 add_topic($state, $node->next, $level);
814 my $order = $state->{order};
815 my $subjs = $state->{subjs};
816 my $latest = $state->{latest};
817 return "\n[No recent topics]</pre>" unless (scalar @$order);
822 while (defined(my $info = shift @$order)) {
823 my ($level, $subj, $topic) = @$info;
824 my $n = delete $subjs->{$topic};
825 my ($mid, $u, $ts) = @{delete $latest->{$topic}};
826 $mid = PublicInbox::Hval->new($mid)->as_href;
827 $subj = PublicInbox::Hval->new($subj)->as_html;
828 $u = PublicInbox::Hval->new($u)->as_html;
829 $pfx = INDENT x ($level - 1);
830 my $nl = $level == $prev ? "\n" : '';
831 my $dot = $level == 0 ? '' : '` ';
832 $dst .= "$nl$pfx$dot<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
835 $ts = POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
837 $attr = "created by $u @ $ts UTC";
840 # $n isn't the total number of posts on the topic,
841 # just the number of posts in the current results
842 # window, so leave it unlabeled
843 $attr = "updated by $u @ $ts UTC";
846 if ($level == 0 || $attr ne $prev_attr) {
847 $pfx .= INDENT if $level > 0;
848 $dst .= "$pfx- ". $attr . $n;
855 sub emit_index_topics {
856 my ($state, $fh) = @_;
857 my $off = $state->{ctx}->{cgi}->param('o');
858 $off = 0 unless defined $off;
859 $state->{order} = [];
860 $state->{subjs} = {};
861 $state->{latest} = {};
863 my %opts = ( offset => int $off, limit => $max * 4 );
864 while (scalar @{$state->{order}} < $max) {
865 my $res = $state->{srch}->query('', \%opts);
866 my $nr = scalar @{$res->{msgs}} or last;
868 for (rsort_ts(thread_results(load_results($res), 1)->rootset)) {
869 add_topic($state, $_, 0);
871 $opts{offset} += $nr;
874 $fh->write(dump_topics($state));