blib/lib/MIME/Lite/Generator.pm

filter: Statementstmt 91.0% 122 / 134 Branchbran 80.9% 68 / 84 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 88.2% 15 / 17 Podpod 16.6% 2 / 12 totaltotal 82.3% 215 / 261 SCAR 39.6
CC
47
Cov
86%
get
15.6
new
15.1
get_encoded_chunk
7.7
CRAP 52.6
↑ Summary
1 package MIME::Lite::Generator;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MIME::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = 0.01;
10  
11 sub new {
12 46 ▶ my ( $class, $msg, $is_smtp ) = @_;
Subroutine new: called CC=15 SCAR=27.1
Pod documented
13
14 46 my $encoding = uc( $msg->{Attrs}{'content-transfer-encoding'} );
15 46 my $chunk_getter;
16 46 ▶ if (defined( $msg->{Path} ) || defined( $msg->{FH} ) || defined ( $msg->{Data} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msg->{'Path'} or defined $msg->{'FH'} or defined $msg->{'Data'}) { } 36 10 46
Condition83%5/6
defined $msg->{"Path"} or defined $msg->{"FH"} or defined $msg->{"Data"}
l!l&&r!l&&!r
102610
defined $msg->{"Path"} or defined $msg->{"FH"}
l!l&&r!l&&!r
10036
MC/DC66%2/3
defined $msg->{"Path"} or defined $msg->{"FH"} or defined $msg->{"Data"}
defined $msg->{"Path"} defined $msg->{"FH"} defined $msg->{"Data"}
Truth table75%3/4
A or B or C
A= defined $msg->{"Path"}B= defined $msg->{"FH"}C= defined $msg->{"Data"}
ABCresult
1XX1
01X1
0011
0000
17 36 ▶ if ($encoding eq 'BINARY') {
Branch: 5 branches
BranchTrueFalseTotal
if ($encoding eq 'BINARY') { } 4 32 36
elsif ($encoding eq '8BIT') { } 19 13 32
elsif ($encoding eq '7BIT') { } 4 9 13
elsif ($encoding eq 'QUOTED-PRINTABLE') { } 4 5 9
elsif ($encoding eq 'BASE64') { } 5 0 5
18 $chunk_getter = defined ( $msg->{Data} )
19 4 ▶ ? 'get_encoded_chunk_data_other'
Branch: 1 branch
BranchTrueFalseTotal
defined $msg->{'Data'} ? : 2 2 4
20 : 'get_encoded_chunk_fh_binary';
21 }
22 elsif ($encoding eq '8BIT') {
23 $chunk_getter = defined ( $msg->{Data} )
24 19 ▶ ? 'get_encoded_chunk_data_other'
Branch: 1 branch
BranchTrueFalseTotal
defined $msg->{'Data'} ? : 17 2 19
25 : 'get_encoded_chunk_fh_8bit';
26 }
27 elsif ($encoding eq '7BIT') {
28 $chunk_getter = defined ( $msg->{Data} )
29 4 ▶ ? 'get_encoded_chunk_data_other'
Branch: 1 branch
BranchTrueFalseTotal
defined $msg->{'Data'} ? : 2 2 4
30 : 'get_encoded_chunk_fh_7bit';
31 }
32 elsif ($encoding eq 'QUOTED-PRINTABLE') {
33 $chunk_getter = defined ( $msg->{Data} )
34 4 ▶ ? 'get_encoded_chunk_data_qp'
Branch: 1 branch
BranchTrueFalseTotal
defined $msg->{'Data'} ? : 2 2 4
35 : 'get_encoded_chunk_fh_qp';
36 }
37 elsif ($encoding eq 'BASE64') {
38 $chunk_getter = defined ( $msg->{Data} )
39 5 ▶ ? 'get_encoded_chunk_data_other'
Branch: 1 branch
BranchTrueFalseTotal
defined $msg->{'Data'} ? : 3 2 5
40 : 'get_encoded_chunk_fh_base64';
41 }
42 else {
43 0 $chunk_getter = 'get_encoded_chunk_unknown';
44 }
45 }
46 else {
47 10 $chunk_getter = 'get_encoded_chunk_nodata';
48 }
49
50 46 ▶ bless {
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 25 21 46
51 msg => $msg,
52 is_smtp => $is_smtp,
53 generators => [],
54 has_chunk => 0,
55 state => 'init',
56 encoding => $encoding,
57 chunk_getter => $chunk_getter,
58 last => '',
59 }, ref($class) ? ref($class) : $class;
60 }
61  
62 sub get {
63 1520 ▶ my $self = shift;
Subroutine get: called CC=13 SCAR=27.5
Pod documented
64
65 ### Do we have generators for embedded/main part(s)
66 1520 ▶ while (@{ $self->{generators} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"generators"};}) 735 785 1520
67 735 my $str_ref = $self->{generators}[0]->get();
68 735 ▶ return $str_ref if $str_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $str_ref 710 25 735
69
70 25 shift @{ $self->{generators} };
71
72 25 ▶ if ($self->{boundary}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"boundary"}) 25 0 25
73 25 ▶ if (@{ $self->{generators} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"generators"};}) 15 10 25
74 15 return \"\n--$self->{boundary}\n";
75 }
76
77 ### Boundary at the end
78 10 return \"\n--$self->{boundary}--\n";
79 }
80 }
81
82 ### What we should to generate
83 785 ▶ if ($self->{state} eq 'init') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} eq "init") 45 740 785
84 45 my $attrs = $self->{msg}{Attrs};
85 45 my $sub_attrs = $self->{msg}{SubAttrs};
86 45 my $rv;
87 45 $self->{state} = 'first';
88
89 ### Output either the body or the parts.
90 ### Notice that we key off of the content-type! We expect fewer
91 ### accidents that way, since the syntax will always match the MIME type.
92 45 my $type = $attrs->{'content-type'};
93 45 ▶ if ( $type =~ m{^multipart/}i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ m[^multipart/]i) { } 10 35 45
elsif ($type =~ m[^message/]) { } 0 35 35
94 10 $self->{boundary} = $sub_attrs->{'content-type'}{'boundary'};
95  
96 ### Preamble:
97 $rv = \($self->{msg}->header_as_string . "\n" . (
98 defined( $self->{msg}{Preamble} )
99 ? $self->{msg}{Preamble}
100 10 ▶ : "This is a multi-part message in MIME format.\n"
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'msg'}{'Preamble'} ? : 0 10 10
101 ) .
102 "\n--$self->{boundary}\n");
103  
104 ### Parts:
105 10 my $part;
106 10 foreach $part ( @{ $self->{msg}{Parts} } ) {
107 25 push @{ $self->{generators} }, $self->new($part, $self->{out}, $self->{is_smtp});
108 }
109 }
110 elsif ( $type =~ m{^message/} ) {
111 0 my @parts = @{ $self->{msg}{Parts} };
112  
113 ### It's a toss-up; try both data and parts:
114 0 ▶ if ( @parts == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@parts == 0) { } 0 0 0
elsif (@parts == 1) { } 0 0 0
115 0 $self->{has_chunk} = 1;
116 0 $rv = $self->get_encoded_chunk()
117 }
118 elsif ( @parts == 1 ) {
119 0 $self->{generators}[0] = $self->new($parts[0], $self->{out}, $self->{is_smtp});
120 0 $rv = $self->{generators}[0]->get();
121 }
122 else {
123 0 Carp::croak "can't handle message with >1 part\n";
124 }
125 }
126 else {
127 35 $self->{has_chunk} = 1;
128 35 $rv = $self->get_encoded_chunk();
129 }
130
131 45 return $rv;
132 }
133
134 740 ▶ return $self->{has_chunk} ? $self->get_encoded_chunk() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'has_chunk'} ? : 705 35 740
135 }
136  
137 sub get_encoded_chunk {
138 740 ▶ my $self = shift;
Subroutine get_encoded_chunk: called CC=7 SCAR=20.4
Pod undocumented
139
140 740 ▶ if ($self->{state} eq 'first') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} eq "first") 35 705 740
141 35 $self->{state} = '';
142 ### Open file if necessary:
143 35 ▶ unless (defined $self->{msg}{Data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"msg"}{"Data"}) 10 25 35
144 10 ▶ if ( defined( $self->{msg}{Path} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'msg'}{'Path'}) { } 10 0 10
145 10 ▶ $self->{fh} = new FileHandle || Carp::croak "can't get new filehandle\n";
Condition33%1/3
'FileHandle'->new || Carp::croak("can't get new filehandle\n")
l!l&&r!l&&!r
1000
MC/DC0%0/2
'FileHandle'->new || Carp::croak("can't get new filehandle\n")
'FileHandle'->new Carp::croak("can't get new filehandle\n")
Truth table33%1/3
A || B
A= 'FileHandle'->newB= Carp::croak("can't get new filehandle\n")
ABresult
1X1
011
000
146 $self->{fh}->open($self->{msg}{Path})
147 10 ▶ or Carp::croak "open $self->{msg}{Path}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->open($self->{"msg"}{"Path"}) or Carp::croak(qq[open $self->{"msg"}{"Path"}: $!\n]) 0 10 10
148 }
149 else {
150 0 $self->{fh} = $self->{msg}{FH};
151 }
152 10 ▶ CORE::binmode($self->{fh}) if $self->{msg}->binmode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"msg"}->binmode 0 10 10
153 }
154
155 ### Headers first
156 35 return \($self->{msg}->header_as_string . "\n");
157 }
158
159 705 my $chunk_getter = $self->{chunk_getter};
160 705 $self->$chunk_getter();
161 }
162  
163 sub get_encoded_chunk_data_qp {
164 4 ▶ my $self = shift;
Subroutine get_encoded_chunk_data_qp: called CC=2 SCAR=6.9
Pod undocumented
165
166 ### Encode it line by line:
167 4 ▶ if ($self->{msg}{Data} =~ m{^(.*[\r\n]*)}smg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"msg"}{"Data"} =~ /^(.*[\r\n]*)/gms) 2 2 4
168 2 my $line = $1; # copy to avoid weird bug; rt 39334
169 2 return \MIME::Lite::encode_qp($line);
170 }
171
172 2 $self->{has_chunk} = 0;
173 2 return;
174 }
175  
176 sub get_encoded_chunk_data_other {
177 23 ▶ my $self = shift;
Subroutine get_encoded_chunk_data_other: called CC=6 SCAR=18.0
Pod undocumented
178 23 $self->{has_chunk} = 0;
179
180 23 ▶ if ($self->{encoding} eq 'BINARY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"encoding"} eq "BINARY") 2 21 23
181 2 ▶ $self->{is_smtp} and $self->{msg}{Data} =~ s/(?!\r)\n\z/\r/;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"is_smtp"} and $self->{"msg"}{"Data"} =~ s/(?!\r)\n\z/\r/ 0 2 2
182 2 return \$self->{msg}{Data};
183 }
184
185 21 ▶ if ($self->{encoding} eq '8BIT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"encoding"} eq "8BIT") 17 4 21
186 17 return \MIME::Lite::encode_8bit( $self->{msg}{Data} );
187 }
188
189 4 ▶ if ($self->{encoding} eq '7BIT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"encoding"} eq "7BIT") 2 2 4
190 2 return \MIME::Lite::encode_7bit( $self->{msg}{Data} );
191 }
192
193 2 ▶ if ($self->{encoding} eq 'BASE64') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"encoding"} eq "BASE64") 2 0 2
194 2 return \MIME::Lite::encode_base64( $self->{msg}{Data} );
195 }
196 }
197  
198 sub get_encoded_chunk_fh_binary {
199 6 ▶ my $self = shift;
Subroutine get_encoded_chunk_fh_binary: called CC=5 SCAR=16.3
Pod undocumented
200 6 my $rv;
201
202 6 ▶ if ( read( $self->{fh}, $_, 2048 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $self->{'fh'}, $_, 2048) { } 4 2 6
203 4 $rv = $self->{last};
204 4 $self->{last} = $_;
205 }
206 else {
207 2 seek $self->{fh}, 0, 0;
208 2 $self->{has_chunk} = 0;
209 2 ▶ if ( length $self->{last} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"last"}) 2 0 2
210 2 ▶ $self->{is_smtp} and $self->{last} =~ s/(?!\r)\n\z/\r/;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"is_smtp"} and $self->{"last"} =~ s/(?!\r)\n\z/\r/ 0 2 2
211 2 $rv = $self->{last};
212 }
213 }
214
215 6 ▶ return defined($rv) ? \$rv : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $rv ? : 6 0 6
216 }
217  
218 sub get_encoded_chunk_fh_8bit {
219 184 ▶ my $self = shift;
Subroutine get_encoded_chunk_fh_8bit: called CC=2 SCAR=6.9
Pod undocumented
220
221 184 ▶ if ( defined( $_ = readline( $self->{fh} ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $self->{"fh"})) 182 2 184
222 182 return \MIME::Lite::encode_8bit($_);
223 }
224
225 2 $self->{has_chunk} = 0;
226 2 seek $self->{fh}, 0, 0;
227 2 return;
228 }
229  
230 sub get_encoded_chunk_fh_7bit {
231 184 ▶ my $self = shift;
Subroutine get_encoded_chunk_fh_7bit: called CC=2 SCAR=6.9
Pod undocumented
232
233 184 ▶ if ( defined( $_ = readline( $self->{fh} ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $self->{"fh"})) 182 2 184
234 182 return \MIME::Lite::encode_7bit($_);
235 }
236
237 2 $self->{has_chunk} = 0;
238 2 seek $self->{fh}, 0, 0;
239 2 return;
240 }
241  
242 sub get_encoded_chunk_fh_qp {
243 184 ▶ my $self = shift;
Subroutine get_encoded_chunk_fh_qp: called CC=2 SCAR=6.9
Pod undocumented
244
245 184 ▶ if ( defined( $_ = readline( $self->{fh} ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $self->{"fh"})) 182 2 184
246 182 return \MIME::Lite::encode_qp($_);
247 }
248
249 2 $self->{has_chunk} = 0;
250 2 seek $self->{fh}, 0, 0;
251 2 return;
252 }
253  
254 sub get_encoded_chunk_fh_base64 {
255 120 ▶ my $self = shift;
Subroutine get_encoded_chunk_fh_base64: called CC=2 SCAR=6.9
Pod undocumented
256
257 120 ▶ if ( read( $self->{fh}, $_, 45 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $self->{"fh"}, $_, 45) 118 2 120
258 118 return \MIME::Lite::encode_base64($_);
259 }
260
261 2 $self->{has_chunk} = 0;
262 2 seek $self->{fh}, 0, 0;
263 2 return;
264 }
265  
266 sub get_encoded_chunk_unknown {
267 0 ▶ croak "unsupported encoding: `$_[0]->{encoding}'\n";
Subroutine get_encoded_chunk_unknown: not called CC=1 SCAR=6.9
Pod undocumented
268 }
269  
270 sub get_encoded_chunk_nodata {
271 0 ▶ croak "no data in this part\n";
Subroutine get_encoded_chunk_nodata: not called CC=1 SCAR=6.9
Pod undocumented
272 }
273  
274 2 1;
275  
276 __END__
↑ Summary