X-Git-Url: http://www.git.stargrave.org/?p=public-inbox.git;a=blobdiff_plain;f=lib%2FPublicInbox%2FView.pm;h=58851edc59f5226bc47a4c6ba3f7627a19ba3e00;hp=c151f221cc9ea1667fafd20253157c1bc9d7c7a5;hb=55b22e6414b78336633ca243c113c712af2c3bcd;hpb=f886760b1b5c79181143743814a576a8fa5cf4d9 diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm index c151f221..58851edc 100644 --- a/lib/PublicInbox/View.pm +++ b/lib/PublicInbox/View.pm @@ -15,7 +15,9 @@ use PublicInbox::Address; use PublicInbox::WwwStream; use PublicInbox::Reply; require POSIX; +use Time::Local qw(timegm); +use constant COLS => 72; use constant INDENT => ' '; use constant TCHILD => '` '; sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD }; @@ -57,20 +59,16 @@ sub msg_page { my ($ctx) = @_; my $mid = $ctx->{mid}; my $ibx = $ctx->{-inbox}; - my ($first, $more, $head, $tail, $db); + my ($first, $more); my $smsg; if (my $srch = $ibx->search) { - $srch->retry_reopen(sub { - ($head, $tail, $db) = $srch->each_smsg_by_mid($mid); - for (; !defined($first) && $head != $tail; $head++) { - my @args = ($head, $db, $mid); - $smsg = PublicInbox::SearchMsg->get(@args); - $first = $ibx->msg_by_smsg($smsg); - } - if ($head != $tail) { - $more = [ $head, $tail, $db ]; - } - }); + my ($id, $prev); + $smsg = $srch->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; + } return unless $first; } else { $first = $ibx->msg_by_mid($mid) or return; @@ -81,18 +79,11 @@ sub msg_page { sub msg_html_more { my ($ctx, $more, $nr) = @_; my $str = eval { - my $smsg; - my ($head, $tail, $db) = @$more; + my ($id, $prev, $smsg) = @$more; my $mid = $ctx->{mid}; - for (; !defined($smsg) && $head != $tail; $head++) { - my $m = PublicInbox::SearchMsg->get($head, $db, $mid); - $smsg = $ctx->{-inbox}->smsg_mime($m); - } - if ($head == $tail) { # done - @$more = (); - } else { - $more->[0] = $head; - } + $smsg = $ctx->{-inbox}->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) . '/'; @@ -174,6 +165,24 @@ sub in_reply_to { $refs->[-1]; } +sub fold_addresses ($) { + return $_[0] if length($_[0]) <= COLS; + # try to fold on commas after non-word chars before $lim chars, + # Try to get the "," preceeded by ">" or ")", but avoid folding + # on the comma where somebody uses "Lastname, Firstname". + # We also try to keep the last and penultimate addresses in + # the list on the same line if possible, hence the extra \z + # Fall back to folding on spaces at $lim + 1 chars + my $lim = COLS - 8; # 8 = "\t" display width + my $too_long = $lim + 1; + $_[0] =~ s/\s*\z//s; # Email::Simple doesn't strip trailing spaces + $_[0] = join("\n\t", + ($_[0] =~ /(.{0,$lim}\W(?:,|\z)| + .{1,$lim}(?:,|\z)| + .{1,$lim}| + .{$too_long,}?)(?:\s|\z)/xgo)); +} + sub _hdr_names_html ($$) { my ($hdr, $field) = @_; my $val = $hdr->header($field) or return ''; @@ -208,13 +217,6 @@ sub index_entry { my @tocc; my $mime = $smsg->{mime}; my $hdr = $mime->header_obj; - foreach my $f (qw(To Cc)) { - my $dst = _hdr_names_html($hdr, $f); - if ($dst ne '') { - obfuscate_addrs($obfs_ibx, $dst) if $obfs_ibx; - push @tocc, "$f: $dst"; - } - } my $from = _hdr_names_html($hdr, 'From'); obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx; $rv .= "From: $from @ ".fmt_ts($smsg->ds)." UTC"; @@ -222,7 +224,24 @@ sub index_entry { my $mhref = $upfx . mid_escape($mid_raw) . '/'; $rv .= qq{ (permalink / }; $rv .= qq{raw)\n}; - $rv .= ' '.join('; +', @tocc) . "\n" if @tocc; + my $to = fold_addresses(_hdr_names_html($hdr, 'To')); + my $cc = fold_addresses(_hdr_names_html($hdr, 'Cc')); + my ($tlen, $clen) = (length($to), length($cc)); + my $to_cc = ''; + if (($tlen + $clen) > COLS) { + $to_cc .= ' To: '.$to."\n" if $tlen; + $to_cc .= ' Cc: '.$cc."\n" if $clen; + } else { + if ($tlen) { + $to_cc .= ' To: '.$to; + $to_cc .= '; +Cc: '.$cc if $clen; + } else { + $to_cc .= ' Cc: '.$cc if $clen; + } + $to_cc .= "\n"; + } + obfuscate_addrs($obfs_ibx, $to_cc) if $obfs_ibx; + $rv .= $to_cc; my $mapping = $ctx->{mapping}; if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) { @@ -408,9 +427,7 @@ sub thread_html { my ($ctx) = @_; my $mid = $ctx->{mid}; my $srch = $ctx->{srch}; - my $sres = $srch->get_thread($mid); - my $msgs = $sres->{msgs}; - my $nr = $sres->{total}; + my ($nr, $msgs) = $srch->get_thread($mid); return missing_thread($ctx) if $nr == 0; my $skel = '
';
 	$skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
