+ # Some mail processors will add " to unquoted names that were
+ # not in the original message. For the purposes of deduplication,
+ # do not take it into account:
+ foreach my $h (qw(To Cc)) {
+ my @v = $hdr->header($h);
+ digest_addr($dig, $h, $_) foreach @v;
+ }
+ msg_iter($mime, sub {
+ my ($part, $depth, @idx) = @{$_[0]};
+ $dig->add("\0$depth:".join('.', @idx)."\0");
+ my $fn = $part->filename;
+ if (defined $fn) {
+ utf8::encode($fn);
+ $dig->add("fn\0$fn\0");
+ }
+ my @d = $part->header('Content-Description');
+ foreach my $d (@d) {
+ utf8::encode($d);
+ $dig->add("d\0$d\0");
+ }
+ $dig->add("b\0");
+ my $ct = $part->content_type || 'text/plain';
+ my $s = eval { $part->body_str };
+ if ($@ && $ct =~ m!\btext/plain\b!i) {
+ # Try to assume UTF-8 because Alpine
+ # seems to do wacky things and set
+ # charset=X-UNKNOWN
+ $part->charset_set('UTF-8');
+ $s = eval { $part->body_str };
+ }
+ if (defined $s) {
+ $s =~ s/\r\n/\n/gs;
+ $s =~ s/\s*\z//s;
+ utf8::encode($s);
+ } else {
+ $s = $part->body;
+ }
+ $dig->add($s);
+ });
+ $dig;
+}
+
+sub content_id ($) {
+ content_digest($_[0])->digest;