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);
123 # mailto: link only works if address obfuscation is disabled
127 * If your mail client supports setting the <b>In-Reply-To</b> header
128 via mailto: links, try the <a
129 href="$link">mailto: link</a>
133 push @$arg, '/path/to/YOUR_REPLY';
134 $arg = ascii_html(join(" \\\n ", '', @$arg));
137 id=R><b>Reply instructions:</b>
139 You may reply publically to <a
140 href=#t>this message</a> via plain-text email
141 using any one of the following methods:
143 * Save the following mbox file, import it into your mail client,
144 and $reply_to_all from there: <a
147 Avoid top-posting and favor interleaved quoting:
149 href="$p_url">$p_url</a>
151 * Reply using the <b>--to</b>, <b>--cc</b>, and <b>--in-reply-to</b>
152 switches of git-send-email(1):
157 href="$se_url">$se_url</a>
164 my $refs = references($hdr);
168 sub fold_addresses ($) {
169 return $_[0] if length($_[0]) <= COLS;
170 # try to fold on commas after non-word chars before $lim chars,
171 # Try to get the "," preceeded by ">" or ")", but avoid folding
172 # on the comma where somebody uses "Lastname, Firstname".
173 # We also try to keep the last and penultimate addresses in
174 # the list on the same line if possible, hence the extra \z
175 # Fall back to folding on spaces at $lim + 1 chars
176 my $lim = COLS - 8; # 8 = "\t" display width
177 my $too_long = $lim + 1;
178 $_[0] =~ s/\s*\z//s; # Email::Simple doesn't strip trailing spaces
180 ($_[0] =~ /(.{0,$lim}\W(?:,|\z)|
183 .{$too_long,}?)(?:\s|\z)/xgo));
186 sub _hdr_names_html ($$) {
187 my ($hdr, $field) = @_;
188 my $val = $hdr->header($field) or return '';
189 ascii_html(join(', ', PublicInbox::Address::names($val)));
193 my ($nr, $singular, $plural) = @_;
194 return "0 $plural" if $nr == 0;
195 $nr == 1 ? "$nr $singular" : "$nr $plural";
198 # this is already inside a <pre>
200 my ($smsg, $ctx, $more) = @_;
201 my $srch = $ctx->{srch};
202 my $subj = $smsg->subject;
203 my $mid_raw = $smsg->mid;
204 my $id = id_compress($mid_raw, 1);
207 my $root_anchor = $ctx->{root_anchor} || '';
209 my $obfs_ibx = $ctx->{-obfs_ibx};
211 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
212 $subj = '<b>'.ascii_html($subj).'</b>';
213 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
214 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
216 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
218 my $mime = $smsg->{mime};
219 my $hdr = $mime->header_obj;
220 my $from = _hdr_names_html($hdr, 'From');
221 obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
222 $rv .= "From: $from @ ".fmt_ts($smsg->ds)." UTC";
223 my $upfx = $ctx->{-upfx};
224 my $mhref = $upfx . mid_escape($mid_raw) . '/';
225 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
226 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
227 my $to = fold_addresses(_hdr_names_html($hdr, 'To'));
228 my $cc = fold_addresses(_hdr_names_html($hdr, 'Cc'));
229 my ($tlen, $clen) = (length($to), length($cc));
231 if (($tlen + $clen) > COLS) {
232 $to_cc .= ' To: '.$to."\n" if $tlen;
233 $to_cc .= ' Cc: '.$cc."\n" if $clen;
236 $to_cc .= ' To: '.$to;
237 $to_cc .= '; <b>+Cc:</b> '.$cc if $clen;
239 $to_cc .= ' Cc: '.$cc if $clen;
243 obfuscate_addrs($obfs_ibx, $to_cc) if $obfs_ibx;
246 my $mapping = $ctx->{mapping};
247 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
248 my $mirt = PublicInbox::Hval->new_msgid($irt);
249 my $href = $upfx . $mirt->{href}. '/';
250 my $html = $mirt->as_html;
251 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
255 # scan through all parts, looking for displayable text
256 msg_iter($mime, sub { $rv .= add_text_body($mhref, $obfs_ibx, $_[0]) });
259 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
260 "<a\nhref=\"$mhref\">permalink</a>" .
261 " <a\nhref=\"${mhref}raw\">raw</a>" .
262 " <a\nhref=\"${mhref}#R\">reply</a>";
265 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
266 $rv .= "\t[relevance $pct->{$mid_raw}%]";
269 my $nested = 'nested';
274 $flat = "<b>$flat</b>";
276 $nested = "<b>$nested</b>";
278 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
279 $rv .= "|<a\nhref=\"${mhref}t/#u\">$nested</a>]";
280 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
285 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
289 sub pad_link ($$;$) {
290 my ($mid, $level, $s) = @_;
292 my $id = id_compress($mid, 1);
293 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
297 my ($mid_raw, $irt, $id, $ctx) = @_;
299 my $mapping = $ctx->{mapping} or return $rv;
301 my $mid_map = $mapping->{$mid_raw};
303 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
304 my ($attr, $node, $idx, $level) = @$mid_map;
305 my $children = $node->{children};
306 my $nr_c = scalar @$children;
309 if (my $smsg = $node->{smsg}) {
310 ($$irt) = (($smsg->{references} || '') =~ m/<([^>]+)>\z/);
312 my $irt_map = $mapping->{$$irt} if defined $$irt;
313 if (defined $irt_map) {
314 $siblings = $irt_map->[1]->{children};
315 $nr_s = scalar(@$siblings) - 1;
316 $rv .= $pad . $irt_map->[0];
318 my $prev = $siblings->[$idx - 1];
319 my $pmid = $prev->{id};
321 my $s = ($idx - 1). ' preceding siblings ...';
322 $rv .= pad_link($pmid, $level, $s);
323 } elsif ($idx == 2) {
324 my $ppmid = $siblings->[0]->{id};
325 $rv .= $pad . $mapping->{$ppmid}->[0];
327 $rv .= $pad . $mapping->{$pmid}->[0];
330 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
331 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
332 $attr =~ s!\n\z!</b>\n!s;
333 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
334 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
337 my $cmid = $children->[0]->{id};
338 $rv .= $pad . $mapping->{$cmid}->[0];
340 my $s = ($nr_c - 1). ' more replies';
341 $rv .= pad_link($cmid, $level + 1, $s);
342 } elsif (my $cn = $children->[1]) {
343 $rv .= $pad . $mapping->{$cn->{id}}->[0];
347 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
349 my $nmid = $next->{id};
350 $rv .= $pad . $mapping->{$nmid}->[0];
351 my $nnext = $nr_s - $idx;
353 my $s = ($nnext - 1).' subsequent siblings';
354 $rv .= pad_link($nmid, $level, $s);
355 } elsif (my $nn = $siblings->[$idx + 2]) {
356 $rv .= $pad . $mapping->{$nn->{id}}->[0];
359 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
363 my ($rootset, $ctx, $cb) = @_;
364 my @q = map { (0, $_, -1) } @$rootset;
366 my ($level, $node, $i) = splice(@q, 0, 3);
367 defined $node or next;
368 $cb->($ctx, $level, $node, $i);
371 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
376 my ($ctx, $level, $node, $idx) = @_;
377 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
378 skel_dump($ctx, $level, $node);
381 sub thread_index_entry {
382 my ($ctx, $level, $smsg) = @_;
383 my ($beg, $end) = thread_adj_level($ctx, $level);
384 $beg . '<pre>' . index_entry($smsg, $ctx, 0) . '</pre>' . $end;
387 sub stream_thread ($$) {
388 my ($rootset, $ctx) = @_;
389 my $inbox = $ctx->{-inbox};
390 my @q = map { (0, $_) } @$rootset;
395 my $node = shift @q or next;
397 unshift @q, map { ($cl, $_) } @{$node->{children}};
398 $smsg = $inbox->smsg_mime($node->{smsg}) and last;
400 return missing_thread($ctx) unless $smsg;
402 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
403 $ctx->{-title_html} = ascii_html($smsg->subject);
404 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg);
406 PublicInbox::WwwStream->response($ctx, 200, sub {
410 my $node = shift @q or next;
412 unshift @q, map { ($cl, $_) } @{$node->{children}};
413 if ($smsg = $inbox->smsg_mime($node->{smsg})) {
414 return thread_index_entry($ctx, $level, $smsg);
416 return ghost_index_entry($ctx, $level, $node);
419 my $ret = join('', thread_adj_level($ctx, 0));
420 $ret .= ${$ctx->{dst}}; # skel
428 my $mid = $ctx->{mid};
429 my $srch = $ctx->{srch};
430 my ($nr, $msgs) = $srch->get_thread($mid);
431 return missing_thread($ctx) if $nr == 0;
432 my $skel = '<hr><pre>';
433 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
434 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
435 $skel .= "<b\nid=t>Thread overview:</b> ";
436 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
437 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
438 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
439 $skel .= "-- links below jump to the message on this page --\n";
440 $ctx->{-upfx} = '../../';
441 $ctx->{cur_level} = 0;
442 $ctx->{dst} = \$skel;
443 $ctx->{prev_attr} = '';
444 $ctx->{prev_level} = 0;
445 $ctx->{root_anchor} = anchor_for($mid);
446 $ctx->{mapping} = {};
447 $ctx->{s_nr} = "$nr+ messages in thread";
449 my $rootset = thread_results($ctx, $msgs);
451 # reduce hash lookups in pre_thread->skel_dump
452 my $inbox = $ctx->{-inbox};
453 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
454 walk_thread($rootset, $ctx, *pre_thread);
457 return stream_thread($rootset, $ctx) unless $ctx->{flat};
459 # flat display: lazy load the full message from smsg
461 while (my $m = shift @$msgs) {
462 $smsg = $inbox->smsg_mime($m) and last;
464 return missing_thread($ctx) unless $smsg;
465 $ctx->{-title_html} = ascii_html($smsg->subject);
466 $ctx->{-html_tip} = '<pre>'.index_entry($smsg, $ctx, scalar @$msgs);
468 PublicInbox::WwwStream->response($ctx, 200, sub {
471 while (my $m = shift @$msgs) {
472 $smsg = $inbox->smsg_mime($m) and last;
474 return index_entry($smsg, $ctx, scalar @$msgs) if $smsg;
480 sub multipart_text_as_html {
481 my ($mime, $upfx, $obfs_ibx) = @_;
484 # scan through all parts, looking for displayable text
485 msg_iter($mime, sub { $rv .= add_text_body($upfx, $obfs_ibx, $_[0]) });
490 my ($s, $l, $quot) = @_;
492 # show everything in the full version with anchor from
493 # short version (see above)
494 my $rv = $l->linkify_1(join('', @$quot));
497 # we use a <span> here to allow users to specify their own
498 # color for quoted text
499 $rv = $l->linkify_2(ascii_html($rv));
500 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
503 sub attach_link ($$$$;$) {
504 my ($upfx, $ct, $p, $fn, $err) = @_;
505 my ($part, $depth, @idx) = @$p;
506 my $nl = $idx[-1] > 1 ? "\n" : '';
507 my $idx = join('.', @idx);
508 my $size = bytes::length($part->body);
510 # hide attributes normally, unless we want to aid users in
511 # spotting MUA problems:
512 $ct =~ s/;.*// unless $err;
513 $ct = ascii_html($ct);
514 my $desc = $part->header('Content-Description');
515 $desc = $fn unless defined $desc;
516 $desc = '' unless defined $desc;
518 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
520 } elsif ($ct eq 'text/plain') {
525 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
528 "[-- Warning: decoded text below may be mangled --]\n";
530 $ret .= "[-- Attachment #$idx: ";
531 my $ts = "Type: $ct, Size: $size bytes";
532 $desc = ascii_html($desc);
533 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
538 my ($upfx, $obfs_ibx, $p) = @_;
539 # $p - from msg_iter: [ Email::MIME, depth, @idx ]
540 my ($part, $depth) = @$p; # attachment @idx is unused
541 my $ct = $part->content_type || 'text/plain';
542 my $fn = $part->filename;
544 if ($ct =~ m!\btext/x?html\b!i) {
545 return attach_link($upfx, $ct, $p, $fn);
548 my $s = eval { $part->body_str };
550 # badly-encoded message? tell the world about it!
553 if ($ct =~ m!\btext/plain\b!i) {
554 # Try to assume UTF-8 because Alpine seems to
555 # do wacky things and set charset=X-UNKNOWN
556 $part->charset_set('UTF-8');
557 $s = eval { $part->body_str };
559 # If forcing charset=UTF-8 failed,
560 # attach_link will warn further down...
561 $s = $part->body if $@;
563 return attach_link($upfx, $ct, $p, $fn);
567 my @lines = split(/^/m, $s);
569 if (defined($fn) || $depth > 0 || $err) {
570 $s .= attach_link($upfx, $ct, $p, $fn, $err);
574 my $l = PublicInbox::Linkify->new;
575 foreach my $cur (@lines) {
577 # show the previously buffered quote inline
578 flush_quote(\$s, $l, \@quot) if @quot;
582 $s .= $l->linkify_2(ascii_html($cur));
588 if (@quot) { # ugh, top posted
589 flush_quote(\$s, $l, \@quot);
590 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
593 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
594 if ($s =~ /\n\z/s) { # common, last line ends with a newline
596 } else { # some editors don't do newlines...
602 sub _msg_html_prepare {
603 my ($hdr, $ctx, $more, $nr) = @_;
604 my $srch = $ctx->{srch} if $ctx;
606 my $obfs_ibx = $ctx->{-obfs_ibx};
608 my $mids = mids($hdr);
609 my $multiple = scalar(@$mids) > 1; # zero, one, infinity
613 "<pre>WARNING: multiple messages refer to this Message-ID\n</pre>";
615 $rv .= "<pre\nid=b>"; # anchor for body start
620 $ctx->{-upfx} = '../';
624 if (defined($v = $hdr->header('From'))) {
625 $v = PublicInbox::Hval->new($v);
626 my @n = PublicInbox::Address::names($v->raw);
627 $title[1] = ascii_html(join(', ', @n));
630 obfuscate_addrs($obfs_ibx, $v);
631 obfuscate_addrs($obfs_ibx, $title[1]);
633 $rv .= "From: $v\n" if $v ne '';
635 foreach my $h (qw(To Cc)) {
636 defined($v = $hdr->header($h)) or next;
639 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
640 $rv .= "$h: $v\n" if $v ne '';
642 if (defined($v = $hdr->header('Subject')) && ($v ne '')) {
644 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
646 $rv .= qq(Subject: <a\nhref="#r"\nid=t>$v</a>\n);
648 $rv .= "Subject: $v\n";
651 } else { # dummy anchor for thread skeleton at bottom of page
652 $rv .= qq(<a\nhref="#r"\nid=t></a>) if $srch;
653 $title[0] = '(no subject)';
655 if (defined($v = $hdr->header('Date'))) {
657 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; # possible :P
660 $ctx->{-title_html} = join(' - ', @title);
662 my $mid = PublicInbox::Hval->new_msgid($_) ;
663 my $mhtml = $mid->as_html;
665 my $href = $mid->{href};
666 $rv .= "Message-ID: ";
667 $rv .= "<a\nhref=\"../$href/\">";
668 $rv .= "<$mhtml></a> ";
669 $rv .= "(<a\nhref=\"../$href/raw\">raw</a>)\n";
671 $rv .= "Message-ID: <$mhtml> ";
672 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
675 $rv .= _parent_headers($hdr, $srch);
680 my ($dst, $ctx, $hdr, $tpfx) = @_;
681 my $srch = $ctx->{srch};
682 my $mid = mids($hdr)->[0];
683 my ($nr, $msgs) = $srch->get_thread($mid);
684 my $expand = qq(expand[<a\nhref="${tpfx}T/#u">flat</a>) .
685 qq(|<a\nhref="${tpfx}t/#u">nested</a>] ) .
686 qq(<a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
687 qq(<a\nhref="${tpfx}t.atom">Atom feed</a>);
689 my $parent = in_reply_to($hdr);
690 $$dst .= "\n<b>Thread overview: </b>";
692 if (defined $parent) {
693 $$dst .= "$expand\n ";
694 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
696 $$dst .= "[no followups] $expand\n";
698 $ctx->{next_msg} = undef;
699 $ctx->{parent_msg} = $parent;
703 $$dst .= "$nr+ messages / $expand";
704 $$dst .= qq! <a\nhref="#b">top</a>\n!;
706 my $subj = $hdr->header('Subject');
707 defined $subj or $subj = '';
708 $ctx->{prev_subj} = [ split(/ /, $srch->subject_normalized($subj)) ];
710 $ctx->{prev_attr} = '';
711 $ctx->{prev_level} = 0;
714 # reduce hash lookups in skel_dump
715 my $ibx = $ctx->{-inbox};
716 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
717 walk_thread(thread_results($ctx, $msgs), $ctx, *skel_dump);
719 $ctx->{parent_msg} = $parent;
722 sub _parent_headers {
723 my ($hdr, $srch) = @_;
726 my $refs = references($hdr);
727 my $irt = pop @$refs;
729 my $v = PublicInbox::Hval->new_msgid($irt);
730 my $html = $v->as_html;
731 my $href = $v->{href};
732 $rv .= "In-Reply-To: <";
733 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
736 # do not display References: if search is present,
737 # we show the thread skeleton at the bottom, instead.
741 @$refs = map { linkify_ref_nosrch($_) } @$refs;
742 $rv .= 'References: '. join("\n\t", @$refs) . "\n";
748 my ($hdr, $standalone, $ctx, $rhref) = @_;
750 my $srch = $ctx->{srch} if $ctx;
753 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
757 thread_skel(\$idx, $ctx, $hdr, $tpfx);
762 if (my $n = $ctx->{next_msg}) {
763 $n = PublicInbox::Hval->new_msgid($n)->{href};
764 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
767 my $par = $ctx->{parent_msg};
769 $u = PublicInbox::Hval->new_msgid($par)->{href};
772 if (my $p = $ctx->{prev_msg}) {
773 $prev = PublicInbox::Hval->new_msgid($p)->{href};
774 if ($p && $par && $p eq $par) {
775 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
776 'rel=prev>prev parent</a>';
779 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
781 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
783 } elsif ($u) { # unlikely
784 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
786 $irt = "$next $prev$parent ";
791 $irt .= qq(<a\nhref="$rhref">reply</a>);
795 sub linkify_ref_nosrch {
796 my $v = PublicInbox::Hval->new_msgid($_[0]);
797 my $html = $v->as_html;
798 my $href = $v->{href};
799 "<<a\nhref=\"../$href/\">$html</a>>";
804 'm' . id_compress($msgid, 1);
808 my ($upfx, $mid) = @_;
810 $mid = PublicInbox::Hval->new_msgid($mid);
811 my $href = $mid->{href};
812 my $html = $mid->as_html;
813 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
818 $level ? INDENT x ($level - 1) : '';
822 my ($ctx, $msgs) = @_;
823 require PublicInbox::SearchThread;
824 PublicInbox::SearchThread::thread($msgs, *sort_ds, $ctx->{-inbox});
829 require PublicInbox::ExtMsg;
830 PublicInbox::ExtMsg::ext_msg($ctx);
835 fmt_ts(msg_datestamp($hdr));
838 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
841 my ($prev_subj, $subj, $val) = @_;
843 my $omit = ''; # '"' denotes identical text omitted
844 my (@prev_pop, @curr_pop);
845 while (@$prev_subj && @$subj && $subj->[-1] eq $prev_subj->[-1]) {
846 push(@prev_pop, pop(@$prev_subj));
847 push(@curr_pop, pop(@$subj));
850 pop @$subj if @$subj && $subj->[-1] =~ /^re:\s*/i;
851 if (scalar(@curr_pop) == 1) {
853 push @$prev_subj, @prev_pop;
854 push @$subj, @curr_pop;
860 my ($ctx, $level, $node) = @_;
861 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
863 my $dst = $ctx->{dst};
864 my $cur = $ctx->{cur};
865 my $mid = $smsg->{mid};
867 my $f = ascii_html($smsg->from_name);
868 my $obfs_ibx = $ctx->{-obfs_ibx};
869 obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
871 my $d = fmt_ts($smsg->{ds}) . ' ' . indent_for($level) . th_pfx($level);
873 $ctx->{first_level} ||= $level;
875 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
876 $ctx->{prev_attr} = $attr;
878 $ctx->{prev_level} = $level;
883 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
884 "$attr [this message]</a></b>\n";
887 $ctx->{prev_msg} = $mid;
890 $ctx->{next_msg} ||= $mid;
893 # Subject is never undef, this mail was loaded from
894 # our Xapian which would've resulted in '' if it were
895 # really missing (and Filter rejects empty subjects)
896 my @subj = split(/ /, $ctx->{srch}->subject_normalized($smsg->subject));
898 # remove common suffixes from the subject if it matches the previous,
899 # so we do not show redundant text at the end.
900 my $prev_subj = $ctx->{prev_subj} || [];
901 $ctx->{prev_subj} = [ @subj ];
902 my $omit = dedupe_subject($prev_subj, \@subj, '" ');
905 my $subj = join(' ', @subj);
906 $subj = ascii_html($subj);
907 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
908 $end = "$subj</a> $omit$f\n"
914 my $mapping = $ctx->{mapping};
916 my $map = $mapping->{$mid};
917 $id = id_compress($mid, 1);
919 $map->[0] = "$d<a\nhref=\"$m\">$end";
922 $m = $ctx->{-upfx}.mid_escape($mid).'/';
924 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
928 my ($ctx, $level, $node) = @_;
930 my $mid = $node->{id};
931 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
933 $d .= indent_for($level) . th_pfx($level);
934 my $upfx = $ctx->{-upfx};
935 my $m = PublicInbox::Hval->new_msgid($mid);
936 my $href = $upfx . $m->{href} . '/';
937 my $html = $m->as_html;
939 my $mapping = $ctx->{mapping};
940 my $map = $mapping->{$mid} if $mapping;
942 my $id = id_compress($mid, 1);
943 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
944 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
946 $d .= qq{<<a\nhref="$href">$html</a>>\n};
948 my $dst = $ctx->{dst};
954 (eval { $a->topmost->{smsg}->ds } || 0) <=>
955 (eval { $b->topmost->{smsg}->ds } || 0)
959 # accumulate recent topics if search is supported
960 # returns 200 if done, 404 if not
962 my ($ctx, $level, $node) = @_;
963 my $srch = $ctx->{srch};
964 my $mid = $node->{id};
965 my $x = $node->{smsg} || $ctx->{-inbox}->smsg_by_mid($mid);
970 $subj = $srch->subject_normalized($subj);
973 $topic = [ $ds, 1, { $subj => $mid }, $subj ];
974 $ctx->{-cur_topic} = $topic;
975 push @{$ctx->{order}}, $topic;
979 $topic = $ctx->{-cur_topic}; # should never be undef
980 $topic->[0] = $ds if $ds > $topic->[0];
982 my $seen = $topic->[2];
983 if (scalar(@$topic) == 3) { # parent was a ghost
985 } elsif (!$seen->{$subj}) {
986 push @$topic, $level, $subj;
988 $seen->{$subj} = $mid; # latest for subject
989 } else { # ghost message
990 return if $level != 0; # ignore child ghosts
991 $topic = [ -666, 0, {} ];
992 $ctx->{-cur_topic} = $topic;
993 push @{$ctx->{order}}, $topic;
999 my $order = delete $ctx->{order}; # [ ds, subj1, subj2, subj3, ... ]
1001 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
1006 my $ibx = $ctx->{-inbox};
1007 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
1008 my $srch = $ctx->{srch};
1010 # sort by recency, this allows new posts to "bump" old topics...
1011 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
1012 my ($ds, $n, $seen, $top, @ex) = @$topic;
1014 next unless defined $top; # ghost topic
1015 my $mid = delete $seen->{$top};
1016 my $href = mid_escape($mid);
1017 my $prev_subj = [ split(/ /, $top) ];
1018 $top = PublicInbox::Hval->new($top)->as_html;
1021 # $n isn't the total number of posts on the topic,
1022 # just the number of posts in the current results window
1026 $anchor = '#u'; # top of only message
1028 $n = " ($n+ messages)";
1029 $anchor = '#t'; # thread skeleton
1032 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
1033 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
1034 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
1035 " $ds UTC $n - $mbox / $atom\n";
1036 for (my $i = 0; $i < scalar(@ex); $i += 2) {
1037 my $level = $ex[$i];
1038 my $subj = $ex[$i + 1];
1039 $mid = delete $seen->{$subj};
1040 my @subj = split(/ /, $srch->subject_normalized($subj));
1041 my @next_prev = @subj; # full copy
1042 my $omit = dedupe_subject($prev_subj, \@subj, ' "');
1043 $prev_subj = \@next_prev;
1044 $subj = ascii_html(join(' ', @subj));
1045 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
1046 $href = mid_escape($mid);
1047 $s .= indent_for($level) . TCHILD;
1048 $s .= "<a\nhref=\"$href/T/#u\">$subj</a>$omit\n";
1052 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
1058 POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts));
1062 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]);
1063 timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
1066 sub pagination_footer ($$) {
1067 my ($ctx, $latest) = @_;
1068 delete $ctx->{qp} or return;
1069 my $next = $ctx->{next_page} || '';
1070 my $prev = $ctx->{prev_page} || '';
1072 $next = $next ? "$next " : ' ';
1073 $prev .= qq! <a\nhref='$latest'>latest</a>!;
1075 "<hr><pre>page: $next$prev</pre>";
1078 sub index_nav { # callback for WwwStream
1079 my (undef, $ctx) = @_;
1080 pagination_footer($ctx, '.')
1083 sub paginate_recent ($$) {
1084 my ($ctx, $lim) = @_;
1085 my $t = $ctx->{qp}->{t} || '';
1086 my $opts = { limit => $lim };
1087 my ($after, $before);
1089 # Xapian uses '..' but '-' is perhaps friendier to URL linkifiers
1090 # if only $after exists "YYYYMMDD.." because "." could be skipped
1091 # if interpreted as an end-of-sentence
1092 $t =~ s/\A(\d{8,14})-// and $after = str2ts($1);
1093 $t =~ /\A(\d{8,14})\z/ and $before = str2ts($1);
1095 my $ibx = $ctx->{-inbox};
1096 my $msgs = $ibx->recent($opts, $after, $before);
1097 my $nr = scalar @$msgs;
1098 if ($nr < $lim && defined($after)) {
1099 $after = $before = undef;
1100 $msgs = $ibx->recent($opts);
1101 $nr = scalar @$msgs;
1103 my $more = $nr == $lim;
1104 my ($newest, $oldest);
1106 $newest = $msgs->[0]->{ts};
1107 $oldest = $msgs->[-1]->{ts};
1108 # if we only had $after, our SQL query in ->recent ordered
1109 if ($newest < $oldest) {
1110 ($oldest, $newest) = ($newest, $oldest);
1111 $more = 0 if defined($after) && $after < $oldest;
1114 if (defined($oldest) && $more) {
1115 my $s = ts2str($oldest);
1116 $ctx->{next_page} = qq!<a\nhref="?t=$s"\nrel=next>next</a>!;
1118 if (defined($newest) && (defined($before) || defined($after))) {
1119 my $s = ts2str($newest);
1120 $ctx->{prev_page} = qq!<a\nhref="?t=$s-"\nrel=prev>prev</a>!;
1127 my $msgs = paginate_recent($ctx, 200); # 200 is our window
1129 walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic);
1131 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
1134 sub thread_adj_level {
1135 my ($ctx, $level) = @_;
1137 my $max = $ctx->{cur_level};
1139 return ('', '') if $max == 0; # flat output
1141 # reset existing lists
1142 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
1143 $ctx->{cur_level} = 0;
1145 } elsif ($level == $max) { # continue existing list
1147 } elsif ($level < $max) {
1148 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1149 $ctx->{cur_level} = $level;
1150 ("$beg<li>", '</li>');
1151 } else { # ($level > $max) # start a new level
1152 $ctx->{cur_level} = $level;
1153 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1158 sub ghost_index_entry {
1159 my ($ctx, $level, $node) = @_;
1160 my ($beg, $end) = thread_adj_level($ctx, $level);
1161 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})