@@ -603,29 +620,43 @@ sub _msg_html_prepare {
 		$ctx->{-upfx} = '../';
 	}
 	my @title;
-	foreach my $h (qw(From To Cc Subject Date)) {
-		my $v = $hdr->header($h);
-		defined($v) && ($v ne '') or next;
+	my $v;
+	if (defined($v = $hdr->header('From'))) {
 		$v = PublicInbox::Hval->new($v);
-
-		if ($h eq 'From') {
-			my @n = PublicInbox::Address::names($v->raw);
-			$title[1] = ascii_html(join(', ', @n));
-			obfuscate_addrs($obfs_ibx, $title[1]) if $obfs_ibx;
-		} elsif ($h eq 'Subject') {
-			$title[0] = $v->as_html;
-			if ($srch) {
-				$rv .= qq($h: );
-				$rv .= $v->as_html . "\n";
-				next;
-			}
-		}
+		my @n = PublicInbox::Address::names($v->raw);
+		$title[1] = ascii_html(join(', ', @n));
 		$v = $v->as_html;
+		if ($obfs_ibx) {
+			obfuscate_addrs($obfs_ibx, $v);
+			obfuscate_addrs($obfs_ibx, $title[1]);
+		}
+		$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";
-
+		$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";
+		}
+		$title[0] = $v;
+	} else { # dummy anchor for thread skeleton at bottom of page
+		$rv .= qq() if $srch;
+		$title[0] = '(no subject)';
+	}
+	if (defined($v = $hdr->header('Date'))) {
+		$v = ascii_html($v);
+		obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; # possible :P
+		$rv .= "Date: $v\n";
 	}
