| 1 | package Courriel::Helpers; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 10 | our $VERSION = '0.49'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Encode qw( decode ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use List::AllUtils qw( first ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 10 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 13 | fold_header | ||||||||||||||||||||||||||||
| 14 | parse_header_with_attributes | ||||||||||||||||||||||||||||
| 15 | quote_and_escape_attribute_value | ||||||||||||||||||||||||||||
| 16 | unique_boundary | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 10 | our $CRLF = "\x0d\x0a"; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # from Email::Simple | ||||||||||||||||||||||||||||
| 22 | 10 | our $LINE_SEP_RE = qr/(?:\x0a\x0d|\x0d\x0a|\x0a|\x0d)/; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub fold_header { | ||||||||||||||||||||||||||
|
Subroutine
fold_header: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 0 | my $line = shift; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | my $folded = q{}; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # Algorithm stolen from Email::Simple::Header | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | while ( length $line ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ( $line =~ s/^(.{0,76})(\s|\z)// ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | $folded .= $1 . $CRLF; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | $folded .= q{ } if length $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | else { | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Basically nothing we can do. :( | ||||||||||||||||||||||||||||
| 38 | 0 | $folded .= $line . $CRLF; | |||||||||||||||||||||||||||
| 39 | 0 | last; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | return $folded; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub quote_and_escape_attribute_value { | ||||||||||||||||||||||||||
|
Subroutine
quote_and_escape_attribute_value: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 0 | my $val = shift; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | return $val unless $val =~ /[^a-zA-Z0-9\-]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | $val =~ s/(\\|")/\\$1/g; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | return qq{"$val"}; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 209 | ▶ | sub parse_header_with_attributes { | ||||||||||||||||||||||||||
|
Subroutine
parse_header_with_attributes: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 209 | my $text = shift; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 209 | ▶ | return unless defined $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 209 | my ($val) = $text =~ /([^\s;]+)(?:\s*;\s*(.*))?\z/s; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | ## no critic (RegularExpressions::ProhibitCaptureWithoutTest) | ||||||||||||||||||||||||||||
| 64 | return ( | ||||||||||||||||||||||||||||
| 65 | 209 | $val, | |||||||||||||||||||||||||||
| 66 | _parse_attributes($2), | ||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 10 | our $TSPECIALS = qr{\Q()<>@,;:\"/[]?=}; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 10 | my $extract_quoted = qr/ | |||||||||||||||||||||||||||
| 73 | (?: | ||||||||||||||||||||||||||||
| 74 | \" | ||||||||||||||||||||||||||||
| 75 | (?<quoted_value> | ||||||||||||||||||||||||||||
| 76 | [^\\\"]* | ||||||||||||||||||||||||||||
| 77 | (?: | ||||||||||||||||||||||||||||
| 78 | \\.[^\\\"]* | ||||||||||||||||||||||||||||
| 79 | )* | ||||||||||||||||||||||||||||
| 80 | ) | ||||||||||||||||||||||||||||
| 81 | \" | ||||||||||||||||||||||||||||
| 82 | | | ||||||||||||||||||||||||||||
| 83 | \' | ||||||||||||||||||||||||||||
| 84 | (?<quoted_value> | ||||||||||||||||||||||||||||
| 85 | [^\\\']* | ||||||||||||||||||||||||||||
| 86 | (?: | ||||||||||||||||||||||||||||
| 87 | \\.[^\\\']* | ||||||||||||||||||||||||||||
| 88 | )* | ||||||||||||||||||||||||||||
| 89 | ) | ||||||||||||||||||||||||||||
| 90 | \' | ||||||||||||||||||||||||||||
| 91 | ) | ||||||||||||||||||||||||||||
| 92 | /x; | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # This is a very loose regex. RFC 2231 has a much tighter definition of what | ||||||||||||||||||||||||||||
| 95 | # can go in an attribute name, but this parser is designed to accept all the | ||||||||||||||||||||||||||||
| 96 | # crap the internet throws at it. | ||||||||||||||||||||||||||||
| 97 | 10 | my $attr_re = qr/ | |||||||||||||||||||||||||||
| 98 | (?<name>[^\s=\*]+) # names cannot include spaces, "=", or "*" | ||||||||||||||||||||||||||||
| 99 | (?: | ||||||||||||||||||||||||||||
| 100 | \*(?<order>[\d+]) | ||||||||||||||||||||||||||||
| 101 | )? | ||||||||||||||||||||||||||||
| 102 | (?<is_encoded>\*)? | ||||||||||||||||||||||||||||
| 103 | = | ||||||||||||||||||||||||||||
| 104 | (?: | ||||||||||||||||||||||||||||
| 105 | $extract_quoted | ||||||||||||||||||||||||||||
| 106 | | | ||||||||||||||||||||||||||||
| 107 | (?<value>[^\s;]+) # unquoted values cannot contain spaces | ||||||||||||||||||||||||||||
| 108 | ) | ||||||||||||||||||||||||||||
| 109 | (\s*;\s*)? | ||||||||||||||||||||||||||||
| 110 | /xs; | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 209 | ▶ | sub _parse_attributes { | ||||||||||||||||||||||||||
|
Subroutine
_parse_attributes: called CC=9 SCAR=24.6
| |||||||||||||||||||||||||||||
| 113 | 209 | my $attr_text = shift; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 209 | ▶ | return {} unless defined $attr_text && length $attr_text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $attr_text and length $attr_text
MC/DC50%1/2
defined $attr_text and length $attr_text
defined $attr_text
length $attr_text
Truth table66%2/3
A and B
A= defined $attr_textB= length $attr_text
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 127 | my $attrs = {}; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 127 | ▶ | while ( $attr_text =~ /\G$attr_re/g ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 151 | my $name = $+{name}; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 151 | my $charset; | |||||||||||||||||||||||||||
| 123 | my $language; | ||||||||||||||||||||||||||||
| 124 | 151 | my $value; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 151 | ▶ | my $order = $+{order} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$+{'order'} || 0
MC/DC0%0/1
$+{'order'} || 0
$+{'order'}
Truth table50%1/2
A
A= $+{'order'}
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 151 | ▶ | if ( $+{is_encoded} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ($order) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | $value = $+{value}; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | else { | ||||||||||||||||||||||||||||
| 133 | 0 | ( $charset, $language, my $raw ) = split /\'/, $+{value}, 3; | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | $language = undef unless length $language; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | $value = $raw; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | elsif ( defined $+{quoted_value} ) { | ||||||||||||||||||||||||||||
| 139 | 81 | ( $value = $+{quoted_value} ) =~ s/\G(.*?)\\(.)/$1$2/g; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||||||||||
| 142 | 70 | $value = $+{value}; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 151 | $attrs->{$name}[$order] = { | |||||||||||||||||||||||||||
| 146 | value => $value, | ||||||||||||||||||||||||||||
| 147 | charset => $charset, | ||||||||||||||||||||||||||||
| 148 | language => $language, | ||||||||||||||||||||||||||||
| 149 | }; | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | return { | ||||||||||||||||||||||||||||
| 153 | 151 | map { $_ => _inflate_attribute( $_, $attrs->{$_} ) } | |||||||||||||||||||||||||||
| 154 | 127 | keys %{$attrs} | |||||||||||||||||||||||||||
| 155 | }; | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 151 | ▶ | sub _inflate_attribute { | ||||||||||||||||||||||||||
|
Subroutine
_inflate_attribute: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 159 | 151 | my $name = shift; | |||||||||||||||||||||||||||
| 160 | 151 | my $raw_data = shift; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 151 | my $value = join q{}, grep {defined} map { $_->{value} } @{$raw_data}; | |||||||||||||||||||||||||||
| 163 | 151 | my %p = ( | |||||||||||||||||||||||||||
| 164 | name => $_, | ||||||||||||||||||||||||||||
| 165 | value => $value, | ||||||||||||||||||||||||||||
| 166 | ); | ||||||||||||||||||||||||||||
| 167 | 151 | for my $key (qw( charset language )) { | |||||||||||||||||||||||||||
| 168 | $p{$key} = $raw_data->[0]{$key} | ||||||||||||||||||||||||||||
| 169 | 302 | ▶ | if defined $raw_data->[0]{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 151 | $p{value} = _decode_raw_value( $p{value}, $p{charset} ); | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 151 | return Courriel::HeaderAttribute->new(%p); | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 151 | ▶ | sub _decode_raw_value { | ||||||||||||||||||||||||||
|
Subroutine
_decode_raw_value: called CC=2 SCAR=7.9
| |||||||||||||||||||||||||||||
| 177 | 151 | my $raw = shift; | |||||||||||||||||||||||||||
| 178 | 151 | my $charset = shift; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | $raw =~ s/%([\da-fA-F]{2})/chr(hex($1))/eg; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 151 | ▶ | return $raw unless defined $charset; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | return decode( $charset, $raw ); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 2 | ▶ | sub unique_boundary { | ||||||||||||||||||||||||||
|
Subroutine
unique_boundary: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 188 | 2 | return Email::MessageID->new->user; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # Courriel::HeaderAttribute requires that $TSPECIALS be defined | ||||||||||||||||||||||||||||
| 192 | 10 | require Courriel::HeaderAttribute; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 10 | 1; | |||||||||||||||||||||||||||