1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <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 PublicInbox::MsgTime qw(msg_datestamp);
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 mids
14 use PublicInbox::MsgIter;
15 use PublicInbox::Address;
16 use PublicInbox::WwwStream;
17 use PublicInbox::Reply;
20 use constant INDENT => ' ';
21 use constant TCHILD => '` ';
22 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
24 # public functions: (unstable)
27 my ($ctx, $mime, $more) = @_;
28 my $hdr = $mime->header_obj;
29 my $ibx = $ctx->{-inbox};
30 my $obfs_ibx = $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
31 my $tip = _msg_html_prepare($hdr, $ctx, $more, 0);
33 PublicInbox::WwwStream->response($ctx, 200, sub {
36 $tip . multipart_text_as_html($mime, '', $obfs_ibx) .
38 } elsif ($more && @$more) {
40 msg_html_more($ctx, $more, $nr);
41 } elsif ($nr == $end) {
42 # fake an EOF if generating the footer fails;
43 # we want to at least show the message if something
46 '<pre>' . html_footer($hdr, 1, $ctx) .
47 '</pre>' . msg_reply($ctx, $hdr)
57 my $mid = $ctx->{mid};
58 my $ibx = $ctx->{-inbox};
59 my ($first, $more, $head, $tail, $db);
60 if (my $srch = $ibx->search) {
61 $srch->retry_reopen(sub {
62 ($head, $tail, $db) = $srch->each_smsg_by_mid($mid);
63 for (; !defined($first) && $head != $tail; $head++) {
64 my @args = ($head, $db, $mid);
65 my $smsg = PublicInbox::SearchMsg->get(@args);
66 $first = $ibx->msg_by_smsg($smsg);
69 $more = [ $head, $tail, $db ];
74 $first = $ibx->msg_by_mid($mid) or return;
76 msg_html($ctx, PublicInbox::MIME->new($first), $more);
80 my ($ctx, $more, $nr) = @_;
83 my ($head, $tail, $db) = @$more;
84 my $mid = $ctx->{mid};
85 for (; !defined($smsg) && $head != $tail; $head++) {
86 my $m = PublicInbox::SearchMsg->get($head, $db, $mid);
87 $smsg = $ctx->{-inbox}->smsg_mime($m);
89 if ($head == $tail) { # done
95 my $mime = $smsg->{mime};
96 _msg_html_prepare($mime->header_obj, $ctx, $more, $nr) .
97 multipart_text_as_html($mime, '',
105 warn "Error lookup up additional messages: $@\n";
106 $str = '<pre>Error looking up additional messages</pre>';
111 # /$INBOX/$MESSAGE_ID/#R
113 my ($ctx, $hdr) = @_;
115 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
117 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
120 my $ibx = $ctx->{-inbox};
121 if (my $url = $ibx->{infourl}) {
122 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
123 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
126 my ($arg, $link, $reply_to_all) =
127 PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
129 # mailto: link only works if address obfuscation is disabled
133 * If your mail client supports setting the <b>In-Reply-To</b> header
134 via mailto: links, try the <a
135 href="$link">mailto: link</a>
139 push @$arg, '/path/to/YOUR_REPLY';
140 $arg = ascii_html(join(" \\\n ", '', @$arg));
143 id=R><b>Reply instructions:</b>
145 You may reply publically to <a
146 href=#t>this message</a> via plain-text email
147 using any one of the following methods:
149 * Save the following mbox file, import it into your mail client,
150 and $reply_to_all from there: <a
153 Avoid top-posting and favor interleaved quoting:
155 href="$p_url">$p_url</a>
157 * Reply using the <b>--to</b>, <b>--cc</b>, and <b>--in-reply-to</b>
158 switches of git-send-email(1):
163 href="$se_url">$se_url</a>
170 my $refs = references($hdr);
174 sub _hdr_names_html ($$) {
175 my ($hdr, $field) = @_;
176 my $val = $hdr->header($field) or return '';
177 ascii_html(join(', ', PublicInbox::Address::names($val)));
181 my ($nr, $singular, $plural) = @_;
182 return "0 $plural" if $nr == 0;
183 $nr == 1 ? "$nr $singular" : "$nr $plural";
186 # this is already inside a <pre>
188 my ($smsg, $ctx, $more) = @_;
189 my $srch = $ctx->{srch};
190 my $subj = $smsg->subject;
191 my $mid_raw = $smsg->mid;
192 my $id = id_compress($mid_raw, 1);
195 my $root_anchor = $ctx->{root_anchor} || '';
197 my $obfs_ibx = $ctx->{-obfs_ibx};
199 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
200 $subj = '<b>'.ascii_html($subj).'</b>';
201 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
202 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
204 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
206 my $mime = $smsg->{mime};
207 my $hdr = $mime->header_obj;
208 foreach my $f (qw(To Cc)) {
209 my $dst = _hdr_names_html($hdr, $f);
211 obfuscate_addrs($obfs_ibx, $dst) if $obfs_ibx;
212 push @tocc, "$f: $dst";
215 my $from = _hdr_names_html($hdr, 'From');
216 obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
217 $rv .= "From: $from @ ".fmt_ts($smsg->ds)." UTC";
218 my $upfx = $ctx->{-upfx};
219 my $mhref = $upfx . mid_escape($mid_raw) . '/';
220 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
221 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
222 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
224 my $mapping = $ctx->{mapping};
225 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
226 my $mirt = PublicInbox::Hval->new_msgid($irt);
227 my $href = $upfx . $mirt->{href}. '/';
228 my $html = $mirt->as_html;
229 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
233 # scan through all parts, looking for displayable text
234 msg_iter($mime, sub { $rv .= add_text_body($mhref, $obfs_ibx, $_[0]) });
237 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
238 "<a\nhref=\"$mhref\">permalink</a>" .
239 " <a\nhref=\"${mhref}raw\">raw</a>" .
240 " <a\nhref=\"${mhref}#R\">reply</a>";
243 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
244 $rv .= "\t[relevance $pct->{$mid_raw}%]";
247 my $nested = 'nested';
252 $flat = "<b>$flat</b>";
254 $nested = "<b>$nested</b>";
256 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
257 $rv .= "|<a\nhref=\"${mhref}t/#u\">$nested</a>]";
258 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
263 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
267 sub pad_link ($$;$) {
268 my ($mid, $level, $s) = @_;
270 my $id = id_compress($mid, 1);
271 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
275 my ($mid_raw, $irt, $id, $ctx) = @_;
277 my $mapping = $ctx->{mapping} or return $rv;
279 my $mid_map = $mapping->{$mid_raw};
281 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
282 my ($attr, $node, $idx, $level) = @$mid_map;
283 my $children = $node->{children};
284 my $nr_c = scalar @$children;
287 if (my $smsg = $node->{smsg}) {
288 ($$irt) = (($smsg->{references} || '') =~ m/<([^>]+)>\z/);
290 my $irt_map = $mapping->{$$irt} if defined $$irt;
291 if (defined $irt_map) {
292 $siblings = $irt_map->[1]->{children};
293 $nr_s = scalar(@$siblings) - 1;
294 $rv .= $pad . $irt_map->[0];
296 my $prev = $siblings->[$idx - 1];
297 my $pmid = $prev->{id};
299 my $s = ($idx - 1). ' preceding siblings ...';
300 $rv .= pad_link($pmid, $level, $s);
301 } elsif ($idx == 2) {
302 my $ppmid = $siblings->[0]->{id};
303 $rv .= $pad . $mapping->{$ppmid}->[0];
305 $rv .= $pad . $mapping->{$pmid}->[0];
308 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
309 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
310 $attr =~ s!\n\z!</b>\n!s;
311 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
312 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
315 my $cmid = $children->[0]->{id};
316 $rv .= $pad . $mapping->{$cmid}->[0];
318 my $s = ($nr_c - 1). ' more replies';
319 $rv .= pad_link($cmid, $level + 1, $s);
320 } elsif (my $cn = $children->[1]) {
321 $rv .= $pad . $mapping->{$cn->{id}}->[0];
325 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
327 my $nmid = $next->{id};
328 $rv .= $pad . $mapping->{$nmid}->[0];
329 my $nnext = $nr_s - $idx;
331 my $s = ($nnext - 1).' subsequent siblings';
332 $rv .= pad_link($nmid, $level, $s);
333 } elsif (my $nn = $siblings->[$idx + 2]) {
334 $rv .= $pad . $mapping->{$nn->{id}}->[0];
337 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
341 my ($rootset, $ctx, $cb) = @_;
342 my @q = map { (0, $_, -1) } @$rootset;
344 my ($level, $node, $i) = splice(@q, 0, 3);
345 defined $node or next;
346 $cb->($ctx, $level, $node, $i);
349 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
354 my ($ctx, $level, $node, $idx) = @_;
355 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
356 skel_dump($ctx, $level, $node);
359 sub thread_index_entry {
360 my ($ctx, $level, $smsg) = @_;
361 my ($beg, $end) = thread_adj_level($ctx, $level);
362 $beg . '<pre>' . index_entry($smsg, $ctx, 0) . '</pre>' . $end;
365 sub stream_thread ($$) {
366 my ($rootset, $ctx) = @_;
367 my $inbox = $ctx->{-inbox};
368 my @q = map { (0, $_) } @$rootset;
373 my $node = shift @q or next;
375 unshift @q, map { ($cl, $_) } @{$node->{children}};
376 $smsg = $inbox->smsg_mime($node->{smsg}) and last;
378 return missing_thread($ctx) unless $smsg;
380 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
381 $ctx->{-title_html} = ascii_html($smsg->subject);
382 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg);
384 PublicInbox::WwwStream->response($ctx, 200, sub {
388 my $node = shift @q or next;
390 unshift @q, map { ($cl, $_) } @{$node->{children}};
391 if ($smsg = $inbox->smsg_mime($node->{smsg})) {
392 return thread_index_entry($ctx, $level, $smsg);
394 return ghost_index_entry($ctx, $level, $node);
397 my $ret = join('', thread_adj_level($ctx, 0));
398 $ret .= ${$ctx->{dst}}; # skel
406 my $mid = $ctx->{mid};
407 my $srch = $ctx->{srch};
408 my $sres = $srch->get_thread($mid);
409 my $msgs = load_results($srch, $sres);
410 my $nr = $sres->{total};
411 return missing_thread($ctx) if $nr == 0;
412 my $skel = '<hr><pre>';
413 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
414 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
415 $skel .= "<b\nid=t>Thread overview:</b> ";
416 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
417 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
418 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
419 $skel .= "-- links below jump to the message on this page --\n";
420 $ctx->{-upfx} = '../../';
421 $ctx->{cur_level} = 0;
422 $ctx->{dst} = \$skel;
423 $ctx->{prev_attr} = '';
424 $ctx->{prev_level} = 0;
425 $ctx->{root_anchor} = anchor_for($mid);
426 $ctx->{mapping} = {};
427 $ctx->{s_nr} = "$nr+ messages in thread";
429 my $rootset = thread_results($msgs, $srch);
431 # reduce hash lookups in pre_thread->skel_dump
432 my $inbox = $ctx->{-inbox};
433 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
434 walk_thread($rootset, $ctx, *pre_thread);
437 return stream_thread($rootset, $ctx) unless $ctx->{flat};
439 # flat display: lazy load the full message from smsg
441 while (my $m = shift @$msgs) {
442 $smsg = $inbox->smsg_mime($m) and last;
444 return missing_thread($ctx) unless $smsg;
445 $ctx->{-title_html} = ascii_html($smsg->subject);
446 $ctx->{-html_tip} = '<pre>'.index_entry($smsg, $ctx, scalar @$msgs);
448 PublicInbox::WwwStream->response($ctx, 200, sub {
451 while (my $m = shift @$msgs) {
452 $smsg = $inbox->smsg_mime($m) and last;
454 return index_entry($smsg, $ctx, scalar @$msgs) if $smsg;
460 sub multipart_text_as_html {
461 my ($mime, $upfx, $obfs_ibx) = @_;
464 # scan through all parts, looking for displayable text
465 msg_iter($mime, sub { $rv .= add_text_body($upfx, $obfs_ibx, $_[0]) });
470 my ($s, $l, $quot) = @_;
472 # show everything in the full version with anchor from
473 # short version (see above)
474 my $rv = $l->linkify_1(join('', @$quot));
477 # we use a <span> here to allow users to specify their own
478 # color for quoted text
479 $rv = $l->linkify_2(ascii_html($rv));
480 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
483 sub attach_link ($$$$;$) {
484 my ($upfx, $ct, $p, $fn, $err) = @_;
485 my ($part, $depth, @idx) = @$p;
486 my $nl = $idx[-1] > 1 ? "\n" : '';
487 my $idx = join('.', @idx);
488 my $size = bytes::length($part->body);
490 # hide attributes normally, unless we want to aid users in
491 # spotting MUA problems:
492 $ct =~ s/;.*// unless $err;
493 $ct = ascii_html($ct);
494 my $desc = $part->header('Content-Description');
495 $desc = $fn unless defined $desc;
496 $desc = '' unless defined $desc;
498 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
500 } elsif ($ct eq 'text/plain') {
505 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
508 "[-- Warning: decoded text below may be mangled --]\n";
510 $ret .= "[-- Attachment #$idx: ";
511 my $ts = "Type: $ct, Size: $size bytes";
512 $desc = ascii_html($desc);
513 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
518 my ($upfx, $obfs_ibx, $p) = @_;
519 # $p - from msg_iter: [ Email::MIME, depth, @idx ]
520 my ($part, $depth) = @$p; # attachment @idx is unused
521 my $ct = $part->content_type || 'text/plain';
522 my $fn = $part->filename;
524 if ($ct =~ m!\btext/x?html\b!i) {
525 return attach_link($upfx, $ct, $p, $fn);
528 my $s = eval { $part->body_str };
530 # badly-encoded message? tell the world about it!
533 if ($ct =~ m!\btext/plain\b!i) {
534 # Try to assume UTF-8 because Alpine seems to
535 # do wacky things and set charset=X-UNKNOWN
536 $part->charset_set('UTF-8');
537 $s = eval { $part->body_str };
539 # If forcing charset=UTF-8 failed,
540 # attach_link will warn further down...
541 $s = $part->body if $@;
543 return attach_link($upfx, $ct, $p, $fn);
547 my @lines = split(/^/m, $s);
549 if (defined($fn) || $depth > 0 || $err) {
550 $s .= attach_link($upfx, $ct, $p, $fn, $err);
554 my $l = PublicInbox::Linkify->new;
555 foreach my $cur (@lines) {
557 # show the previously buffered quote inline
558 flush_quote(\$s, $l, \@quot) if @quot;
562 $s .= $l->linkify_2(ascii_html($cur));
568 if (@quot) { # ugh, top posted
569 flush_quote(\$s, $l, \@quot);
570 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
573 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
574 if ($s =~ /\n\z/s) { # common, last line ends with a newline
576 } else { # some editors don't do newlines...
582 sub _msg_html_prepare {
583 my ($hdr, $ctx, $more, $nr) = @_;
584 my $srch = $ctx->{srch} if $ctx;
586 my $obfs_ibx = $ctx->{-obfs_ibx};
588 my $mids = mids($hdr);
589 my $multiple = scalar(@$mids) > 1; # zero, one, infinity
593 "<pre>WARNING: multiple messages refer to this Message-ID\n</pre>";
595 $rv .= "<pre\nid=b>"; # anchor for body start
600 $ctx->{-upfx} = '../';
603 foreach my $h (qw(From To Cc Subject Date)) {
604 my $v = $hdr->header($h);
605 defined($v) && ($v ne '') or next;
606 $v = PublicInbox::Hval->new($v);
609 my @n = PublicInbox::Address::names($v->raw);
610 $title[1] = ascii_html(join(', ', @n));
611 obfuscate_addrs($obfs_ibx, $title[1]) if $obfs_ibx;
612 } elsif ($h eq 'Subject') {
613 $title[0] = $v->as_html;
615 $rv .= qq($h: <a\nhref="#r"\nid=t>);
616 $rv .= $v->as_html . "</a>\n";
621 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
625 $title[0] ||= '(no subject)';
626 $ctx->{-title_html} = join(' - ', @title);
628 my $mid = PublicInbox::Hval->new_msgid($_) ;
629 my $mhtml = $mid->as_html;
631 my $href = $mid->{href};
632 $rv .= "Message-ID: ";
633 $rv .= "<a\nhref=\"../$href/\">";
634 $rv .= "<$mhtml></a> ";
635 $rv .= "(<a\nhref=\"../$href/raw\">raw</a>)\n";
637 $rv .= "Message-ID: <$mhtml> ";
638 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
641 $rv .= _parent_headers($hdr, $srch);
646 my ($dst, $ctx, $hdr, $tpfx) = @_;
647 my $srch = $ctx->{srch};
648 my $mid = mids($hdr)->[0];
649 my $sres = $srch->get_thread($mid);
650 my $nr = $sres->{total};
651 my $expand = qq(expand[<a\nhref="${tpfx}T/#u">flat</a>) .
652 qq(|<a\nhref="${tpfx}t/#u">nested</a>] ) .
653 qq(<a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
654 qq(<a\nhref="${tpfx}t.atom">Atom feed</a>);
656 my $parent = in_reply_to($hdr);
657 $$dst .= "\n<b>Thread overview: </b>";
659 if (defined $parent) {
660 $$dst .= "$expand\n ";
661 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
663 $$dst .= "[no followups] $expand\n";
665 $ctx->{next_msg} = undef;
666 $ctx->{parent_msg} = $parent;
670 $$dst .= "$nr+ messages / $expand";
671 $$dst .= qq! <a\nhref="#b">top</a>\n!;
673 my $subj = $hdr->header('Subject');
674 defined $subj or $subj = '';
675 $ctx->{prev_subj} = [ split(/ /, $srch->subject_normalized($subj)) ];
677 $ctx->{prev_attr} = '';
678 $ctx->{prev_level} = 0;
680 $sres = load_results($srch, $sres);
682 # reduce hash lookups in skel_dump
683 my $ibx = $ctx->{-inbox};
684 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
685 walk_thread(thread_results($sres, $srch), $ctx, *skel_dump);
687 $ctx->{parent_msg} = $parent;
690 sub _parent_headers {
691 my ($hdr, $srch) = @_;
694 my $irt = in_reply_to($hdr);
696 my $v = PublicInbox::Hval->new_msgid($irt);
697 my $html = $v->as_html;
698 my $href = $v->{href};
699 $rv .= "In-Reply-To: <";
700 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
703 # do not display References: if search is present,
704 # we show the thread skeleton at the bottom, instead.
707 my $refs = $hdr->header_raw('References');
709 # avoid redundant URLs wasting bandwidth
711 $seen{$irt} = 1 if defined $irt;
713 my @raw_refs = ($refs =~ /<([^>]+)>/g);
714 foreach my $ref (@raw_refs) {
717 push @refs, linkify_ref_nosrch($ref);
721 $rv .= 'References: '. join("\n\t", @refs) . "\n";
728 my ($hdr, $standalone, $ctx, $rhref) = @_;
730 my $srch = $ctx->{srch} if $ctx;
733 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
737 thread_skel(\$idx, $ctx, $hdr, $tpfx);
742 if (my $n = $ctx->{next_msg}) {
743 $n = PublicInbox::Hval->new_msgid($n)->{href};
744 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
747 my $par = $ctx->{parent_msg};
749 $u = PublicInbox::Hval->new_msgid($par)->{href};
752 if (my $p = $ctx->{prev_msg}) {
753 $prev = PublicInbox::Hval->new_msgid($p)->{href};
754 if ($p && $par && $p eq $par) {
755 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
756 'rel=prev>prev parent</a>';
759 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
761 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
763 } elsif ($u) { # unlikely
764 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
766 $irt = "$next $prev$parent ";
771 $irt .= qq(<a\nhref="$rhref">reply</a>);
775 sub linkify_ref_nosrch {
776 my $v = PublicInbox::Hval->new_msgid($_[0]);
777 my $html = $v->as_html;
778 my $href = $v->{href};
779 "<<a\nhref=\"../$href/\">$html</a>>";
784 'm' . id_compress($msgid, 1);
788 my ($upfx, $mid) = @_;
790 $mid = PublicInbox::Hval->new_msgid($mid);
791 my $href = $mid->{href};
792 my $html = $mid->as_html;
793 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
798 $level ? INDENT x ($level - 1) : '';
802 my ($srch, $sres) = @_;
803 my $msgs = delete $sres->{msgs};
804 $srch->retry_reopen(sub { [ map { $_->mid; $_ } @$msgs ] });
808 my ($msgs, $srch) = @_;
809 require PublicInbox::SearchThread;
810 PublicInbox::SearchThread::thread($msgs, *sort_ds, $srch);
815 require PublicInbox::ExtMsg;
816 PublicInbox::ExtMsg::ext_msg($ctx);
821 fmt_ts(msg_datestamp($hdr));
824 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
827 my ($prev_subj, $subj, $val) = @_;
829 my $omit = ''; # '"' denotes identical text omitted
830 my (@prev_pop, @curr_pop);
831 while (@$prev_subj && @$subj && $subj->[-1] eq $prev_subj->[-1]) {
832 push(@prev_pop, pop(@$prev_subj));
833 push(@curr_pop, pop(@$subj));
836 pop @$subj if @$subj && $subj->[-1] =~ /^re:\s*/i;
837 if (scalar(@curr_pop) == 1) {
839 push @$prev_subj, @prev_pop;
840 push @$subj, @curr_pop;
846 my ($ctx, $level, $node) = @_;
847 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
849 my $dst = $ctx->{dst};
850 my $cur = $ctx->{cur};
851 my $mid = $smsg->{mid};
853 my $f = ascii_html($smsg->from_name);
854 my $obfs_ibx = $ctx->{-obfs_ibx};
855 obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
857 my $d = fmt_ts($smsg->{ds}) . ' ' . indent_for($level) . th_pfx($level);
859 $ctx->{first_level} ||= $level;
861 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
862 $ctx->{prev_attr} = $attr;
864 $ctx->{prev_level} = $level;
869 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
870 "$attr [this message]</a></b>\n";
873 $ctx->{prev_msg} = $mid;
876 $ctx->{next_msg} ||= $mid;
879 # Subject is never undef, this mail was loaded from
880 # our Xapian which would've resulted in '' if it were
881 # really missing (and Filter rejects empty subjects)
882 my @subj = split(/ /, $ctx->{srch}->subject_normalized($smsg->subject));
884 # remove common suffixes from the subject if it matches the previous,
885 # so we do not show redundant text at the end.
886 my $prev_subj = $ctx->{prev_subj} || [];
887 $ctx->{prev_subj} = [ @subj ];
888 my $omit = dedupe_subject($prev_subj, \@subj, '" ');
891 my $subj = join(' ', @subj);
892 $subj = ascii_html($subj);
893 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
894 $end = "$subj</a> $omit$f\n"
900 my $mapping = $ctx->{mapping};
902 my $map = $mapping->{$mid};
903 $id = id_compress($mid, 1);
905 $map->[0] = "$d<a\nhref=\"$m\">$end";
908 $m = $ctx->{-upfx}.mid_escape($mid).'/';
910 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
914 my ($ctx, $level, $node) = @_;
916 my $mid = $node->{id};
917 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
919 $d .= indent_for($level) . th_pfx($level);
920 my $upfx = $ctx->{-upfx};
921 my $m = PublicInbox::Hval->new_msgid($mid);
922 my $href = $upfx . $m->{href} . '/';
923 my $html = $m->as_html;
925 my $mapping = $ctx->{mapping};
926 my $map = $mapping->{$mid} if $mapping;
928 my $id = id_compress($mid, 1);
929 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
930 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
932 $d .= qq{<<a\nhref="$href">$html</a>>\n};
934 my $dst = $ctx->{dst};
940 (eval { $a->topmost->{smsg}->ds } || 0) <=>
941 (eval { $b->topmost->{smsg}->ds } || 0)
945 # accumulate recent topics if search is supported
946 # returns 200 if done, 404 if not
948 my ($ctx, $level, $node) = @_;
949 my $srch = $ctx->{srch};
950 my $mid = $node->{id};
951 my $x = $node->{smsg} || $srch->lookup_mail($mid);
956 $subj = $srch->subject_normalized($subj);
959 $topic = [ $ds, 1, { $subj => $mid }, $subj ];
960 $ctx->{-cur_topic} = $topic;
961 push @{$ctx->{order}}, $topic;
965 $topic = $ctx->{-cur_topic}; # should never be undef
966 $topic->[0] = $ds if $ds > $topic->[0];
968 my $seen = $topic->[2];
969 if (scalar(@$topic) == 3) { # parent was a ghost
971 } elsif (!$seen->{$subj}) {
972 push @$topic, $level, $subj;
974 $seen->{$subj} = $mid; # latest for subject
975 } else { # ghost message
976 return if $level != 0; # ignore child ghosts
977 $topic = [ -666, 0, {} ];
978 $ctx->{-cur_topic} = $topic;
979 push @{$ctx->{order}}, $topic;
985 my $order = delete $ctx->{order}; # [ ds, subj1, subj2, subj3, ... ]
987 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
992 my $ibx = $ctx->{-inbox};
993 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
994 my $srch = $ctx->{srch};
996 # sort by recency, this allows new posts to "bump" old topics...
997 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
998 my ($ds, $n, $seen, $top, @ex) = @$topic;
1000 next unless defined $top; # ghost topic
1001 my $mid = delete $seen->{$top};
1002 my $href = mid_escape($mid);
1003 my $prev_subj = [ split(/ /, $top) ];
1004 $top = PublicInbox::Hval->new($top)->as_html;
1007 # $n isn't the total number of posts on the topic,
1008 # just the number of posts in the current results window
1012 $anchor = '#u'; # top of only message
1014 $n = " ($n+ messages)";
1015 $anchor = '#t'; # thread skeleton
1018 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
1019 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
1020 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
1021 " $ds UTC $n - $mbox / $atom\n";
1022 for (my $i = 0; $i < scalar(@ex); $i += 2) {
1023 my $level = $ex[$i];
1024 my $subj = $ex[$i + 1];
1025 $mid = delete $seen->{$subj};
1026 my @subj = split(/ /, $srch->subject_normalized($subj));
1027 my @next_prev = @subj; # full copy
1028 my $omit = dedupe_subject($prev_subj, \@subj, ' "');
1029 $prev_subj = \@next_prev;
1030 $subj = ascii_html(join(' ', @subj));
1031 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
1032 $href = mid_escape($mid);
1033 $s .= indent_for($level) . TCHILD;
1034 $s .= "<a\nhref=\"$href/T/#u\">$subj</a>$omit\n";
1038 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
1042 sub index_nav { # callback for WwwStream
1043 my (undef, $ctx) = @_;
1044 delete $ctx->{qp} or return;
1046 $next = $prev = ' ';
1049 my $next_o = $ctx->{-next_o};
1051 $next = qq!<a\nhref="?o=$next_o"\nrel=next>next</a>!;
1053 if (my $cur_o = $ctx->{-cur_o}) {
1054 $latest = qq! <a\nhref=.>latest</a>!;
1056 my $o = $cur_o - ($next_o - $cur_o);
1058 $prev = qq!<a\nhref="?o=$o"\nrel=prev>prev</a>!;
1060 $prev = qq!<a\nhref=.\nrel=prev>prev</a>!;
1063 "<hr><pre>page: $next $prev$latest</pre>";
1068 my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/);
1069 my $opts = { offset => $off, limit => 200 };
1072 my $srch = $ctx->{srch};
1073 my $sres = $srch->query('', $opts);
1074 my $nr = scalar @{$sres->{msgs}};
1076 $sres = load_results($srch, $sres);
1077 walk_thread(thread_results($sres, $srch), $ctx, *acc_topic);
1079 $ctx->{-next_o} = $off+ $nr;
1080 $ctx->{-cur_o} = $off;
1081 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
1084 sub thread_adj_level {
1085 my ($ctx, $level) = @_;
1087 my $max = $ctx->{cur_level};
1089 return ('', '') if $max == 0; # flat output
1091 # reset existing lists
1092 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
1093 $ctx->{cur_level} = 0;
1095 } elsif ($level == $max) { # continue existing list
1097 } elsif ($level < $max) {
1098 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1099 $ctx->{cur_level} = $level;
1100 ("$beg<li>", '</li>');
1101 } else { # ($level > $max) # start a new level
1102 $ctx->{cur_level} = $level;
1103 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1108 sub ghost_index_entry {
1109 my ($ctx, $level, $node) = @_;
1110 my ($beg, $end) = thread_adj_level($ctx, $level);
1111 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})