-	$title[0] ||= '(no subject)';
 	$ctx->{-title_html} = join(' - ', @title);
 	foreach (@$mids) {
 		my $mid = PublicInbox::Hval->new_msgid($_) ;
@@ -649,8 +680,7 @@ sub thread_skel {
 	my ($dst, $ctx, $hdr, $tpfx) = @_;
 	my $srch = $ctx->{srch};
 	my $mid = mids($hdr)->[0];
-	my $sres = $srch->get_thread($mid);
-	my $nr = $sres->{total};
+	my ($nr, $msgs) = $srch->get_thread($mid);
 	my $expand = qq(expand[flat) .
 	                qq(|nested]  ) .
 			qq(mbox.gz  ) .
@@ -680,12 +710,11 @@ sub thread_skel {
 	$ctx->{prev_attr} = '';
 	$ctx->{prev_level} = 0;
 	$ctx->{dst} = $dst;
-	$sres = $sres->{msgs};
 
 	# reduce hash lookups in skel_dump
 	my $ibx = $ctx->{-inbox};
 	$ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
-	walk_thread(thread_results($ctx, $sres), $ctx, *skel_dump);
+	walk_thread(thread_results($ctx, $msgs), $ctx, *skel_dump);
 
 	$ctx->{parent_msg} = $parent;
 }
@@ -694,7 +723,8 @@ sub _parent_headers {
 	my ($hdr, $srch) = @_;
 	my $rv = '';
 
-	my $irt = in_reply_to($hdr);
+	my $refs = references($hdr);
+	my $irt = pop @$refs;
 	if (defined $irt) {
 		my $v = PublicInbox::Hval->new_msgid($irt);
 		my $html = $v->as_html;
@@ -707,22 +737,9 @@ sub _parent_headers {
 	# we show the thread skeleton at the bottom, instead.
 	return $rv if $srch;
 
-	my $refs = $hdr->header_raw('References');
-	if ($refs) {
-		# avoid redundant URLs wasting bandwidth
-		my %seen;
-		$seen{$irt} = 1 if defined $irt;
-		my @refs;
-		my @raw_refs = ($refs =~ /<([^>]+)>/g);
-		foreach my $ref (@raw_refs) {
-			next if $seen{$ref};
-			$seen{$ref} = 1;
-			push @refs, linkify_ref_nosrch($ref);
-		}
-
-		if (@refs) {
-			$rv .= 'References: '. join("\n\t", @refs) . "\n";
-		}
+	if (@$refs) {
+		@$refs = map { linkify_ref_nosrch($_) } @$refs;
+		$rv .= 'References: '. join("\n\t", @$refs) . "\n";
 	}
 	$rv;
 }
@@ -1036,59 +1053,81 @@ sub dump_topics {
 	200;
 }
 
+sub ts2str ($) {
+	my ($ts) = @_;
+	POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts));
+}
+
+sub str2ts ($) {
+	my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]);
+	timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
+}
+
+sub pagination_footer ($$) {
+	my ($ctx, $latest) = @_;
+	delete $ctx->{qp} or return;
+	my $next = $ctx->{next_page} || '';
+	my $prev = $ctx->{prev_page} || '';
+	if ($prev) {
+		$next = $next ? "$next " : '     ';
+		$prev .= qq! latest!;
+	}
+	"
page: $next$prev
"; +} + sub index_nav { # callback for WwwStream my (undef, $ctx) = @_; - delete $ctx->{qp} or return; - my ($next, $prev); - $next = $prev = ' '; - my $latest = ''; + pagination_footer($ctx, '.') +} - my $next_o = $ctx->{-next_o}; - if ($next_o) { - $next = qq!next!; +sub paginate_recent ($$) { + my ($ctx, $lim) = @_; + my $t = $ctx->{qp}->{t} || ''; + my $opts = { limit => $lim }; + my ($after, $before); + + # 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); + + my $ibx = $ctx->{-inbox}; + my $msgs = $ibx->recent($opts, $after, $before); + my $nr = scalar @$msgs; + if ($nr < $lim && defined($after)) { + $after = $before = undef; + $msgs = $ibx->recent($opts); + $nr = scalar @$msgs; } - if (my $cur_o = $ctx->{-cur_o}) { - $latest = qq! latest!; - - my $o = $cur_o - ($next_o - $cur_o); - if ($o > 0) { - $prev = qq!prev!; - } elsif ($o == 0) { - $prev = qq!prev!; + my $more = $nr == $lim; + my ($newest, $oldest); + if ($nr) { + $newest = $msgs->[0]->{ts}; + $oldest = $msgs->[-1]->{ts}; + # if we only had $after, our SQL query in ->recent ordered + if ($newest < $oldest) { + ($oldest, $newest) = ($newest, $oldest); + $more = 0 if defined($after) && $after < $oldest; } } - "
page: $next $prev$latest
"; + if (defined($oldest) && $more) { + my $s = ts2str($oldest); + $ctx->{next_page} = qq!next!; + } + if (defined($newest) && (defined($before) || defined($after))) { + my $s = ts2str($newest); + $ctx->{prev_page} = qq!prev!; + } + $msgs; } sub index_topics { my ($ctx) = @_; - my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/); - my $lim = 200; - my $opts = { offset => $off, limit => $lim }; - - $ctx->{order} = []; - my $srch = $ctx->{srch}; - - my $qs = ''; - # this complicated bit cuts loading time by over 400ms on my system: - if ($off == 0) { - my ($min, $max) = $ctx->{-inbox}->mm->minmax; - my $n = $max - $lim; - $n = $min if $n < $min; - for (; $qs eq '' && $n >= $min; --$n) { - my $smsg = $srch->lookup_article($n) or next; - $qs = POSIX::strftime('d:%Y%m%d..', gmtime($smsg->ts)); - } - } - - my $sres = $srch->query($qs, $opts); - $sres = $sres->{msgs}; - my $nr = scalar @$sres; - if ($nr) { - walk_thread(thread_results($ctx, $sres), $ctx, *acc_topic); + my $msgs = paginate_recent($ctx, 200); # 200 is our window + if (@$msgs) { + walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic); } - $ctx->{-next_o} = $off + $nr; - $ctx->{-cur_o} = $off; PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav); }