user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 2/5] search: avoid creating ghosts for circular References
Date: Tue, 18 Aug 2015 01:21:07 +0000	[thread overview]
Message-ID: <1439860870-8086-2-git-send-email-e@80x24.org> (raw)
In-Reply-To: <1439860870-8086-1-git-send-email-e@80x24.org>

Some mail software incorrectly creates circular references
and causes us to create ghosts before the actual mail doc
is created.
---
 lib/PublicInbox/Search.pm | 43 ++++++++++++++++++++++++++++++-------------
 t/search.t                | 19 +++++++++++++++++++
 2 files changed, 49 insertions(+), 13 deletions(-)

diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm
index 617c267..db86301 100644
--- a/lib/PublicInbox/Search.pm
+++ b/lib/PublicInbox/Search.pm
@@ -79,8 +79,8 @@ sub add_message {
 	my $db = $self->{xdb};
 
 	my $doc_id;
-	my $mid = mid_clean($mime->header_obj->header_raw('Message-ID'));
-	$mid = mid_compressed($mid);
+	my $mid_orig = mid_clean($mime->header_obj->header_raw('Message-ID'));
+	my $mid = mid_compressed($mid_orig);
 	my $was_ghost = 0;
 	my $ct_msg = $mime->header('Content-Type') || 'text/plain';
 	my $enc_msg = PublicInbox::View::enc_for($ct_msg);
@@ -176,7 +176,7 @@ sub add_message {
 	};
 
 	if ($@) {
-		warn "failed to index message <$mid>: $@\n";
+		warn "failed to index message <$mid_orig>: $@\n";
 		return undef;
 	}
 	$doc_id;
@@ -184,11 +184,11 @@ sub add_message {
 
 # returns deleted doc_id on success, undef on missing
 sub remove_message {
-	my ($self, $mid) = @_;
+	my ($self, $mid_orig) = @_;
 	my $db = $self->{xdb};
 	my $doc_id;
-	$mid = mid_clean($mid);
-	$mid = mid_compressed($mid);
+	$mid_orig = mid_clean($mid_orig);
+	my $mid = mid_compressed($mid_orig);
 
 	eval {
 		$doc_id = $self->find_unique_doc_id('mid', $mid);
@@ -196,7 +196,7 @@ sub remove_message {
 	};
 
 	if ($@) {
-		warn "failed to remove message <$mid>: $@\n";
+		warn "failed to remove message <$mid_orig>: $@\n";
 		return undef;
 	}
 	$doc_id;
@@ -347,16 +347,33 @@ sub link_message_to_parents {
 		if ($irt =~ /<([^>]+)>/) {
 			$irt = $1;
 		}
-		push @refs, $irt;
+
+		# maybe some crazies will try to make a circular reference:
+		if ($irt eq $mid) {
+			$irt = undef;
+		} else {
+			push @refs, $irt;
+		}
 	}
 
 	my $tid;
 	if (@refs) {
-		@refs = map { mid_compressed($_) } @refs;
-		my %uniq;
-		@refs = grep { !$uniq{$_}++ } @refs; # uniq
-
-		$doc->add_term(xpfx('inreplyto') . $refs[-1]);
+		my @crefs = map { mid_compressed($_) } @refs;
+		my %uniq = ($mid => 1);
+
+		# prevent circular references via References: here:
+		@refs = ();
+		foreach my $ref (@crefs) {
+			next if $uniq{$ref};
+			$uniq{$ref} = 1;
+			push @refs, $ref;
+		}
+		$irt = undef if (defined $irt && !$uniq{$irt});
+	}
+	if (@refs) {
+		if (defined $irt) {
+			$doc->add_term(xpfx('inreplyto') . $irt);
+		}
 
 		my $ref_pfx = xpfx('references');
 
diff --git a/t/search.t b/t/search.t
index 0ad0886..55abe9e 100644
--- a/t/search.t
+++ b/t/search.t
@@ -243,6 +243,25 @@ sub filter_mids {
 		"quoted result returned if nothing else");
 }
 
+# circular references
+{
+	my $doc_id = $rw->add_message(Email::MIME->create(
+		header_str => [
+			Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
+			Subject => 'Circle',
+			'Message-ID' => '<circle@a>',
+			'References' => '<circle@a>',
+			'In-Reply-To' => '<circle@a>',
+			From => 'Circle <circle@example.com>',
+			To => 'list@example.com',
+		],
+		body => "LOOP!\n"));
+	ok($doc_id > 0, "doc_id defined with circular reference");
+	my $smsg = $rw->lookup_message('circle@a');
+	$smsg->ensure_metadata;
+	is($smsg->{references}, undef, "no references created");
+}
+
 done_testing();
 
 1;
-- 
EW


  reply	other threads:[~2015-08-18  1:21 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-18  1:21 [PATCH 1/5] view: cleaner Message-ID filtering for References Eric Wong
2015-08-18  1:21 ` Eric Wong [this message]
2015-08-18  1:21 ` [PATCH 3/5] search: common Subject: normalization for Re: prefixes Eric Wong
2015-08-18  1:21 ` [PATCH 4/5] search: expose $PublicInbox::Search::LANG variable Eric Wong
2015-08-18  1:21 ` [PATCH 5/5] search: bump SCHEMA_VERSION to 4 Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1439860870-8086-2-git-send-email-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).