| 1 | package Mail::Pyzor::Digest; | ||||||||||
| 2 | |||||||||||
| 3 | # Copyright 2018 cPanel, LLC. | ||||||||||
| 4 | # All rights reserved. | ||||||||||
| 5 | # http://cpanel.net | ||||||||||
| 6 | # | ||||||||||
| 7 | # This is free software; you can redistribute it and/or modify it under the | ||||||||||
| 8 | # same terms as Perl itself. See L<perlartistic>. | ||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | =encoding utf-8 | ||||||||||
| 14 | |||||||||||
| 15 | =head1 NAME | ||||||||||
| 16 | |||||||||||
| 17 | Mail::Pyzor::Digest | ||||||||||
| 18 | |||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||
| 20 | |||||||||||
| 21 | my $digest = Mail::Pyzor::Digest::get( $mime_text ); | ||||||||||
| 22 | |||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||
| 24 | |||||||||||
| 25 | A reimplementation of L<https://github.com/SpamExperts/pyzor/blob/master/pyzor/digest.py>. | ||||||||||
| 26 | |||||||||||
| 27 | =cut | ||||||||||
| 28 | |||||||||||
| 29 | #---------------------------------------------------------------------- | ||||||||||
| 30 | |||||||||||
| 31 | 1 | ▶ | use Email::MIME (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | 1 | ▶ | use Mail::Pyzor::Digest::Pieces (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 1 | ▶ | use Mail::Pyzor::SHA (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | |||||||||||
| 36 | #---------------------------------------------------------------------- | ||||||||||
| 37 | |||||||||||
| 38 | =head1 FUNCTIONS | ||||||||||
| 39 | |||||||||||
| 40 | =head2 $hex = get( $MSG ) | ||||||||||
| 41 | |||||||||||
| 42 | This takes an email message in raw MIME text format (i.e., as saved in the | ||||||||||
| 43 | standard mbox format) and returns the message’s Pyzor digest in lower-case | ||||||||||
| 44 | hexadecimal. | ||||||||||
| 45 | |||||||||||
| 46 | The output from this function should normally be identical to that of | ||||||||||
| 47 | the C<pyzor> script’s C<digest> command. It is suitable for use in | ||||||||||
| 48 | L<Mail::Pyzor::Client>’s request methods. | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | sub get { | ||||||||||
| 53 | 0 | ▶ | return Mail::Pyzor::SHA::sha1_hex( ${ _get_predigest( $_[0] ) } ); | ||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | # NB: This is called from the test. | ||||||||||
| 57 | sub _get_predigest { ## no critic qw(RequireArgUnpacking) | ||||||||||
| 58 | 0 | ▶ | my ($msg_text_sr) = \$_[0]; | ||||||||
|
Subroutine
_get_predigest: not called CC=5 SCAR=34.0
| |||||||||||
| 59 | |||||||||||
| 60 | 0 | my $parsed = Email::MIME->new($$msg_text_sr); | |||||||||
| 61 | |||||||||||
| 62 | 0 | my @lines; | |||||||||
| 63 | |||||||||||
| 64 | 0 | my $payloads_ar = Mail::Pyzor::Digest::Pieces::digest_payloads($parsed); | |||||||||
| 65 | |||||||||||
| 66 | 0 | for my $payload (@$payloads_ar) { | |||||||||
| 67 | 0 | my @p_lines = Mail::Pyzor::Digest::Pieces::splitlines($payload); | |||||||||
| 68 | 0 | for my $line (@p_lines) { | |||||||||
| 69 | 0 | Mail::Pyzor::Digest::Pieces::normalize($line); | |||||||||
| 70 | |||||||||||
| 71 | 0 | ▶ | next if !Mail::Pyzor::Digest::Pieces::should_handle_line($line); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | |||||||||||
| 73 | # Make sure we have an octet string. | ||||||||||
| 74 | 0 | ▶ | utf8::encode($line) if utf8::is_utf8($line); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | |||||||||||
| 76 | 0 | push @lines, $line; | |||||||||
| 77 | } | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 0 | my $digest_sr = Mail::Pyzor::Digest::Pieces::assemble_lines( \@lines ); | |||||||||
| 81 | |||||||||||
| 82 | 0 | return $digest_sr; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 1 | 1; | |||||||||