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 COLS => 72;
21 use constant INDENT => ' ';
22 use constant TCHILD => '` ';
23 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
25 # public functions: (unstable)
28 my ($ctx, $mime, $more, $smsg) = @_;
29 my $hdr = $mime->header_obj;
30 my $ibx = $ctx->{-inbox};
31 my $obfs_ibx = $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
32 my $tip = _msg_html_prepare($hdr, $ctx, $more, 0);
34 PublicInbox::WwwStream->response($ctx, 200, sub {
37 # $more cannot be true w/o $smsg being defined:
38 my $upfx = $more ? '../'.mid_escape($smsg->mid).'/' : '';
39 $tip . multipart_text_as_html($mime, $upfx, $obfs_ibx) .
41 } elsif ($more && @$more) {
43 msg_html_more($ctx, $more, $nr);
44 } elsif ($nr == $end) {
45 # fake an EOF if generating the footer fails;
46 # we want to at least show the message if something
49 '<pre>' . html_footer($hdr, 1, $ctx) .
50 '</pre>' . msg_reply($ctx, $hdr)
60 my $mid = $ctx->{mid};
61 my $ibx = $ctx->{-inbox};
64 if (my $srch = $ibx->search) {
66 $smsg = $srch->next_by_mid($mid, \$id, \$prev);
67 $first = $ibx->msg_by_smsg($smsg) if $smsg;
69 my $next = $srch->next_by_mid($mid, \$id, \$prev);
70 $more = [ $id, $prev, $next ] if $next;
74 $first = $ibx->msg_by_mid($mid) or return;
76 msg_html($ctx, PublicInbox::MIME->new($first), $more, $smsg);
80 my ($ctx, $more, $nr) = @_;
82 my ($id, $prev, $smsg) = @$more;
83 my $mid = $ctx->{mid};
84 $smsg = $ctx->{-inbox}->smsg_mime($smsg);
85 my $next = $ctx->{srch}->next_by_mid($mid, \$id, \$prev);
86 @$more = $next ? ($id, $prev, $next) : ();
88 my $mime = $smsg->{mime};
89 my $upfx = '../' . mid_escape($smsg->mid) . '/';
90 _msg_html_prepare($mime->header_obj, $ctx, $more, $nr) .
91 multipart_text_as_html($mime, $upfx,
99 warn "Error lookup up additional messages: $@\n";
100 $str = '<pre>Error looking up additional messages</pre>';
105 # /$INBOX/$MESSAGE_ID/#R
107 my ($ctx, $hdr) = @_;
109 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
111 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
114 my $ibx = $ctx->{-inbox};
115 if (my $url = $ibx->{infourl}) {
116 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
117 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
120 my ($arg, $link, $reply_to_all) =
121 PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
122 if (ref($arg) eq 'SCALAR') {
123 return '<pre id=R>'.ascii_html($$arg).'</pre>';
126 # mailto: link only works if address obfuscation is disabled
130 * If your mail client supports setting the <b>In-Reply-To</b> header
131 via mailto: links, try the <a
132 href="$link">mailto: link</a>
136 push @$arg, '/path/to/YOUR_REPLY';
137 $arg = ascii_html(join(" \\\n ", '', @$arg));
140 id=R><b>Reply instructions:</b>
142 You may reply publically to <a
143 href=#t>this message</a> via plain-text email
144 using any one of the following methods:
146 * Save the following mbox file, import it into your mail client,
147 and $reply_to_all from there: <a
150 Avoid top-posting and favor interleaved quoting:
152 href="$p_url">$p_url</a>
154 * Reply using the <b>--to</b>, <b>--cc</b>, and <b>--in-reply-to</b>
155 switches of git-send-email(1):
160 href="$se_url">$se_url</a>
167 my $refs = references($hdr);
171 sub fold_addresses ($) {
172 return $_[0] if length($_[0]) <= COLS;
173 # try to fold on commas after non-word chars before $lim chars,
174 # Try to get the "," preceeded by ">" or ")", but avoid folding
175 # on the comma where somebody uses "Lastname, Firstname".
176 # We also try to keep the last and penultimate addresses in
177 # the list on the same line if possible, hence the extra \z
178 # Fall back to folding on spaces at $lim + 1 chars
179 my $lim = COLS - 8; # 8 = "\t" display width
180 my $too_long = $lim + 1;
181 $_[0] =~ s/\s*\z//s; # Email::Simple doesn't strip trailing spaces
183 ($_[0] =~ /(.{0,$lim}\W(?:,|\z)|
186 .{$too_long,}?)(?:\s|\z)/xgo));
189 sub _hdr_names_html ($$) {
190 my ($hdr, $field) = @_;
191 my $val = $hdr->header($field) or return '';
192 ascii_html(join(', ', PublicInbox::Address::names($val)));
196 my ($nr, $singular, $plural) = @_;
197 return "0 $plural" if $nr == 0;
198 $nr == 1 ? "$nr $singular" : "$nr $plural";
201 # this is already inside a <pre>
203 my ($smsg, $ctx, $more) = @_;
204 my $srch = $ctx->{srch};
205 my $subj = $smsg->subject;
206 my $mid_raw = $smsg->mid;
207 my $id = id_compress($mid_raw, 1);
210 my $root_anchor = $ctx->{root_anchor} || '';
212 my $obfs_ibx = $ctx->{-obfs_ibx};
214 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
215 $subj = '<b>'.ascii_html($subj).'</b>';
216 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
217 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
219 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
221 my $ds = $smsg->ds; # for v1 non-Xapian/SQLite users
222 # deleting {mime} is critical to memory use,
223 # the rest of the fields saves about 400K as we iterate across 1K msgs
224 my ($mime) = delete @$smsg{qw(mime ds ts blob subject)};
226 my $hdr = $mime->header_obj;
227 my $from = _hdr_names_html($hdr, 'From');
228 obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
229 $rv .= "From: $from @ ".fmt_ts($ds)." UTC";
230 my $upfx = $ctx->{-upfx};
231 my $mhref = $upfx . mid_escape($mid_raw) . '/';
232 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
233 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
234 my $to = fold_addresses(_hdr_names_html($hdr, 'To'));
235 my $cc = fold_addresses(_hdr_names_html($hdr, 'Cc'));
236 my ($tlen, $clen) = (length($to), length($cc));
238 if (($tlen + $clen) > COLS) {
239 $to_cc .= ' To: '.$to."\n" if $tlen;
240 $to_cc .= ' Cc: '.$cc."\n" if $clen;
243 $to_cc .= ' To: '.$to;
244 $to_cc .= '; <b>+Cc:</b> '.$cc if $clen;
246 $to_cc .= ' Cc: '.$cc if $clen;
250 obfuscate_addrs($obfs_ibx, $to_cc) if $obfs_ibx;
253 my $mapping = $ctx->{mapping};
254 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
255 my $mirt = PublicInbox::Hval->new_msgid($irt);
256 my $href = $upfx . $mirt->{href}. '/';
257 my $html = $mirt->as_html;
258 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
262 # scan through all parts, looking for displayable text
263 msg_iter($mime, sub { $rv .= add_text_body($mhref, $obfs_ibx, $_[0]) });
266 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
267 "<a\nhref=\"$mhref\">permalink</a>" .
268 " <a\nhref=\"${mhref}raw\">raw</a>" .
269 " <a\nhref=\"${mhref}#R\">reply</a>";
272 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
273 $rv .= "\t[relevance $pct->{$mid_raw}%]";
276 my $nested = 'nested';
281 $flat = "<b>$flat</b>";
283 $nested = "<b>$nested</b>";
285 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
286 $rv .= "|<a\nhref=\"${mhref}t/#u\">$nested</a>]";
287 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
292 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
296 sub pad_link ($$;$) {
297 my ($mid, $level, $s) = @_;
299 my $id = id_compress($mid, 1);
300 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
304 my ($mid_raw, $irt, $id, $ctx) = @_;
306 my $mapping = $ctx->{mapping} or return $rv;
308 my $mid_map = $mapping->{$mid_raw};
310 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
311 my ($attr, $node, $idx, $level) = @$mid_map;
312 my $children = $node->{children};
313 my $nr_c = scalar @$children;
316 if (my $smsg = $node->{smsg}) {
317 # delete saves about 200KB on a 1K message thread
318 if (my $refs = delete $smsg->{references}) {
319 ($$irt) = ($refs =~ m/<([^>]+)>\z/);
322 my $irt_map = $mapping->{$$irt} if defined $$irt;
323 if (defined $irt_map) {
324 $siblings = $irt_map->[1]->{children};
325 $nr_s = scalar(@$siblings) - 1;
326 $rv .= $pad . $irt_map->[0];
328 my $prev = $siblings->[$idx - 1];
329 my $pmid = $prev->{id};
331 my $s = ($idx - 1). ' preceding siblings ...';
332 $rv .= pad_link($pmid, $level, $s);
333 } elsif ($idx == 2) {
334 my $ppmid = $siblings->[0]->{id};
335 $rv .= $pad . $mapping->{$ppmid}->[0];
337 $rv .= $pad . $mapping->{$pmid}->[0];
340 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
341 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
342 $attr =~ s!\n\z!</b>\n!s;
343 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
344 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
347 my $cmid = $children->[0]->{id};
348 $rv .= $pad . $mapping->{$cmid}->[0];
350 my $s = ($nr_c - 1). ' more replies';
351 $rv .= pad_link($cmid, $level + 1, $s);
352 } elsif (my $cn = $children->[1]) {
353 $rv .= $pad . $mapping->{$cn->{id}}->[0];
357 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
359 my $nmid = $next->{id};
360 $rv .= $pad . $mapping->{$nmid}->[0];
361 my $nnext = $nr_s - $idx;
363 my $s = ($nnext - 1).' subsequent siblings';
364 $rv .= pad_link($nmid, $level, $s);
365 } elsif (my $nn = $siblings->[$idx + 2]) {
366 $rv .= $pad . $mapping->{$nn->{id}}->[0];
369 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
373 my ($rootset, $ctx, $cb) = @_;
374 my @q = map { (0, $_, -1) } @$rootset;
376 my ($level, $node, $i) = splice(@q, 0, 3);
377 defined $node or next;
378 $cb->($ctx, $level, $node, $i) or return;
381 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
386 my ($ctx, $level, $node, $idx) = @_;
387 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
388 skel_dump($ctx, $level, $node);
391 sub thread_index_entry {
392 my ($ctx, $level, $smsg) = @_;
393 my ($beg, $end) = thread_adj_level($ctx, $level);
394 $beg . '<pre>' . index_entry($smsg, $ctx, 0) . '</pre>' . $end;
397 sub stream_thread ($$) {
398 my ($rootset, $ctx) = @_;
399 my $inbox = $ctx->{-inbox};
400 my @q = map { (0, $_) } @$rootset;
405 my $node = shift @q or next;
407 unshift @q, map { ($cl, $_) } @{$node->{children}};
408 $smsg = $inbox->smsg_mime($node->{smsg}) and last;
410 return missing_thread($ctx) unless $smsg;
412 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
413 $ctx->{-title_html} = ascii_html($smsg->subject);
414 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg);
416 PublicInbox::WwwStream->response($ctx, 200, sub {
420 my $node = shift @q or next;
422 unshift @q, map { ($cl, $_) } @{$node->{children}};
423 if ($smsg = $inbox->smsg_mime($node->{smsg})) {
424 return thread_index_entry($ctx, $level, $smsg);
426 return ghost_index_entry($ctx, $level, $node);
429 my $ret = join('', thread_adj_level($ctx, 0));
430 $ret .= ${$ctx->{dst}}; # skel
438 my $mid = $ctx->{mid};
439 my $srch = $ctx->{srch};
440 my ($nr, $msgs) = $srch->get_thread($mid);
441 return missing_thread($ctx) if $nr == 0;
442 my $skel = '<hr><pre>';
443 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
444 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
445 $skel .= "<b\nid=t>Thread overview:</b> ";
446 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
447 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
448 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
449 $skel .= "-- links below jump to the message on this page --\n";
450 $ctx->{-upfx} = '../../';
451 $ctx->{cur_level} = 0;
452 $ctx->{dst} = \$skel;
453 $ctx->{prev_attr} = '';
454 $ctx->{prev_level} = 0;
455 $ctx->{root_anchor} = anchor_for($mid);
456 $ctx->{mapping} = {};
457 $ctx->{s_nr} = "$nr+ messages in thread";
459 my $rootset = thread_results($ctx, $msgs);
461 # reduce hash lookups in pre_thread->skel_dump
462 my $inbox = $ctx->{-inbox};
463 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
464 walk_thread($rootset, $ctx, *pre_thread);
467 return stream_thread($rootset, $ctx) unless $ctx->{flat};
469 # flat display: lazy load the full message from smsg
471 while (my $m = shift @$msgs) {
472 $smsg = $inbox->smsg_mime($m) and last;
474 return missing_thread($ctx) unless $smsg;
475 $ctx->{-title_html} = ascii_html($smsg->subject);
476 $ctx->{-html_tip} = '<pre>'.index_entry($smsg, $ctx, scalar @$msgs);
478 PublicInbox::WwwStream->response($ctx, 200, sub {
481 while (my $m = shift @$msgs) {
482 $smsg = $inbox->smsg_mime($m) and last;
484 return index_entry($smsg, $ctx, scalar @$msgs) if $smsg;
490 sub multipart_text_as_html {
491 my ($mime, $upfx, $obfs_ibx) = @_;
494 # scan through all parts, looking for displayable text
495 msg_iter($mime, sub { $rv .= add_text_body($upfx, $obfs_ibx, $_[0]) });
500 my ($s, $l, $quot) = @_;
502 # show everything in the full version with anchor from
503 # short version (see above)
504 my $rv = $l->linkify_1(join('', @$quot));
507 # we use a <span> here to allow users to specify their own
508 # color for quoted text
509 $rv = $l->linkify_2(ascii_html($rv));
510 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
513 sub attach_link ($$$$;$) {
514 my ($upfx, $ct, $p, $fn, $err) = @_;
515 my ($part, $depth, @idx) = @$p;
516 my $nl = $idx[-1] > 1 ? "\n" : '';
517 my $idx = join('.', @idx);
518 my $size = bytes::length($part->body);
520 # hide attributes normally, unless we want to aid users in
521 # spotting MUA problems:
522 $ct =~ s/;.*// unless $err;
523 $ct = ascii_html($ct);
524 my $desc = $part->header('Content-Description');
525 $desc = $fn unless defined $desc;
526 $desc = '' unless defined $desc;
528 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
530 } elsif ($ct eq 'text/plain') {
535 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
538 "[-- Warning: decoded text below may be mangled --]\n";
540 $ret .= "[-- Attachment #$idx: ";
541 my $ts = "Type: $ct, Size: $size bytes";
542 $desc = ascii_html($desc);
543 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
548 my ($upfx, $obfs_ibx, $p) = @_;
549 # $p - from msg_iter: [ Email::MIME, depth, @idx ]
550 my ($part, $depth) = @$p; # attachment @idx is unused
551 my $ct = $part->content_type || 'text/plain';
552 my $fn = $part->filename;
553 my ($s, $err) = msg_part_text($part, $ct);
555 return attach_link($upfx, $ct, $p, $fn) unless defined $s;
557 my @lines = split(/^/m, $s);
559 if (defined($fn) || $depth > 0 || $err) {
560 # badly-encoded message with $err? tell the world about it!
561 $s .= attach_link($upfx, $ct, $p, $fn, $err);
565 my $l = PublicInbox::Linkify->new;
566 foreach my $cur (@lines) {
568 # show the previously buffered quote inline
569 flush_quote(\$s, $l, \@quot) if @quot;
573 $s .= $l->linkify_2(ascii_html($cur));
579 if (@quot) { # ugh, top posted
580 flush_quote(\$s, $l, \@quot);
581 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
584 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
585 if ($s =~ /\n\z/s) { # common, last line ends with a newline
587 } else { # some editors don't do newlines...
593 sub _msg_html_prepare {
594 my ($hdr, $ctx, $more, $nr) = @_;
595 my $srch = $ctx->{srch} if $ctx;
597 my $obfs_ibx = $ctx->{-obfs_ibx};
599 my $mids = mids($hdr);
600 my $multiple = scalar(@$mids) > 1; # zero, one, infinity
604 "<pre>WARNING: multiple messages refer to this Message-ID\n</pre>";
606 $rv .= "<pre\nid=b>"; # anchor for body start
611 $ctx->{-upfx} = '../';
615 if (defined($v = $hdr->header('From'))) {
616 $v = PublicInbox::Hval->new($v);
617 my @n = PublicInbox::Address::names($v->raw);
618 $title[1] = ascii_html(join(', ', @n));
621 obfuscate_addrs($obfs_ibx, $v);
622 obfuscate_addrs($obfs_ibx, $title[1]);
624 $rv .= "From: $v\n" if $v ne '';
626 foreach my $h (qw(To Cc)) {
627 defined($v = $hdr->header($h)) or next;
630 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
631 $rv .= "$h: $v\n" if $v ne '';
633 if (defined($v = $hdr->header('Subject')) && ($v ne '')) {
635 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
637 $rv .= qq(Subject: <a\nhref="#r"\nid=t>$v</a>\n);
639 $rv .= "Subject: $v\n";
642 } else { # dummy anchor for thread skeleton at bottom of page
643 $rv .= qq(<a\nhref="#r"\nid=t></a>) if $srch;
644 $title[0] = '(no subject)';
646 if (defined($v = $hdr->header('Date'))) {
648 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; # possible :P
651 $ctx->{-title_html} = join(' - ', @title);
653 my $mid = PublicInbox::Hval->new_msgid($_) ;
654 my $mhtml = $mid->as_html;
656 my $href = $mid->{href};
657 $rv .= "Message-ID: ";
658 $rv .= "<a\nhref=\"../$href/\">";
659 $rv .= "<$mhtml></a> ";
660 $rv .= "(<a\nhref=\"../$href/raw\">raw</a>)\n";
662 $rv .= "Message-ID: <$mhtml> ";
663 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
666 $rv .= _parent_headers($hdr, $srch);
671 my ($dst, $ctx, $hdr, $tpfx) = @_;
672 my $srch = $ctx->{srch};
673 my $mid = mids($hdr)->[0];
674 my ($nr, $msgs) = $srch->get_thread($mid);
675 my $expand = qq(expand[<a\nhref="${tpfx}T/#u">flat</a>) .
676 qq(|<a\nhref="${tpfx}t/#u">nested</a>] ) .
677 qq(<a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
678 qq(<a\nhref="${tpfx}t.atom">Atom feed</a>);
680 my $parent = in_reply_to($hdr);
681 $$dst .= "\n<b>Thread overview: </b>";
683 if (defined $parent) {
684 $$dst .= "$expand\n ";
685 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
687 $$dst .= "[no followups] $expand\n";
689 $ctx->{next_msg} = undef;
690 $ctx->{parent_msg} = $parent;
694 $$dst .= "$nr+ messages / $expand";
695 $$dst .= qq! <a\nhref="#b">top</a>\n!;
697 my $subj = $hdr->header('Subject');
698 defined $subj or $subj = '';
699 $ctx->{prev_subj} = [ split(/ /, $srch->subject_normalized($subj)) ];
701 $ctx->{prev_attr} = '';
702 $ctx->{prev_level} = 0;
705 # reduce hash lookups in skel_dump
706 my $ibx = $ctx->{-inbox};
707 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
708 walk_thread(thread_results($ctx, $msgs), $ctx, *skel_dump);
710 $ctx->{parent_msg} = $parent;
713 sub _parent_headers {
714 my ($hdr, $srch) = @_;
717 my $refs = references($hdr);
718 my $irt = pop @$refs;
720 my $v = PublicInbox::Hval->new_msgid($irt);
721 my $html = $v->as_html;
722 my $href = $v->{href};
723 $rv .= "In-Reply-To: <";
724 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
727 # do not display References: if search is present,
728 # we show the thread skeleton at the bottom, instead.
732 @$refs = map { linkify_ref_nosrch($_) } @$refs;
733 $rv .= 'References: '. join("\n\t", @$refs) . "\n";
739 my ($hdr, $standalone, $ctx, $rhref) = @_;
741 my $srch = $ctx->{srch} if $ctx;
744 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
748 thread_skel(\$idx, $ctx, $hdr, $tpfx);
753 if (my $n = $ctx->{next_msg}) {
754 $n = PublicInbox::Hval->new_msgid($n)->{href};
755 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
758 my $par = $ctx->{parent_msg};
760 $u = PublicInbox::Hval->new_msgid($par)->{href};
763 if (my $p = $ctx->{prev_msg}) {
764 $prev = PublicInbox::Hval->new_msgid($p)->{href};
765 if ($p && $par && $p eq $par) {
766 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
767 'rel=prev>prev parent</a>';
770 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
772 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
774 } elsif ($u) { # unlikely
775 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
777 $irt = "$next $prev$parent ";
782 $irt .= qq(<a\nhref="$rhref">reply</a>);
786 sub linkify_ref_nosrch {
787 my $v = PublicInbox::Hval->new_msgid($_[0]);
788 my $html = $v->as_html;
789 my $href = $v->{href};
790 "<<a\nhref=\"../$href/\">$html</a>>";
795 'm' . id_compress($msgid, 1);
799 my ($upfx, $mid) = @_;
801 $mid = PublicInbox::Hval->new_msgid($mid);
802 my $href = $mid->{href};
803 my $html = $mid->as_html;
804 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
809 $level ? INDENT x ($level - 1) : '';
813 my ($ctx, $level, $node, $idx) = @_;
814 ++$ctx->{root_idx} if $level == 0;
815 if ($node->{id} eq $ctx->{mid}) {
816 $ctx->{found_mid_at} = $ctx->{root_idx};
822 sub strict_loose_note ($) {
825 " -- strict thread matches above, loose matches on Subject: below --\n";
827 if ($nr > PublicInbox::Over::DEFAULT_LIMIT()) {
829 " -- use mbox.gz link to download all $nr messages --\n";
835 my ($ctx, $msgs) = @_;
836 require PublicInbox::SearchThread;
837 my $ibx = $ctx->{-inbox};
838 my $rootset = PublicInbox::SearchThread::thread($msgs, *sort_ds, $ibx);
840 # FIXME: `tid' is broken on --reindex, so that needs to be fixed
841 # and preserved in the future. This bug is hidden by `sid' matches
842 # in get_thread, so we never noticed it until now. And even when
843 # reindexing is fixed, we'll keep this code until a SCHEMA_VERSION
844 # bump since reindexing is expensive and users may not do it
846 # loose threading could've returned too many results,
847 # put the root the message we care about at the top:
848 my $mid = $ctx->{mid};
849 if (defined($mid) && scalar(@$rootset) > 1) {
850 $ctx->{root_idx} = -1;
851 my $nr = scalar @$msgs;
852 walk_thread($rootset, $ctx, *find_mid_root);
853 my $idx = $ctx->{found_mid_at};
854 if (defined($idx) && $idx != 0) {
855 my $tip = splice(@$rootset, $idx, 1);
856 @$rootset = reverse @$rootset;
857 unshift @$rootset, $tip;
858 $ctx->{sl_note} = strict_loose_note($nr);
866 require PublicInbox::ExtMsg;
867 PublicInbox::ExtMsg::ext_msg($ctx);
872 fmt_ts(msg_datestamp($hdr));
875 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
878 my ($prev_subj, $subj, $val) = @_;
880 my $omit = ''; # '"' denotes identical text omitted
881 my (@prev_pop, @curr_pop);
882 while (@$prev_subj && @$subj && $subj->[-1] eq $prev_subj->[-1]) {
883 push(@prev_pop, pop(@$prev_subj));
884 push(@curr_pop, pop(@$subj));
887 pop @$subj if @$subj && $subj->[-1] =~ /^re:\s*/i;
888 if (scalar(@curr_pop) == 1) {
890 push @$prev_subj, @prev_pop;
891 push @$subj, @curr_pop;
897 my ($ctx, $level, $node) = @_;
898 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
900 my $dst = $ctx->{dst};
901 my $cur = $ctx->{cur};
902 my $mid = $smsg->{mid};
904 if ($level == 0 && $ctx->{skel_dump_roots}++) {
905 $$dst .= delete $ctx->{sl_note} || '';
908 my $f = ascii_html($smsg->from_name);
909 my $obfs_ibx = $ctx->{-obfs_ibx};
910 obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
912 my $d = fmt_ts($smsg->{ds}) . ' ' . indent_for($level) . th_pfx($level);
914 $ctx->{first_level} ||= $level;
916 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
917 $ctx->{prev_attr} = $attr;
919 $ctx->{prev_level} = $level;
924 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
925 "$attr [this message]</a></b>\n";
928 $ctx->{prev_msg} = $mid;
931 $ctx->{next_msg} ||= $mid;
934 # Subject is never undef, this mail was loaded from
935 # our Xapian which would've resulted in '' if it were
936 # really missing (and Filter rejects empty subjects)
937 my @subj = split(/ /, $ctx->{srch}->subject_normalized($smsg->subject));
939 # remove common suffixes from the subject if it matches the previous,
940 # so we do not show redundant text at the end.
941 my $prev_subj = $ctx->{prev_subj} || [];
942 $ctx->{prev_subj} = [ @subj ];
943 my $omit = dedupe_subject($prev_subj, \@subj, '" ');
946 my $subj = join(' ', @subj);
947 $subj = ascii_html($subj);
948 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
949 $end = "$subj</a> $omit$f\n"
955 my $mapping = $ctx->{mapping};
957 my $map = $mapping->{$mid};
958 $id = id_compress($mid, 1);
960 $map->[0] = "$d<a\nhref=\"$m\">$end";
963 $m = $ctx->{-upfx}.mid_escape($mid).'/';
965 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
970 my ($ctx, $level, $node) = @_;
972 my $mid = $node->{id};
973 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
975 $d .= indent_for($level) . th_pfx($level);
976 my $upfx = $ctx->{-upfx};
977 my $m = PublicInbox::Hval->new_msgid($mid);
978 my $href = $upfx . $m->{href} . '/';
979 my $html = $m->as_html;
981 my $mapping = $ctx->{mapping};
982 my $map = $mapping->{$mid} if $mapping;
984 my $id = id_compress($mid, 1);
985 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
986 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
988 $d .= qq{<<a\nhref="$href">$html</a>>\n};
990 my $dst = $ctx->{dst};
997 (eval { $a->topmost->{smsg}->ds } || 0) <=>
998 (eval { $b->topmost->{smsg}->ds } || 0)
1002 # accumulate recent topics if search is supported
1003 # returns 200 if done, 404 if not
1005 my ($ctx, $level, $node) = @_;
1006 my $srch = $ctx->{srch};
1007 my $mid = $node->{id};
1008 my $x = $node->{smsg} || $ctx->{-inbox}->smsg_by_mid($mid);
1012 $subj = $x->subject;
1013 $subj = $srch->subject_normalized($subj);
1016 $topic = [ $ds, 1, { $subj => $mid }, $subj ];
1017 $ctx->{-cur_topic} = $topic;
1018 push @{$ctx->{order}}, $topic;
1022 $topic = $ctx->{-cur_topic}; # should never be undef
1023 $topic->[0] = $ds if $ds > $topic->[0];
1025 my $seen = $topic->[2];
1026 if (scalar(@$topic) == 3) { # parent was a ghost
1027 push @$topic, $subj;
1028 } elsif (!$seen->{$subj}) {
1029 push @$topic, $level, $subj;
1031 $seen->{$subj} = $mid; # latest for subject
1032 } else { # ghost message
1033 return 1 if $level != 0; # ignore child ghosts
1034 $topic = [ -666, 0, {} ];
1035 $ctx->{-cur_topic} = $topic;
1036 push @{$ctx->{order}}, $topic;
1043 my $order = delete $ctx->{order}; # [ ds, subj1, subj2, subj3, ... ]
1045 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
1050 my $ibx = $ctx->{-inbox};
1051 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
1052 my $srch = $ctx->{srch};
1054 # sort by recency, this allows new posts to "bump" old topics...
1055 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
1056 my ($ds, $n, $seen, $top, @ex) = @$topic;
1058 next unless defined $top; # ghost topic
1059 my $mid = delete $seen->{$top};
1060 my $href = mid_escape($mid);
1061 my $prev_subj = [ split(/ /, $top) ];
1062 $top = PublicInbox::Hval->new($top)->as_html;
1065 # $n isn't the total number of posts on the topic,
1066 # just the number of posts in the current results window
1070 $anchor = '#u'; # top of only message
1072 $n = " ($n+ messages)";
1073 $anchor = '#t'; # thread skeleton
1076 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
1077 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
1078 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
1079 " $ds UTC $n - $mbox / $atom\n";
1080 for (my $i = 0; $i < scalar(@ex); $i += 2) {
1081 my $level = $ex[$i];
1082 my $subj = $ex[$i + 1];
1083 $mid = delete $seen->{$subj};
1084 my @subj = split(/ /, $srch->subject_normalized($subj));
1085 my @next_prev = @subj; # full copy
1086 my $omit = dedupe_subject($prev_subj, \@subj, ' "');
1087 $prev_subj = \@next_prev;
1088 $subj = ascii_html(join(' ', @subj));
1089 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
1090 $href = mid_escape($mid);
1091 $s .= indent_for($level) . TCHILD;
1092 $s .= "<a\nhref=\"$href/T/#u\">$subj</a>$omit\n";
1096 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
1102 POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts));
1106 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]);
1107 timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
1110 sub pagination_footer ($$) {
1111 my ($ctx, $latest) = @_;
1112 delete $ctx->{qp} or return;
1113 my $next = $ctx->{next_page} || '';
1114 my $prev = $ctx->{prev_page} || '';
1116 $next = $next ? "$next " : ' ';
1117 $prev .= qq! <a\nhref='$latest'>latest</a>!;
1119 "<hr><pre>page: $next$prev</pre>";
1122 sub index_nav { # callback for WwwStream
1123 my (undef, $ctx) = @_;
1124 pagination_footer($ctx, '.')
1127 sub paginate_recent ($$) {
1128 my ($ctx, $lim) = @_;
1129 my $t = $ctx->{qp}->{t} || '';
1130 my $opts = { limit => $lim };
1131 my ($after, $before);
1133 # Xapian uses '..' but '-' is perhaps friendier to URL linkifiers
1134 # if only $after exists "YYYYMMDD.." because "." could be skipped
1135 # if interpreted as an end-of-sentence
1136 $t =~ s/\A(\d{8,14})-// and $after = str2ts($1);
1137 $t =~ /\A(\d{8,14})\z/ and $before = str2ts($1);
1139 my $ibx = $ctx->{-inbox};
1140 my $msgs = $ibx->recent($opts, $after, $before);
1141 my $nr = scalar @$msgs;
1142 if ($nr < $lim && defined($after)) {
1143 $after = $before = undef;
1144 $msgs = $ibx->recent($opts);
1145 $nr = scalar @$msgs;
1147 my $more = $nr == $lim;
1148 my ($newest, $oldest);
1150 $newest = $msgs->[0]->{ts};
1151 $oldest = $msgs->[-1]->{ts};
1152 # if we only had $after, our SQL query in ->recent ordered
1153 if ($newest < $oldest) {
1154 ($oldest, $newest) = ($newest, $oldest);
1155 $more = 0 if defined($after) && $after < $oldest;
1158 if (defined($oldest) && $more) {
1159 my $s = ts2str($oldest);
1160 $ctx->{next_page} = qq!<a\nhref="?t=$s"\nrel=next>next</a>!;
1162 if (defined($newest) && (defined($before) || defined($after))) {
1163 my $s = ts2str($newest);
1164 $ctx->{prev_page} = qq!<a\nhref="?t=$s-"\nrel=prev>prev</a>!;
1171 my $msgs = paginate_recent($ctx, 200); # 200 is our window
1173 walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic);
1175 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
1178 sub thread_adj_level {
1179 my ($ctx, $level) = @_;
1181 my $max = $ctx->{cur_level};
1183 return ('', '') if $max == 0; # flat output
1185 # reset existing lists
1186 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
1187 $ctx->{cur_level} = 0;
1189 } elsif ($level == $max) { # continue existing list
1191 } elsif ($level < $max) {
1192 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1193 $ctx->{cur_level} = $level;
1194 ("$beg<li>", '</li>');
1195 } else { # ($level > $max) # start a new level
1196 $ctx->{cur_level} = $level;
1197 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1202 sub ghost_index_entry {
1203 my ($ctx, $level, $node) = @_;
1204 my ($beg, $end) = thread_adj_level($ctx, $level);
1205 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})