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 qw/find_encoding/;
12 use Encode::MIME::Header;
13 use Email::MIME::ContentType qw/parse_content_type/;
14 use PublicInbox::Hval qw/ascii_html/;
15 use PublicInbox::Linkify;
16 use PublicInbox::MID qw/mid_clean id_compress mid2path mid_mime/;
17 use PublicInbox::MsgIter;
20 use constant INDENT => ' ';
21 use constant TCHILD => '` ';
22 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
26 my ($ctx, $mime, $footer) = @_;
27 $footer = defined($footer) ? "\n$footer" : '';
28 my $hdr = $mime->header_obj;
29 headers_to_html_header($hdr, $ctx) .
30 multipart_text_as_html($mime) .
32 html_footer($hdr, 1, $ctx, 'R/') .
34 '</pre></body></html>';
37 # /$INBOX/$MESSAGE_ID/R/
39 my ($ctx, $hdr, $footer) = @_;
40 my $s = $hdr->header('Subject');
41 $s = '(no subject)' if (!defined $s) || ($s eq '');
42 my $f = $hdr->header('From');
43 $f = '' unless defined $f;
44 my $mid = $hdr->header_raw('Message-ID');
45 $mid = PublicInbox::Hval->new_msgid($mid);
46 my $t = ascii_html($s);
48 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
50 my ($arg, $link) = mailto_arg_link($hdr);
51 push @$arg, '/path/to/YOUR_REPLY';
53 "<html><head><title>replying to \"$t\"</title></head><body><pre>" .
54 "replying to message:\n\n" .
55 "Subject: <b>$t</b>\n" .
56 "From: ". ascii_html($f) .
57 "\nDate: " . ascii_html($hdr->header('Date')) .
58 "\nMessage-ID: <" . $mid->as_html . ">\n\n" .
59 "There are multiple ways to reply:\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 "\tgit send-email \\\n\t\t" .
65 join(" \\ \n\t\t", @$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) .
70 "\nFor context, the original <a\nhref=../>message</a> or " .
71 qq(<a\nhref="../t/#u">thread</a>) .
72 '</pre><hr /><pre>' . $footer . '</pre></body></html>';
77 my $irt = $hdr->header_raw('In-Reply-To');
79 return mid_clean($irt) if (defined $irt);
81 my $refs = $hdr->header_raw('References');
82 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
88 # this is already inside a <pre>
90 my ($mime, $level, $state) = @_;
91 my $midx = $state->{anchor_idx}++;
92 my $ctx = $state->{ctx};
93 my $srch = $ctx->{srch};
94 my $hdr = $mime->header_obj;
95 my $subj = $hdr->header('Subject');
97 my $mid_raw = mid_clean(mid_mime($mime));
98 my $id = anchor_for($mid_raw);
99 my $seen = $state->{seen};
100 $seen->{$id} = "#$id"; # save the anchor for children, later
102 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
103 my $from = $hdr->header('From');
104 my @from = Email::Address->parse($from);
105 $from = $from[0]->name;
107 my $root_anchor = $state->{root_anchor} || '';
108 my $path = $root_anchor ? '../../' : '';
109 my $href = $mid->as_href;
110 my $irt = in_reply_to($hdr);
111 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
113 $from = ascii_html($from);
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 $fh->write($rv .= "- $from @ $ts UTC (<a\nhref=\"$txt\">raw</a>)\n\n");
125 my $mhref = "${path}$href/";
127 # scan through all parts, looking for displayable text
128 msg_iter($mime, sub { index_walk($fh, $_[0]) });
129 $rv = "\n" . html_footer($hdr, 0, $ctx, "$path$href/R/");
132 unless (defined $parent_anchor) {
133 my $v = PublicInbox::Hval->new_msgid($irt, 1);
135 $parent_anchor = "${path}$v/";
137 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
139 if (my $pct = $state->{pct}) { # used by SearchView.pm
140 $rv .= " [relevance $pct->{$mid_raw}%]";
142 my $threaded = 'threaded';
146 $flat = "<b>$flat</b>";
147 $end = "\n"; # for lynx
149 $threaded = "<b>$threaded</b>";
151 $rv .= " [<a\nhref=\"${path}$href/t/#u\">$threaded</a>";
152 $rv .= "|<a\nhref=\"${path}$href/T/#u\">$flat</a>]$end";
154 $fh->write($rv .= '</pre>');
158 my ($ctx, $foot, $srch) = @_;
159 # $_[0] in sub is the Plack callback
160 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
163 # only private functions below.
165 sub emit_thread_html {
166 my ($res, $ctx, $foot, $srch) = @_;
167 my $mid = $ctx->{mid};
168 my $flat = $ctx->{flat};
169 my $msgs = load_results($srch->get_thread($mid, { asc => $flat }));
170 my $nr = scalar @$msgs;
171 return missing_thread($res, $ctx) if $nr == 0;
177 root_anchor => anchor_for($mid),
182 require PublicInbox::Git;
183 $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
185 pre_anchor_entry($seen, $_) for (@$msgs);
186 __thread_entry($state, $_, 0) for (@$msgs);
188 my $th = thread_results($msgs);
189 thread_entry($state, $_, 0) for $th->rootset;
190 if (my $max = $state->{cur_level}) {
192 ('</ul></li>' x ($max - 1)) . '</ul>');
195 Email::Address->purge_cache;
197 # there could be a race due to a message being deleted in git
198 # but still being in the Xapian index:
199 my $fh = delete $state->{fh} or return missing_thread($res, $ctx);
201 my $final_anchor = $state->{anchor_idx};
202 my $next = "<a\nid=s$final_anchor>";
203 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
204 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
205 $next .= "\ndownload thread: ";
206 $next .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
207 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
208 $fh->write('<hr /><pre>' . $next . "\n\n".
209 $foot . '</pre></body></html>');
215 my $s = add_text_body($p);
219 $s .= "\n"; # ensure there's a trailing newline
224 sub multipart_text_as_html {
228 # scan through all parts, looking for displayable text
229 msg_iter($mime, sub {
231 $p = add_text_body($p);
233 $rv .= "\n" if $p ne '';
239 my ($s, $l, $quot) = @_;
241 # show everything in the full version with anchor from
242 # short version (see above)
243 my $rv = $l->linkify_1(join('', @$quot));
246 # we use a <div> here to allow users to specify their own
247 # color for quoted text
248 $rv = $l->linkify_2(ascii_html($rv));
249 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
252 sub attach_link ($$$) {
253 my ($ct, $p, $fn) = @_;
254 my ($part, $depth, @idx) = @$p;
255 my $nl = $idx[-1] > 1 ? "\n" : '';
256 my $idx = join('.', @idx);
257 my $size = bytes::length($part->body);
258 $ct ||= 'text/plain';
259 $ct =~ s/;.*//; # no attributes
260 $ct = ascii_html($ct);
261 my $desc = $part->header('Content-Description');
262 $desc = $fn unless defined $desc;
263 $desc = '' unless defined $desc;
264 $desc = ': '.$desc if $desc;
265 "$nl<b>[-- Attachment #$idx$desc --]\n" .
266 "[-- Type: $ct, Size: $size bytes --]</b>"
270 my ($p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
271 my ($part, $depth, @idx) = @$p;
272 my $ct = $part->content_type;
273 my $fn = $part->filename;
275 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
276 return attach_link($ct, $p, $fn);
279 my $s = eval { $part->body_str };
281 # badly-encoded message? tell the world about it!
282 return attach_link($ct, $p, $fn) if $@;
284 my @lines = split(/^/m, $s);
286 if (defined($fn) || $depth > 1 || $idx[0] > 1) {
287 $s .= attach_link($ct, $p, $fn);
291 my $l = PublicInbox::Linkify->new;
292 while (defined(my $cur = shift @lines)) {
294 # show the previously buffered quote inline
295 flush_quote(\$s, $l, \@quot) if @quot;
298 $cur = $l->linkify_1($cur);
299 $cur = ascii_html($cur);
300 $s .= $l->linkify_2($cur);
306 flush_quote(\$s, $l, \@quot) if @quot;
307 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
308 $s =~ s/\A\n+//s; # kill leading blank lines
309 $s =~ s/\s+\z//s; # kill all trailing spaces (final "\n" added if ne '')
313 sub headers_to_html_header {
314 my ($hdr, $ctx) = @_;
315 my $srch = $ctx->{srch} if $ctx;
321 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
322 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
326 my $mid = $hdr->header_raw('Message-ID');
327 $mid = PublicInbox::Hval->new_msgid($mid);
328 foreach my $h (qw(From To Cc Subject Date)) {
329 my $v = $hdr->header($h);
330 defined($v) && ($v ne '') or next;
331 $v = PublicInbox::Hval->new($v);
334 my @from = Email::Address->parse($v->raw);
335 $title[1] = ascii_html($from[0]->name);
336 } elsif ($h eq 'Subject') {
337 $title[0] = $v->as_html;
339 $rv .= qq($h: <a\nhref="#r"\nid=t>);
340 $rv .= $v->as_html . "</a>\n";
344 $rv .= "$h: " . $v->as_html . "\n";
347 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
348 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
349 $rv .= _parent_headers($hdr, $srch);
352 ("<html><head><title>". join(' - ', @title) . "</title>$atom".
353 PublicInbox::Hval::STYLE .
354 "</head><body><pre\nid=b>" . # anchor for body start
359 my ($dst, $ctx, $hdr, $tpfx) = @_;
360 my $srch = $ctx->{srch};
361 my $mid = mid_clean($hdr->header_raw('Message-ID'));
362 my $sres = $srch->get_thread($mid);
363 my $nr = $sres->{total};
364 my $expand = qq(<a\nhref="${tpfx}t/#u">expand</a> ) .
365 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
366 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
368 my $parent = in_reply_to($hdr);
370 if (defined $parent) {
371 $$dst .= "($expand)\n ";
372 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
374 $$dst .= "[no followups, yet] ($expand)\n";
376 $ctx->{next_msg} = undef;
377 $ctx->{parent_msg} = $parent;
381 $$dst .= "$nr+ messages in thread ($expand";
382 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
384 my $subj = $srch->subject_path($hdr->header('Subject'));
386 seen => { $subj => 1 },
392 for (thread_results(load_results($sres))->rootset) {
393 skel_dump($dst, $state, $tpfx, $_, 0);
395 $ctx->{next_msg} = $state->{next_msg};
396 $ctx->{parent_msg} = $parent;
399 sub _parent_headers {
400 my ($hdr, $srch) = @_;
403 my $irt = in_reply_to($hdr);
405 my $v = PublicInbox::Hval->new_msgid($irt, 1);
406 my $html = $v->as_html;
407 my $href = $v->as_href;
408 $rv .= "In-Reply-To: <";
409 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
412 # do not display References: if search is present,
413 # we show the thread skeleton at the bottom, instead.
416 my $refs = $hdr->header_raw('References');
418 # avoid redundant URLs wasting bandwidth
420 $seen{$irt} = 1 if defined $irt;
422 my @raw_refs = ($refs =~ /<([^>]+)>/g);
423 foreach my $ref (@raw_refs) {
426 push @refs, linkify_ref_nosrch($ref);
430 $rv .= 'References: '. join(' ', @refs) . "\n";
436 sub mailto_arg_link {
438 my %cc; # everyone else
439 my $to; # this is the From address
441 foreach my $h (qw(From To Cc)) {
442 my $v = $hdr->header($h);
443 defined($v) && ($v ne '') or next;
444 my @addrs = Email::Address->parse($v);
445 foreach my $recip (@addrs) {
446 my $address = $recip->address;
447 my $dst = lc($address);
448 $cc{$dst} ||= $address;
452 Email::Address->purge_cache;
455 my $subj = $hdr->header('Subject') || '';
456 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
457 my $mid = $hdr->header_raw('Message-ID');
458 push @arg, "--in-reply-to='" . ascii_html($mid) . "'";
459 my $irt = uri_escape_utf8($mid);
461 push @arg, '--to=' . ascii_html($to);
462 $to = uri_escape_utf8($to);
463 $subj = uri_escape_utf8($subj);
464 my $cc = join(',', sort values %cc);
465 push @arg, '--cc=' . ascii_html($cc);
466 $cc = uri_escape_utf8($cc);
467 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
474 my ($hdr, $standalone, $ctx, $rhref) = @_;
476 my $srch = $ctx->{srch} if $ctx;
479 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
483 thread_skel(\$idx, $ctx, $hdr, $tpfx);
484 my $p = $ctx->{parent_msg};
485 my $next = $ctx->{next_msg};
487 $p = PublicInbox::Hval->new_msgid($p);
489 $irt = "<a\nhref=\"$upfx$p/\"rel=prev>parent</a> ";
491 $irt = ' ' x length('parent ');
494 my $n = PublicInbox::Hval->new_msgid($next)->as_href;
495 $irt .= "<a\nhref=\"$upfx$n/\"\nrel=next>next</a> ";
497 $irt .= ' ' x length('next ');
503 $irt . qq(<a\nhref="$rhref">reply</a>) . $idx;
506 sub linkify_ref_nosrch {
507 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
508 my $html = $v->as_html;
509 my $href = $v->as_href;
510 "<<a\nhref=\"../$href/\">$html</a>>";
516 if ($id !~ /\A[a-f0-9]{40}\z/) {
517 $id = id_compress(mid_clean($id), 1);
522 sub thread_html_head {
523 my ($hdr, $state) = @_;
524 my $res = delete $state->{res} or die "BUG: no Plack callback in {res}";
525 my $fh = $res->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
528 my $s = ascii_html($hdr->header('Subject'));
529 $fh->write("<html><head><title>$s</title>".
530 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
531 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
532 PublicInbox::Hval::STYLE .
536 sub pre_anchor_entry {
537 my ($seen, $mime) = @_;
538 my $id = anchor_for(mid_mime($mime));
539 $seen->{$id} = "#$id"; # save the anchor for children, later
543 my ($upfx, $mid) = @_;
544 # 'subject dummy' is used internally by Mail::Thread
545 return '[no common parent]' if ($mid eq 'subject dummy');
547 $mid = PublicInbox::Hval->new_msgid($mid);
548 my $href = $mid->as_href;
549 my $html = $mid->as_html;
550 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
553 sub thread_adj_level {
554 my ($state, $level) = @_;
556 my $max = $state->{cur_level};
558 return '' if $max == 0; # flat output
560 # reset existing lists
561 my $x = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
562 $state->{fh}->write($x . '</ul>');
563 $state->{cur_level} = 0;
566 if ($level == $max) { # continue existing list
567 $state->{fh}->write('<li>');
568 } elsif ($level < $max) {
569 my $x = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
570 $state->{fh}->write($x .= '<li>');
571 $state->{cur_level} = $level;
572 } else { # ($level > $max) # start a new level
573 $state->{cur_level} = $level;
574 $state->{fh}->write(($max ? '<li>' : '') . '<ul><li>');
580 my ($state, $upfx, $mid, $level) = @_;
581 my $end = '<pre>'. ghost_parent($upfx, $mid) . '</pre>';
582 $state->{fh}->write($end .= thread_adj_level($state, $level));
586 my ($state, $mime, $level) = @_;
588 # lazy load the full message from mini_mime:
590 my $path = mid2path(mid_clean(mid_mime($mime)));
591 Email::MIME->new($state->{ctx}->{git}->cat_file('HEAD:'.$path));
594 thread_html_head($mime, $state) if $state->{anchor_idx} == 0;
595 if (my $ghost = delete $state->{ghost}) {
596 # n.b. ghost messages may only be parents, not children
597 foreach my $g (@$ghost) {
598 ghost_flush($state, '../../', @$g);
601 my $end = thread_adj_level($state, $level);
602 index_entry($mime, $level, $state);
603 $state->{fh}->write($end) if $end;
610 INDENT x ($level - 1);
613 sub __ghost_prepare {
614 my ($state, $node, $level) = @_;
615 my $ghost = $state->{ghost} ||= [];
616 push @$ghost, [ $node->messageid, $level ];
620 my ($state, $node, $level) = @_;
622 if (my $mime = $node->message) {
623 unless (__thread_entry($state, $mime, $level)) {
624 __ghost_prepare($state, $node, $level);
627 __ghost_prepare($state, $node, $level);
630 thread_entry($state, $node->child, $level + 1);
631 thread_entry($state, $node->next, $level);
637 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
642 my $ts = eval { str2time($hdr->header('Date')) };
643 defined($ts) ? $ts : 0;
648 require PublicInbox::Thread;
649 my $th = PublicInbox::Thread->new(@$msgs);
651 $th->order(*sort_ts);
656 my ($res, $ctx) = @_;
657 require PublicInbox::ExtMsg;
659 $res->(PublicInbox::ExtMsg::ext_msg($ctx))
664 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
668 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
671 my ($dst, $state, $upfx, $hdr, $level) = @_;
673 my $cur = $state->{cur};
674 my $mid = mid_clean($hdr->header_raw('Message-ID'));
675 my $f = ascii_html($hdr->header('X-PI-From'));
676 my $d = _msg_date($hdr);
677 my $pfx = "$d " . indent_for($level) . th_pfx($level);
679 $state->{first_level} ||= $level;
681 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
682 $state->{prev_attr} = $attr;
686 $state->{prev_level} = $level;
690 delete $state->{cur};
691 $$dst .= "$pfx<b><a\nid=r\nhref=\"#t\">".
692 "$attr [this message]</a></b>\n";
697 $state->{next_msg} ||= $mid;
700 # Subject is never undef, this mail was loaded from
701 # our Xapian which would've resulted in '' if it were
702 # really missing (and Filter rejects empty subjects)
703 my $s = $hdr->header('Subject');
704 my $h = $state->{srch}->subject_path($s);
705 if ($state->{seen}->{$h}) {
708 $state->{seen}->{$h} = 1;
709 $s = PublicInbox::Hval->new($s);
712 my $m = PublicInbox::Hval->new_msgid($mid);
713 $m = $upfx . '../' . $m->as_href . '/';
714 $$dst .= "$pfx<a\nhref=\"$m\">";
715 $$dst .= defined($s) ? "$s</a> $f\n" : "$f</a>\n";
719 my ($dst, $state, $upfx, $node, $level) = @_;
721 if (my $mime = $node->message) {
722 my $hdr = $mime->header_obj;
723 my $mid = mid_clean($hdr->header_raw('Message-ID'));
724 _skel_header($dst, $state, $upfx, $hdr, $level);
726 my $mid = $node->messageid;
727 if ($mid eq 'subject dummy') {
728 $$dst .= "\t[no common parent]\n";
730 $$dst .= ' [not found] ';
731 $$dst .= indent_for($level) . th_pfx($level);
732 $mid = PublicInbox::Hval->new_msgid($mid);
733 my $href = "$upfx../" . $mid->as_href . '/';
734 my $html = $mid->as_html;
735 $$dst .= qq{<<a\nhref="$href">$html</a>>\n};
738 skel_dump($dst, $state, $upfx, $node->child, $level+1);
739 skel_dump($dst, $state, $upfx, $node->next, $level);
744 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
745 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
749 # accumulate recent topics if search is supported
750 # returns 1 if done, undef if not
752 my ($state, $node, $level) = @_;
754 my $child_adjust = 1;
756 if (my $x = $node->message) {
760 $subj = $x->header('Subject');
761 $subj = $state->{srch}->subject_normalized($subj);
763 if (++$state->{subjs}->{$subj} == 1) {
764 push @{$state->{order}}, [ $level, $subj ];
767 my $mid = mid_clean($x->header_raw('Message-ID'));
769 my $ts = $x->header('X-PI-TS');
770 my $exist = $state->{latest}->{$subj};
771 if (!$exist || $exist->[1] < $ts) {
772 $state->{latest}->{$subj} = [ $mid, $ts ];
775 # ghost message, do not bump level
779 add_topic($state, $node->child, $level + $child_adjust);
780 add_topic($state, $node->next, $level);
785 my $order = $state->{order};
786 my $subjs = $state->{subjs};
787 my $latest = $state->{latest};
788 my $fh = $state->{fh};
789 return $fh->write("\n[No topics in range]</pre>") unless scalar @$order;
795 while (defined(my $info = shift @$order)) {
796 my ($level, $subj) = @$info;
797 my $n = delete $subjs->{$subj};
798 my ($mid, $ts) = @{delete $latest->{$subj}};
799 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
800 $subj = PublicInbox::Hval->new($subj)->as_html;
801 $pfx = indent_for($level);
802 my $nl = $level == $prev ? "\n" : '';
807 $cur ||= [ $ts, '' ];
808 $cur->[0] = $ts if $ts > $cur->[0];
809 $cur->[1] .= $nl . $pfx . th_pfx($level) .
810 "<a\nhref=\"$mid/t/#u\"><b>" .
811 $subj . "</b></a>\n";
814 my $attr = " $ts UTC";
816 # $n isn't the total number of posts on the topic,
817 # just the number of posts in the current results window
818 $n = $n == 1 ? '' : " ($n+ messages)";
820 if ($level == 0 || $attr ne $prev_attr) {
821 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
822 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
823 $pfx .= INDENT if $level > 0;
824 $cur->[1] .= $pfx . $attr . $n . " - $mbox / $atom\n";
828 push @recent, $cur if $cur;
829 @recent = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @recent;
830 $fh->write(join('', @recent) . '</pre>');
833 sub emit_index_topics {
835 my $off = $state->{ctx}->{cgi}->param('o');
836 $off = 0 unless defined $off;
837 $state->{order} = [];
838 $state->{subjs} = {};
839 $state->{latest} = {};
841 my %opts = ( offset => int $off, limit => $max * 4 );
842 while (scalar @{$state->{order}} < $max) {
843 my $sres = $state->{srch}->query('', \%opts);
844 my $nr = scalar @{$sres->{msgs}} or last;
846 for (thread_results(load_results($sres))->rootset) {
847 add_topic($state, $_, 0);
849 $opts{offset} += $nr;