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 Date::Parse qw/str2time/;
10 use PublicInbox::Hval qw/ascii_html/;
11 use PublicInbox::Linkify;
12 use PublicInbox::MID qw/mid_clean id_compress mid_mime mid_escape/;
13 use PublicInbox::MsgIter;
14 use PublicInbox::Address;
15 use PublicInbox::WwwStream;
16 use PublicInbox::Reply;
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 my $ibx = $ctx->{-inbox};
56 if (my $url = $ibx->{infourl}) {
57 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
58 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
61 my ($arg, $link) = PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
62 push @$arg, '/path/to/YOUR_REPLY';
63 $arg = ascii_html(join(" \\\n ", '', @$arg));
66 id=R><b>Reply instructions:</b>
68 You may reply publically to <a
69 href=#t>this message</a> via plain-text email
70 using any one of the following methods:
72 * Save the following mbox file, import it into your mail client,
73 and reply-to-all from there: <a
76 Avoid top-posting and favor interleaved quoting:
78 href="$p_url">$p_url</a>
80 * Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,
81 and <b>--in-reply-to</b> switches of git-send-email(1):
86 href="$se_url">$se_url</a>
88 * If your mail client supports setting the <b>In-Reply-To</b> header
89 via mailto: links, try the <a
90 href="$link">mailto: link</a></pre>
96 my %mid = map { $_ => 1 } $hdr->header_raw('Message-ID');
97 my @refs = ($hdr->header_raw('References'),
98 $hdr->header_raw('In-Reply-To'));
99 @refs = ((join(' ', @refs)) =~ /<([^>]+)>/g);
100 while (defined(my $irt = pop @refs)) {
101 next if $mid{"<$irt>"};
107 sub _hdr_names ($$) {
108 my ($hdr, $field) = @_;
109 my $val = $hdr->header($field) or return '';
110 ascii_html(join(', ', PublicInbox::Address::names($val)));
114 my ($nr, $singular, $plural) = @_;
115 return "0 $plural" if $nr == 0;
116 $nr == 1 ? "$nr $singular" : "$nr $plural";
119 # this is already inside a <pre>
121 my ($mime, $ctx, $more) = @_;
122 my $srch = $ctx->{srch};
123 my $hdr = $mime->header_obj;
124 my $subj = $hdr->header('Subject');
126 my $mid_raw = mid_clean(mid_mime($mime));
127 my $id = id_compress($mid_raw, 1);
130 my $root_anchor = $ctx->{root_anchor} || '';
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_escape($mid_raw) . '/';
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 && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
152 my $mirt = PublicInbox::Hval->new_msgid($irt);
153 my $href = $upfx . $mirt->{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>";
169 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
170 $rv .= "\t[relevance $pct->{$mid_raw}%]";
173 my $threaded = 'threaded';
178 $flat = "<b>$flat</b>";
180 $threaded = "<b>$threaded</b>";
182 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
183 $rv .= "|<a\nhref=\"${mhref}t/#u\">$threaded</a>]";
184 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
189 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
193 sub pad_link ($$;$) {
194 my ($mid, $level, $s) = @_;
196 my $id = id_compress($mid, 1);
197 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
201 my ($mid_raw, $irt, $id, $ctx) = @_;
203 my $mapping = $ctx->{mapping} or return $rv;
205 my $mid_map = $mapping->{$mid_raw};
207 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
208 my ($attr, $node, $idx, $level) = @$mid_map;
209 my $children = $node->{children};
210 my $nr_c = scalar @$children;
213 if (my $smsg = $node->{smsg}) {
214 ($$irt) = (($smsg->{references} || '') =~ m/<([^>]+)>\z/);
216 my $irt_map = $mapping->{$$irt} if defined $$irt;
217 if (defined $irt_map) {
218 $siblings = $irt_map->[1]->{children};
219 $nr_s = scalar(@$siblings) - 1;
220 $rv .= $pad . $irt_map->[0];
222 my $prev = $siblings->[$idx - 1];
223 my $pmid = $prev->{id};
225 my $s = ($idx - 1). ' preceding siblings ...';
226 $rv .= pad_link($pmid, $level, $s);
227 } elsif ($idx == 2) {
228 my $ppmid = $siblings->[0]->{id};
229 $rv .= $pad . $mapping->{$ppmid}->[0];
231 $rv .= $pad . $mapping->{$pmid}->[0];
234 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
235 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
236 $attr =~ s!\n\z!</b>\n!s;
237 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
238 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
241 my $cmid = $children->[0]->{id};
242 $rv .= $pad . $mapping->{$cmid}->[0];
244 my $s = ($nr_c - 1). ' more replies';
245 $rv .= pad_link($cmid, $level + 1, $s);
246 } elsif (my $cn = $children->[1]) {
247 $rv .= $pad . $mapping->{$cn->{id}}->[0];
251 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
253 my $nmid = $next->{id};
254 $rv .= $pad . $mapping->{$nmid}->[0];
255 my $nnext = $nr_s - $idx;
257 my $s = ($nnext - 1).' subsequent siblings';
258 $rv .= pad_link($nmid, $level, $s);
259 } elsif (my $nn = $siblings->[$idx + 2]) {
260 $rv .= $pad . $mapping->{$nn->{id}}->[0];
263 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
267 my ($rootset, $ctx, $cb) = @_;
268 my @q = map { (0, $_, -1) } @$rootset;
270 my ($level, $node, $i) = splice(@q, 0, 3);
271 defined $node or next;
272 $cb->($ctx, $level, $node, $i);
275 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
280 my ($ctx, $level, $node, $idx) = @_;
281 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
282 skel_dump($ctx, $level, $node);
285 sub thread_index_entry {
286 my ($ctx, $level, $mime) = @_;
287 my ($beg, $end) = thread_adj_level($ctx, $level);
288 $beg . '<pre>' . index_entry($mime, $ctx, 0) . '</pre>' . $end;
291 sub stream_thread ($$) {
292 my ($rootset, $ctx) = @_;
293 my $inbox = $ctx->{-inbox};
295 my @q = map { (0, $_) } @$rootset;
299 my $node = shift @q or next;
301 unshift @q, map { ($cl, $_) } @{$node->{children}};
302 $mime = $inbox->msg_by_smsg($node->{smsg}) and last;
304 return missing_thread($ctx) unless $mime;
306 $mime = PublicInbox::MIME->new($mime);
307 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
308 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
309 PublicInbox::WwwStream->response($ctx, 200, sub {
313 my $node = shift @q or next;
315 unshift @q, map { ($cl, $_) } @{$node->{children}};
316 my $mid = $node->{id};
317 if ($mime = $inbox->msg_by_smsg($node->{smsg})) {
318 $mime = PublicInbox::MIME->new($mime);
319 return thread_index_entry($ctx, $level, $mime);
321 return ghost_index_entry($ctx, $level, $node);
324 my $ret = join('', thread_adj_level($ctx, 0));
325 $ret .= ${$ctx->{dst}}; # skel
333 my $mid = $ctx->{mid};
334 my $srch = $ctx->{srch};
335 my $sres = $srch->get_thread($mid);
336 my $msgs = load_results($srch, $sres);
337 my $nr = $sres->{total};
338 return missing_thread($ctx) if $nr == 0;
339 my $skel = '<hr><pre>';
340 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
341 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
342 $skel .= "<b\nid=t>Thread overview:</b> ";
343 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
344 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
345 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
346 $skel .= "-- links below jump to the message on this page --\n";
347 $ctx->{-upfx} = '../../';
348 $ctx->{cur_level} = 0;
349 $ctx->{dst} = \$skel;
350 $ctx->{prev_attr} = '';
351 $ctx->{prev_level} = 0;
352 $ctx->{root_anchor} = anchor_for($mid);
354 $ctx->{mapping} = {};
355 $ctx->{s_nr} = "$nr+ messages in thread";
357 my $rootset = thread_results($msgs);
358 walk_thread($rootset, $ctx, *pre_thread);
360 return stream_thread($rootset, $ctx) unless $ctx->{flat};
362 # flat display: lazy load the full message from smsg
363 my $inbox = $ctx->{-inbox};
365 while ($mime = shift @$msgs) {
366 $mime = $inbox->msg_by_smsg($mime) and last;
368 return missing_thread($ctx) unless $mime;
369 $mime = PublicInbox::MIME->new($mime);
370 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
371 $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
373 PublicInbox::WwwStream->response($ctx, 200, sub {
375 while ($mime = shift @$msgs) {
376 $mime = $inbox->msg_by_smsg($mime) and last;
379 $mime = PublicInbox::MIME->new($mime);
380 return index_entry($mime, $ctx, scalar @$msgs);
387 sub multipart_text_as_html {
388 my ($mime, $upfx) = @_;
391 # scan through all parts, looking for displayable text
392 msg_iter($mime, sub {
394 $rv .= add_text_body($upfx, $p);
400 my ($s, $l, $quot) = @_;
402 # show everything in the full version with anchor from
403 # short version (see above)
404 my $rv = $l->linkify_1(join('', @$quot));
407 # we use a <span> here to allow users to specify their own
408 # color for quoted text
409 $rv = $l->linkify_2(ascii_html($rv));
410 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
413 sub attach_link ($$$$;$) {
414 my ($upfx, $ct, $p, $fn, $err) = @_;
415 my ($part, $depth, @idx) = @$p;
416 my $nl = $idx[-1] > 1 ? "\n" : '';
417 my $idx = join('.', @idx);
418 my $size = bytes::length($part->body);
420 # hide attributes normally, unless we want to aid users in
421 # spotting MUA problems:
422 $ct =~ s/;.*// unless $err;
423 $ct = ascii_html($ct);
424 my $desc = $part->header('Content-Description');
425 $desc = $fn unless defined $desc;
426 $desc = '' unless defined $desc;
428 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
430 } elsif ($ct eq 'text/plain') {
435 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
438 "[-- Warning: decoded text below may be mangled --]\n";
440 $ret .= "[-- Attachment #$idx: ";
441 my $ts = "Type: $ct, Size: $size bytes";
442 $desc = ascii_html($desc);
443 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
448 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
449 my ($part, $depth) = @$p; # attachment @idx is unused
450 my $ct = $part->content_type || 'text/plain';
451 my $fn = $part->filename;
453 if ($ct =~ m!\btext/x?html\b!i) {
454 return attach_link($upfx, $ct, $p, $fn);
457 my $s = eval { $part->body_str };
459 # badly-encoded message? tell the world about it!
462 if ($ct =~ m!\btext/plain\b!i) {
463 # Try to assume UTF-8 because Alpine seems to
464 # do wacky things and set charset=X-UNKNOWN
465 $part->charset_set('UTF-8');
466 $s = eval { $part->body_str };
468 # If forcing charset=UTF-8 failed,
469 # attach_link will warn further down...
470 $s = $part->body if $@;
472 return attach_link($upfx, $ct, $p, $fn);
476 my @lines = split(/^/m, $s);
478 if (defined($fn) || $depth > 0 || $err) {
479 $s .= attach_link($upfx, $ct, $p, $fn, $err);
483 my $l = PublicInbox::Linkify->new;
484 foreach my $cur (@lines) {
486 # show the previously buffered quote inline
487 flush_quote(\$s, $l, \@quot) if @quot;
491 $s .= $l->linkify_2(ascii_html($cur));
497 if (@quot) { # ugh, top posted
498 flush_quote(\$s, $l, \@quot);
499 } elsif ($s =~ /\n\z/s) { # common, last line ends with a newline
501 } else { # some editors don't do newlines...
506 sub _msg_html_prepare {
507 my ($hdr, $ctx) = @_;
508 my $srch = $ctx->{srch} if $ctx;
510 my $rv = "<pre\nid=b>"; # anchor for body start
513 $ctx->{-upfx} = '../';
516 my $mid = mid_clean($hdr->header_raw('Message-ID'));
517 $mid = PublicInbox::Hval->new_msgid($mid);
518 foreach my $h (qw(From To Cc Subject Date)) {
519 my $v = $hdr->header($h);
520 defined($v) && ($v ne '') or next;
521 $v = PublicInbox::Hval->new($v);
524 my @n = PublicInbox::Address::names($v->raw);
525 $title[1] = ascii_html(join(', ', @n));
526 } elsif ($h eq 'Subject') {
527 $title[0] = $v->as_html;
529 $rv .= qq($h: <a\nhref="#r"\nid=t>);
530 $rv .= $v->as_html . "</a>\n";
535 $v =~ s/(\@[^,]+,) /$1\n\t/g if ($h eq 'Cc' || $h eq 'To');
539 $title[0] ||= '(no subject)';
540 $ctx->{-title_html} = join(' - ', @title);
541 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
542 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
543 $rv .= _parent_headers($hdr, $srch);
548 my ($dst, $ctx, $hdr, $tpfx) = @_;
549 my $srch = $ctx->{srch};
550 my $mid = mid_clean($hdr->header_raw('Message-ID'));
551 my $sres = $srch->get_thread($mid);
552 my $nr = $sres->{total};
553 my $expand = qq(<a\nhref="${tpfx}T/#u">expand</a> ) .
554 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
555 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
557 my $parent = in_reply_to($hdr);
558 $$dst .= "\n<b>Thread overview: </b>";
560 if (defined $parent) {
561 $$dst .= "($expand)\n ";
562 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
564 $$dst .= "[no followups, yet] ($expand)\n";
566 $ctx->{next_msg} = undef;
567 $ctx->{parent_msg} = $parent;
571 $$dst .= "$nr+ messages in thread ($expand";
572 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
574 my $subj = $srch->subject_path($hdr->header('Subject'));
575 $ctx->{seen} = { $subj => 1 };
577 $ctx->{prev_attr} = '';
578 $ctx->{prev_level} = 0;
580 $sres = load_results($srch, $sres);
581 walk_thread(thread_results($sres), $ctx, *skel_dump);
582 $ctx->{parent_msg} = $parent;
585 sub _parent_headers {
586 my ($hdr, $srch) = @_;
589 my $irt = in_reply_to($hdr);
591 my $v = PublicInbox::Hval->new_msgid($irt);
592 my $html = $v->as_html;
593 my $href = $v->{href};
594 $rv .= "In-Reply-To: <";
595 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
598 # do not display References: if search is present,
599 # we show the thread skeleton at the bottom, instead.
602 my $refs = $hdr->header_raw('References');
604 # avoid redundant URLs wasting bandwidth
606 $seen{$irt} = 1 if defined $irt;
608 my @raw_refs = ($refs =~ /<([^>]+)>/g);
609 foreach my $ref (@raw_refs) {
612 push @refs, linkify_ref_nosrch($ref);
616 $rv .= 'References: '. join("\n\t", @refs) . "\n";
623 my ($hdr, $standalone, $ctx, $rhref) = @_;
625 my $srch = $ctx->{srch} if $ctx;
628 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
632 thread_skel(\$idx, $ctx, $hdr, $tpfx);
637 if (my $n = $ctx->{next_msg}) {
638 $n = PublicInbox::Hval->new_msgid($n)->{href};
639 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
642 my $par = $ctx->{parent_msg};
644 $u = PublicInbox::Hval->new_msgid($par)->{href};
647 if (my $p = $ctx->{prev_msg}) {
648 $prev = PublicInbox::Hval->new_msgid($p)->{href};
649 if ($p && $par && $p eq $par) {
650 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
651 'rel=prev>prev parent</a>';
654 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
656 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
658 } elsif ($u) { # unlikely
659 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
661 $irt = "$next $prev$parent ";
666 $irt .= qq(<a\nhref="$rhref">reply</a>);
670 sub linkify_ref_nosrch {
671 my $v = PublicInbox::Hval->new_msgid($_[0]);
672 my $html = $v->as_html;
673 my $href = $v->{href};
674 "<<a\nhref=\"../$href/\">$html</a>>";
679 'm' . id_compress($msgid, 1);
683 my ($upfx, $mid) = @_;
685 $mid = PublicInbox::Hval->new_msgid($mid);
686 my $href = $mid->{href};
687 my $html = $mid->as_html;
688 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
693 INDENT x ($level - 1);
697 my ($srch, $sres) = @_;
698 my $msgs = delete $sres->{msgs};
699 $srch->retry_reopen(sub { [ map { $_->mid; $_ } @$msgs ] });
704 my $ts = eval { str2time($hdr->header('Date')) };
705 defined($ts) ? $ts : 0;
710 require PublicInbox::SearchThread;
711 PublicInbox::SearchThread::thread($msgs, *sort_ts);
716 require PublicInbox::ExtMsg;
717 PublicInbox::ExtMsg::ext_msg($ctx);
722 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
726 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
729 my ($ctx, $level, $node) = @_;
730 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
732 my $dst = $ctx->{dst};
733 my $cur = $ctx->{cur};
734 my $mid = $smsg->{mid};
735 my $f = ascii_html($smsg->from_name);
736 my $d = fmt_ts($smsg->{ts}) . ' ' . indent_for($level) . th_pfx($level);
738 $ctx->{first_level} ||= $level;
740 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
741 $ctx->{prev_attr} = $attr;
743 $ctx->{prev_level} = $level;
748 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
749 "$attr [this message]</a></b>\n";
752 $ctx->{prev_msg} = $mid;
755 $ctx->{next_msg} ||= $mid;
758 # Subject is never undef, this mail was loaded from
759 # our Xapian which would've resulted in '' if it were
760 # really missing (and Filter rejects empty subjects)
761 my $s = $smsg->subject;
762 my $h = $ctx->{srch}->subject_path($s);
763 if ($ctx->{seen}->{$h}) {
766 $ctx->{seen}->{$h} = 1;
767 $s = PublicInbox::Hval->new($s);
772 my $mapping = $ctx->{mapping};
773 my $end = defined($s) ? "$s</a> $f\n" : "$f</a>\n";
775 my $map = $mapping->{$mid};
776 $id = id_compress($mid, 1);
778 $map->[0] = "$d<a\nhref=\"$m\">$end";
781 $m = $ctx->{-upfx}.mid_escape($mid).'/';
783 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
787 my ($ctx, $level, $node) = @_;
789 my $mid = $node->{id};
790 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
792 $d .= indent_for($level) . th_pfx($level);
793 my $upfx = $ctx->{-upfx};
794 my $m = PublicInbox::Hval->new_msgid($mid);
795 my $href = $upfx . $m->{href} . '/';
796 my $html = $m->as_html;
798 my $mapping = $ctx->{mapping};
799 my $map = $mapping->{$mid} if $mapping;
801 my $id = id_compress($mid, 1);
802 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
803 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
805 $d .= qq{<<a\nhref="$href">$html</a>>\n};
807 my $dst = $ctx->{dst};
813 (eval { $a->topmost->{smsg}->ts } || 0) <=>
814 (eval { $b->topmost->{smsg}->ts } || 0)
818 # accumulate recent topics if search is supported
819 # returns 200 if done, 404 if not
821 my ($ctx, $level, $node) = @_;
822 my $srch = $ctx->{srch};
823 my $mid = $node->{id};
824 my $x = $node->{smsg} || $srch->lookup_mail($mid);
829 $subj = $srch->subject_normalized($subj);
832 $topic = [ $ts, 1, { $subj => $mid }, $subj ];
833 $ctx->{-cur_topic} = $topic;
834 push @{$ctx->{order}}, $topic;
838 $topic = $ctx->{-cur_topic}; # should never be undef
839 $topic->[0] = $ts if $ts > $topic->[0];
841 my $seen = $topic->[2];
842 if (scalar(@$topic) == 3) { # parent was a ghost
844 } elsif (!$seen->{$subj}) {
845 push @$topic, $level, $subj;
847 $seen->{$subj} = $mid; # latest for subject
848 } else { # ghost message
849 return if $level != 0; # ignore child ghosts
850 $topic = [ -666, 0, {} ];
851 $ctx->{-cur_topic} = $topic;
852 push @{$ctx->{order}}, $topic;
858 my $order = delete $ctx->{order}; # [ ts, subj1, subj2, subj3, ... ]
860 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
866 # sort by recency, this allows new posts to "bump" old topics...
867 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
868 my ($ts, $n, $seen, $top, @ex) = @$topic;
870 next unless defined $top; # ghost topic
871 my $mid = delete $seen->{$top};
872 my $href = mid_escape($mid);
873 $top = PublicInbox::Hval->new($top)->as_html;
876 # $n isn't the total number of posts on the topic,
877 # just the number of posts in the current results window
881 $anchor = '#u'; # top of only message
883 $n = " ($n+ messages)";
884 $anchor = '#t'; # thread skeleton
887 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
888 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
889 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
890 " $ts UTC $n - $mbox / $atom\n";
891 for (my $i = 0; $i < scalar(@ex); $i += 2) {
893 my $sub = $ex[$i + 1];
894 $mid = delete $seen->{$sub};
895 $sub = PublicInbox::Hval->new($sub)->as_html;
896 $href = mid_escape($mid);
897 $s .= indent_for($level) . TCHILD;
898 $s .= "<a\nhref=\"$href/T/#u\">$sub</a>\n";
902 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
906 sub index_nav { # callback for WwwStream
907 my (undef, $ctx) = @_;
908 delete $ctx->{qp} or return;
913 my $next_o = $ctx->{-next_o};
915 $next = qq!<a\nhref="?o=$next_o"\nrel=next>next</a>!;
917 if (my $cur_o = $ctx->{-cur_o}) {
918 $latest = qq! <a\nhref=.>latest</a>!;
920 my $o = $cur_o - ($next_o - $cur_o);
922 $prev = qq!<a\nhref="?o=$o"\nrel=prev>prev</a>!;
924 $prev = qq!<a\nhref=.\nrel=prev>prev</a>!;
927 "<hr><pre>page: $next $prev$latest</pre>";
932 my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/);
933 my $opts = { offset => $off, limit => 200 };
936 my $srch = $ctx->{srch};
937 my $sres = $srch->query('', $opts);
938 my $nr = scalar @{$sres->{msgs}};
940 $sres = load_results($srch, $sres);
941 walk_thread(thread_results($sres), $ctx, *acc_topic);
943 $ctx->{-next_o} = $off+ $nr;
944 $ctx->{-cur_o} = $off;
945 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
948 sub thread_adj_level {
949 my ($ctx, $level) = @_;
951 my $max = $ctx->{cur_level};
953 return ('', '') if $max == 0; # flat output
955 # reset existing lists
956 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
957 $ctx->{cur_level} = 0;
959 } elsif ($level == $max) { # continue existing list
961 } elsif ($level < $max) {
962 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
963 $ctx->{cur_level} = $level;
964 ("$beg<li>", '</li>');
965 } else { # ($level > $max) # start a new level
966 $ctx->{cur_level} = $level;
967 my $beg = ($max ? '<li>' : '') . '<ul><li>';
972 sub ghost_index_entry {
973 my ($ctx, $level, $node) = @_;
974 my ($beg, $end) = thread_adj_level($ctx, $level);
975 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})