| 1 | package Mail::Pyzor::Digest; | ||||||||||
| 2 | |||||||||||
| 3 | # Copyright 2018 cPanel, LLC. | ||||||||||
| 4 | # All rights reserved. | ||||||||||
| 5 | # http://cpanel.net | ||||||||||
| 6 | # | ||||||||||
| 7 | # <@LICENSE> | ||||||||||
| 8 | # Licensed to the Apache Software Foundation (ASF) under one or more | ||||||||||
| 9 | # contributor license agreements. See the NOTICE file distributed with | ||||||||||
| 10 | # this work for additional information regarding copyright ownership. | ||||||||||
| 11 | # The ASF licenses this file to you under the Apache License, Version 2.0 | ||||||||||
| 12 | # (the "License"); you may not use this file except in compliance with | ||||||||||
| 13 | # the License. You may obtain a copy of the License at: | ||||||||||
| 14 | # | ||||||||||
| 15 | # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||
| 16 | # | ||||||||||
| 17 | # Unless required by applicable law or agreed to in writing, software | ||||||||||
| 18 | # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||
| 19 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||
| 20 | # See the License for the specific language governing permissions and | ||||||||||
| 21 | # limitations under the License. | ||||||||||
| 22 | # </@LICENSE> | ||||||||||
| 23 | # | ||||||||||
| 24 | |||||||||||
| 25 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | =encoding utf-8 | ||||||||||
| 29 | |||||||||||
| 30 | =head1 NAME | ||||||||||
| 31 | |||||||||||
| 32 | Mail::Pyzor::Digest | ||||||||||
| 33 | |||||||||||
| 34 | =head1 SYNOPSIS | ||||||||||
| 35 | |||||||||||
| 36 | my $digest = Mail::Pyzor::Digest::get( $mime_text ); | ||||||||||
| 37 | |||||||||||
| 38 | =head1 DESCRIPTION | ||||||||||
| 39 | |||||||||||
| 40 | A reimplementation of L<https://github.com/SpamExperts/pyzor/blob/master/pyzor/digest.py>. | ||||||||||
| 41 | |||||||||||
| 42 | =cut | ||||||||||
| 43 | |||||||||||
| 44 | #---------------------------------------------------------------------- | ||||||||||
| 45 | |||||||||||
| 46 | 1 | ▶ | use Email::MIME (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | |||||||||||
| 48 | 1 | ▶ | use Mail::Pyzor::Digest::Pieces (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 1 | ▶ | use Mail::Pyzor::SHA (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | |||||||||||
| 51 | 1 | our $VERSION = '0.06'; | |||||||||
| 52 | |||||||||||
| 53 | #---------------------------------------------------------------------- | ||||||||||
| 54 | |||||||||||
| 55 | =head1 FUNCTIONS | ||||||||||
| 56 | |||||||||||
| 57 | =head2 $hex = get( $MSG ) | ||||||||||
| 58 | |||||||||||
| 59 | This takes an email message in raw MIME text format (i.e., as saved in the | ||||||||||
| 60 | standard mbox format) and returns the message’s Pyzor digest in lower-case | ||||||||||
| 61 | hexadecimal. | ||||||||||
| 62 | |||||||||||
| 63 | The output from this function should normally be identical to that of | ||||||||||
| 64 | the C<pyzor> script’s C<digest> command. It is suitable for use in | ||||||||||
| 65 | L<Mail::Pyzor::Client>’s request methods. | ||||||||||
| 66 | |||||||||||
| 67 | =cut | ||||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | sub get { | ||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 70 | 0 | return Mail::Pyzor::SHA::sha1_hex( ${ _get_predigest( $_[0] ) } ); | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | # NB: This is called from the test. | ||||||||||
| 74 | 0 | ▶ | sub _get_predigest { ## no critic qw(RequireArgUnpacking) | ||||||||
|
Subroutine
_get_predigest: not called CC=5 SCAR=34.0
| |||||||||||
| 75 | 0 | my ($msg_text_sr) = \$_[0]; | |||||||||
| 76 | |||||||||||
| 77 | 0 | my $parsed = Email::MIME->new($$msg_text_sr); | |||||||||
| 78 | |||||||||||
| 79 | 0 | my @lines; | |||||||||
| 80 | |||||||||||
| 81 | 0 | my $payloads_ar = Mail::Pyzor::Digest::Pieces::digest_payloads($parsed); | |||||||||
| 82 | |||||||||||
| 83 | 0 | for my $payload (@$payloads_ar) { | |||||||||
| 84 | 0 | my @p_lines = Mail::Pyzor::Digest::Pieces::splitlines($payload); | |||||||||
| 85 | 0 | for my $line (@p_lines) { | |||||||||
| 86 | 0 | Mail::Pyzor::Digest::Pieces::normalize($line); | |||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | next if !Mail::Pyzor::Digest::Pieces::should_handle_line($line); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | |||||||||||
| 90 | # Make sure we have an octet string. | ||||||||||
| 91 | 0 | ▶ | utf8::encode($line) if utf8::is_utf8($line); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | |||||||||||
| 93 | 0 | push @lines, $line; | |||||||||
| 94 | } | ||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 0 | my $digest_sr = Mail::Pyzor::Digest::Pieces::assemble_lines( \@lines ); | |||||||||
| 98 | |||||||||||
| 99 | 0 | return $digest_sr; | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 1 | 1; | |||||||||