user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
blob dd3155befefbd2d628683e6099847e72a34aec5a 2367 bytes (raw)
name: lib/PublicInbox/ContentId.pm 	 # note: path name is non-authoritative(*)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 
# Copyright (C) 2018 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

package PublicInbox::ContentId;
use strict;
use warnings;
use base qw/Exporter/;
our @EXPORT_OK = qw/content_id content_digest/;
use PublicInbox::MID qw(mids references);
use PublicInbox::MsgIter;

# not sure if less-widely supported hash families are worth bothering with
use Digest::SHA;

sub digest_addr ($$$) {
	my ($dig, $h, $v) = @_;
	$v =~ tr/"//d;
	$v =~ s/@([a-z0-9\_\.\-\(\)]*([A-Z])\S*)/'@'.lc($1)/ge;
	utf8::encode($v);
	$dig->add("$h\0$v\0");
}

sub content_digest ($) {
	my ($mime) = @_;
	my $dig = Digest::SHA->new(256);
	my $hdr = $mime->header_obj;

	# References: and In-Reply-To: get used interchangeably
	# in some "duplicates" in LKML.  We treat them the same
	# in SearchIdx, so treat them the same for this:
	my %seen;
	foreach my $mid (@{mids($hdr)}) {
		# do NOT consider the Message-ID as part of the content_id
		# if we got here, we've already got Message-ID reuse
		$seen{$mid} = 1;
	}
	foreach my $mid (@{references($hdr)}) {
		next if $seen{$mid};
		$dig->add("ref\0$mid\0");
	}

	# Only use Sender: if From is not present
	foreach my $h (qw(From Sender)) {
		my @v = $hdr->header($h);
		if (@v) {
			digest_addr($dig, $h, $_) foreach @v;
		}
	}
	foreach my $h (qw(Subject Date)) {
		my @v = $hdr->header($h);
		foreach my $v (@v) {
			utf8::encode($v);
			$dig->add("$h\0$v\0");
		}
	}
	# 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, undef) = msg_part_text($part, $ct);
		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;
}

1;

debug log:

solving dd3155b ...
found dd3155b in https://80x24.org/public-inbox.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

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).