/usr/share/perl5/Courriel/Helpers.pm is in libcourriel-perl 0.31-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | package Courriel::Helpers;
{
$Courriel::Helpers::VERSION = '0.31';
}
use strict;
use warnings;
use Encode qw( decode );
use Exporter qw( import );
use List::AllUtils qw( first );
our @EXPORT_OK = qw(
fold_header
parse_header_with_attributes
quote_and_escape_attribute_value
unique_boundary
);
our $CRLF = "\x0d\x0a";
# from Email::Simple
our $LINE_SEP_RE = qr/(?:\x0a\x0d|\x0d\x0a|\x0a|\x0d)/;
sub fold_header {
my $line = shift;
my $folded = q{};
# Algorithm stolen from Email::Simple::Header
while ($line) {
if ( $line =~ s/^(.{0,76})(\s|\z)// ) {
$folded .= $1 . $CRLF;
$folded .= q{ } if $line;
}
else {
# Basically nothing we can do. :(
$folded .= $line . $CRLF;
last;
}
}
return $folded;
}
sub quote_and_escape_attribute_value {
my $val = shift;
return $val unless $val =~ /[^a-zA-Z0-9\-]/;
$val =~ s/(\\|")/\\$1/g;
return qq{"$val"};
}
sub parse_header_with_attributes {
my $text = shift;
return unless defined $text;
my ($val) = $text =~ /([^\s;]+)(?:\s*;\s*(.*))?\z/s;
return (
$val,
_parse_attributes($2),
);
}
our $TSPECIALS = qr{\Q()<>@,;:\"/[]?=};
my $extract_quoted = qr/
(?:
\"
(?<quoted_value>
[^\\\"]*
(?:
\\.[^\\\"]*
)*
)
\"
|
\'
(?<quoted_value>
[^\\\']*
(?:
\\.[^\\\']*
)*
)
\'
)
/x;
# This is a very loose regex. RFC 2231 has a much tighter definition of what
# can go in an attribute name, but this parser is designed to accept all the
# crap the internet throws at it.
my $attr_re = qr/
(?<name>[^\s=\*]+) # names cannot include spaces, "=", or "*"
(?:
\*(?<order>[\d+])
)?
(?<is_encoded>\*)?
=
(?:
$extract_quoted
|
(?<value>[^\s;]+) # unquoted values cannot contain spaces
)
(\s*;\s*)?
/xs;
sub _parse_attributes {
my $attr_text = shift;
return {} unless defined $attr_text && length $attr_text;
my $attrs = {};
while ( $attr_text =~ /\G$attr_re/g ) {
my $name = $+{name};
my $value;
my $charset;
my $language;
my $order = $+{order} || 0 ;
if ( $+{is_encoded} ) {
if ($order) {
$value = _decode_raw_value(
$+{value},
$attrs->{$name}[$order]{charset},
);
}
else {
( $charset, $language, my $raw ) = split /\'/, $+{value}, 3;
$language = undef unless length $language;
$value = _decode_raw_value( $raw, $charset );
}
}
elsif ( defined $+{quoted_value} ) {
( $value = $+{quoted_value} ) =~ s/\G(.*?)\\(.)/$1$2/g;
}
else {
$value = $+{value};
}
$attrs->{$name}[ $order] = {
value => $value,
charset => $charset,
language => $language,
};
}
return {
map { $_ => _inflate_attribute( $_, $attrs->{$_} ) }
keys %{$attrs}
};
}
sub _decode_raw_value {
my $raw = shift;
my $charset = shift;
$raw =~ s/%([\da-fA-F]{2})/chr(hex($1))/eg;
return $raw unless defined $charset;
return decode( $charset, $raw );
}
sub _inflate_attribute {
my $name = shift;
my $raw_data = shift;
my $value = join q{}, grep { defined } map { $_->{value} } @{$raw_data};
my %p = (
name => $_,
value => $value,
);
for my $key (qw( charset language )) {
$p{$key} = $raw_data->[0]{$key}
if defined $raw_data->[0]{$key};
}
return Courriel::HeaderAttribute->new(%p);
}
sub unique_boundary {
return Email::MessageID->new()->user();
}
# Courriel::HeaderAttribute requires that $TSPECIALS be defined
require Courriel::HeaderAttribute;
1;
|