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 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 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 };
27 # public functions: (unstable)
30 my ($ctx, $mime, $more, $smsg) = @_;
31 my $hdr = $mime->header_obj;
32 my $ibx = $ctx->{-inbox};
33 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
34 my $tip = _msg_html_prepare($hdr, $ctx, $more, 0);
36 PublicInbox::WwwStream->response($ctx, 200, sub {
39 # $more cannot be true w/o $smsg being defined:
40 my $upfx = $more ? '../'.mid_escape($smsg->mid).'/' : '';
41 $tip . multipart_text_as_html($mime, $upfx, $ctx) .
43 } elsif ($more && @$more) {
45 msg_html_more($ctx, $more, $nr);
46 } elsif ($nr == $end) {
47 # fake an EOF if generating the footer fails;
48 # we want to at least show the message if something
51 '<pre>' . html_footer($hdr, 1, $ctx) .
52 '</pre>' . msg_reply($ctx, $hdr)
62 my $mid = $ctx->{mid};
63 my $ibx = $ctx->{-inbox};
66 if (my $over = $ibx->over) {
68 $smsg = $over->next_by_mid($mid, \$id, \$prev);
69 $first = $ibx->msg_by_smsg($smsg) if $smsg;
71 my $next = $over->next_by_mid($mid, \$id, \$prev);
72 $more = [ $id, $prev, $next ] if $next;
76 $first = $ibx->msg_by_mid($mid) or return;
78 msg_html($ctx, PublicInbox::MIME->new($first), $more, $smsg);
82 my ($ctx, $more, $nr) = @_;
84 my ($id, $prev, $smsg) = @$more;
85 my $mid = $ctx->{mid};
86 my $ibx = $ctx->{-inbox};
87 $smsg = $ibx->smsg_mime($smsg);
88 my $next = $ibx->over->next_by_mid($mid, \$id, \$prev);
89 @$more = $next ? ($id, $prev, $next) : ();
91 my $mime = $smsg->{mime};
92 my $upfx = '../' . mid_escape($smsg->mid) . '/';
93 _msg_html_prepare($mime->header_obj, $ctx, $more, $nr) .
94 multipart_text_as_html($mime, $upfx, $ctx) .
101 warn "Error lookup up additional messages: $@\n";
102 $str = '<pre>Error looking up additional messages</pre>';
107 # /$INBOX/$MESSAGE_ID/#R
109 my ($ctx, $hdr) = @_;
111 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
113 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
116 my $ibx = $ctx->{-inbox};
117 if (my $url = $ibx->{infourl}) {
118 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
119 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
122 my ($arg, $link, $reply_to_all) =
123 PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
124 if (ref($arg) eq 'SCALAR') {
125 return '<pre id=R>'.ascii_html($$arg).'</pre>';
128 # mailto: link only works if address obfuscation is disabled
132 * If your mail client supports setting the <b>In-Reply-To</b> header
133 via mailto: links, try the <a
134 href="$link">mailto: link</a>
138 push @$arg, '/path/to/YOUR_REPLY';
139 $arg = ascii_html(join(" \\\n ", '', @$arg));
142 id=R><b>Reply instructions:</b>
144 You may reply publically to <a
145 href=#t>this message</a> via plain-text email
146 using any one of the following methods:
148 * Save the following mbox file, import it into your mail client,
149 and $reply_to_all from there: <a
152 Avoid top-posting and favor interleaved quoting:
154 href="$p_url">$p_url</a>
156 * Reply using the <b>--to</b>, <b>--cc</b>, and <b>--in-reply-to</b>
157 switches of git-send-email(1):
162 href="$se_url">$se_url</a>
169 my $refs = references($hdr);
173 sub fold_addresses ($) {
174 return $_[0] if length($_[0]) <= COLS;
175 # try to fold on commas after non-word chars before $lim chars,
176 # Try to get the "," preceeded by ">" or ")", but avoid folding
177 # on the comma where somebody uses "Lastname, Firstname".
178 # We also try to keep the last and penultimate addresses in
179 # the list on the same line if possible, hence the extra \z
180 # Fall back to folding on spaces at $lim + 1 chars
181 my $lim = COLS - 8; # 8 = "\t" display width
182 my $too_long = $lim + 1;
183 $_[0] =~ s/\s*\z//s; # Email::Simple doesn't strip trailing spaces
185 ($_[0] =~ /(.{0,$lim}\W(?:,|\z)|
188 .{$too_long,}?)(?:\s|\z)/xgo));
191 sub _hdr_names_html ($$) {
192 my ($hdr, $field) = @_;
193 my $val = $hdr->header($field) or return '';
194 ascii_html(join(', ', PublicInbox::Address::names($val)));
198 my ($nr, $singular, $plural) = @_;
199 return "0 $plural" if $nr == 0;
200 $nr == 1 ? "$nr $singular" : "$nr $plural";
203 # this is already inside a <pre>
205 my ($smsg, $ctx, $more) = @_;
206 my $subj = $smsg->subject;
207 my $mid_raw = $smsg->mid;
208 my $id = id_compress($mid_raw, 1);
211 my $root_anchor = $ctx->{root_anchor} || '';
213 my $obfs_ibx = $ctx->{-obfs_ibx};
215 $subj = '(no subject)' if $subj eq '';
216 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
217 $subj = '<b>'.ascii_html($subj).'</b>';
218 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
219 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
221 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
223 my $ds = $smsg->ds; # for v1 non-Xapian/SQLite users
224 # deleting {mime} is critical to memory use,
225 # the rest of the fields saves about 400K as we iterate across 1K msgs
226 my ($mime) = delete @$smsg{qw(mime ds ts blob subject)};
228 my $hdr = $mime->header_obj;
229 my $from = _hdr_names_html($hdr, 'From');
230 obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
231 $rv .= "From: $from @ ".fmt_ts($ds)." UTC";
232 my $upfx = $ctx->{-upfx};
233 my $mhref = $upfx . mid_escape($mid_raw) . '/';
234 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
235 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
236 my $to = fold_addresses(_hdr_names_html($hdr, 'To'));
237 my $cc = fold_addresses(_hdr_names_html($hdr, 'Cc'));
238 my ($tlen, $clen) = (length($to), length($cc));
240 if (($tlen + $clen) > COLS) {
241 $to_cc .= ' To: '.$to."\n" if $tlen;
242 $to_cc .= ' Cc: '.$cc."\n" if $clen;
245 $to_cc .= ' To: '.$to;
246 $to_cc .= '; <b>+Cc:</b> '.$cc if $clen;
248 $to_cc .= ' Cc: '.$cc if $clen;
252 obfuscate_addrs($obfs_ibx, $to_cc) if $obfs_ibx;
255 my $mapping = $ctx->{mapping};
256 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
257 my $mirt = PublicInbox::Hval->new_msgid($irt);
258 my $href = $upfx . $mirt->{href}. '/';
259 my $html = $mirt->as_html;
260 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
264 # scan through all parts, looking for displayable text
265 my $ibx = $ctx->{-inbox};
266 msg_iter($mime, sub { $rv .= add_text_body($mhref, $ctx, $_[0]) });
269 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
270 "<a\nhref=\"$mhref\">permalink</a>" .
271 " <a\nhref=\"${mhref}raw\">raw</a>" .
272 " <a\nhref=\"${mhref}#R\">reply</a>";
275 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
276 $rv .= "\t[relevance $pct->{$mid_raw}%]";
279 my $nested = 'nested';
284 $flat = "<b>$flat</b>";
286 $nested = "<b>$nested</b>";
288 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
289 $rv .= "|<a\nhref=\"${mhref}t/#u\">$nested</a>]";
290 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
295 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
299 sub pad_link ($$;$) {
300 my ($mid, $level, $s) = @_;
302 my $id = id_compress($mid, 1);
303 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
307 my ($mid_raw, $irt, $id, $ctx) = @_;
309 my $mapping = $ctx->{mapping} or return $rv;
311 my $mid_map = $mapping->{$mid_raw};
313 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
314 my ($attr, $node, $idx, $level) = @$mid_map;
315 my $children = $node->{children};
316 my $nr_c = scalar @$children;
319 if (my $smsg = $node->{smsg}) {
320 # delete saves about 200KB on a 1K message thread
321 if (my $refs = delete $smsg->{references}) {
322 ($$irt) = ($refs =~ m/<([^>]+)>\z/);
325 my $irt_map = $mapping->{$$irt} if defined $$irt;
326 if (defined $irt_map) {
327 $siblings = $irt_map->[1]->{children};
328 $nr_s = scalar(@$siblings) - 1;
329 $rv .= $pad . $irt_map->[0];
331 my $prev = $siblings->[$idx - 1];
332 my $pmid = $prev->{id};
334 my $s = ($idx - 1). ' preceding siblings ...';
335 $rv .= pad_link($pmid, $level, $s);
336 } elsif ($idx == 2) {
337 my $ppmid = $siblings->[0]->{id};
338 $rv .= $pad . $mapping->{$ppmid}->[0];
340 $rv .= $pad . $mapping->{$pmid}->[0];
343 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
344 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
345 $attr =~ s!\n\z!</b>\n!s;
346 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
347 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
350 my $cmid = $children->[0]->{id};
351 $rv .= $pad . $mapping->{$cmid}->[0];
353 my $s = ($nr_c - 1). ' more replies';
354 $rv .= pad_link($cmid, $level + 1, $s);
355 } elsif (my $cn = $children->[1]) {
356 $rv .= $pad . $mapping->{$cn->{id}}->[0];
360 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
362 my $nmid = $next->{id};
363 $rv .= $pad . $mapping->{$nmid}->[0];
364 my $nnext = $nr_s - $idx;
366 my $s = ($nnext - 1).' subsequent siblings';
367 $rv .= pad_link($nmid, $level, $s);
368 } elsif (my $nn = $siblings->[$idx + 2]) {
369 $rv .= $pad . $mapping->{$nn->{id}}->[0];
372 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
376 my ($rootset, $ctx, $cb) = @_;
377 my @q = map { (0, $_, -1) } @$rootset;
379 my ($level, $node, $i) = splice(@q, 0, 3);
380 defined $node or next;
381 $cb->($ctx, $level, $node, $i) or return;
384 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
389 my ($ctx, $level, $node, $idx) = @_;
390 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
391 skel_dump($ctx, $level, $node);
394 sub thread_index_entry {
395 my ($ctx, $level, $smsg) = @_;
396 my ($beg, $end) = thread_adj_level($ctx, $level);
397 $beg . '<pre>' . index_entry($smsg, $ctx, 0) . '</pre>' . $end;
400 sub stream_thread ($$) {
401 my ($rootset, $ctx) = @_;
402 my $ibx = $ctx->{-inbox};
403 my @q = map { (0, $_) } @$rootset;
408 my $node = shift @q or next;
410 unshift @q, map { ($cl, $_) } @{$node->{children}};
411 $smsg = $ibx->smsg_mime($node->{smsg}) and last;
413 return missing_thread($ctx) unless $smsg;
415 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
416 $ctx->{-title_html} = ascii_html($smsg->subject);
417 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg);
419 PublicInbox::WwwStream->response($ctx, 200, sub {
423 my $node = shift @q or next;
425 unshift @q, map { ($cl, $_) } @{$node->{children}};
426 if ($smsg = $ibx->smsg_mime($node->{smsg})) {
427 return thread_index_entry($ctx, $level, $smsg);
429 return ghost_index_entry($ctx, $level, $node);
432 my $ret = join('', thread_adj_level($ctx, 0));
433 $ret .= ${$ctx->{dst}}; # skel
441 my $mid = $ctx->{mid};
442 my $ibx = $ctx->{-inbox};
443 my ($nr, $msgs) = $ibx->over->get_thread($mid);
444 return missing_thread($ctx) if $nr == 0;
445 my $skel = '<hr><pre>';
446 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
447 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
448 $skel .= "<b\nid=t>Thread overview:</b> ";
449 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
450 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
451 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
452 $skel .= "-- links below jump to the message on this page --\n";
453 $ctx->{-upfx} = '../../';
454 $ctx->{cur_level} = 0;
455 $ctx->{dst} = \$skel;
456 $ctx->{prev_attr} = '';
457 $ctx->{prev_level} = 0;
458 $ctx->{root_anchor} = anchor_for($mid);
459 $ctx->{mapping} = {};
460 $ctx->{s_nr} = ($nr > 1 ? "$nr+ messages" : 'only message')
463 my $rootset = thread_results($ctx, $msgs);
465 # reduce hash lookups in pre_thread->skel_dump
466 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
467 walk_thread($rootset, $ctx, *pre_thread);
470 return stream_thread($rootset, $ctx) unless $ctx->{flat};
472 # flat display: lazy load the full message from smsg
474 while (my $m = shift @$msgs) {
475 $smsg = $ibx->smsg_mime($m) and last;
477 return missing_thread($ctx) unless $smsg;
478 $ctx->{-title_html} = ascii_html($smsg->subject);
479 $ctx->{-html_tip} = '<pre>'.index_entry($smsg, $ctx, scalar @$msgs);
481 PublicInbox::WwwStream->response($ctx, 200, sub {
484 while (my $m = shift @$msgs) {
485 $smsg = $ibx->smsg_mime($m) and last;
487 return index_entry($smsg, $ctx, scalar @$msgs) if $smsg;
493 sub multipart_text_as_html {
494 my ($mime, $upfx, $ctx) = @_;
497 # scan through all parts, looking for displayable text
498 msg_iter($mime, sub { $rv .= add_text_body($upfx, $ctx, $_[0]) });
503 my ($s, $l, $quot) = @_;
505 # show everything in the full version with anchor from
506 # short version (see above)
507 my $rv = $l->linkify_1($$quot);
509 # we use a <span> here to allow users to specify their own
510 # color for quoted text
511 $rv = $l->linkify_2(ascii_html($rv));
513 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
516 sub attach_link ($$$$;$) {
517 my ($upfx, $ct, $p, $fn, $err) = @_;
518 my ($part, $depth, @idx) = @$p;
519 my $nl = $idx[-1] > 1 ? "\n" : '';
520 my $idx = join('.', @idx);
521 my $size = bytes::length($part->body);
523 # hide attributes normally, unless we want to aid users in
524 # spotting MUA problems:
525 $ct =~ s/;.*// unless $err;
526 $ct = ascii_html($ct);
527 my $desc = $part->header('Content-Description');
528 $desc = $fn unless defined $desc;
529 $desc = '' unless defined $desc;
531 if (defined $fn && $fn =~ /\A$PublicInbox::Hval::FN\z/o) {
533 } elsif ($ct eq 'text/plain') {
538 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
541 "[-- Warning: decoded text below may be mangled --]\n";
543 $ret .= "[-- Attachment #$idx: ";
544 my $ts = "Type: $ct, Size: $size bytes";
545 $desc = ascii_html($desc);
546 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
551 my ($upfx, $ctx, $p) = @_;
552 my $ibx = $ctx->{-inbox};
553 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
554 # $p - from msg_iter: [ Email::MIME, depth, @idx ]
555 my ($part, $depth, @idx) = @$p;
556 my $ct = $part->content_type || 'text/plain';
557 my $fn = $part->filename;
558 my ($s, $err) = msg_part_text($part, $ct);
560 return attach_link($upfx, $ct, $p, $fn) unless defined $s;
562 # makes no difference to browsers, and don't screw up filename
563 # link generation in diffs with the extra '%0D'
566 # always support diff-highlighting, but we can't linkify hunk
567 # headers for solver unless some coderepo are configured:
569 if ($s =~ /^(?:diff|---|\+{3}) /ms) {
570 # diffstat anchors do not link across attachments or messages:
571 $idx[0] = $upfx . $idx[0] if $upfx ne '';
572 $ctx->{-apfx} = join('/', @idx);
573 $ctx->{-anchors} = {}; # attr => filename
574 $ctx->{-diff} = $diff = [];
575 delete $ctx->{-long_path};
577 if ($ibx->{-repo_objs}) {
578 if (index($upfx, '//') >= 0) { # absolute URL (Atom feeds)
580 $spfx =~ s!/([^/]*)/\z!/!;
582 my $n_slash = $upfx =~ tr!/!/!;
585 } elsif ($n_slash == 1) {
587 } else { # nslash == 2
592 $ctx->{-spfx} = $spfx;
595 # some editors don't put trailing newlines at the end:
596 $s .= "\n" unless $s =~ /\n\z/s;
598 # split off quoted and unquoted blocks:
599 my @sections = split(/((?:^>[^\n]*\n)+)/sm, $s);
601 if (defined($fn) || $depth > 0 || $err) {
602 # badly-encoded message with $err? tell the world about it!
603 $s .= attach_link($upfx, $ct, $p, $fn, $err);
606 my $l = PublicInbox::Linkify->new;
607 foreach my $cur (@sections) {
609 flush_quote(\$s, $l, \$cur);
611 @$diff = split(/^/m, $cur);
613 flush_diff(\$s, $ctx, $l);
617 $s .= $l->linkify_2(ascii_html($cur));
622 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
626 sub _msg_html_prepare {
627 my ($hdr, $ctx, $more, $nr) = @_;
629 my $over = $ctx->{-inbox}->over;
630 my $obfs_ibx = $ctx->{-obfs_ibx};
632 my $mids = mids($hdr);
633 my $multiple = scalar(@$mids) > 1; # zero, one, infinity
637 "<pre>WARNING: multiple messages refer to this Message-ID\n</pre>";
639 $rv .= "<pre\nid=b>"; # anchor for body start
644 $ctx->{-upfx} = '../';
648 if (defined($v = $hdr->header('From'))) {
649 $v = PublicInbox::Hval->new($v);
650 my @n = PublicInbox::Address::names($v->raw);
651 $title[1] = ascii_html(join(', ', @n));
654 obfuscate_addrs($obfs_ibx, $v);
655 obfuscate_addrs($obfs_ibx, $title[1]);
657 $rv .= "From: $v\n" if $v ne '';
659 foreach my $h (qw(To Cc)) {
660 defined($v = $hdr->header($h)) or next;
663 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
664 $rv .= "$h: $v\n" if $v ne '';
666 if (defined($v = $hdr->header('Subject')) && ($v ne '')) {
668 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
670 $rv .= qq(Subject: <a\nhref="#r"\nid=t>$v</a>\n);
672 $rv .= "Subject: $v\n";
675 } else { # dummy anchor for thread skeleton at bottom of page
676 $rv .= qq(<a\nhref="#r"\nid=t></a>) if $over;
677 $title[0] = '(no subject)';
679 if (defined($v = $hdr->header('Date'))) {
681 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; # possible :P
684 $ctx->{-title_html} = join(' - ', @title);
686 my $mid = PublicInbox::Hval->new_msgid($_) ;
687 my $mhtml = $mid->as_html;
689 my $href = $mid->{href};
690 $rv .= "Message-ID: ";
691 $rv .= "<a\nhref=\"../$href/\">";
692 $rv .= "<$mhtml></a> ";
693 $rv .= "(<a\nhref=\"../$href/raw\">raw</a>)\n";
695 $rv .= "Message-ID: <$mhtml> ";
696 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
699 $rv .= _parent_headers($hdr, $over);
704 my ($dst, $ctx, $hdr, $tpfx) = @_;
705 my $mid = mids($hdr)->[0];
706 my $ibx = $ctx->{-inbox};
707 my ($nr, $msgs) = $ibx->over->get_thread($mid);
708 my $expand = qq(expand[<a\nhref="${tpfx}T/#u">flat</a>) .
709 qq(|<a\nhref="${tpfx}t/#u">nested</a>] ) .
710 qq(<a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
711 qq(<a\nhref="${tpfx}t.atom">Atom feed</a>);
713 my $parent = in_reply_to($hdr);
714 $$dst .= "\n<b>Thread overview: </b>";
716 if (defined $parent) {
717 $$dst .= "$expand\n ";
718 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
720 $$dst .= "[no followups] $expand\n";
722 $ctx->{next_msg} = undef;
723 $ctx->{parent_msg} = $parent;
727 $$dst .= "$nr+ messages / $expand";
728 $$dst .= qq! <a\nhref="#b">top</a>\n!;
730 my $subj = $hdr->header('Subject');
731 defined $subj or $subj = '';
732 $subj = '(no subject)' if $subj eq '';
733 $ctx->{prev_subj} = [ split(/ /, subject_normalized($subj)) ];
735 $ctx->{prev_attr} = '';
736 $ctx->{prev_level} = 0;
739 # reduce hash lookups in skel_dump
740 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
741 walk_thread(thread_results($ctx, $msgs), $ctx, *skel_dump);
743 $ctx->{parent_msg} = $parent;
746 sub _parent_headers {
747 my ($hdr, $over) = @_;
750 my $refs = references($hdr);
751 my $irt = pop @$refs;
753 my $v = PublicInbox::Hval->new_msgid($irt);
754 my $html = $v->as_html;
755 my $href = $v->{href};
756 $rv .= "In-Reply-To: <";
757 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
760 # do not display References: if search is present,
761 # we show the thread skeleton at the bottom, instead.
765 @$refs = map { linkify_ref_no_over($_) } @$refs;
766 $rv .= 'References: '. join("\n\t", @$refs) . "\n";
772 my ($hdr, $standalone, $ctx, $rhref) = @_;
774 my $ibx = $ctx->{-inbox} if $ctx;
777 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
779 if ($idx && $ibx->over) {
781 thread_skel(\$idx, $ctx, $hdr, $tpfx);
786 if (my $n = $ctx->{next_msg}) {
787 $n = PublicInbox::Hval->new_msgid($n)->{href};
788 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
791 my $par = $ctx->{parent_msg};
793 $u = PublicInbox::Hval->new_msgid($par)->{href};
796 if (my $p = $ctx->{prev_msg}) {
797 $prev = PublicInbox::Hval->new_msgid($p)->{href};
798 if ($p && $par && $p eq $par) {
799 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
800 'rel=prev>prev parent</a>';
803 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
805 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
807 } elsif ($u) { # unlikely
808 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
810 $irt = "$next $prev$parent ";
815 $irt .= qq(<a\nhref="$rhref">reply</a>);
819 sub linkify_ref_no_over {
820 my $v = PublicInbox::Hval->new_msgid($_[0]);
821 my $html = $v->as_html;
822 my $href = $v->{href};
823 "<<a\nhref=\"../$href/\">$html</a>>";
828 'm' . id_compress($msgid, 1);
832 my ($upfx, $mid) = @_;
834 $mid = PublicInbox::Hval->new_msgid($mid);
835 my $href = $mid->{href};
836 my $html = $mid->as_html;
837 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
842 $level ? INDENT x ($level - 1) : '';
846 my ($ctx, $level, $node, $idx) = @_;
847 ++$ctx->{root_idx} if $level == 0;
848 if ($node->{id} eq $ctx->{mid}) {
849 $ctx->{found_mid_at} = $ctx->{root_idx};
855 sub strict_loose_note ($) {
858 " -- strict thread matches above, loose matches on Subject: below --\n";
860 if ($nr > PublicInbox::Over::DEFAULT_LIMIT()) {
862 " -- use mbox.gz link to download all $nr messages --\n";
868 my ($ctx, $msgs) = @_;
869 require PublicInbox::SearchThread;
870 my $ibx = $ctx->{-inbox};
871 my $rootset = PublicInbox::SearchThread::thread($msgs, *sort_ds, $ibx);
873 # FIXME: `tid' is broken on --reindex, so that needs to be fixed
874 # and preserved in the future. This bug is hidden by `sid' matches
875 # in get_thread, so we never noticed it until now. And even when
876 # reindexing is fixed, we'll keep this code until a SCHEMA_VERSION
877 # bump since reindexing is expensive and users may not do it
879 # loose threading could've returned too many results,
880 # put the root the message we care about at the top:
881 my $mid = $ctx->{mid};
882 if (defined($mid) && scalar(@$rootset) > 1) {
883 $ctx->{root_idx} = -1;
884 my $nr = scalar @$msgs;
885 walk_thread($rootset, $ctx, *find_mid_root);
886 my $idx = $ctx->{found_mid_at};
887 if (defined($idx) && $idx != 0) {
888 my $tip = splice(@$rootset, $idx, 1);
889 @$rootset = reverse @$rootset;
890 unshift @$rootset, $tip;
891 $ctx->{sl_note} = strict_loose_note($nr);
899 require PublicInbox::ExtMsg;
900 PublicInbox::ExtMsg::ext_msg($ctx);
903 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
906 my ($prev_subj, $subj, $val) = @_;
908 my $omit = ''; # '"' denotes identical text omitted
909 my (@prev_pop, @curr_pop);
910 while (@$prev_subj && @$subj && $subj->[-1] eq $prev_subj->[-1]) {
911 push(@prev_pop, pop(@$prev_subj));
912 push(@curr_pop, pop(@$subj));
915 pop @$subj if @$subj && $subj->[-1] =~ /^re:\s*/i;
916 if (scalar(@curr_pop) == 1) {
918 push @$prev_subj, @prev_pop;
919 push @$subj, @curr_pop;
925 my ($ctx, $level, $node) = @_;
926 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
928 my $dst = $ctx->{dst};
929 my $cur = $ctx->{cur};
930 my $mid = $smsg->{mid};
932 if ($level == 0 && $ctx->{skel_dump_roots}++) {
933 $$dst .= delete $ctx->{sl_note} || '';
936 my $f = ascii_html($smsg->from_name);
937 my $obfs_ibx = $ctx->{-obfs_ibx};
938 obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
940 my $d = fmt_ts($smsg->{ds}) . ' ' . indent_for($level) . th_pfx($level);
942 $ctx->{first_level} ||= $level;
944 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
945 $ctx->{prev_attr} = $attr;
947 $ctx->{prev_level} = $level;
952 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
953 "$attr [this message]</a></b>\n";
956 $ctx->{prev_msg} = $mid;
959 $ctx->{next_msg} ||= $mid;
962 # Subject is never undef, this mail was loaded from
963 # our Xapian which would've resulted in '' if it were
964 # really missing (and Filter rejects empty subjects)
965 my @subj = split(/ /, subject_normalized($smsg->subject));
967 # remove common suffixes from the subject if it matches the previous,
968 # so we do not show redundant text at the end.
969 my $prev_subj = $ctx->{prev_subj} || [];
970 $ctx->{prev_subj} = [ @subj ];
971 my $omit = dedupe_subject($prev_subj, \@subj, '" ');
974 my $subj = join(' ', @subj);
975 $subj = ascii_html($subj);
976 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
977 $end = "$subj</a> $omit$f\n"
983 my $mapping = $ctx->{mapping};
985 my $map = $mapping->{$mid};
986 $id = id_compress($mid, 1);
988 $map->[0] = "$d<a\nhref=\"$m\">$end";
991 $m = $ctx->{-upfx}.mid_escape($mid).'/';
993 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
998 my ($ctx, $level, $node) = @_;
1000 my $mid = $node->{id};
1001 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
1003 $d .= indent_for($level) . th_pfx($level);
1004 my $upfx = $ctx->{-upfx};
1005 my $m = PublicInbox::Hval->new_msgid($mid);
1006 my $href = $upfx . $m->{href} . '/';
1007 my $html = $m->as_html;
1009 my $mapping = $ctx->{mapping};
1010 my $map = $mapping->{$mid} if $mapping;
1012 my $id = id_compress($mid, 1);
1013 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
1014 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
1016 $d .= qq{<<a\nhref="$href">$html</a>>\n};
1018 my $dst = $ctx->{dst};
1025 (eval { $a->topmost->{smsg}->ds } || 0) <=>
1026 (eval { $b->topmost->{smsg}->ds } || 0)
1030 # accumulate recent topics if search is supported
1031 # returns 200 if done, 404 if not
1033 my ($ctx, $level, $node) = @_;
1034 my $mid = $node->{id};
1035 my $x = $node->{smsg} || $ctx->{-inbox}->smsg_by_mid($mid);
1039 $subj = $x->subject;
1040 $subj = subject_normalized($subj);
1041 $subj = '(no subject)' if $subj eq '';
1044 $topic = [ $ds, 1, { $subj => $mid }, $subj ];
1045 $ctx->{-cur_topic} = $topic;
1046 push @{$ctx->{order}}, $topic;
1050 $topic = $ctx->{-cur_topic}; # should never be undef
1051 $topic->[0] = $ds if $ds > $topic->[0];
1053 my $seen = $topic->[2];
1054 if (scalar(@$topic) == 3) { # parent was a ghost
1055 push @$topic, $subj;
1056 } elsif (!$seen->{$subj}) {
1057 push @$topic, $level, $subj;
1059 $seen->{$subj} = $mid; # latest for subject
1060 } else { # ghost message
1061 return 1 if $level != 0; # ignore child ghosts
1062 $topic = [ -666, 0, {} ];
1063 $ctx->{-cur_topic} = $topic;
1064 push @{$ctx->{order}}, $topic;
1071 my $order = delete $ctx->{order}; # [ ds, subj1, subj2, subj3, ... ]
1073 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
1078 my $ibx = $ctx->{-inbox};
1079 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
1081 # sort by recency, this allows new posts to "bump" old topics...
1082 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
1083 my ($ds, $n, $seen, $top, @ex) = @$topic;
1085 next unless defined $top; # ghost topic
1086 my $mid = delete $seen->{$top};
1087 my $href = mid_escape($mid);
1088 my $prev_subj = [ split(/ /, $top) ];
1089 $top = PublicInbox::Hval->new($top)->as_html;
1092 # $n isn't the total number of posts on the topic,
1093 # just the number of posts in the current results window
1097 $anchor = '#u'; # top of only message
1099 $n = " ($n+ messages)";
1100 $anchor = '#t'; # thread skeleton
1103 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
1104 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
1105 my $s = "<a\nhref=\"$href/T/$anchor\">$top</a>\n" .
1106 " $ds UTC $n - $mbox / $atom\n";
1107 for (my $i = 0; $i < scalar(@ex); $i += 2) {
1108 my $level = $ex[$i];
1109 my $subj = $ex[$i + 1];
1110 $mid = delete $seen->{$subj};
1111 my @subj = split(/ /, subject_normalized($subj));
1112 my @next_prev = @subj; # full copy
1113 my $omit = dedupe_subject($prev_subj, \@subj, ' "');
1114 $prev_subj = \@next_prev;
1115 $subj = ascii_html(join(' ', @subj));
1116 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
1117 $href = mid_escape($mid);
1118 $s .= indent_for($level) . TCHILD;
1119 $s .= "<a\nhref=\"$href/T/#u\">$subj</a>$omit\n";
1123 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
1129 POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts));
1133 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]);
1134 timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
1137 sub pagination_footer ($$) {
1138 my ($ctx, $latest) = @_;
1139 delete $ctx->{qp} or return;
1140 my $next = $ctx->{next_page} || '';
1141 my $prev = $ctx->{prev_page} || '';
1143 $next = $next ? "$next " : ' ';
1144 $prev .= qq! <a\nhref='$latest'>latest</a>!;
1146 "<hr><pre>page: $next$prev</pre>";
1149 sub index_nav { # callback for WwwStream
1150 my (undef, $ctx) = @_;
1151 pagination_footer($ctx, '.')
1154 sub paginate_recent ($$) {
1155 my ($ctx, $lim) = @_;
1156 my $t = $ctx->{qp}->{t} || '';
1157 my $opts = { limit => $lim };
1158 my ($after, $before);
1160 # Xapian uses '..' but '-' is perhaps friendier to URL linkifiers
1161 # if only $after exists "YYYYMMDD.." because "." could be skipped
1162 # if interpreted as an end-of-sentence
1163 $t =~ s/\A([0-9]{8,14})-// and $after = str2ts($1);
1164 $t =~ /\A([0-9]{8,14})\z/ and $before = str2ts($1);
1166 my $ibx = $ctx->{-inbox};
1167 my $msgs = $ibx->recent($opts, $after, $before);
1168 my $nr = scalar @$msgs;
1169 if ($nr < $lim && defined($after)) {
1170 $after = $before = undef;
1171 $msgs = $ibx->recent($opts);
1172 $nr = scalar @$msgs;
1174 my $more = $nr == $lim;
1175 my ($newest, $oldest);
1177 $newest = $msgs->[0]->{ts};
1178 $oldest = $msgs->[-1]->{ts};
1179 # if we only had $after, our SQL query in ->recent ordered
1180 if ($newest < $oldest) {
1181 ($oldest, $newest) = ($newest, $oldest);
1182 $more = 0 if defined($after) && $after < $oldest;
1185 if (defined($oldest) && $more) {
1186 my $s = ts2str($oldest);
1187 $ctx->{next_page} = qq!<a\nhref="?t=$s"\nrel=next>next</a>!;
1189 if (defined($newest) && (defined($before) || defined($after))) {
1190 my $s = ts2str($newest);
1191 $ctx->{prev_page} = qq!<a\nhref="?t=$s-"\nrel=prev>prev</a>!;
1198 my $msgs = paginate_recent($ctx, 200); # 200 is our window
1200 walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic);
1202 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
1205 sub thread_adj_level {
1206 my ($ctx, $level) = @_;
1208 my $max = $ctx->{cur_level};
1210 return ('', '') if $max == 0; # flat output
1212 # reset existing lists
1213 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
1214 $ctx->{cur_level} = 0;
1216 } elsif ($level == $max) { # continue existing list
1218 } elsif ($level < $max) {
1219 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1220 $ctx->{cur_level} = $level;
1221 ("$beg<li>", '</li>');
1222 } else { # ($level > $max) # start a new level
1223 $ctx->{cur_level} = $level;
1224 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1229 sub ghost_index_entry {
1230 my ($ctx, $level, $node) = @_;
1231 my ($beg, $end) = thread_adj_level($ctx, $level);
1232 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})