X-Git-Url: http://www.git.stargrave.org/?p=public-inbox.git;a=blobdiff_plain;f=lib%2FPublicInbox%2FView.pm;h=f6df2b41664108a5ee69e32d3f636f82b7438b5a;hp=782e66861742ad376785e0199aaa8157aee7762a;hb=a56c80e063f992075a39d49dec4cafc0479c850e;hpb=1e03323d76eb6c029ab6749fe783f25d5d109e1f diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm index 782e6686..f6df2b41 100644 --- a/lib/PublicInbox/View.pm +++ b/lib/PublicInbox/View.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2014-2018 all contributors +# Copyright (C) 2014-2019 all contributors # License: AGPL-3.0+ # # Used for displaying the HTML web interface. @@ -6,105 +6,99 @@ package PublicInbox::View; use strict; use warnings; +use bytes (); # only for bytes::length use PublicInbox::MsgTime qw(msg_datestamp); -use PublicInbox::Hval qw/ascii_html obfuscate_addrs/; +use PublicInbox::Hval qw(ascii_html obfuscate_addrs prurl); use PublicInbox::Linkify; -use PublicInbox::MID qw/id_compress mid_escape mids references/; +use PublicInbox::MID qw/id_compress mid_escape mids mids_for_index references/; use PublicInbox::MsgIter; use PublicInbox::Address; use PublicInbox::WwwStream; use PublicInbox::Reply; use PublicInbox::ViewDiff qw(flush_diff); -require POSIX; +use POSIX qw(strftime); use Time::Local qw(timegm); - +use PublicInbox::SearchMsg qw(subject_normalized); use constant COLS => 72; use constant INDENT => ' '; use constant TCHILD => '` '; sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD }; +sub msg_html_i { + my ($nr, $ctx) = @_; + my $more = $ctx->{more}; + if ($nr == 1) { + # $more cannot be true w/o $smsg being defined: + my $upfx = $more ? '../'.mid_escape($ctx->{smsg}->mid).'/' : ''; + $ctx->{tip} . + multipart_text_as_html(delete $ctx->{mime}, $upfx, + $ctx) . '
' + } elsif ($more) { + ++$ctx->{end_nr}; + # fake an EOF if {more} retrieval fails fails; + eval { msg_html_more($ctx, $nr) }; + } elsif ($nr == $ctx->{end_nr}) { + # fake an EOF if generating the footer fails; + # we want to at least show the message if something + # here crashes: + eval { html_footer($ctx) }; + } else { + undef + } +} + # public functions: (unstable) sub msg_html { - my ($ctx, $mime, $more, $smsg) = @_; - my $hdr = $mime->header_obj; + my ($ctx, $mime, $smsg) = @_; my $ibx = $ctx->{-inbox}; $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef; - my $tip = _msg_html_prepare($hdr, $ctx, $more, 0); - my $end = 2; - PublicInbox::WwwStream->response($ctx, 200, sub { - my ($nr, undef) = @_; - if ($nr == 1) { - # $more cannot be true w/o $smsg being defined: - my $upfx = $more ? '../'.mid_escape($smsg->mid).'/' : ''; - $tip . multipart_text_as_html($mime, $upfx, $ctx) . - '
' - } elsif ($more && @$more) { - ++$end; - msg_html_more($ctx, $more, $nr); - } elsif ($nr == $end) { - # fake an EOF if generating the footer fails; - # we want to at least show the message if something - # here crashes: - eval { - '
' . html_footer($hdr, 1, $ctx) .
-				'
' . msg_reply($ctx, $hdr) - }; - } else { - undef - } - }); + my $hdr = $ctx->{hdr} = $mime->header_obj; + $ctx->{tip} = _msg_html_prepare($hdr, $ctx, 0); + $ctx->{end_nr} = 2; + $ctx->{smsg} = $smsg; + $ctx->{mime} = $mime; + PublicInbox::WwwStream->response($ctx, 200, \&msg_html_i); } sub msg_page { my ($ctx) = @_; my $mid = $ctx->{mid}; my $ibx = $ctx->{-inbox}; - my ($first, $more); + my ($first); my $smsg; - if (my $srch = $ibx->search) { + if (my $over = $ibx->over) { my ($id, $prev); - $smsg = $srch->next_by_mid($mid, \$id, \$prev); + $smsg = $over->next_by_mid($mid, \$id, \$prev); $first = $ibx->msg_by_smsg($smsg) if $smsg; if ($first) { - my $next = $srch->next_by_mid($mid, \$id, \$prev); - $more = [ $id, $prev, $next ] if $next; + my $next = $over->next_by_mid($mid, \$id, \$prev); + $ctx->{more} = [ $id, $prev, $next ] if $next; } return unless $first; } else { $first = $ibx->msg_by_mid($mid) or return; } - msg_html($ctx, PublicInbox::MIME->new($first), $more, $smsg); + msg_html($ctx, PublicInbox::MIME->new($first), $smsg); } sub msg_html_more { - my ($ctx, $more, $nr) = @_; - my $str = eval { - my ($id, $prev, $smsg) = @$more; - my $mid = $ctx->{mid}; - my $ibx = $ctx->{-inbox}; - $smsg = $ibx->smsg_mime($smsg); - my $next = $ctx->{srch}->next_by_mid($mid, \$id, \$prev); - @$more = $next ? ($id, $prev, $next) : (); - if ($smsg) { - my $mime = $smsg->{mime}; - my $upfx = '../' . mid_escape($smsg->mid) . '/'; - _msg_html_prepare($mime->header_obj, $ctx, $more, $nr) . - multipart_text_as_html($mime, $upfx, $ctx) . - '
' - } else { - ''; - } - }; - if ($@) { - warn "Error lookup up additional messages: $@\n"; - $str = '
Error looking up additional messages
'; - } - $str; + my ($ctx, $nr) = @_; + my ($id, $prev, $smsg) = @{$ctx->{more}}; + my $ibx = $ctx->{-inbox}; + $smsg = $ibx->smsg_mime($smsg); + my $next = $ibx->over->next_by_mid($ctx->{mid}, \$id, \$prev); + $ctx->{more} = $next ? [ $id, $prev, $next ] : undef; + return '' unless $smsg; + my $upfx = '../' . mid_escape($smsg->mid) . '/'; + my $mime = delete $smsg->{mime}; + _msg_html_prepare($mime->header_obj, $ctx, $nr) . + multipart_text_as_html($mime, $upfx, $ctx) . + '
' } # /$INBOX/$MESSAGE_ID/#R -sub msg_reply { +sub msg_reply ($$) { my ($ctx, $hdr) = @_; my $se_url = 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html'; @@ -114,7 +108,7 @@ sub msg_reply { my $info = ''; my $ibx = $ctx->{-inbox}; if (my $url = $ibx->{infourl}) { - $url = PublicInbox::Hval::prurl($ctx->{env}, $url); + $url = prurl($ctx->{env}, $url); $info = qq(\n List information: $url\n); } @@ -140,7 +134,7 @@ EOF
Reply instructions:
 
-You may reply publically to this message via plain-text email
 using any one of the following methods:
 
@@ -189,8 +183,8 @@ sub fold_addresses ($) {
 
 sub _hdr_names_html ($$) {
 	my ($hdr, $field) = @_;
-	my $val = $hdr->header($field) or return '';
-	ascii_html(join(', ', PublicInbox::Address::names($val)));
+	my @vals = $hdr->header($field) or return '';
+	ascii_html(join(', ', PublicInbox::Address::names(join(',', @vals))));
 }
 
 sub nr_to_s ($$$) {
@@ -199,10 +193,12 @@ sub nr_to_s ($$$) {
 	$nr == 1 ? "$nr $singular" : "$nr $plural";
 }
 
+# human-friendly format
+sub fmt_ts ($) { strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
+
 # this is already inside a 
 sub index_entry {
 	my ($smsg, $ctx, $more) = @_;
-	my $srch = $ctx->{srch};
 	my $subj = $smsg->subject;
 	my $mid_raw = $smsg->mid;
 	my $id = id_compress($mid_raw, 1);
@@ -212,6 +208,7 @@ sub index_entry {
 	my $irt;
 	my $obfs_ibx = $ctx->{-obfs_ibx};
 
+	$subj = '(no subject)' if $subj eq '';
 	my $rv = "* ";
 	$subj = ''.ascii_html($subj).'';
 	obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
@@ -261,8 +258,10 @@ sub index_entry {
 	$rv .= "\n";
 
 	# scan through all parts, looking for displayable text
-	my $ibx = $ctx->{-inbox};
-	msg_iter($mime, sub { $rv .= add_text_body($mhref, $ctx, $_[0]) });
+	$ctx->{mhref} = $mhref;
+	$ctx->{rv} = \$rv;
+	msg_iter($mime, \&add_text_body, $ctx, 1);
+	delete $ctx->{rv};
 
 	# add the footer
 	$rv .= "\n^ ".
@@ -271,8 +270,8 @@ sub index_entry {
 		" reply";
 
 	my $hr;
-	if (my $pct = $ctx->{pct}) { # used by SearchView.pm
-		$rv .= "\t[relevance $pct->{$mid_raw}%]";
+	if (defined(my $pct = $smsg->{pct})) { # used by SearchView.pm
+		$rv .= "\t[relevance $pct%]";
 		$hr = 1;
 	} elsif ($mapping) {
 		my $nested = 'nested';
@@ -371,7 +370,8 @@ sub _th_index_lite {
 	$rv .= $pad ."$s_s, $s_c; $ctx->{s_nr}\n";
 }
 
-sub walk_thread {
+# non-recursive thread walker
+sub walk_thread ($$$) {
 	my ($rootset, $ctx, $cb) = @_;
 	my @q = map { (0, $_, -1) } @$rootset;
 	while (@q) {
@@ -384,7 +384,7 @@ sub walk_thread {
 	}
 }
 
-sub pre_thread  {
+sub pre_thread  { # walk_thread callback
 	my ($ctx, $level, $node, $idx) = @_;
 	$ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
 	skel_dump($ctx, $level, $node);
@@ -396,50 +396,51 @@ sub thread_index_entry {
 	$beg . '
' . index_entry($smsg, $ctx, 0) . '
' . $end; } +sub stream_thread_i { # PublicInbox::WwwStream::getline callback + my ($nr, $ctx) = @_; + return unless exists($ctx->{skel}); + my $q = $ctx->{-queue}; + while (@$q) { + my $level = shift @$q; + my $node = shift @$q or next; + my $cl = $level + 1; + unshift @$q, map { ($cl, $_) } @{$node->{children}}; + if (my $smsg = $ctx->{-inbox}->smsg_mime($node->{smsg})) { + return thread_index_entry($ctx, $level, $smsg); + } else { + return ghost_index_entry($ctx, $level, $node); + } + } + join('', thread_adj_level($ctx, 0)) . ${delete $ctx->{skel}}; +} + sub stream_thread ($$) { my ($rootset, $ctx) = @_; - my $inbox = $ctx->{-inbox}; + my $ibx = $ctx->{-inbox}; my @q = map { (0, $_) } @$rootset; - my $level; - my $smsg; + my ($smsg, $level); while (@q) { $level = shift @q; my $node = shift @q or next; my $cl = $level + 1; unshift @q, map { ($cl, $_) } @{$node->{children}}; - $smsg = $inbox->smsg_mime($node->{smsg}) and last; + $smsg = $ibx->smsg_mime($node->{smsg}) and last; } return missing_thread($ctx) unless $smsg; - $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef; + $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef; $ctx->{-title_html} = ascii_html($smsg->subject); $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg); - $smsg = undef; - PublicInbox::WwwStream->response($ctx, 200, sub { - return unless $ctx; - while (@q) { - $level = shift @q; - my $node = shift @q or next; - my $cl = $level + 1; - unshift @q, map { ($cl, $_) } @{$node->{children}}; - if ($smsg = $inbox->smsg_mime($node->{smsg})) { - return thread_index_entry($ctx, $level, $smsg); - } else { - return ghost_index_entry($ctx, $level, $node); - } - } - my $ret = join('', thread_adj_level($ctx, 0)); - $ret .= ${$ctx->{dst}}; # skel - $ctx = undef; - $ret; - }); + $ctx->{-queue} = \@q; + PublicInbox::WwwStream->response($ctx, 200, \&stream_thread_i); } +# /$INBOX/$MESSAGE_ID/t/ sub thread_html { my ($ctx) = @_; my $mid = $ctx->{mid}; - my $srch = $ctx->{srch}; - my ($nr, $msgs) = $srch->get_thread($mid); + my $ibx = $ctx->{-inbox}; + my ($nr, $msgs) = $ibx->over->get_thread($mid); return missing_thread($ctx) if $nr == 0; my $skel = '
';
 	$skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
@@ -451,19 +452,19 @@ sub thread_html {
 	$skel .= "-- links below jump to the message on this page --\n";
 	$ctx->{-upfx} = '../../';
 	$ctx->{cur_level} = 0;
-	$ctx->{dst} = \$skel;
+	$ctx->{skel} = \$skel;
 	$ctx->{prev_attr} = '';
 	$ctx->{prev_level} = 0;
 	$ctx->{root_anchor} = anchor_for($mid);
 	$ctx->{mapping} = {};
-	$ctx->{s_nr} = "$nr+ messages in thread";
+	$ctx->{s_nr} = ($nr > 1 ? "$nr+ messages" : 'only message')
+	               .' in thread';
 
 	my $rootset = thread_results($ctx, $msgs);
 
 	# reduce hash lookups in pre_thread->skel_dump
-	my $inbox = $ctx->{-inbox};
-	$ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
-	walk_thread($rootset, $ctx, *pre_thread);
+	$ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
+	walk_thread($rootset, $ctx, \&pre_thread);
 
 	$skel .= '
'; return stream_thread($rootset, $ctx) unless $ctx->{flat}; @@ -471,31 +472,34 @@ sub thread_html { # flat display: lazy load the full message from smsg my $smsg; while (my $m = shift @$msgs) { - $smsg = $inbox->smsg_mime($m) and last; + $smsg = $ibx->smsg_mime($m) and last; } return missing_thread($ctx) unless $smsg; $ctx->{-title_html} = ascii_html($smsg->subject); $ctx->{-html_tip} = '
'.index_entry($smsg, $ctx, scalar @$msgs);
-	$smsg = undef;
-	PublicInbox::WwwStream->response($ctx, 200, sub {
-		return unless $msgs;
-		$smsg = undef;
-		while (my $m = shift @$msgs) {
-			$smsg = $inbox->smsg_mime($m) and last;
-		}
-		return index_entry($smsg, $ctx, scalar @$msgs) if $smsg;
-		$msgs = undef;
-		$skel;
-	});
+	$ctx->{msgs} = $msgs;
+	PublicInbox::WwwStream->response($ctx, 200, \&thread_html_i);
+}
+
+sub thread_html_i { # PublicInbox::WwwStream::getline callback
+	my ($nr, $ctx) = @_;
+	my $msgs = $ctx->{msgs} or return;
+	while (my $smsg = shift @$msgs) {
+		$ctx->{-inbox}->smsg_mime($smsg) or next;
+		return index_entry($smsg, $ctx, scalar @$msgs);
+	}
+	my ($skel) = delete @$ctx{qw(skel msgs)};
+	$$skel;
 }
 
 sub multipart_text_as_html {
-	my ($mime, $upfx, $ctx) = @_;
-	my $rv = "";
+	my (undef, $mhref, $ctx) = @_; # $mime = $_[0]
+	$ctx->{mhref} = $mhref;
+	$ctx->{rv} = \(my $rv = '');
 
 	# scan through all parts, looking for displayable text
-	msg_iter($mime, sub { $rv .= add_text_body($upfx, $ctx, $_[0]) });
-	$rv;
+	msg_iter($_[0], \&add_text_body, $ctx, 1);
+	${delete $ctx->{rv}};
 }
 
 sub flush_quote {
@@ -503,17 +507,17 @@ sub flush_quote {
 
 	# show everything in the full version with anchor from
 	# short version (see above)
-	my $rv = $l->linkify_1(join('', @$quot));
-	@$quot = ();
+	my $rv = $l->linkify_1($$quot);
 
 	# we use a  here to allow users to specify their own
 	# color for quoted text
 	$rv = $l->linkify_2(ascii_html($rv));
+	$$quot = undef;
 	$$s .= qq() . $rv . ''
 }
 
 sub attach_link ($$$$;$) {
-	my ($upfx, $ct, $p, $fn, $err) = @_;
+	my ($ctx, $ct, $p, $fn, $err) = @_;
 	my ($part, $depth, @idx) = @$p;
 	my $nl = $idx[-1] > 1 ? "\n" : '';
 	my $idx = join('.', @idx);
@@ -527,36 +531,36 @@ sub attach_link ($$$$;$) {
 	$desc = $fn unless defined $desc;
 	$desc = '' unless defined $desc;
 	my $sfn;
-	if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
+	if (defined $fn && $fn =~ /\A$PublicInbox::Hval::FN\z/o) {
 		$sfn = $fn;
 	} elsif ($ct eq 'text/plain') {
 		$sfn = 'a.txt';
 	} else {
 		$sfn = 'a.bin';
 	}
-	my $ret = qq($nl);
+	my $rv = $ctx->{rv};
+	$$rv .= qq($nl{mhref}$idx-$sfn">);
 	if ($err) {
-		$ret .=
-"[-- Warning: decoded text below may be mangled --]\n";
+		$$rv .= "[-- Warning: decoded text below may be mangled --]\n";
 	}
-	$ret .= "[-- Attachment #$idx: ";
+	$$rv .= "[-- Attachment #$idx: ";
 	my $ts = "Type: $ct, Size: $size bytes";
 	$desc = ascii_html($desc);
-	$ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
-	$ret .= "\n";
+	$$rv .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
+	$$rv .= "\n";
+	undef;
 }
 
-sub add_text_body {
-	my ($upfx, $ctx, $p) = @_;
+sub add_text_body { # callback for msg_iter
+	my ($p, $ctx) = @_;
+	my $upfx = $ctx->{mhref};
 	my $ibx = $ctx->{-inbox};
-	my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
 	# $p - from msg_iter: [ Email::MIME, depth, @idx ]
 	my ($part, $depth, @idx) = @$p;
 	my $ct = $part->content_type || 'text/plain';
 	my $fn = $part->filename;
 	my ($s, $err) = msg_part_text($part, $ct);
-
-	return attach_link($upfx, $ct, $p, $fn) unless defined $s;
+	return attach_link($ctx, $ct, $p, $fn) unless defined $s;
 
 	# makes no difference to browsers, and don't screw up filename
 	# link generation in diffs with the extra '%0D'
@@ -571,6 +575,7 @@ sub add_text_body {
 		$ctx->{-apfx} = join('/', @idx);
 		$ctx->{-anchors} = {}; # attr => filename
 		$ctx->{-diff} = $diff = [];
+		delete $ctx->{-long_path};
 		my $spfx;
 		if ($ibx->{-repo_objs}) {
 			if (index($upfx, '//') >= 0) { # absolute URL (Atom feeds)
@@ -590,75 +595,61 @@ sub add_text_body {
 		$ctx->{-spfx} = $spfx;
 	};
 
-	my @lines = split(/^/m, $s);
+	# some editors don't put trailing newlines at the end:
+	$s .= "\n" unless $s =~ /\n\z/s;
+
+	# split off quoted and unquoted blocks:
+	my @sections = split(/((?:^>[^\n]*\n)+)/sm, $s);
 	$s = '';
+	my $rv = $ctx->{rv};
 	if (defined($fn) || $depth > 0 || $err) {
 		# badly-encoded message with $err? tell the world about it!
-		$s .= attach_link($upfx, $ct, $p, $fn, $err);
-		$s .= "\n";
+		attach_link($ctx, $ct, $p, $fn, $err);
+		$$rv .= "\n";
 	}
-	my @quot;
 	my $l = PublicInbox::Linkify->new;
-	foreach my $cur (@lines) {
-		if ($cur !~ /^>/) {
-			# show the previously buffered quote inline
-			flush_quote(\$s, $l, \@quot) if @quot;
-
-			if ($diff) {
-				push @$diff, $cur;
-			} else {
-				# regular line, OK
-				$l->linkify_1($cur);
-				$s .= $l->linkify_2(ascii_html($cur));
-			}
+	foreach my $cur (@sections) {
+		if ($cur =~ /\A>/) {
+			flush_quote($rv, $l, \$cur);
+		} elsif ($diff) {
+			@$diff = split(/^/m, $cur);
+			$cur = undef;
+			flush_diff($rv, $ctx, $l);
 		} else {
-			flush_diff(\$s, $ctx, $l) if $diff && @$diff;
-			push @quot, $cur;
+			# regular lines, OK
+			$l->linkify_1($cur);
+			$$rv .= $l->linkify_2(ascii_html($cur));
+			$cur = undef;
 		}
 	}
 
-	if (@quot) { # ugh, top posted
-		flush_quote(\$s, $l, \@quot);
-		flush_diff(\$s, $ctx, $l) if $diff && @$diff;
-		obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
-		$s;
-	} else {
-		flush_diff(\$s, $ctx, $l) if $diff && @$diff;
-		obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
-		if ($s =~ /\n\z/s) { # common, last line ends with a newline
-			$s;
-		} else { # some editors don't do newlines...
-			$s .= "\n";
-		}
-	}
+	obfuscate_addrs($ibx, $$rv) if $ibx->{obfuscate};
 }
 
 sub _msg_html_prepare {
-	my ($hdr, $ctx, $more, $nr) = @_;
-	my $srch = $ctx->{srch} if $ctx;
+	my ($hdr, $ctx, $nr) = @_;
 	my $atom = '';
+	my $over = $ctx->{-inbox}->over;
 	my $obfs_ibx = $ctx->{-obfs_ibx};
 	my $rv = '';
-	my $mids = mids($hdr);
-	my $multiple = scalar(@$mids) > 1; # zero, one, infinity
+	my $mids = mids_for_index($hdr);
 	if ($nr == 0) {
-		if ($more) {
+		if ($ctx->{more}) {
 			$rv .=
-"
WARNING: multiple messages refer to this Message-ID\n
"; +"
WARNING: multiple messages have this Message-ID\n
"; } $rv .= ""; # anchor for body start } else { $rv .= '
';
 	}
-	if ($srch) {
+	if ($over) {
 		$ctx->{-upfx} = '../';
 	}
-	my @title;
-	my $v;
-	if (defined($v = $hdr->header('From'))) {
+	my @title; # (Subject[0], From[0])
+	for my $v ($hdr->header('From')) {
 		$v = PublicInbox::Hval->new($v);
 		my @n = PublicInbox::Address::names($v->raw);
-		$title[1] = ascii_html(join(', ', @n));
+		$title[1] //= ascii_html(join(', ', @n));
 		$v = $v->as_html;
 		if ($obfs_ibx) {
 			obfuscate_addrs($obfs_ibx, $v);
@@ -667,128 +658,146 @@ sub _msg_html_prepare {
 		$rv .= "From: $v\n" if $v ne '';
 	}
 	foreach my $h (qw(To Cc)) {
-		defined($v = $hdr->header($h)) or next;
-		fold_addresses($v);
-		$v = ascii_html($v);
-		obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
-		$rv .= "$h: $v\n" if $v ne '';
+		for my $v ($hdr->header($h)) {
+			fold_addresses($v);
+			$v = ascii_html($v);
+			obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
+			$rv .= "$h: $v\n" if $v ne '';
+		}
 	}
-	if (defined($v = $hdr->header('Subject')) && ($v ne '')) {
-		$v = ascii_html($v);
-		obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
-		if ($srch) {
-			$rv .= qq(Subject: $v\n);
-		} else {
-			$rv .= "Subject: $v\n";
+	my @subj = $hdr->header('Subject');
+	if (@subj) {
+		for my $v (@subj) {
+			$v = ascii_html($v);
+			obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
+			$rv .= 'Subject: ';
+			if ($over) {
+				$rv .= qq($v\n);
+			} else {
+				$rv .= "$v\n";
+			}
+			$title[0] //= $v;
 		}
-		$title[0] = $v;
 	} else { # dummy anchor for thread skeleton at bottom of page
-		$rv .= qq() if $srch;
+		$rv .= qq() if $over;
 		$title[0] = '(no subject)';
 	}
-	if (defined($v = $hdr->header('Date'))) {
+	for my $v ($hdr->header('Date')) {
 		$v = ascii_html($v);
 		obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; # possible :P
 		$rv .= "Date: $v\n";
 	}
 	$ctx->{-title_html} = join(' - ', @title);
-	foreach (@$mids) {
-		my $mid = PublicInbox::Hval->new_msgid($_) ;
+	if (scalar(@$mids) == 1) { # common case
+		my $mid = PublicInbox::Hval->new_msgid($mids->[0]);
 		my $mhtml = $mid->as_html;
-		if ($multiple) {
-			my $href = $mid->{href};
-			$rv .= "Message-ID: ";
-			$rv .= "";
-			$rv .= "<$mhtml> ";
-			$rv .= "(raw)\n";
-		} else {
-			$rv .= "Message-ID: <$mhtml> ";
-			$rv .= "(raw)\n";
+		$rv .= "Message-ID: <$mhtml> ";
+		$rv .= "(raw)\n";
+	} else {
+		# X-Alt-Message-ID can happen if a message is injected from
+		# public-inbox-nntpd because of multiple Message-ID headers.
+		my $lnk = PublicInbox::Linkify->new;
+		my $s = '';
+		for my $h (qw(Message-ID X-Alt-Message-ID)) {
+			$s .= "$h: $_\n" for ($hdr->header_raw($h));
 		}
+		$lnk->linkify_mids('..', \$s, 1);
+		$rv .= $s;
 	}
-	$rv .= _parent_headers($hdr, $srch);
+	$rv .= _parent_headers($hdr, $over);
 	$rv .= "\n";
 }
 
-sub thread_skel {
-	my ($dst, $ctx, $hdr, $tpfx) = @_;
-	my $srch = $ctx->{srch};
-	my $mid = mids($hdr)->[0];
-	my ($nr, $msgs) = $srch->get_thread($mid);
-	my $expand = qq(expand[flat) .
-	                qq(|nested]  ) .
-			qq(mbox.gz  ) .
-			qq(Atom feed);
+sub SKEL_EXPAND () {
+	qq(expand[flat) .
+		qq(|nested]  ) .
+		qq(mbox.gz  ) .
+		qq(Atom feed);
+}
 
+sub thread_skel ($$$) {
+	my ($skel, $ctx, $hdr) = @_;
+	my $mid = mids($hdr)->[0];
+	my $ibx = $ctx->{-inbox};
+	my ($nr, $msgs) = $ibx->over->get_thread($mid);
 	my $parent = in_reply_to($hdr);
-	$$dst .= "\nThread overview: ";
+	$$skel .= "\nThread overview: ";
 	if ($nr <= 1) {
 		if (defined $parent) {
-			$$dst .= "$expand\n ";
-			$$dst .= ghost_parent("$tpfx../", $parent) . "\n";
+			$$skel .= SKEL_EXPAND."\n ";
+			$$skel .= ghost_parent('../', $parent) . "\n";
 		} else {
-			$$dst .= "[no followups] $expand\n";
+			$$skel .= '[no followups] '.SKEL_EXPAND."\n";
 		}
 		$ctx->{next_msg} = undef;
 		$ctx->{parent_msg} = $parent;
 		return;
 	}
 
-	$$dst .= "$nr+ messages / $expand";
-	$$dst .= qq!  top\n!;
+	$$skel .= $nr;
+	$$skel .= '+ messages / '.SKEL_EXPAND.qq!  top\n!;
 
-	my $subj = $hdr->header('Subject');
-	defined $subj or $subj = '';
-	$ctx->{prev_subj} = [ split(/ /, $srch->subject_normalized($subj)) ];
+	# nb: mutt only shows the first Subject in the index pane
+	# when multiple Subject: headers are present, so we follow suit:
+	my $subj = $hdr->header('Subject') // '';
+	$subj = '(no subject)' if $subj eq '';
+	$ctx->{prev_subj} = [ split(/ /, subject_normalized($subj)) ];
 	$ctx->{cur} = $mid;
 	$ctx->{prev_attr} = '';
 	$ctx->{prev_level} = 0;
-	$ctx->{dst} = $dst;
+	$ctx->{skel} = $skel;
 
 	# reduce hash lookups in skel_dump
-	my $ibx = $ctx->{-inbox};
 	$ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
-	walk_thread(thread_results($ctx, $msgs), $ctx, *skel_dump);
+	walk_thread(thread_results($ctx, $msgs), $ctx, \&skel_dump);
 
 	$ctx->{parent_msg} = $parent;
 }
 
 sub _parent_headers {
-	my ($hdr, $srch) = @_;
+	my ($hdr, $over) = @_;
 	my $rv = '';
-
-	my $refs = references($hdr);
-	my $irt = pop @$refs;
-	if (defined $irt) {
-		my $v = PublicInbox::Hval->new_msgid($irt);
-		my $html = $v->as_html;
-		my $href = $v->{href};
-		$rv .= "In-Reply-To: <";
-		$rv .= "$html>\n";
+	my @irt = $hdr->header_raw('In-Reply-To');
+	my $refs;
+	if (@irt) {
+		my $lnk = PublicInbox::Linkify->new;
+		$rv .= "In-Reply-To: $_\n" for @irt;
+		$lnk->linkify_mids('..', \$rv);
+	} else {
+		$refs = references($hdr);
+		my $irt = pop @$refs;
+		if (defined $irt) {
+			my $v = PublicInbox::Hval->new_msgid($irt);
+			my $html = $v->as_html;
+			my $href = $v->{href};
+			$rv .= "In-Reply-To: <";
+			$rv .= "$html>\n";
+		}
 	}
 
 	# do not display References: if search is present,
 	# we show the thread skeleton at the bottom, instead.
-	return $rv if $srch;
+	return $rv if $over;
 
+	$refs //= references($hdr);
 	if (@$refs) {
-		@$refs = map { linkify_ref_nosrch($_) } @$refs;
+		@$refs = map { linkify_ref_no_over($_) } @$refs;
 		$rv .= 'References: '. join("\n\t", @$refs) . "\n";
 	}
 	$rv;
 }
 
+# returns a string buffer via ->getline
 sub html_footer {
-	my ($hdr, $standalone, $ctx, $rhref) = @_;
-
-	my $srch = $ctx->{srch} if $ctx;
+	my ($ctx) = @_;
+	my $ibx = $ctx->{-inbox};
+	my $hdr = delete $ctx->{hdr};
 	my $upfx = '../';
-	my $tpfx = '';
-	my $idx = $standalone ? " index" : '';
-	my $irt = '';
-	if ($idx && $srch) {
-		$idx .= "\n";
-		thread_skel(\$idx, $ctx, $hdr, $tpfx);
+	my $skel = " index";
+	my $rv = '
';
+	if ($ibx->over) {
+		$skel .= "\n";
+		thread_skel(\$skel, $ctx, $hdr);
 		my ($next, $prev);
 		my $parent = '       ';
 		$next = $prev = '    ';
@@ -817,16 +826,15 @@ sub html_footer {
 		} elsif ($u) { # unlikely
 			$parent = " parent";
 		}
-		$irt = "$next $prev$parent ";
-	} else {
-		$irt = '';
+		$rv .= "$next $prev$parent ";
 	}
-	$rhref ||= '#R';
-	$irt .= qq(reply);
-	$irt .= $idx;
+	$rv .= qq(reply);
+	$rv .= $skel;
+	$rv .= '
'; + $rv .= msg_reply($ctx, $hdr); } -sub linkify_ref_nosrch { +sub linkify_ref_no_over { my $v = PublicInbox::Hval->new_msgid($_[0]); my $html = $v->as_html; my $href = $v->{href}; @@ -877,8 +885,7 @@ sub strict_loose_note ($) { sub thread_results { my ($ctx, $msgs) = @_; require PublicInbox::SearchThread; - my $ibx = $ctx->{-inbox}; - my $rootset = PublicInbox::SearchThread::thread($msgs, *sort_ds, $ibx); + my $rootset = PublicInbox::SearchThread::thread($msgs, \&sort_ds, $ctx); # FIXME: `tid' is broken on --reindex, so that needs to be fixed # and preserved in the future. This bug is hidden by `sid' matches @@ -892,7 +899,7 @@ sub thread_results { if (defined($mid) && scalar(@$rootset) > 1) { $ctx->{root_idx} = -1; my $nr = scalar @$msgs; - walk_thread($rootset, $ctx, *find_mid_root); + walk_thread($rootset, $ctx, \&find_mid_root); my $idx = $ctx->{found_mid_at}; if (defined($idx) && $idx != 0) { my $tip = splice(@$rootset, $idx, 1); @@ -910,8 +917,6 @@ sub missing_thread { PublicInbox::ExtMsg::ext_msg($ctx); } -sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) } - sub dedupe_subject { my ($prev_subj, $subj, $val) = @_; @@ -931,23 +936,33 @@ sub dedupe_subject { $omit; } -sub skel_dump { +sub skel_dump { # walk_thread callback my ($ctx, $level, $node) = @_; my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node); - my $dst = $ctx->{dst}; + my $skel = $ctx->{skel}; my $cur = $ctx->{cur}; my $mid = $smsg->{mid}; if ($level == 0 && $ctx->{skel_dump_roots}++) { - $$dst .= delete $ctx->{sl_note} || ''; + $$skel .= delete($ctx->{sl_note}) || ''; } my $f = ascii_html($smsg->from_name); my $obfs_ibx = $ctx->{-obfs_ibx}; obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx; - my $d = fmt_ts($smsg->{ds}) . ' ' . indent_for($level) . th_pfx($level); + my $d = fmt_ts($smsg->{ds}); + my $unmatched; # if lazy-loaded by SearchThread::Msg::visible() + if (exists $ctx->{searchview}) { + if (defined(my $pct = $smsg->{pct})) { + $d .= (sprintf(' % 2u', $pct) . '%'); + } else { + $unmatched = 1; + $d .= ' '; + } + } + $d .= ' ' . indent_for($level) . th_pfx($level); my $attr = $f; $ctx->{first_level} ||= $level; @@ -959,7 +974,7 @@ sub skel_dump { if ($cur) { if ($cur eq $mid) { delete $ctx->{cur}; - $$dst .= "$d". + $$skel .= "$d". "$attr [this message]\n"; return 1; } else { @@ -972,8 +987,7 @@ sub skel_dump { # Subject is never undef, this mail was loaded from # our Xapian which would've resulted in '' if it were # really missing (and Filter rejects empty subjects) - my @subj = split(/ /, $ctx->{srch}->subject_normalized($smsg->subject)); - + my @subj = split(/ /, subject_normalized($smsg->subject)); # remove common suffixes from the subject if it matches the previous, # so we do not show redundant text at the end. my $prev_subj = $ctx->{prev_subj} || []; @@ -990,7 +1004,7 @@ sub skel_dump { } my $m; my $id = ''; - my $mapping = $ctx->{mapping}; + my $mapping = $unmatched ? undef : $ctx->{mapping}; if ($mapping) { my $map = $mapping->{$mid}; $id = id_compress($mid, 1); @@ -1000,7 +1014,7 @@ sub skel_dump { } else { $m = $ctx->{-upfx}.mid_escape($mid).'/'; } - $$dst .= $d . "" . $end; + $$skel .= $d . "" . $end; 1; } @@ -1008,8 +1022,8 @@ sub _skel_ghost { my ($ctx, $level, $node) = @_; my $mid = $node->{id}; - my $d = $ctx->{pct} ? ' [irrelevant] ' # search result - : ' [not found] '; + my $d = ' [not found] '; + $d .= ' ' if exists $ctx->{searchview}; $d .= indent_for($level) . th_pfx($level); my $upfx = $ctx->{-upfx}; my $m = PublicInbox::Hval->new_msgid($mid); @@ -1025,8 +1039,7 @@ sub _skel_ghost { } else { $d .= qq{<$html>\n}; } - my $dst = $ctx->{dst}; - $$dst .= $d; + ${$ctx->{skel}} .= $d; 1; } @@ -1039,16 +1052,16 @@ sub sort_ds { # accumulate recent topics if search is supported # returns 200 if done, 404 if not -sub acc_topic { +sub acc_topic { # walk_thread callback my ($ctx, $level, $node) = @_; - my $srch = $ctx->{srch}; my $mid = $node->{id}; my $x = $node->{smsg} || $ctx->{-inbox}->smsg_by_mid($mid); my ($subj, $ds); my $topic; if ($x) { $subj = $x->subject; - $subj = $srch->subject_normalized($subj); + $subj = subject_normalized($subj); + $subj = '(no subject)' if $subj eq ''; $ds = $x->ds; if ($level == 0) { $topic = [ $ds, 1, { $subj => $mid }, $subj ]; @@ -1087,7 +1100,6 @@ sub dump_topics { my @out; my $ibx = $ctx->{-inbox}; my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef; - my $srch = $ctx->{srch}; # sort by recency, this allows new posts to "bump" old topics... foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) { @@ -1119,7 +1131,7 @@ sub dump_topics { my $level = $ex[$i]; my $subj = $ex[$i + 1]; $mid = delete $seen->{$subj}; - my @subj = split(/ /, $srch->subject_normalized($subj)); + my @subj = split(/ /, subject_normalized($subj)); my @next_prev = @subj; # full copy my $omit = dedupe_subject($prev_subj, \@subj, ' "'); $prev_subj = \@next_prev; @@ -1135,10 +1147,8 @@ sub dump_topics { 200; } -sub ts2str ($) { - my ($ts) = @_; - POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts)); -} +# only for the t= query parameter passed to overview DB +sub ts2str ($) { strftime('%Y%m%d%H%M%S', gmtime($_[0])) }; sub str2ts ($) { my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]); @@ -1171,8 +1181,8 @@ sub paginate_recent ($$) { # Xapian uses '..' but '-' is perhaps friendier to URL linkifiers # if only $after exists "YYYYMMDD.." because "." could be skipped # if interpreted as an end-of-sentence - $t =~ s/\A(\d{8,14})-// and $after = str2ts($1); - $t =~ /\A(\d{8,14})\z/ and $before = str2ts($1); + $t =~ s/\A([0-9]{8,14})-// and $after = str2ts($1); + $t =~ /\A([0-9]{8,14})\z/ and $before = str2ts($1); my $ibx = $ctx->{-inbox}; my $msgs = $ibx->recent($opts, $after, $before); @@ -1208,9 +1218,9 @@ sub index_topics { my ($ctx) = @_; my $msgs = paginate_recent($ctx, 200); # 200 is our window if (@$msgs) { - walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic); + walk_thread(thread_results($ctx, $msgs), $ctx, \&acc_topic); } - PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav); + PublicInbox::WwwStream->response($ctx, dump_topics($ctx), \&index_nav); } sub thread_adj_level {