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::Linkify;
16 use PublicInbox::MID qw/mid_clean id_compress mid2path mid_mime/;
19 # TODO: make these constants tunable
20 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
21 use constant MAX_TRUNC_LEN => 72;
22 use constant T_ANCHOR => '#u';
23 use constant INDENT => ' ';
25 *ascii_html = *PublicInbox::Hval::ascii_html;
27 my $enc_utf8 = find_encoding('UTF-8');
31 my ($ctx, $mime, $full_pfx, $footer) = @_;
32 if (defined $footer) {
33 $footer = "\n" . $footer;
37 my $hdr = $mime->header_obj;
38 headers_to_html_header($hdr, $full_pfx, $ctx) .
39 multipart_text_as_html($mime, $full_pfx) .
41 html_footer($hdr, 1, $full_pfx, $ctx) .
43 '</pre></body></html>';
46 # /$LISTNAME/$MESSAGE_ID/R/
48 my ($ctx, $hdr, $footer) = @_;
49 my $s = $hdr->header('Subject');
50 $s = '(no subject)' if (!defined $s) || ($s eq '');
51 my $f = $hdr->header('From');
52 $f = '' unless defined $f;
53 $s = PublicInbox::Hval->new_oneline($s);
54 my $mid = $hdr->header_raw('Message-ID');
55 $mid = PublicInbox::Hval->new_msgid($mid);
58 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
60 my ($arg, $link) = mailto_arg_link($hdr);
61 push @$arg, '/path/to/YOUR_REPLY';
63 "<html><head><title>replying to \"$t\"</title></head><body><pre>" .
64 "replying to message:\n\n" .
65 "Subject: <b>$t</b>\n" .
66 "From: ". ascii_html($f) .
67 "\nDate: " . ascii_html($hdr->header('Date')) .
68 "\nMessage-ID: <" . $mid->as_html . ">\n\n" .
69 "There are multiple ways to reply:\n\n" .
70 "* Save the following mbox file, import it into your mail client,\n" .
71 " and reply-to-all from there: <a\nhref=../raw>mbox</a>\n\n" .
72 "* Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,\n" .
73 " and <b>--in-reply-to</b> switches of git-send-email(1):\n\n" .
74 "\tgit send-email \\\n\t\t" .
75 join(" \\ \n\t\t", @$arg ). "\n\n" .
76 qq( <a\nhref="$se_url">$se_url</a>\n\n) .
77 "* If your mail client supports setting the <b>In-Reply-To</b>" .
78 " header\n via mailto: links, try the " .
79 qq(<a\nhref="$link">mailto: link</a>\n) .
80 "\nFor context, the original <a\nhref=../>message</a> or " .
81 qq(<a\nhref="../t/#u">thread</a>) .
82 '</pre><hr /><pre>' . $footer . '</pre></body></html>';
86 my ($class, $mime, $full_pfx) = @_;
88 # no <head> here for <style>...
89 PublicInbox::Hval::PRE .
90 multipart_text_as_html($mime, $full_pfx) . '</pre>';
95 my $irt = $hdr->header_raw('In-Reply-To');
97 return mid_clean($irt) if (defined $irt);
99 my $refs = $hdr->header_raw('References');
100 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
106 # this is already inside a <pre>
108 my ($fh, $mime, $level, $state) = @_;
109 my $midx = $state->{anchor_idx}++;
110 my $ctx = $state->{ctx};
111 my $srch = $ctx->{srch};
112 my ($prev, $next) = ($midx - 1, $midx + 1);
114 my $hdr = $mime->header_obj;
115 my $enc = enc_for($hdr->header("Content-Type"));
116 my $subj = $hdr->header('Subject');
118 my $mid_raw = mid_clean(mid_mime($mime));
119 my $id = anchor_for($mid_raw);
120 my $seen = $state->{seen};
121 $seen->{$id} = "#$id"; # save the anchor for children, later
123 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
124 my $from = PublicInbox::Hval->new_oneline($hdr->header('From'))->raw;
125 my @from = Email::Address->parse($from);
126 $from = $from[0]->name;
128 $from = PublicInbox::Hval->new_oneline($from)->as_html;
129 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
130 my $root_anchor = $state->{root_anchor} || '';
131 my $path = $root_anchor ? '../../' : '';
132 my $href = $mid->as_href;
133 my $irt = in_reply_to($hdr);
134 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
137 my $t = $ctx->{flat} ? 'T' : 't';
138 $subj = "<a\nhref=\"${path}$href/$t/#u\">$subj</a>";
140 if ($root_anchor eq $id) {
141 $subj = "<u\nid=u>$subj</u>";
144 my $ts = _msg_date($hdr);
145 my $rv = "<pre\nid=s$midx>";
146 $rv .= "<b\nid=$id>$subj</b>\n";
147 $rv .= "- $from @ $ts UTC - ";
148 $rv .= "<a\nhref=\"#s$next\">next</a>";
150 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
152 $fh->write($rv .= "\n\n");
154 my ($fhref, $more_ref);
155 my $mhref = "${path}$href/";
156 my $more = 'permalink';
158 # show full message if it's our root message
159 my $neq = $root_anchor ne $id;
160 if ($neq || ($neq && $level != 0 && !$ctx->{flat})) {
161 $fhref = "${path}$href/f/";
164 # scan through all parts, looking for displayable text
165 $mime->walk_parts(sub {
166 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
170 my $txt = "${path}$href/raw";
171 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
172 $rv .= html_footer($hdr, 0, undef, $ctx, $mhref);
175 unless (defined $parent_anchor) {
176 my $v = PublicInbox::Hval->new_msgid($irt, 1);
178 $parent_anchor = "${path}$v/";
180 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
182 if (my $pct = $state->{pct}) { # used by SearchView.pm
183 $rv .= " [relevance $pct->{$mid_raw}%]";
186 $rv .= " [<a\nhref=\"${path}$href/t/#u\">threaded</a>" .
189 $rv .= " [<b>threaded</b>|" .
190 "<a\nhref=\"${path}$href/T/#u\">flat</a>]";
193 $fh->write($rv .= '</pre>');
197 my ($ctx, $foot, $srch) = @_;
198 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
201 # only private functions below.
203 sub emit_thread_html {
204 my ($cb, $ctx, $foot, $srch) = @_;
205 my $mid = $ctx->{mid};
206 my $res = $srch->get_thread($mid);
207 my $msgs = load_results($res);
208 my $nr = scalar @$msgs;
209 return missing_thread($cb, $ctx) if $nr == 0;
210 my $flat = $ctx->{flat};
216 root_anchor => anchor_for($mid),
221 require PublicInbox::Git;
222 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
224 pre_anchor_entry($seen, $_) for (@$msgs);
225 __thread_entry(\$cb, $git, $state, $_, 0) for (@$msgs);
227 my $th = thread_results($msgs);
228 thread_entry(\$cb, $git, $state, $_, 0) for $th->rootset;
229 if (my $max = $state->{cur_level}) {
230 $cb->write(('</ul></li>' x ($max - 1)) . '</ul>');
234 Email::Address->purge_cache;
236 # there could be a race due to a message being deleted in git
237 # but still being in the Xapian index:
238 return missing_thread($cb, $ctx) if ($orig_cb eq $cb);
240 my $final_anchor = $state->{anchor_idx};
241 my $next = "<a\nid=s$final_anchor>";
242 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
243 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
244 $next .= "\ndownload thread: ";
245 $next .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
246 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
247 $cb->write('<hr /><pre>' . $next . "\n\n".
248 $foot . '</pre></body></html>');
253 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
254 my $s = add_text_body($enc, $part, $part_nr, $fhref);
258 $s .= "\n"; # ensure there's a trailing newline
264 my ($ct, $default) = @_;
265 $default ||= $enc_utf8;
266 defined $ct or return $default;
267 my $ct_parsed = parse_content_type($ct);
269 if (my $charset = $ct_parsed->{attributes}->{charset}) {
270 my $enc = find_encoding($charset);
277 sub multipart_text_as_html {
278 my ($mime, $full_pfx, $srch) = @_;
281 my $enc = enc_for($mime->header("Content-Type"));
283 # scan through all parts, looking for displayable text
284 $mime->walk_parts(sub {
286 $part = add_text_body($enc, $part, \$part_nr, $full_pfx, 1);
288 $rv .= "\n" if $part ne '';
294 sub add_filename_line {
298 $fn = $enc->decode($fn);
300 $pad x= ($len/2) if ($len > 0);
301 "$pad " . ascii_html($fn) . " $pad\n";
305 my ($quot, $n, $part_nr, $full_pfx, $final, $do_anchor) = @_;
307 # n.b.: do not use <blockquote> since it screws up alignment
308 # w.r.t. unquoted text. Repliers may rely on pre-formatted
309 # alignment to point out a certain word in quoted text.
311 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
313 my $l = PublicInbox::Linkify->new;
314 my $rv = join('', map { $l->linkify_1($_) } @$quot);
316 $rv = ascii_html($rv);
317 return $l->linkify_2($rv);
320 # show a short snippet of quoted text and link to full version:
321 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
322 my $cur = join(' ', @$quot);
323 @$quot = split(/\s+/, $cur);
326 my $tmp = shift(@$quot);
327 my $len = length($tmp) + length($cur);
328 if ($len > MAX_TRUNC_LEN) {
333 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
335 $cur =~ s/ \z/ .../s;
336 $cur = ascii_html($cur);
338 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
340 # show everything in the full version with anchor from
341 # short version (see above)
342 my $l = PublicInbox::Linkify->new;
343 my $rv .= join('', map { $l->linkify_1($_) } @$quot);
345 $rv = ascii_html($rv);
346 return $l->linkify_2($rv) unless $do_anchor;
348 "<a\nid=q${part_nr}_$nr></a>" . $l->linkify_2($rv);
353 my ($enc_msg, $part, $part_nr, $full_pfx, $do_anchor) = @_;
354 return '' if $part->subparts;
356 my $ct = $part->content_type;
357 # account for filter bugs...
358 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
362 my $enc = enc_for($ct, $enc_msg);
367 $s = $enc->decode($s);
368 my @lines = split(/^/m, $s);
372 my $fn = $part->filename;
373 defined($fn) or $fn = "part #" . ($$part_nr + 1);
374 $s .= add_filename_line($enc, $fn);
378 while (defined(my $cur = shift @lines)) {
380 # show the previously buffered quote inline
382 $s .= flush_quote(\@quot, \$n, $$part_nr,
383 $full_pfx, 0, $do_anchor);
387 my $l = PublicInbox::Linkify->new;
388 $cur = $l->linkify_1($cur);
389 $cur = ascii_html($cur);
390 $s .= $l->linkify_2($cur);
396 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1,
401 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
402 $s =~ s/\A\n+//s; # kill leading blank lines
403 $s =~ s/\s+\z//s; # kill all trailing spaces (final "\n" added if ne '')
407 sub headers_to_html_header {
408 my ($hdr, $full_pfx, $ctx) = @_;
409 my $srch = $ctx->{srch} if $ctx;
412 my $mid = $hdr->header_raw('Message-ID');
413 $mid = PublicInbox::Hval->new_msgid($mid);
414 foreach my $h (qw(From To Cc Subject Date)) {
415 my $v = $hdr->header($h);
416 defined($v) && ($v ne '') or next;
417 $v = PublicInbox::Hval->new_oneline($v);
420 my @from = Email::Address->parse($v->raw);
421 $title[1] = ascii_html($from[0]->name);
422 } elsif ($h eq 'Subject') {
423 $title[0] = $v->as_html;
425 $rv .= "$h: <b\nid=t>";
426 $rv .= $v->as_html . "</b>\n";
430 $rv .= "$h: " . $v->as_html . "\n";
433 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
434 my $upfx = $full_pfx ? '' : '../';
435 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
438 thread_inline(\$rv, $ctx, $hdr, $upfx);
440 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
441 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
443 $rv .= _parent_headers_nosrch($hdr);
448 ("<html><head><title>". join(' - ', @title) . "</title>$atom".
449 PublicInbox::Hval::STYLE . "</head><body><pre>" . $rv);
453 my ($dst, $ctx, $hdr, $upfx) = @_;
454 my $srch = $ctx->{srch};
455 my $mid = mid_clean($hdr->header_raw('Message-ID'));
456 my $res = $srch->get_thread($mid);
457 my $nr = $res->{total};
458 my $expand = "<a\nhref=\"${upfx}t/#u\">expand</a> " .
459 "/ <a\nhref=\"${upfx}t.mbox.gz\">mbox.gz</a>";
462 my $parent = in_reply_to($hdr);
464 if (defined $parent) {
465 $$dst .= "($expand)\n ";
466 $$dst .= ghost_parent("$upfx../", $parent) . "\n";
468 $$dst .= "[no followups, yet] ($expand)\n";
470 $ctx->{next_msg} = undef;
471 $ctx->{parent_msg} = $parent;
475 $$dst .= "~$nr messages ($expand";
476 if ($nr > MAX_INLINE_QUOTED) {
477 $$dst .= qq! / <a\nhref="#b">[scroll down]</a>!;
481 my $subj = $srch->subject_path($hdr->header('Subject'));
483 seen => { $subj => 1 },
486 parent_cmp => defined $parent ? $parent : '',
491 for (thread_results(load_results($res))->rootset) {
492 inline_dump($dst, $state, $upfx, $_, 0);
494 $$dst .= "<a\nid=b></a>"; # anchor for body start
495 $ctx->{next_msg} = $state->{next_msg};
496 $ctx->{parent_msg} = $state->{parent};
499 sub _parent_headers_nosrch {
503 my $irt = in_reply_to($hdr);
505 my $v = PublicInbox::Hval->new_msgid($irt, 1);
506 my $html = $v->as_html;
507 my $href = $v->as_href;
508 $rv .= "In-Reply-To: <";
509 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
512 my $refs = $hdr->header_raw('References');
514 # avoid redundant URLs wasting bandwidth
516 $seen{$irt} = 1 if defined $irt;
518 my @raw_refs = ($refs =~ /<([^>]+)>/g);
519 foreach my $ref (@raw_refs) {
522 push @refs, linkify_ref_nosrch($ref);
526 $rv .= 'References: '. join(' ', @refs) . "\n";
532 sub mailto_arg_link {
534 my %cc; # everyone else
535 my $to; # this is the From address
537 foreach my $h (qw(From To Cc)) {
538 my $v = $hdr->header($h);
539 defined($v) && ($v ne '') or next;
540 my @addrs = Email::Address->parse($v);
541 foreach my $recip (@addrs) {
542 my $address = $recip->address;
543 my $dst = lc($address);
544 $cc{$dst} ||= $address;
548 Email::Address->purge_cache;
551 my $subj = $hdr->header('Subject') || '';
552 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
553 my $mid = $hdr->header_raw('Message-ID');
554 push @arg, "--in-reply-to='" . ascii_html($mid) . "'";
555 my $irt = uri_escape_utf8($mid);
557 push @arg, '--to=' . ascii_html($to);
558 $to = uri_escape_utf8($to);
559 $subj = uri_escape_utf8($subj);
560 my $cc = join(',', sort values %cc);
561 push @arg, '--cc=' . ascii_html($cc);
562 $cc = uri_escape_utf8($cc);
563 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
570 my ($mime, $standalone, $full_pfx, $ctx, $mhref) = @_;
572 my $srch = $ctx->{srch} if $ctx;
573 my $upfx = $full_pfx ? '../' : '../../';
574 my $tpfx = $full_pfx ? '' : '../';
575 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
578 if ($srch && $standalone) {
579 $idx .= qq{ / follow: <a\nhref="${tpfx}t.atom">Atom feed</a>\n};
582 my $p = $ctx->{parent_msg};
583 my $next = $ctx->{next_msg};
585 $p = PublicInbox::Hval->new_oneline($p);
587 $irt = "<a\nhref=\"$upfx$p/\">parent</a> ";
589 $irt = ' ' x length('parent ');
592 $irt .= "<a\nhref=\"$upfx$next/\">next</a> ";
594 $irt .= ' ' x length('next ');
597 $irt .= "<a\nhref=\"${tpfx}t/#u\">thread</a> ";
599 $irt .= ' ' x length('thread ');
605 $mhref = './' unless defined $mhref;
606 $irt . qq(<a\nhref="${mhref}R/">reply</a>) . $idx;
609 sub linkify_ref_nosrch {
610 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
611 my $html = $v->as_html;
612 my $href = $v->as_href;
613 "<<a\nhref=\"../$href/\">$html</a>>";
619 if ($id !~ /\A[a-f0-9]{40}\z/) {
620 $id = id_compress(mid_clean($id), 1);
625 sub thread_html_head {
626 my ($cb, $header, $state) = @_;
627 $$cb = $$cb->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
629 my $s = PublicInbox::Hval->new_oneline($header->header('Subject'));
631 $$cb->write("<html><head><title>$s</title>".
632 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
633 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
634 PublicInbox::Hval::STYLE .
638 sub pre_anchor_entry {
639 my ($seen, $mime) = @_;
640 my $id = anchor_for(mid_mime($mime));
641 $seen->{$id} = "#$id"; # save the anchor for children, later
645 my ($upfx, $mid) = @_;
646 # 'subject dummy' is used internally by Mail::Thread
647 return '[no common parent]' if ($mid eq 'subject dummy');
649 $mid = PublicInbox::Hval->new_msgid($mid);
650 my $href = $mid->as_href;
651 my $html = $mid->as_html;
652 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
655 sub thread_adj_level {
656 my ($fh, $state, $level) = @_;
658 my $max = $state->{cur_level};
660 return '' if $max == 0; # flat output
662 # reset existing lists
663 my $x = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
664 $fh->write($x . '</ul>');
665 $state->{cur_level} = 0;
668 if ($level == $max) { # continue existing list
670 } elsif ($level < $max) {
671 my $x = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
672 $fh->write($x .= '<li>');
673 $state->{cur_level} = $level;
674 } else { # ($level > $max) # start a new level
675 $state->{cur_level} = $level;
676 $fh->write(($max ? '<li>' : '') . '<ul><li>');
682 my ($fh, $state, $upfx, $mid, $level) = @_;
684 my $end = thread_adj_level($fh, $state, $level);
685 $fh->write('<pre>'. ghost_parent($upfx, $mid) . '</pre>' . $end);
689 my ($cb, $git, $state, $mime, $level) = @_;
691 # lazy load the full message from mini_mime:
693 my $path = mid2path(mid_clean(mid_mime($mime)));
694 Email::MIME->new($git->cat_file('HEAD:'.$path));
697 if ($state->{anchor_idx} == 0) {
698 thread_html_head($cb, $mime, $state, $level);
701 if (my $ghost = delete $state->{ghost}) {
702 # n.b. ghost messages may only be parents, not children
703 foreach my $g (@$ghost) {
704 ghost_flush($fh, $state, '../../', @$g);
707 my $end = thread_adj_level($fh, $state, $level);
708 index_entry($fh, $mime, $level, $state);
709 $fh->write($end) if $end;
716 INDENT x ($level - 1);
719 sub __ghost_prepare {
720 my ($state, $node, $level) = @_;
721 my $ghost = $state->{ghost} ||= [];
722 push @$ghost, [ $node->messageid, $level ];
726 my ($cb, $git, $state, $node, $level) = @_;
728 if (my $mime = $node->message) {
729 unless (__thread_entry($cb, $git, $state, $mime, $level)) {
730 __ghost_prepare($state, $node, $level);
733 __ghost_prepare($state, $node, $level);
736 thread_entry($cb, $git, $state, $node->child, $level + 1);
737 thread_entry($cb, $git, $state, $node->next, $level);
743 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
748 my $ts = eval { str2time($hdr->header('Date')) };
749 defined($ts) ? $ts : 0;
753 my ($msgs, $nosubject) = @_;
754 require PublicInbox::Thread;
755 my $th = PublicInbox::Thread->new(@$msgs);
757 $Mail::Thread::nosubject = $nosubject;
759 $th->order(*sort_ts);
765 require PublicInbox::ExtMsg;
767 $cb->(PublicInbox::ExtMsg::ext_msg($ctx))
772 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
776 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
779 my ($dst, $state, $upfx, $hdr, $level) = @_;
780 my $dot = $level == 0 ? '' : '` ';
782 my $cur = $state->{cur};
783 my $mid = mid_clean($hdr->header_raw('Message-ID'));
784 my $f = $hdr->header('X-PI-From');
785 my $d = _msg_date($hdr);
786 $f = PublicInbox::Hval->new_oneline($f)->as_html;
787 my $pfx = ' ' . $d . ' ' . indent_for($level);
789 $state->{first_level} ||= $level;
791 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
792 $state->{prev_attr} = $attr;
796 $state->{prev_level} = $level;
800 delete $state->{cur};
801 $$dst .= "$pfx$dot<b><a\nid=r\nhref=\"#b\">".
802 "$attr [this message]</a></b>\n";
807 $state->{next_msg} ||= $mid;
810 # Subject is never undef, this mail was loaded from
811 # our Xapian which would've resulted in '' if it were
812 # really missing (and Filter rejects empty subjects)
813 my $s = $hdr->header('Subject');
814 my $h = $state->{srch}->subject_path($s);
815 if ($state->{seen}->{$h}) {
818 $state->{seen}->{$h} = 1;
819 $s = PublicInbox::Hval->new($s);
822 my $m = PublicInbox::Hval->new_msgid($mid);
823 $m = $upfx . '../' . $m->as_href . '/';
825 $$dst .= "$pfx$dot<a\nhref=\"$m\">$s</a> $attr\n";
827 $$dst .= "$pfx$dot<a\nhref=\"$m\">$f</a>\n";
832 my ($dst, $state, $upfx, $node, $level) = @_;
834 if (my $mime = $node->message) {
835 my $hdr = $mime->header_obj;
836 my $mid = mid_clean($hdr->header_obj('Message-ID'));
837 if ($mid eq $state->{parent_cmp}) {
838 $state->{parent} = $mid;
840 _inline_header($dst, $state, $upfx, $hdr, $level);
842 my $dot = $level == 0 ? '' : '` ';
843 my $pfx = (' ' x length(' 1970-01-01 13:37 ')).
844 indent_for($level) . $dot;
846 $$dst .= ghost_parent("$upfx../", $node->messageid) . "\n";
848 inline_dump($dst, $state, $upfx, $node->child, $level+1);
849 inline_dump($dst, $state, $upfx, $node->next, $level);
854 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
855 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
861 (eval { $b->topmost->message->header('X-PI-TS') } || 0) <=>
862 (eval { $a->topmost->message->header('X-PI-TS') } || 0)
866 # accumulate recent topics if search is supported
867 # returns 1 if done, undef if not
869 my ($state, $node, $level) = @_;
871 my $child_adjust = 1;
873 if (my $x = $node->message) {
877 $subj = $x->header('Subject');
878 $subj = $state->{srch}->subject_normalized($subj);
880 if (++$state->{subjs}->{$subj} == 1) {
881 push @{$state->{order}}, [ $level, $subj ];
884 my $mid = mid_clean($x->header_raw('Message-ID'));
886 my $ts = $x->header('X-PI-TS');
887 my $exist = $state->{latest}->{$subj};
888 if (!$exist || $exist->[1] < $ts) {
889 $state->{latest}->{$subj} = [ $mid, $ts ];
892 # ghost message, do not bump level
896 add_topic($state, $node->child, $level + $child_adjust);
897 add_topic($state, $node->next, $level);
902 my $order = $state->{order};
903 my $subjs = $state->{subjs};
904 my $latest = $state->{latest};
905 return "\n[No topics in range]</pre>" unless (scalar @$order);
910 while (defined(my $info = shift @$order)) {
911 my ($level, $subj) = @$info;
912 my $n = delete $subjs->{$subj};
913 my ($mid, $ts) = @{delete $latest->{$subj}};
914 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
915 $subj = PublicInbox::Hval->new($subj)->as_html;
916 $pfx = indent_for($level);
917 my $nl = $level == $prev ? "\n" : '';
918 my $dot = $level == 0 ? '' : '` ';
919 $dst .= "$nl$pfx$dot<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
925 # $n isn't the total number of posts on the topic,
926 # just the number of posts in the current results
927 # window, so leave it unlabeled
928 $n = $n == 1 ? '' : " ($n+ messages)";
930 if ($level == 0 || $attr ne $prev_attr) {
931 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
932 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
933 $pfx .= INDENT if $level > 0;
934 $dst .= $pfx . $attr . $n . " - $mbox / $atom\n";
941 sub emit_index_topics {
942 my ($state, $fh) = @_;
943 my $off = $state->{ctx}->{cgi}->param('o');
944 $off = 0 unless defined $off;
945 $state->{order} = [];
946 $state->{subjs} = {};
947 $state->{latest} = {};
949 my %opts = ( offset => int $off, limit => $max * 4 );
950 while (scalar @{$state->{order}} < $max) {
951 my $res = $state->{srch}->query('', \%opts);
952 my $nr = scalar @{$res->{msgs}} or last;
954 for (rsort_ts(thread_results(load_results($res), 1)->rootset)) {
955 add_topic($state, $_, 0);
957 $opts{offset} += $nr;
960 $fh->write(dump_topics($state));