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/id_compress mid_escape mids references/;
13 use PublicInbox::MsgIter;
14 use PublicInbox::Address;
15 use PublicInbox::WwwStream;
16 use PublicInbox::Reply;
18 use Time::Local qw(timegm);
20 use constant INDENT => ' ';
21 use constant TCHILD => '` ';
22 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
24 # public functions: (unstable)
27 my ($ctx, $mime, $more, $smsg) = @_;
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 # $more cannot be true w/o $smsg being defined:
37 my $upfx = $more ? '../'.mid_escape($smsg->mid).'/' : '';
38 $tip . multipart_text_as_html($mime, $upfx, $obfs_ibx) .
40 } elsif ($more && @$more) {
42 msg_html_more($ctx, $more, $nr);
43 } elsif ($nr == $end) {
44 # fake an EOF if generating the footer fails;
45 # we want to at least show the message if something
48 '<pre>' . html_footer($hdr, 1, $ctx) .
49 '</pre>' . msg_reply($ctx, $hdr)
59 my $mid = $ctx->{mid};
60 my $ibx = $ctx->{-inbox};
61 my ($first, $more, $head, $tail, $db);
63 if (my $srch = $ibx->search) {
64 $srch->retry_reopen(sub {
65 ($head, $tail, $db) = $srch->each_smsg_by_mid($mid);
66 for (; !defined($first) && $head != $tail; $head++) {
67 my @args = ($head, $db, $mid);
68 $smsg = PublicInbox::SearchMsg->get(@args);
69 $first = $ibx->msg_by_smsg($smsg);
72 $more = [ $head, $tail, $db ];
77 $first = $ibx->msg_by_mid($mid) or return;
79 msg_html($ctx, PublicInbox::MIME->new($first), $more, $smsg);
83 my ($ctx, $more, $nr) = @_;
86 my ($head, $tail, $db) = @$more;
87 my $mid = $ctx->{mid};
88 for (; !defined($smsg) && $head != $tail; $head++) {
89 my $m = PublicInbox::SearchMsg->get($head, $db, $mid);
90 $smsg = $ctx->{-inbox}->smsg_mime($m);
92 if ($head == $tail) { # done
98 my $mime = $smsg->{mime};
99 my $upfx = '../' . mid_escape($smsg->mid) . '/';
100 _msg_html_prepare($mime->header_obj, $ctx, $more, $nr) .
101 multipart_text_as_html($mime, $upfx,
109 warn "Error lookup up additional messages: $@\n";
110 $str = '<pre>Error looking up additional messages</pre>';
115 # /$INBOX/$MESSAGE_ID/#R
117 my ($ctx, $hdr) = @_;
119 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
121 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
124 my $ibx = $ctx->{-inbox};
125 if (my $url = $ibx->{infourl}) {
126 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
127 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
130 my ($arg, $link, $reply_to_all) =
131 PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
133 # mailto: link only works if address obfuscation is disabled
137 * If your mail client supports setting the <b>In-Reply-To</b> header
138 via mailto: links, try the <a
139 href="$link">mailto: link</a>
143 push @$arg, '/path/to/YOUR_REPLY';
144 $arg = ascii_html(join(" \\\n ", '', @$arg));
147 id=R><b>Reply instructions:</b>
149 You may reply publically to <a
150 href=#t>this message</a> via plain-text email
151 using any one of the following methods:
153 * Save the following mbox file, import it into your mail client,
154 and $reply_to_all from there: <a
157 Avoid top-posting and favor interleaved quoting:
159 href="$p_url">$p_url</a>
161 * Reply using the <b>--to</b>, <b>--cc</b>, and <b>--in-reply-to</b>
162 switches of git-send-email(1):
167 href="$se_url">$se_url</a>
174 my $refs = references($hdr);
178 sub _hdr_names_html ($$) {
179 my ($hdr, $field) = @_;
180 my $val = $hdr->header($field) or return '';
181 ascii_html(join(', ', PublicInbox::Address::names($val)));
185 my ($nr, $singular, $plural) = @_;
186 return "0 $plural" if $nr == 0;
187 $nr == 1 ? "$nr $singular" : "$nr $plural";
190 # this is already inside a <pre>
192 my ($smsg, $ctx, $more) = @_;
193 my $srch = $ctx->{srch};
194 my $subj = $smsg->subject;
195 my $mid_raw = $smsg->mid;
196 my $id = id_compress($mid_raw, 1);
199 my $root_anchor = $ctx->{root_anchor} || '';
201 my $obfs_ibx = $ctx->{-obfs_ibx};
203 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
204 $subj = '<b>'.ascii_html($subj).'</b>';
205 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
206 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
208 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
210 my $mime = $smsg->{mime};
211 my $hdr = $mime->header_obj;
212 foreach my $f (qw(To Cc)) {
213 my $dst = _hdr_names_html($hdr, $f);
215 obfuscate_addrs($obfs_ibx, $dst) if $obfs_ibx;
216 push @tocc, "$f: $dst";
219 my $from = _hdr_names_html($hdr, 'From');
220 obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
221 $rv .= "From: $from @ ".fmt_ts($smsg->ds)." UTC";
222 my $upfx = $ctx->{-upfx};
223 my $mhref = $upfx . mid_escape($mid_raw) . '/';
224 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
225 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
226 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
228 my $mapping = $ctx->{mapping};
229 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
230 my $mirt = PublicInbox::Hval->new_msgid($irt);
231 my $href = $upfx . $mirt->{href}. '/';
232 my $html = $mirt->as_html;
233 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
237 # scan through all parts, looking for displayable text
238 msg_iter($mime, sub { $rv .= add_text_body($mhref, $obfs_ibx, $_[0]) });
241 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
242 "<a\nhref=\"$mhref\">permalink</a>" .
243 " <a\nhref=\"${mhref}raw\">raw</a>" .
244 " <a\nhref=\"${mhref}#R\">reply</a>";
247 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
248 $rv .= "\t[relevance $pct->{$mid_raw}%]";
251 my $nested = 'nested';
256 $flat = "<b>$flat</b>";
258 $nested = "<b>$nested</b>";
260 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
261 $rv .= "|<a\nhref=\"${mhref}t/#u\">$nested</a>]";
262 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
267 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
271 sub pad_link ($$;$) {
272 my ($mid, $level, $s) = @_;
274 my $id = id_compress($mid, 1);
275 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
279 my ($mid_raw, $irt, $id, $ctx) = @_;
281 my $mapping = $ctx->{mapping} or return $rv;
283 my $mid_map = $mapping->{$mid_raw};
285 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
286 my ($attr, $node, $idx, $level) = @$mid_map;
287 my $children = $node->{children};
288 my $nr_c = scalar @$children;
291 if (my $smsg = $node->{smsg}) {
292 ($$irt) = (($smsg->{references} || '') =~ m/<([^>]+)>\z/);
294 my $irt_map = $mapping->{$$irt} if defined $$irt;
295 if (defined $irt_map) {
296 $siblings = $irt_map->[1]->{children};
297 $nr_s = scalar(@$siblings) - 1;
298 $rv .= $pad . $irt_map->[0];
300 my $prev = $siblings->[$idx - 1];
301 my $pmid = $prev->{id};
303 my $s = ($idx - 1). ' preceding siblings ...';
304 $rv .= pad_link($pmid, $level, $s);
305 } elsif ($idx == 2) {
306 my $ppmid = $siblings->[0]->{id};
307 $rv .= $pad . $mapping->{$ppmid}->[0];
309 $rv .= $pad . $mapping->{$pmid}->[0];
312 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
313 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
314 $attr =~ s!\n\z!</b>\n!s;
315 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
316 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
319 my $cmid = $children->[0]->{id};
320 $rv .= $pad . $mapping->{$cmid}->[0];
322 my $s = ($nr_c - 1). ' more replies';
323 $rv .= pad_link($cmid, $level + 1, $s);
324 } elsif (my $cn = $children->[1]) {
325 $rv .= $pad . $mapping->{$cn->{id}}->[0];
329 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
331 my $nmid = $next->{id};
332 $rv .= $pad . $mapping->{$nmid}->[0];
333 my $nnext = $nr_s - $idx;
335 my $s = ($nnext - 1).' subsequent siblings';
336 $rv .= pad_link($nmid, $level, $s);
337 } elsif (my $nn = $siblings->[$idx + 2]) {
338 $rv .= $pad . $mapping->{$nn->{id}}->[0];
341 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
345 my ($rootset, $ctx, $cb) = @_;
346 my @q = map { (0, $_, -1) } @$rootset;
348 my ($level, $node, $i) = splice(@q, 0, 3);
349 defined $node or next;
350 $cb->($ctx, $level, $node, $i);
353 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
358 my ($ctx, $level, $node, $idx) = @_;
359 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
360 skel_dump($ctx, $level, $node);
363 sub thread_index_entry {
364 my ($ctx, $level, $smsg) = @_;
365 my ($beg, $end) = thread_adj_level($ctx, $level);
366 $beg . '<pre>' . index_entry($smsg, $ctx, 0) . '</pre>' . $end;
369 sub stream_thread ($$) {
370 my ($rootset, $ctx) = @_;
371 my $inbox = $ctx->{-inbox};
372 my @q = map { (0, $_) } @$rootset;
377 my $node = shift @q or next;
379 unshift @q, map { ($cl, $_) } @{$node->{children}};
380 $smsg = $inbox->smsg_mime($node->{smsg}) and last;
382 return missing_thread($ctx) unless $smsg;
384 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
385 $ctx->{-title_html} = ascii_html($smsg->subject);
386 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg);
388 PublicInbox::WwwStream->response($ctx, 200, sub {
392 my $node = shift @q or next;
394 unshift @q, map { ($cl, $_) } @{$node->{children}};
395 if ($smsg = $inbox->smsg_mime($node->{smsg})) {
396 return thread_index_entry($ctx, $level, $smsg);
398 return ghost_index_entry($ctx, $level, $node);
401 my $ret = join('', thread_adj_level($ctx, 0));
402 $ret .= ${$ctx->{dst}}; # skel
410 my $mid = $ctx->{mid};
411 my $srch = $ctx->{srch};
412 my ($nr, $msgs) = $srch->get_thread($mid);
413 return missing_thread($ctx) if $nr == 0;
414 my $skel = '<hr><pre>';
415 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
416 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
417 $skel .= "<b\nid=t>Thread overview:</b> ";
418 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
419 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
420 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
421 $skel .= "-- links below jump to the message on this page --\n";
422 $ctx->{-upfx} = '../../';
423 $ctx->{cur_level} = 0;
424 $ctx->{dst} = \$skel;
425 $ctx->{prev_attr} = '';
426 $ctx->{prev_level} = 0;
427 $ctx->{root_anchor} = anchor_for($mid);
428 $ctx->{mapping} = {};
429 $ctx->{s_nr} = "$nr+ messages in thread";
431 my $rootset = thread_results($ctx, $msgs);
433 # reduce hash lookups in pre_thread->skel_dump
434 my $inbox = $ctx->{-inbox};
435 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
436 walk_thread($rootset, $ctx, *pre_thread);
439 return stream_thread($rootset, $ctx) unless $ctx->{flat};
441 # flat display: lazy load the full message from smsg
443 while (my $m = shift @$msgs) {
444 $smsg = $inbox->smsg_mime($m) and last;
446 return missing_thread($ctx) unless $smsg;
447 $ctx->{-title_html} = ascii_html($smsg->subject);
448 $ctx->{-html_tip} = '<pre>'.index_entry($smsg, $ctx, scalar @$msgs);
450 PublicInbox::WwwStream->response($ctx, 200, sub {
453 while (my $m = shift @$msgs) {
454 $smsg = $inbox->smsg_mime($m) and last;
456 return index_entry($smsg, $ctx, scalar @$msgs) if $smsg;
462 sub multipart_text_as_html {
463 my ($mime, $upfx, $obfs_ibx) = @_;
466 # scan through all parts, looking for displayable text
467 msg_iter($mime, sub { $rv .= add_text_body($upfx, $obfs_ibx, $_[0]) });
472 my ($s, $l, $quot) = @_;
474 # show everything in the full version with anchor from
475 # short version (see above)
476 my $rv = $l->linkify_1(join('', @$quot));
479 # we use a <span> here to allow users to specify their own
480 # color for quoted text
481 $rv = $l->linkify_2(ascii_html($rv));
482 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
485 sub attach_link ($$$$;$) {
486 my ($upfx, $ct, $p, $fn, $err) = @_;
487 my ($part, $depth, @idx) = @$p;
488 my $nl = $idx[-1] > 1 ? "\n" : '';
489 my $idx = join('.', @idx);
490 my $size = bytes::length($part->body);
492 # hide attributes normally, unless we want to aid users in
493 # spotting MUA problems:
494 $ct =~ s/;.*// unless $err;
495 $ct = ascii_html($ct);
496 my $desc = $part->header('Content-Description');
497 $desc = $fn unless defined $desc;
498 $desc = '' unless defined $desc;
500 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
502 } elsif ($ct eq 'text/plain') {
507 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
510 "[-- Warning: decoded text below may be mangled --]\n";
512 $ret .= "[-- Attachment #$idx: ";
513 my $ts = "Type: $ct, Size: $size bytes";
514 $desc = ascii_html($desc);
515 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
520 my ($upfx, $obfs_ibx, $p) = @_;
521 # $p - from msg_iter: [ Email::MIME, depth, @idx ]
522 my ($part, $depth) = @$p; # attachment @idx is unused
523 my $ct = $part->content_type || 'text/plain';
524 my $fn = $part->filename;
526 if ($ct =~ m!\btext/x?html\b!i) {
527 return attach_link($upfx, $ct, $p, $fn);
530 my $s = eval { $part->body_str };
532 # badly-encoded message? tell the world about it!
535 if ($ct =~ m!\btext/plain\b!i) {
536 # Try to assume UTF-8 because Alpine seems to
537 # do wacky things and set charset=X-UNKNOWN
538 $part->charset_set('UTF-8');
539 $s = eval { $part->body_str };
541 # If forcing charset=UTF-8 failed,
542 # attach_link will warn further down...
543 $s = $part->body if $@;
545 return attach_link($upfx, $ct, $p, $fn);
549 my @lines = split(/^/m, $s);
551 if (defined($fn) || $depth > 0 || $err) {
552 $s .= attach_link($upfx, $ct, $p, $fn, $err);
556 my $l = PublicInbox::Linkify->new;
557 foreach my $cur (@lines) {
559 # show the previously buffered quote inline
560 flush_quote(\$s, $l, \@quot) if @quot;
564 $s .= $l->linkify_2(ascii_html($cur));
570 if (@quot) { # ugh, top posted
571 flush_quote(\$s, $l, \@quot);
572 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
575 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
576 if ($s =~ /\n\z/s) { # common, last line ends with a newline
578 } else { # some editors don't do newlines...
584 sub _msg_html_prepare {
585 my ($hdr, $ctx, $more, $nr) = @_;
586 my $srch = $ctx->{srch} if $ctx;
588 my $obfs_ibx = $ctx->{-obfs_ibx};
590 my $mids = mids($hdr);
591 my $multiple = scalar(@$mids) > 1; # zero, one, infinity
595 "<pre>WARNING: multiple messages refer to this Message-ID\n</pre>";
597 $rv .= "<pre\nid=b>"; # anchor for body start
602 $ctx->{-upfx} = '../';
605 foreach my $h (qw(From To Cc Subject Date)) {
606 my $v = $hdr->header($h);
607 defined($v) && ($v ne '') or next;
608 $v = PublicInbox::Hval->new($v);
611 my @n = PublicInbox::Address::names($v->raw);
612 $title[1] = ascii_html(join(', ', @n));
613 obfuscate_addrs($obfs_ibx, $title[1]) if $obfs_ibx;
614 } elsif ($h eq 'Subject') {
615 $title[0] = $v->as_html;
617 $rv .= qq($h: <a\nhref="#r"\nid=t>);
618 $rv .= $v->as_html . "</a>\n";
623 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
627 $title[0] ||= '(no subject)';
628 $ctx->{-title_html} = join(' - ', @title);
630 my $mid = PublicInbox::Hval->new_msgid($_) ;
631 my $mhtml = $mid->as_html;
633 my $href = $mid->{href};
634 $rv .= "Message-ID: ";
635 $rv .= "<a\nhref=\"../$href/\">";
636 $rv .= "<$mhtml></a> ";
637 $rv .= "(<a\nhref=\"../$href/raw\">raw</a>)\n";
639 $rv .= "Message-ID: <$mhtml> ";
640 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
643 $rv .= _parent_headers($hdr, $srch);
648 my ($dst, $ctx, $hdr, $tpfx) = @_;
649 my $srch = $ctx->{srch};
650 my $mid = mids($hdr)->[0];
651 my ($nr, $msgs) = $srch->get_thread($mid);
652 my $expand = qq(expand[<a\nhref="${tpfx}T/#u">flat</a>) .
653 qq(|<a\nhref="${tpfx}t/#u">nested</a>] ) .
654 qq(<a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
655 qq(<a\nhref="${tpfx}t.atom">Atom feed</a>);
657 my $parent = in_reply_to($hdr);
658 $$dst .= "\n<b>Thread overview: </b>";
660 if (defined $parent) {
661 $$dst .= "$expand\n ";
662 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
664 $$dst .= "[no followups] $expand\n";
666 $ctx->{next_msg} = undef;
667 $ctx->{parent_msg} = $parent;
671 $$dst .= "$nr+ messages / $expand";
672 $$dst .= qq! <a\nhref="#b">top</a>\n!;
674 my $subj = $hdr->header('Subject');
675 defined $subj or $subj = '';
676 $ctx->{prev_subj} = [ split(/ /, $srch->subject_normalized($subj)) ];
678 $ctx->{prev_attr} = '';
679 $ctx->{prev_level} = 0;
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($ctx, $msgs), $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 ($ctx, $msgs) = @_;
803 require PublicInbox::SearchThread;
804 PublicInbox::SearchThread::thread($msgs, *sort_ds, $ctx->{-inbox});
809 require PublicInbox::ExtMsg;
810 PublicInbox::ExtMsg::ext_msg($ctx);
815 fmt_ts(msg_datestamp($hdr));
818 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
821 my ($prev_subj, $subj, $val) = @_;
823 my $omit = ''; # '"' denotes identical text omitted
824 my (@prev_pop, @curr_pop);
825 while (@$prev_subj && @$subj && $subj->[-1] eq $prev_subj->[-1]) {
826 push(@prev_pop, pop(@$prev_subj));
827 push(@curr_pop, pop(@$subj));
830 pop @$subj if @$subj && $subj->[-1] =~ /^re:\s*/i;
831 if (scalar(@curr_pop) == 1) {
833 push @$prev_subj, @prev_pop;
834 push @$subj, @curr_pop;
840 my ($ctx, $level, $node) = @_;
841 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
843 my $dst = $ctx->{dst};
844 my $cur = $ctx->{cur};
845 my $mid = $smsg->{mid};
847 my $f = ascii_html($smsg->from_name);
848 my $obfs_ibx = $ctx->{-obfs_ibx};
849 obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
851 my $d = fmt_ts($smsg->{ds}) . ' ' . indent_for($level) . th_pfx($level);
853 $ctx->{first_level} ||= $level;
855 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
856 $ctx->{prev_attr} = $attr;
858 $ctx->{prev_level} = $level;
863 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
864 "$attr [this message]</a></b>\n";
867 $ctx->{prev_msg} = $mid;
870 $ctx->{next_msg} ||= $mid;
873 # Subject is never undef, this mail was loaded from
874 # our Xapian which would've resulted in '' if it were
875 # really missing (and Filter rejects empty subjects)
876 my @subj = split(/ /, $ctx->{srch}->subject_normalized($smsg->subject));
878 # remove common suffixes from the subject if it matches the previous,
879 # so we do not show redundant text at the end.
880 my $prev_subj = $ctx->{prev_subj} || [];
881 $ctx->{prev_subj} = [ @subj ];
882 my $omit = dedupe_subject($prev_subj, \@subj, '" ');
885 my $subj = join(' ', @subj);
886 $subj = ascii_html($subj);
887 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
888 $end = "$subj</a> $omit$f\n"
894 my $mapping = $ctx->{mapping};
896 my $map = $mapping->{$mid};
897 $id = id_compress($mid, 1);
899 $map->[0] = "$d<a\nhref=\"$m\">$end";
902 $m = $ctx->{-upfx}.mid_escape($mid).'/';
904 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
908 my ($ctx, $level, $node) = @_;
910 my $mid = $node->{id};
911 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
913 $d .= indent_for($level) . th_pfx($level);
914 my $upfx = $ctx->{-upfx};
915 my $m = PublicInbox::Hval->new_msgid($mid);
916 my $href = $upfx . $m->{href} . '/';
917 my $html = $m->as_html;
919 my $mapping = $ctx->{mapping};
920 my $map = $mapping->{$mid} if $mapping;
922 my $id = id_compress($mid, 1);
923 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
924 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
926 $d .= qq{<<a\nhref="$href">$html</a>>\n};
928 my $dst = $ctx->{dst};
934 (eval { $a->topmost->{smsg}->ds } || 0) <=>
935 (eval { $b->topmost->{smsg}->ds } || 0)
939 # accumulate recent topics if search is supported
940 # returns 200 if done, 404 if not
942 my ($ctx, $level, $node) = @_;
943 my $srch = $ctx->{srch};
944 my $mid = $node->{id};
945 my $x = $node->{smsg} || $ctx->{-inbox}->smsg_by_mid($mid);
950 $subj = $srch->subject_normalized($subj);
953 $topic = [ $ds, 1, { $subj => $mid }, $subj ];
954 $ctx->{-cur_topic} = $topic;
955 push @{$ctx->{order}}, $topic;
959 $topic = $ctx->{-cur_topic}; # should never be undef
960 $topic->[0] = $ds if $ds > $topic->[0];
962 my $seen = $topic->[2];
963 if (scalar(@$topic) == 3) { # parent was a ghost
965 } elsif (!$seen->{$subj}) {
966 push @$topic, $level, $subj;
968 $seen->{$subj} = $mid; # latest for subject
969 } else { # ghost message
970 return if $level != 0; # ignore child ghosts
971 $topic = [ -666, 0, {} ];
972 $ctx->{-cur_topic} = $topic;
973 push @{$ctx->{order}}, $topic;
979 my $order = delete $ctx->{order}; # [ ds, subj1, subj2, subj3, ... ]
981 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
986 my $ibx = $ctx->{-inbox};
987 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
988 my $srch = $ctx->{srch};
990 # sort by recency, this allows new posts to "bump" old topics...
991 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
992 my ($ds, $n, $seen, $top, @ex) = @$topic;
994 next unless defined $top; # ghost topic
995 my $mid = delete $seen->{$top};
996 my $href = mid_escape($mid);
997 my $prev_subj = [ split(/ /, $top) ];
998 $top = PublicInbox::Hval->new($top)->as_html;
1001 # $n isn't the total number of posts on the topic,
1002 # just the number of posts in the current results window
1006 $anchor = '#u'; # top of only message
1008 $n = " ($n+ messages)";
1009 $anchor = '#t'; # thread skeleton
1012 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
1013 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
1014 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
1015 " $ds UTC $n - $mbox / $atom\n";
1016 for (my $i = 0; $i < scalar(@ex); $i += 2) {
1017 my $level = $ex[$i];
1018 my $subj = $ex[$i + 1];
1019 $mid = delete $seen->{$subj};
1020 my @subj = split(/ /, $srch->subject_normalized($subj));
1021 my @next_prev = @subj; # full copy
1022 my $omit = dedupe_subject($prev_subj, \@subj, ' "');
1023 $prev_subj = \@next_prev;
1024 $subj = ascii_html(join(' ', @subj));
1025 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
1026 $href = mid_escape($mid);
1027 $s .= indent_for($level) . TCHILD;
1028 $s .= "<a\nhref=\"$href/T/#u\">$subj</a>$omit\n";
1032 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
1038 POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts));
1042 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]);
1043 timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
1046 sub pagination_footer ($$) {
1047 my ($ctx, $latest) = @_;
1048 delete $ctx->{qp} or return;
1049 my $next = $ctx->{next_page} || '';
1050 my $prev = $ctx->{prev_page} || '';
1052 $next = $next ? "$next " : ' ';
1053 $prev .= qq! <a\nhref='$latest'>latest</a>!;
1055 "<hr><pre>page: $next$prev</pre>";
1058 sub index_nav { # callback for WwwStream
1059 my (undef, $ctx) = @_;
1060 pagination_footer($ctx, '.')
1063 sub paginate_recent ($) {
1065 my $t = $ctx->{qp}->{t} || '';
1066 my $lim = 200; # this is our window
1067 my $opts = { limit => $lim };
1068 my ($after, $before);
1070 # Xapian uses '..' but '-' is perhaps friendier to URL linkifiers
1071 # if only $after exists "YYYYMMDD.." because "." could be skipped
1072 # if interpreted as an end-of-sentence
1073 $t =~ s/\A(\d{8,14})-// and $after = str2ts($1);
1074 $t =~ /\A(\d{8,14})\z/ and $before = str2ts($1);
1076 my $ibx = $ctx->{-inbox};
1077 my $msgs = $ibx->recent($opts, $after, $before);
1078 my $nr = scalar @$msgs;
1079 if ($nr < $lim && defined($after)) {
1080 $after = $before = undef;
1081 $msgs = $ibx->recent($opts);
1082 $nr = scalar @$msgs;
1084 my $more = $nr == $lim;
1085 my ($newest, $oldest);
1087 $newest = $msgs->[0]->{ts};
1088 $oldest = $msgs->[-1]->{ts};
1089 # if we only had $after, our SQL query in ->recent ordered
1090 if ($newest < $oldest) {
1091 ($oldest, $newest) = ($newest, $oldest);
1092 $more = 0 if defined($after) && $after < $oldest;
1095 if (defined($oldest) && $more) {
1096 my $s = ts2str($oldest);
1097 $ctx->{next_page} = qq!<a\nhref="?t=$s"\nrel=next>next</a>!;
1099 if (defined($newest) && (defined($before) || defined($after))) {
1100 my $s = ts2str($newest);
1101 $ctx->{prev_page} = qq!<a\nhref="?t=$s-"\nrel=prev>prev</a>!;
1108 my $msgs = paginate_recent($ctx);
1110 walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic);
1112 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
1115 sub thread_adj_level {
1116 my ($ctx, $level) = @_;
1118 my $max = $ctx->{cur_level};
1120 return ('', '') if $max == 0; # flat output
1122 # reset existing lists
1123 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
1124 $ctx->{cur_level} = 0;
1126 } elsif ($level == $max) { # continue existing list
1128 } elsif ($level < $max) {
1129 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1130 $ctx->{cur_level} = $level;
1131 ("$beg<li>", '</li>');
1132 } else { # ($level > $max) # start a new level
1133 $ctx->{cur_level} = $level;
1134 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1139 sub ghost_index_entry {
1140 my ($ctx, $level, $node) = @_;
1141 my ($beg, $end) = thread_adj_level($ctx, $level);
1142 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})