1 # Copyright (C) 2014-2019 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 bytes (); # only for bytes::length
10 use PublicInbox::MsgTime qw(msg_datestamp);
11 use PublicInbox::Hval qw/ascii_html obfuscate_addrs/;
12 use PublicInbox::Linkify;
13 use PublicInbox::MID qw/id_compress mid_escape mids mids_for_index references/;
14 use PublicInbox::MsgIter;
15 use PublicInbox::Address;
16 use PublicInbox::WwwStream;
17 use PublicInbox::Reply;
18 use PublicInbox::ViewDiff qw(flush_diff);
20 use Time::Local qw(timegm);
21 use PublicInbox::SearchMsg qw(subject_normalized);
22 use constant COLS => 72;
23 use constant INDENT => ' ';
24 use constant TCHILD => '` ';
25 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
29 my $more = $ctx->{more};
31 # $more cannot be true w/o $smsg being defined:
32 my $upfx = $more ? '../'.mid_escape($ctx->{smsg}->mid).'/' : '';
34 multipart_text_as_html($ctx->{mime}, $upfx, $ctx) .
36 } elsif ($more && @$more) {
38 msg_html_more($ctx, $more, $nr);
39 } elsif ($nr == $ctx->{end_nr}) {
40 # fake an EOF if generating the footer fails;
41 # we want to at least show the message if something
44 my $hdr = delete($ctx->{mime})->header_obj;
45 '<pre>' . html_footer($hdr, 1, $ctx) .
46 '</pre>' . msg_reply($ctx, $hdr)
53 # public functions: (unstable)
56 my ($ctx, $mime, $more, $smsg) = @_;
57 my $ibx = $ctx->{-inbox};
58 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
59 $ctx->{tip} = _msg_html_prepare($mime->header_obj, $ctx, $more, 0);
64 PublicInbox::WwwStream->response($ctx, 200, \&msg_html_i);
69 my $mid = $ctx->{mid};
70 my $ibx = $ctx->{-inbox};
73 if (my $over = $ibx->over) {
75 $smsg = $over->next_by_mid($mid, \$id, \$prev);
76 $first = $ibx->msg_by_smsg($smsg) if $smsg;
78 my $next = $over->next_by_mid($mid, \$id, \$prev);
79 $more = [ $id, $prev, $next ] if $next;
83 $first = $ibx->msg_by_mid($mid) or return;
85 msg_html($ctx, PublicInbox::MIME->new($first), $more, $smsg);
89 my ($ctx, $more, $nr) = @_;
91 my ($id, $prev, $smsg) = @$more;
92 my $mid = $ctx->{mid};
93 my $ibx = $ctx->{-inbox};
94 $smsg = $ibx->smsg_mime($smsg);
95 my $next = $ibx->over->next_by_mid($mid, \$id, \$prev);
96 @$more = $next ? ($id, $prev, $next) : ();
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, $ctx) .
108 warn "Error lookup up additional messages: $@\n";
109 $str = '<pre>Error looking up additional messages</pre>';
114 # /$INBOX/$MESSAGE_ID/#R
116 my ($ctx, $hdr) = @_;
118 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
120 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
123 my $ibx = $ctx->{-inbox};
124 if (my $url = $ibx->{infourl}) {
125 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
126 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
129 my ($arg, $link, $reply_to_all) =
130 PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
131 if (ref($arg) eq 'SCALAR') {
132 return '<pre id=R>'.ascii_html($$arg).'</pre>';
135 # mailto: link only works if address obfuscation is disabled
139 * If your mail client supports setting the <b>In-Reply-To</b> header
140 via mailto: links, try the <a
141 href="$link">mailto: link</a>
145 push @$arg, '/path/to/YOUR_REPLY';
146 $arg = ascii_html(join(" \\\n ", '', @$arg));
149 id=R><b>Reply instructions:</b>
151 You may reply publically to <a
152 href=#t>this message</a> via plain-text email
153 using any one of the following methods:
155 * Save the following mbox file, import it into your mail client,
156 and $reply_to_all from there: <a
159 Avoid top-posting and favor interleaved quoting:
161 href="$p_url">$p_url</a>
163 * Reply using the <b>--to</b>, <b>--cc</b>, and <b>--in-reply-to</b>
164 switches of git-send-email(1):
169 href="$se_url">$se_url</a>
176 my $refs = references($hdr);
180 sub fold_addresses ($) {
181 return $_[0] if length($_[0]) <= COLS;
182 # try to fold on commas after non-word chars before $lim chars,
183 # Try to get the "," preceeded by ">" or ")", but avoid folding
184 # on the comma where somebody uses "Lastname, Firstname".
185 # We also try to keep the last and penultimate addresses in
186 # the list on the same line if possible, hence the extra \z
187 # Fall back to folding on spaces at $lim + 1 chars
188 my $lim = COLS - 8; # 8 = "\t" display width
189 my $too_long = $lim + 1;
190 $_[0] =~ s/\s*\z//s; # Email::Simple doesn't strip trailing spaces
192 ($_[0] =~ /(.{0,$lim}\W(?:,|\z)|
195 .{$too_long,}?)(?:\s|\z)/xgo));
198 sub _hdr_names_html ($$) {
199 my ($hdr, $field) = @_;
200 my @vals = $hdr->header($field) or return '';
201 ascii_html(join(', ', PublicInbox::Address::names(join(',', @vals))));
205 my ($nr, $singular, $plural) = @_;
206 return "0 $plural" if $nr == 0;
207 $nr == 1 ? "$nr $singular" : "$nr $plural";
210 # this is already inside a <pre>
212 my ($smsg, $ctx, $more) = @_;
213 my $subj = $smsg->subject;
214 my $mid_raw = $smsg->mid;
215 my $id = id_compress($mid_raw, 1);
218 my $root_anchor = $ctx->{root_anchor} || '';
220 my $obfs_ibx = $ctx->{-obfs_ibx};
222 $subj = '(no subject)' if $subj eq '';
223 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
224 $subj = '<b>'.ascii_html($subj).'</b>';
225 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
226 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
228 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
230 my $ds = $smsg->ds; # for v1 non-Xapian/SQLite users
231 # deleting {mime} is critical to memory use,
232 # the rest of the fields saves about 400K as we iterate across 1K msgs
233 my ($mime) = delete @$smsg{qw(mime ds ts blob subject)};
235 my $hdr = $mime->header_obj;
236 my $from = _hdr_names_html($hdr, 'From');
237 obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
238 $rv .= "From: $from @ ".fmt_ts($ds)." UTC";
239 my $upfx = $ctx->{-upfx};
240 my $mhref = $upfx . mid_escape($mid_raw) . '/';
241 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
242 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
243 my $to = fold_addresses(_hdr_names_html($hdr, 'To'));
244 my $cc = fold_addresses(_hdr_names_html($hdr, 'Cc'));
245 my ($tlen, $clen) = (length($to), length($cc));
247 if (($tlen + $clen) > COLS) {
248 $to_cc .= ' To: '.$to."\n" if $tlen;
249 $to_cc .= ' Cc: '.$cc."\n" if $clen;
252 $to_cc .= ' To: '.$to;
253 $to_cc .= '; <b>+Cc:</b> '.$cc if $clen;
255 $to_cc .= ' Cc: '.$cc if $clen;
259 obfuscate_addrs($obfs_ibx, $to_cc) if $obfs_ibx;
262 my $mapping = $ctx->{mapping};
263 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
264 my $mirt = PublicInbox::Hval->new_msgid($irt);
265 my $href = $upfx . $mirt->{href}. '/';
266 my $html = $mirt->as_html;
267 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
271 # scan through all parts, looking for displayable text
272 $ctx->{mhref} = $mhref;
274 msg_iter($mime, \&add_text_body, $ctx);
278 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
279 "<a\nhref=\"$mhref\">permalink</a>" .
280 " <a\nhref=\"${mhref}raw\">raw</a>" .
281 " <a\nhref=\"${mhref}#R\">reply</a>";
284 if (defined(my $pct = $smsg->{pct})) { # used by SearchView.pm
285 $rv .= "\t[relevance $pct%]";
288 my $nested = 'nested';
293 $flat = "<b>$flat</b>";
295 $nested = "<b>$nested</b>";
297 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
298 $rv .= "|<a\nhref=\"${mhref}t/#u\">$nested</a>]";
299 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
304 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
308 sub pad_link ($$;$) {
309 my ($mid, $level, $s) = @_;
311 my $id = id_compress($mid, 1);
312 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
316 my ($mid_raw, $irt, $id, $ctx) = @_;
318 my $mapping = $ctx->{mapping} or return $rv;
320 my $mid_map = $mapping->{$mid_raw};
322 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
323 my ($attr, $node, $idx, $level) = @$mid_map;
324 my $children = $node->{children};
325 my $nr_c = scalar @$children;
328 if (my $smsg = $node->{smsg}) {
329 # delete saves about 200KB on a 1K message thread
330 if (my $refs = delete $smsg->{references}) {
331 ($$irt) = ($refs =~ m/<([^>]+)>\z/);
334 my $irt_map = $mapping->{$$irt} if defined $$irt;
335 if (defined $irt_map) {
336 $siblings = $irt_map->[1]->{children};
337 $nr_s = scalar(@$siblings) - 1;
338 $rv .= $pad . $irt_map->[0];
340 my $prev = $siblings->[$idx - 1];
341 my $pmid = $prev->{id};
343 my $s = ($idx - 1). ' preceding siblings ...';
344 $rv .= pad_link($pmid, $level, $s);
345 } elsif ($idx == 2) {
346 my $ppmid = $siblings->[0]->{id};
347 $rv .= $pad . $mapping->{$ppmid}->[0];
349 $rv .= $pad . $mapping->{$pmid}->[0];
352 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
353 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
354 $attr =~ s!\n\z!</b>\n!s;
355 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
356 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
359 my $cmid = $children->[0]->{id};
360 $rv .= $pad . $mapping->{$cmid}->[0];
362 my $s = ($nr_c - 1). ' more replies';
363 $rv .= pad_link($cmid, $level + 1, $s);
364 } elsif (my $cn = $children->[1]) {
365 $rv .= $pad . $mapping->{$cn->{id}}->[0];
369 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
371 my $nmid = $next->{id};
372 $rv .= $pad . $mapping->{$nmid}->[0];
373 my $nnext = $nr_s - $idx;
375 my $s = ($nnext - 1).' subsequent siblings';
376 $rv .= pad_link($nmid, $level, $s);
377 } elsif (my $nn = $siblings->[$idx + 2]) {
378 $rv .= $pad . $mapping->{$nn->{id}}->[0];
381 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
385 my ($rootset, $ctx, $cb) = @_;
386 my @q = map { (0, $_, -1) } @$rootset;
388 my ($level, $node, $i) = splice(@q, 0, 3);
389 defined $node or next;
390 $cb->($ctx, $level, $node, $i) or return;
393 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
398 my ($ctx, $level, $node, $idx) = @_;
399 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
400 skel_dump($ctx, $level, $node);
403 sub thread_index_entry {
404 my ($ctx, $level, $smsg) = @_;
405 my ($beg, $end) = thread_adj_level($ctx, $level);
406 $beg . '<pre>' . index_entry($smsg, $ctx, 0) . '</pre>' . $end;
409 sub stream_thread_i { # PublicInbox::WwwStream::getline callback
411 return unless exists($ctx->{dst});
412 my $q = $ctx->{-queue};
414 my $level = shift @$q;
415 my $node = shift @$q or next;
417 unshift @$q, map { ($cl, $_) } @{$node->{children}};
418 if (my $smsg = $ctx->{-inbox}->smsg_mime($node->{smsg})) {
419 return thread_index_entry($ctx, $level, $smsg);
421 return ghost_index_entry($ctx, $level, $node);
424 join('', thread_adj_level($ctx, 0)) . ${delete $ctx->{dst}}; # skel
427 sub stream_thread ($$) {
428 my ($rootset, $ctx) = @_;
429 my $ibx = $ctx->{-inbox};
430 my @q = map { (0, $_) } @$rootset;
434 my $node = shift @q or next;
436 unshift @q, map { ($cl, $_) } @{$node->{children}};
437 $smsg = $ibx->smsg_mime($node->{smsg}) and last;
439 return missing_thread($ctx) unless $smsg;
441 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
442 $ctx->{-title_html} = ascii_html($smsg->subject);
443 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg);
444 $ctx->{-queue} = \@q;
445 PublicInbox::WwwStream->response($ctx, 200, \&stream_thread_i);
450 my $mid = $ctx->{mid};
451 my $ibx = $ctx->{-inbox};
452 my ($nr, $msgs) = $ibx->over->get_thread($mid);
453 return missing_thread($ctx) if $nr == 0;
454 my $skel = '<hr><pre>';
455 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
456 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
457 $skel .= "<b\nid=t>Thread overview:</b> ";
458 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
459 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
460 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
461 $skel .= "-- links below jump to the message on this page --\n";
462 $ctx->{-upfx} = '../../';
463 $ctx->{cur_level} = 0;
464 $ctx->{dst} = \$skel;
465 $ctx->{prev_attr} = '';
466 $ctx->{prev_level} = 0;
467 $ctx->{root_anchor} = anchor_for($mid);
468 $ctx->{mapping} = {};
469 $ctx->{s_nr} = ($nr > 1 ? "$nr+ messages" : 'only message')
472 my $rootset = thread_results($ctx, $msgs);
474 # reduce hash lookups in pre_thread->skel_dump
475 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
476 walk_thread($rootset, $ctx, *pre_thread);
479 return stream_thread($rootset, $ctx) unless $ctx->{flat};
481 # flat display: lazy load the full message from smsg
483 while (my $m = shift @$msgs) {
484 $smsg = $ibx->smsg_mime($m) and last;
486 return missing_thread($ctx) unless $smsg;
487 $ctx->{-title_html} = ascii_html($smsg->subject);
488 $ctx->{-html_tip} = '<pre>'.index_entry($smsg, $ctx, scalar @$msgs);
489 $ctx->{msgs} = $msgs;
490 PublicInbox::WwwStream->response($ctx, 200, \&thread_html_i);
493 sub thread_html_i { # PublicInbox::WwwStream::getline callback
495 my $msgs = $ctx->{msgs} or return;
496 while (my $smsg = shift @$msgs) {
497 $ctx->{-inbox}->smsg_mime($smsg) or next;
498 return index_entry($smsg, $ctx, scalar @$msgs);
500 my ($skel) = delete @$ctx{qw(dst msgs)};
504 sub multipart_text_as_html {
505 my ($mime, $mhref, $ctx) = @_;
506 $ctx->{mhref} = $mhref;
507 $ctx->{rv} = \(my $rv = '');
509 # scan through all parts, looking for displayable text
510 msg_iter($mime, \&add_text_body, $ctx);
511 ${delete $ctx->{rv}};
515 my ($s, $l, $quot) = @_;
517 # show everything in the full version with anchor from
518 # short version (see above)
519 my $rv = $l->linkify_1($$quot);
521 # we use a <span> here to allow users to specify their own
522 # color for quoted text
523 $rv = $l->linkify_2(ascii_html($rv));
525 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
528 sub attach_link ($$$$;$) {
529 my ($ctx, $ct, $p, $fn, $err) = @_;
530 my ($part, $depth, @idx) = @$p;
531 my $nl = $idx[-1] > 1 ? "\n" : '';
532 my $idx = join('.', @idx);
533 my $size = bytes::length($part->body);
535 # hide attributes normally, unless we want to aid users in
536 # spotting MUA problems:
537 $ct =~ s/;.*// unless $err;
538 $ct = ascii_html($ct);
539 my $desc = $part->header('Content-Description');
540 $desc = $fn unless defined $desc;
541 $desc = '' unless defined $desc;
543 if (defined $fn && $fn =~ /\A$PublicInbox::Hval::FN\z/o) {
545 } elsif ($ct eq 'text/plain') {
551 $$rv .= qq($nl<a\nhref="$ctx->{mhref}$idx-$sfn">);
553 $$rv .= "[-- Warning: decoded text below may be mangled --]\n";
555 $$rv .= "[-- Attachment #$idx: ";
556 my $ts = "Type: $ct, Size: $size bytes";
557 $desc = ascii_html($desc);
558 $$rv .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
563 sub add_text_body { # callback for msg_iter
565 my $upfx = $ctx->{mhref};
566 my $ibx = $ctx->{-inbox};
567 # $p - from msg_iter: [ Email::MIME, depth, @idx ]
568 my ($part, $depth, @idx) = @$p;
569 my $ct = $part->content_type || 'text/plain';
570 my $fn = $part->filename;
571 my ($s, $err) = msg_part_text($part, $ct);
572 return attach_link($ctx, $ct, $p, $fn) unless defined $s;
574 # makes no difference to browsers, and don't screw up filename
575 # link generation in diffs with the extra '%0D'
578 # always support diff-highlighting, but we can't linkify hunk
579 # headers for solver unless some coderepo are configured:
581 if ($s =~ /^(?:diff|---|\+{3}) /ms) {
582 # diffstat anchors do not link across attachments or messages:
583 $idx[0] = $upfx . $idx[0] if $upfx ne '';
584 $ctx->{-apfx} = join('/', @idx);
585 $ctx->{-anchors} = {}; # attr => filename
586 $ctx->{-diff} = $diff = [];
587 delete $ctx->{-long_path};
589 if ($ibx->{-repo_objs}) {
590 if (index($upfx, '//') >= 0) { # absolute URL (Atom feeds)
592 $spfx =~ s!/([^/]*)/\z!/!;
594 my $n_slash = $upfx =~ tr!/!/!;
597 } elsif ($n_slash == 1) {
599 } else { # nslash == 2
604 $ctx->{-spfx} = $spfx;
607 # some editors don't put trailing newlines at the end:
608 $s .= "\n" unless $s =~ /\n\z/s;
610 # split off quoted and unquoted blocks:
611 my @sections = split(/((?:^>[^\n]*\n)+)/sm, $s);
614 if (defined($fn) || $depth > 0 || $err) {
615 # badly-encoded message with $err? tell the world about it!
616 attach_link($ctx, $ct, $p, $fn, $err);
619 my $l = PublicInbox::Linkify->new;
620 foreach my $cur (@sections) {
622 flush_quote($rv, $l, \$cur);
624 @$diff = split(/^/m, $cur);
626 flush_diff($rv, $ctx, $l);
630 $$rv .= $l->linkify_2(ascii_html($cur));
635 obfuscate_addrs($ibx, $$rv) if $ibx->{obfuscate};
638 sub _msg_html_prepare {
639 my ($hdr, $ctx, $more, $nr) = @_;
641 my $over = $ctx->{-inbox}->over;
642 my $obfs_ibx = $ctx->{-obfs_ibx};
644 my $mids = mids_for_index($hdr);
648 "<pre>WARNING: multiple messages have this Message-ID\n</pre>";
650 $rv .= "<pre\nid=b>"; # anchor for body start
655 $ctx->{-upfx} = '../';
657 my @title; # (Subject[0], From[0])
658 for my $v ($hdr->header('From')) {
659 $v = PublicInbox::Hval->new($v);
660 my @n = PublicInbox::Address::names($v->raw);
661 $title[1] //= ascii_html(join(', ', @n));
664 obfuscate_addrs($obfs_ibx, $v);
665 obfuscate_addrs($obfs_ibx, $title[1]);
667 $rv .= "From: $v\n" if $v ne '';
669 foreach my $h (qw(To Cc)) {
670 for my $v ($hdr->header($h)) {
673 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
674 $rv .= "$h: $v\n" if $v ne '';
677 my @subj = $hdr->header('Subject');
681 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
684 $rv .= qq(<a\nhref="#r"\nid=t>$v</a>\n);
690 } else { # dummy anchor for thread skeleton at bottom of page
691 $rv .= qq(<a\nhref="#r"\nid=t></a>) if $over;
692 $title[0] = '(no subject)';
694 for my $v ($hdr->header('Date')) {
696 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; # possible :P
699 $ctx->{-title_html} = join(' - ', @title);
700 if (scalar(@$mids) == 1) { # common case
701 my $mid = PublicInbox::Hval->new_msgid($mids->[0]);
702 my $mhtml = $mid->as_html;
703 $rv .= "Message-ID: <$mhtml> ";
704 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
706 # X-Alt-Message-ID can happen if a message is injected from
707 # public-inbox-nntpd because of multiple Message-ID headers.
708 my $lnk = PublicInbox::Linkify->new;
710 for my $h (qw(Message-ID X-Alt-Message-ID)) {
711 $s .= "$h: $_\n" for ($hdr->header_raw($h));
713 $lnk->linkify_mids('..', \$s, 1);
716 $rv .= _parent_headers($hdr, $over);
721 my ($dst, $ctx, $hdr, $tpfx) = @_;
722 my $mid = mids($hdr)->[0];
723 my $ibx = $ctx->{-inbox};
724 my ($nr, $msgs) = $ibx->over->get_thread($mid);
725 my $expand = qq(expand[<a\nhref="${tpfx}T/#u">flat</a>) .
726 qq(|<a\nhref="${tpfx}t/#u">nested</a>] ) .
727 qq(<a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
728 qq(<a\nhref="${tpfx}t.atom">Atom feed</a>);
730 my $parent = in_reply_to($hdr);
731 $$dst .= "\n<b>Thread overview: </b>";
733 if (defined $parent) {
734 $$dst .= "$expand\n ";
735 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
737 $$dst .= "[no followups] $expand\n";
739 $ctx->{next_msg} = undef;
740 $ctx->{parent_msg} = $parent;
744 $$dst .= "$nr+ messages / $expand";
745 $$dst .= qq! <a\nhref="#b">top</a>\n!;
747 # nb: mutt only shows the first Subject in the index pane
748 # when multiple Subject: headers are present, so we follow suit:
749 my $subj = $hdr->header('Subject') // '';
750 $subj = '(no subject)' if $subj eq '';
751 $ctx->{prev_subj} = [ split(/ /, subject_normalized($subj)) ];
753 $ctx->{prev_attr} = '';
754 $ctx->{prev_level} = 0;
757 # reduce hash lookups in skel_dump
758 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
759 walk_thread(thread_results($ctx, $msgs), $ctx, *skel_dump);
761 $ctx->{parent_msg} = $parent;
764 sub _parent_headers {
765 my ($hdr, $over) = @_;
767 my @irt = $hdr->header_raw('In-Reply-To');
770 my $lnk = PublicInbox::Linkify->new;
771 $rv .= "In-Reply-To: $_\n" for @irt;
772 $lnk->linkify_mids('..', \$rv);
774 $refs = references($hdr);
775 my $irt = pop @$refs;
777 my $v = PublicInbox::Hval->new_msgid($irt);
778 my $html = $v->as_html;
779 my $href = $v->{href};
780 $rv .= "In-Reply-To: <";
781 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
785 # do not display References: if search is present,
786 # we show the thread skeleton at the bottom, instead.
789 $refs //= references($hdr);
791 @$refs = map { linkify_ref_no_over($_) } @$refs;
792 $rv .= 'References: '. join("\n\t", @$refs) . "\n";
798 my ($hdr, $standalone, $ctx, $rhref) = @_;
800 my $ibx = $ctx->{-inbox} if $ctx;
803 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
805 if ($idx && $ibx->over) {
807 thread_skel(\$idx, $ctx, $hdr, $tpfx);
812 if (my $n = $ctx->{next_msg}) {
813 $n = PublicInbox::Hval->new_msgid($n)->{href};
814 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
817 my $par = $ctx->{parent_msg};
819 $u = PublicInbox::Hval->new_msgid($par)->{href};
822 if (my $p = $ctx->{prev_msg}) {
823 $prev = PublicInbox::Hval->new_msgid($p)->{href};
824 if ($p && $par && $p eq $par) {
825 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
826 'rel=prev>prev parent</a>';
829 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
831 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
833 } elsif ($u) { # unlikely
834 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
836 $irt = "$next $prev$parent ";
841 $irt .= qq(<a\nhref="$rhref">reply</a>);
845 sub linkify_ref_no_over {
846 my $v = PublicInbox::Hval->new_msgid($_[0]);
847 my $html = $v->as_html;
848 my $href = $v->{href};
849 "<<a\nhref=\"../$href/\">$html</a>>";
854 'm' . id_compress($msgid, 1);
858 my ($upfx, $mid) = @_;
860 $mid = PublicInbox::Hval->new_msgid($mid);
861 my $href = $mid->{href};
862 my $html = $mid->as_html;
863 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
868 $level ? INDENT x ($level - 1) : '';
872 my ($ctx, $level, $node, $idx) = @_;
873 ++$ctx->{root_idx} if $level == 0;
874 if ($node->{id} eq $ctx->{mid}) {
875 $ctx->{found_mid_at} = $ctx->{root_idx};
881 sub strict_loose_note ($) {
884 " -- strict thread matches above, loose matches on Subject: below --\n";
886 if ($nr > PublicInbox::Over::DEFAULT_LIMIT()) {
888 " -- use mbox.gz link to download all $nr messages --\n";
894 my ($ctx, $msgs) = @_;
895 require PublicInbox::SearchThread;
896 my $rootset = PublicInbox::SearchThread::thread($msgs, \&sort_ds, $ctx);
898 # FIXME: `tid' is broken on --reindex, so that needs to be fixed
899 # and preserved in the future. This bug is hidden by `sid' matches
900 # in get_thread, so we never noticed it until now. And even when
901 # reindexing is fixed, we'll keep this code until a SCHEMA_VERSION
902 # bump since reindexing is expensive and users may not do it
904 # loose threading could've returned too many results,
905 # put the root the message we care about at the top:
906 my $mid = $ctx->{mid};
907 if (defined($mid) && scalar(@$rootset) > 1) {
908 $ctx->{root_idx} = -1;
909 my $nr = scalar @$msgs;
910 walk_thread($rootset, $ctx, *find_mid_root);
911 my $idx = $ctx->{found_mid_at};
912 if (defined($idx) && $idx != 0) {
913 my $tip = splice(@$rootset, $idx, 1);
914 @$rootset = reverse @$rootset;
915 unshift @$rootset, $tip;
916 $ctx->{sl_note} = strict_loose_note($nr);
924 require PublicInbox::ExtMsg;
925 PublicInbox::ExtMsg::ext_msg($ctx);
928 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
931 my ($prev_subj, $subj, $val) = @_;
933 my $omit = ''; # '"' denotes identical text omitted
934 my (@prev_pop, @curr_pop);
935 while (@$prev_subj && @$subj && $subj->[-1] eq $prev_subj->[-1]) {
936 push(@prev_pop, pop(@$prev_subj));
937 push(@curr_pop, pop(@$subj));
940 pop @$subj if @$subj && $subj->[-1] =~ /^re:\s*/i;
941 if (scalar(@curr_pop) == 1) {
943 push @$prev_subj, @prev_pop;
944 push @$subj, @curr_pop;
950 my ($ctx, $level, $node) = @_;
951 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
953 my $dst = $ctx->{dst};
954 my $cur = $ctx->{cur};
955 my $mid = $smsg->{mid};
957 if ($level == 0 && $ctx->{skel_dump_roots}++) {
958 $$dst .= delete $ctx->{sl_note} || '';
961 my $f = ascii_html($smsg->from_name);
962 my $obfs_ibx = $ctx->{-obfs_ibx};
963 obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
965 my $d = fmt_ts($smsg->{ds});
966 my $unmatched; # if lazy-loaded by SearchThread::Msg::visible()
967 if (exists $ctx->{searchview}) {
968 if (defined(my $pct = $smsg->{pct})) {
969 $d .= (sprintf(' % 2u', $pct) . '%');
975 $d .= ' ' . indent_for($level) . th_pfx($level);
977 $ctx->{first_level} ||= $level;
979 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
980 $ctx->{prev_attr} = $attr;
982 $ctx->{prev_level} = $level;
987 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
988 "$attr [this message]</a></b>\n";
991 $ctx->{prev_msg} = $mid;
994 $ctx->{next_msg} ||= $mid;
997 # Subject is never undef, this mail was loaded from
998 # our Xapian which would've resulted in '' if it were
999 # really missing (and Filter rejects empty subjects)
1000 my @subj = split(/ /, subject_normalized($smsg->subject));
1001 # remove common suffixes from the subject if it matches the previous,
1002 # so we do not show redundant text at the end.
1003 my $prev_subj = $ctx->{prev_subj} || [];
1004 $ctx->{prev_subj} = [ @subj ];
1005 my $omit = dedupe_subject($prev_subj, \@subj, '" ');
1008 my $subj = join(' ', @subj);
1009 $subj = ascii_html($subj);
1010 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
1011 $end = "$subj</a> $omit$f\n"
1017 my $mapping = $unmatched ? undef : $ctx->{mapping};
1019 my $map = $mapping->{$mid};
1020 $id = id_compress($mid, 1);
1022 $map->[0] = "$d<a\nhref=\"$m\">$end";
1025 $m = $ctx->{-upfx}.mid_escape($mid).'/';
1027 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
1032 my ($ctx, $level, $node) = @_;
1034 my $mid = $node->{id};
1035 my $d = ' [not found] ';
1036 $d .= ' ' if exists $ctx->{searchview};
1037 $d .= indent_for($level) . th_pfx($level);
1038 my $upfx = $ctx->{-upfx};
1039 my $m = PublicInbox::Hval->new_msgid($mid);
1040 my $href = $upfx . $m->{href} . '/';
1041 my $html = $m->as_html;
1043 my $mapping = $ctx->{mapping};
1044 my $map = $mapping->{$mid} if $mapping;
1046 my $id = id_compress($mid, 1);
1047 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
1048 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
1050 $d .= qq{<<a\nhref="$href">$html</a>>\n};
1052 my $dst = $ctx->{dst};
1059 (eval { $a->topmost->{smsg}->ds } || 0) <=>
1060 (eval { $b->topmost->{smsg}->ds } || 0)
1064 # accumulate recent topics if search is supported
1065 # returns 200 if done, 404 if not
1067 my ($ctx, $level, $node) = @_;
1068 my $mid = $node->{id};
1069 my $x = $node->{smsg} || $ctx->{-inbox}->smsg_by_mid($mid);
1073 $subj = $x->subject;
1074 $subj = subject_normalized($subj);
1075 $subj = '(no subject)' if $subj eq '';
1078 $topic = [ $ds, 1, { $subj => $mid }, $subj ];
1079 $ctx->{-cur_topic} = $topic;
1080 push @{$ctx->{order}}, $topic;
1084 $topic = $ctx->{-cur_topic}; # should never be undef
1085 $topic->[0] = $ds if $ds > $topic->[0];
1087 my $seen = $topic->[2];
1088 if (scalar(@$topic) == 3) { # parent was a ghost
1089 push @$topic, $subj;
1090 } elsif (!$seen->{$subj}) {
1091 push @$topic, $level, $subj;
1093 $seen->{$subj} = $mid; # latest for subject
1094 } else { # ghost message
1095 return 1 if $level != 0; # ignore child ghosts
1096 $topic = [ -666, 0, {} ];
1097 $ctx->{-cur_topic} = $topic;
1098 push @{$ctx->{order}}, $topic;
1105 my $order = delete $ctx->{order}; # [ ds, subj1, subj2, subj3, ... ]
1107 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
1112 my $ibx = $ctx->{-inbox};
1113 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
1115 # sort by recency, this allows new posts to "bump" old topics...
1116 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
1117 my ($ds, $n, $seen, $top, @ex) = @$topic;
1119 next unless defined $top; # ghost topic
1120 my $mid = delete $seen->{$top};
1121 my $href = mid_escape($mid);
1122 my $prev_subj = [ split(/ /, $top) ];
1123 $top = PublicInbox::Hval->new($top)->as_html;
1126 # $n isn't the total number of posts on the topic,
1127 # just the number of posts in the current results window
1131 $anchor = '#u'; # top of only message
1133 $n = " ($n+ messages)";
1134 $anchor = '#t'; # thread skeleton
1137 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
1138 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
1139 my $s = "<a\nhref=\"$href/T/$anchor\">$top</a>\n" .
1140 " $ds UTC $n - $mbox / $atom\n";
1141 for (my $i = 0; $i < scalar(@ex); $i += 2) {
1142 my $level = $ex[$i];
1143 my $subj = $ex[$i + 1];
1144 $mid = delete $seen->{$subj};
1145 my @subj = split(/ /, subject_normalized($subj));
1146 my @next_prev = @subj; # full copy
1147 my $omit = dedupe_subject($prev_subj, \@subj, ' "');
1148 $prev_subj = \@next_prev;
1149 $subj = ascii_html(join(' ', @subj));
1150 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
1151 $href = mid_escape($mid);
1152 $s .= indent_for($level) . TCHILD;
1153 $s .= "<a\nhref=\"$href/T/#u\">$subj</a>$omit\n";
1157 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
1163 POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts));
1167 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]);
1168 timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
1171 sub pagination_footer ($$) {
1172 my ($ctx, $latest) = @_;
1173 delete $ctx->{qp} or return;
1174 my $next = $ctx->{next_page} || '';
1175 my $prev = $ctx->{prev_page} || '';
1177 $next = $next ? "$next " : ' ';
1178 $prev .= qq! <a\nhref='$latest'>latest</a>!;
1180 "<hr><pre>page: $next$prev</pre>";
1183 sub index_nav { # callback for WwwStream
1184 my (undef, $ctx) = @_;
1185 pagination_footer($ctx, '.')
1188 sub paginate_recent ($$) {
1189 my ($ctx, $lim) = @_;
1190 my $t = $ctx->{qp}->{t} || '';
1191 my $opts = { limit => $lim };
1192 my ($after, $before);
1194 # Xapian uses '..' but '-' is perhaps friendier to URL linkifiers
1195 # if only $after exists "YYYYMMDD.." because "." could be skipped
1196 # if interpreted as an end-of-sentence
1197 $t =~ s/\A([0-9]{8,14})-// and $after = str2ts($1);
1198 $t =~ /\A([0-9]{8,14})\z/ and $before = str2ts($1);
1200 my $ibx = $ctx->{-inbox};
1201 my $msgs = $ibx->recent($opts, $after, $before);
1202 my $nr = scalar @$msgs;
1203 if ($nr < $lim && defined($after)) {
1204 $after = $before = undef;
1205 $msgs = $ibx->recent($opts);
1206 $nr = scalar @$msgs;
1208 my $more = $nr == $lim;
1209 my ($newest, $oldest);
1211 $newest = $msgs->[0]->{ts};
1212 $oldest = $msgs->[-1]->{ts};
1213 # if we only had $after, our SQL query in ->recent ordered
1214 if ($newest < $oldest) {
1215 ($oldest, $newest) = ($newest, $oldest);
1216 $more = 0 if defined($after) && $after < $oldest;
1219 if (defined($oldest) && $more) {
1220 my $s = ts2str($oldest);
1221 $ctx->{next_page} = qq!<a\nhref="?t=$s"\nrel=next>next</a>!;
1223 if (defined($newest) && (defined($before) || defined($after))) {
1224 my $s = ts2str($newest);
1225 $ctx->{prev_page} = qq!<a\nhref="?t=$s-"\nrel=prev>prev</a>!;
1232 my $msgs = paginate_recent($ctx, 200); # 200 is our window
1234 walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic);
1236 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
1239 sub thread_adj_level {
1240 my ($ctx, $level) = @_;
1242 my $max = $ctx->{cur_level};
1244 return ('', '') if $max == 0; # flat output
1246 # reset existing lists
1247 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
1248 $ctx->{cur_level} = 0;
1250 } elsif ($level == $max) { # continue existing list
1252 } elsif ($level < $max) {
1253 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1254 $ctx->{cur_level} = $level;
1255 ("$beg<li>", '</li>');
1256 } else { # ($level > $max) # start a new level
1257 $ctx->{cur_level} = $level;
1258 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1263 sub ghost_index_entry {
1264 my ($ctx, $level, $node) = @_;
1265 my ($beg, $end) = thread_adj_level($ctx, $level);
1266 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})