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 # map = [children, attr, node, idx, level]
205 my $map = $mapping->{$mid_raw};
206 my $nr_c = scalar @{$map->[0]};
208 my $level = $map->[4];
210 my $irt_map = $mapping->{$irt} if defined $irt;
211 if (defined $irt_map) {
212 my $siblings = $irt_map->[0];
213 $nr_s = scalar(@$siblings) - 1;
214 $rv .= $pad . $irt_map->[1];
216 my $prev = $siblings->[$idx - 1];
217 my $pmid = $prev->messageid;
219 my $s = ($idx - 1). ' preceding siblings ...';
220 $rv .= pad_link($pmid, $level, $s);
221 } elsif ($idx == 2) {
222 my $ppmid = $siblings->[0]->messageid;
223 $rv .= $pad . $mapping->{$ppmid}->[1];
225 $rv .= $pad . $mapping->{$pmid}->[1];
228 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
229 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
230 my $this = $map->[1];
231 $this =~ s!\n\z!</b>\n!s;
232 $this =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
233 $this =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
235 my $node = $map->[2];
236 if (my $child = $node->child) {
237 my $cmid = $child->messageid;
238 $rv .= $pad . $mapping->{$cmid}->[1];
240 my $s = ($nr_c - 1). ' more replies';
241 $rv .= pad_link($cmid, $level + 1, $s);
242 } elsif (my $cn = $child->next) {
243 $rv .= $pad . $mapping->{$cn->messageid}->[1];
246 if (my $next = $node->next) {
247 my $nmid = $next->messageid;
248 $rv .= $pad . $mapping->{$nmid}->[1];
249 my $nnext = $nr_s - $idx;
251 my $s = ($nnext - 1).' subsequent siblings';
252 $rv .= pad_link($nmid, $level, $s);
253 } elsif (my $nn = $next->next) {
254 $rv .= $pad . $mapping->{$nn->messageid}->[1];
257 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
261 my ($th, $ctx, $cb) = @_;
262 my @q = map { (0, $_) } $th->rootset;
264 my $level = shift @q;
265 my $node = shift @q or next;
266 $cb->($ctx, $level, $node);
267 unshift @q, $level+1, $node->child, $level, $node->next;
272 my ($ctx, $level, $node) = @_;
273 my $mapping = $ctx->{mapping};
275 if (my $parent = $node->parent) {
276 my $m = $mapping->{$parent->messageid}->[0];
280 $mapping->{$node->messageid} = [ [], '', $node, $idx, $level ];
281 skel_dump($ctx, $level, $node);
284 sub thread_index_entry {
285 my ($ctx, $level, $mime) = @_;
286 my ($beg, $end) = thread_adj_level($ctx, $level);
287 $beg . '<pre>' . index_entry($mime, $ctx, 0) . '</pre>' . $end;
290 sub stream_thread ($$) {
292 my $inbox = $ctx->{-inbox};
294 my @q = map { (0, $_) } $th->rootset;
298 my $node = shift @q or next;
299 unshift @q, $level+1, $node->child, $level, $node->next;
300 $mime = $inbox->msg_by_mid($node->messageid) and last;
302 return missing_thread($ctx) unless $mime;
304 $mime = Email::MIME->new($mime);
305 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
306 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
307 PublicInbox::WwwStream->response($ctx, 200, sub {
311 my $node = shift @q or next;
312 unshift @q, $level+1, $node->child, $level, $node->next;
313 my $mid = $node->messageid;
314 if ($mime = $inbox->msg_by_mid($mid)) {
315 $mime = Email::MIME->new($mime);
316 return thread_index_entry($ctx, $level, $mime);
318 return ghost_index_entry($ctx, $level, $mid);
321 my $ret = join('', thread_adj_level($ctx, 0));
322 $ret .= ${$ctx->{dst}}; # skel
330 my $mid = $ctx->{mid};
331 my $sres = $ctx->{srch}->get_thread($mid, { asc => 1 });
332 my $msgs = load_results($sres);
333 my $nr = $sres->{total};
334 return missing_thread($ctx) if $nr == 0;
335 my $skel = '<hr><pre>';
336 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
337 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
338 $skel .= "<b\nid=t>Thread overview:</b> ";
339 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
340 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
341 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
342 $skel .= "-- links below jump to the message on this page --\n";
343 $ctx->{-upfx} = '../../';
344 $ctx->{cur_level} = 0;
345 $ctx->{dst} = \$skel;
346 $ctx->{prev_attr} = '';
347 $ctx->{prev_level} = 0;
348 $ctx->{root_anchor} = anchor_for($mid);
350 $ctx->{mapping} = {};
351 $ctx->{s_nr} = "$nr+ messages in thread";
353 my $th = thread_results($msgs);
354 walk_thread($th, $ctx, *pre_thread);
356 return stream_thread($th, $ctx) unless $ctx->{flat};
358 # flat display: lazy load the full message from mini_mime:
359 my $inbox = $ctx->{-inbox};
361 while ($mime = shift @$msgs) {
362 $mime = $inbox->msg_by_mid(mid_clean(mid_mime($mime))) and last;
364 return missing_thread($ctx) unless $mime;
365 $mime = Email::MIME->new($mime);
366 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
367 $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
369 PublicInbox::WwwStream->response($ctx, 200, sub {
371 while ($mime = shift @$msgs) {
372 $mid = mid_clean(mid_mime($mime));
373 $mime = $inbox->msg_by_mid($mid) and last;
376 $mime = Email::MIME->new($mime);
377 return index_entry($mime, $ctx, scalar @$msgs);
384 sub multipart_text_as_html {
385 my ($mime, $upfx) = @_;
388 # scan through all parts, looking for displayable text
389 msg_iter($mime, sub {
391 $rv .= add_text_body($upfx, $p);
397 my ($s, $l, $quot) = @_;
399 # show everything in the full version with anchor from
400 # short version (see above)
401 my $rv = $l->linkify_1(join('', @$quot));
404 # we use a <span> here to allow users to specify their own
405 # color for quoted text
406 $rv = $l->linkify_2(ascii_html($rv));
407 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
410 sub attach_link ($$$$;$) {
411 my ($upfx, $ct, $p, $fn, $err) = @_;
412 my ($part, $depth, @idx) = @$p;
413 my $nl = $idx[-1] > 1 ? "\n" : '';
414 my $idx = join('.', @idx);
415 my $size = bytes::length($part->body);
417 # hide attributes normally, unless we want to aid users in
418 # spotting MUA problems:
419 $ct =~ s/;.*// unless $err;
420 $ct = ascii_html($ct);
421 my $desc = $part->header('Content-Description');
422 $desc = $fn unless defined $desc;
423 $desc = '' unless defined $desc;
425 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
427 } elsif ($ct eq 'text/plain') {
432 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
435 "[-- Warning: decoded text below may be mangled --]\n";
437 $ret .= "[-- Attachment #$idx: ";
438 my $ts = "Type: $ct, Size: $size bytes";
439 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
444 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
445 my ($part, $depth, @idx) = @$p;
446 my $ct = $part->content_type || 'text/plain';
447 my $fn = $part->filename;
449 if ($ct =~ m!\btext/x?html\b!i) {
450 return attach_link($upfx, $ct, $p, $fn);
453 my $s = eval { $part->body_str };
455 # badly-encoded message? tell the world about it!
458 if ($ct =~ m!\btext/plain\b!i) {
459 # Try to assume UTF-8 because Alpine seems to
460 # do wacky things and set charset=X-UNKNOWN
461 $part->charset_set('UTF-8');
462 $s = eval { $part->body_str };
464 # If forcing charset=UTF-8 failed,
465 # attach_link will warn further down...
466 $s = $part->body if $@;
468 return attach_link($upfx, $ct, $p, $fn);
472 my @lines = split(/^/m, $s);
474 if (defined($fn) || $depth > 0 || $err) {
475 $s .= attach_link($upfx, $ct, $p, $fn, $err);
479 my $l = PublicInbox::Linkify->new;
480 while (defined(my $cur = shift @lines)) {
482 # show the previously buffered quote inline
483 flush_quote(\$s, $l, \@quot) if @quot;
486 $cur = $l->linkify_1($cur);
487 $cur = ascii_html($cur);
488 $s .= $l->linkify_2($cur);
497 flush_quote(\$s, $l, \@quot);
499 $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace
500 $s =~ s/\A\n+//s; # kill leading blank lines
501 $s =~ s/\s+\z//s; # kill all trailing spaces
505 sub _msg_html_prepare {
506 my ($hdr, $ctx) = @_;
507 my $srch = $ctx->{srch} if $ctx;
509 my $rv = "<pre\nid=b>"; # anchor for body start
512 $ctx->{-upfx} = '../';
515 my $mid = mid_clean($hdr->header_raw('Message-ID'));
516 $mid = PublicInbox::Hval->new_msgid($mid);
517 foreach my $h (qw(From To Cc Subject Date)) {
518 my $v = $hdr->header($h);
519 defined($v) && ($v ne '') or next;
520 $v = PublicInbox::Hval->new($v);
523 my @n = PublicInbox::Address::names($v->raw);
524 $title[1] = ascii_html(join(', ', @n));
525 } elsif ($h eq 'Subject') {
526 $title[0] = $v->as_html;
528 $rv .= qq($h: <a\nhref="#r"\nid=t>);
529 $rv .= $v->as_html . "</a>\n";
534 $v =~ s/(\@[^,]+,) /$1\n\t/g if ($h eq 'Cc' || $h eq 'To');
538 $title[0] ||= '(no subject)';
539 $ctx->{-title_html} = join(' - ', @title);
540 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
541 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
542 $rv .= _parent_headers($hdr, $srch);
547 my ($dst, $ctx, $hdr, $tpfx) = @_;
548 my $srch = $ctx->{srch};
549 my $mid = mid_clean($hdr->header_raw('Message-ID'));
550 my $sres = $srch->get_thread($mid);
551 my $nr = $sres->{total};
552 my $expand = qq(<a\nhref="${tpfx}T/#u">expand</a> ) .
553 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
554 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
556 my $parent = in_reply_to($hdr);
557 $$dst .= "\n<b>Thread overview: </b>";
559 if (defined $parent) {
560 $$dst .= "($expand)\n ";
561 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
563 $$dst .= "[no followups, yet] ($expand)\n";
565 $ctx->{next_msg} = undef;
566 $ctx->{parent_msg} = $parent;
570 $$dst .= "$nr+ messages in thread ($expand";
571 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
573 my $subj = $srch->subject_path($hdr->header('Subject'));
574 $ctx->{seen} = { $subj => 1 };
576 $ctx->{prev_attr} = '';
577 $ctx->{prev_level} = 0;
579 walk_thread(thread_results(load_results($sres)), $ctx, *skel_dump);
580 $ctx->{parent_msg} = $parent;
583 sub _parent_headers {
584 my ($hdr, $srch) = @_;
587 my $irt = in_reply_to($hdr);
589 my $v = PublicInbox::Hval->new_msgid($irt);
590 my $html = $v->as_html;
591 my $href = $v->{href};
592 $rv .= "In-Reply-To: <";
593 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
596 # do not display References: if search is present,
597 # we show the thread skeleton at the bottom, instead.
600 my $refs = $hdr->header_raw('References');
602 # avoid redundant URLs wasting bandwidth
604 $seen{$irt} = 1 if defined $irt;
606 my @raw_refs = ($refs =~ /<([^>]+)>/g);
607 foreach my $ref (@raw_refs) {
610 push @refs, linkify_ref_nosrch($ref);
614 $rv .= 'References: '. join("\n\t", @refs) . "\n";
620 sub squote_maybe ($) {
622 if ($val =~ m{([^\w@\./,\%\+\-])}) {
623 $val =~ s/(['!])/'\\$1'/g; # '!' for csh
629 sub mailto_arg_link {
631 my %cc; # everyone else
632 my $to; # this is the From address
634 foreach my $h (qw(From To Cc)) {
635 my $v = $hdr->header($h);
636 defined($v) && ($v ne '') or next;
637 my @addrs = PublicInbox::Address::emails($v);
638 foreach my $address (@addrs) {
639 my $dst = lc($address);
640 $cc{$dst} ||= $address;
646 my $subj = $hdr->header('Subject') || '';
647 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
648 my $mid = $hdr->header_raw('Message-ID');
649 push @arg, '--in-reply-to='.squote_maybe(mid_clean($mid));
650 my $irt = mid_escape($mid);
652 push @arg, "--to=$to";
653 $to = uri_escape_utf8($to);
654 $subj = uri_escape_utf8($subj);
655 my @cc = sort values %cc;
656 push(@arg, map { "--cc=$_" } @cc);
657 my $cc = uri_escape_utf8(join(',', @cc));
658 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
661 (\@arg, ascii_html($href));
665 my ($hdr, $standalone, $ctx, $rhref) = @_;
667 my $srch = $ctx->{srch} if $ctx;
670 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
674 thread_skel(\$idx, $ctx, $hdr, $tpfx);
679 if (my $n = $ctx->{next_msg}) {
680 $n = PublicInbox::Hval->new_msgid($n)->{href};
681 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
684 my $par = $ctx->{parent_msg};
686 $u = PublicInbox::Hval->new_msgid($par)->{href};
689 if (my $p = $ctx->{prev_msg}) {
690 $prev = PublicInbox::Hval->new_msgid($p)->{href};
691 if ($p && $par && $p eq $par) {
692 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
693 'rel=prev>prev parent</a>';
696 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
698 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
700 } elsif ($u) { # unlikely
701 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
703 $irt = "$next $prev$parent ";
708 $irt .= qq(<a\nhref="$rhref">reply</a>);
712 sub linkify_ref_nosrch {
713 my $v = PublicInbox::Hval->new_msgid($_[0]);
714 my $html = $v->as_html;
715 my $href = $v->{href};
716 "<<a\nhref=\"../$href/\">$html</a>>";
721 'm' . id_compress($msgid, 1);
725 my ($upfx, $mid) = @_;
726 # 'subject dummy' is used internally by Mail::Thread
727 return '[no common parent]' if ($mid eq 'subject dummy');
729 $mid = PublicInbox::Hval->new_msgid($mid);
730 my $href = $mid->{href};
731 my $html = $mid->as_html;
732 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
737 INDENT x ($level - 1);
743 [ map { $_->mini_mime } @{delete $sres->{msgs}} ];
748 my $ts = eval { str2time($hdr->header('Date')) };
749 defined($ts) ? $ts : 0;
754 require PublicInbox::Thread;
755 my $th = PublicInbox::Thread->new(@$msgs);
757 $th->order(*sort_ts);
763 require PublicInbox::ExtMsg;
764 PublicInbox::ExtMsg::ext_msg($ctx);
769 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
773 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
776 my ($ctx, $hdr, $level) = @_;
778 my $dst = $ctx->{dst};
779 my $cur = $ctx->{cur};
780 my $mid = mid_clean($hdr->header_raw('Message-ID'));
781 my $f = ascii_html($hdr->header('X-PI-From'));
782 my $d = _msg_date($hdr) . ' ' . indent_for($level) . th_pfx($level);
784 $ctx->{first_level} ||= $level;
786 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
787 $ctx->{prev_attr} = $attr;
789 $ctx->{prev_level} = $level;
794 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
795 "$attr [this message]</a></b>\n";
798 $ctx->{prev_msg} = $mid;
801 $ctx->{next_msg} ||= $mid;
804 # Subject is never undef, this mail was loaded from
805 # our Xapian which would've resulted in '' if it were
806 # really missing (and Filter rejects empty subjects)
807 my $s = $hdr->header('Subject');
808 my $h = $ctx->{srch}->subject_path($s);
809 if ($ctx->{seen}->{$h}) {
812 $ctx->{seen}->{$h} = 1;
813 $s = PublicInbox::Hval->new($s);
818 my $mapping = $ctx->{mapping};
819 my $end = defined($s) ? "$s</a> $f\n" : "$f</a>\n";
821 my $map = $mapping->{$mid};
822 $id = id_compress($mid, 1);
824 $map->[1] = "$d<a\nhref=\"$m\">$end";
827 $m = $ctx->{-upfx}.mid_escape($mid).'/';
829 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
833 my ($ctx, $level, $node) = @_;
834 if (my $mime = $node->message) {
835 _skel_header($ctx, $mime->header_obj, $level);
837 my $mid = $node->messageid;
838 my $dst = $ctx->{dst};
839 my $mapping = $ctx->{mapping};
840 my $map = $mapping->{$mid} if $mapping;
841 if ($mid eq 'subject dummy') {
842 my $ncp = "\t[no common parent]\n";
843 $map->[1] = $ncp if $map;
847 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
849 $d .= indent_for($level) . th_pfx($level);
850 my $upfx = $ctx->{-upfx};
851 my $m = PublicInbox::Hval->new_msgid($mid);
852 my $href = $upfx . $m->{href} . '/';
853 my $html = $m->as_html;
856 my $id = id_compress($mid, 1);
857 $map->[1] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
858 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
860 $d .= qq{<<a\nhref="$href">$html</a>>\n};
868 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
869 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
873 sub _tryload_ghost ($$) {
874 my ($srch, $mid) = @_;
875 my $smsg = $srch->lookup_mail($mid) or return;
879 # accumulate recent topics if search is supported
880 # returns 200 if done, 404 if not
882 my ($ctx, $level, $node) = @_;
883 my $srch = $ctx->{srch};
884 my $mid = $node->messageid;
885 my $x = $node->message || _tryload_ghost($srch, $mid);
890 $subj = $x->header('Subject') || '';
891 $subj = $srch->subject_normalized($subj);
892 $ts = $x->header('X-PI-TS');
894 $topic = [ $ts, 1, { $subj => $mid }, $subj ];
895 $ctx->{-cur_topic} = $topic;
896 push @{$ctx->{order}}, $topic;
900 $topic = $ctx->{-cur_topic}; # should never be undef
901 $topic->[0] = $ts if $ts > $topic->[0];
903 my $seen = $topic->[2];
904 if (scalar(@$topic) == 3) { # parent was a ghost
906 } elsif (!$seen->{$subj}) {
907 push @$topic, $level, $subj;
909 $seen->{$subj} = $mid; # latest for subject
910 } else { # ghost message
911 return if $level != 0; # ignore child ghosts
912 $topic = [ -666, 0, {} ];
913 $ctx->{-cur_topic} = $topic;
914 push @{$ctx->{order}}, $topic;
920 my $order = delete $ctx->{order}; # [ ts, subj1, subj2, subj3, ... ]
922 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
928 # sort by recency, this allows new posts to "bump" old topics...
929 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
930 my ($ts, $n, $seen, $top, @ex) = @$topic;
932 next unless defined $top; # ghost topic
933 my $mid = delete $seen->{$top};
934 my $href = mid_escape($mid);
935 $top = PublicInbox::Hval->new($top)->as_html;
938 # $n isn't the total number of posts on the topic,
939 # just the number of posts in the current results window
943 $anchor = '#u'; # top of only message
945 $n = " ($n+ messages)";
946 $anchor = '#t'; # thread skeleton
949 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
950 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
951 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
952 " $ts UTC $n - $mbox / $atom\n";
953 for (my $i = 0; $i < scalar(@ex); $i += 2) {
955 my $sub = $ex[$i + 1];
956 $mid = delete $seen->{$sub};
957 $sub = PublicInbox::Hval->new($sub)->as_html;
958 $href = mid_escape($mid);
959 $s .= indent_for($level) . TCHILD;
960 $s .= "<a\nhref=\"$href/T/#u\">$sub</a>\n";
964 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
968 sub index_nav { # callback for WwwStream
969 my (undef, $ctx) = @_;
970 delete $ctx->{qp} or return;
975 my $next_o = $ctx->{-next_o};
977 $next = qq!<a\nhref="?o=$next_o"\nrel=next>next</a>!;
979 if (my $cur_o = $ctx->{-cur_o}) {
980 $latest = qq! <a\nhref=.>latest</a>!;
982 my $o = $cur_o - ($next_o - $cur_o);
984 $prev = qq!<a\nhref="?o=$o"\nrel=prev>prev</a>!;
986 $prev = qq!<a\nhref=.\nrel=prev>prev</a>!;
989 "<hr><pre>page: $next $prev$latest</pre>";
994 my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/);
995 my $opts = { offset => $off, limit => 200 };
998 my $sres = $ctx->{srch}->query('', $opts);
999 my $nr = scalar @{$sres->{msgs}};
1001 $sres = load_results($sres);
1002 walk_thread(thread_results($sres), $ctx, *acc_topic);
1004 $ctx->{-next_o} = $off+ $nr;
1005 $ctx->{-cur_o} = $off;
1006 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
1009 sub thread_adj_level {
1010 my ($ctx, $level) = @_;
1012 my $max = $ctx->{cur_level};
1014 return ('', '') if $max == 0; # flat output
1016 # reset existing lists
1017 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
1018 $ctx->{cur_level} = 0;
1020 } elsif ($level == $max) { # continue existing list
1022 } elsif ($level < $max) {
1023 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1024 $ctx->{cur_level} = $level;
1025 ("$beg<li>", '</li>');
1026 } else { # ($level > $max) # start a new level
1027 $ctx->{cur_level} = $level;
1028 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1033 sub ghost_index_entry {
1034 my ($ctx, $level, $mid) = @_;
1035 my ($beg, $end) = thread_adj_level($ctx, $level);
1036 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $mid) . '</pre>' . $end;