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 PublicInbox::Hval qw/ascii_html/;
12 use PublicInbox::Linkify;
13 use PublicInbox::MID qw/mid_clean id_compress mid_mime mid_escape/;
14 use PublicInbox::MsgIter;
15 use PublicInbox::Address;
16 use PublicInbox::WwwStream;
19 use constant INDENT => ' ';
20 use constant TCHILD => '` ';
21 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
23 # public functions: (unstable)
25 my ($ctx, $mime) = @_;
26 my $hdr = $mime->header_obj;
27 my $tip = _msg_html_prepare($hdr, $ctx);
28 PublicInbox::WwwStream->response($ctx, 200, sub {
31 $tip . multipart_text_as_html($mime, '') . '</pre><hr>'
33 # fake an EOF if generating the footer fails;
34 # we want to at least show the message if something
37 '<pre>' . html_footer($hdr, 1, $ctx) .
38 '</pre>' . msg_reply($ctx, $hdr)
46 # /$INBOX/$MESSAGE_ID/#R
50 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
52 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
55 if (my $url = $ctx->{-inbox}->{infourl}) {
56 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
57 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
60 my ($arg, $link) = mailto_arg_link($hdr);
61 push @$arg, '/path/to/YOUR_REPLY';
62 $arg = ascii_html(join(" \\\n ", '', @$arg));
65 id=R><b>Reply instructions:</b>
67 You may reply publically to <a
68 href=#t>this message</a> via plain-text email
69 using any one of the following methods:
71 * Save the following mbox file, import it into your mail client,
72 and reply-to-all from there: <a
75 Avoid top-posting and favor interleaved quoting:
77 href="$p_url">$p_url</a>
79 * Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,
80 and <b>--in-reply-to</b> switches of git-send-email(1):
85 href="$se_url">$se_url</a>
87 * If your mail client supports setting the <b>In-Reply-To</b> header
88 via mailto: links, try the <a
89 href="$link">mailto: link</a></pre>
95 my $irt = $hdr->header_raw('In-Reply-To');
97 return mid_clean($irt) if defined $irt && $irt ne '';
99 my $refs = $hdr->header_raw('References');
100 if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
106 sub _hdr_names ($$) {
107 my ($hdr, $field) = @_;
108 my $val = $hdr->header($field) or return '';
109 ascii_html(join(', ', PublicInbox::Address::names($val)));
113 my ($nr, $singular, $plural) = @_;
114 return "0 $plural" if $nr == 0;
115 $nr == 1 ? "$nr $singular" : "$nr $plural";
118 # this is already inside a <pre>
120 my ($mime, $ctx, $more) = @_;
121 my $srch = $ctx->{srch};
122 my $hdr = $mime->header_obj;
123 my $subj = $hdr->header('Subject');
125 my $mid_raw = mid_clean(mid_mime($mime));
126 my $id = id_compress($mid_raw, 1);
129 my $root_anchor = $ctx->{root_anchor} || '';
130 my $irt = in_reply_to($hdr);
132 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
133 $subj = '<b>'.ascii_html($subj).'</b>';
134 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
136 $rv .= _th_index_lite($mid_raw, $irt, $id, $ctx);
138 foreach my $f (qw(To Cc)) {
139 my $dst = _hdr_names($hdr, $f);
140 push @tocc, "$f: $dst" if $dst ne '';
142 $rv .= "From: "._hdr_names($hdr, 'From').' @ '._msg_date($hdr)." UTC";
143 my $upfx = $ctx->{-upfx};
144 my $mhref = $upfx . mid_escape($mid_raw) . '/';
145 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
146 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
147 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
149 my $mapping = $ctx->{mapping};
150 if (!$mapping && $irt) {
151 my $mirt = PublicInbox::Hval->new_msgid($irt);
152 my $href = $upfx . $mirt->{href}. '/';
153 my $html = $mirt->as_html;
154 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
158 # scan through all parts, looking for displayable text
159 msg_iter($mime, sub { $rv .= add_text_body($mhref, $_[0]) });
162 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
163 "<a\nhref=\"$mhref\">permalink</a>" .
164 " <a\nhref=\"${mhref}raw\">raw</a>" .
165 " <a\nhref=\"${mhref}#R\">reply</a>";
168 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
169 $rv .= "\t[relevance $pct->{$mid_raw}%]";
172 my $threaded = 'threaded';
177 $flat = "<b>$flat</b>";
179 $threaded = "<b>$threaded</b>";
181 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
182 $rv .= "|<a\nhref=\"${mhref}t/#u\">$threaded</a>]";
183 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
188 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
192 sub pad_link ($$;$) {
193 my ($mid, $level, $s) = @_;
195 my $id = id_compress($mid, 1);
196 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
200 my ($mid_raw, $irt, $id, $ctx) = @_;
202 my $mapping = $ctx->{mapping} or return $rv;
204 my ($attr, $node, $idx, $level) = @{$mapping->{$mid_raw}};
205 my $children = $node->{children};
206 my $nr_c = scalar @$children;
209 my $irt_map = $mapping->{$irt} if defined $irt;
210 if (defined $irt_map) {
211 $siblings = $irt_map->[1]->{children};
212 $nr_s = scalar(@$siblings) - 1;
213 $rv .= $pad . $irt_map->[0];
215 my $prev = $siblings->[$idx - 1];
216 my $pmid = $prev->{id};
218 my $s = ($idx - 1). ' preceding siblings ...';
219 $rv .= pad_link($pmid, $level, $s);
220 } elsif ($idx == 2) {
221 my $ppmid = $siblings->[0]->{id};
222 $rv .= $pad . $mapping->{$ppmid}->[0];
224 $rv .= $pad . $mapping->{$pmid}->[0];
227 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
228 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
229 $attr =~ s!\n\z!</b>\n!s;
230 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
231 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
234 my $cmid = $children->[0]->{id};
235 $rv .= $pad . $mapping->{$cmid}->[0];
237 my $s = ($nr_c - 1). ' more replies';
238 $rv .= pad_link($cmid, $level + 1, $s);
239 } elsif (my $cn = $children->[1]) {
240 $rv .= $pad . $mapping->{$cn->{id}}->[0];
244 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
246 my $nmid = $next->{id};
247 $rv .= $pad . $mapping->{$nmid}->[0];
248 my $nnext = $nr_s - $idx;
250 my $s = ($nnext - 1).' subsequent siblings';
251 $rv .= pad_link($nmid, $level, $s);
252 } elsif (my $nn = $siblings->[$idx + 2]) {
253 $rv .= $pad . $mapping->{$nn->{id}}->[0];
256 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
260 my ($th, $ctx, $cb) = @_;
261 my @q = map { (0, $_, -1) } @{$th->{rootset}};
263 my ($level, $node, $i) = splice(@q, 0, 3);
264 defined $node or next;
265 $cb->($ctx, $level, $node, $i);
268 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
273 my ($ctx, $level, $node, $idx) = @_;
274 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
275 skel_dump($ctx, $level, $node);
278 sub thread_index_entry {
279 my ($ctx, $level, $mime) = @_;
280 my ($beg, $end) = thread_adj_level($ctx, $level);
281 $beg . '<pre>' . index_entry($mime, $ctx, 0) . '</pre>' . $end;
284 sub stream_thread ($$) {
286 my $inbox = $ctx->{-inbox};
288 my @q = map { (0, $_) } @{$th->{rootset}};
292 my $node = shift @q or next;
294 unshift @q, map { ($cl, $_) } @{$node->{children}};
295 $mime = $inbox->msg_by_smsg($node->{smsg}) and last;
297 return missing_thread($ctx) unless $mime;
299 $mime = Email::MIME->new($mime);
300 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
301 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
302 PublicInbox::WwwStream->response($ctx, 200, sub {
306 my $node = shift @q or next;
308 unshift @q, map { ($cl, $_) } @{$node->{children}};
309 my $mid = $node->{id};
310 if ($mime = $inbox->msg_by_smsg($node->{smsg})) {
311 $mime = Email::MIME->new($mime);
312 return thread_index_entry($ctx, $level, $mime);
314 return ghost_index_entry($ctx, $level, $node);
317 my $ret = join('', thread_adj_level($ctx, 0));
318 $ret .= ${$ctx->{dst}}; # skel
326 my $mid = $ctx->{mid};
327 my $sres = $ctx->{srch}->get_thread($mid, { asc => 1 });
328 my $msgs = load_results($sres);
329 my $nr = $sres->{total};
330 return missing_thread($ctx) if $nr == 0;
331 my $skel = '<hr><pre>';
332 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
333 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
334 $skel .= "<b\nid=t>Thread overview:</b> ";
335 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
336 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
337 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
338 $skel .= "-- links below jump to the message on this page --\n";
339 $ctx->{-upfx} = '../../';
340 $ctx->{cur_level} = 0;
341 $ctx->{dst} = \$skel;
342 $ctx->{prev_attr} = '';
343 $ctx->{prev_level} = 0;
344 $ctx->{root_anchor} = anchor_for($mid);
346 $ctx->{mapping} = {};
347 $ctx->{s_nr} = "$nr+ messages in thread";
349 my $th = thread_results($msgs);
350 walk_thread($th, $ctx, *pre_thread);
352 return stream_thread($th, $ctx) unless $ctx->{flat};
354 # flat display: lazy load the full message from smsg
355 my $inbox = $ctx->{-inbox};
357 while ($mime = shift @$msgs) {
358 $mime = $inbox->msg_by_smsg($mime) and last;
360 return missing_thread($ctx) unless $mime;
361 $mime = Email::MIME->new($mime);
362 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
363 $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
365 PublicInbox::WwwStream->response($ctx, 200, sub {
367 while ($mime = shift @$msgs) {
368 $mime = $inbox->msg_by_smsg($mime) and last;
371 $mime = Email::MIME->new($mime);
372 return index_entry($mime, $ctx, scalar @$msgs);
379 sub multipart_text_as_html {
380 my ($mime, $upfx) = @_;
383 # scan through all parts, looking for displayable text
384 msg_iter($mime, sub {
386 $rv .= add_text_body($upfx, $p);
392 my ($s, $l, $quot) = @_;
394 # show everything in the full version with anchor from
395 # short version (see above)
396 my $rv = $l->linkify_1(join('', @$quot));
399 # we use a <span> here to allow users to specify their own
400 # color for quoted text
401 $rv = $l->linkify_2(ascii_html($rv));
402 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
405 sub attach_link ($$$$;$) {
406 my ($upfx, $ct, $p, $fn, $err) = @_;
407 my ($part, $depth, @idx) = @$p;
408 my $nl = $idx[-1] > 1 ? "\n" : '';
409 my $idx = join('.', @idx);
410 my $size = bytes::length($part->body);
412 # hide attributes normally, unless we want to aid users in
413 # spotting MUA problems:
414 $ct =~ s/;.*// unless $err;
415 $ct = ascii_html($ct);
416 my $desc = $part->header('Content-Description');
417 $desc = $fn unless defined $desc;
418 $desc = '' unless defined $desc;
420 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
422 } elsif ($ct eq 'text/plain') {
427 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
430 "[-- Warning: decoded text below may be mangled --]\n";
432 $ret .= "[-- Attachment #$idx: ";
433 my $ts = "Type: $ct, Size: $size bytes";
434 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
439 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
440 my ($part, $depth, @idx) = @$p;
441 my $ct = $part->content_type || 'text/plain';
442 my $fn = $part->filename;
444 if ($ct =~ m!\btext/x?html\b!i) {
445 return attach_link($upfx, $ct, $p, $fn);
448 my $s = eval { $part->body_str };
450 # badly-encoded message? tell the world about it!
453 if ($ct =~ m!\btext/plain\b!i) {
454 # Try to assume UTF-8 because Alpine seems to
455 # do wacky things and set charset=X-UNKNOWN
456 $part->charset_set('UTF-8');
457 $s = eval { $part->body_str };
459 # If forcing charset=UTF-8 failed,
460 # attach_link will warn further down...
461 $s = $part->body if $@;
463 return attach_link($upfx, $ct, $p, $fn);
467 my @lines = split(/^/m, $s);
469 if (defined($fn) || $depth > 0 || $err) {
470 $s .= attach_link($upfx, $ct, $p, $fn, $err);
474 my $l = PublicInbox::Linkify->new;
475 while (defined(my $cur = shift @lines)) {
477 # show the previously buffered quote inline
478 flush_quote(\$s, $l, \@quot) if @quot;
481 $cur = $l->linkify_1($cur);
482 $cur = ascii_html($cur);
483 $s .= $l->linkify_2($cur);
492 flush_quote(\$s, $l, \@quot);
494 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
495 $s =~ s/\A\n+//s; # kill leading blank lines
496 $s =~ s/\s+\z//s; # kill all trailing spaces
500 sub _msg_html_prepare {
501 my ($hdr, $ctx) = @_;
502 my $srch = $ctx->{srch} if $ctx;
504 my $rv = "<pre\nid=b>"; # anchor for body start
507 $ctx->{-upfx} = '../';
510 my $mid = mid_clean($hdr->header_raw('Message-ID'));
511 $mid = PublicInbox::Hval->new_msgid($mid);
512 foreach my $h (qw(From To Cc Subject Date)) {
513 my $v = $hdr->header($h);
514 defined($v) && ($v ne '') or next;
515 $v = PublicInbox::Hval->new($v);
518 my @n = PublicInbox::Address::names($v->raw);
519 $title[1] = ascii_html(join(', ', @n));
520 } elsif ($h eq 'Subject') {
521 $title[0] = $v->as_html;
523 $rv .= qq($h: <a\nhref="#r"\nid=t>);
524 $rv .= $v->as_html . "</a>\n";
529 $v =~ s/(\@[^,]+,) /$1\n\t/g if ($h eq 'Cc' || $h eq 'To');
533 $title[0] ||= '(no subject)';
534 $ctx->{-title_html} = join(' - ', @title);
535 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
536 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
537 $rv .= _parent_headers($hdr, $srch);
542 my ($dst, $ctx, $hdr, $tpfx) = @_;
543 my $srch = $ctx->{srch};
544 my $mid = mid_clean($hdr->header_raw('Message-ID'));
545 my $sres = $srch->get_thread($mid);
546 my $nr = $sres->{total};
547 my $expand = qq(<a\nhref="${tpfx}T/#u">expand</a> ) .
548 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
549 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
551 my $parent = in_reply_to($hdr);
552 $$dst .= "\n<b>Thread overview: </b>";
554 if (defined $parent) {
555 $$dst .= "($expand)\n ";
556 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
558 $$dst .= "[no followups, yet] ($expand)\n";
560 $ctx->{next_msg} = undef;
561 $ctx->{parent_msg} = $parent;
565 $$dst .= "$nr+ messages in thread ($expand";
566 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
568 my $subj = $srch->subject_path($hdr->header('Subject'));
569 $ctx->{seen} = { $subj => 1 };
571 $ctx->{prev_attr} = '';
572 $ctx->{prev_level} = 0;
574 walk_thread(thread_results(load_results($sres)), $ctx, *skel_dump);
575 $ctx->{parent_msg} = $parent;
578 sub _parent_headers {
579 my ($hdr, $srch) = @_;
582 my $irt = in_reply_to($hdr);
584 my $v = PublicInbox::Hval->new_msgid($irt);
585 my $html = $v->as_html;
586 my $href = $v->{href};
587 $rv .= "In-Reply-To: <";
588 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
591 # do not display References: if search is present,
592 # we show the thread skeleton at the bottom, instead.
595 my $refs = $hdr->header_raw('References');
597 # avoid redundant URLs wasting bandwidth
599 $seen{$irt} = 1 if defined $irt;
601 my @raw_refs = ($refs =~ /<([^>]+)>/g);
602 foreach my $ref (@raw_refs) {
605 push @refs, linkify_ref_nosrch($ref);
609 $rv .= 'References: '. join("\n\t", @refs) . "\n";
615 sub squote_maybe ($) {
617 if ($val =~ m{([^\w@\./,\%\+\-])}) {
618 $val =~ s/(['!])/'\\$1'/g; # '!' for csh
624 sub mailto_arg_link {
626 my %cc; # everyone else
627 my $to; # this is the From address
629 foreach my $h (qw(From To Cc)) {
630 my $v = $hdr->header($h);
631 defined($v) && ($v ne '') or next;
632 my @addrs = PublicInbox::Address::emails($v);
633 foreach my $address (@addrs) {
634 my $dst = lc($address);
635 $cc{$dst} ||= $address;
641 my $subj = $hdr->header('Subject') || '';
642 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
643 my $mid = $hdr->header_raw('Message-ID');
644 push @arg, '--in-reply-to='.squote_maybe(mid_clean($mid));
645 my $irt = mid_escape($mid);
647 push @arg, "--to=$to";
648 $to = uri_escape_utf8($to);
649 $subj = uri_escape_utf8($subj);
650 my @cc = sort values %cc;
651 push(@arg, map { "--cc=$_" } @cc);
652 my $cc = uri_escape_utf8(join(',', @cc));
653 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
655 (\@arg, ascii_html($href));
659 my ($hdr, $standalone, $ctx, $rhref) = @_;
661 my $srch = $ctx->{srch} if $ctx;
664 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
668 thread_skel(\$idx, $ctx, $hdr, $tpfx);
673 if (my $n = $ctx->{next_msg}) {
674 $n = PublicInbox::Hval->new_msgid($n)->{href};
675 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
678 my $par = $ctx->{parent_msg};
680 $u = PublicInbox::Hval->new_msgid($par)->{href};
683 if (my $p = $ctx->{prev_msg}) {
684 $prev = PublicInbox::Hval->new_msgid($p)->{href};
685 if ($p && $par && $p eq $par) {
686 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
687 'rel=prev>prev parent</a>';
690 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
692 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
694 } elsif ($u) { # unlikely
695 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
697 $irt = "$next $prev$parent ";
702 $irt .= qq(<a\nhref="$rhref">reply</a>);
706 sub linkify_ref_nosrch {
707 my $v = PublicInbox::Hval->new_msgid($_[0]);
708 my $html = $v->as_html;
709 my $href = $v->{href};
710 "<<a\nhref=\"../$href/\">$html</a>>";
715 'm' . id_compress($msgid, 1);
719 my ($upfx, $mid) = @_;
721 $mid = PublicInbox::Hval->new_msgid($mid);
722 my $href = $mid->{href};
723 my $html = $mid->as_html;
724 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
729 INDENT x ($level - 1);
735 [ map { $_->ensure_metadata; $_ } @{delete $sres->{msgs}} ];
740 my $ts = eval { str2time($hdr->header('Date')) };
741 defined($ts) ? $ts : 0;
746 require PublicInbox::SearchThread;
747 my $th = PublicInbox::SearchThread->new($msgs);
749 $th->order(*sort_ts);
755 require PublicInbox::ExtMsg;
756 PublicInbox::ExtMsg::ext_msg($ctx);
761 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
765 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
768 my ($ctx, $smsg, $level) = @_;
770 my $dst = $ctx->{dst};
771 my $cur = $ctx->{cur};
772 my $mid = $smsg->{mid};
773 my $f = ascii_html($smsg->from_name);
774 my $d = fmt_ts($smsg->{ts}) . ' ' . indent_for($level) . th_pfx($level);
776 $ctx->{first_level} ||= $level;
778 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
779 $ctx->{prev_attr} = $attr;
781 $ctx->{prev_level} = $level;
786 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
787 "$attr [this message]</a></b>\n";
790 $ctx->{prev_msg} = $mid;
793 $ctx->{next_msg} ||= $mid;
796 # Subject is never undef, this mail was loaded from
797 # our Xapian which would've resulted in '' if it were
798 # really missing (and Filter rejects empty subjects)
799 my $s = $smsg->subject;
800 my $h = $ctx->{srch}->subject_path($s);
801 if ($ctx->{seen}->{$h}) {
804 $ctx->{seen}->{$h} = 1;
805 $s = PublicInbox::Hval->new($s);
810 my $mapping = $ctx->{mapping};
811 my $end = defined($s) ? "$s</a> $f\n" : "$f</a>\n";
813 my $map = $mapping->{$mid};
814 $id = id_compress($mid, 1);
816 $map->[0] = "$d<a\nhref=\"$m\">$end";
819 $m = $ctx->{-upfx}.mid_escape($mid).'/';
821 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
825 my ($ctx, $level, $node) = @_;
826 if (my $smsg = $node->{smsg}) {
827 _skel_header($ctx, $smsg, $level);
829 my $mid = $node->{id};
830 my $dst = $ctx->{dst};
831 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
833 $d .= indent_for($level) . th_pfx($level);
834 my $upfx = $ctx->{-upfx};
835 my $m = PublicInbox::Hval->new_msgid($mid);
836 my $href = $upfx . $m->{href} . '/';
837 my $html = $m->as_html;
839 my $mapping = $ctx->{mapping};
840 my $map = $mapping->{$mid} if $mapping;
842 my $id = id_compress($mid, 1);
843 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
844 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
846 $d .= qq{<<a\nhref="$href">$html</a>>\n};
854 (eval { $a->topmost->{smsg}->ts } || 0) <=>
855 (eval { $b->topmost->{smsg}->ts } || 0)
859 # accumulate recent topics if search is supported
860 # returns 200 if done, 404 if not
862 my ($ctx, $level, $node) = @_;
863 my $srch = $ctx->{srch};
864 my $mid = $node->{id};
865 my $x = $node->{smsg} || $srch->lookup_mail($mid);
870 $subj = $srch->subject_normalized($subj);
873 $topic = [ $ts, 1, { $subj => $mid }, $subj ];
874 $ctx->{-cur_topic} = $topic;
875 push @{$ctx->{order}}, $topic;
879 $topic = $ctx->{-cur_topic}; # should never be undef
880 $topic->[0] = $ts if $ts > $topic->[0];
882 my $seen = $topic->[2];
883 if (scalar(@$topic) == 3) { # parent was a ghost
885 } elsif (!$seen->{$subj}) {
886 push @$topic, $level, $subj;
888 $seen->{$subj} = $mid; # latest for subject
889 } else { # ghost message
890 return if $level != 0; # ignore child ghosts
891 $topic = [ -666, 0, {} ];
892 $ctx->{-cur_topic} = $topic;
893 push @{$ctx->{order}}, $topic;
899 my $order = delete $ctx->{order}; # [ ts, subj1, subj2, subj3, ... ]
901 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
907 # sort by recency, this allows new posts to "bump" old topics...
908 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
909 my ($ts, $n, $seen, $top, @ex) = @$topic;
911 next unless defined $top; # ghost topic
912 my $mid = delete $seen->{$top};
913 my $href = mid_escape($mid);
914 $top = PublicInbox::Hval->new($top)->as_html;
917 # $n isn't the total number of posts on the topic,
918 # just the number of posts in the current results window
922 $anchor = '#u'; # top of only message
924 $n = " ($n+ messages)";
925 $anchor = '#t'; # thread skeleton
928 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
929 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
930 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
931 " $ts UTC $n - $mbox / $atom\n";
932 for (my $i = 0; $i < scalar(@ex); $i += 2) {
934 my $sub = $ex[$i + 1];
935 $mid = delete $seen->{$sub};
936 $sub = PublicInbox::Hval->new($sub)->as_html;
937 $href = mid_escape($mid);
938 $s .= indent_for($level) . TCHILD;
939 $s .= "<a\nhref=\"$href/T/#u\">$sub</a>\n";
943 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
947 sub index_nav { # callback for WwwStream
948 my (undef, $ctx) = @_;
949 delete $ctx->{qp} or return;
954 my $next_o = $ctx->{-next_o};
956 $next = qq!<a\nhref="?o=$next_o"\nrel=next>next</a>!;
958 if (my $cur_o = $ctx->{-cur_o}) {
959 $latest = qq! <a\nhref=.>latest</a>!;
961 my $o = $cur_o - ($next_o - $cur_o);
963 $prev = qq!<a\nhref="?o=$o"\nrel=prev>prev</a>!;
965 $prev = qq!<a\nhref=.\nrel=prev>prev</a>!;
968 "<hr><pre>page: $next $prev$latest</pre>";
973 my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/);
974 my $opts = { offset => $off, limit => 200 };
977 my $sres = $ctx->{srch}->query('', $opts);
978 my $nr = scalar @{$sres->{msgs}};
980 $sres = load_results($sres);
981 walk_thread(thread_results($sres), $ctx, *acc_topic);
983 $ctx->{-next_o} = $off+ $nr;
984 $ctx->{-cur_o} = $off;
985 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
988 sub thread_adj_level {
989 my ($ctx, $level) = @_;
991 my $max = $ctx->{cur_level};
993 return ('', '') if $max == 0; # flat output
995 # reset existing lists
996 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
997 $ctx->{cur_level} = 0;
999 } elsif ($level == $max) { # continue existing list
1001 } elsif ($level < $max) {
1002 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1003 $ctx->{cur_level} = $level;
1004 ("$beg<li>", '</li>');
1005 } else { # ($level > $max) # start a new level
1006 $ctx->{cur_level} = $level;
1007 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1012 sub ghost_index_entry {
1013 my ($ctx, $level, $node) = @_;
1014 my ($beg, $end) = thread_adj_level($ctx, $level);
1015 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})