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->new($ctx, 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
52 my $se_url = 'https://git-htmldocs.bogomips.org/git-send-email.html';
54 my ($arg, $link) = mailto_arg_link($hdr);
55 push @$arg, '/path/to/YOUR_REPLY';
58 "You may reply publically to <a\nhref=#t>this message</a> via\n".
59 "plain-text email using any one of the following methods:\n\n" .
60 "* Save the following mbox file, import it into your mail client,\n" .
61 " and reply-to-all from there: <a\nhref=raw>mbox</a>\n\n" .
62 "* Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,\n" .
63 " and <b>--in-reply-to</b> switches of git-send-email(1):\n\n" .
64 " git send-email \\\n " .
65 join(" \\\n ", @$arg ). "\n\n" .
66 qq( <a\nhref="$se_url">$se_url</a>\n\n) .
67 "* If your mail client supports setting the <b>In-Reply-To</b>" .
68 " header\n via mailto: links, try the " .
69 qq(<a\nhref="$link">mailto: link</a>\n) .
75 my $irt = $hdr->header_raw('In-Reply-To');
77 return mid_clean($irt) if (defined $irt);
79 my $refs = $hdr->header_raw('References');
80 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
87 my ($hdr, $field) = @_;
88 my $val = $hdr->header($field) or return '';
89 ascii_html(join(', ', PublicInbox::Address::names($val)));
93 my ($nr, $singular, $plural) = @_;
94 return "0 $plural" if $nr == 0;
95 $nr == 1 ? "$nr $singular" : "$nr $plural";
98 # this is already inside a <pre>
100 my ($mime, $ctx, $more) = @_;
101 my $srch = $ctx->{srch};
102 my $hdr = $mime->header_obj;
103 my $subj = $hdr->header('Subject');
105 my $mid_raw = mid_clean(mid_mime($mime));
106 my $id = id_compress($mid_raw, 1);
108 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
110 my $root_anchor = $ctx->{root_anchor} || '';
111 my $irt = in_reply_to($hdr);
113 my $rv = '<b>'.ascii_html($subj).'</b>';
114 $rv = "<u\nid=u>$rv</u>" if $root_anchor eq $id_m;
116 $rv .= _th_index_lite($mid_raw, $irt, $id, $ctx);
118 foreach my $f (qw(To Cc)) {
119 my $dst = _hdr_names($hdr, $f);
120 push @tocc, "$f: $dst" if $dst ne '';
122 $rv .= "From: "._hdr_names($hdr, 'From').' @ '._msg_date($hdr)." UTC";
123 my $upfx = $ctx->{-upfx};
124 my $mhref = $upfx . $mid->as_href . '/';
125 $rv .= qq{ (<a\nhref="$mhref/">permalink</a> / };
126 $rv .= qq{<a\nhref="$mhref/raw">raw</a>)\n};
127 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
129 my $mapping = $ctx->{mapping};
130 if (!$mapping && $irt) {
131 my $mirt = PublicInbox::Hval->msgid($irt);
132 my $href = $upfx . $mirt->as_href . '/';
133 my $html = $mirt->as_html;
134 $rv .= qq(In-Reply-To: <<a\nhref="$href/">$html</a>>\n)
138 # scan through all parts, looking for displayable text
139 msg_iter($mime, sub { $rv .= add_text_body($mhref, $_[0]) });
142 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
143 "<a\nhref=\"$mhref\">permalink</a>" .
144 " / <a\nhref=\"${mhref}raw\">raw</a>" .
145 " / <a\nhref=\"${mhref}#R\">reply</a>";
146 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
147 $rv .= " [relevance $pct->{$mid_raw}%]";
149 my $threaded = 'threaded';
153 $flat = "<b>$flat</b>";
155 $threaded = "<b>$threaded</b>";
157 $rv .= " [<a\nhref=\"${mhref}t/#u\">$threaded</a>";
158 $rv .= "|<a\nhref=\"${mhref}T/#u\">$flat</a>]";
161 $rv .= $more ? "\n\n" : "\n";
164 sub pad_link ($$;$) {
165 my ($mid, $level, $s) = @_;
167 my $id = id_compress($mid, 1);
168 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
172 my ($mid_raw, $irt, $id, $ctx) = @_;
174 my $mapping = $ctx->{mapping} or return $rv;
176 # map = [children, attr, node, idx, level]
177 my $map = $mapping->{$mid_raw};
178 my $nr_c = scalar @{$map->[0]};
180 my $level = $map->[4];
183 my $irt_map = $mapping->{$irt};
184 my $siblings = $irt_map->[0];
185 $nr_s = scalar(@$siblings) - 1;
186 $rv .= $pad . $irt_map->[1];
188 my $prev = $siblings->[$idx - 1];
189 my $pmid = $prev->messageid;
191 my $s = ($idx - 1). ' preceding siblings ...';
192 $rv .= pad_link($pmid, $level, $s);
193 } elsif ($idx == 2) {
194 my $ppmid = $siblings->[0]->messageid;
195 $rv .= $pad . $mapping->{$ppmid}->[1];
197 $rv .= $pad . $mapping->{$pmid}->[1];
200 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
201 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
202 my $this = $map->[1];
203 $this =~ s!\n\z!</b>\n!s;
204 $this =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
206 my $node = $map->[2];
207 if (my $child = $node->child) {
208 my $cmid = $child->messageid;
209 $rv .= $pad . $mapping->{$cmid}->[1];
211 my $s = ($nr_c - 1). ' more replies';
212 $rv .= pad_link($cmid, $level + 1, $s);
213 } elsif (my $cn = $child->next) {
214 $rv .= $pad . $mapping->{$cn->messageid}->[1];
217 if (my $next = $node->next) {
218 my $nmid = $next->messageid;
219 $rv .= $pad . $mapping->{$nmid}->[1];
220 my $nnext = $nr_s - $idx;
222 my $s = ($nnext - 1).' subsequent siblings';
223 $rv .= pad_link($nmid, $level, $s);
224 } elsif (my $nn = $next->next) {
225 $rv .= $pad . $mapping->{$nn->messageid}->[1];
228 $rv .= "<a\nhref=#e$id\nid=m$id>_</a> <a\nhref=#r$id>$s_s, $s_c</a>\n";
232 my ($th, $ctx, $cb) = @_;
233 my @q = map { (0, $_) } $th->rootset;
235 my $level = shift @q;
236 my $node = shift @q or next;
237 $cb->($ctx, $level, $node);
238 unshift @q, $level+1, $node->child, $level, $node->next;
243 my ($ctx, $level, $node) = @_;
244 my $mapping = $ctx->{mapping};
246 if (my $parent = $node->parent) {
247 my $m = $mapping->{$parent->messageid}->[0];
251 $mapping->{$node->messageid} = [ [], '', $node, $idx, $level ];
252 skel_dump($ctx, $level, $node);
255 sub thread_index_entry {
256 my ($ctx, $level, $mime) = @_;
257 my ($beg, $end) = thread_adj_level($ctx, $level);
258 $beg . '<pre>' . index_entry($mime, $ctx, 0) . '</pre>' . $end;
261 sub stream_thread ($$) {
263 my $inbox = $ctx->{-inbox};
265 my @q = map { (0, $_) } $th->rootset;
269 my $node = shift @q or next;
270 unshift @q, $level+1, $node->child, $level, $node->next;
271 $mime = $inbox->msg_by_mid($node->messageid) and last;
273 return missing_thread($ctx) unless $mime;
275 $mime = Email::MIME->new($mime);
276 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
277 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
278 my $body = PublicInbox::WwwStream->new($ctx, sub {
282 my $node = shift @q or next;
283 unshift @q, $level+1, $node->child, $level, $node->next;
284 my $mid = $node->messageid;
285 if ($mime = $inbox->msg_by_mid($mid)) {
286 $mime = Email::MIME->new($mime);
287 return thread_index_entry($ctx, $level, $mime);
289 return ghost_index_entry($ctx, $level, $mid);
292 my $ret = join('', thread_adj_level($ctx, 0));
293 $ret .= ${$ctx->{dst}}; # skel
297 [ 200, ['Content-Type', 'text/html; charset=UTF-8'], $body ];
302 my $mid = $ctx->{mid};
303 my $sres = $ctx->{srch}->get_thread($mid, { asc => 1 });
304 my $msgs = load_results($sres);
305 my $nr = $sres->{total};
306 return missing_thread($ctx) if $nr == 0;
307 my $skel = '<hr /><pre>';
308 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
309 $skel .= ", back to <a\nhref=\"../../\">index</a>";
310 $skel .= "\n<a\nid=t>$nr+ messages in thread:</a> (download: ";
311 $skel .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
312 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
313 $ctx->{-upfx} = '../../';
314 $ctx->{cur_level} = 0;
315 $ctx->{dst} = \$skel;
316 $ctx->{prev_attr} = '';
317 $ctx->{prev_level} = 0;
318 $ctx->{root_anchor} = anchor_for($mid);
320 $ctx->{mapping} = {};
322 my $th = thread_results($msgs);
323 walk_thread($th, $ctx, *pre_thread);
325 return stream_thread($th, $ctx) unless $ctx->{flat};
327 # flat display: lazy load the full message from mini_mime:
328 my $inbox = $ctx->{-inbox};
330 while ($mime = shift @$msgs) {
331 $mime = $inbox->msg_by_mid(mid_clean(mid_mime($mime))) and last;
333 return missing_thread($ctx) unless $mime;
334 $mime = Email::MIME->new($mime);
335 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
336 $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
338 my $body = PublicInbox::WwwStream->new($ctx, sub {
340 while ($mime = shift @$msgs) {
341 $mid = mid_clean(mid_mime($mime));
342 $mime = $inbox->msg_by_mid($mid) and last;
345 $mime = Email::MIME->new($mime);
346 return index_entry($mime, $ctx, scalar @$msgs);
351 [ 200, ['Content-Type', 'text/html; charset=UTF-8'], $body ];
354 sub multipart_text_as_html {
355 my ($mime, $upfx) = @_;
358 # scan through all parts, looking for displayable text
359 msg_iter($mime, sub {
361 $rv .= add_text_body($upfx, $p);
367 my ($s, $l, $quot) = @_;
369 # show everything in the full version with anchor from
370 # short version (see above)
371 my $rv = $l->linkify_1(join('', @$quot));
374 # we use a <span> here to allow users to specify their own
375 # color for quoted text
376 $rv = $l->linkify_2(ascii_html($rv));
377 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
380 sub attach_link ($$$$) {
381 my ($upfx, $ct, $p, $fn) = @_;
382 my ($part, $depth, @idx) = @$p;
383 my $nl = $idx[-1] > 1 ? "\n" : '';
384 my $idx = join('.', @idx);
385 my $size = bytes::length($part->body);
386 $ct ||= 'text/plain';
387 $ct =~ s/;.*//; # no attributes
388 $ct = ascii_html($ct);
389 my $desc = $part->header('Content-Description');
390 $desc = $fn unless defined $desc;
391 $desc = '' unless defined $desc;
393 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
395 } elsif ($ct eq 'text/plain') {
400 my @ret = qq($nl<a\nhref="$upfx$idx-$sfn">[-- Attachment #$idx: );
401 my $ts = "Type: $ct, Size: $size bytes";
402 push(@ret, ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]");
403 join('', @ret, "</a>\n");
407 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
408 my ($part, $depth, @idx) = @$p;
409 my $ct = $part->content_type;
410 my $fn = $part->filename;
412 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
413 return attach_link($upfx, $ct, $p, $fn);
416 my $s = eval { $part->body_str };
418 # badly-encoded message? tell the world about it!
419 return attach_link($upfx, $ct, $p, $fn) if $@;
421 my @lines = split(/^/m, $s);
423 if (defined($fn) || $depth > 0) {
424 $s .= attach_link($upfx, $ct, $p, $fn);
428 my $l = PublicInbox::Linkify->new;
429 while (defined(my $cur = shift @lines)) {
431 # show the previously buffered quote inline
432 flush_quote(\$s, $l, \@quot) if @quot;
435 $cur = $l->linkify_1($cur);
436 $cur = ascii_html($cur);
437 $s .= $l->linkify_2($cur);
446 flush_quote(\$s, $l, \@quot);
448 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
449 $s =~ s/\A\n+//s; # kill leading blank lines
450 $s =~ s/\s+\z//s; # kill all trailing spaces
454 sub _msg_html_prepare {
455 my ($hdr, $ctx) = @_;
456 my $srch = $ctx->{srch} if $ctx;
458 my $rv = "<pre\nid=b>"; # anchor for body start
461 $ctx->{-upfx} = '../';
464 my $mid = $hdr->header_raw('Message-ID');
465 $mid = PublicInbox::Hval->new_msgid($mid);
466 foreach my $h (qw(From To Cc Subject Date)) {
467 my $v = $hdr->header($h);
468 defined($v) && ($v ne '') or next;
469 $v = PublicInbox::Hval->new($v);
472 my @n = PublicInbox::Address::names($v->raw);
473 $title[1] = ascii_html(join(', ', @n));
474 } elsif ($h eq 'Subject') {
475 $title[0] = $v->as_html;
477 $rv .= qq($h: <a\nhref="#r"\nid=t>);
478 $rv .= $v->as_html . "</a>\n";
483 $v =~ s/(\@[^,]+,) /$1\n\t/g if ($h eq 'Cc' || $h eq 'To');
487 $title[0] ||= '(no subject)';
488 $ctx->{-title_html} = join(' - ', @title);
489 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
490 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
491 $rv .= _parent_headers($hdr, $srch);
496 my ($dst, $ctx, $hdr, $tpfx) = @_;
497 my $srch = $ctx->{srch};
498 my $mid = mid_clean($hdr->header_raw('Message-ID'));
499 my $sres = $srch->get_thread($mid);
500 my $nr = $sres->{total};
501 my $expand = qq(<a\nhref="${tpfx}t/#u">expand</a> ) .
502 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
503 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
505 my $parent = in_reply_to($hdr);
507 if (defined $parent) {
508 $$dst .= "($expand)\n ";
509 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
511 $$dst .= "[no followups, yet] ($expand)\n";
513 $ctx->{next_msg} = undef;
514 $ctx->{parent_msg} = $parent;
518 $$dst .= "$nr+ messages in thread ($expand";
519 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
521 my $subj = $srch->subject_path($hdr->header('Subject'));
522 $ctx->{seen} = { $subj => 1 };
524 $ctx->{prev_attr} = '';
525 $ctx->{prev_level} = 0;
527 walk_thread(thread_results(load_results($sres)), $ctx, *skel_dump);
528 $ctx->{parent_msg} = $parent;
531 sub _parent_headers {
532 my ($hdr, $srch) = @_;
535 my $irt = in_reply_to($hdr);
537 my $v = PublicInbox::Hval->new_msgid($irt, 1);
538 my $html = $v->as_html;
539 my $href = $v->as_href;
540 $rv .= "In-Reply-To: <";
541 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
544 # do not display References: if search is present,
545 # we show the thread skeleton at the bottom, instead.
548 my $refs = $hdr->header_raw('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("\n\t", @refs) . "\n";
568 sub squote_maybe ($) {
570 if ($val =~ m{([^\w@\./,\%\+\-])}) {
571 $val =~ s/(['!])/'\\$1'/g; # '!' for csh
577 sub mailto_arg_link {
579 my %cc; # everyone else
580 my $to; # this is the From address
582 foreach my $h (qw(From To Cc)) {
583 my $v = $hdr->header($h);
584 defined($v) && ($v ne '') or next;
585 my @addrs = PublicInbox::Address::emails($v);
586 foreach my $address (@addrs) {
587 my $dst = lc($address);
588 $cc{$dst} ||= $address;
594 my $subj = $hdr->header('Subject') || '';
595 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
596 my $mid = $hdr->header_raw('Message-ID');
597 push @arg, '--in-reply-to='.ascii_html(squote_maybe(mid_clean($mid)));
598 my $irt = uri_escape_utf8($mid);
600 push @arg, '--to=' . ascii_html($to);
601 $to = uri_escape_utf8($to);
602 $subj = uri_escape_utf8($subj);
603 my $cc = join(',', sort values %cc);
604 push @arg, '--cc=' . ascii_html($cc);
605 $cc = uri_escape_utf8($cc);
606 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
609 (\@arg, ascii_html($href));
613 my ($hdr, $standalone, $ctx, $rhref) = @_;
615 my $srch = $ctx->{srch} if $ctx;
618 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
622 thread_skel(\$idx, $ctx, $hdr, $tpfx);
623 my $p = $ctx->{parent_msg};
624 my $next = $ctx->{next_msg};
626 $p = PublicInbox::Hval->new_msgid($p);
628 $irt = "<a\nhref=\"$upfx$p/\"\nrel=prev>parent</a> ";
630 $irt = ' ' x length('parent ');
633 my $n = PublicInbox::Hval->new_msgid($next)->as_href;
634 $irt .= "<a\nhref=\"$upfx$n/\"\nrel=next>next</a> ";
636 $irt .= ' ' x length('next ');
642 $irt .= qq(<a\nhref="$rhref">reply</a>);
646 sub linkify_ref_nosrch {
647 my $v = PublicInbox::Hval->new_msgid($_[0], 1);
648 my $html = $v->as_html;
649 my $href = $v->as_href;
650 "<<a\nhref=\"../$href/\">$html</a>>";
655 'm' . id_compress($msgid, 1);
659 my ($upfx, $mid) = @_;
660 # 'subject dummy' is used internally by Mail::Thread
661 return '[no common parent]' if ($mid eq 'subject dummy');
663 $mid = PublicInbox::Hval->new_msgid($mid);
664 my $href = $mid->as_href;
665 my $html = $mid->as_html;
666 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
671 INDENT x ($level - 1);
677 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
682 my $ts = eval { str2time($hdr->header('Date')) };
683 defined($ts) ? $ts : 0;
688 require PublicInbox::Thread;
689 my $th = PublicInbox::Thread->new(@$msgs);
691 $th->order(*sort_ts);
697 require PublicInbox::ExtMsg;
698 PublicInbox::ExtMsg::ext_msg($ctx);
703 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
707 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
710 my ($ctx, $hdr, $level) = @_;
712 my $dst = $ctx->{dst};
713 my $cur = $ctx->{cur};
714 my $mid = mid_clean($hdr->header_raw('Message-ID'));
715 my $f = ascii_html($hdr->header('X-PI-From'));
716 my $d = _msg_date($hdr) . ' ' . indent_for($level) . th_pfx($level);
718 $ctx->{first_level} ||= $level;
720 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
721 $ctx->{prev_attr} = $attr;
725 $ctx->{prev_level} = $level;
731 $$dst .= "<b><a\nid=r\nhref=\"#t\">".
732 "$attr [this message]</a></b>\n";
736 $ctx->{next_msg} ||= $mid;
739 # Subject is never undef, this mail was loaded from
740 # our Xapian which would've resulted in '' if it were
741 # really missing (and Filter rejects empty subjects)
742 my $s = $hdr->header('Subject');
743 my $h = $ctx->{srch}->subject_path($s);
744 if ($ctx->{seen}->{$h}) {
747 $ctx->{seen}->{$h} = 1;
748 $s = PublicInbox::Hval->new($s);
751 my $m = PublicInbox::Hval->new_msgid($mid);
753 my $mapping = $ctx->{mapping};
754 my $end = defined($s) ? "$s</a> $f\n" : "$f</a>\n";
756 my $map = $mapping->{$mid};
757 $id = id_compress($mid, 1);
759 $map->[1] = "$d<a\nhref=\"$m\">$end";
762 $m = $ctx->{-upfx}.$m->as_href.'/';
764 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
768 my ($ctx, $level, $node) = @_;
769 if (my $mime = $node->message) {
770 _skel_header($ctx, $mime->header_obj, $level);
772 my $mid = $node->messageid;
773 my $dst = $ctx->{dst};
774 my $mapping = $ctx->{mapping};
775 my $map = $mapping->{$mid} if $mapping;
776 if ($mid eq 'subject dummy') {
777 my $ncp = "\t[no common parent]\n";
778 $map->[1] = $ncp if $map;
782 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
784 $d .= indent_for($level) . th_pfx($level);
785 my $upfx = $ctx->{-upfx};
786 my $m = PublicInbox::Hval->new_msgid($mid);
787 my $href = $upfx . $m->as_href . '/';
788 my $html = $m->as_html;
791 my $id = id_compress($mid, 1);
792 $map->[1] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
793 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
795 $d .= qq{<<a\nhref="$href">$html</a>>\n};
803 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
804 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
808 sub _tryload_ghost ($$) {
809 my ($srch, $mid) = @_;
810 my $smsg = $srch->lookup_mail($mid) or return;
814 # accumulate recent topics if search is supported
815 # returns 1 if done, undef if not
817 my ($ctx, $level, $node) = @_;
818 my $srch = $ctx->{srch};
819 my $mid = $node->messageid;
820 my $x = $node->message || _tryload_ghost($srch, $mid);
824 $subj = $x->header('Subject');
825 $subj = $srch->subject_normalized($subj);
826 $ts = $x->header('X-PI-TS');
827 } else { # ghost message, do not bump level
831 if (++$ctx->{subjs}->{$subj} == 1) {
832 push @{$ctx->{order}}, [ $level, $subj ];
834 my $exist = $ctx->{latest}->{$subj};
835 if (!$exist || $exist->[1] < $ts) {
836 $ctx->{latest}->{$subj} = [ $mid, $ts ];
842 my $order = $ctx->{order};
843 my $subjs = $ctx->{subjs};
844 my $latest = $ctx->{latest};
846 return $fh->write("\n[No topics in range]</pre>") unless scalar @$order;
852 while (defined(my $info = shift @$order)) {
853 my ($level, $subj) = @$info;
854 my $n = delete $subjs->{$subj};
855 my ($mid, $ts) = @{delete $latest->{$subj}};
856 $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
857 $pfx = indent_for($level);
858 my $nl = $level == $prev ? "\n" : '';
863 $cur ||= [ $ts, '' ];
864 $cur->[0] = $ts if $ts > $cur->[0];
865 $cur->[1] .= $nl . $pfx . th_pfx($level);
866 if ($ts == -666) { # ghost
867 $cur->[1] .= ghost_parent('', $mid) . "\n";
868 next; # child will have mbox / atom link
871 $subj = PublicInbox::Hval->new($subj)->as_html;
872 $cur->[1] .= "<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
874 my $attr = " $ts UTC";
876 # $n isn't the total number of posts on the topic,
877 # just the number of posts in the current results window
878 $n = $n == 1 ? '' : " ($n+ messages)";
880 if ($level == 0 || $attr ne $prev_attr) {
881 my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
882 my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
883 $pfx .= INDENT if $level > 0;
884 $cur->[1] .= $pfx . $attr . $n . " - $mbox / $atom\n";
888 push @recent, $cur if $cur;
889 @recent = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @recent;
890 $fh->write(join('', @recent) . '</pre>');
893 sub emit_index_topics {
895 my ($off) = (($ctx->{cgi}->param('o') || '0') =~ /(\d+)/);
900 my %opts = ( offset => $off, limit => $max * 4 );
901 while (scalar @{$ctx->{order}} < $max) {
902 my $sres = $ctx->{srch}->query('', \%opts);
903 my $nr = scalar @{$sres->{msgs}} or last;
904 $sres = load_results($sres);
905 walk_thread(thread_results($sres), $ctx, *add_topic);
906 $opts{offset} += $nr;
913 sub thread_adj_level {
914 my ($ctx, $level) = @_;
916 my $max = $ctx->{cur_level};
918 return ('', '') if $max == 0; # flat output
920 # reset existing lists
921 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
922 $ctx->{cur_level} = 0;
924 } elsif ($level == $max) { # continue existing list
926 } elsif ($level < $max) {
927 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
928 $ctx->{cur_level} = $level;
929 ("$beg<li>", '</li>');
930 } else { # ($level > $max) # start a new level
931 $ctx->{cur_level} = $level;
932 my $beg = ($max ? '<li>' : '') . '<ul><li>';
937 sub ghost_index_entry {
938 my ($ctx, $level, $mid) = @_;
939 my ($beg, $end) = thread_adj_level($ctx, $level);
940 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $mid) . '</pre>' . $end;