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 obfuscate_addrs/;
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 $obfs = $ctx->{-inbox}->{obfuscate};
28 my $tip = _msg_html_prepare($hdr, $ctx, $obfs);
29 PublicInbox::WwwStream->response($ctx, 200, sub {
32 $tip . multipart_text_as_html($mime, '', $obfs) .
35 # fake an EOF if generating the footer fails;
36 # we want to at least show the message if something
39 '<pre>' . html_footer($hdr, 1, $ctx) .
40 '</pre>' . msg_reply($ctx, $hdr)
48 # /$INBOX/$MESSAGE_ID/#R
52 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
54 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
57 my $ibx = $ctx->{-inbox};
58 if (my $url = $ibx->{infourl}) {
59 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
60 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
63 my ($arg, $link) = PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
65 # mailto: link only works if address obfuscation is disabled
69 * If your mail client supports setting the <b>In-Reply-To</b> header
70 via mailto: links, try the <a
71 href="$link">mailto: link</a></pre>
74 push @$arg, '/path/to/YOUR_REPLY';
75 $arg = ascii_html(join(" \\\n ", '', @$arg));
78 id=R><b>Reply instructions:</b>
80 You may reply publically to <a
81 href=#t>this message</a> via plain-text email
82 using any one of the following methods:
84 * Save the following mbox file, import it into your mail client,
85 and reply-to-all from there: <a
88 Avoid top-posting and favor interleaved quoting:
90 href="$p_url">$p_url</a>
92 * Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,
93 and <b>--in-reply-to</b> switches of git-send-email(1):
98 href="$se_url">$se_url</a>
105 my %mid = map { $_ => 1 } $hdr->header_raw('Message-ID');
106 my @refs = ($hdr->header_raw('References'),
107 $hdr->header_raw('In-Reply-To'));
108 @refs = ((join(' ', @refs)) =~ /<([^>]+)>/g);
109 while (defined(my $irt = pop @refs)) {
110 next if $mid{"<$irt>"};
116 sub _hdr_names_html ($$) {
117 my ($hdr, $field) = @_;
118 my $val = $hdr->header($field) or return '';
119 ascii_html(join(', ', PublicInbox::Address::names($val)));
123 my ($nr, $singular, $plural) = @_;
124 return "0 $plural" if $nr == 0;
125 $nr == 1 ? "$nr $singular" : "$nr $plural";
128 # this is already inside a <pre>
130 my ($mime, $ctx, $more) = @_;
131 my $srch = $ctx->{srch};
132 my $hdr = $mime->header_obj;
133 my $subj = $hdr->header('Subject');
135 my $mid_raw = mid_clean(mid_mime($mime));
136 my $id = id_compress($mid_raw, 1);
139 my $root_anchor = $ctx->{root_anchor} || '';
141 my $obfs = $ctx->{-obfuscate};
143 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
144 $subj = '<b>'.ascii_html($subj).'</b>';
145 obfuscate_addrs($subj) if $obfs;
146 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
148 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
150 foreach my $f (qw(To Cc)) {
151 my $dst = _hdr_names_html($hdr, $f);
153 obfuscate_addrs($dst) if $obfs;
154 push @tocc, "$f: $dst";
157 my $from = _hdr_names_html($hdr, 'From');
158 obfuscate_addrs($from) if $obfs;
159 $rv .= "From: $from @ "._msg_date($hdr)." UTC";
160 my $upfx = $ctx->{-upfx};
161 my $mhref = $upfx . mid_escape($mid_raw) . '/';
162 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
163 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
164 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
166 my $mapping = $ctx->{mapping};
167 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
168 my $mirt = PublicInbox::Hval->new_msgid($irt);
169 my $href = $upfx . $mirt->{href}. '/';
170 my $html = $mirt->as_html;
171 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
175 # scan through all parts, looking for displayable text
176 msg_iter($mime, sub { $rv .= add_text_body($mhref, $obfs, $_[0]) });
179 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
180 "<a\nhref=\"$mhref\">permalink</a>" .
181 " <a\nhref=\"${mhref}raw\">raw</a>" .
182 " <a\nhref=\"${mhref}#R\">reply</a>";
185 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
186 $rv .= "\t[relevance $pct->{$mid_raw}%]";
189 my $threaded = 'threaded';
194 $flat = "<b>$flat</b>";
196 $threaded = "<b>$threaded</b>";
198 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
199 $rv .= "|<a\nhref=\"${mhref}t/#u\">$threaded</a>]";
200 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
205 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
209 sub pad_link ($$;$) {
210 my ($mid, $level, $s) = @_;
212 my $id = id_compress($mid, 1);
213 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
217 my ($mid_raw, $irt, $id, $ctx) = @_;
219 my $mapping = $ctx->{mapping} or return $rv;
221 my $mid_map = $mapping->{$mid_raw};
223 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
224 my ($attr, $node, $idx, $level) = @$mid_map;
225 my $children = $node->{children};
226 my $nr_c = scalar @$children;
229 if (my $smsg = $node->{smsg}) {
230 ($$irt) = (($smsg->{references} || '') =~ m/<([^>]+)>\z/);
232 my $irt_map = $mapping->{$$irt} if defined $$irt;
233 if (defined $irt_map) {
234 $siblings = $irt_map->[1]->{children};
235 $nr_s = scalar(@$siblings) - 1;
236 $rv .= $pad . $irt_map->[0];
238 my $prev = $siblings->[$idx - 1];
239 my $pmid = $prev->{id};
241 my $s = ($idx - 1). ' preceding siblings ...';
242 $rv .= pad_link($pmid, $level, $s);
243 } elsif ($idx == 2) {
244 my $ppmid = $siblings->[0]->{id};
245 $rv .= $pad . $mapping->{$ppmid}->[0];
247 $rv .= $pad . $mapping->{$pmid}->[0];
250 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
251 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
252 $attr =~ s!\n\z!</b>\n!s;
253 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
254 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
257 my $cmid = $children->[0]->{id};
258 $rv .= $pad . $mapping->{$cmid}->[0];
260 my $s = ($nr_c - 1). ' more replies';
261 $rv .= pad_link($cmid, $level + 1, $s);
262 } elsif (my $cn = $children->[1]) {
263 $rv .= $pad . $mapping->{$cn->{id}}->[0];
267 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
269 my $nmid = $next->{id};
270 $rv .= $pad . $mapping->{$nmid}->[0];
271 my $nnext = $nr_s - $idx;
273 my $s = ($nnext - 1).' subsequent siblings';
274 $rv .= pad_link($nmid, $level, $s);
275 } elsif (my $nn = $siblings->[$idx + 2]) {
276 $rv .= $pad . $mapping->{$nn->{id}}->[0];
279 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
283 my ($rootset, $ctx, $cb) = @_;
284 my @q = map { (0, $_, -1) } @$rootset;
286 my ($level, $node, $i) = splice(@q, 0, 3);
287 defined $node or next;
288 $cb->($ctx, $level, $node, $i);
291 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
296 my ($ctx, $level, $node, $idx) = @_;
297 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
298 skel_dump($ctx, $level, $node);
301 sub thread_index_entry {
302 my ($ctx, $level, $mime) = @_;
303 my ($beg, $end) = thread_adj_level($ctx, $level);
304 $beg . '<pre>' . index_entry($mime, $ctx, 0) . '</pre>' . $end;
307 sub stream_thread ($$) {
308 my ($rootset, $ctx) = @_;
309 my $inbox = $ctx->{-inbox};
311 my @q = map { (0, $_) } @$rootset;
315 my $node = shift @q or next;
317 unshift @q, map { ($cl, $_) } @{$node->{children}};
318 $mime = $inbox->msg_by_smsg($node->{smsg}) and last;
320 return missing_thread($ctx) unless $mime;
322 $ctx->{-obfuscate} = $ctx->{-inbox}->{obfuscate};
323 $mime = PublicInbox::MIME->new($mime);
324 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
325 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
326 PublicInbox::WwwStream->response($ctx, 200, sub {
330 my $node = shift @q or next;
332 unshift @q, map { ($cl, $_) } @{$node->{children}};
333 my $mid = $node->{id};
334 if ($mime = $inbox->msg_by_smsg($node->{smsg})) {
335 $mime = PublicInbox::MIME->new($mime);
336 return thread_index_entry($ctx, $level, $mime);
338 return ghost_index_entry($ctx, $level, $node);
341 my $ret = join('', thread_adj_level($ctx, 0));
342 $ret .= ${$ctx->{dst}}; # skel
350 my $mid = $ctx->{mid};
351 my $srch = $ctx->{srch};
352 my $sres = $srch->get_thread($mid);
353 my $msgs = load_results($srch, $sres);
354 my $nr = $sres->{total};
355 return missing_thread($ctx) if $nr == 0;
356 my $skel = '<hr><pre>';
357 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
358 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
359 $skel .= "<b\nid=t>Thread overview:</b> ";
360 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
361 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
362 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
363 $skel .= "-- links below jump to the message on this page --\n";
364 $ctx->{-upfx} = '../../';
365 $ctx->{cur_level} = 0;
366 $ctx->{dst} = \$skel;
367 $ctx->{prev_attr} = '';
368 $ctx->{prev_level} = 0;
369 $ctx->{root_anchor} = anchor_for($mid);
371 $ctx->{mapping} = {};
372 $ctx->{s_nr} = "$nr+ messages in thread";
374 my $rootset = thread_results($msgs);
376 # reduce hash lookups in pre_thread->skel_dump
377 $ctx->{-obfuscate} = $ctx->{-inbox}->{obfuscate};
378 walk_thread($rootset, $ctx, *pre_thread);
381 return stream_thread($rootset, $ctx) unless $ctx->{flat};
383 # flat display: lazy load the full message from smsg
384 my $inbox = $ctx->{-inbox};
386 while ($mime = shift @$msgs) {
387 $mime = $inbox->msg_by_smsg($mime) and last;
389 return missing_thread($ctx) unless $mime;
390 $mime = PublicInbox::MIME->new($mime);
391 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
392 $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
394 PublicInbox::WwwStream->response($ctx, 200, sub {
396 while ($mime = shift @$msgs) {
397 $mime = $inbox->msg_by_smsg($mime) and last;
400 $mime = PublicInbox::MIME->new($mime);
401 return index_entry($mime, $ctx, scalar @$msgs);
408 sub multipart_text_as_html {
409 my ($mime, $upfx, $obfs) = @_;
412 # scan through all parts, looking for displayable text
413 msg_iter($mime, sub { $rv .= add_text_body($upfx, $obfs, $_[0]) });
418 my ($s, $l, $quot) = @_;
420 # show everything in the full version with anchor from
421 # short version (see above)
422 my $rv = $l->linkify_1(join('', @$quot));
425 # we use a <span> here to allow users to specify their own
426 # color for quoted text
427 $rv = $l->linkify_2(ascii_html($rv));
428 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
431 sub attach_link ($$$$;$) {
432 my ($upfx, $ct, $p, $fn, $err) = @_;
433 my ($part, $depth, @idx) = @$p;
434 my $nl = $idx[-1] > 1 ? "\n" : '';
435 my $idx = join('.', @idx);
436 my $size = bytes::length($part->body);
438 # hide attributes normally, unless we want to aid users in
439 # spotting MUA problems:
440 $ct =~ s/;.*// unless $err;
441 $ct = ascii_html($ct);
442 my $desc = $part->header('Content-Description');
443 $desc = $fn unless defined $desc;
444 $desc = '' unless defined $desc;
446 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
448 } elsif ($ct eq 'text/plain') {
453 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
456 "[-- Warning: decoded text below may be mangled --]\n";
458 $ret .= "[-- Attachment #$idx: ";
459 my $ts = "Type: $ct, Size: $size bytes";
460 $desc = ascii_html($desc);
461 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
466 my ($upfx, $obfs, $p) = @_;
467 # $p - from msg_iter: [ Email::MIME, depth, @idx ]
468 my ($part, $depth) = @$p; # attachment @idx is unused
469 my $ct = $part->content_type || 'text/plain';
470 my $fn = $part->filename;
472 if ($ct =~ m!\btext/x?html\b!i) {
473 return attach_link($upfx, $ct, $p, $fn);
476 my $s = eval { $part->body_str };
478 # badly-encoded message? tell the world about it!
481 if ($ct =~ m!\btext/plain\b!i) {
482 # Try to assume UTF-8 because Alpine seems to
483 # do wacky things and set charset=X-UNKNOWN
484 $part->charset_set('UTF-8');
485 $s = eval { $part->body_str };
487 # If forcing charset=UTF-8 failed,
488 # attach_link will warn further down...
489 $s = $part->body if $@;
491 return attach_link($upfx, $ct, $p, $fn);
495 my @lines = split(/^/m, $s);
497 if (defined($fn) || $depth > 0 || $err) {
498 $s .= attach_link($upfx, $ct, $p, $fn, $err);
502 my $l = PublicInbox::Linkify->new;
503 foreach my $cur (@lines) {
505 # show the previously buffered quote inline
506 flush_quote(\$s, $l, \@quot) if @quot;
510 $s .= $l->linkify_2(ascii_html($cur));
516 if (@quot) { # ugh, top posted
517 flush_quote(\$s, $l, \@quot);
518 obfuscate_addrs($s) if $obfs;
521 obfuscate_addrs($s) if $obfs;
522 if ($s =~ /\n\z/s) { # common, last line ends with a newline
524 } else { # some editors don't do newlines...
530 sub _msg_html_prepare {
531 my ($hdr, $ctx, $obfs) = @_;
532 my $srch = $ctx->{srch} if $ctx;
534 my $rv = "<pre\nid=b>"; # anchor for body start
537 $ctx->{-upfx} = '../';
540 my $mid = mid_clean($hdr->header_raw('Message-ID'));
541 $mid = PublicInbox::Hval->new_msgid($mid);
542 foreach my $h (qw(From To Cc Subject Date)) {
543 my $v = $hdr->header($h);
544 defined($v) && ($v ne '') or next;
545 $v = PublicInbox::Hval->new($v);
548 my @n = PublicInbox::Address::names($v->raw);
549 $title[1] = ascii_html(join(', ', @n));
550 obfuscate_addrs($title[1]) if $obfs;
551 } elsif ($h eq 'Subject') {
552 $title[0] = $v->as_html;
554 $rv .= qq($h: <a\nhref="#r"\nid=t>);
555 $rv .= $v->as_html . "</a>\n";
560 obfuscate_addrs($v) if $obfs;
564 $title[0] ||= '(no subject)';
565 $ctx->{-title_html} = join(' - ', @title);
566 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
567 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
568 $rv .= _parent_headers($hdr, $srch);
573 my ($dst, $ctx, $hdr, $tpfx) = @_;
574 my $srch = $ctx->{srch};
575 my $mid = mid_clean($hdr->header_raw('Message-ID'));
576 my $sres = $srch->get_thread($mid);
577 my $nr = $sres->{total};
578 my $expand = qq(<a\nhref="${tpfx}T/#u">expand</a> ) .
579 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
580 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
582 my $parent = in_reply_to($hdr);
583 $$dst .= "\n<b>Thread overview: </b>";
585 if (defined $parent) {
586 $$dst .= "($expand)\n ";
587 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
589 $$dst .= "[no followups, yet] ($expand)\n";
591 $ctx->{next_msg} = undef;
592 $ctx->{parent_msg} = $parent;
596 $$dst .= "$nr+ messages in thread ($expand";
597 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
599 my $subj = $srch->subject_path($hdr->header('Subject'));
600 $ctx->{seen} = { $subj => 1 };
602 $ctx->{prev_attr} = '';
603 $ctx->{prev_level} = 0;
605 $sres = load_results($srch, $sres);
607 # reduce hash lookups in skel_dump
608 $ctx->{-obfuscate} = $ctx->{-inbox}->{obfuscate};
609 walk_thread(thread_results($sres), $ctx, *skel_dump);
611 $ctx->{parent_msg} = $parent;
614 sub _parent_headers {
615 my ($hdr, $srch) = @_;
618 my $irt = in_reply_to($hdr);
620 my $v = PublicInbox::Hval->new_msgid($irt);
621 my $html = $v->as_html;
622 my $href = $v->{href};
623 $rv .= "In-Reply-To: <";
624 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
627 # do not display References: if search is present,
628 # we show the thread skeleton at the bottom, instead.
631 my $refs = $hdr->header_raw('References');
633 # avoid redundant URLs wasting bandwidth
635 $seen{$irt} = 1 if defined $irt;
637 my @raw_refs = ($refs =~ /<([^>]+)>/g);
638 foreach my $ref (@raw_refs) {
641 push @refs, linkify_ref_nosrch($ref);
645 $rv .= 'References: '. join("\n\t", @refs) . "\n";
652 my ($hdr, $standalone, $ctx, $rhref) = @_;
654 my $srch = $ctx->{srch} if $ctx;
657 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
661 thread_skel(\$idx, $ctx, $hdr, $tpfx);
666 if (my $n = $ctx->{next_msg}) {
667 $n = PublicInbox::Hval->new_msgid($n)->{href};
668 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
671 my $par = $ctx->{parent_msg};
673 $u = PublicInbox::Hval->new_msgid($par)->{href};
676 if (my $p = $ctx->{prev_msg}) {
677 $prev = PublicInbox::Hval->new_msgid($p)->{href};
678 if ($p && $par && $p eq $par) {
679 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
680 'rel=prev>prev parent</a>';
683 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
685 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
687 } elsif ($u) { # unlikely
688 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
690 $irt = "$next $prev$parent ";
695 $irt .= qq(<a\nhref="$rhref">reply</a>);
699 sub linkify_ref_nosrch {
700 my $v = PublicInbox::Hval->new_msgid($_[0]);
701 my $html = $v->as_html;
702 my $href = $v->{href};
703 "<<a\nhref=\"../$href/\">$html</a>>";
708 'm' . id_compress($msgid, 1);
712 my ($upfx, $mid) = @_;
714 $mid = PublicInbox::Hval->new_msgid($mid);
715 my $href = $mid->{href};
716 my $html = $mid->as_html;
717 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
722 INDENT x ($level - 1);
726 my ($srch, $sres) = @_;
727 my $msgs = delete $sres->{msgs};
728 $srch->retry_reopen(sub { [ map { $_->mid; $_ } @$msgs ] });
733 my $ts = eval { str2time($hdr->header('Date')) };
734 defined($ts) ? $ts : 0;
739 require PublicInbox::SearchThread;
740 PublicInbox::SearchThread::thread($msgs, *sort_ts);
745 require PublicInbox::ExtMsg;
746 PublicInbox::ExtMsg::ext_msg($ctx);
751 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
755 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
758 my ($ctx, $level, $node) = @_;
759 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
761 my $dst = $ctx->{dst};
762 my $cur = $ctx->{cur};
763 my $mid = $smsg->{mid};
765 my $f = ascii_html($smsg->from_name);
766 my $obfs = $ctx->{-obfuscate};
767 obfuscate_addrs($f) if $obfs;
769 my $d = fmt_ts($smsg->{ts}) . ' ' . indent_for($level) . th_pfx($level);
771 $ctx->{first_level} ||= $level;
773 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
774 $ctx->{prev_attr} = $attr;
776 $ctx->{prev_level} = $level;
781 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
782 "$attr [this message]</a></b>\n";
785 $ctx->{prev_msg} = $mid;
788 $ctx->{next_msg} ||= $mid;
791 # Subject is never undef, this mail was loaded from
792 # our Xapian which would've resulted in '' if it were
793 # really missing (and Filter rejects empty subjects)
794 my $subj = $smsg->subject;
795 my $h = $ctx->{srch}->subject_path($subj);
796 if ($ctx->{seen}->{$h}) {
799 $ctx->{seen}->{$h} = 1;
800 $subj = PublicInbox::Hval->new($subj);
801 $subj = $subj->as_html;
802 obfuscate_addrs($subj) if $obfs;
806 my $mapping = $ctx->{mapping};
807 my $end = defined($subj) ? "$subj</a> $f\n" : "$f</a>\n";
809 my $map = $mapping->{$mid};
810 $id = id_compress($mid, 1);
812 $map->[0] = "$d<a\nhref=\"$m\">$end";
815 $m = $ctx->{-upfx}.mid_escape($mid).'/';
817 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
821 my ($ctx, $level, $node) = @_;
823 my $mid = $node->{id};
824 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
826 $d .= indent_for($level) . th_pfx($level);
827 my $upfx = $ctx->{-upfx};
828 my $m = PublicInbox::Hval->new_msgid($mid);
829 my $href = $upfx . $m->{href} . '/';
830 my $html = $m->as_html;
832 my $mapping = $ctx->{mapping};
833 my $map = $mapping->{$mid} if $mapping;
835 my $id = id_compress($mid, 1);
836 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
837 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
839 $d .= qq{<<a\nhref="$href">$html</a>>\n};
841 my $dst = $ctx->{dst};
847 (eval { $a->topmost->{smsg}->ts } || 0) <=>
848 (eval { $b->topmost->{smsg}->ts } || 0)
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->{id};
858 my $x = $node->{smsg} || $srch->lookup_mail($mid);
863 $subj = $srch->subject_normalized($subj);
866 $topic = [ $ts, 1, { $subj => $mid }, $subj ];
867 $ctx->{-cur_topic} = $topic;
868 push @{$ctx->{order}}, $topic;
872 $topic = $ctx->{-cur_topic}; # should never be undef
873 $topic->[0] = $ts if $ts > $topic->[0];
875 my $seen = $topic->[2];
876 if (scalar(@$topic) == 3) { # parent was a ghost
878 } elsif (!$seen->{$subj}) {
879 push @$topic, $level, $subj;
881 $seen->{$subj} = $mid; # latest for subject
882 } else { # ghost message
883 return if $level != 0; # ignore child ghosts
884 $topic = [ -666, 0, {} ];
885 $ctx->{-cur_topic} = $topic;
886 push @{$ctx->{order}}, $topic;
892 my $order = delete $ctx->{order}; # [ ts, subj1, subj2, subj3, ... ]
894 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
899 my $obfs = $ctx->{-inbox}->{obfuscate};
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 = mid_escape($mid);
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 $subj = $ex[$i + 1];
929 $mid = delete $seen->{$subj};
930 $subj = ascii_html($subj);
931 obfuscate_addrs($subj) if $obfs;
932 $href = mid_escape($mid);
933 $s .= indent_for($level) . TCHILD;
934 $s .= "<a\nhref=\"$href/T/#u\">$subj</a>\n";
938 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
942 sub index_nav { # callback for WwwStream
943 my (undef, $ctx) = @_;
944 delete $ctx->{qp} or return;
949 my $next_o = $ctx->{-next_o};
951 $next = qq!<a\nhref="?o=$next_o"\nrel=next>next</a>!;
953 if (my $cur_o = $ctx->{-cur_o}) {
954 $latest = qq! <a\nhref=.>latest</a>!;
956 my $o = $cur_o - ($next_o - $cur_o);
958 $prev = qq!<a\nhref="?o=$o"\nrel=prev>prev</a>!;
960 $prev = qq!<a\nhref=.\nrel=prev>prev</a>!;
963 "<hr><pre>page: $next $prev$latest</pre>";
968 my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/);
969 my $opts = { offset => $off, limit => 200 };
972 my $srch = $ctx->{srch};
973 my $sres = $srch->query('', $opts);
974 my $nr = scalar @{$sres->{msgs}};
976 $sres = load_results($srch, $sres);
977 walk_thread(thread_results($sres), $ctx, *acc_topic);
979 $ctx->{-next_o} = $off+ $nr;
980 $ctx->{-cur_o} = $off;
981 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
984 sub thread_adj_level {
985 my ($ctx, $level) = @_;
987 my $max = $ctx->{cur_level};
989 return ('', '') if $max == 0; # flat output
991 # reset existing lists
992 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
993 $ctx->{cur_level} = 0;
995 } elsif ($level == $max) { # continue existing list
997 } elsif ($level < $max) {
998 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
999 $ctx->{cur_level} = $level;
1000 ("$beg<li>", '</li>');
1001 } else { # ($level > $max) # start a new level
1002 $ctx->{cur_level} = $level;
1003 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1008 sub ghost_index_entry {
1009 my ($ctx, $level, $node) = @_;
1010 my ($beg, $end) = thread_adj_level($ctx, $level);
1011 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})