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::MIME::Header;
13 use PublicInbox::Hval qw/ascii_html/;
14 use PublicInbox::Linkify;
15 use PublicInbox::MID qw/mid_clean id_compress mid_mime/;
16 use PublicInbox::MsgIter;
17 use PublicInbox::Address;
18 use PublicInbox::WwwStream;
21 use constant INDENT => ' ';
22 use constant TCHILD => '` ';
23 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
25 # public functions: (unstable)
27 my ($ctx, $mime, $footer) = @_;
28 my $hdr = $mime->header_obj;
29 my $tip = _msg_html_prepare($hdr, $ctx);
30 PublicInbox::WwwStream->response($ctx, 200, sub {
33 $tip . multipart_text_as_html($mime, '') .
36 # fake an EOF if generating the footer fails;
37 # we want to at least show the message if something
40 '<pre>' . html_footer($hdr, 1, $ctx) .
41 '</pre>' . msg_reply($ctx, $hdr)
49 # /$INBOX/$MESSAGE_ID/#R
53 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
55 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
57 my ($arg, $link) = mailto_arg_link($hdr);
58 push @$arg, '/path/to/YOUR_REPLY';
59 $arg = join(" \\\n ", '', @$arg);
62 id=R>You may reply publically to <a
63 href=#t>this message</a> via
64 plain-text email using any one of the following methods:
66 * Save the following mbox file, import it into your mail client,
67 and reply-to-all from there: <a
70 Avoid top-posting and favor interleaved quoting:
72 href="$p_url">$p_url</a>
74 * Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,
75 and <b>--in-reply-to</b> switches of git-send-email(1):
80 href="$se_url">$se_url</a>
82 * If your mail client supports setting the <b>In-Reply-To</b> header
83 via mailto: links, try the <a
84 href="$link">mailto: link</a></pre>
90 my $irt = $hdr->header_raw('In-Reply-To');
92 return mid_clean($irt) if (defined $irt);
94 my $refs = $hdr->header_raw('References');
95 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
101 sub _hdr_names ($$) {
102 my ($hdr, $field) = @_;
103 my $val = $hdr->header($field) or return '';
104 ascii_html(join(', ', PublicInbox::Address::names($val)));
108 my ($nr, $singular, $plural) = @_;
109 return "0 $plural" if $nr == 0;
110 $nr == 1 ? "$nr $singular" : "$nr $plural";
113 # this is already inside a <pre>
115 my ($mime, $ctx, $more) = @_;
116 my $srch = $ctx->{srch};
117 my $hdr = $mime->header_obj;
118 my $subj = $hdr->header('Subject');
120 my $mid_raw = mid_clean(mid_mime($mime));
121 my $id = id_compress($mid_raw, 1);
123 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
125 my $root_anchor = $ctx->{root_anchor} || '';
126 my $irt = in_reply_to($hdr);
128 my $rv = '<b>'.ascii_html($subj).'</b>';
129 $rv = "<u\nid=u>$rv</u>" if $root_anchor eq $id_m;
131 $rv .= _th_index_lite($mid_raw, $irt, $id, $ctx);
133 foreach my $f (qw(To Cc)) {
134 my $dst = _hdr_names($hdr, $f);
135 push @tocc, "$f: $dst" if $dst ne '';
137 $rv .= "From: "._hdr_names($hdr, 'From').' @ '._msg_date($hdr)." UTC";
138 my $upfx = $ctx->{-upfx};
139 my $mhref = $upfx . $mid->as_href . '/';
140 $rv .= qq{ (<a\nhref="$mhref/">permalink</a> / };
141 $rv .= qq{<a\nhref="$mhref/raw">raw</a>)\n};
142 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
144 my $mapping = $ctx->{mapping};
145 if (!$mapping && $irt) {
146 my $mirt = PublicInbox::Hval->new_msgid($irt);
147 my $href = $upfx . $mirt->as_href . '/';
148 my $html = $mirt->as_html;
149 $rv .= qq(In-Reply-To: <<a\nhref="$href/">$html</a>>\n)
153 # scan through all parts, looking for displayable text
154 msg_iter($mime, sub { $rv .= add_text_body($mhref, $_[0]) });
157 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
158 "<a\nhref=\"$mhref\">permalink</a>" .
159 " / <a\nhref=\"${mhref}raw\">raw</a>" .
160 " / <a\nhref=\"${mhref}#R\">reply</a>";
161 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
162 $rv .= " [relevance $pct->{$mid_raw}%]";
164 my $threaded = 'threaded';
168 $flat = "<b>$flat</b>";
170 $threaded = "<b>$threaded</b>";
172 $rv .= " / [<a\nhref=\"${mhref}T/#u\">$flat</a>";
173 $rv .= "|<a\nhref=\"${mhref}t/#u\">$threaded</a>]";
174 $rv .= " / <a\nhref=#r$id>$ctx->{s_nr}</a>";
177 $rv .= $more ? "\n\n" : "\n";
180 sub pad_link ($$;$) {
181 my ($mid, $level, $s) = @_;
183 my $id = id_compress($mid, 1);
184 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
188 my ($mid_raw, $irt, $id, $ctx) = @_;
190 my $mapping = $ctx->{mapping} or return $rv;
192 # map = [children, attr, node, idx, level]
193 my $map = $mapping->{$mid_raw};
194 my $nr_c = scalar @{$map->[0]};
196 my $level = $map->[4];
199 my $irt_map = $mapping->{$irt};
200 my $siblings = $irt_map->[0];
201 $nr_s = scalar(@$siblings) - 1;
202 $rv .= $pad . $irt_map->[1];
204 my $prev = $siblings->[$idx - 1];
205 my $pmid = $prev->messageid;
207 my $s = ($idx - 1). ' preceding siblings ...';
208 $rv .= pad_link($pmid, $level, $s);
209 } elsif ($idx == 2) {
210 my $ppmid = $siblings->[0]->messageid;
211 $rv .= $pad . $mapping->{$ppmid}->[1];
213 $rv .= $pad . $mapping->{$pmid}->[1];
216 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
217 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
218 my $this = $map->[1];
219 $this =~ s!\n\z!</b>\n!s;
220 $this =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
222 my $node = $map->[2];
223 if (my $child = $node->child) {
224 my $cmid = $child->messageid;
225 $rv .= $pad . $mapping->{$cmid}->[1];
227 my $s = ($nr_c - 1). ' more replies';
228 $rv .= pad_link($cmid, $level + 1, $s);
229 } elsif (my $cn = $child->next) {
230 $rv .= $pad . $mapping->{$cn->messageid}->[1];
233 if (my $next = $node->next) {
234 my $nmid = $next->messageid;
235 $rv .= $pad . $mapping->{$nmid}->[1];
236 my $nnext = $nr_s - $idx;
238 my $s = ($nnext - 1).' subsequent siblings';
239 $rv .= pad_link($nmid, $level, $s);
240 } elsif (my $nn = $next->next) {
241 $rv .= $pad . $mapping->{$nn->messageid}->[1];
244 $rv .= "<a\nhref=#e$id\nid=m$id>_</a> ";
245 $rv .= "<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
249 my ($th, $ctx, $cb) = @_;
250 my @q = map { (0, $_) } $th->rootset;
252 my $level = shift @q;
253 my $node = shift @q or next;
254 $cb->($ctx, $level, $node);
255 unshift @q, $level+1, $node->child, $level, $node->next;
260 my ($ctx, $level, $node) = @_;
261 my $mapping = $ctx->{mapping};
263 if (my $parent = $node->parent) {
264 my $m = $mapping->{$parent->messageid}->[0];
268 $mapping->{$node->messageid} = [ [], '', $node, $idx, $level ];
269 skel_dump($ctx, $level, $node);
272 sub thread_index_entry {
273 my ($ctx, $level, $mime) = @_;
274 my ($beg, $end) = thread_adj_level($ctx, $level);
275 $beg . '<pre>' . index_entry($mime, $ctx, 0) . '</pre>' . $end;
278 sub stream_thread ($$) {
280 my $inbox = $ctx->{-inbox};
282 my @q = map { (0, $_) } $th->rootset;
286 my $node = shift @q or next;
287 unshift @q, $level+1, $node->child, $level, $node->next;
288 $mime = $inbox->msg_by_mid($node->messageid) and last;
290 return missing_thread($ctx) unless $mime;
292 $mime = Email::MIME->new($mime);
293 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
294 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
295 PublicInbox::WwwStream->response($ctx, 200, sub {
299 my $node = shift @q or next;
300 unshift @q, $level+1, $node->child, $level, $node->next;
301 my $mid = $node->messageid;
302 if ($mime = $inbox->msg_by_mid($mid)) {
303 $mime = Email::MIME->new($mime);
304 return thread_index_entry($ctx, $level, $mime);
306 return ghost_index_entry($ctx, $level, $mid);
309 my $ret = join('', thread_adj_level($ctx, 0));
310 $ret .= ${$ctx->{dst}}; # skel
318 my $mid = $ctx->{mid};
319 my $sres = $ctx->{srch}->get_thread($mid, { asc => 1 });
320 my $msgs = load_results($sres);
321 my $nr = $sres->{total};
322 return missing_thread($ctx) if $nr == 0;
323 my $skel = '<hr /><pre>';
324 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
325 $skel .= ", back to <a\nhref=\"../../\">index</a>";
326 $skel .= "\n<a\nid=t>$nr+ messages in thread:</a> (download: ";
327 $skel .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
328 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
329 $ctx->{-upfx} = '../../';
330 $ctx->{cur_level} = 0;
331 $ctx->{dst} = \$skel;
332 $ctx->{prev_attr} = '';
333 $ctx->{prev_level} = 0;
334 $ctx->{root_anchor} = anchor_for($mid);
336 $ctx->{mapping} = {};
337 $ctx->{s_nr} = "$nr+ messages in thread";
339 my $th = thread_results($msgs);
340 walk_thread($th, $ctx, *pre_thread);
342 return stream_thread($th, $ctx) unless $ctx->{flat};
344 # flat display: lazy load the full message from mini_mime:
345 my $inbox = $ctx->{-inbox};
347 while ($mime = shift @$msgs) {
348 $mime = $inbox->msg_by_mid(mid_clean(mid_mime($mime))) and last;
350 return missing_thread($ctx) unless $mime;
351 $mime = Email::MIME->new($mime);
352 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
353 $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
355 PublicInbox::WwwStream->response($ctx, 200, sub {
357 while ($mime = shift @$msgs) {
358 $mid = mid_clean(mid_mime($mime));
359 $mime = $inbox->msg_by_mid($mid) and last;
362 $mime = Email::MIME->new($mime);
363 return index_entry($mime, $ctx, scalar @$msgs);
370 sub multipart_text_as_html {
371 my ($mime, $upfx) = @_;
374 # scan through all parts, looking for displayable text
375 msg_iter($mime, sub {
377 $rv .= add_text_body($upfx, $p);
383 my ($s, $l, $quot) = @_;
385 # show everything in the full version with anchor from
386 # short version (see above)
387 my $rv = $l->linkify_1(join('', @$quot));
390 # we use a <span> here to allow users to specify their own
391 # color for quoted text
392 $rv = $l->linkify_2(ascii_html($rv));
393 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
396 sub attach_link ($$$$) {
397 my ($upfx, $ct, $p, $fn) = @_;
398 my ($part, $depth, @idx) = @$p;
399 my $nl = $idx[-1] > 1 ? "\n" : '';
400 my $idx = join('.', @idx);
401 my $size = bytes::length($part->body);
402 $ct ||= 'text/plain';
403 $ct =~ s/;.*//; # no attributes
404 $ct = ascii_html($ct);
405 my $desc = $part->header('Content-Description');
406 $desc = $fn unless defined $desc;
407 $desc = '' unless defined $desc;
409 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
411 } elsif ($ct eq 'text/plain') {
416 my @ret = qq($nl<a\nhref="$upfx$idx-$sfn">[-- Attachment #$idx: );
417 my $ts = "Type: $ct, Size: $size bytes";
418 push(@ret, ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]");
419 join('', @ret, "</a>\n");
423 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
424 my ($part, $depth, @idx) = @$p;
425 my $ct = $part->content_type;
426 my $fn = $part->filename;
428 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
429 return attach_link($upfx, $ct, $p, $fn);
432 my $s = eval { $part->body_str };
434 # badly-encoded message? tell the world about it!
435 return attach_link($upfx, $ct, $p, $fn) if $@;
437 my @lines = split(/^/m, $s);
439 if (defined($fn) || $depth > 0) {
440 $s .= attach_link($upfx, $ct, $p, $fn);
444 my $l = PublicInbox::Linkify->new;
445 while (defined(my $cur = shift @lines)) {
447 # show the previously buffered quote inline
448 flush_quote(\$s, $l, \@quot) if @quot;
451 $cur = $l->linkify_1($cur);
452 $cur = ascii_html($cur);
453 $s .= $l->linkify_2($cur);
462 flush_quote(\$s, $l, \@quot);
464 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
465 $s =~ s/\A\n+//s; # kill leading blank lines
466 $s =~ s/\s+\z//s; # kill all trailing spaces
470 sub _msg_html_prepare {
471 my ($hdr, $ctx) = @_;
472 my $srch = $ctx->{srch} if $ctx;
474 my $rv = "<pre\nid=b>"; # anchor for body start
477 $ctx->{-upfx} = '../';
480 my $mid = $hdr->header_raw('Message-ID');
481 $mid = PublicInbox::Hval->new_msgid($mid);
482 foreach my $h (qw(From To Cc Subject Date)) {
483 my $v = $hdr->header($h);
484 defined($v) && ($v ne '') or next;
485 $v = PublicInbox::Hval->new($v);
488 my @n = PublicInbox::Address::names($v->raw);
489 $title[1] = ascii_html(join(', ', @n));
490 } elsif ($h eq 'Subject') {
491 $title[0] = $v->as_html;
493 $rv .= qq($h: <a\nhref="#r"\nid=t>);
494 $rv .= $v->as_html . "</a>\n";
499 $v =~ s/(\@[^,]+,) /$1\n\t/g if ($h eq 'Cc' || $h eq 'To');
503 $title[0] ||= '(no subject)';
504 $ctx->{-title_html} = join(' - ', @title);
505 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
506 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
507 $rv .= _parent_headers($hdr, $srch);
512 my ($dst, $ctx, $hdr, $tpfx) = @_;
513 my $srch = $ctx->{srch};
514 my $mid = mid_clean($hdr->header_raw('Message-ID'));
515 my $sres = $srch->get_thread($mid);
516 my $nr = $sres->{total};
517 my $expand = qq(<a\nhref="${tpfx}T/#u">expand</a> ) .
518 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
519 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
521 my $parent = in_reply_to($hdr);
523 if (defined $parent) {
524 $$dst .= "($expand)\n ";
525 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
527 $$dst .= "[no followups, yet] ($expand)\n";
529 $ctx->{next_msg} = undef;
530 $ctx->{parent_msg} = $parent;
534 $$dst .= "$nr+ messages in thread ($expand";
535 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
537 my $subj = $srch->subject_path($hdr->header('Subject'));
538 $ctx->{seen} = { $subj => 1 };
540 $ctx->{prev_attr} = '';
541 $ctx->{prev_level} = 0;
543 walk_thread(thread_results(load_results($sres)), $ctx, *skel_dump);
544 $ctx->{parent_msg} = $parent;
547 sub _parent_headers {
548 my ($hdr, $srch) = @_;
551 my $irt = in_reply_to($hdr);
553 my $v = PublicInbox::Hval->new_msgid($irt, 1);
554 my $html = $v->as_html;
555 my $href = $v->as_href;
556 $rv .= "In-Reply-To: <";
557 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
560 # do not display References: if search is present,
561 # we show the thread skeleton at the bottom, instead.
564 my $refs = $hdr->header_raw('References');
566 # avoid redundant URLs wasting bandwidth
568 $seen{$irt} = 1 if defined $irt;
570 my @raw_refs = ($refs =~ /<([^>]+)>/g);
571 foreach my $ref (@raw_refs) {
574 push @refs, linkify_ref_nosrch($ref);
578 $rv .= 'References: '. join("\n\t", @refs) . "\n";
584 sub squote_maybe ($) {
586 if ($val =~ m{([^\w@\./,\%\+\-])}) {
587 $val =~ s/(['!])/'\\$1'/g; # '!' for csh
593 sub mailto_arg_link {
595 my %cc; # everyone else
596 my $to; # this is the From address
598 foreach my $h (qw(From To Cc)) {
599 my $v = $hdr->header($h);
600 defined($v) && ($v ne '') or next;
601 my @addrs = PublicInbox::Address::emails($v);
602 foreach my $address (@addrs) {
603 my $dst = lc($address);
604 $cc{$dst} ||= $address;
610 my $subj = $hdr->header('Subject') || '';
611 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
612 my $mid = $hdr->header_raw('Message-ID');
613 push @arg, '--in-reply-to='.ascii_html(squote_maybe(mid_clean($mid)));
614 my $irt = uri_escape_utf8($mid);
616 push @arg, '--to=' . ascii_html($to);
617 $to = uri_escape_utf8($to);
618 $subj = uri_escape_utf8($subj);
619 my $cc = join(',', sort values %cc);
620 push @arg, '--cc=' . ascii_html($cc);
621 $cc = uri_escape_utf8($cc);
622 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
625 (\@arg, ascii_html($href));
629 my ($hdr, $standalone, $ctx, $rhref) = @_;
631 my $srch = $ctx->{srch} if $ctx;
634 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
638 thread_skel(\$idx, $ctx, $hdr, $tpfx);
639 my $p = $ctx->{parent_msg};
640 my $next = $ctx->{next_msg};
642 $p = PublicInbox::Hval->new_msgid($p);
644 $irt = "<a\nhref=\"$upfx$p/\"\nrel=prev>parent</a> ";
646 $irt = ' ' x length('parent ');
649 my $n = PublicInbox::Hval->new_msgid($next)->as_href;
650 $irt .= "<a\nhref=\"$upfx$n/\"\nrel=next>next</a> ";
652 $irt .= ' ' x length('next ');
658 $irt .= qq(<a\nhref="$rhref">reply</a>);
662 sub linkify_ref_nosrch {
663 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
664 my $html = $v->as_html;
665 my $href = $v->as_href;
666 "<<a\nhref=\"../$href/\">$html</a>>";
671 'm' . id_compress($msgid, 1);
675 my ($upfx, $mid) = @_;
676 # 'subject dummy' is used internally by Mail::Thread
677 return '[no common parent]' if ($mid eq 'subject dummy');
679 $mid = PublicInbox::Hval->new_msgid($mid);
680 my $href = $mid->as_href;
681 my $html = $mid->as_html;
682 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
687 INDENT x ($level - 1);
693 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
698 my $ts = eval { str2time($hdr->header('Date')) };
699 defined($ts) ? $ts : 0;
704 require PublicInbox::Thread;
705 my $th = PublicInbox::Thread->new(@$msgs);
707 $th->order(*sort_ts);
713 require PublicInbox::ExtMsg;
714 PublicInbox::ExtMsg::ext_msg($ctx);
719 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
723 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
726 my ($ctx, $hdr, $level) = @_;
728 my $dst = $ctx->{dst};
729 my $cur = $ctx->{cur};
730 my $mid = mid_clean($hdr->header_raw('Message-ID'));
731 my $f = ascii_html($hdr->header('X-PI-From'));
732 my $d = _msg_date($hdr) . ' ' . indent_for($level) . th_pfx($level);
734 $ctx->{first_level} ||= $level;
736 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
737 $ctx->{prev_attr} = $attr;
741 $ctx->{prev_level} = $level;
747 $$dst .= "<b><a\nid=r\nhref=\"#t\">".
748 "$attr [this message]</a></b>\n";
752 $ctx->{next_msg} ||= $mid;
755 # Subject is never undef, this mail was loaded from
756 # our Xapian which would've resulted in '' if it were
757 # really missing (and Filter rejects empty subjects)
758 my $s = $hdr->header('Subject');
759 my $h = $ctx->{srch}->subject_path($s);
760 if ($ctx->{seen}->{$h}) {
763 $ctx->{seen}->{$h} = 1;
764 $s = PublicInbox::Hval->new($s);
767 my $m = PublicInbox::Hval->new_msgid($mid);
769 my $mapping = $ctx->{mapping};
770 my $end = defined($s) ? "$s</a> $f\n" : "$f</a>\n";
772 my $map = $mapping->{$mid};
773 $id = id_compress($mid, 1);
775 $map->[1] = "$d<a\nhref=\"$m\">$end";
778 $m = $ctx->{-upfx}.$m->as_href.'/';
780 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
784 my ($ctx, $level, $node) = @_;
785 if (my $mime = $node->message) {
786 _skel_header($ctx, $mime->header_obj, $level);
788 my $mid = $node->messageid;
789 my $dst = $ctx->{dst};
790 my $mapping = $ctx->{mapping};
791 my $map = $mapping->{$mid} if $mapping;
792 if ($mid eq 'subject dummy') {
793 my $ncp = "\t[no common parent]\n";
794 $map->[1] = $ncp if $map;
798 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
800 $d .= indent_for($level) . th_pfx($level);
801 my $upfx = $ctx->{-upfx};
802 my $m = PublicInbox::Hval->new_msgid($mid);
803 my $href = $upfx . $m->as_href . '/';
804 my $html = $m->as_html;
807 my $id = id_compress($mid, 1);
808 $map->[1] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
809 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
811 $d .= qq{<<a\nhref="$href">$html</a>>\n};
819 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
820 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
824 sub _tryload_ghost ($$) {
825 my ($srch, $mid) = @_;
826 my $smsg = $srch->lookup_mail($mid) or return;
830 # accumulate recent topics if search is supported
831 # returns 1 if done, undef if not
833 my ($ctx, $level, $node) = @_;
834 my $srch = $ctx->{srch};
835 my $mid = $node->messageid;
836 my $x = $node->message || _tryload_ghost($srch, $mid);
840 $subj = $x->header('Subject');
841 $subj = $srch->subject_normalized($subj);
842 $ts = $x->header('X-PI-TS');
843 } else { # ghost message, do not bump level
847 if (++$ctx->{subjs}->{$subj} == 1) {
848 push @{$ctx->{order}}, [ $level, $subj ];
850 my $exist = $ctx->{latest}->{$subj};
851 if (!$exist || $exist->[1] < $ts) {
852 $ctx->{latest}->{$subj} = [ $mid, $ts ];
858 my $order = $ctx->{order};
859 my $subjs = $ctx->{subjs};
860 my $latest = $ctx->{latest};
862 return $fh->write("\n[No topics in range]</pre>") unless scalar @$order;
868 while (defined(my $info = shift @$order)) {
869 my ($level, $subj) = @$info;
870 my $n = delete $subjs->{$subj};
871 my ($mid, $ts) = @{delete $latest->{$subj}};
872 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
873 $pfx = indent_for($level);
874 my $nl = $level == $prev ? "\n" : '';
879 $cur ||= [ $ts, '' ];
880 $cur->[0] = $ts if $ts > $cur->[0];
881 $cur->[1] .= $nl . $pfx . th_pfx($level);
882 if ($ts == -666) { # ghost
883 $cur->[1] .= ghost_parent('', $mid) . "\n";
884 next; # child will have mbox / atom link
887 $subj = PublicInbox::Hval->new($subj)->as_html;
888 $cur->[1] .= "<a\nhref=\"$mid/T/#u\"><b>$subj</b></a>\n";
890 my $attr = " $ts UTC";
892 # $n isn't the total number of posts on the topic,
893 # just the number of posts in the current results window
894 $n = $n == 1 ? '' : " ($n+ messages)";
896 if ($level == 0 || $attr ne $prev_attr) {
897 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
898 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
899 $pfx .= INDENT if $level > 0;
900 $cur->[1] .= $pfx . $attr . $n . " - $mbox / $atom\n";
904 push @recent, $cur if $cur;
905 @recent = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @recent;
906 $fh->write(join('', @recent) . '</pre>');
909 sub emit_index_topics {
911 my ($off) = (($ctx->{cgi}->param('o') || '0') =~ /(\d+)/);
916 my %opts = ( offset => $off, limit => $max * 4 );
917 while (scalar @{$ctx->{order}} < $max) {
918 my $sres = $ctx->{srch}->query('', \%opts);
919 my $nr = scalar @{$sres->{msgs}} or last;
920 $sres = load_results($sres);
921 walk_thread(thread_results($sres), $ctx, *add_topic);
922 $opts{offset} += $nr;
929 sub thread_adj_level {
930 my ($ctx, $level) = @_;
932 my $max = $ctx->{cur_level};
934 return ('', '') if $max == 0; # flat output
936 # reset existing lists
937 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
938 $ctx->{cur_level} = 0;
940 } elsif ($level == $max) { # continue existing list
942 } elsif ($level < $max) {
943 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
944 $ctx->{cur_level} = $level;
945 ("$beg<li>", '</li>');
946 } else { # ($level > $max) # start a new level
947 $ctx->{cur_level} = $level;
948 my $beg = ($max ? '<li>' : '') . '<ul><li>';
953 sub ghost_index_entry {
954 my ($ctx, $level, $mid) = @_;
955 my ($beg, $end) = thread_adj_level($ctx, $level);
956 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $mid) . '</pre>' . $end;