From 69329215485cf2ab9d8cd1fa7faf65d8ec42dc0b Mon Sep 17 00:00:00 2001 From: "Eric Wong (Contractor, The Linux Foundation)" Date: Wed, 18 Apr 2018 09:13:10 +0000 Subject: v2: generate better Message-IDs for duplicates While hunting duplicates, I noticed a leading '-' in some Message-IDs as a result of RFC4648 encoding. While '-' seems allowed by RFC5322 and URL-friendly (RFC4648), they are uncommon and make using Message-IDs as arguments for command-line tools more difficult. So prefix them with a datestamp to at least give readers some sense of the age. And shorten the "localhost" hostname to "z" to save space. --- lib/PublicInbox/Import.pm | 18 ++++++++++-------- lib/PublicInbox/V2Writable.pm | 6 +++--- 2 files changed, 13 insertions(+), 11 deletions(-) (limited to 'lib/PublicInbox') diff --git a/lib/PublicInbox/Import.pm b/lib/PublicInbox/Import.pm index 9e8900f3..c7a96e1e 100644 --- a/lib/PublicInbox/Import.pm +++ b/lib/PublicInbox/Import.pm @@ -14,6 +14,7 @@ use PublicInbox::Address; use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp); use PublicInbox::ContentId qw(content_digest); use PublicInbox::MDA; +use POSIX qw(strftime); sub new { my ($class, $git, $name, $email, $ibx) = @_; @@ -330,7 +331,7 @@ sub v1_mid0 ($) { my $mids = mids($hdr); if (!scalar(@$mids)) { # spam often has no Message-Id - my $mid0 = digest2mid(content_digest($mime)); + my $mid0 = digest2mid(content_digest($mime), $hdr); append_mid($hdr, $mid0); return $mid0; } @@ -445,18 +446,19 @@ sub atfork_child { } } -sub digest2mid ($) { - my ($dig) = @_; +sub digest2mid ($$) { + my ($dig, $hdr) = @_; my $b64 = $dig->clone->b64digest; # Make our own URLs nicer: # See "Base 64 Encoding with URL and Filename Safe Alphabet" in RFC4648 $b64 =~ tr!+/=!-_!d; - # We can make this more meaningful with a date prefix or other things, - # but this is only needed for crap that fails to generate a Message-ID - # or reuses one. In other words, it's usually spammers who hit this - # so they don't deserve nice Message-IDs :P - $b64 . '@localhost'; + # Add a date prefix to prevent a leading '-' in case that trips + # up some tools (e.g. if a Message-ID were a expected as a + # command-line arg) + my $dt = msg_datestamp($hdr); + $dt = POSIX::strftime('%Y%m%d%H%M%S', gmtime($dt)); + "$dt.$b64" . '@z'; } sub clean_purge_buffer { diff --git a/lib/PublicInbox/V2Writable.pm b/lib/PublicInbox/V2Writable.pm index 66f8a8a3..0dcdedae 100644 --- a/lib/PublicInbox/V2Writable.pm +++ b/lib/PublicInbox/V2Writable.pm @@ -174,19 +174,19 @@ sub num_for_harder { my $hdr = $mime->header_obj; my $dig = content_digest($mime); - $$mid0 = PublicInbox::Import::digest2mid($dig); + $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr); my $num = $self->{mm}->mid_insert($$mid0); unless (defined $num) { # it's hard to spoof the last Received: header my @recvd = $hdr->header_raw('Received'); $dig->add("Received: $_") foreach (@recvd); - $$mid0 = PublicInbox::Import::digest2mid($dig); + $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr); $num = $self->{mm}->mid_insert($$mid0); # fall back to a random Message-ID and give up determinism: until (defined($num)) { $dig->add(rand); - $$mid0 = PublicInbox::Import::digest2mid($dig); + $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr); warn "using random Message-ID <$$mid0> as fallback\n"; $num = $self->{mm}->mid_insert($$mid0); } -- cgit v1.2.3-24-ge0c7