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 my $hdr = $mime->header_obj;
39 headers_to_html_header($hdr, $full_pfx, $ctx) .
40 multipart_text_as_html($mime, $full_pfx) .
42 html_footer($hdr, 1, $full_pfx, $ctx) .
44 '</pre></body></html>';
47 # /$LISTNAME/$MESSAGE_ID/R/
49 my ($ctx, $hdr, $footer) = @_;
50 my $s = $hdr->header('Subject');
51 $s = '(no subject)' if (!defined $s) || ($s eq '');
52 my $f = $hdr->header('From');
53 $f = '' unless defined $f;
54 $s = PublicInbox::Hval->new_oneline($s);
55 my $mid = $hdr->header('Message-ID');
56 $mid = PublicInbox::Hval->new_msgid($mid);
59 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
61 my ($arg, $link) = mailto_arg_link($hdr);
62 push @$arg, '/path/to/YOUR_REPLY';
64 "<html><head><title>replying to \"$t\"</title></head><body><pre>" .
65 "replying to message:\n\n" .
66 "Subject: <b>$t</b>\n" .
67 "From: ". ascii_html($f) .
68 "\nDate: " . ascii_html($hdr->header('Date')) .
69 "\nMessage-ID: <" . $mid->as_html . ">\n\n" .
70 "There are multiple ways to reply:\n\n" .
71 "* Save the following mbox file, import it into your mail client,\n" .
72 " and reply-to-all from there: <a\nhref=../raw>mbox</a>\n\n" .
73 "* Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,\n" .
74 " and <b>--in-reply-to</b> switches of git-send-email(1):\n\n" .
75 "\tgit send-email \\\n\t\t" .
76 join(" \\ \n\t\t", @$arg ). "\n\n" .
77 qq( <a\nhref="$se_url">$se_url</a>\n\n) .
78 "* If your mail client supports setting the <b>In-Reply-To</b>" .
79 " header\n via mailto: links, try the " .
80 qq(<a\nhref="$link">mailto: link</a>\n) .
81 "\nFor context, the original <a\nhref=../>message</a> or " .
82 qq(<a\nhref="../t/#u">thread</a>) .
83 '</pre><hr /><pre>' . $footer . '</pre></body></html>';
87 my ($class, $mime, $full_pfx) = @_;
89 # no <head> here for <style>...
90 PublicInbox::Hval::PRE .
91 multipart_text_as_html($mime, $full_pfx) . '</pre>';
96 my $irt = $hdr->header('In-Reply-To');
98 return mid_clean($irt) if (defined $irt);
100 my $refs = $hdr->header('References');
101 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
107 # this is already inside a <pre>
109 my ($fh, $mime, $level, $state) = @_;
110 my $midx = $state->{anchor_idx}++;
111 my $ctx = $state->{ctx};
112 my $srch = $ctx->{srch};
113 my ($prev, $next) = ($midx - 1, $midx + 1);
115 my $hdr = $mime->header_obj;
116 my $enc = enc_for($hdr->header("Content-Type"));
117 my $subj = $hdr->header('Subject');
119 my $mid_raw = mid_clean($hdr->header('Message-ID'));
120 my $id = anchor_for($mid_raw);
121 my $seen = $state->{seen};
122 $seen->{$id} = "#$id"; # save the anchor for children, later
124 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
125 my $from = PublicInbox::Hval->new_oneline($hdr->header('From'))->raw;
126 my @from = Email::Address->parse($from);
127 $from = $from[0]->name;
129 $from = PublicInbox::Hval->new_oneline($from)->as_html;
130 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
131 my $root_anchor = $state->{root_anchor} || '';
132 my $path = $root_anchor ? '../../' : '';
133 my $href = $mid->as_href;
134 my $irt = in_reply_to($hdr);
135 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
138 my $t = $ctx->{flat} ? 'T' : 't';
139 $subj = "<a\nhref=\"${path}$href/$t/#u\">$subj</a>";
141 if ($root_anchor eq $id) {
142 $subj = "<u\nid=u>$subj</u>";
145 my $ts = _msg_date($hdr);
146 my $rv = "<pre\nid=s$midx>";
147 $rv .= "<b\nid=$id>$subj</b>\n";
148 $rv .= "- $from @ $ts UTC - ";
149 $rv .= "<a\nhref=\"#s$next\">next</a>";
151 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
153 $fh->write($rv .= "\n\n");
155 my ($fhref, $more_ref);
156 my $mhref = "${path}$href/";
157 my $more = 'permalink';
159 # show full message if it's our root message
160 my $neq = $root_anchor ne $id;
161 if ($neq || ($neq && $level != 0 && !$ctx->{flat})) {
162 $fhref = "${path}$href/f/";
165 # scan through all parts, looking for displayable text
166 $mime->walk_parts(sub {
167 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
171 my $txt = "${path}$href/raw";
172 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
173 $rv .= html_footer($hdr, 0, undef, $ctx, $mhref);
176 unless (defined $parent_anchor) {
177 my $v = PublicInbox::Hval->new_msgid($irt, 1);
179 $parent_anchor = "${path}$v/";
181 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
183 if (my $pct = $state->{pct}) { # used by SearchView.pm
184 $rv .= " [relevance $pct->{$mid_raw}%]";
187 $rv .= " [<a\nhref=\"${path}$href/t/#u\">threaded</a>" .
190 $rv .= " [<b>threaded</b>|" .
191 "<a\nhref=\"${path}$href/T/#u\">flat</a>]";
194 $fh->write($rv .= '</pre>');
198 my ($ctx, $foot, $srch) = @_;
199 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
202 # only private functions below.
204 sub emit_thread_html {
205 my ($cb, $ctx, $foot, $srch) = @_;
206 my $mid = $ctx->{mid};
207 my $res = $srch->get_thread($mid);
208 my $msgs = load_results($res);
209 my $nr = scalar @$msgs;
210 return missing_thread($cb, $ctx) if $nr == 0;
211 my $flat = $ctx->{flat};
217 root_anchor => anchor_for($mid),
222 require PublicInbox::Git;
223 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
225 pre_anchor_entry($seen, $_) for (@$msgs);
226 __thread_entry(\$cb, $git, $state, $_, 0) for (@$msgs);
228 my $th = thread_results($msgs);
229 thread_entry(\$cb, $git, $state, $_, 0) for $th->rootset;
230 if (my $max = $state->{cur_level}) {
231 $cb->write(('</ul></li>' x ($max - 1)) . '</ul>');
235 Email::Address->purge_cache;
237 # there could be a race due to a message being deleted in git
238 # but still being in the Xapian index:
239 return missing_thread($cb, $ctx) if ($orig_cb eq $cb);
241 my $final_anchor = $state->{anchor_idx};
242 my $next = "<a\nid=s$final_anchor>";
243 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
244 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
245 $next .= "\ndownload thread: ";
246 $next .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
247 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
248 $cb->write('<hr /><pre>' . $next . "\n\n".
249 $foot . '</pre></body></html>');
254 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
255 my $s = add_text_body($enc, $part, $part_nr, $fhref);
257 # kill any leading or trailing whitespace lines
262 # kill per-line trailing whitespace
263 $s =~ s/[ \t]+$//sgm;
264 $s .= "\n" unless $s =~ /\n\z/s;
270 my ($ct, $default) = @_;
271 $default ||= $enc_utf8;
272 defined $ct or return $default;
273 my $ct_parsed = parse_content_type($ct);
275 if (my $charset = $ct_parsed->{attributes}->{charset}) {
276 my $enc = find_encoding($charset);
283 sub multipart_text_as_html {
284 my ($mime, $full_pfx, $srch) = @_;
287 my $enc = enc_for($mime->header("Content-Type"));
289 # scan through all parts, looking for displayable text
290 $mime->walk_parts(sub {
292 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx, 1);
298 sub add_filename_line {
302 $fn = $enc->decode($fn);
304 $pad x= ($len/2) if ($len > 0);
305 "$pad " . ascii_html($fn) . " $pad\n";
308 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://
310 ?[\@\w\+\&\?\.\%\;/#=-]*)!x;
313 my ($link_map, $s) = @_;
316 # salt this, as this could be exploited to show
317 # links in the HTML which don't show up in the raw mail.
318 my $key = sha1_hex($url . $SALT);
319 $link_map->{$key} = $url;
326 my ($link_map, $s) = @_;
328 # Added "PI-LINK-" prefix to avoid false-positives on git commits
329 $s =~ s!\bPI-LINK-([a-f0-9]{40})\b!
331 my $url = $link_map->{$key};
333 $url = ascii_html($url);
334 "<a\nhref=\"$url\">$url</a>";
336 # false positive or somebody tried to mess with us
344 my ($quot, $n, $part_nr, $full_pfx, $final, $do_anchor) = @_;
346 # n.b.: do not use <blockquote> since it screws up alignment
347 # w.r.t. unquoted text. Repliers may rely on pre-formatted
348 # alignment to point out a certain word in quoted text.
350 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
353 my $rv = join('', map { linkify_1(\%l, $_) } @$quot);
355 $rv = ascii_html($rv);
356 return linkify_2(\%l, $rv);
359 # show a short snippet of quoted text and link to full version:
360 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
361 my $cur = join(' ', @$quot);
362 @$quot = split(/\s+/, $cur);
365 my $tmp = shift(@$quot);
366 my $len = length($tmp) + length($cur);
367 if ($len > MAX_TRUNC_LEN) {
372 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
374 $cur =~ s/ \z/ .../s;
375 $cur = ascii_html($cur);
377 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
379 # show everything in the full version with anchor from
380 # short version (see above)
382 my $rv .= join('', map { linkify_1(\%l, $_) } @$quot);
384 $rv = ascii_html($rv);
385 return linkify_2(\%l, $rv) unless $do_anchor;
387 "<a\nid=q${part_nr}_$nr></a>" . linkify_2(\%l, $rv);
392 my ($enc_msg, $part, $part_nr, $full_pfx, $do_anchor) = @_;
393 return '' if $part->subparts;
395 my $ct = $part->content_type;
396 # account for filter bugs...
397 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
401 my $enc = enc_for($ct, $enc_msg);
406 $s = $enc->decode($s);
407 my @lines = split(/^/m, $s);
411 my $fn = $part->filename;
412 defined($fn) or $fn = "part #" . ($$part_nr + 1);
413 $s .= add_filename_line($enc, $fn);
417 while (defined(my $cur = shift @lines)) {
419 # show the previously buffered quote inline
421 $s .= flush_quote(\@quot, \$n, $$part_nr,
422 $full_pfx, 0, $do_anchor);
427 $cur = linkify_1(\%l, $cur);
428 $cur = ascii_html($cur);
429 $s .= linkify_2(\%l, $cur);
435 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1,
438 $s .= "\n" unless $s =~ /\n\z/s;
443 sub headers_to_html_header {
444 my ($hdr, $full_pfx, $ctx) = @_;
445 my $srch = $ctx->{srch} if $ctx;
448 my $mid = $hdr->header('Message-ID');
449 $mid = PublicInbox::Hval->new_msgid($mid);
450 foreach my $h (qw(From To Cc Subject Date)) {
451 my $v = $hdr->header($h);
452 defined($v) && ($v ne '') or next;
453 $v = PublicInbox::Hval->new_oneline($v);
456 my @from = Email::Address->parse($v->raw);
457 $title[1] = ascii_html($from[0]->name);
458 } elsif ($h eq 'Subject') {
459 $title[0] = $v->as_html;
461 $rv .= "$h: <b\nid=t>";
462 $rv .= $v->as_html . "</b>\n";
466 $rv .= "$h: " . $v->as_html . "\n";
469 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
470 my $upfx = $full_pfx ? '' : '../';
471 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
474 thread_inline(\$rv, $ctx, $hdr, $upfx);
476 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
477 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
479 $rv .= _parent_headers_nosrch($hdr);
484 ("<html><head><title>". join(' - ', @title) . "</title>$atom".
485 PublicInbox::Hval::STYLE . "</head><body><pre>" . $rv);
489 my ($dst, $ctx, $hdr, $upfx) = @_;
490 my $srch = $ctx->{srch};
491 my $mid = mid_clean($hdr->header('Message-ID'));
492 my $res = $srch->get_thread($mid);
493 my $nr = $res->{total};
494 my $expand = "<a\nhref=\"${upfx}t/#u\">expand</a> " .
495 "/ <a\nhref=\"${upfx}t.mbox.gz\">mbox.gz</a>";
498 my $parent = in_reply_to($hdr);
500 if (defined $parent) {
501 $$dst .= "($expand)\n ";
502 $$dst .= ghost_parent("$upfx../", $parent) . "\n";
504 $$dst .= "[no followups, yet] ($expand)\n";
506 $ctx->{next_msg} = undef;
507 $ctx->{parent_msg} = $parent;
511 $$dst .= "~$nr messages ($expand";
512 if ($nr > MAX_INLINE_QUOTED) {
513 $$dst .= qq! / <a\nhref="#b">[scroll down]</a>!;
517 my $subj = $srch->subject_path($hdr->header('Subject'));
519 seen => { $subj => 1 },
522 parent_cmp => defined $parent ? $parent : '',
527 for (thread_results(load_results($res))->rootset) {
528 inline_dump($dst, $state, $upfx, $_, 0);
530 $$dst .= "<a\nid=b></a>"; # anchor for body start
531 $ctx->{next_msg} = $state->{next_msg};
532 $ctx->{parent_msg} = $state->{parent};
535 sub _parent_headers_nosrch {
539 my $irt = in_reply_to($hdr);
541 my $v = PublicInbox::Hval->new_msgid($irt, 1);
542 my $html = $v->as_html;
543 my $href = $v->as_href;
544 $rv .= "In-Reply-To: <";
545 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
548 my $refs = $hdr->header('References');
550 # avoid redundant URLs wasting bandwidth
552 $seen{$irt} = 1 if defined $irt;
554 my @raw_refs = ($refs =~ /<([^>]+)>/g);
555 foreach my $ref (@raw_refs) {
558 push @refs, linkify_ref_nosrch($ref);
562 $rv .= 'References: '. join(' ', @refs) . "\n";
568 sub mailto_arg_link {
570 my %cc; # everyone else
571 my $to; # this is the From address
573 foreach my $h (qw(From To Cc)) {
574 my $v = $hdr->header($h);
575 defined($v) && ($v ne '') or next;
576 my @addrs = Email::Address->parse($v);
577 foreach my $recip (@addrs) {
578 my $address = $recip->address;
579 my $dst = lc($address);
580 $cc{$dst} ||= $address;
584 Email::Address->purge_cache;
587 my $subj = $hdr->header('Subject') || '';
588 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
589 my $mid = $hdr->header('Message-ID');
590 push @arg, "--in-reply-to='" . ascii_html($mid) . "'";
591 my $irt = uri_escape_utf8($mid);
593 push @arg, '--to=' . ascii_html($to);
594 $to = uri_escape_utf8($to);
595 $subj = uri_escape_utf8($subj);
596 my $cc = join(',', sort values %cc);
597 push @arg, '--cc=' . ascii_html($cc);
598 $cc = uri_escape_utf8($cc);
599 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
606 my ($mime, $standalone, $full_pfx, $ctx, $mhref) = @_;
608 my $srch = $ctx->{srch} if $ctx;
609 my $upfx = $full_pfx ? '../' : '../../';
610 my $tpfx = $full_pfx ? '' : '../';
611 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
614 if ($srch && $standalone) {
615 $idx .= qq{ / follow: <a\nhref="${tpfx}t.atom">Atom feed</a>\n};
618 my $p = $ctx->{parent_msg};
619 my $next = $ctx->{next_msg};
621 $p = PublicInbox::Hval->new_oneline($p);
623 $irt = "<a\nhref=\"$upfx$p/\">parent</a> ";
625 $irt = ' ' x length('parent ');
628 $irt .= "<a\nhref=\"$upfx$next/\">next</a> ";
630 $irt .= ' ' x length('next ');
633 $irt .= "<a\nhref=\"${tpfx}t/#u\">thread</a> ";
635 $irt .= ' ' x length('thread ');
641 $mhref = './' unless defined $mhref;
642 $irt . qq(<a\nhref="${mhref}R/">reply</a>) . $idx;
645 sub linkify_ref_nosrch {
646 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
647 my $html = $v->as_html;
648 my $href = $v->as_href;
649 "<<a\nhref=\"../$href/\">$html</a>>";
655 if ($id !~ /\A[a-f0-9]{40}\z/) {
656 $id = id_compress(mid_clean($id), 1);
661 sub thread_html_head {
662 my ($cb, $header, $state) = @_;
663 $$cb = $$cb->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
665 my $s = PublicInbox::Hval->new_oneline($header->header('Subject'));
667 $$cb->write("<html><head><title>$s</title>".
668 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
669 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
670 PublicInbox::Hval::STYLE .
674 sub pre_anchor_entry {
675 my ($seen, $mime) = @_;
676 my $id = anchor_for($mime->header('Message-ID'));
677 $seen->{$id} = "#$id"; # save the anchor for children, later
681 my ($upfx, $mid) = @_;
682 # 'subject dummy' is used internally by Mail::Thread
683 return '[no common parent]' if ($mid eq 'subject dummy');
685 $mid = PublicInbox::Hval->new_msgid($mid);
686 my $href = $mid->as_href;
687 my $html = $mid->as_html;
688 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
691 sub thread_adj_level {
692 my ($fh, $state, $level) = @_;
694 my $max = $state->{cur_level};
696 return '' if $max == 0; # flat output
698 # reset existing lists
699 my $x = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
700 $fh->write($x . '</ul>');
701 $state->{cur_level} = 0;
704 if ($level == $max) { # continue existing list
706 } elsif ($level < $max) {
707 my $x = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
708 $fh->write($x .= '<li>');
709 $state->{cur_level} = $level;
710 } else { # ($level > $max) # start a new level
711 $state->{cur_level} = $level;
712 $fh->write(($max ? '<li>' : '') . '<ul><li>');
718 my ($fh, $state, $upfx, $mid, $level) = @_;
720 my $end = thread_adj_level($fh, $state, $level);
721 $fh->write('<pre>'. ghost_parent($upfx, $mid) . '</pre>' . $end);
725 my ($cb, $git, $state, $mime, $level) = @_;
727 # lazy load the full message from mini_mime:
729 my $path = mid2path(mid_clean($mime->header('Message-ID')));
730 Email::MIME->new($git->cat_file('HEAD:'.$path));
733 if ($state->{anchor_idx} == 0) {
734 thread_html_head($cb, $mime, $state, $level);
737 if (my $ghost = delete $state->{ghost}) {
738 # n.b. ghost messages may only be parents, not children
739 foreach my $g (@$ghost) {
740 ghost_flush($fh, $state, '../../', @$g);
743 my $end = thread_adj_level($fh, $state, $level);
744 index_entry($fh, $mime, $level, $state);
745 $fh->write($end) if $end;
752 INDENT x ($level - 1);
755 sub __ghost_prepare {
756 my ($state, $node, $level) = @_;
757 my $ghost = $state->{ghost} ||= [];
758 push @$ghost, [ $node->messageid, $level ];
762 my ($cb, $git, $state, $node, $level) = @_;
764 if (my $mime = $node->message) {
765 unless (__thread_entry($cb, $git, $state, $mime, $level)) {
766 __ghost_prepare($state, $node, $level);
769 __ghost_prepare($state, $node, $level);
772 thread_entry($cb, $git, $state, $node->child, $level + 1);
773 thread_entry($cb, $git, $state, $node->next, $level);
779 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
784 my $ts = eval { str2time($hdr->header('Date')) };
785 defined($ts) ? $ts : 0;
789 my ($msgs, $nosubject) = @_;
790 require PublicInbox::Thread;
791 my $th = PublicInbox::Thread->new(@$msgs);
793 $Mail::Thread::nosubject = $nosubject;
795 $th->order(*sort_ts);
801 require PublicInbox::ExtMsg;
803 $cb->(PublicInbox::ExtMsg::ext_msg($ctx))
808 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
812 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
815 my ($dst, $state, $upfx, $hdr, $level) = @_;
816 my $dot = $level == 0 ? '' : '` ';
818 my $cur = $state->{cur};
819 my $mid = mid_clean($hdr->header('Message-ID'));
820 my $f = $hdr->header('X-PI-From');
821 my $d = _msg_date($hdr);
822 $f = PublicInbox::Hval->new_oneline($f)->as_html;
823 my $pfx = ' ' . $d . ' ' . indent_for($level);
825 $state->{first_level} ||= $level;
827 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
828 $state->{prev_attr} = $attr;
832 $state->{prev_level} = $level;
836 delete $state->{cur};
837 $$dst .= "$pfx$dot<b><a\nid=r\nhref=\"#b\">".
838 "$attr [this message]</a></b>\n";
843 $state->{next_msg} ||= $mid;
846 # Subject is never undef, this mail was loaded from
847 # our Xapian which would've resulted in '' if it were
848 # really missing (and Filter rejects empty subjects)
849 my $s = $hdr->header('Subject');
850 my $h = $state->{srch}->subject_path($s);
851 if ($state->{seen}->{$h}) {
854 $state->{seen}->{$h} = 1;
855 $s = PublicInbox::Hval->new($s);
858 my $m = PublicInbox::Hval->new_msgid($mid);
859 $m = $upfx . '../' . $m->as_href . '/';
861 $$dst .= "$pfx$dot<a\nhref=\"$m\">$s</a> $attr\n";
863 $$dst .= "$pfx$dot<a\nhref=\"$m\">$f</a>\n";
868 my ($dst, $state, $upfx, $node, $level) = @_;
870 if (my $mime = $node->message) {
871 my $hdr = $mime->header_obj;
872 my $mid = mid_clean($hdr->header('Message-ID'));
873 if ($mid eq $state->{parent_cmp}) {
874 $state->{parent} = $mid;
876 _inline_header($dst, $state, $upfx, $hdr, $level);
878 my $dot = $level == 0 ? '' : '` ';
879 my $pfx = (' ' x length(' 1970-01-01 13:37 ')).
880 indent_for($level) . $dot;
882 $$dst .= ghost_parent("$upfx../", $node->messageid) . "\n";
884 inline_dump($dst, $state, $upfx, $node->child, $level+1);
885 inline_dump($dst, $state, $upfx, $node->next, $level);
890 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
891 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
897 (eval { $b->topmost->message->header('X-PI-TS') } || 0) <=>
898 (eval { $a->topmost->message->header('X-PI-TS') } || 0)
902 # accumulate recent topics if search is supported
903 # returns 1 if done, undef if not
905 my ($state, $node, $level) = @_;
907 my $child_adjust = 1;
909 if (my $x = $node->message) {
913 $subj = $x->header('Subject');
914 $subj = $state->{srch}->subject_normalized($subj);
916 if (++$state->{subjs}->{$subj} == 1) {
917 push @{$state->{order}}, [ $level, $subj ];
920 my $mid = mid_clean($x->header('Message-ID'));
922 my $ts = $x->header('X-PI-TS');
923 my $exist = $state->{latest}->{$subj};
924 if (!$exist || $exist->[1] < $ts) {
925 $state->{latest}->{$subj} = [ $mid, $ts ];
928 # ghost message, do not bump level
932 add_topic($state, $node->child, $level + $child_adjust);
933 add_topic($state, $node->next, $level);
938 my $order = $state->{order};
939 my $subjs = $state->{subjs};
940 my $latest = $state->{latest};
941 return "\n[No topics in range]</pre>" unless (scalar @$order);
946 while (defined(my $info = shift @$order)) {
947 my ($level, $subj) = @$info;
948 my $n = delete $subjs->{$subj};
949 my ($mid, $ts) = @{delete $latest->{$subj}};
950 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
951 $subj = PublicInbox::Hval->new($subj)->as_html;
952 $pfx = indent_for($level);
953 my $nl = $level == $prev ? "\n" : '';
954 my $dot = $level == 0 ? '' : '` ';
955 $dst .= "$nl$pfx$dot<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
961 # $n isn't the total number of posts on the topic,
962 # just the number of posts in the current results
963 # window, so leave it unlabeled
964 $n = $n == 1 ? '' : " ($n+ messages)";
966 if ($level == 0 || $attr ne $prev_attr) {
967 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
968 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
969 $pfx .= INDENT if $level > 0;
970 $dst .= $pfx . $attr . $n . " - $mbox / $atom\n";
977 sub emit_index_topics {
978 my ($state, $fh) = @_;
979 my $off = $state->{ctx}->{cgi}->param('o');
980 $off = 0 unless defined $off;
981 $state->{order} = [];
982 $state->{subjs} = {};
983 $state->{latest} = {};
985 my %opts = ( offset => int $off, limit => $max * 4 );
986 while (scalar @{$state->{order}} < $max) {
987 my $res = $state->{srch}->query('', \%opts);
988 my $nr = scalar @{$res->{msgs}} or last;
990 for (rsort_ts(thread_results(load_results($res), 1)->rootset)) {
991 add_topic($state, $_, 0);
993 $opts{offset} += $nr;
996 $fh->write(dump_topics($state));