Message/Parser.pm

filter: Statementstmt 9.6% 64 / 661 Branchbran 0.0% 0 / 322 Conditioncond 0.0% 0 / 94 MC/DCmcdc 0.0% 0 / 57 Subroutinesub 24.6% 16 / 65 Podpod 0.0% 0 / 16 totaltotal 6.5% 80 / 1215 CC 206 SCAR 62.6
CC
206
Cov
6%
read
2162.0
parse
650.0
decode
650.0
CRAP 524.8
↑ Summary Message.pm »
1  
2 package Matts::Message::Parser;
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw(@Try_Encodings $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 $VERSION = '1.0';
7  
8 # MIME Message parser, for email and nntp engines.
9  
10 1 ▶use Matts::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use MIME::QuotedPrint;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use UNIVERSAL;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 @Try_Encodings = qw(euc-cn euc-jp shiftjis euc-kr big5-eten iso-8859-15 );
17  
18 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
19 0 ▶ return unless $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"DEBUG"} 0 0 0
20 0 warn((caller)[2], @_);
21 }
22  
23 0 ▶sub mkbinmode {
Subroutine mkbinmode: not called CC=2 SCAR=17.9
Pod undocumented
24 0 ▶ if ($] > 5.007) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] > 5.007) { } 0 0 0
25 0 binmode($_[0], ':utf8');
26 }
27 else {
28 0 binmode($_[0]);
29 }
30 }
31  
32 =head1 NAME
33  
34 Matts::Message::Parser - a MIME message parser for email and nttp
35  
36 =head1 SYNOPSIS
37  
38 use Matts::Message::Parser;
39 open(my $fh, "foo.eml");
40 my $msg = Matts::Message::Parser->parse($fh);
41  
42 =head1 DESCRIPTION
43  
44 This is an email parser I originally wrote when I ran my own business that tries
45 quite hard to decode the various parts of an email correctly and down to unicode
46 so that all strings can be treated the same in perl.
47  
48 DO NOT USE THIS MODULE
49  
50 I urge you, please don't. It's not a very good API. I'm just uploading it to
51 CPAN because it's better for my purposes than most of the Email::* and Mail::*
52 classes I can find, and it's fast, and doesn't use any memory when parsing very
53 large emails, which is a huge bonus for me. But I have no intention of documenting
54 this module any more than I have to.
55  
56 =head1 AUTHOR
57  
58 Matt Sergeant, <matt@sergeant.org>
59  
60 =head1 LICENSE
61  
62 This is free software. You may use it and redistribute it under the same terms
63 as perl itself.
64  
65 =head1 HACKING NOTES
66  
67 =head2 This is how mail messages can come in:
68  
69 =over 4
70  
71 =item 1. Plain text
72  
73 Plain text messages come in with a content-type of text/plain. They
74 may contain attachments as UU Encoded strings.
75  
76 =item 2. HTML text
77  
78 Straight HTML messages come in with a content-type of text/html. They
79 may not contain attachments as far as I'm aware.
80  
81 =item 3. Mixed text, html and maybe other.
82  
83 These messages come in as MIME messages with the content-type of
84 multipart/alternative (alternate means you get to pick which view of the
85 message to display, as all must contain the same basic information).
86  
87 There may not be attachments this way as far as I'm aware.
88  
89 =item 4. Plain text with attachments
90  
91 Here the content-type is multipart/mixed. The first part of the multipart
92 message is the the plain text message (after the preamble, that is), with
93 a content type of text/plain. The remaining parts are attachments.
94  
95 =item 5. HTML text with attachments
96  
97 Again, the content-type is multipart/mixed. The first part of the multipart
98 message is the html message, with a content-type of text/html. The
99 remaining parts are attachments.
100  
101 =item 6. Mixed text, html with attachments
102  
103 Here the main part of the message has a content-type of multipart/mixed. The
104 first part has a content-type of multipart/alternative, and is identical to
105 item 3 above. The remaining parts are the attachments.
106  
107 =item 7. Report.
108  
109 This is a delivery status report. It comes with the main part of the message
110 having a content-type of multipart/report, the first one or two parts of which
111 may be textual content of some sort, and the last seems to be of type
112 message/rfc822.
113  
114 =back
115  
116 Overall this is a fairly naive way to view email messages, as the
117 attachments can be email messages themselves, and thus it gets very
118 recursive. But this should be enough for us to deal with right now.
119  
120 =cut
121  
122 # constructor
123 0 ▶sub parse {
Subroutine parse: not called CC=25 SCAR=64.8
Pod undocumented
124 0 my $class = shift;
125
126 0 my $ioref;
127
128 0 ▶ if (ref($_[0]) and UNIVERSAL::isa($_[0], 'IO::Handle')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and UNIVERSAL::isa($_[0], 'IO::Handle')) { } 0 0 0
Condition0%0/3
ref $_[0] and UNIVERSAL::isa($_[0], "IO::Handle")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and UNIVERSAL::isa($_[0], "IO::Handle")
ref $_[0] UNIVERSAL::isa($_[0], "IO::Handle")
Truth table0%0/3
A and B
A= ref $_[0]B= UNIVERSAL::isa($_[0], "IO::Handle")
ABresult
0X0
100
111
129 0 $ioref = $_[0];
130 }
131 else {
132 0 eval {
133 0 ▶ if (defined(*{$_[0]})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined *{$_[0];}) 0 0 0
134 # throw an exception if not a FH
135 0 $ioref = *{$_[0]}{IO};
136 # if no exception thrown, just use the real FH
137 0 ▶ $ioref = $_[0] if defined($ioref);
Branch: 1 branch
BranchTrueFalseTotal
if defined $ioref 0 0 0
138 }
139 };
140 }
141
142 0 ▶ if (!defined $ioref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ioref) 0 0 0
143 0 $ioref = $class->new_tmpfile();
144 0 print $ioref $_[0];
145 0 seek($ioref, 0, 0);
146 }
147
148 0 shift; # lose $_[0] now
149 0 my %opts = @_;
150
151 0 binmode($ioref);
152
153 0 my $msg = Matts::Message->new();
154  
155 0 $msg->size((stat($ioref))[7]);
156 0 $msg->mtime((stat($ioref))[9]);
157
158 0 my $header = <$ioref>;
159 0 $header =~ s/\r\n/\n/;
160 # $header =~ s/\s+$//;
161
162 0 ▶ if ($header =~ /^from\s+([^:].*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /^from\s+([^:].*)$/) 0 0 0
163 0 $msg->header('Envelope-From', $1);
164 0 $header = <$ioref>;
165 0 $header =~ s/\r\n/\n/;
166 # $header =~ s/\s+$//;
167 }
168
169 0 local $_; # protect from abuse
170
171 HEADER:
172 0 ▶ while (my $last = <$ioref>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $last = readline $ioref)) 0 0 0
173 0 $last =~ s/\r\n/\n/;
174 # chomp($last);
175 # $last =~ s/\s+$//;
176 0 ▶ if ($last =~ /^\s+\S/) { # if its a continuation
Branch: 1 branch
BranchTrueFalseTotal
if ($last =~ /^\s+\S/) 0 0 0
177 0 $header .= $last; # fold continuations
178 0 next HEADER;
179 }
180
181 # not a continuation...
182 0 my ($key, $value) = split( /:\s*/, $header, 2);
183 0 ▶ if ($value =~ /[\x80-\xff]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /[\x80-\xff]/) { } 0 0 0
184 # header contains high 8bit chars
185 0 $msg->binary_header($key, $value);
186 }
187 else {
188 0 $value = $class->decode_header($key, $value);
189 # debug("Got header: $key: $value\n");
190 0 $msg->header($key, $value);
191 }
192
193 0 $header = $last;
194
195 0 ▶ last HEADER if ($last =~ /^$/m);
Branch: 1 branch
BranchTrueFalseTotal
if $last =~ /^$/m 0 0 0
196 }
197
198 0 ▶ if ($opts{header_only}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"header_only"}) 0 0 0
199 0 ▶ if ($msg->binary_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->binary_headers) 0 0 0
200 0 debug("Binary headers found - trying to decode them without body hints\n");
201 0 my $conv = $class->converter('ASCII');
202 0 foreach my $header ($msg->binary_headers()) {
203 0 debug("Trying to fix up $header\n");
204 0 $msg->header($header, $conv->convert($msg->binary_header($header)));
205 }
206 }
207 0 return $msg;
208 }
209
210 0 my $body = $class->new_tmpfile();
211 0 binmode($body);
212 0 my $lines = 0;
213
214 0 ▶ while (my $line = <$ioref>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $ioref)) 0 0 0
215 0 $line =~ s/\r\n/\n/;
216 0 print $body $line;
217 0 $lines++;
218 }
219
220 0 seek($body, 0, 0);
221
222 0 $class->parse_body($msg, $msg, $body);
223  
224 # warn("Fixup binary headers\n");
225  
226 0 ▶ return $msg unless $msg->binary_headers;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg->binary_headers 0 0 0
227  
228 0 my $binenc = '';
229 0 my @bodies = $msg->bodies;
230 0 my $id = 0;
231 0 ▶ while (@bodies) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bodies) 0 0 0
232 0 my ($type, $fh) = splice(@bodies, 0, 2);
233 0 my $enc = $msg->body_enc($id);
234 0 ▶ if ($enc ne 'null') {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc ne "null") 0 0 0
235 0 $binenc = $enc;
236 0 last;
237 }
238 0 $id++;
239 }
240
241 0 debug("Fixup binary headers. Got binenc: $binenc\n");
242
243 0 ▶ if (!$binenc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($binenc) 0 0 0
244 0 my $ct = $msg->header('x-original-content-type');
245 0 debug("binenc was blank. Trying content-type: $ct\n");
246 0 ▶ if ($ct and $ct =~ /charset="?([^\";]+)"?;?/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct and $ct =~ /charset="?([^\";]+)"?;?/i) 0 0 0
Condition0%0/3
$ct and $ct =~ /charset="?([^\";]+)"?;?/i
!ll&&!rl&&r
000
MC/DC0%0/2
$ct and $ct =~ /charset="?([^\";]+)"?;?/i
$ct $ct =~ /charset="?([^\";]+)"?;?/i
Truth table0%0/3
A and B
A= $ctB= $ct =~ /charset="?([^\";]+)"?;?/i
ABresult
0X0
100
111
247 0 $binenc = $1;
248 }
249 }
250
251 0 ▶ $binenc ||= 'ASCII';
Condition0%0/2
$binenc ||= "ASCII"
l!l
00
MC/DC0%0/1
$binenc ||= "ASCII"
$binenc
Truth table0%0/2
A
A= $binenc
Aresult
11
00
252
253 0 my $conv = $class->converter($binenc);
254 0 foreach my $header ($msg->binary_headers()) {
255 0 debug("Fixing up $header to be $binenc\n");
256 0 $msg->header($header,
257 $conv->convert($msg->binary_header($header)));
258 }
259
260 0 ▶ unless ($msg->header('message-id')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg->header("message-id")) 0 0 0
261 0 $msg->header('message-id', '<' . time . '@unknown>');
262 }
263
264 0 ▶ unless ($msg->header('subject')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg->header("subject")) 0 0 0
265 0 $msg->header('subject', "No Topic");
266 }
267
268 0 return $msg;
269 }
270  
271 0 ▶sub parse_body {
Subroutine parse_body: not called CC=9 SCAR=45.0
Pod undocumented
272 0 my $class = shift;
273 0 my ($msg, $_msg, $body) = @_;
274
275 0 ▶ my $type = $_msg->header('Content-Type') || 'text/plain';
Condition0%0/2
$_msg->header('Content-Type') || 'text/plain'
l!l
00
MC/DC0%0/1
$_msg->header('Content-Type') || 'text/plain'
$_msg->header('Content-Type')
Truth table0%0/2
A
A= $_msg->header('Content-Type')
Aresult
11
00
276
277 0 debug("Parsing message of type: $type\n");
278
279 0 ▶ if ($type =~ /^text\/html/i) {
Branch: 5 branches
BranchTrueFalseTotal
if ($type =~ m[^text/html]i) { } 0 0 0
elsif ($type =~ /^text/i) { } 0 0 0
elsif ($type =~ /ms-tnef/i) { } 0 0 0
elsif ($type =~ m[^multipart/alternative]i) { } 0 0 0
elsif ($type =~ m[^multipart/]i) { } 0 0 0
280 0 debug("Parse text/html\n");
281 0 $class->parse_normal($msg, $_msg, $body);
282 }
283 elsif ($type =~ /^text/i) {
284 0 debug("Parse text/plain\n");
285 0 $class->parse_normal($msg, $_msg, $body);
286 #$class->process_uue($msg);
287 }
288 elsif ($type =~ /ms-tnef/i) {
289 0 debug("Parse ms-tnef\n");
290 0 eval {
291 0 $class->parse_tnef($msg, $_msg, $body);
292 };
293 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
294 0 warn("parse_tnef failed: $@\n");
295 }
296 }
297 elsif ($type =~ /^multipart\/alternative/i) {
298 0 debug("Parse multipart/alternative\n");
299 0 $class->parse_multipart_alternate($msg, $_msg, $body);
300 }
301 elsif ($type =~ /^multipart\//i) {
302 0 debug("Parse $type\n");
303 0 $class->parse_multipart_mixed($msg, $_msg, $body);
304 }
305 else {
306 0 debug("Regular attachment\n");
307 0 $class->decode_attachment($msg, $_msg, $body);
308 }
309
310 0 ▶ if (!$msg->body()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg->body) 0 0 0
311 0 debug("No message body found. Reparsing\n");
312 0 my $part_fh = $class->new_tmpfile();
313 0 my $part_msg = Matts::Message->new();
314 0 $class->decode_body($msg, $part_msg, $part_fh);
315 }
316
317 0 $class->process_uue($msg);
318 }
319  
320 0 ▶sub parse_multipart_alternate {
Subroutine parse_multipart_alternate: not called CC=13 SCAR=52.0
Pod undocumented
321 0 my $class = shift;
322 0 my ($msg, $_msg, $body) = @_;
323
324 0 my ($boundary) = $_msg->header('content-type') =~ /boundary\s*=\s*["']?([^"';]+)["']?/i;
325
326 0 my $preamble = '';
327  
328 0 debug("m/a got boundary: $boundary\n");
329
330 # extract preamble (normally contains "This message is in Multipart/MIME format")
331 0 ▶ while(my $line = <$body>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $body)) 0 0 0
332 0 $line =~ s/\r\n/\n/;
333 0 ▶ last if $line =~ /^\-\-\Q$boundary\E$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\-\-\Q$boundary\E$/ 0 0 0
334 0 $preamble .= $line;
335 }
336  
337 0 debug("preamble: [[$preamble]]\n");
338
339 0 my $part_fh = $class->new_tmpfile();
340 0 my $part_msg = Matts::Message->new();
341 0 my $in_body = 0;
342
343 0 my $header;
344  
345 0 ▶ while(<$body>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $body)) 0 0 0
346 0 s/\r\n/\n/;
347 # debug($_);
348 0 ▶ if (/^\-\-\Q$boundary\E/ || eof($body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\-\-\Q$boundary\E/ or eof $body) 0 0 0
Condition0%0/3
/^\-\-\Q$boundary\E/ or eof $body
l!l&&r!l&&!r
000
MC/DC0%0/2
/^\-\-\Q$boundary\E/ or eof $body
/^\-\-\Q$boundary\E/ eof $body
Truth table0%0/3
A or B
A= /^\-\-\Q$boundary\E/B= eof $body
ABresult
1X1
011
000
349 0 debug("m/a got end of section\n");
350 # end of part
351 0 seek($part_fh, 0, 0);
352 0 my $line = $_;
353 # assume body part if it's text
354 0 ▶ if ($part_msg->header('content-type') =~ /^text/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part_msg->header('content-type') =~ /^text/i) { } 0 0 0
355 0 $class->decode_body($msg, $part_msg, $part_fh);
356 }
357 else {
358 0 debug("Likely virus?\n");
359 0 $class->decode_attachment($msg, $part_msg, $part_fh);
360 }
361 0 ▶ last if $line =~ /^\-\-\Q$boundary\E\-\-$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\-\-\Q$boundary\E\-\-$/ 0 0 0
362 0 $in_body = 0;
363 0 $part_msg = Matts::Message->new();
364 0 $part_fh = $class->new_tmpfile();
365 0 next;
366 }
367
368 0 ▶ if ($in_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_body) { } 0 0 0
369 0 print $part_fh $_;
370 }
371 else {
372 # chomp($_);
373 0 s/\s+$//;
374 0 ▶ if (m/^\S/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\S/) { } 0 0 0
elsif (/^$/) { } 0 0 0
375 0 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 0 0 0
376 0 my ($key, $value) = split( /:\s*/, $header, 2);
377 0 $part_msg->header($key, $value);
378 }
379 0 $header = $_;
380 }
381 elsif (/^$/) {
382 0 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 0 0 0
383 0 my ($key, $value) = split( /:\s*/, $header, 2);
384 0 $part_msg->header($key, $value);
385 }
386 0 $in_body = 1;
387 }
388 else {
389 0 $_ =~ s/^\s*//;
390 0 $header .= $_;
391 }
392 }
393 }
394
395 }
396  
397 0 ▶sub parse_multipart_mixed {
Subroutine parse_multipart_mixed: not called CC=12 SCAR=50.5
Pod undocumented
398 0 my $class = shift;
399 0 my ($msg, $_msg, $body) = @_;
400
401 0 my ($boundary) = $_msg->header('content-type') =~ /boundary\s*=\s*["']?([^"';]+)["']?/i;
402
403 0 debug("m/m Got boundary: $boundary\n");
404 0 my $preamble = '';
405
406 # extract preamble (normally contains "This message is in Multipart/MIME format")
407 0 ▶ while(my $line = <$body>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $body)) 0 0 0
408 0 $line =~ s/\r\n/\n/;
409 0 ▶ last if $line =~ /^\-\-\Q$boundary\E$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\-\-\Q$boundary\E$/ 0 0 0
410 0 $preamble .= $line;
411 }
412  
413 0 debug("Extracted preamble: [[$preamble]]\n");
414
415 0 my $part_fh = $class->new_tmpfile();
416 0 my $part_msg = Matts::Message->new(); # just used for headers storage
417 0 my $in_body = 0;
418
419 0 my $header;
420  
421 0 ▶ while(<$body>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $body)) 0 0 0
422 0 s/\r\n/\n/;
423 # debug($_);
424 0 ▶ if (/^\-\-\Q$boundary\E/ || eof($body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\-\-\Q$boundary\E/ or eof $body) 0 0 0
Condition0%0/3
/^\-\-\Q$boundary\E/ or eof $body
l!l&&r!l&&!r
000
MC/DC0%0/2
/^\-\-\Q$boundary\E/ or eof $body
/^\-\-\Q$boundary\E/ eof $body
Truth table0%0/3
A or B
A= /^\-\-\Q$boundary\E/B= eof $body
ABresult
1X1
011
000
425 # end of part
426 0 debug("Got end of MIME section: $_\n");
427 0 my $line = $_;
428 0 seek($part_fh, 0, 0);
429 0 $class->parse_body($msg, $part_msg, $part_fh);
430
431 0 ▶ last if $line =~ /^\-\-\Q${boundary}\E\-\-$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\-\-\Q$boundary\E\-\-$/ 0 0 0
432 0 $in_body = 0;
433 0 $part_msg = Matts::Message->new();
434 0 $part_fh = $class->new_tmpfile();
435 0 next;
436 }
437
438 0 ▶ if ($in_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_body) { } 0 0 0
439 0 print $part_fh $_;
440 }
441 else {
442 # chomp($_);
443 0 s/\s+$//;
444 0 ▶ if (m/^\S/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\S/) { } 0 0 0
elsif (/^$/) { } 0 0 0
445 0 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 0 0 0
446 0 my ($key, $value) = split( /:\s*/, $header, 2);
447 0 $part_msg->header($key, $value);
448 }
449 0 $header = $_;
450 }
451 elsif (/^$/) {
452 0 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 0 0 0
453 0 my ($key, $value) = split( /:\s*/, $header, 2);
454 0 $part_msg->header($key, $value);
455 }
456 0 $in_body = 1;
457 }
458 else {
459 0 $_ =~ s/^\s*//;
460 0 $header .= $_;
461 }
462 }
463 }
464
465 }
466  
467 0 ▶sub parse_normal {
Subroutine parse_normal: not called CC=1 SCAR=6.9
Pod undocumented
468 0 my $class = shift;
469 0 my ($msg, $_msg, $body) = @_;
470
471 # extract body, store it in $msg
472 0 $class->decode_body($msg, $_msg, $body);
473 }
474  
475 1 ▶use File::Path qw(rmtree);
Subroutine BEGIN: called CC=1 SCAR=0.0
476  
477 0 ▶sub parse_tnef {
Subroutine parse_tnef: not called CC=4 SCAR=30.0
Pod undocumented
478 0 my $class = shift;
479 0 my ($msg, $_msg, $body) = @_;
480
481 0 my ($type, $main) = $class->decode($_msg, $body);
482 0 debug("got tnef: $type\n");
483
484 0 my $dir = $class->new_tmpdir();
485
486 # Create a tnef object
487 0 ▶ my $tnef = Matts::Message::TNEF->read($main, { output_dir => $dir })
Branch: 1 branch
BranchTrueFalseTotal
my $tnef = "Matts::Message::TNEF"->read($main, {"output_dir", $dir}) or die $Matts::Message::TNEF::errstr 0 0 0
488 or die $Matts::Message::TNEF::errstr;
489  
490 0 my $body_part = $tnef->message();
491 0 ▶ if (my $data = $body_part->data) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $body_part->data) 0 0 0
492 0 my $fh = $class->new_tmpfile();
493 0 debug("Got tnef body part: $data\n");
494 0 print $fh $data;
495 0 seek($fh, 0, 0);
496 # Make possibly invalid assumption that it's text/plain
497 0 debug("Adding tnef body part\n");
498 0 $msg->add_body_part("text/plain", $fh);
499 }
500
501 0 for my $part ($tnef->attachments) {
502 0 my $fh = $class->new_tmpfile();
503 0 print $fh $part->data;
504 0 seek($fh, 0, 0);
505 0 my $filename = $part->longname;
506 0 debug("Got tnef attachment part $filename\n");
507 0 debug("Adding tnef attachment: $filename\n");
508 0 $msg->add_attachment("application/octet-stream", $fh, $filename);
509 }
510
511 0 rmtree($dir);
512 }
513  
514 0 ▶sub process_uue {
Subroutine process_uue: not called CC=1 SCAR=6.9
Pod undocumented
515 0 my $class = shift;
516 0 my ($msg) = @_;
517 }
518  
519 0 ▶sub _decode_header {
Subroutine _decode_header: not called CC=3 SCAR=24.8
520 0 my ($encoding, $cte, $data) = @_;
521
522 0 my $converter = __PACKAGE__->converter($encoding);
523 0 ▶ my $decoder = $cte eq 'B' ? \&MIME::Base64::decode_base64 :
Branch: 2 branches
BranchTrueFalseTotal
$cte eq 'B' ? : 0 0 0
$cte eq 'Q' ? : 0 0 0
524 $cte eq 'Q' ? \&MIME::QuotedPrint::decode_qp :
525 die "Unknown encoding type '$cte' in RFC2047 header";
526
527 0 return $converter->convert($decoder->($data));
528 }
529  
530 # decode according to RFC2047
531 0 ▶sub decode_header {
Subroutine decode_header: not called CC=4 SCAR=30.0
Pod undocumented
532 0 my $class = shift;
533 0 my ($key, $header) = @_;
534  
535 0 ▶ return '' unless $header;
Branch: 1 branch
BranchTrueFalseTotal
unless $header 0 0 0
536 0 ▶ return $header unless ($header =~ /=\?/ or $header =~ /[\x80-\xff]/);
Branch: 1 branch
BranchTrueFalseTotal
unless $header =~ /=\?/ or $header =~ /[\x80-\xff]/ 0 0 0
Condition0%0/3
$header =~ /=\?/ or $header =~ /[\x80-\xff]/
l!l&&r!l&&!r
000
MC/DC0%0/2
$header =~ /=\?/ or $header =~ /[\x80-\xff]/
$header =~ /=\?/ $header =~ /[\x80-\xff]/
Truth table0%0/3
A or B
A= $header =~ /=\?/B= $header =~ /[\x80-\xff]/
ABresult
1X1
011
000
537  
538 0 $header =~ s/=\?([\w_-]+)\?([bqBQ])\?(.*?)\?=/_decode_header($1, uc($2), $3)/ge;
539 0 return $header;
540 }
541  
542 0 ▶sub decode_body {
Subroutine decode_body: not called CC=1 SCAR=6.9
Pod undocumented
543 0 my $class = shift;
544 0 my ($msg, $part_msg, $body) = @_;
545
546 0 my ($type, $main) = $class->decode($part_msg, $body);
547  
548 0 debug("got body: $type\n");
549
550 0 $msg->add_body_part($type, $main);
551 }
552  
553 0 ▶sub decode_attachment {
Subroutine decode_attachment: not called CC=1 SCAR=6.9
Pod undocumented
554 0 my $class = shift;
555 0 my ($msg, $part_msg, $fh) = @_;
556
557 0 debug("decoding attachment\n");
558
559 0 my ($type, $content, $filename) = $class->decode($part_msg, $fh);
560
561 0 $msg->add_attachment($type, $content, $filename);
562 }
563  
564 0 ▶sub decode ($$;$) {
Subroutine decode: not called CC=25 SCAR=64.8
Pod undocumented
565 0 my $class = shift;
566 0 my ($msg, $body) = @_;
567
568 0 my $fh = $class->new_tmpfile();
569 0 binmode($fh);
570
571 0 my $hibit;
572
573 0 ▶ if (lc($msg->header('content-transfer-encoding')) eq 'quoted-printable') {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $msg->header('content-transfer-encoding') eq 'quoted-printable') { } 0 0 0
elsif (lc $msg->header('content-transfer-encoding') eq 'base64') { } 0 0 0
574 0 debug("decoding QP file\n");
575 0 ▶ while(<$body>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $body)) 0 0 0
576 0 $_ = MIME::QuotedPrint::decode_qp($_);
577 0 print $fh $_;
578 0 ▶ $hibit++ if /[\x80-\xff]/;
Branch: 1 branch
BranchTrueFalseTotal
if /[\x80-\xff]/ 0 0 0
579 }
580 }
581 elsif (lc($msg->header('content-transfer-encoding')) eq 'base64') {
582 0 debug("decoding B64 file\n");
583 0 my $output = '';
584 0 local $/ = "\n";
585 0 my $not_really_base64 = 0;
586 0 ▶ while(<$body>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $body)) 0 0 0
587 # check to see if its really base64 encoded data
588 0 ▶ $not_really_base64++ if /[<\.,]/;
Branch: 1 branch
BranchTrueFalseTotal
if /[<\.,]/ 0 0 0
589 0 chomp;
590 0 ▶ if ($not_really_base64) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_really_base64) { } 0 0 0
591 0 print $fh $_;
592 0 ▶ $hibit++ if /[\x80-\xff]/;
Branch: 1 branch
BranchTrueFalseTotal
if /[\x80-\xff]/ 0 0 0
593 }
594 else {
595 # pad with = chars - stops MIME::Base64 outputting warnings
596 0 ▶ if (my $len = (length($_) % 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = length($_) % 4) 0 0 0
597 0 $_ .= "=" x (4 - $len);
598 }
599 0 $_ = MIME::Base64::decode_base64($_);
600 0 print $fh $_;
601 0 ▶ $hibit++ if /[\x80-\xff]/;
Branch: 1 branch
BranchTrueFalseTotal
if /[\x80-\xff]/ 0 0 0
602 }
603 }
604 }
605 else {
606 0 debug("decoding other encoding\n");
607 # Encoding is one of 7bit, 8bit, binary or x-something - just save.
608 0 my $buf;
609 0 ▶ while($buf = <$body>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($buf = readline $body)) 0 0 0
610 # debug("BODY: $buf");
611 0 print $fh $buf;
612 0 ▶ $hibit++ if $buf =~ /[\x80-\xff]/;
Branch: 1 branch
BranchTrueFalseTotal
if $buf =~ /[\x80-\xff]/ 0 0 0
613 }
614 }
615
616 0 seek($fh, 0, 0);
617
618 0 my $type = $msg->header('content-type');
619 0 local $^W;
620 0 my ($filename) = ($msg->header('content-disposition') =~ /name="?([^\";]+)"?/i);
621 0 ▶ if (!$filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 0 0
622 0 ($filename) = ($msg->header('content-type') =~ /name="?([^\";]+)"?/i);
623 }
624
625 0 debug("Body type was: $type\n");
626
627 0 my $binenc;
628 0 ▶ if ($type =~ /html/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /html/i) 0 0 0
629 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
630 0 ▶ if (/<META\s[^>]*charset="?([\w-]*)[^>]*>/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/<META\s[^>]*charset="?([\w-]*)[^>]*>/i) 0 0 0
631 0 $binenc = $1;
632 0 $type = "text/html";
633 0 last;
634 }
635 }
636 0 seek($fh, 0, 0);
637 }
638
639 0 my $converter;
640 0 ▶ if ($binenc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($binenc) { } 0 0 0
elsif ($type and $type =~ /^text\b/i) { } 0 0 0
641 0 $converter = $class->converter($binenc);
642 0 $msg->header('content-type', 'text/html');
643 }
644 ▶ elsif ($type && ($type =~ /^text\b/i)) {
Condition0%0/3
$type and $type =~ /^text\b/i
!ll&&!rl&&r
000
MC/DC0%0/2
$type and $type =~ /^text\b/i
$type $type =~ /^text\b/i
Truth table0%0/3
A and B
A= $typeB= $type =~ /^text\b/i
ABresult
0X0
100
111
645 # text type - might need to translate to UTF8
646 0 debug("Trying to get charset from content-type: $type\n");
647 # remember to strip charset portion - we can always add it later.
648 0 $msg->header('X-Original-Content-Type', $type);
649 0 ▶ if ($type =~ s/charset="?([^\";]+)"?;?//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ s/charset="?([^\";]+)"?;?//i) 0 0 0
650 0 $binenc = $1;
651 0 $converter = $class->converter($binenc);
652 0 $msg->header('content-type', $type);
653 }
654 }
655
656 0 ▶ if ($hibit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hibit) 0 0 0
657 # we say ascii here, but in reality the converter should skip to the next converter
658 0 ▶ $converter ||= $class->converter('ascii');
Condition0%0/3
$converter ||= $class->converter("ascii")
l!l&&r!l&&!r
000
MC/DC0%0/2
$converter ||= $class->converter("ascii")
$converter $class->converter("ascii")
Truth table0%0/3
A ||= B
A= $converterB= $class->converter("ascii")
ABresult
1X1
011
000
659 }
660
661 0 ▶ if ($converter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($converter) 0 0 0
662 0 my $decoded_fh = $class->new_tmpfile();
663 0 my $data = '';
664 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
665 0 $data .= $_;
666 }
667 0 $data =~ s/<META\s[^>]*charset="?([\w-]*)[^>]*>//i;
668 0 print $decoded_fh $converter->convert($data);
669 0 seek($decoded_fh, 0, 0);
670 # warn("Decoded body. Returning type = $type; charset=$binenc\n");
671 0 return "$type; charset=$binenc", $decoded_fh, $filename;
672 }
673
674 0 return $type, $fh, $filename;
675 }
676  
677 1 ▶use File::Temp qw(tempfile tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
678  
679 0 ▶sub new_tmpfile {
Subroutine new_tmpfile: not called CC=2 SCAR=17.9
Pod undocumented
680 0 my $class = shift;
681 0 ▶ my $tmpfile = tempfile() || croak "new_tmpfile failed : $!";
Condition0%0/2
tempfile() || croak("new_tmpfile failed : $!")
l!l
00
MC/DC0%0/1
tempfile() || croak("new_tmpfile failed : $!")
tempfile()
Truth table0%0/2
A
A= tempfile()
Aresult
11
00
682 0 mkbinmode($tmpfile);
683 0 return $tmpfile;
684 }
685  
686 0 ▶sub new_tmpdir {
Subroutine new_tmpdir: not called CC=2 SCAR=17.9
Pod undocumented
687 0 my $class = shift;
688 0 ▶ return tempdir(CLEANUP => 1) || croak "new_tmpdir failed: $!";
Condition0%0/2
tempdir('CLEANUP', 1) || croak("new_tmpdir failed: $!")
l!l
00
MC/DC0%0/1
tempdir('CLEANUP', 1) || croak("new_tmpdir failed: $!")
tempdir('CLEANUP', 1)
Truth table0%0/2
A
A= tempdir('CLEANUP', 1)
Aresult
11
00
689 }
690  
691 0 ▶sub converter {
Subroutine converter: not called CC=2 SCAR=17.9
Pod undocumented
692 0 my $class = shift;
693 0 my ($charset) = @_;
694
695 # some broken mailers say they're us-ascii, but include higher chars
696 # $charset = 'ISO-8859-15' if $charset =~ /us-?ascii/i;
697  
698 0 ▶ my $converter = eval { EncodeConverter->new($charset) }
Condition0%0/3
eval { do { 'EncodeConverter'->new($charset) } } || 'NullConverter'->new
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { 'EncodeConverter'->new($charset) } } || 'NullConverter'->new
eval { do { 'EncodeConverter'->new($charset) } } 'NullConverter'->new
Truth table0%0/3
A || B
A= eval { do { 'EncodeConverter'->new($charset) } }B= 'NullConverter'->new
ABresult
1X1
011
000
699 || NullConverter->new();
700  
701 0 return $converter;
702 }
703  
704 package EncodeConverter;
705  
706 1 *debug = *Matts::Message::Parser::debug;
707  
708 1 my $loaded = 0;
709 0 ▶sub load_encode {
Subroutine load_encode: not called CC=6 SCAR=37.4
710 0 ▶ return if $loaded;
Branch: 1 branch
BranchTrueFalseTotal
if $loaded 0 0 0
711 0 require Encode;
712 0 require Encode::Alias;
713 0 foreach my $enc (Encode->encodings(':all')) {
714 0 ▶ $enc =~ /^cp(\d+)$/ or next;
Branch: 1 branch
BranchTrueFalseTotal
$enc =~ /^cp(\d+)$/ or next 0 0 0
715 # warn("Defining alias: windows-$1 => $enc\n");
716 0 eval { Encode::Alias::define_alias("windows-$1" => $enc) };
717 0 ▶ warn($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
718 }
719 0 eval { Encode::Alias::define_alias("big5" => 'big5-eten') };
720 0 eval { Encode::Alias::define_alias("iso-8859-8-i" => 'iso-8859-8') };
721 0 ▶ warn($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
722 0 $loaded++;
723 }
724  
725 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
726 0 my $class = shift;
727 0 my ($from) = @_;
728 0 load_encode();
729 0 return bless { from => $from }, $class;
730 }
731  
732 0 ▶sub try_decode {
Subroutine try_decode: not called CC=7 SCAR=40.3
733 0 my $self = shift;
734 0 my $data = shift;
735 0 foreach my $enc ($self->{from}, @Matts::Message::Parser::Try_Encodings) {
736 0 ▶ next if $enc =~ /ascii/i;
Branch: 1 branch
BranchTrueFalseTotal
if $enc =~ /ascii/i 0 0 0
737 0 debug("Trying: $enc\n");
738 0 my $d = $data;
739 0 my $results = eval {
740 0 Encode::decode($enc, $d, Encode::FB_CROAK());
741 };
742 0 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
743 # debug("Success!: $enc => $results\n");
744 0 ▶ if ($self->{from} ne $enc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"from"} ne $enc) 0 0 0
745 0 $self->{from} = $enc;
746 }
747 0 ▶ return wantarray ? ($results, $enc) : $results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
748 }
749 0 debug("$enc failed: $@\n");
750 }
751 0 ▶ return wantarray ? ($data, 'UTF-8') : $data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
752 }
753  
754 0 ▶sub convert {
Subroutine convert: not called CC=1 SCAR=6.9
755 0 my $self = shift;
756 0 return scalar $self->try_decode($_[0]);
757 }
758  
759 package NullConverter;
760  
761 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
762 0 my $class = shift;
763 0 bless {}, $class;
764 }
765  
766 0 ▶sub try_decode {
Subroutine try_decode: not called CC=1 SCAR=6.9
767 0 my $self = shift;
768 0 return $_[0];
769 }
770  
771 0 ▶sub convert {
Subroutine convert: not called CC=1 SCAR=6.9
772 0 my $self = shift;
773 0 return $_[0];
774 }
775  
776 # Taken from Convert::TNEF.pm
777 #
778 # Copyright (c) 1999 Douglas Wilson <dougw@cpan.org>. All rights reserved.
779 # This program is free software; you can redistribute it and/or
780 # modify it under the same terms as Perl itself.
781  
782 package Matts::Message::TNEF;
783  
784 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
785 1 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
786 1 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
787 $TNEF_SIGNATURE
788 $TNEF_PURE
789 $LVL_MESSAGE
790 $LVL_ATTACHMENT
791 $errstr
792 $g_file_cnt
793 %dflts
794 %atp
795 %att
796 %att_name
797 1 );
798  
799 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
800 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
801  
802 # Set some TNEF constants. Everything turned
803 # out to be in little endian order, so I just added
804 # 'reverse' everywhere that I needed to
805 # instead of reversing the hex codes.
806 1 $TNEF_SIGNATURE = reverse pack( 'H*', '223E9F78' );
807 1 $TNEF_PURE = reverse pack( 'H*', '00010000' );
808  
809 1 $LVL_MESSAGE = pack( 'H*', '01' );
810 1 $LVL_ATTACHMENT = pack( 'H*', '02' );
811  
812 1 %atp = (
813 Triples => pack( 'H*', '0000' ),
814 String => pack( 'H*', '0001' ),
815 Text => pack( 'H*', '0002' ),
816 Date => pack( 'H*', '0003' ),
817 Short => pack( 'H*', '0004' ),
818 Long => pack( 'H*', '0005' ),
819 Byte => pack( 'H*', '0006' ),
820 Word => pack( 'H*', '0007' ),
821 Dword => pack( 'H*', '0008' ),
822 Max => pack( 'H*', '0009' ),
823 );
824  
825 1 for ( keys %atp ) {
826 10 $atp{$_} = reverse $atp{$_};
827 }
828  
829 33 ▶sub _ATT {
Subroutine _ATT: called CC=1 SCAR=0.0
830 33 my ( $att, $id ) = @_;
831 33 return reverse($id) . $att;
832 }
833  
834 # The side comments are 'MAPI' equivalents
835 %att = (
836 Null => _ATT( pack( 'H*', '0000' ), pack( 'H4', '0000' ) ),
837 # PR_ORIGINATOR_RETURN_ADDRESS
838 From => _ATT( $atp{Triples}, pack( 'H*', '8000' ) ),
839 # PR_SUBJECT
840 Subject => _ATT( $atp{String}, pack( 'H*', '8004' ) ),
841 # PR_CLIENT_SUBMIT_TIME
842 DateSent => _ATT( $atp{Date}, pack( 'H*', '8005' ) ),
843 # PR_MESSAGE_DELIVERY_TIME
844 DateRecd => _ATT( $atp{Date}, pack( 'H*', '8006' ) ),
845 # PR_MESSAGE_FLAGS
846 MessageStatus => _ATT( $atp{Byte}, pack( 'H*', '8007' ) ),
847 # PR_MESSAGE_CLASS
848 MessageClass => _ATT( $atp{Word}, pack( 'H*', '8008' ) ),
849 # PR_MESSAGE_ID
850 MessageID => _ATT( $atp{String}, pack( 'H*', '8009' ) ),
851 # PR_PARENT_ID
852 ParentID => _ATT( $atp{String}, pack( 'H*', '800A' ) ),
853 # PR_CONVERSATION_ID
854 ConversationID => _ATT( $atp{String}, pack( 'H*', '800B' ) ),
855 Body => _ATT( $atp{Text}, pack( 'H*', '800C' ) ), # PR_BODY
856 # PR_IMPORTANCE
857 Priority => _ATT( $atp{Short}, pack( 'H*', '800D' ) ),
858 # PR_ATTACH_DATA_xxx
859 AttachData => _ATT( $atp{Byte}, pack( 'H*', '800F' ) ),
860 # PR_ATTACH_FILENAME
861 AttachTitle => _ATT( $atp{String}, pack( 'H*', '8010' ) ),
862 # PR_ATTACH_RENDERING
863 AttachMetaFile => _ATT( $atp{Byte}, pack( 'H*', '8011' ) ),
864 # PR_CREATION_TIME
865 AttachCreateDate => _ATT( $atp{Date}, pack( 'H*', '8012' ) ),
866 # PR_LAST_MODIFICATION_TIME
867 AttachModifyDate => _ATT( $atp{Date}, pack( 'H*', '8013' ) ),
868 # PR_LAST_MODIFICATION_TIME
869 DateModified => _ATT( $atp{Date}, pack( 'H*', '8020' ) ),
870 #PR_ATTACH_TRANSPORT_NAME
871 AttachTransportFilename => _ATT( $atp{Byte}, pack( 'H*', '9001' ) ),
872 AttachRenddata => _ATT( $atp{Byte}, pack( 'H*', '9002' ) ),
873 MAPIProps => _ATT( $atp{Byte}, pack( 'H*', '9003' ) ),
874 # PR_MESSAGE_RECIPIENTS
875 RecipTable => _ATT( $atp{Byte}, pack( 'H*', '9004' ) ),
876 Attachment => _ATT( $atp{Byte}, pack( 'H*', '9005' ) ),
877 TnefVersion => _ATT( $atp{Dword}, pack( 'H*', '9006' ) ),
878 OemCodepage => _ATT( $atp{Byte}, pack( 'H*', '9007' ) ),
879 # PR_ORIG_MESSAGE_CLASS
880 OriginalMessageClass => _ATT( $atp{Word}, pack( 'H*', '0006' ) ),
881  
882 # PR_RCVD_REPRESENTING_xxx or PR_SENT_REPRESENTING_xxx
883 Owner => _ATT( $atp{Byte}, pack( 'H*', '0000' ) ),
884 # PR_SENT_REPRESENTING_xxx
885 SentFor => _ATT( $atp{Byte}, pack( 'H*', '0001' ) ),
886 # PR_RCVD_REPRESENTING_xxx
887 Delegate => _ATT( $atp{Byte}, pack( 'H*', '0002' ) ),
888 # PR_DATE_START
889 DateStart => _ATT( $atp{Date}, pack( 'H*', '0006' ) ),
890 DateEnd => _ATT( $atp{Date}, pack( 'H*', '0007' ) ), # PR_DATE_END
891 # PR_OWNER_APPT_ID
892 AidOwner => _ATT( $atp{Long}, pack( 'H*', '0008' ) ),
893 # PR_RESPONSE_REQUESTED
894 1 RequestRes => _ATT( $atp{Short}, pack( 'H*', '0009' ) ),
895 );
896  
897 # Create reverse lookup table
898 1 %att_name = reverse %att;
899  
900 # Global counter for creating file names
901 1 $g_file_cnt = 0;
902  
903 # Set some package global defaults for new objects
904 # which can be overridden for any individual object.
905 1 %dflts = (
906 debug => 0,
907 debug_max_display => 1024,
908 debug_max_line_size => 64,
909 ignore_checksum => 0,
910 display_after_err => 32,
911 output_to_core => 4096,
912 output_dir => File::Spec->curdir,
913 output_prefix => "tnef",
914 buffer_size => 1024,
915 );
916  
917 # Make a file name
918 0 ▶sub _mk_fname {
Subroutine _mk_fname: not called CC=1 SCAR=6.9
919 0 my $parms = shift;
920 File::Spec->catfile( $parms->{output_dir},
921 0 $parms->{output_prefix} . "-" . $$ . "-"
922 . ++$g_file_cnt . ".doc" );
923 }
924  
925 0 ▶sub _rtn_err {
Subroutine _rtn_err: not called CC=5 SCAR=34.0
926 0 my ( $errmsg, $fh, $parms ) = @_;
927 0 $errstr = $errmsg;
928 0 ▶ if ( $parms->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parms->{"debug"}) 0 0 0
929 0 ▶ my $read_size = $parms->{display_after_err} || 32;
Condition0%0/2
$parms->{'display_after_err'} || 32
l!l
00
MC/DC0%0/1
$parms->{'display_after_err'} || 32
$parms->{'display_after_err'}
Truth table0%0/2
A
A= $parms->{'display_after_err'}
Aresult
11
00
930 0 my $data;
931 0 read($fh, $data, $read_size );
932 0 print "Error: $errstr\n";
933 0 print "Data:\n";
934 0 ▶ print $1, "\n" while $data =~
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /([^\r\n]{0,$parms->{"debug_max_line_size"}})\r?\n?/g and do { print $1, "\n" } 0 0 0
935 /([^\r\n]{0,$parms->{debug_max_line_size}})\r?\n?/g;
936 0 print "HData:\n";
937 0 my $hdata = unpack( "H*", $data );
938 0 ▶ print $1, "\n"
Branch: 1 branch
BranchTrueFalseTotal
$hdata =~ /(.{0,$parms->{"debug_max_line_size"}})/g and do { print $1, "\n" } 0 0 0
939 while $hdata =~ /(.{0,$parms->{debug_max_line_size}})/g;
940 }
941 0 return undef;
942 }
943  
944 0 ▶sub _read_err {
Subroutine _read_err: not called CC=2 SCAR=17.9
945 0 my ( $bytes, $fh, $errmsg ) = @_;
946 0 ▶ $errstr =
Branch: 1 branch
BranchTrueFalseTotal
defined $bytes ? : 0 0 0
947 ( defined $bytes ) ? "Premature EOF" : "Read Error:" . $errmsg;
948 0 return undef;
949 }
950  
951 0 ▶sub read {
Subroutine read: not called CC=46 SCAR=76.8
952 0 ▶ croak "Usage: Matts::Message::TNEF->read(fh, parameters) "
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 or @_ == 3 0 0 0
Condition0%0/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table0%0/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
953 unless @_ == 2 or @_ == 3;
954 0 my $self = shift;
955 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
956 0 $self = {};
957 0 bless $self, $class;
958 0 my ( $fd, $parms ) = @_;
959
960 0 my %parms = %dflts;
961 0 ▶ @parms{ keys %$parms } = values %$parms if defined $parms;
Branch: 1 branch
BranchTrueFalseTotal
if defined $parms 0 0 0
962 0 $parms = \%parms;
963 0 my $debug = $parms{debug};
964 0 my $ignore_checksum = $parms{ignore_checksum};
965  
966 # Start of TNEF stream
967 0 my $data;
968 0 my $num_bytes = read($fd, $data, 4 );
969 0 ▶ return _read_err( $num_bytes, $fd, $! ) unless $num_bytes == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_bytes == 4 0 0 0
970 0 ▶ print "TNEF start: ", unpack( "H*", $data ), "\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
971 0 ▶ return _rtn_err( "Not TNEF-encapsulated", $fd, $parms )
Branch: 1 branch
BranchTrueFalseTotal
unless $data eq $Matts::Message::TNEF::TNEF_SIGNATURE 0 0 0
972 unless $data eq $TNEF_SIGNATURE;
973  
974 # Key
975 0 $num_bytes = read($fd, $data, 2 );
976 0 ▶ return _read_err( $num_bytes, $fd, $! ) unless $num_bytes == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_bytes == 2 0 0 0
977 0 ▶ print "TNEF key: ", unpack( "H*", $data ), "\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
978  
979 # Start of First Object
980 0 $num_bytes = read($fd, $data, 1 );
981 0 ▶ return _read_err( $num_bytes, $fd, $! ) unless $num_bytes == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_bytes == 1 0 0 0
982  
983 0 my $msg_att = "";
984  
985 0 my $is_msg = ( $data eq $LVL_MESSAGE );
986 0 my $is_att = ( $data eq $LVL_ATTACHMENT );
987 0 ▶ print "TNEF object start: ", unpack( "H*", $data ), "\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
988 0 ▶ return _rtn_err( "Neither a message nor an attachment", $fd,
Branch: 1 branch
BranchTrueFalseTotal
unless $is_msg or $is_att 0 0 0
Condition0%0/3
$is_msg or $is_att
l!l&&r!l&&!r
000
MC/DC0%0/2
$is_msg or $is_att
$is_msg $is_att
Truth table0%0/3
A or B
A= $is_msgB= $is_att
ABresult
1X1
011
000
989 $parms )
990 unless $is_msg or $is_att;
991  
992 0 my $msg = Matts::Message::TNEF::Data->new;
993 0 my @atts;
994  
995 # Current message or attachment in loop
996 0 my $ent = $msg;
997  
998 # Read message and attachments
999 LOOP: {
1000 0 ▶ my $type = $is_msg ? 'message' : 'attachment';
Branch: 1 branch
BranchTrueFalseTotal
$is_msg ? : 0 0 0
1001 0 ▶ print "Reading $type attribute\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
1002 0 $num_bytes = read($fd, $data, 4 );
1003 0 ▶ return _read_err( $num_bytes, $fd, $! ) unless $num_bytes == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_bytes == 4 0 0 0
1004 0 my $att_id = $data;
1005 0 my $att_name = $att_name{$att_id};
1006  
1007 0 ▶ print "TNEF $type attribute: ", unpack( "H*", $data ), "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
1008 if $debug;
1009 return _rtn_err( "Bad Attribute found in $type", $fd, $parms )
1010 0 ▶ unless $att_name{$att_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $Matts::Message::TNEF::att_name{$att_id} 0 0 0
1011 0 ▶ if ( $att_id eq $att{TnefVersion} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($att_id eq $Matts::Message::TNEF::att{'TnefVersion'}) { } 0 0 0
elsif ($att_id eq $Matts::Message::TNEF::att{'MessageClass'}) { } 0 0 0
elsif ($att_id eq $Matts::Message::TNEF::att{'AttachRenddata'}) { } 0 0 0
1012 0 ▶ return _rtn_err( "Version attribute found in attachment", $fd,
Branch: 1 branch
BranchTrueFalseTotal
if $is_att 0 0 0
1013 $parms )
1014 if $is_att;
1015 } elsif ( $att_id eq $att{MessageClass} ) {
1016 0 ▶ return _rtn_err( "MessageClass attribute found in attachment",
Branch: 1 branch
BranchTrueFalseTotal
if $is_att 0 0 0
1017 $fd, $parms )
1018 if $is_att;
1019 } elsif ( $att_id eq $att{AttachRenddata} ) {
1020 0 ▶ return _rtn_err( "AttachRenddata attribute found in message",
Branch: 1 branch
BranchTrueFalseTotal
if $is_msg 0 0 0
1021 $fd, $parms )
1022 if $is_msg;
1023 0 push @atts, ( $ent = Matts::Message::TNEF::Data->new );
1024 } else {
1025 0 ▶ return _rtn_err( "AttachRenddata must be first attribute", $fd,
Branch: 1 branch
BranchTrueFalseTotal
if $is_att and not @atts and $att_name ne "AttachRenddata" 0 0 0
Condition0%0/6
$is_att and not @atts and $att_name ne "AttachRenddata"
!ll&&!rl&&r
000
$is_att and not @atts
!ll&&!rl&&r
000
MC/DC0%0/3
$is_att and not @atts and $att_name ne "AttachRenddata"
$is_att not @atts $att_name ne "AttachRenddata"
Truth table0%0/4
A and not(B) and C
A= $is_attB= not @attsC= $att_name ne "AttachRenddata"
ABCresult
0XX0
10X0
1100
1111
1026 $parms )
1027 if $is_att
1028 and !@atts
1029 and $att_name ne "AttachRenddata";
1030 }
1031 0 ▶ print "Got attribute:$att_name{$att_id}\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
1032  
1033 0 $num_bytes = read($fd, $data, 4 );
1034 0 ▶ return _read_err( $num_bytes, $fd, $! ) unless $num_bytes == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_bytes == 4 0 0 0
1035  
1036 0 ▶ print "HLength:", unpack( "H8", $data ), "\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
1037 0 my $length = unpack( "V", $data );
1038 0 ▶ print "Length: $length\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
1039  
1040 # Get the attribute data (returns an object since data may
1041 # actually end up in a file)
1042 0 my $calc_chksum;
1043 0 ▶ $data = _build_data( $fd, $length, \$calc_chksum, $parms )
Branch: 1 branch
BranchTrueFalseTotal
$data = _build_data($fd, $length, \$calc_chksum, $parms) or return undef 0 0 0
1044 or return undef;
1045 0 ▶ _debug_print( $length, $att_id, $data, $parms ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
1046 0 $ent->datahandle( $att_name, $data, $length );
1047  
1048 0 $num_bytes = read($fd, $data, 2 );
1049 0 ▶ return _read_err( $num_bytes, $fd, $! ) unless $num_bytes == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_bytes == 2 0 0 0
1050 0 my $file_chksum = $data;
1051 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
1052 0 print "Calc Chksum:", unpack( "H*", $calc_chksum ), "\n";
1053 0 print "File Chksum:", unpack( "H*", $file_chksum ), "\n";
1054 }
1055 0 ▶ return _rtn_err( "Bad Checksum", $fd, $parms )
Branch: 1 branch
BranchTrueFalseTotal
unless $calc_chksum eq $file_chksum or $ignore_checksum 0 0 0
Condition0%0/3
$calc_chksum eq $file_chksum or $ignore_checksum
l!l&&r!l&&!r
000
MC/DC0%0/2
$calc_chksum eq $file_chksum or $ignore_checksum
$calc_chksum eq $file_chksum $ignore_checksum
Truth table0%0/3
A or B
A= $calc_chksum eq $file_chksumB= $ignore_checksum
ABresult
1X1
011
000
1056 unless $calc_chksum eq $file_chksum
1057 or $ignore_checksum;
1058  
1059 0 my $num_bytes = read($fd, $data, 1 );
1060  
1061 # EOF (0 bytes) is ok
1062 0 ▶ return _read_err( $num_bytes, $fd, $! ) unless defined $num_bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num_bytes 0 0 0
1063 0 ▶ last LOOP if $num_bytes < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $num_bytes < 1 0 0 0
1064 0 ▶ print "Next token:", unpack( "H2", $data ), "\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
1065 0 $is_msg = ( $data eq $LVL_MESSAGE );
1066 0 ▶ return _rtn_err( "Found message data in attachment", $fd, $parms )
Branch: 1 branch
BranchTrueFalseTotal
if $is_msg and $is_att 0 0 0
Condition0%0/3
$is_msg and $is_att
!ll&&!rl&&r
000
MC/DC0%0/2
$is_msg and $is_att
$is_msg $is_att
Truth table0%0/3
A and B
A= $is_msgB= $is_att
ABresult
0X0
100
111
1067 if $is_msg and $is_att;
1068 0 $is_att = ( $data eq $LVL_ATTACHMENT );
1069 0 ▶ redo LOOP if $is_msg or $is_att;
Branch: 1 branch
BranchTrueFalseTotal
if $is_msg or $is_att 0 0 0
Condition0%0/3
$is_msg or $is_att
l!l&&r!l&&!r
000
MC/DC0%0/2
$is_msg or $is_att
$is_msg $is_att
Truth table0%0/3
A or B
A= $is_msgB= $is_att
ABresult
1X1
011
000
1070 0 return _rtn_err( "Not a TNEF $type", $fd, $parms );
1071 }
1072  
1073 0 ▶ print "EOF\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
1074  
1075 0 $self->{TN_Message} = $msg;
1076 0 $self->{TN_Attachments} = \@atts;
1077 0 return $self;
1078 }
1079  
1080 0 ▶sub _debug_print {
Subroutine _debug_print: not called CC=13 SCAR=52.0
1081 0 my ( $length, $att_id, $data, $parms ) = @_;
1082 0 ▶ if ( $length < $parms->{debug_max_display} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length < $parms->{'debug_max_display'}) { } 0 0 0
1083 0 $data = $data->data;
1084 0 ▶ if ( $att_id eq $att{TnefVersion} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($att_id eq $Matts::Message::TNEF::att{'TnefVersion'}) { } 0 0 0
elsif (substr($att_id, 2) eq $Matts::Message::TNEF::atp{'Date'} and $length == 14) { } 0 0 0
elsif ($att_id eq $Matts::Message::TNEF::att{'AttachRenddata'} and $length == 14) { } 0 0 0
1085 0 $data = unpack( "L", $data );
1086 0 print "Version: $data\n";
1087 ▶ } elsif ( substr( $att_id, 2 ) eq $atp{Date} and $length == 14 ) {
Condition0%0/3
substr($att_id, 2) eq $Matts::Message::TNEF::atp{"Date"} and $length == 14
!ll&&!rl&&r
000
MC/DC0%0/2
substr($att_id, 2) eq $Matts::Message::TNEF::atp{"Date"} and $length == 14
substr($att_id, 2) eq $Matts::Message::TNEF::atp{"Date"} $length == 14
Truth table0%0/3
A and B
A= substr($att_id, 2) eq $Matts::Message::TNEF::atp{"Date"}B= $length == 14
ABresult
0X0
100
111
1088 0 my ( $yr, $mo, $day, $hr, $min, $sec, $dow ) =
1089 unpack( "vvvvvvv", $data );
1090 0 my $date = join ":", $yr, $mo, $day, $hr, $min, $sec, $dow;
1091 0 print "Date: $date\n";
1092 0 print "HDate:", unpack( "H*", $data ), "\n";
1093 ▶ } elsif ( $att_id eq $att{AttachRenddata} and $length == 14 ) {
Condition0%0/3
$att_id eq $Matts::Message::TNEF::att{"AttachRenddata"} and $length == 14
!ll&&!rl&&r
000
MC/DC0%0/2
$att_id eq $Matts::Message::TNEF::att{"AttachRenddata"} and $length == 14
$att_id eq $Matts::Message::TNEF::att{"AttachRenddata"} $length == 14
Truth table0%0/3
A and B
A= $att_id eq $Matts::Message::TNEF::att{"AttachRenddata"}B= $length == 14
ABresult
0X0
100
111
1094 0 my ( $atyp, $ulPosition, $dxWidth, $dyHeight, $dwFlags ) =
1095 unpack( "vVvvV", $data );
1096 0 $data = join ":", $atyp, $ulPosition, $dxWidth, $dyHeight,
1097 $dwFlags;
1098 0 print "AttachRendData: $data\n";
1099 } else {
1100 0 print "Data:\n";
1101 0 ▶ print $1, "\n" while $data =~
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /([^\r\n]{0,$parms->{"debug_max_line_size"}})\r?\n?/g and do { print $1, "\n" } 0 0 0
1102 /([^\r\n]{0,$parms->{debug_max_line_size}})\r?\n?/g;
1103 0 print "HData:\n";
1104 0 my $hdata = unpack( "H*", $data );
1105 0 ▶ print $1, "\n"
Branch: 1 branch
BranchTrueFalseTotal
$hdata =~ /(.{0,$parms->{"debug_max_line_size"}})/g and do { print $1, "\n" } 0 0 0
1106 while $hdata =~ /(.{0,$parms->{debug_max_line_size}})/g;
1107 }
1108 } else {
1109 0 ▶ my $io = $data->open("r")
Branch: 1 branch
BranchTrueFalseTotal
my $io = $data->open("r") or croak("Error opening attachment data handle: $!") 0 0 0
1110 or croak "Error opening attachment data handle: $!";
1111 0 my $buffer;
1112 0 CORE::read($io, $buffer, $parms->{debug_max_display} );
1113 0 ▶ close($io) or croak "Error closing attachment data handle: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $io or croak("Error closing attachment data handle: $!") 0 0 0
1114 0 print "Data:\n";
1115 0 ▶ print $1, "\n" while $buffer =~
Branch: 1 branch
BranchTrueFalseTotal
$buffer =~ /([^\r\n]{0,$parms->{"debug_max_line_size"}})\r?\n?/gs and do { print $1, "\n" } 0 0 0
1116 /([^\r\n]{0,$parms->{debug_max_line_size}})\r?\n?/sg;
1117 0 print "HData:\n";
1118 0 my $hdata = unpack( "H*", $buffer );
1119 0 ▶ print $1, "\n"
Branch: 1 branch
BranchTrueFalseTotal
$hdata =~ /(.{0,$parms->{"debug_max_line_size"}})/g and do { print $1, "\n" } 0 0 0
1120 while $hdata =~ /(.{0,$parms->{debug_max_line_size}})/g;
1121 }
1122 }
1123  
1124 0 ▶sub _build_data {
Subroutine _build_data: not called CC=5 SCAR=34.0
1125 0 my ( $fd, $length, $chksumref, $parms ) = @_;
1126  
1127 # Just borrow some other objects for the attachment attribute data
1128 0 my $body = new Matts::Message::TNEF::Body _mk_fname($parms);;
1129 0 $body->binmode(1);
1130 0 my $io = $body->open("w");
1131 0 my $bufsiz = $parms->{buffer_size};
1132 0 ▶ $bufsiz = $length if $length < $bufsiz;
Branch: 1 branch
BranchTrueFalseTotal
if $length < $bufsiz 0 0 0
1133 0 my $buffer;
1134 0 my $chksum = 0;
1135  
1136 0 ▶ while ( $length > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) 0 0 0
1137 0 my $num_bytes = CORE::read($fd, $buffer, $bufsiz );
1138 0 ▶ return _read_err( $num_bytes, $fd, $! )
Branch: 1 branch
BranchTrueFalseTotal
unless $num_bytes == $bufsiz 0 0 0
1139 unless $num_bytes == $bufsiz;
1140 0 $io->print($buffer);
1141 0 $chksum += unpack( "%16C*", $buffer );
1142 0 $chksum %= 65536;
1143 0 $length -= $bufsiz;
1144 0 ▶ $bufsiz = $length if $length < $bufsiz;
Branch: 1 branch
BranchTrueFalseTotal
if $length < $bufsiz 0 0 0
1145 }
1146 0 $$chksumref = pack( "v", $chksum );
1147 0 $io->close;
1148 0 return $body;
1149 }
1150  
1151 0 ▶sub purge {
Subroutine purge: not called CC=6 SCAR=37.4
1152 0 my $self = shift;
1153 0 my $msg = $self->{TN_Message};
1154 0 my @atts = $self->attachments;
1155 0 for ( keys %$msg ) {
1156 0 ▶ $msg->{$_}->purge if exists $att{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Matts::Message::TNEF::att{$_} 0 0 0
1157 }
1158 0 for my $attch (@atts) {
1159 0 for ( keys %$attch ) {
1160 0 ▶ $attch->{$_}->purge if exists $att{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Matts::Message::TNEF::att{$_} 0 0 0
1161 }
1162 }
1163 }
1164  
1165 0 ▶sub message {
Subroutine message: not called CC=1 SCAR=6.9
1166 0 my $self = shift;
1167 0 $self->{TN_Message};
1168 }
1169  
1170 0 ▶sub attachments {
Subroutine attachments: not called CC=2 SCAR=17.9
1171 0 my $self = shift;
1172 0 ▶ return @{ $self->{TN_Attachments} } if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
1173 0 $self->{TN_Attachments};
1174 }
1175  
1176 # This is for Messages or Attachments
1177 # since they are essentially the same thing except
1178 # for the leading attribute code
1179 package Matts::Message::TNEF::Data;
1180  
1181 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
1182 0 my $proto = shift;
1183 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
1184 0 my $self = {};
1185 0 $self->{TN_Size} = {};
1186 0 bless $self, $class;
1187 }
1188  
1189 0 ▶sub data {
Subroutine data: not called CC=3 SCAR=24.8
1190 0 my $self = shift;
1191 0 ▶ my $attr = shift || 'AttachData';
Condition0%0/2
shift() || 'AttachData'
l!l
00
MC/DC0%0/1
shift() || 'AttachData'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1192 0 ▶ return $self->{$attr} && $self->{$attr}->as_string;
Condition0%0/3
$self->{$attr} && $self->{$attr}->as_string
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{$attr} && $self->{$attr}->as_string
$self->{$attr} $self->{$attr}->as_string
Truth table0%0/3
A && B
A= $self->{$attr}B= $self->{$attr}->as_string
ABresult
0X0
100
111
1193 }
1194  
1195 0 ▶sub name {
Subroutine name: not called CC=4 SCAR=30.0
1196 0 my $self = shift;
1197 0 ▶ my $attr = shift || 'AttachTitle';
Condition0%0/2
shift() || 'AttachTitle'
l!l
00
MC/DC0%0/1
shift() || 'AttachTitle'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1198 0 ▶ my $name = $self->{$attr} && $self->{$attr}->data;
Condition0%0/3
$self->{$attr} && $self->{$attr}->data
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{$attr} && $self->{$attr}->data
$self->{$attr} $self->{$attr}->data
Truth table0%0/3
A && B
A= $self->{$attr}B= $self->{$attr}->data
ABresult
0X0
100
111
1199 0 ▶ $name =~ s/\x00+$// if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
1200 0 return $name;
1201 }
1202  
1203 # Try to get the long filename out of the
1204 # 'Attachment' attribute.
1205 0 ▶sub longname {
Subroutine longname: not called CC=5 SCAR=34.0
1206 0 my $self = shift;
1207  
1208 0 my $data = $self->data("Attachment");
1209 0 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
1210 0 my $pos = index( $data, pack( "H*", "1e00013001" ) );
1211 0 ▶ return $self->name unless $pos >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $pos >= 0 0 0 0
1212 0 my $len = unpack( "V", substr( $data, $pos + 8, 4 ) );
1213 0 my $longname = substr( $data, $pos + 12, $len );
1214 0 ▶ $longname =~ s/\x00+$// if $longname;
Branch: 1 branch
BranchTrueFalseTotal
if $longname 0 0 0
1215 0 ▶ return $longname || $self->name;
Condition0%0/3
$longname || $self->name
l!l&&r!l&&!r
000
MC/DC0%0/2
$longname || $self->name
$longname $self->name
Truth table0%0/3
A || B
A= $longnameB= $self->name
ABresult
1X1
011
000
1216 }
1217  
1218 0 ▶sub datahandle {
Subroutine datahandle: not called CC=4 SCAR=30.0
1219 0 my $self = shift;
1220 0 ▶ my $attr = shift || 'AttachData';
Condition0%0/2
shift() || 'AttachData'
l!l
00
MC/DC0%0/1
shift() || 'AttachData'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1221 0 ▶ $self->{$attr} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
1222 0 ▶ $self->size( $attr, shift ) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
1223 0 return $self->{$attr};
1224 }
1225  
1226 0 ▶sub size {
Subroutine size: not called CC=3 SCAR=24.8
1227 0 my $self = shift;
1228 0 ▶ my $attr = shift || 'AttachData';
Condition0%0/2
shift() || 'AttachData'
l!l
00
MC/DC0%0/1
shift() || 'AttachData'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1229 0 ▶ $self->{TN_Size}->{$attr} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
1230 0 return $self->{TN_Size}->{$attr};
1231 }
1232  
1233 package Matts::Message::TNEF::Body;
1234  
1235 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
1236 0 my $self = bless {}, shift;
1237 0 $self->init(@_);
1238 0 $self;
1239 }
1240  
1241 0 ▶sub as_lines {
Subroutine as_lines: not called CC=3 SCAR=24.8
1242 0 my $self = shift;
1243 0 my @lines;
1244 0 ▶ my $io = $self->open("r") || return ();
Condition0%0/2
$self->open('r') || (return ())
l!l
00
MC/DC0%0/1
$self->open('r') || (return ())
$self->open('r')
Truth table0%0/2
A
A= $self->open('r')
Aresult
11
00
1245 0 ▶ push @lines, $_ while (defined($_ = $io->getline()));
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = $io->getline) and do { push @lines, $_ } 0 0 0
1246 0 $io->close;
1247 0 @lines;
1248 }
1249  
1250 0 ▶sub as_string {
Subroutine as_string: not called CC=3 SCAR=24.8
1251 0 my $self = shift;
1252 0 my $str = '';
1253 0 my $buf = '';
1254 0 ▶ my $io = $self->open("r") || return undef;
Condition0%0/2
$self->open('r') || (return undef)
l!l
00
MC/DC0%0/1
$self->open('r') || (return undef)
$self->open('r')
Truth table0%0/2
A
A= $self->open('r')
Aresult
11
00
1255 0 my $nread = 0;
1256 0 ▶ $str .= $buf while ($nread = read($io, $buf, 2048));
Branch: 1 branch
BranchTrueFalseTotal
$nread = read($io, $buf, 2048) and do { $str .= $buf } 0 0 0
1257 0 $io->close;
1258 0 return $str;
1259 }
1260 1 *data = \&as_string; ### silenty invoke preferred usage
1261  
1262 0 ▶sub binmode {
Subroutine binmode: not called CC=2 SCAR=17.9
1263 0 my ($self, $onoff) = @_;
1264 0 ▶ $self->{MB_Binmode} = $onoff if (@_ > 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
1265 0 $self->{MB_Binmode};
1266 }
1267  
1268 0 ▶sub dup {
Subroutine dup: not called CC=1 SCAR=6.9
1269 0 my $self = shift;
1270 0 bless { %$self }, ref($self); ### shallow copy ok for ::File and ::Scalar
1271 }
1272  
1273 0 ▶sub path {
Subroutine path: not called CC=2 SCAR=17.9
1274 0 my $self = shift;
1275 0 ▶ $self->{MB_Path} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
1276 0 $self->{MB_Path};
1277 }
1278  
1279 0 ▶sub print {
Subroutine print: not called CC=3 SCAR=24.8
1280 0 my ($self, $fh) = @_;
1281 0 my $nread;
1282  
1283 ### Write it:
1284 0 my $buf = '';
1285 0 ▶ my $io = $self->open("r") || return undef;
Condition0%0/2
$self->open('r') || (return undef)
l!l
00
MC/DC0%0/1
$self->open('r') || (return undef)
$self->open('r')
Truth table0%0/2
A
A= $self->open('r')
Aresult
11
00
1286 0 ▶ $fh->print($buf) while ($nread = read($io, $buf, 2048));
Branch: 1 branch
BranchTrueFalseTotal
$nread = read($io, $buf, 2048) and do { $fh->print($buf) } 0 0 0
1287 0 $io->close;
1288 0 return defined($nread); ### how'd we do?
1289 }
1290  
1291 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
1292 0 my ($self, $path) = @_;
1293 0 $self->path($path); ### use it as-is
1294 0 $self;
1295 }
1296  
1297 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
1298  
1299 0 ▶sub open {
Subroutine open: not called CC=6 SCAR=37.4
1300 0 my ($self, $mode) = @_;
1301 0 my $IO;
1302 0 my $path = $self->path;
1303 0 ▶ if ($mode eq 'w') { ### writing
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'w') { } 0 0 0
elsif ($mode eq 'r') { } 0 0 0
1304 0 ▶ $IO = FileHandle->new(">$path") || die "write-open $path: $!";
Condition0%0/2
'FileHandle'->new(">$path") || die("write-open ${path}: $!")
l!l
00
MC/DC0%0/1
'FileHandle'->new(">$path") || die("write-open ${path}: $!")
'FileHandle'->new(">$path")
Truth table0%0/2
A
A= 'FileHandle'->new(">$path")
Aresult
11
00
1305 }
1306 elsif ($mode eq 'r') { ### reading
1307 0 ▶ $IO = FileHandle->new("<$path") || die "read-open $path: $!";
Condition0%0/2
'FileHandle'->new("<$path") || die("read-open ${path}: $!")
l!l
00
MC/DC0%0/1
'FileHandle'->new("<$path") || die("read-open ${path}: $!")
'FileHandle'->new("<$path")
Truth table0%0/2
A
A= 'FileHandle'->new("<$path")
Aresult
11
00
1308 }
1309 else {
1310 0 die "bad mode: '$mode'";
1311 }
1312 0 ▶ CORE::binmode($IO) if $self->binmode; ### set binary read/write mode?
Branch: 1 branch
BranchTrueFalseTotal
if $self->binmode 0 0 0
1313 0 return $IO;
1314 }
1315  
1316 0 ▶sub purge {
Subroutine purge: not called CC=3 SCAR=24.8
1317 0 my $self = shift;
1318 0 ▶ if (defined($self->path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->path) 0 0 0
1319 0 ▶ unlink $self->path or die("couldn't unlink ".$self->path.": $!");
Branch: 1 branch
BranchTrueFalseTotal
unlink $self->path or die q[couldn't unlink ] . $self->path . ": $!" 0 0 0
1320 0 $self->path(undef);
1321 }
1322 0 1;
1323 }
1324  
1325 1 1;
1326 __END__
↑ Summary Message.pm »