Fingerprint.pm

filter: Statementstmt 99.4% 171 / 172 Branchbran 95.4% 42 / 44 Conditioncond 86.6% 13 / 15 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 41 / 41 Podpod 100% 8 / 8 totaltotal 97.5% 282 / 289 CC 30 SCAR 12.3
CC
30
Cov
98%
checksum
9.0
read
7.0
_extract_headers
5.0
CRAP 3.4
« Fingerprint/App/EliminateDups.pm ↑ Summary Fingerprint/Cache/AnyDBM.pm »
1 package Email::Fingerprint;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Class::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Mail::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use List::MoreUtils qw( apply );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Email::Fingerprint - Calculate a digest for recognizing duplicate emails
17  
18 =head1 VERSION
19  
20 Version 0.48
21  
22 =cut
23  
24 7 our $VERSION = '0.48';
25  
26 =head1 SYNOPSIS
27  
28 Email::Fingerprint calculates a checksum that uniquely identifies an email,
29 for use in spotting duplicate messages. The checksum is based on: the
30 Message-ID: header; or if it doesn't exist, on the Date:, From:,
31 To: and Cc: headers together; or if those don't exist, on the body of the
32 message.
33  
34 use Email::Fingerprint;
35  
36 my $foo = Email::Fingerprint->new();
37 ...
38  
39 =head1 ATTRIBUTES
40  
41 =cut
42  
43 7 my %header : ATTR( :get<header> ); # Header and body are
44 7 my %body : ATTR( :get<body> ); # read-only fields
45 7 my %input : ATTR( :init_arg<input> :get<input> :default(0) );
46 7 my %strict_checking : ATTR( :name<strict_checking> :default(0) );
47 7 my %checksum : ATTR( :get<checksum> :default('unpack') );
48  
49 =head1 FUNCTIONS
50  
51 =head2 new
52  
53 $fp = new Email::Fingerprint({
54 input => \*INPUT, # Or $string, \@lines, etc.
55 checksum => "Digest::SHA", # Or "Digest::MD5", etc.
56 strict_checking => 1, # If true, use message bodies
57 %mail_header_opts,
58 });
59  
60 Create a new fingerprinting object. If the C<input> option is used,
61 C<Email::Fingerprint> attempts to intelligently read the email message
62 given by that option, whether it's a string, an array of lines or a
63 filehandle.
64  
65 If C<$opts{checksum}> is not supplied, then C<Email::Fingerprint> will use
66 the first checksum module that it finds. If it finds no modules, it will
67 use C<unpack> in a ghastly manner you don't want to think about.
68  
69 Any C<%opts> are also passed along to C<Mail::Header->new>; see the
70 perldoc for C<Mail::Header> options.
71  
72 =cut
73  
74 sub BUILD {
75 80 ▶ my ( $self, $ident, $args ) = @_;
Subroutine BUILD: called CC=3 SCAR=11.0
Pod documented
76  
77 80 ▶ $self->set_checksum( $args->{checksum} || 'unpack' );
Condition100%2/2
$args->{'checksum'} || 'unpack'
l!l
791
MC/DC100%1/1
$args->{'checksum'} || 'unpack'
$args->{'checksum'}
Truth table100%2/2
A
A= $args->{'checksum'}
Aresult
11
00
78  
79 # Try to be "smart" and input the message by hook or by crook.
80 # Here we do something slightly nasty, and let Mail::Header see our
81 # args.
82 80 ▶ $self->read( $args->{input}, $args ) if exists $args->{input};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"input"} 19 61 80
83 }
84  
85 =head2 checksum
86  
87 # Uses original/default settings to take checksum
88 $checksum = $fp->checksum;
89  
90 # Can use any options accepted by constructor
91 $options = {
92 input => \*INPUT, # Or $string, \@lines, etc.
93 checksum => "Digest::SHA", # Or "Digest::MD5", etc.
94 strict_checking => 1, # If true, use message bodies
95 %mail_header_opts,
96 };
97  
98 # Overrides one or more original/default settings
99 $checksum = $fp->checksum($options);
100  
101 Calculates the actual email fingerprint. The optional hashref
102 argument will permanently override the object's previous settings.
103  
104 =cut
105  
106 sub checksum {
107 142 ▶ my $self = shift;
Subroutine checksum: called CC=9 SCAR=22.0
Pod documented
108 142 ▶ my %opts = %{ shift || {} };
Branch: 1 branch
BranchTrueFalseTotal
shift() or {} 100 42 142
109  
110 # Optionally override strict checking
111 $self->set_strict_checking($opts{strict_checking})
112 142 ▶ if exists $opts{strict_checking};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"strict_checking"} 42 100 142
113  
114 # Optionally override the checksum to use
115 142 ▶ $self->set_checksum($opts{checksum}) if exists $opts{checksum};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"checksum"} 42 100 142
116  
117 # Optionally read a new email message
118 142 ▶ $self->read( $opts{input}, \%opts ) if exists $opts{input};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"input"} 42 100 142
119  
120 # It's an error to call checksum without first loading a message.
121 142 ▶ croak "No mesage loaded for checksum" unless $self->message_loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->message_loaded 1 141 142
122  
123 141 my $module = $self->get_checksum;
124 141 my $header = $self->_extract_headers;
125 141 ▶ my $body = $self->get_strict_checking ? $self->_extract_body : "";
Branch: 1 branch
BranchTrueFalseTotal
$self->get_strict_checking ? : 126 15 141
126  
127 # Only here for backward compatibility!
128 141 ▶ if ( not $module or $module eq 'unpack' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $module or $module eq "unpack") 123 18 141
Condition66%2/3
not $module or $module eq "unpack"
l!l&&r!l&&!r
012318
MC/DC50%1/2
not $module or $module eq "unpack"
not $module $module eq "unpack"
Truth table66%2/3
not(A) or B
A= not $moduleB= $module eq "unpack"
ABresult
1X1
011
000
129 123 return unpack("%32C*", $header . $body);
130 }
131  
132 18 my $digest = $module->new;
133  
134 18 $digest->add( $header . $body );
135  
136 18 return $digest->hexdigest;
137 }
138  
139 =head2 read
140  
141 $fingerprint->read_string( $email );
142 $fingerprint->read_string( $email, \%mh_args );
143  
144 Accepts the email message C<$email> and attempts to read it
145 intelligently, distinguishing strings, array references and file
146 handles. If supplied, the optional hash reference is passed on to
147 Mail::Header.
148  
149 =cut
150  
151 sub read {
152 69 ▶ my ( $self, $input, $mh_args ) = @_;
Subroutine read: called CC=7 SCAR=19.5
Pod documented
153  
154 69 ▶ if ( not ref $input ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $input) { } 26 43 69
elsif (ref $input eq 'ARRAY') { } 26 17 43
elsif (builtin::reftype($input) eq 'GLOB') { } 13 4 17
155  
156 # Simple case: scalars are treated as strings.
157 26 return $self->read_string( $input );
158 }
159 elsif ( ref $input eq 'ARRAY' ) {
160  
161 # Another simple case: array references
162 26 return $self->read_arrayref( $input, $mh_args );
163 }
164 elsif ( reftype $input eq 'GLOB' ) {
165  
166 # Also simple: filehandle. Using Scalar::Util::reftype()
167 # instead of ref() quietly does the right thing, e.g., for
168 # FileHandle objects, which are blessed GLOB references.
169 13 return $self->read_filehandle( $input, $mh_args );
170 }
171  
172 # If execution gets this far, $input had better be an object.
173 # None of Perl's other types are supported.
174 4 ▶ if ( not blessed $input ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($input)) 2 2 4
175 2 croak "Unknown input type: ", ref $input;
176 }
177  
178 2 ▶ if ( overload::Method( $input, '""' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (overload::Method($input, '""')) { } 1 1 2
elsif (overload::Method($input, '<>')) { } 0 1 1
179  
180 # Treat it as a string
181 1 return $self->read_string( $input, $mh_args );
182 }
183 elsif ( overload::Method( $input, '<>' ) ) {
184  
185 # Treat it as a filehandle
186 0 return $self->read_filehandle( $input, $mh_args );
187 }
188  
189 # OK, I give up.
190 1 croak "Unknown input type: ", ref $input;
191 }
192  
193 =head2 read_string
194  
195 $fingerprint->read_string( $email_string );
196 $fingerprint->read_string( $email_string, \%mh_args );
197  
198 Accepts the email message C<$email_string> and prepares it for
199 checksum computation. If supplied, the optional hashref is passed
200 on to Mail::Header.
201  
202 =cut
203  
204 sub read_string {
205 45 ▶ my ( $self, $message, $mh_args ) = @_;
Subroutine read_string: called CC=1 SCAR=0.0
Pod documented
206  
207 # Split the stringified message into an array of lines. We can't use
208 # split(/\n/,$input); that would discard trailing blank lines.
209 45 $message = [ "$message" =~ m{ ( ^ [^\n]* \n? ) }xmg ];
210  
211 # Now delegate
212 45 return $self->read_arrayref( $message, $mh_args );
213 }
214  
215 =head2 read_filehandle
216  
217 $fingerprint->read_filehandle( $email_fh );
218 $fingerprint->read_filehandle( $email_fh, \%mh_args );
219  
220 Accepts the email message C<$email_fh> and prepares it for checksum
221 computation. If supplied, the optional hashref is passed on to
222 Mail::Header.
223  
224 =cut
225  
226 sub read_filehandle {
227 31 ▶ my ( $self, $message, $mh_args ) = @_;
Subroutine read_filehandle: called CC=1 SCAR=0.0
Pod documented
228  
229 # Slurp everything into an arrayref
230 31 $message = [ <$message> ];
231  
232 # Now delegate
233 31 return $self->read_arrayref( $message, $mh_args );
234 }
235  
236 =head2 read_arrayref
237  
238 $fingerprint->read_arrayref( \@email_lines );
239 $fingerprint->read_arrayref( \@email_lines, \%mh_args );
240  
241 Accepts the email message C<\@email_lines> and prepares it for
242 checksum computation. If supplied, the optional hashref is passed
243 on to Mail::Header.
244  
245 =cut
246  
247 sub read_arrayref {
248 120 ▶ my ( $self, $message, $mh_args ) = @_;
Subroutine read_arrayref: called CC=2 SCAR=6.9
Pod documented
249 120 ▶ $mh_args ||= {};
Condition100%2/2
$mh_args ||= {}
l!l
3684
MC/DC100%1/1
$mh_args ||= {}
$mh_args
Truth table100%2/2
A
A= $mh_args
Aresult
11
00
250  
251 # Prepare args to pass on to Mail::Header constructor. The ordering
252 # below causes $mh_args to override the default settings in this
253 # hashref.
254 120 $mh_args = {
255 Modify => 0, # Don't mess with the header.
256 MailFrom => 'IGNORE', # Accept message in mbox format.
257 %$mh_args,
258 };
259  
260 # Initializations. This is the ONLY method that sets the
261 # "input" and "header" fields.
262 120 $header{ ident $self } = Mail::Header->new( $message, %$mh_args );
263 120 $input{ ident $self } = $message;
264 120 delete $body{ ident $self };
265 }
266  
267 =head2 message_loaded
268  
269 Returns true if an email message has been loaded and is ready for checksum,
270 or false if no message has been loaded or an error has occurred.
271  
272 =cut
273  
274 sub message_loaded {
275 142 ▶ my $self = shift;
Subroutine message_loaded: called CC=2 SCAR=6.9
Pod documented
276  
277 142 ▶ return defined $self->get_header ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->get_header ? : 141 1 142
278 }
279  
280 =head2 set_checksum
281  
282 Specifies the checksum method to be used.
283  
284 =cut
285  
286 sub set_checksum {
287 122 ▶ my ( $self, $checksum ) = @_;
Subroutine set_checksum: called CC=4 SCAR=13.9
Pod documented
288 122 $checksum{ ident $self } = $checksum;
289  
290 122 ▶ return if not $checksum or $checksum eq 'unpack';
Branch: 1 branch
BranchTrueFalseTotal
if not $checksum or $checksum eq "unpack" 104 18 122
Condition66%2/3
not $checksum or $checksum eq "unpack"
l!l&&r!l&&!r
010418
MC/DC50%1/2
not $checksum or $checksum eq "unpack"
not $checksum $checksum eq "unpack"
Truth table66%2/3
not(A) or B
A= not $checksumB= $checksum eq "unpack"
ABresult
1X1
011
000
291  
292 18 ▶ eval "use $checksum"; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
293 18 ▶ croak "Invalid checksum: $checksum\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 18 18
294 }
295  
296 =head1 INTERNAL METHODS
297  
298 =head2 BUILD
299  
300 A constructor helper method called from the C<Class::Std> framework. To
301 execute C<BUILD>, use C<new()>.
302  
303 =head2 _extract_headers
304  
305 Extract the Message-ID: header. If that does not exist, extract
306 the Date:, From:, To: and Cc: headers. If those do not exist, then
307 force strict checking so that the message body will be
308 fingerprinted.
309  
310 =cut
311  
312 sub _extract_headers :RESTRICTED {
313 141 ▶ my $self = shift;
Subroutine _extract_headers: called CC=5 SCAR=16.1
314  
315 141 my $raw = $self->get_header->header_hashref;
316  
317 141 my %headers;
318  
319 141 my $extracted_headers = "";
320  
321 141 map { my $key = lc( $_ ); $headers{$key} = $raw->{$_} } keys %$raw;
322  
323 141 ▶ if (defined $headers{'message-id'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers{'message-id'}) { } 93 48 141
324 93 $extracted_headers .= $self->_concat( $headers{'message-id'} );
325 }
326 else {
327 48 foreach my $h ('date', 'from', 'to', 'cc') {
328 192 ▶ next unless exists $headers{$h};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $headers{$h} 154 38 192
329  
330 38 $extracted_headers .= $self->_concat( $headers{$h}, "$h:" );
331 }
332 }
333  
334 141 ▶ $self->set_strict_checking(1) unless $extracted_headers;
Branch: 1 branch
BranchTrueFalseTotal
unless $extracted_headers 29 112 141
335 141 return $extracted_headers;
336 7 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
337  
338 =head2 _extract_body
339  
340 $body = $fp->_extract_body;
341  
342 Gets the body of the message, as a string. Line-endings are preserved, so
343 the body can, e.g., be printed.
344  
345 This method must only be called after a message has been read. No
346 validation is done in the method itself, so this is the user's
347 responsibility.
348  
349 =cut
350  
351 sub _extract_body :RESTRICTED {
352  
353 126 ▶ my $self = shift;
Subroutine _extract_body: called CC=4 SCAR=13.9
354  
355 # Use the cached body, if any
356 126 my $body = $self->get_body;
357 126 ▶ return $body if defined $body;
Branch: 1 branch
BranchTrueFalseTotal
if defined $body 21 105 126
358  
359 105 my $input = $self->get_input;
360  
361 # Copy the message. We don't want to munge the original!
362 105 my @message = @$input;
363  
364 105 my $line;
365  
366 # Discard the RFC822 header. Perhaps not as bullet-proof
367 # as it could be...
368 105 ▶ do { $line = shift @message } while ( $line and $line !~ m{ ^$ }xmsg );
Branch: 1 branch
BranchTrueFalseTotal
$line and not $line =~ / ^$ /gmsx and do { do { $line = shift @message } } 37400 105 37505
Condition100%3/3
$line and not $line =~ / ^$ /gmsx
!ll&&!rl&&r
79837400
MC/DC100%2/2
$line and not $line =~ / ^$ /gmsx
$line not $line =~ / ^$ /gmsx
Truth table100%3/3
A and not(B)
A= $lineB= not $line =~ / ^$ /gmsx
ABresult
0X0
100
111
369  
370 105 $body .= join "", @message;
371  
372 # Cache the body for reuse. This is the ONLY method that sets the
373 # "body" field.
374 105 $body{ident $self} = $body;
375  
376 105 return $body;
377 7 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
378  
379 =head2 _concat
380  
381 @headers = qw( foo@example.com bar@example.com );
382 $delim = 'To:';
383 $string = $fp->_concat( \@headers, $delim );
384  
385 # $string is now 'To:foo@example.comTo:bar@example.com'
386  
387 Returns the concatenation of C<\@headers>, with C<$delim> prepended
388 to each element of C<\@headers>. If C<$delim> is omitted, the empty
389 string is used. C<\@headers> elements are all chomped before
390 concatenation.
391  
392 =cut
393  
394 sub _concat :PRIVATE {
395 131 ▶ my $self = shift;
Subroutine _concat: called CC=2 SCAR=6.9
396 131 my $data = shift;
397 131 ▶ my $delim = shift || "";
Condition100%2/2
shift() || ''
l!l
3893
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
398  
399 131 ▶ return $delim . join($delim, apply {chomp } @$data);
Subroutine __ANON__: called CC=1 SCAR=0.0
400 7 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
401  
402 =head1 AUTHOR
403  
404 Len Budney, C<< <lbudney at pobox.com> >>
405  
406 =head1 BUGS
407  
408 Please report any bugs or feature requests to
409 C<bug-email-fingerprint at rt.cpan.org>, or through the web interface at
410 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Email-Fingerprint>.
411 I will be notified, and then you'll automatically be notified of progress on
412 your bug as I make changes.
413  
414 =head1 SUPPORT
415  
416 You can find documentation for this module with the perldoc command.
417  
418 perldoc Email::Fingerprint
419  
420 You can also look for information at:
421  
422 =over 4
423  
424 =item * AnnoCPAN: Annotated CPAN documentation
425  
426 L<http://annocpan.org/dist/Email-Fingerprint>
427  
428 =item * CPAN Ratings
429  
430 L<http://cpanratings.perl.org/d/Email-Fingerprint>
431  
432 =item * RT: CPAN's request tracker
433  
434 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Email-Fingerprint>
435  
436 =item * Search CPAN
437  
438 L<http://search.cpan.org/dist/Email-Fingerprint>
439  
440 =back
441  
442 =head1 SEE ALSO
443  
444 See B<Mail::Header> for options governing the parsing of email headers.
445  
446 =head1 ACKNOWLEDGEMENTS
447  
448 Email::Fingerprint is based on the C<eliminate_dups> script by Peter Samuel
449 and available at L<http://www.qmail.org/>.
450  
451 =head1 COPYRIGHT & LICENSE
452  
453 Copyright 2006-2011 Len Budney, all rights reserved.
454  
455 This program is free software; you can redistribute it and/or modify it
456 under the same terms as Perl itself.
457  
458 =cut
459  
460 7 1; # End of Email::Fingerprint
« Fingerprint/App/EliminateDups.pm ↑ Summary Fingerprint/Cache/AnyDBM.pm »