]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Import.pm
import: switch to URL-safe Base64 for Message-IDs
[public-inbox.git] / lib / PublicInbox / Import.pm
index 12df7d598fc71d38088d4aeaebd0ed6d8ed3e37d..77e74c13af6cf66797685f770838bb6ab1995a87 100644 (file)
@@ -9,9 +9,10 @@ use strict;
 use warnings;
 use Fcntl qw(:flock :DEFAULT);
 use PublicInbox::Spawn qw(spawn);
-use PublicInbox::MID qw(mid_mime mid2path);
+use PublicInbox::MID qw(mids mid_mime mid2path);
 use PublicInbox::Address;
 use PublicInbox::MsgTime qw(msg_timestamp);
+use PublicInbox::ContentId qw(content_digest);
 
 sub new {
        my ($class, $git, $name, $email, $ibx) = @_;
@@ -308,7 +309,12 @@ sub add {
 
        my $path;
        if ($path_type eq '2/38') {
-               $path = mid2path(mid_mime($mime));
+               my $mids = mids($mime->header_obj);
+               if (!scalar(@$mids)) {
+                       my $dig = content_digest($mime);
+                       @$mids = (digest2mid($dig));
+               }
+               $path = mid2path($mids->[0]);
        } else { # v2 layout, one file:
                $path = 'm';
        }
@@ -393,6 +399,20 @@ sub atfork_child {
        }
 }
 
+sub digest2mid ($) {
+       my ($dig) = @_;
+       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';
+}
+
 1;
 __END__
 =pod