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/;
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);
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);
128 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
130 my $root_anchor = $ctx->{root_anchor} || '';
131 my $irt = in_reply_to($hdr);
133 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
134 $subj = '<b>'.ascii_html($subj).'</b>';
135 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
137 $rv .= _th_index_lite($mid_raw, $irt, $id, $ctx);
139 foreach my $f (qw(To Cc)) {
140 my $dst = _hdr_names($hdr, $f);
141 push @tocc, "$f: $dst" if $dst ne '';
143 $rv .= "From: "._hdr_names($hdr, 'From').' @ '._msg_date($hdr)." UTC";
144 my $upfx = $ctx->{-upfx};
145 my $mhref = $upfx . $mid->as_href . '/';
146 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
147 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
148 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
150 my $mapping = $ctx->{mapping};
151 if (!$mapping && $irt) {
152 my $mirt = PublicInbox::Hval->new_msgid($irt);
153 my $href = $upfx . $mirt->as_href . '/';
154 my $html = $mirt->as_html;
155 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
159 # scan through all parts, looking for displayable text
160 msg_iter($mime, sub { $rv .= add_text_body($mhref, $_[0]) });
163 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
164 "<a\nhref=\"$mhref\">permalink</a>" .
165 " <a\nhref=\"${mhref}raw\">raw</a>" .
166 " <a\nhref=\"${mhref}#R\">reply</a>";
167 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
168 $rv .= "\t[relevance $pct->{$mid_raw}%]";
170 my $threaded = 'threaded';
174 $flat = "<b>$flat</b>";
176 $threaded = "<b>$threaded</b>";
178 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
179 $rv .= "|<a\nhref=\"${mhref}t/#u\">$threaded</a>]";
180 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
183 $rv .= $more ? "\n\n" : "\n";
186 sub pad_link ($$;$) {
187 my ($mid, $level, $s) = @_;
189 my $id = id_compress($mid, 1);
190 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
194 my ($mid_raw, $irt, $id, $ctx) = @_;
196 my $mapping = $ctx->{mapping} or return $rv;
198 # map = [children, attr, node, idx, level]
199 my $map = $mapping->{$mid_raw};
200 my $nr_c = scalar @{$map->[0]};
202 my $level = $map->[4];
205 my $irt_map = $mapping->{$irt};
206 my $siblings = $irt_map->[0];
207 $nr_s = scalar(@$siblings) - 1;
208 $rv .= $pad . $irt_map->[1];
210 my $prev = $siblings->[$idx - 1];
211 my $pmid = $prev->messageid;
213 my $s = ($idx - 1). ' preceding siblings ...';
214 $rv .= pad_link($pmid, $level, $s);
215 } elsif ($idx == 2) {
216 my $ppmid = $siblings->[0]->messageid;
217 $rv .= $pad . $mapping->{$ppmid}->[1];
219 $rv .= $pad . $mapping->{$pmid}->[1];
222 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
223 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
224 my $this = $map->[1];
225 $this =~ s!\n\z!</b>\n!s;
226 $this =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
227 $this =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
229 my $node = $map->[2];
230 if (my $child = $node->child) {
231 my $cmid = $child->messageid;
232 $rv .= $pad . $mapping->{$cmid}->[1];
234 my $s = ($nr_c - 1). ' more replies';
235 $rv .= pad_link($cmid, $level + 1, $s);
236 } elsif (my $cn = $child->next) {
237 $rv .= $pad . $mapping->{$cn->messageid}->[1];
240 if (my $next = $node->next) {
241 my $nmid = $next->messageid;
242 $rv .= $pad . $mapping->{$nmid}->[1];
243 my $nnext = $nr_s - $idx;
245 my $s = ($nnext - 1).' subsequent siblings';
246 $rv .= pad_link($nmid, $level, $s);
247 } elsif (my $nn = $next->next) {
248 $rv .= $pad . $mapping->{$nn->messageid}->[1];
251 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
255 my ($th, $ctx, $cb) = @_;
256 my @q = map { (0, $_) } $th->rootset;
258 my $level = shift @q;
259 my $node = shift @q or next;
260 $cb->($ctx, $level, $node);
261 unshift @q, $level+1, $node->child, $level, $node->next;
266 my ($ctx, $level, $node) = @_;
267 my $mapping = $ctx->{mapping};
269 if (my $parent = $node->parent) {
270 my $m = $mapping->{$parent->messageid}->[0];
274 $mapping->{$node->messageid} = [ [], '', $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;
293 unshift @q, $level+1, $node->child, $level, $node->next;
294 $mime = $inbox->msg_by_mid($node->messageid) and last;
296 return missing_thread($ctx) unless $mime;
298 $mime = Email::MIME->new($mime);
299 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
300 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
301 PublicInbox::WwwStream->response($ctx, 200, sub {
305 my $node = shift @q or next;
306 unshift @q, $level+1, $node->child, $level, $node->next;
307 my $mid = $node->messageid;
308 if ($mime = $inbox->msg_by_mid($mid)) {
309 $mime = Email::MIME->new($mime);
310 return thread_index_entry($ctx, $level, $mime);
312 return ghost_index_entry($ctx, $level, $mid);
315 my $ret = join('', thread_adj_level($ctx, 0));
316 $ret .= ${$ctx->{dst}}; # skel
324 my $mid = $ctx->{mid};
325 my $sres = $ctx->{srch}->get_thread($mid, { asc => 1 });
326 my $msgs = load_results($sres);
327 my $nr = $sres->{total};
328 return missing_thread($ctx) if $nr == 0;
329 my $skel = '<hr><pre>';
330 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
331 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
332 $skel .= "<b\nid=t>Thread overview:</b> ";
333 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
334 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
335 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
336 $skel .= "-- links below jump to the message on this page --\n";
337 $ctx->{-upfx} = '../../';
338 $ctx->{cur_level} = 0;
339 $ctx->{dst} = \$skel;
340 $ctx->{prev_attr} = '';
341 $ctx->{prev_level} = 0;
342 $ctx->{root_anchor} = anchor_for($mid);
344 $ctx->{mapping} = {};
345 $ctx->{s_nr} = "$nr+ messages in thread";
347 my $th = thread_results($msgs);
348 walk_thread($th, $ctx, *pre_thread);
350 return stream_thread($th, $ctx) unless $ctx->{flat};
352 # flat display: lazy load the full message from mini_mime:
353 my $inbox = $ctx->{-inbox};
355 while ($mime = shift @$msgs) {
356 $mime = $inbox->msg_by_mid(mid_clean(mid_mime($mime))) and last;
358 return missing_thread($ctx) unless $mime;
359 $mime = Email::MIME->new($mime);
360 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
361 $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
363 PublicInbox::WwwStream->response($ctx, 200, sub {
365 while ($mime = shift @$msgs) {
366 $mid = mid_clean(mid_mime($mime));
367 $mime = $inbox->msg_by_mid($mid) and last;
370 $mime = Email::MIME->new($mime);
371 return index_entry($mime, $ctx, scalar @$msgs);
378 sub multipart_text_as_html {
379 my ($mime, $upfx) = @_;
382 # scan through all parts, looking for displayable text
383 msg_iter($mime, sub {
385 $rv .= add_text_body($upfx, $p);
391 my ($s, $l, $quot) = @_;
393 # show everything in the full version with anchor from
394 # short version (see above)
395 my $rv = $l->linkify_1(join('', @$quot));
398 # we use a <span> here to allow users to specify their own
399 # color for quoted text
400 $rv = $l->linkify_2(ascii_html($rv));
401 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
404 sub attach_link ($$$$) {
405 my ($upfx, $ct, $p, $fn) = @_;
406 my ($part, $depth, @idx) = @$p;
407 my $nl = $idx[-1] > 1 ? "\n" : '';
408 my $idx = join('.', @idx);
409 my $size = bytes::length($part->body);
410 $ct ||= 'text/plain';
411 $ct =~ s/;.*//; # no attributes
412 $ct = ascii_html($ct);
413 my $desc = $part->header('Content-Description');
414 $desc = $fn unless defined $desc;
415 $desc = '' unless defined $desc;
417 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
419 } elsif ($ct eq 'text/plain') {
424 my @ret = qq($nl<a\nhref="$upfx$idx-$sfn">[-- Attachment #$idx: );
425 my $ts = "Type: $ct, Size: $size bytes";
426 push(@ret, ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]");
427 join('', @ret, "</a>\n");
431 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
432 my ($part, $depth, @idx) = @$p;
433 my $ct = $part->content_type;
434 my $fn = $part->filename;
436 if (defined $ct && $ct =~ m!\btext/x?html\b!i) {
437 return attach_link($upfx, $ct, $p, $fn);
440 my $s = eval { $part->body_str };
442 # badly-encoded message? tell the world about it!
443 return attach_link($upfx, $ct, $p, $fn) if $@;
445 my @lines = split(/^/m, $s);
447 if (defined($fn) || $depth > 0) {
448 $s .= attach_link($upfx, $ct, $p, $fn);
452 my $l = PublicInbox::Linkify->new;
453 while (defined(my $cur = shift @lines)) {
455 # show the previously buffered quote inline
456 flush_quote(\$s, $l, \@quot) if @quot;
459 $cur = $l->linkify_1($cur);
460 $cur = ascii_html($cur);
461 $s .= $l->linkify_2($cur);
470 flush_quote(\$s, $l, \@quot);
472 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
473 $s =~ s/\A\n+//s; # kill leading blank lines
474 $s =~ s/\s+\z//s; # kill all trailing spaces
478 sub _msg_html_prepare {
479 my ($hdr, $ctx) = @_;
480 my $srch = $ctx->{srch} if $ctx;
482 my $rv = "<pre\nid=b>"; # anchor for body start
485 $ctx->{-upfx} = '../';
488 my $mid = $hdr->header_raw('Message-ID');
489 $mid = PublicInbox::Hval->new_msgid($mid);
490 foreach my $h (qw(From To Cc Subject Date)) {
491 my $v = $hdr->header($h);
492 defined($v) && ($v ne '') or next;
493 $v = PublicInbox::Hval->new($v);
496 my @n = PublicInbox::Address::names($v->raw);
497 $title[1] = ascii_html(join(', ', @n));
498 } elsif ($h eq 'Subject') {
499 $title[0] = $v->as_html;
501 $rv .= qq($h: <a\nhref="#r"\nid=t>);
502 $rv .= $v->as_html . "</a>\n";
507 $v =~ s/(\@[^,]+,) /$1\n\t/g if ($h eq 'Cc' || $h eq 'To');
511 $title[0] ||= '(no subject)';
512 $ctx->{-title_html} = join(' - ', @title);
513 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
514 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
515 $rv .= _parent_headers($hdr, $srch);
520 my ($dst, $ctx, $hdr, $tpfx) = @_;
521 my $srch = $ctx->{srch};
522 my $mid = mid_clean($hdr->header_raw('Message-ID'));
523 my $sres = $srch->get_thread($mid);
524 my $nr = $sres->{total};
525 my $expand = qq(<a\nhref="${tpfx}T/#u">expand</a> ) .
526 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
527 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
529 my $parent = in_reply_to($hdr);
530 $$dst .= "\n<b>Thread overview: </b>";
532 if (defined $parent) {
533 $$dst .= "($expand)\n ";
534 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
536 $$dst .= "[no followups, yet] ($expand)\n";
538 $ctx->{next_msg} = undef;
539 $ctx->{parent_msg} = $parent;
543 $$dst .= "$nr+ messages in thread ($expand";
544 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
546 my $subj = $srch->subject_path($hdr->header('Subject'));
547 $ctx->{seen} = { $subj => 1 };
549 $ctx->{prev_attr} = '';
550 $ctx->{prev_level} = 0;
552 walk_thread(thread_results(load_results($sres)), $ctx, *skel_dump);
553 $ctx->{parent_msg} = $parent;
556 sub _parent_headers {
557 my ($hdr, $srch) = @_;
560 my $irt = in_reply_to($hdr);
562 my $v = PublicInbox::Hval->new_msgid($irt);
563 my $html = $v->as_html;
564 my $href = $v->as_href;
565 $rv .= "In-Reply-To: <";
566 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
569 # do not display References: if search is present,
570 # we show the thread skeleton at the bottom, instead.
573 my $refs = $hdr->header_raw('References');
575 # avoid redundant URLs wasting bandwidth
577 $seen{$irt} = 1 if defined $irt;
579 my @raw_refs = ($refs =~ /<([^>]+)>/g);
580 foreach my $ref (@raw_refs) {
583 push @refs, linkify_ref_nosrch($ref);
587 $rv .= 'References: '. join("\n\t", @refs) . "\n";
593 sub squote_maybe ($) {
595 if ($val =~ m{([^\w@\./,\%\+\-])}) {
596 $val =~ s/(['!])/'\\$1'/g; # '!' for csh
602 sub mailto_arg_link {
604 my %cc; # everyone else
605 my $to; # this is the From address
607 foreach my $h (qw(From To Cc)) {
608 my $v = $hdr->header($h);
609 defined($v) && ($v ne '') or next;
610 my @addrs = PublicInbox::Address::emails($v);
611 foreach my $address (@addrs) {
612 my $dst = lc($address);
613 $cc{$dst} ||= $address;
619 my $subj = $hdr->header('Subject') || '';
620 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
621 my $mid = $hdr->header_raw('Message-ID');
622 push @arg, '--in-reply-to='.squote_maybe(mid_clean($mid));
623 my $irt = uri_escape_utf8($mid);
625 push @arg, "--to=$to";
626 $to = uri_escape_utf8($to);
627 $subj = uri_escape_utf8($subj);
628 my @cc = sort values %cc;
629 push(@arg, map { "--cc=$_" } @cc);
630 my $cc = uri_escape_utf8(join(',', @cc));
631 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
634 (\@arg, ascii_html($href));
638 my ($hdr, $standalone, $ctx, $rhref) = @_;
640 my $srch = $ctx->{srch} if $ctx;
643 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
647 thread_skel(\$idx, $ctx, $hdr, $tpfx);
652 if (my $n = $ctx->{next_msg}) {
653 $n = PublicInbox::Hval->new_msgid($n)->as_href;
654 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
657 my $par = $ctx->{parent_msg};
659 $u = PublicInbox::Hval->new_msgid($par)->as_href;
662 if (my $p = $ctx->{prev_msg}) {
663 $prev = PublicInbox::Hval->new_msgid($p)->as_href;
664 if ($p && $par && $p eq $par) {
665 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
666 'rel=prev>prev parent</a>';
669 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
671 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
673 } elsif ($u) { # unlikely
674 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
676 $irt = "$next $prev$parent ";
681 $irt .= qq(<a\nhref="$rhref">reply</a>);
685 sub linkify_ref_nosrch {
686 my $v = PublicInbox::Hval->new_msgid($_[0]);
687 my $html = $v->as_html;
688 my $href = $v->as_href;
689 "<<a\nhref=\"../$href/\">$html</a>>";
694 'm' . id_compress($msgid, 1);
698 my ($upfx, $mid) = @_;
699 # 'subject dummy' is used internally by Mail::Thread
700 return '[no common parent]' if ($mid eq 'subject dummy');
702 $mid = PublicInbox::Hval->new_msgid($mid);
703 my $href = $mid->as_href;
704 my $html = $mid->as_html;
705 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
710 INDENT x ($level - 1);
716 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
721 my $ts = eval { str2time($hdr->header('Date')) };
722 defined($ts) ? $ts : 0;
727 require PublicInbox::Thread;
728 my $th = PublicInbox::Thread->new(@$msgs);
730 $th->order(*sort_ts);
736 require PublicInbox::ExtMsg;
737 PublicInbox::ExtMsg::ext_msg($ctx);
742 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
746 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
749 my ($ctx, $hdr, $level) = @_;
751 my $dst = $ctx->{dst};
752 my $cur = $ctx->{cur};
753 my $mid = mid_clean($hdr->header_raw('Message-ID'));
754 my $f = ascii_html($hdr->header('X-PI-From'));
755 my $d = _msg_date($hdr) . ' ' . indent_for($level) . th_pfx($level);
757 $ctx->{first_level} ||= $level;
759 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
760 $ctx->{prev_attr} = $attr;
762 $ctx->{prev_level} = $level;
767 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
768 "$attr [this message]</a></b>\n";
771 $ctx->{prev_msg} = $mid;
774 $ctx->{next_msg} ||= $mid;
777 # Subject is never undef, this mail was loaded from
778 # our Xapian which would've resulted in '' if it were
779 # really missing (and Filter rejects empty subjects)
780 my $s = $hdr->header('Subject');
781 my $h = $ctx->{srch}->subject_path($s);
782 if ($ctx->{seen}->{$h}) {
785 $ctx->{seen}->{$h} = 1;
786 $s = PublicInbox::Hval->new($s);
789 my $m = PublicInbox::Hval->new_msgid($mid);
791 my $mapping = $ctx->{mapping};
792 my $end = defined($s) ? "$s</a> $f\n" : "$f</a>\n";
794 my $map = $mapping->{$mid};
795 $id = id_compress($mid, 1);
797 $map->[1] = "$d<a\nhref=\"$m\">$end";
800 $m = $ctx->{-upfx}.$m->as_href.'/';
802 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
806 my ($ctx, $level, $node) = @_;
807 if (my $mime = $node->message) {
808 _skel_header($ctx, $mime->header_obj, $level);
810 my $mid = $node->messageid;
811 my $dst = $ctx->{dst};
812 my $mapping = $ctx->{mapping};
813 my $map = $mapping->{$mid} if $mapping;
814 if ($mid eq 'subject dummy') {
815 my $ncp = "\t[no common parent]\n";
816 $map->[1] = $ncp if $map;
820 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
822 $d .= indent_for($level) . th_pfx($level);
823 my $upfx = $ctx->{-upfx};
824 my $m = PublicInbox::Hval->new_msgid($mid);
825 my $href = $upfx . $m->as_href . '/';
826 my $html = $m->as_html;
829 my $id = id_compress($mid, 1);
830 $map->[1] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
831 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
833 $d .= qq{<<a\nhref="$href">$html</a>>\n};
841 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
842 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
846 sub _tryload_ghost ($$) {
847 my ($srch, $mid) = @_;
848 my $smsg = $srch->lookup_mail($mid) or return;
852 # accumulate recent topics if search is supported
853 # returns 200 if done, 404 if not
855 my ($ctx, $level, $node) = @_;
856 my $srch = $ctx->{srch};
857 my $mid = $node->messageid;
858 my $x = $node->message || _tryload_ghost($srch, $mid);
863 $subj = $x->header('Subject') || '';
864 $subj = $srch->subject_normalized($subj);
865 $ts = $x->header('X-PI-TS');
867 $topic = [ $ts, 1, { $subj => $mid }, $subj ];
868 $ctx->{-cur_topic} = $topic;
869 push @{$ctx->{order}}, $topic;
873 $topic = $ctx->{-cur_topic}; # should never be undef
874 $topic->[0] = $ts if $ts > $topic->[0];
876 my $seen = $topic->[2];
877 if (scalar(@$topic) == 3) { # parent was a ghost
879 } elsif (!$seen->{$subj}) {
880 push @$topic, $level, $subj;
882 $seen->{$subj} = $mid; # latest for subject
883 } else { # ghost message
884 return if $level != 0; # ignore child ghosts
885 $topic = [ -666, 0, {} ];
886 $ctx->{-cur_topic} = $topic;
887 push @{$ctx->{order}}, $topic;
893 my $order = delete $ctx->{order}; # [ ts, subj1, subj2, subj3, ... ]
895 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
901 # sort by recency, this allows new posts to "bump" old topics...
902 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
903 my ($ts, $n, $seen, $top, @ex) = @$topic;
905 next unless defined $top; # ghost topic
906 my $mid = delete $seen->{$top};
907 my $href = PublicInbox::Hval->new_msgid($mid)->as_href;
908 $top = PublicInbox::Hval->new($top)->as_html;
911 # $n isn't the total number of posts on the topic,
912 # just the number of posts in the current results window
916 $anchor = '#u'; # top of only message
918 $n = " ($n+ messages)";
919 $anchor = '#t'; # thread skeleton
922 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
923 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
924 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
925 " $ts UTC $n - $mbox / $atom\n";
926 for (my $i = 0; $i < scalar(@ex); $i += 2) {
928 my $sub = $ex[$i + 1];
929 $mid = delete $seen->{$sub};
930 $sub = PublicInbox::Hval->new($sub)->as_html;
931 $href = PublicInbox::Hval->new_msgid($mid)->as_href;
932 $s .= indent_for($level) . TCHILD;
933 $s .= "<a\nhref=\"$href/T/#u\">$sub</a>\n";
937 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
941 sub index_nav { # callback for WwwStream
942 my (undef, $ctx) = @_;
943 delete $ctx->{qp} or return;
948 my $next_o = $ctx->{-next_o};
950 $next = qq!<a\nhref="?o=$next_o"\nrel=next>next</a>!;
952 if (my $cur_o = $ctx->{-cur_o}) {
953 $latest = qq! <a\nhref=.>latest</a>!;
955 my $o = $cur_o - ($next_o - $cur_o);
957 $prev = qq!<a\nhref="?o=$o"\nrel=prev>prev</a>!;
959 $prev = qq!<a\nhref=.\nrel=prev>prev</a>!;
962 "<hr><pre>page: $next $prev$latest</pre>";
967 my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/);
968 my $opts = { offset => $off, limit => 200 };
971 my $sres = $ctx->{srch}->query('', $opts);
972 my $nr = scalar @{$sres->{msgs}};
974 $sres = load_results($sres);
975 walk_thread(thread_results($sres), $ctx, *acc_topic);
977 $ctx->{-next_o} = $off+ $nr;
978 $ctx->{-cur_o} = $off;
979 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
982 sub thread_adj_level {
983 my ($ctx, $level) = @_;
985 my $max = $ctx->{cur_level};
987 return ('', '') if $max == 0; # flat output
989 # reset existing lists
990 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
991 $ctx->{cur_level} = 0;
993 } elsif ($level == $max) { # continue existing list
995 } elsif ($level < $max) {
996 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
997 $ctx->{cur_level} = $level;
998 ("$beg<li>", '</li>');
999 } else { # ($level > $max) # start a new level
1000 $ctx->{cur_level} = $level;
1001 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1006 sub ghost_index_entry {
1007 my ($ctx, $level, $mid) = @_;
1008 my ($beg, $end) = thread_adj_level($ctx, $level);
1009 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $mid) . '</pre>' . $end;