| 1 | |||||||||||||
| 2 | package Matts::Message; | ||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 7 | 0 | bless { | |||||||||||
| 8 | encodings => [], | ||||||||||||
| 9 | bin_headers => {}, | ||||||||||||
| 10 | headers => {}, | ||||||||||||
| 11 | body_parts => [], | ||||||||||||
| 12 | attachments => [], | ||||||||||||
| 13 | raw => '', | ||||||||||||
| 14 | }, shift; | ||||||||||||
| 15 | } | ||||||||||||
| 16 | |||||||||||||
| 17 | 0 | ▶ | sub raw_headers { | ||||||||||
|
Subroutine
raw_headers: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 18 | 0 | my $self = shift; | |||||||||||
| 19 | 0 | return $self->{raw}; | |||||||||||
| 20 | } | ||||||||||||
| 21 | |||||||||||||
| 22 | 0 | ▶ | sub header { | ||||||||||
|
Subroutine
header: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||
| 23 | 0 | my $self = shift; | |||||||||||
| 24 | 0 | my $k = shift; | |||||||||||
| 25 | 0 | my $key = lc($k); | |||||||||||
| 26 | 0 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | 0 | $self->{raw} .= "$k: @_"; | |||||||||||
| 28 | 0 | ▶ | if (exists $self->{headers}{$key}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | 0 | push @{$self->{headers}{$key}}, @_; | |||||||||||
| 30 | } | ||||||||||||
| 31 | else { | ||||||||||||
| 32 | 0 | $self->{headers}{$key} = [@_]; | |||||||||||
| 33 | } | ||||||||||||
| 34 | 0 | return $self->{headers}{$key}[-1]; | |||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | 0 | ▶ | if (wantarray) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | 0 | ▶ | return unless exists $self->{headers}{$key}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | 0 | return @{$self->{headers}{$key}}; | |||||||||||
| 40 | } | ||||||||||||
| 41 | else { | ||||||||||||
| 42 | 0 | ▶ | return '' unless exists $self->{headers}{$key}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | 0 | return $self->{headers}{$key}[-1]; | |||||||||||
| 44 | } | ||||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | 0 | ▶ | sub binary_header { | ||||||||||
|
Subroutine
binary_header: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||
| 48 | 0 | my $self = shift; | |||||||||||
| 49 | 0 | my $key = lc(shift); | |||||||||||
| 50 | 0 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | 0 | ▶ | if (exists $self->{bin_headers}{$key}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | 0 | push @{$self->{bin_headers}{$key}}, @_; | |||||||||||
| 53 | } | ||||||||||||
| 54 | else { | ||||||||||||
| 55 | 0 | $self->{bin_headers}{$key} = [@_]; | |||||||||||
| 56 | } | ||||||||||||
| 57 | 0 | return $self->{bin_headers}{$key}[-1]; | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | 0 | ▶ | if (wantarray) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | 0 | ▶ | return unless exists $self->{bin_headers}{$key}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 0 | return @{$self->{bin_headers}{$key}}; | |||||||||||
| 63 | } | ||||||||||||
| 64 | else { | ||||||||||||
| 65 | 0 | ▶ | return '' unless exists $self->{bin_headers}{$key}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | 0 | return $self->{bin_headers}{$key}[-1]; | |||||||||||
| 67 | } | ||||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 0 | ▶ | sub header_del { | ||||||||||
|
Subroutine
header_del: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 71 | 0 | my $self = shift; | |||||||||||
| 72 | 0 | my $header = lc(shift); | |||||||||||
| 73 | 0 | $self->{raw} =~ s/^$header:.*?^(\S)/$1/ism; | |||||||||||
| 74 | 0 | delete $self->{headers}{$header}; | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | 0 | ▶ | sub headers { | ||||||||||
|
Subroutine
headers: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 78 | 0 | my $self = shift; | |||||||||||
| 79 | 0 | return keys %{$self->{headers}}; | |||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 0 | ▶ | sub binary_headers { | ||||||||||
|
Subroutine
binary_headers: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 83 | 0 | my $self = shift; | |||||||||||
| 84 | 0 | return keys %{$self->{bin_headers}}; | |||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | 0 | ▶ | sub add_body_part { | ||||||||||
|
Subroutine
add_body_part: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 88 | 0 | my $self = shift; | |||||||||||
| 89 | 0 | my ($type, $fh) = @_; | |||||||||||
| 90 | 0 | ▶ | $type ||= 'text/plain'; | ||||||||||
|
Condition0%0/2
$type ||= "text/plain"
MC/DC0%0/1
$type ||= "text/plain"
$type
Truth table0%0/2
A
A= $type
| |||||||||||||
| 91 | 0 | my $enc = 'null'; | |||||||||||
| 92 | 0 | ▶ | if ($type =~ s/;(.*$)//) { # strip everything after first semi-colon | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | 0 | my $cs = $1; | |||||||||||
| 94 | 0 | ▶ | if ($cs =~ /charset="?([\w-]+)/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 0 | $enc = $1; | |||||||||||
| 96 | } | ||||||||||||
| 97 | } | ||||||||||||
| 98 | 0 | $type =~ s/[^a-zA-Z\/]//g; # strip inappropriate chars | |||||||||||
| 99 | 0 | push @{ $self->{encodings} }, $enc; | |||||||||||
| 100 | 0 | push @{ $self->{body_parts} }, [ $type => $fh ]; | |||||||||||
| 101 | } | ||||||||||||
| 102 | |||||||||||||
| 103 | 0 | ▶ | sub add_attachment { | ||||||||||
|
Subroutine
add_attachment: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 104 | 0 | my $self = shift; | |||||||||||
| 105 | 0 | my ($type, $fh, $name) = @_; | |||||||||||
| 106 | 0 | push @{ $self->{attachments} }, { | |||||||||||
| 107 | filename => $name, | ||||||||||||
| 108 | type => $type, | ||||||||||||
| 109 | fh => $fh, | ||||||||||||
| 110 | }; | ||||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | 0 | ▶ | sub body { | ||||||||||
|
Subroutine
body: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||
| 114 | 0 | my $self = shift; | |||||||||||
| 115 | 0 | my $type = shift; | |||||||||||
| 116 | 0 | ▶ | return unless @{ $self->{body_parts} }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 117 | 0 | ▶ | if ($type) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | # warn("body has ", scalar(@{ $self->{body_parts} }), " [$type]\n"); | ||||||||||||
| 119 | 0 | foreach my $body ( @{ $self->{body_parts} } ) { | |||||||||||
| 120 | # warn("type: $body->[0]\n"); | ||||||||||||
| 121 | 0 | ▶ | if (lc($type) eq lc($body->[0])) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | 0 | ▶ | return wantarray ? @$body : $body->[1]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 123 | } | ||||||||||||
| 124 | } | ||||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | 0 | ▶ | return wantarray ? @{ $self->{body_parts}[0] } : $self->{body_parts}[0][1]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 128 | } | ||||||||||||
| 129 | |||||||||||||
| 130 | 0 | ▶ | sub bodies { | ||||||||||
|
Subroutine
bodies: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 131 | 0 | my $self = shift; | |||||||||||
| 132 | 0 | my @ret; | |||||||||||
| 133 | 0 | foreach my $body ( @{ $self->{body_parts} } ) { | |||||||||||
| 134 | 0 | push @ret, lc($body->[0]), $body->[1]; | |||||||||||
| 135 | } | ||||||||||||
| 136 | 0 | return @ret; | |||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | 0 | ▶ | sub body_enc { | ||||||||||
|
Subroutine
body_enc: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 140 | 0 | my $self = shift; | |||||||||||
| 141 | 0 | my ($id) = @_; | |||||||||||
| 142 | 0 | return $self->{encodings}[$id]; | |||||||||||
| 143 | } | ||||||||||||
| 144 | |||||||||||||
| 145 | 0 | ▶ | sub attachment { | ||||||||||
|
Subroutine
attachment: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 146 | 0 | my $self = shift; | |||||||||||
| 147 | 0 | return $self->{attachments}[shift]; | |||||||||||
| 148 | } | ||||||||||||
| 149 | |||||||||||||
| 150 | 0 | ▶ | sub attachments { | ||||||||||
|
Subroutine
attachments: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 151 | 0 | my $self = shift; | |||||||||||
| 152 | 0 | return @{ $self->{attachments} }; | |||||||||||
| 153 | } | ||||||||||||
| 154 | |||||||||||||
| 155 | 0 | ▶ | sub num_attachments { | ||||||||||
|
Subroutine
num_attachments: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 156 | 0 | my $self = shift; | |||||||||||
| 157 | 0 | return scalar @{ $self->{attachments} }; | |||||||||||
| 158 | } | ||||||||||||
| 159 | |||||||||||||
| 160 | 0 | ▶ | sub to_string { | ||||||||||
|
Subroutine
to_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 161 | 0 | my $self = shift; | |||||||||||
| 162 | |||||||||||||
| 163 | 0 | my $output = ''; | |||||||||||
| 164 | 0 | ▶ | my $sub = sub { $output .= join('', @_) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 165 | 0 | $self->_walk_tree($sub); | |||||||||||
| 166 | 0 | return $output; | |||||||||||
| 167 | } | ||||||||||||
| 168 | |||||||||||||
| 169 | 0 | ▶ | sub size { | ||||||||||
|
Subroutine
size: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 170 | 0 | my $self = shift; | |||||||||||
| 171 | 0 | ▶ | @_ and $self->{size} = shift; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 172 | 0 | $self->{size}; | |||||||||||
| 173 | } | ||||||||||||
| 174 | |||||||||||||
| 175 | 0 | ▶ | sub mtime { | ||||||||||
|
Subroutine
mtime: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 176 | 0 | my $self = shift; | |||||||||||
| 177 | 0 | ▶ | @_ and $self->{mtime} = shift; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 178 | 0 | $self->{mtime}; | |||||||||||
| 179 | } | ||||||||||||
| 180 | |||||||||||||
| 181 | 0 | ▶ | sub dump { | ||||||||||
|
Subroutine
dump: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 182 | 0 | my $self = shift; | |||||||||||
| 183 | 0 | my ($fh) = @_; | |||||||||||
| 184 | 0 | ▶ | $fh ||= \*STDOUT; | ||||||||||
|
Condition0%0/2
$fh ||= \*STDOUT
MC/DC0%0/1
$fh ||= \*STDOUT
$fh
Truth table0%0/2
A
A= $fh
| |||||||||||||
| 185 | |||||||||||||
| 186 | 0 | ▶ | my $sub = sub { print $fh @_ }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 187 | 0 | $self->_walk_tree($sub); | |||||||||||
| 188 | } | ||||||||||||
| 189 | |||||||||||||
| 190 | 0 | ▶ | sub _walk_tree { | ||||||||||
|
Subroutine
_walk_tree: not called CC=4 SCAR=30.0
| |||||||||||||
| 191 | 0 | my $msg = shift; | |||||||||||
| 192 | 0 | my ($sub) = @_; | |||||||||||
| 193 | |||||||||||||
| 194 | # Munge the whole thing into a big old multipart/mixed thingy. | ||||||||||||
| 195 | 0 | $msg->header_del('content-type'); | |||||||||||
| 196 | 0 | my $boundary = "----=_NextPart_000_" . $$ . time; | |||||||||||
| 197 | 0 | $msg->header('Content-Type', "multipart/mixed; boundary=\"$boundary\"\n"); | |||||||||||
| 198 | |||||||||||||
| 199 | 0 | $sub->($msg->raw_headers); | |||||||||||
| 200 | |||||||||||||
| 201 | # Output Received headers first. | ||||||||||||
| 202 | #foreach my $value ($msg->header('Received')) { | ||||||||||||
| 203 | # $sub->("Received: $value\n"); | ||||||||||||
| 204 | #} | ||||||||||||
| 205 | |||||||||||||
| 206 | # Output remaining headers in random order | ||||||||||||
| 207 | #foreach my $header ($msg->headers) { | ||||||||||||
| 208 | # next if lc($header) eq 'received'; | ||||||||||||
| 209 | #foreach my $value ($msg->header($header)) { | ||||||||||||
| 210 | # $header =~ s/(^|-)(\w)/$1 . uc($2)/eg; | ||||||||||||
| 211 | #$sub->("$header: $value\n"); | ||||||||||||
| 212 | #} | ||||||||||||
| 213 | #} | ||||||||||||
| 214 | 0 | $sub->("\n"); | |||||||||||
| 215 | |||||||||||||
| 216 | 0 | $sub->("This is a dump of a parsed message. Ignore this bit.\n\n"); | |||||||||||
| 217 | |||||||||||||
| 218 | 0 | $sub->("--$boundary\n"); | |||||||||||
| 219 | |||||||||||||
| 220 | 0 | my $body_boundary = "----=_NextPart_111_" . $$ . time; | |||||||||||
| 221 | 0 | $sub->("Content-Type: multipart/alternate; boundary=\"$body_boundary\"\n"); | |||||||||||
| 222 | 0 | $sub->("\n"); | |||||||||||
| 223 | 0 | my @body_parts = $msg->bodies; | |||||||||||
| 224 | |||||||||||||
| 225 | 0 | ▶ | while (@body_parts) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 226 | 0 | my ($type, $fh) = splice(@body_parts, 0, 2); | |||||||||||
| 227 | 0 | $sub->("--$body_boundary\n"); | |||||||||||
| 228 | 0 | $sub->("Content-Type: $type\n"); | |||||||||||
| 229 | 0 | ▶ | if ($type !~ /^text\//) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 230 | 0 | $sub->("Content-Transfer-Encoding: base64\n"); | |||||||||||
| 231 | 0 | $sub->("\n"); | |||||||||||
| 232 | 0 | local $/; | |||||||||||
| 233 | 0 | $sub->(encode_base64(<$fh>)); | |||||||||||
| 234 | 0 | $sub->("\n"); | |||||||||||
| 235 | } | ||||||||||||
| 236 | else { | ||||||||||||
| 237 | 0 | $sub->("Content-Transfer-Encoding: 8bit\n"); | |||||||||||
| 238 | 0 | $sub->("\n"); | |||||||||||
| 239 | 0 | local $/; | |||||||||||
| 240 | 0 | $sub->(<$fh>); | |||||||||||
| 241 | 0 | $sub->("\n"); | |||||||||||
| 242 | } | ||||||||||||
| 243 | } | ||||||||||||
| 244 | 0 | $sub->("--$body_boundary--\n\n"); | |||||||||||
| 245 | |||||||||||||
| 246 | 0 | foreach my $att ($msg->attachments) { | |||||||||||
| 247 | 0 | $sub->("--$boundary\n"); | |||||||||||
| 248 | 0 | $sub->("Content-Type: $att->{type}\n"); | |||||||||||
| 249 | 0 | $sub->("Content-Disposition: attachment; filename=$att->{filename}\n"); | |||||||||||
| 250 | 0 | $sub->("Content-Transfer-Encoding: base64\n"); | |||||||||||
| 251 | 0 | $sub->("\n"); | |||||||||||
| 252 | |||||||||||||
| 253 | 0 | my $fh = $att->{fh}; | |||||||||||
| 254 | 0 | local $/; | |||||||||||
| 255 | 0 | $sub->(encode_base64(<$fh>)); | |||||||||||
| 256 | 0 | $sub->("\n"); | |||||||||||
| 257 | } | ||||||||||||
| 258 | |||||||||||||
| 259 | 0 | $sub->("--$boundary--\n"); | |||||||||||
| 260 | } | ||||||||||||
| 261 | |||||||||||||
| 262 | 1 | 1; | |||||||||||
| 263 | __END__ | ||||||||||||