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/;
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 my $enc_utf8 = find_encoding('UTF-8');
29 my ($ctx, $mime, $full_pfx, $footer) = @_;
30 $footer = defined($footer) ? "\n$footer" : '';
31 my $hdr = $mime->header_obj;
32 headers_to_html_header($hdr, $full_pfx, $ctx) .
33 multipart_text_as_html($mime, $full_pfx) .
35 html_footer($hdr, 1, $full_pfx, $ctx) .
37 '</pre></body></html>';
40 # /$LISTNAME/$MESSAGE_ID/R/
42 my ($ctx, $hdr, $footer) = @_;
43 my $s = $hdr->header('Subject');
44 $s = '(no subject)' if (!defined $s) || ($s eq '');
45 my $f = $hdr->header('From');
46 $f = '' unless defined $f;
47 my $mid = $hdr->header_raw('Message-ID');
48 $mid = PublicInbox::Hval->new_msgid($mid);
49 my $t = ascii_html($s);
51 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
53 my ($arg, $link) = mailto_arg_link($hdr);
54 push @$arg, '/path/to/YOUR_REPLY';
56 "<html><head><title>replying to \"$t\"</title></head><body><pre>" .
57 "replying to message:\n\n" .
58 "Subject: <b>$t</b>\n" .
59 "From: ". ascii_html($f) .
60 "\nDate: " . ascii_html($hdr->header('Date')) .
61 "\nMessage-ID: <" . $mid->as_html . ">\n\n" .
62 "There are multiple ways to reply:\n\n" .
63 "* Save the following mbox file, import it into your mail client,\n" .
64 " and reply-to-all from there: <a\nhref=../raw>mbox</a>\n\n" .
65 "* Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,\n" .
66 " and <b>--in-reply-to</b> switches of git-send-email(1):\n\n" .
67 "\tgit send-email \\\n\t\t" .
68 join(" \\ \n\t\t", @$arg ). "\n\n" .
69 qq( <a\nhref="$se_url">$se_url</a>\n\n) .
70 "* If your mail client supports setting the <b>In-Reply-To</b>" .
71 " header\n via mailto: links, try the " .
72 qq(<a\nhref="$link">mailto: link</a>\n) .
73 "\nFor context, the original <a\nhref=../>message</a> or " .
74 qq(<a\nhref="../t/#u">thread</a>) .
75 '</pre><hr /><pre>' . $footer . '</pre></body></html>';
79 my ($class, $mime, $full_pfx) = @_;
81 # no <head> here for <style>...
82 PublicInbox::Hval::PRE .
83 multipart_text_as_html($mime, $full_pfx) . '</pre>';
88 my $irt = $hdr->header_raw('In-Reply-To');
90 return mid_clean($irt) if (defined $irt);
92 my $refs = $hdr->header_raw('References');
93 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
99 # this is already inside a <pre>
101 my ($mime, $level, $state) = @_;
102 my $midx = $state->{anchor_idx}++;
103 my $ctx = $state->{ctx};
104 my $srch = $ctx->{srch};
105 my ($prev, $next) = ($midx - 1, $midx + 1);
107 my $hdr = $mime->header_obj;
108 my $enc = enc_for($hdr->header("Content-Type"));
109 my $subj = $hdr->header('Subject');
111 my $mid_raw = mid_clean(mid_mime($mime));
112 my $id = anchor_for($mid_raw);
113 my $seen = $state->{seen};
114 $seen->{$id} = "#$id"; # save the anchor for children, later
116 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
117 my $from = $hdr->header('From');
118 my @from = Email::Address->parse($from);
119 $from = $from[0]->name;
121 my $root_anchor = $state->{root_anchor} || '';
122 my $path = $root_anchor ? '../../' : '';
123 my $href = $mid->as_href;
124 my $irt = in_reply_to($hdr);
125 my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
127 $from = ascii_html($from);
128 $subj = ascii_html($subj);
130 my $t = $ctx->{flat} ? 'T' : 't';
131 $subj = "<a\nhref=\"${path}$href/$t/#u\">$subj</a>";
133 if ($root_anchor eq $id) {
134 $subj = "<u\nid=u>$subj</u>";
137 my $ts = _msg_date($hdr);
138 my $rv = "<pre\nid=s$midx>";
139 $rv .= "<b\nid=$id>$subj</b>\n";
140 $rv .= "- $from @ $ts UTC - ";
141 $rv .= "<a\nhref=\"#s$next\">next</a>";
143 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
145 my $fh = $state->{fh};
146 $fh->write($rv .= "\n\n");
148 my ($fhref, $more_ref);
149 my $mhref = "${path}$href/";
150 my $more = 'permalink';
152 # show full message if it's our root message
153 my $neq = $root_anchor ne $id;
154 if ($neq || ($neq && $level != 0 && !$ctx->{flat})) {
155 $fhref = "${path}$href/f/";
158 # scan through all parts, looking for displayable text
159 $mime->walk_parts(sub {
160 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
164 my $txt = "${path}$href/raw";
165 $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
166 $rv .= html_footer($hdr, 0, undef, $ctx, $mhref);
169 unless (defined $parent_anchor) {
170 my $v = PublicInbox::Hval->new_msgid($irt, 1);
172 $parent_anchor = "${path}$v/";
174 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
176 if (my $pct = $state->{pct}) { # used by SearchView.pm
177 $rv .= " [relevance $pct->{$mid_raw}%]";
180 $rv .= " [<a\nhref=\"${path}$href/t/#u\">threaded</a>" .
183 $rv .= " [<b>threaded</b>|" .
184 "<a\nhref=\"${path}$href/T/#u\">flat</a>]";
187 $fh->write($rv .= '</pre>');
191 my ($ctx, $foot, $srch) = @_;
192 # $_[0] in sub is the Plack callback
193 sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
196 # only private functions below.
198 sub emit_thread_html {
199 my ($res, $ctx, $foot, $srch) = @_;
200 my $mid = $ctx->{mid};
201 my $msgs = load_results($srch->get_thread($mid));
202 my $nr = scalar @$msgs;
203 return missing_thread($res, $ctx) if $nr == 0;
204 my $flat = $ctx->{flat};
210 root_anchor => anchor_for($mid),
215 require PublicInbox::Git;
216 $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
218 pre_anchor_entry($seen, $_) for (@$msgs);
219 __thread_entry($state, $_, 0) for (@$msgs);
221 my $th = thread_results($msgs);
222 thread_entry($state, $_, 0) for $th->rootset;
223 if (my $max = $state->{cur_level}) {
225 ('</ul></li>' x ($max - 1)) . '</ul>');
228 Email::Address->purge_cache;
230 # there could be a race due to a message being deleted in git
231 # but still being in the Xapian index:
232 my $fh = delete $state->{fh} or return missing_thread($res, $ctx);
234 my $final_anchor = $state->{anchor_idx};
235 my $next = "<a\nid=s$final_anchor>";
236 $next .= $final_anchor == 1 ? 'only message in' : 'end of';
237 $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
238 $next .= "\ndownload thread: ";
239 $next .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
240 $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
241 $fh->write('<hr /><pre>' . $next . "\n\n".
242 $foot . '</pre></body></html>');
247 my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
248 my $s = add_text_body($enc, $part, $part_nr, $fhref);
252 $s .= "\n"; # ensure there's a trailing newline
258 my ($ct, $default) = @_;
259 $default ||= $enc_utf8;
260 defined $ct or return $default;
261 my $ct_parsed = parse_content_type($ct);
263 if (my $charset = $ct_parsed->{attributes}->{charset}) {
264 my $enc = find_encoding($charset);
271 sub multipart_text_as_html {
272 my ($mime, $full_pfx, $srch) = @_;
275 my $enc = enc_for($mime->header("Content-Type"));
277 # scan through all parts, looking for displayable text
278 $mime->walk_parts(sub {
280 $part = add_text_body($enc, $part, \$part_nr, $full_pfx, 1);
282 $rv .= "\n" if $part ne '';
288 sub add_filename_line {
292 $fn = $enc->decode($fn);
294 $pad x= ($len/2) if ($len > 0);
295 "$pad " . ascii_html($fn) . " $pad\n";
299 my ($quot, $n, $part_nr, $full_pfx, $final, $do_anchor) = @_;
301 # n.b.: do not use <blockquote> since it screws up alignment
302 # w.r.t. unquoted text. Repliers may rely on pre-formatted
303 # alignment to point out a certain word in quoted text.
305 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
307 my $l = PublicInbox::Linkify->new;
308 my $rv = join('', map { $l->linkify_1($_) } @$quot);
310 $rv = ascii_html($rv);
311 return $l->linkify_2($rv);
314 # show a short snippet of quoted text and link to full version:
315 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
316 my $cur = join(' ', @$quot);
317 @$quot = split(/\s+/, $cur);
320 my $tmp = shift(@$quot);
321 my $len = length($tmp) + length($cur);
322 if ($len > MAX_TRUNC_LEN) {
327 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
329 $cur =~ s/ \z/ .../s;
330 $cur = ascii_html($cur);
332 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
334 # show everything in the full version with anchor from
335 # short version (see above)
336 my $l = PublicInbox::Linkify->new;
337 my $rv .= join('', map { $l->linkify_1($_) } @$quot);
339 $rv = ascii_html($rv);
340 return $l->linkify_2($rv) unless $do_anchor;
342 "<a\nid=q${part_nr}_$nr></a>" . $l->linkify_2($rv);
347 my ($enc_msg, $part, $part_nr, $full_pfx, $do_anchor) = @_;
348 return '' if $part->subparts;
350 my $ct = $part->content_type;
351 # account for filter bugs...
352 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
356 my $enc = enc_for($ct, $enc_msg);
361 $s = $enc->decode($s);
362 my @lines = split(/^/m, $s);
366 my $fn = $part->filename;
367 defined($fn) or $fn = "part #" . ($$part_nr + 1);
368 $s .= add_filename_line($enc, $fn);
372 while (defined(my $cur = shift @lines)) {
374 # show the previously buffered quote inline
376 $s .= flush_quote(\@quot, \$n, $$part_nr,
377 $full_pfx, 0, $do_anchor);
381 my $l = PublicInbox::Linkify->new;
382 $cur = $l->linkify_1($cur);
383 $cur = ascii_html($cur);
384 $s .= $l->linkify_2($cur);
390 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1,
395 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
396 $s =~ s/\A\n+//s; # kill leading blank lines
397 $s =~ s/\s+\z//s; # kill all trailing spaces (final "\n" added if ne '')
401 sub headers_to_html_header {
402 my ($hdr, $full_pfx, $ctx) = @_;
403 my $srch = $ctx->{srch} if $ctx;
406 my $mid = $hdr->header_raw('Message-ID');
407 $mid = PublicInbox::Hval->new_msgid($mid);
408 foreach my $h (qw(From To Cc Subject Date)) {
409 my $v = $hdr->header($h);
410 defined($v) && ($v ne '') or next;
411 $v = PublicInbox::Hval->new($v);
414 my @from = Email::Address->parse($v->raw);
415 $title[1] = ascii_html($from[0]->name);
416 } elsif ($h eq 'Subject') {
417 $title[0] = $v->as_html;
419 $rv .= "$h: <b\nid=t>";
420 $rv .= $v->as_html . "</b>\n";
424 $rv .= "$h: " . $v->as_html . "\n";
427 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
428 my $upfx = $full_pfx ? '' : '../';
429 $rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
432 thread_inline(\$rv, $ctx, $hdr, $upfx);
434 $atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
435 qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
437 $rv .= _parent_headers_nosrch($hdr);
442 ("<html><head><title>". join(' - ', @title) . "</title>$atom".
443 PublicInbox::Hval::STYLE . "</head><body><pre>" . $rv);
447 my ($dst, $ctx, $hdr, $upfx) = @_;
448 my $srch = $ctx->{srch};
449 my $mid = mid_clean($hdr->header_raw('Message-ID'));
450 my $sres = $srch->get_thread($mid);
451 my $nr = $sres->{total};
452 my $expand = "<a\nhref=\"${upfx}t/#u\">expand</a> " .
453 "/ <a\nhref=\"${upfx}t.mbox.gz\">mbox.gz</a>";
456 my $parent = in_reply_to($hdr);
458 if (defined $parent) {
459 $$dst .= "($expand)\n ";
460 $$dst .= ghost_parent("$upfx../", $parent) . "\n";
462 $$dst .= "[no followups, yet] ($expand)\n";
464 $ctx->{next_msg} = undef;
465 $ctx->{parent_msg} = $parent;
469 $$dst .= "~$nr messages ($expand";
470 if ($nr > MAX_INLINE_QUOTED) {
471 $$dst .= qq! / <a\nhref="#b">[scroll down]</a>!;
475 my $subj = $srch->subject_path($hdr->header('Subject'));
477 seen => { $subj => 1 },
480 parent_cmp => defined $parent ? $parent : '',
485 for (thread_results(load_results($sres))->rootset) {
486 inline_dump($dst, $state, $upfx, $_, 0);
488 $$dst .= "<a\nid=b></a>"; # anchor for body start
489 $ctx->{next_msg} = $state->{next_msg};
490 $ctx->{parent_msg} = $state->{parent};
493 sub _parent_headers_nosrch {
497 my $irt = in_reply_to($hdr);
499 my $v = PublicInbox::Hval->new_msgid($irt, 1);
500 my $html = $v->as_html;
501 my $href = $v->as_href;
502 $rv .= "In-Reply-To: <";
503 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
506 my $refs = $hdr->header_raw('References');
508 # avoid redundant URLs wasting bandwidth
510 $seen{$irt} = 1 if defined $irt;
512 my @raw_refs = ($refs =~ /<([^>]+)>/g);
513 foreach my $ref (@raw_refs) {
516 push @refs, linkify_ref_nosrch($ref);
520 $rv .= 'References: '. join(' ', @refs) . "\n";
526 sub mailto_arg_link {
528 my %cc; # everyone else
529 my $to; # this is the From address
531 foreach my $h (qw(From To Cc)) {
532 my $v = $hdr->header($h);
533 defined($v) && ($v ne '') or next;
534 my @addrs = Email::Address->parse($v);
535 foreach my $recip (@addrs) {
536 my $address = $recip->address;
537 my $dst = lc($address);
538 $cc{$dst} ||= $address;
542 Email::Address->purge_cache;
545 my $subj = $hdr->header('Subject') || '';
546 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
547 my $mid = $hdr->header_raw('Message-ID');
548 push @arg, "--in-reply-to='" . ascii_html($mid) . "'";
549 my $irt = uri_escape_utf8($mid);
551 push @arg, '--to=' . ascii_html($to);
552 $to = uri_escape_utf8($to);
553 $subj = uri_escape_utf8($subj);
554 my $cc = join(',', sort values %cc);
555 push @arg, '--cc=' . ascii_html($cc);
556 $cc = uri_escape_utf8($cc);
557 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
564 my ($mime, $standalone, $full_pfx, $ctx, $mhref) = @_;
566 my $srch = $ctx->{srch} if $ctx;
567 my $upfx = $full_pfx ? '../' : '../../';
568 my $tpfx = $full_pfx ? '' : '../';
569 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
572 if ($srch && $standalone) {
573 $idx .= qq{ / follow: <a\nhref="${tpfx}t.atom">Atom feed</a>\n};
576 my $p = $ctx->{parent_msg};
577 my $next = $ctx->{next_msg};
579 $p = PublicInbox::Hval->new_msgid($p);
581 $irt = "<a\nhref=\"$upfx$p/\">parent</a> ";
583 $irt = ' ' x length('parent ');
586 $irt .= "<a\nhref=\"$upfx$next/\">next</a> ";
588 $irt .= ' ' x length('next ');
591 $irt .= "<a\nhref=\"${tpfx}t/#u\">thread</a> ";
593 $irt .= ' ' x length('thread ');
599 $mhref = './' unless defined $mhref;
600 $irt . qq(<a\nhref="${mhref}R/">reply</a>) . $idx;
603 sub linkify_ref_nosrch {
604 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
605 my $html = $v->as_html;
606 my $href = $v->as_href;
607 "<<a\nhref=\"../$href/\">$html</a>>";
613 if ($id !~ /\A[a-f0-9]{40}\z/) {
614 $id = id_compress(mid_clean($id), 1);
619 sub thread_html_head {
620 my ($hdr, $state) = @_;
621 my $res = delete $state->{res} or die "BUG: no Plack callback in {res}";
622 my $fh = $res->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
625 my $s = ascii_html($hdr->header('Subject'));
626 $fh->write("<html><head><title>$s</title>".
627 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
628 qq!href="../t.atom"\ntype="application/atom+xml"/>! .
629 PublicInbox::Hval::STYLE .
633 sub pre_anchor_entry {
634 my ($seen, $mime) = @_;
635 my $id = anchor_for(mid_mime($mime));
636 $seen->{$id} = "#$id"; # save the anchor for children, later
640 my ($upfx, $mid) = @_;
641 # 'subject dummy' is used internally by Mail::Thread
642 return '[no common parent]' if ($mid eq 'subject dummy');
644 $mid = PublicInbox::Hval->new_msgid($mid);
645 my $href = $mid->as_href;
646 my $html = $mid->as_html;
647 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
650 sub thread_adj_level {
651 my ($state, $level) = @_;
653 my $max = $state->{cur_level};
655 return '' if $max == 0; # flat output
657 # reset existing lists
658 my $x = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
659 $state->{fh}->write($x . '</ul>');
660 $state->{cur_level} = 0;
663 if ($level == $max) { # continue existing list
664 $state->{fh}->write('<li>');
665 } elsif ($level < $max) {
666 my $x = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
667 $state->{fh}->write($x .= '<li>');
668 $state->{cur_level} = $level;
669 } else { # ($level > $max) # start a new level
670 $state->{cur_level} = $level;
671 $state->{fh}->write(($max ? '<li>' : '') . '<ul><li>');
677 my ($state, $upfx, $mid, $level) = @_;
678 my $end = '<pre>'. ghost_parent($upfx, $mid) . '</pre>';
679 $state->{fh}->write($end .= thread_adj_level($state, $level));
683 my ($state, $mime, $level) = @_;
685 # lazy load the full message from mini_mime:
687 my $path = mid2path(mid_clean(mid_mime($mime)));
688 Email::MIME->new($state->{ctx}->{git}->cat_file('HEAD:'.$path));
691 thread_html_head($mime, $state) if $state->{anchor_idx} == 0;
692 if (my $ghost = delete $state->{ghost}) {
693 # n.b. ghost messages may only be parents, not children
694 foreach my $g (@$ghost) {
695 ghost_flush($state, '../../', @$g);
698 my $end = thread_adj_level($state, $level);
699 index_entry($mime, $level, $state);
700 $state->{fh}->write($end) if $end;
707 INDENT x ($level - 1);
710 sub __ghost_prepare {
711 my ($state, $node, $level) = @_;
712 my $ghost = $state->{ghost} ||= [];
713 push @$ghost, [ $node->messageid, $level ];
717 my ($state, $node, $level) = @_;
719 if (my $mime = $node->message) {
720 unless (__thread_entry($state, $mime, $level)) {
721 __ghost_prepare($state, $node, $level);
724 __ghost_prepare($state, $node, $level);
727 thread_entry($state, $node->child, $level + 1);
728 thread_entry($state, $node->next, $level);
734 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
739 my $ts = eval { str2time($hdr->header('Date')) };
740 defined($ts) ? $ts : 0;
744 my ($msgs, $nosubject) = @_;
745 require PublicInbox::Thread;
746 my $th = PublicInbox::Thread->new(@$msgs);
748 $Mail::Thread::nosubject = $nosubject;
750 $th->order(*sort_ts);
755 my ($res, $ctx) = @_;
756 require PublicInbox::ExtMsg;
758 $res->(PublicInbox::ExtMsg::ext_msg($ctx))
763 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
767 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
770 my ($dst, $state, $upfx, $hdr, $level) = @_;
771 my $dot = $level == 0 ? '' : '` ';
773 my $cur = $state->{cur};
774 my $mid = mid_clean($hdr->header_raw('Message-ID'));
775 my $f = ascii_html($hdr->header('X-PI-From'));
776 my $d = _msg_date($hdr);
777 my $pfx = ' ' . $d . ' ' . indent_for($level);
779 $state->{first_level} ||= $level;
781 if ($attr ne $state->{prev_attr} || $state->{prev_level} > $level) {
782 $state->{prev_attr} = $attr;
786 $state->{prev_level} = $level;
790 delete $state->{cur};
791 $$dst .= "$pfx$dot<b><a\nid=r\nhref=\"#b\">".
792 "$attr [this message]</a></b>\n";
797 $state->{next_msg} ||= $mid;
800 # Subject is never undef, this mail was loaded from
801 # our Xapian which would've resulted in '' if it were
802 # really missing (and Filter rejects empty subjects)
803 my $s = $hdr->header('Subject');
804 my $h = $state->{srch}->subject_path($s);
805 if ($state->{seen}->{$h}) {
808 $state->{seen}->{$h} = 1;
809 $s = PublicInbox::Hval->new($s);
812 my $m = PublicInbox::Hval->new_msgid($mid);
813 $m = $upfx . '../' . $m->as_href . '/';
815 $$dst .= "$pfx$dot<a\nhref=\"$m\">$s</a> $attr\n";
817 $$dst .= "$pfx$dot<a\nhref=\"$m\">$f</a>\n";
822 my ($dst, $state, $upfx, $node, $level) = @_;
824 if (my $mime = $node->message) {
825 my $hdr = $mime->header_obj;
826 my $mid = mid_clean($hdr->header_raw('Message-ID'));
827 if ($mid eq $state->{parent_cmp}) {
828 $state->{parent} = $mid;
830 _inline_header($dst, $state, $upfx, $hdr, $level);
832 my $dot = $level == 0 ? '' : '` ';
833 my $pfx = (' ' x length(' 1970-01-01 13:37 ')).
834 indent_for($level) . $dot;
836 $$dst .= ghost_parent("$upfx../", $node->messageid) . "\n";
838 inline_dump($dst, $state, $upfx, $node->child, $level+1);
839 inline_dump($dst, $state, $upfx, $node->next, $level);
844 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
845 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
851 (eval { $b->topmost->message->header('X-PI-TS') } || 0) <=>
852 (eval { $a->topmost->message->header('X-PI-TS') } || 0)
856 # accumulate recent topics if search is supported
857 # returns 1 if done, undef if not
859 my ($state, $node, $level) = @_;
861 my $child_adjust = 1;
863 if (my $x = $node->message) {
867 $subj = $x->header('Subject');
868 $subj = $state->{srch}->subject_normalized($subj);
870 if (++$state->{subjs}->{$subj} == 1) {
871 push @{$state->{order}}, [ $level, $subj ];
874 my $mid = mid_clean($x->header_raw('Message-ID'));
876 my $ts = $x->header('X-PI-TS');
877 my $exist = $state->{latest}->{$subj};
878 if (!$exist || $exist->[1] < $ts) {
879 $state->{latest}->{$subj} = [ $mid, $ts ];
882 # ghost message, do not bump level
886 add_topic($state, $node->child, $level + $child_adjust);
887 add_topic($state, $node->next, $level);
892 my $order = $state->{order};
893 my $subjs = $state->{subjs};
894 my $latest = $state->{latest};
895 return "\n[No topics in range]</pre>" unless (scalar @$order);
900 while (defined(my $info = shift @$order)) {
901 my ($level, $subj) = @$info;
902 my $n = delete $subjs->{$subj};
903 my ($mid, $ts) = @{delete $latest->{$subj}};
904 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
905 $subj = PublicInbox::Hval->new($subj)->as_html;
906 $pfx = indent_for($level);
907 my $nl = $level == $prev ? "\n" : '';
908 my $dot = $level == 0 ? '' : '` ';
909 $dst .= "$nl$pfx$dot<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
912 my $attr = " $ts UTC";
914 # $n isn't the total number of posts on the topic,
915 # just the number of posts in the current results window
916 $n = $n == 1 ? '' : " ($n+ messages)";
918 if ($level == 0 || $attr ne $prev_attr) {
919 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
920 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
921 $pfx .= INDENT if $level > 0;
922 $dst .= $pfx . $attr . $n . " - $mbox / $atom\n";
929 sub emit_index_topics {
931 my $off = $state->{ctx}->{cgi}->param('o');
932 $off = 0 unless defined $off;
933 $state->{order} = [];
934 $state->{subjs} = {};
935 $state->{latest} = {};
937 my %opts = ( offset => int $off, limit => $max * 4 );
938 while (scalar @{$state->{order}} < $max) {
939 my $sres = $state->{srch}->query('', \%opts);
940 my $nr = scalar @{$sres->{msgs}} or last;
942 for (rsort_ts(thread_results(load_results($sres), 1)->rootset)){
943 add_topic($state, $_, 0);
945 $opts{offset} += $nr;
948 $state->{fh}->write(dump_topics($state));