]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ContentId.pm
content_id: use Sender header if From is not available
[public-inbox.git] / lib / PublicInbox / ContentId.pm
index d1a009e77f2ed9e9e8b4de30a79f85a706ef37e8..9082b7694002168467ab3d2610a36631348116d2 100644 (file)
@@ -5,19 +5,15 @@ package PublicInbox::ContentId;
 use strict;
 use warnings;
 use base qw/Exporter/;
-our @EXPORT_OK = qw/content_id/;
+our @EXPORT_OK = qw/content_id content_digest/;
 use PublicInbox::MID qw(mids references);
 
 # not sure if less-widely supported hash families are worth bothering with
 use Digest::SHA;
 
-# Content-* headers are often no-ops, so maybe we don't need them
-my @ID_HEADERS = qw(Subject From Date To Cc);
-
-sub content_id ($;$) {
-       my ($mime, $alg) = @_;
-       $alg ||= 256;
-       my $dig = Digest::SHA->new($alg);
+sub content_digest ($) {
+       my ($mime) = @_;
+       my $dig = Digest::SHA->new(256);
        my $hdr = $mime->header_obj;
 
        # References: and In-Reply-To: get used interchangeably
@@ -32,12 +28,27 @@ sub content_id ($;$) {
                next if $seen{$mid};
                $dig->add('ref: '.$mid);
        }
-       foreach my $h (@ID_HEADERS) {
+
+       # Only use Sender: if From is not present
+       foreach my $h (qw(From Sender)) {
+               my @v = $hdr->header_raw($h);
+               if (@v) {
+                       $dig->add("$h: $_") foreach @v;
+                       last;
+               }
+       }
+
+       # Content-* headers are often no-ops, so maybe we don't need them
+       foreach my $h (qw(Subject Date To Cc)) {
                my @v = $hdr->header_raw($h);
                $dig->add("$h: $_") foreach @v;
        }
        $dig->add($mime->body_raw);
-       'SHA-' . $dig->algorithm . ':' . $dig->hexdigest;
+       $dig;
+}
+
+sub content_id ($) {
+       content_digest($_[0])->digest;
 }
 
 1;