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;
15 use PublicInbox::MID qw/mid_clean id_compress mid2path/;
16 use Digest::SHA qw/sha1_hex/;
20 # TODO: make these constants tunable
21 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
22 use constant MAX_TRUNC_LEN => 72;
23 use constant T_ANCHOR => '#u';
24 use constant INDENT => ' ';
26 *ascii_html = *PublicInbox::Hval::ascii_html;
28 my $enc_utf8 = find_encoding('UTF-8');
32 my ($ctx, $mime, $full_pfx, $footer) = @_;
33 if (defined $footer) {
34 $footer = "\n" . $footer;
38 headers_to_html_header($mime, $full_pfx, $ctx) .
39 multipart_text_as_html($mime, $full_pfx) .
41 html_footer($mime, 1, $full_pfx, $ctx) .
43 '</pre></body></html>';
47 my ($class, $mime, $full_pfx) = @_;
49 # no <head> here for <style>...
50 PublicInbox::Hval::PRE .
51 multipart_text_as_html($mime, $full_pfx) . '</pre>';
55 my ($header_obj) = @_;
56 my $irt = $header_obj->header('In-Reply-To');
58 return mid_clean($irt) if (defined $irt);
60 my $refs = $header_obj->header('References');
61 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
67 # this is already inside a <pre>
69 my ($fh, $mime, $level, $state) = @_;
70 my $midx = $state->{anchor_idx}++;
71 my $ctx = $state->{ctx};
72 my $srch = $ctx->{srch};
73 my ($prev, $next) = ($midx - 1, $midx + 1);
75 my $enc = enc_for($mime->header("Content-Type"));
76 my $subj = $mime->header('Subject');
77 my $header_obj = $mime->header_obj;
79 my $mid_raw = mid_clean($header_obj->header('Message-ID'));
80 my $id = anchor_for($mid_raw);
81 my $seen = $state->{seen};
82 $seen->{$id} = "#$id"; # save the anchor for children, later
84 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
85 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
86 my @from = Email::Address->parse($from);
87 $from = $from[0]->name;
89 $from = PublicInbox::Hval->new_oneline($from)->as_html;
90 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
91 my $root_anchor = $state->{root_anchor} || '';
92 my $path = $root_anchor ? '../../' : '';
93 my $href = $mid->as_href;
94 my $irt = in_reply_to($header_obj);
95 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
98 my $t = $ctx->{flat} ? 'T' : 't';
99 $subj = "<a\nhref=\"${path}$href/$t/#u\">$subj</a>";
101 if ($root_anchor eq $id) {
102 $subj = "<u\nid=u>$subj</u>";
105 my $ts = _msg_date($mime);
106 my $rv = "<pre\nid=s$midx>";
107 $rv .= "<b\nid=$id>$subj</b>\n";
108 $rv .= "- $from @ $ts UTC - ";
109 $rv .= "<a\nhref=\"#s$next\">next</a>";
111 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
113 $fh->write($rv .= "\n\n");
115 my ($fhref, $more_ref);
116 my $mhref = "${path}$href/";
117 my $more = 'permalink';
119 # show full message if it's our root message
120 my $neq = $root_anchor ne $id;
121 if ($neq || ($neq && $level != 0 && !$ctx->{flat})) {
122 $fhref = "${path}$href/f/";
125 # scan through all parts, looking for displayable text
126 $mime->walk_parts(sub {
127 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
131 my $txt = "${path}$href/raw";
132 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
133 $rv .= html_footer($mime, 0, undef, $ctx);
136 unless (defined $parent_anchor) {
137 my $v = PublicInbox::Hval->new_msgid($irt, 1);
139 $parent_anchor = "${path}$v/";
141 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
143 if (my $pct = $state->{pct}) { # used by SearchView.pm
144 $rv .= " [relevance $pct->{$mid_raw}%]";
147 $rv .= " [<a\nhref=\"${path}$href/t/#u\">threaded</a>" .
150 $rv .= " [<b>threaded</b>|" .
151 "<a\nhref=\"${path}$href/T/#u\">flat</a>]";
154 $fh->write($rv .= '</pre>');
158 my ($ctx, $foot, $srch) = @_;
159 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
162 # only private functions below.
164 sub emit_thread_html {
165 my ($cb, $ctx, $foot, $srch) = @_;
166 my $mid = $ctx->{mid};
167 my $res = $srch->get_thread($mid);
168 my $msgs = load_results($res);
169 my $nr = scalar @$msgs;
170 return missing_thread($cb, $ctx) if $nr == 0;
171 my $flat = $ctx->{flat};
177 root_anchor => anchor_for($mid),
182 require PublicInbox::Git;
183 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
185 pre_anchor_entry($seen, $_) for (@$msgs);
186 __thread_entry(\$cb, $git, $state, $_, 0) for (@$msgs);
188 my $th = thread_results($msgs);
189 thread_entry(\$cb, $git, $state, $_, 0) for $th->rootset;
190 if (my $max = $state->{cur_level}) {
191 $cb->write(('</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 return missing_thread($cb, $ctx) if ($orig_cb eq $cb);
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 $cb->write('<hr /><pre>' . $next . "\n\n".
209 $foot . '</pre></body></html>');
214 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
215 my $s = add_text_body($enc, $part, $part_nr, $fhref);
219 # drop the remainder of git patches, they're usually better
220 # to review when the full message is viewed
221 $s =~ s!^---+\n.*\z!!ms and $m = 1;
224 $s =~ s/^-- \n.*\z//ms and $m = 1;
225 $$more = "<b>More...</b>\n\n$$more" if $m;
228 # kill any leading or trailing whitespace lines
233 # kill per-line trailing whitespace
234 $s =~ s/[ \t]+$//sgm;
235 $s .= "\n" unless $s =~ /\n\z/s;
241 my ($ct, $default) = @_;
242 $default ||= $enc_utf8;
243 defined $ct or return $default;
244 my $ct_parsed = parse_content_type($ct);
246 if (my $charset = $ct_parsed->{attributes}->{charset}) {
247 my $enc = find_encoding($charset);
254 sub multipart_text_as_html {
255 my ($mime, $full_pfx, $srch) = @_;
258 my $enc = enc_for($mime->header("Content-Type"));
260 # scan through all parts, looking for displayable text
261 $mime->walk_parts(sub {
263 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx, 1);
269 sub add_filename_line {
273 $fn = $enc->decode($fn);
275 $pad x= ($len/2) if ($len > 0);
276 "$pad " . ascii_html($fn) . " $pad\n";
279 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://
281 ?[\@\w\+\&\?\.\%\;/#=-]*)!x;
284 my ($link_map, $s) = @_;
287 # salt this, as this could be exploited to show
288 # links in the HTML which don't show up in the raw mail.
289 my $key = sha1_hex($url . $SALT);
290 $link_map->{$key} = $url;
297 my ($link_map, $s) = @_;
299 # Added "PI-LINK-" prefix to avoid false-positives on git commits
300 $s =~ s!\bPI-LINK-([a-f0-9]{40})\b!
302 my $url = $link_map->{$key};
304 $url = ascii_html($url);
305 "<a\nhref=\"$url\">$url</a>";
307 # false positive or somebody tried to mess with us
315 my ($quot, $n, $part_nr, $full_pfx, $final, $do_anchor) = @_;
318 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
321 my $rv = join('', map { linkify_1(\%l, $_) } @$quot);
323 $rv = ascii_html($rv);
324 return linkify_2(\%l, $rv);
327 # show a short snippet of quoted text and link to full version:
328 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
329 my $cur = join(' ', @$quot);
330 @$quot = split(/\s+/, $cur);
333 my $tmp = shift(@$quot);
334 my $len = length($tmp) + length($cur);
335 if ($len > MAX_TRUNC_LEN) {
340 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
342 $cur =~ s/ \z/ .../s;
343 $cur = ascii_html($cur);
345 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
347 # show everything in the full version with anchor from
348 # short version (see above)
350 my $rv .= join('', map { linkify_1(\%l, $_) } @$quot);
352 $rv = ascii_html($rv);
353 return linkify_2(\%l, $rv) unless $do_anchor;
355 "<a\nid=q${part_nr}_$nr></a>" . linkify_2(\%l, $rv);
360 my ($enc_msg, $part, $part_nr, $full_pfx, $do_anchor) = @_;
361 return '' if $part->subparts;
363 my $ct = $part->content_type;
364 # account for filter bugs...
365 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
369 my $enc = enc_for($ct, $enc_msg);
374 $s = $enc->decode($s);
375 my @lines = split(/^/m, $s);
379 my $fn = $part->filename;
380 defined($fn) or $fn = "part #" . ($$part_nr + 1);
381 $s .= add_filename_line($enc, $fn);
385 while (defined(my $cur = shift @lines)) {
387 # show the previously buffered quote inline
389 $s .= flush_quote(\@quot, \$n, $$part_nr,
390 $full_pfx, 0, $do_anchor);
395 $cur = linkify_1(\%l, $cur);
396 $cur = ascii_html($cur);
397 $s .= linkify_2(\%l, $cur);
403 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1,
406 $s .= "\n" unless $s =~ /\n\z/s;
411 sub headers_to_html_header {
412 my ($mime, $full_pfx, $ctx) = @_;
413 my $srch = $ctx->{srch} if $ctx;
416 my $header_obj = $mime->header_obj;
417 my $mid = $header_obj->header('Message-ID');
418 $mid = PublicInbox::Hval->new_msgid($mid);
419 foreach my $h (qw(From To Cc Subject Date)) {
420 my $v = $header_obj->header($h);
421 defined($v) && ($v ne '') or next;
422 $v = PublicInbox::Hval->new_oneline($v);
425 my @from = Email::Address->parse($v->raw);
426 $title[1] = ascii_html($from[0]->name);
427 } elsif ($h eq 'Subject') {
428 $title[0] = $v->as_html;
430 $rv .= "$h: <b\nid=t>";
431 $rv .= $v->as_html . "</b>\n";
435 $rv .= "$h: " . $v->as_html . "\n";
438 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
439 my $upfx = $full_pfx ? '' : '../';
440 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
443 thread_inline(\$rv, $ctx, $mime, $upfx);
445 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
446 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
448 $rv .= _parent_headers_nosrch($header_obj);
453 ("<html><head><title>". join(' - ', @title) . "</title>$atom".
454 PublicInbox::Hval::STYLE . "</head><body><pre>" . $rv);
458 my ($dst, $ctx, $cur, $upfx) = @_;
459 my $srch = $ctx->{srch};
460 my $mid = mid_clean($cur->header('Message-ID'));
461 my $res = $srch->get_thread($mid);
462 my $nr = $res->{total};
463 my $expand = "<a\nhref=\"${upfx}t/#u\">expand</a> " .
464 "/ <a\nhref=\"${upfx}t.mbox.gz\">mbox.gz</a>";
467 my $parent = in_reply_to($cur);
469 $$dst .= "[no followups, yet] ($expand)\n";
470 $ctx->{next_msg} = undef;
471 $ctx->{parent_msg} = $parent;
475 $$dst .= "~$nr messages (<a\nhref=\"#b\">skip</a> / " .
478 my $subj = $srch->subject_path($cur->header('Subject'));
480 seen => { $subj => 1 },
483 parent_cmp => defined $parent ? $parent : '',
488 for (thread_results(load_results($res))->rootset) {
489 inline_dump($dst, $state, $upfx, $_, 0);
491 $$dst .= "<a\nid=b></a>"; # anchor for body start
492 $ctx->{next_msg} = $state->{next_msg};
493 $ctx->{parent_msg} = $state->{parent};
496 sub _parent_headers_nosrch {
497 my ($header_obj) = @_;
500 my $irt = in_reply_to($header_obj);
502 my $v = PublicInbox::Hval->new_msgid($irt, 1);
503 my $html = $v->as_html;
504 my $href = $v->as_href;
505 $rv .= "In-Reply-To: <";
506 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
509 my $refs = $header_obj->header('References');
511 # avoid redundant URLs wasting bandwidth
513 $seen{$irt} = 1 if defined $irt;
515 my @raw_refs = ($refs =~ /<([^>]+)>/g);
516 foreach my $ref (@raw_refs) {
519 push @refs, linkify_ref_nosrch($ref);
523 $rv .= 'References: '. join(' ', @refs) . "\n";
530 my ($mime, $standalone, $full_pfx, $ctx) = @_;
531 my %cc; # everyone else
532 my $to; # this is the From address
534 foreach my $h (qw(From To Cc)) {
535 my $v = $mime->header($h);
536 defined($v) && ($v ne '') or next;
537 my @addrs = Email::Address->parse($v);
538 foreach my $recip (@addrs) {
539 my $address = $recip->address;
540 my $dst = lc($address);
541 $cc{$dst} ||= $address;
545 Email::Address->purge_cache if $standalone;
547 my $subj = $mime->header('Subject') || '';
548 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
549 my $mid = $mime->header('Message-ID');
550 my $irt = uri_escape_utf8($mid);
552 $to = uri_escape_utf8($to);
553 $subj = uri_escape_utf8($subj);
555 my $cc = uri_escape_utf8(join(',', sort values %cc));
556 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
559 my $srch = $ctx->{srch} if $ctx;
560 my $upfx = $full_pfx ? '../' : '../../';
561 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
563 if ($srch && $standalone) {
564 $idx .= qq{ / follow: <a\nhref="t.atom">Atom feed</a>\n};
567 my $p = $ctx->{parent_msg};
568 my $next = $ctx->{next_msg};
570 $p = PublicInbox::Hval->new_oneline($p);
572 $irt = "<a\nhref=\"$upfx$p/\">parent</a> ";
574 $irt = ' ' x length('parent ');
577 $irt .= "<a\nhref=\"$upfx$next/\">next</a> ";
579 $irt .= ' ' x length('next ');
582 my $tpfx = $full_pfx ? '' : '../';
583 $irt .= "<a\nhref=\"${tpfx}t/#u\">thread</a> ";
585 $irt .= ' ' x length('thread ');
591 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
594 sub linkify_ref_nosrch {
595 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
596 my $html = $v->as_html;
597 my $href = $v->as_href;
598 "<<a\nhref=\"../$href/\">$html</a>>";
604 if ($id !~ /\A[a-f0-9]{40}\z/) {
605 $id = id_compress(mid_clean($id), 1);
610 sub thread_html_head {
611 my ($cb, $header, $state) = @_;
612 $$cb = $$cb->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
614 my $s = PublicInbox::Hval->new_oneline($header->header('Subject'));
616 $$cb->write("<html><head><title>$s</title>".
617 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
618 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
619 PublicInbox::Hval::STYLE .
623 sub pre_anchor_entry {
624 my ($seen, $mime) = @_;
625 my $id = anchor_for($mime->header('Message-ID'));
626 $seen->{$id} = "#$id"; # save the anchor for children, later
630 my ($upfx, $mid) = @_;
631 # 'subject dummy' is used internally by Mail::Thread
632 return '[no common parent]' if ($mid eq 'subject dummy');
634 $mid = PublicInbox::Hval->new_msgid($mid);
635 my $href = $mid->as_href;
636 my $html = $mid->as_html;
637 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
640 sub thread_adj_level {
641 my ($fh, $state, $level) = @_;
643 my $max = $state->{cur_level};
645 return '' if $max == 0; # flat output
647 # reset existing lists
648 my $x = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
649 $fh->write($x . '</ul>');
650 $state->{cur_level} = 0;
653 if ($level == $max) { # continue existing list
655 } elsif ($level < $max) {
656 my $x = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
657 $fh->write($x .= '<li>');
658 $state->{cur_level} = $level;
659 } else { # ($level > $max) # start a new level
660 $state->{cur_level} = $level;
661 $fh->write(($max ? '<li>' : '') . '<ul><li>');
667 my ($fh, $state, $upfx, $mid, $level) = @_;
669 my $end = thread_adj_level($fh, $state, $level);
670 $fh->write('<pre>'. ghost_parent($upfx, $mid) . '</pre>' . $end);
674 my ($cb, $git, $state, $mime, $level) = @_;
676 # lazy load the full message from mini_mime:
678 my $path = mid2path(mid_clean($mime->header('Message-ID')));
679 Email::MIME->new($git->cat_file('HEAD:'.$path));
682 if ($state->{anchor_idx} == 0) {
683 thread_html_head($cb, $mime, $state, $level);
686 if (my $ghost = delete $state->{ghost}) {
687 # n.b. ghost messages may only be parents, not children
688 foreach my $g (@$ghost) {
689 ghost_flush($fh, $state, '../../', @$g);
692 my $end = thread_adj_level($fh, $state, $level);
693 index_entry($fh, $mime, $level, $state);
694 $fh->write($end) if $end;
701 INDENT x ($level - 1);
704 sub __ghost_prepare {
705 my ($state, $node, $level) = @_;
706 my $ghost = $state->{ghost} ||= [];
707 push @$ghost, [ $node->messageid, $level ];
711 my ($cb, $git, $state, $node, $level) = @_;
713 if (my $mime = $node->message) {
714 unless (__thread_entry($cb, $git, $state, $mime, $level)) {
715 __ghost_prepare($state, $node, $level);
718 __ghost_prepare($state, $node, $level);
721 thread_entry($cb, $git, $state, $node->child, $level + 1);
722 thread_entry($cb, $git, $state, $node->next, $level);
728 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
733 my $ts = eval { str2time($mime->header('Date')) };
734 defined($ts) ? $ts : 0;
738 my ($msgs, $nosubject) = @_;
739 require PublicInbox::Thread;
740 my $th = PublicInbox::Thread->new(@$msgs);
742 $Mail::Thread::nosubject = $nosubject;
744 $th->order(*sort_ts);
750 require PublicInbox::ExtMsg;
752 $cb->(PublicInbox::ExtMsg::ext_msg($ctx))
757 my $ts = $mime->header('X-PI-TS') || msg_timestamp($mime);
761 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
764 my ($dst, $state, $upfx, $mime, $level) = @_;
765 my $dot = $level == 0 ? '' : '` ';
767 my $cur = $state->{cur};
768 my $mid = mid_clean($mime->header('Message-ID'));
769 my $f = $mime->header('X-PI-From');
770 my $d = _msg_date($mime);
771 $f = PublicInbox::Hval->new($f)->as_html;
772 $d = PublicInbox::Hval->new($d)->as_html;
773 my $pfx = ' ' . $d . ' ' . indent_for($level);
775 $state->{first_level} ||= $level;
777 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
778 $state->{prev_attr} = $attr;
782 $state->{prev_level} = $level;
786 delete $state->{cur};
787 $$dst .= "$pfx$dot<b><a\nid=r\nhref=\"#b\">".
788 "$attr [this message]</a></b>\n";
793 $state->{next_msg} ||= $mid;
796 # Subject is never undef, this mail was loaded from
797 # our Xapian which would've resulted in '' if it were
798 # really missing (and Filter rejects empty subjects)
799 my $s = $mime->header('Subject');
800 my $h = $state->{srch}->subject_path($s);
801 if ($state->{seen}->{$h}) {
804 $state->{seen}->{$h} = 1;
805 $s = PublicInbox::Hval->new($s);
808 my $m = PublicInbox::Hval->new_msgid($mid);
809 $m = $upfx . '../' . $m->as_href . '/';
811 $$dst .= "$pfx$dot<a\nhref=\"$m\">$s</a> $attr\n";
813 $$dst .= "$pfx$dot<a\nhref=\"$m\">$f</a>\n";
818 my ($dst, $state, $upfx, $node, $level) = @_;
820 if (my $mime = $node->message) {
821 my $mid = mid_clean($mime->header('Message-ID'));
822 if ($mid eq $state->{parent_cmp}) {
823 $state->{parent} = $mid;
825 _inline_header($dst, $state, $upfx, $mime, $level);
827 my $dot = $level == 0 ? '' : '` ';
828 my $pfx = (' ' x length(' 1970-01-01 13:37 ')).
829 indent_for($level) . $dot;
831 $$dst .= ghost_parent("$upfx../", $node->messageid) . "\n";
833 inline_dump($dst, $state, $upfx, $node->child, $level+1);
834 inline_dump($dst, $state, $upfx, $node->next, $level);
839 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
840 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
846 (eval { $b->topmost->message->header('X-PI-TS') } || 0) <=>
847 (eval { $a->topmost->message->header('X-PI-TS') } || 0)
851 # accumulate recent topics if search is supported
852 # returns 1 if done, undef if not
854 my ($state, $node, $level) = @_;
856 my $child_adjust = 1;
858 if (my $x = $node->message) {
862 $subj = $x->header('Subject');
863 $subj = $state->{srch}->subject_normalized($subj);
866 # kill "[PATCH v2]" etc. for summarization
867 unless ($level == 0) {
868 $topic =~ s/\A\s*\[[^\]]+\]\s*//g;
871 if (++$state->{subjs}->{$topic} == 1) {
872 push @{$state->{order}}, [ $level, $subj, $topic ];
875 my $mid = mid_clean($x->header('Message-ID'));
877 my $ts = $x->header('X-PI-TS');
878 my $exist = $state->{latest}->{$topic};
879 if (!$exist || $exist->[1] < $ts) {
880 $state->{latest}->{$topic} = [ $mid, $ts ];
883 # ghost message, do not bump level
887 add_topic($state, $node->child, $level + $child_adjust);
888 add_topic($state, $node->next, $level);
893 my $order = $state->{order};
894 my $subjs = $state->{subjs};
895 my $latest = $state->{latest};
896 return "\n[No topics in range]</pre>" unless (scalar @$order);
901 while (defined(my $info = shift @$order)) {
902 my ($level, $subj, $topic) = @$info;
903 my $n = delete $subjs->{$topic};
904 my ($mid, $ts) = @{delete $latest->{$topic}};
905 $mid = PublicInbox::Hval->new($mid)->as_href;
906 $subj = PublicInbox::Hval->new($subj)->as_html;
907 $pfx = indent_for($level);
908 my $nl = $level == $prev ? "\n" : '';
909 my $dot = $level == 0 ? '' : '` ';
910 $dst .= "$nl$pfx$dot<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
916 # $n isn't the total number of posts on the topic,
917 # just the number of posts in the current results
918 # window, so leave it unlabeled
919 $n = $n == 1 ? '' : " ($n+ messages)";
921 if ($level == 0 || $attr ne $prev_attr) {
922 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
923 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
924 $pfx .= INDENT if $level > 0;
925 $dst .= $pfx . $attr . $n . " - $mbox / $atom\n";
932 sub emit_index_topics {
933 my ($state, $fh) = @_;
934 my $off = $state->{ctx}->{cgi}->param('o');
935 $off = 0 unless defined $off;
936 $state->{order} = [];
937 $state->{subjs} = {};
938 $state->{latest} = {};
940 my %opts = ( offset => int $off, limit => $max * 4 );
941 while (scalar @{$state->{order}} < $max) {
942 my $res = $state->{srch}->query('', \%opts);
943 my $nr = scalar @{$res->{msgs}} or last;
945 for (rsort_ts(thread_results(load_results($res), 1)->rootset)) {
946 add_topic($state, $_, 0);
948 $opts{offset} += $nr;
951 $fh->write(dump_topics($state));