blib/lib/Mail/Message/Attachment/Stripper.pm

filter: Statementstmt 96.4% 54 / 56 Branchbran 66.6% 16 / 24 Conditioncond 64.2% 9 / 14 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 83.6% 97 / 116 CC 19 SCAR 12.9
CC
19
Cov
84%
_filename_for
5.5
_handle_part
5.4
_is_inline_text
5.2
CRAP 3.6
↑ Summary
1 package Mail::Message::Attachment::Stripper;
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  
6 2 our $VERSION = '1.01';
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Mail::Message::Attachment::Stripper - Strip the attachments from a mail
13  
14 =head1 SYNOPSIS
15  
16 my $stripper = Mail::Message::Attachment::Stripper->new($mail);
17  
18 my Mail::Message $msg = $stripper->message;
19 my @attachments = $stripper->attachments;
20  
21 =head1 DESCRIPTION
22  
23 Given a Mail::Message object, detach all attachments from the
24 message. These are then available separately.
25  
26 =head1 METHODS
27  
28 =head2 new
29  
30 my $stripper = Mail::Message::Attachment::Stripper->new($mail);
31  
32 This should be instantiated with a Mail::Message object.
33  
34 =head2 message
35  
36 my Mail::Message $msg = $stripper->message;
37  
38 This returns the message with all the attachments detached. This will
39 alter both the body and the header of the message.
40  
41 =head2 attachments
42  
43 my @attachments = $stripper->attachments;
44  
45 This returns a list of all the attachments we found in the message,
46 as a hash of { filename, content_type, payload }.
47  
48 =cut
49  
50 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
51 2 my ($class, $msg) = @_;
52 2 ▶ croak "Need a message" unless eval { $msg->isa("Mail::Message") };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $msg->isa("Mail::Message") } } 0 2 2
53 2 bless { _msg => $msg }, $class;
54 }
55  
56 2 ▶sub message {
Subroutine message: called CC=2 SCAR=7.1
Pod documented
57 2 my $self = shift;
58 2 ▶ $self->_detach_all unless exists $self->{_atts};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_atts"} 2 0 2
59 2 return $self->{_msg};
60 }
61  
62 3 ▶sub attachments {
Subroutine attachments: called CC=3 SCAR=11.4
Pod documented
63 3 my $self = shift;
64 3 ▶ $self->_detach_all unless exists $self->{_atts};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_atts"} 0 3 3
65 3 ▶ return $self->{_atts} ? @{ $self->{_atts} } : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_atts'} ? : 3 0 3
66 }
67  
68 2 ▶sub _detach_all {
Subroutine _detach_all: called CC=1 SCAR=0.0
69 2 my $self = shift;
70 2 my $mm = $self->{_msg};
71  
72 2 $self->{_atts} = [];
73 2 $self->{_body} = [];
74  
75 2 $self->_handle_part($mm);
76 2 $mm->body(Mail::Message::Body->new(data => $self->{_body}));
77 2 $self;
78 }
79  
80 2 ▶sub _handle_part {
Subroutine _handle_part: called CC=5 SCAR=16.8
81 2 my ($self, $mm) = @_;
82  
83 # According to Mail::Message docs, this ternary is not required. However,
84 # $Mail_Message->parts calls $Mail_Message->deleted which is
85 # unimplemented
86 2 ▶ foreach my $part ($mm->isMultipart ? $mm->parts : $mm) {
Branch: 1 branch
BranchTrueFalseTotal
$mm->isMultipart ? : 2 0 2
87 6 ▶ if ($self->_is_inline_text($part)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_inline_text($part)) { } 2 4 6
elsif ($self->_should_recurse($part)) { } 0 4 4
88 2 $self->_gather_body($part);
89 } elsif ($self->_should_recurse($part)) {
90 0 $self->_handle_part($part);
91 } else {
92 4 $self->_gather_att($part);
93 }
94 }
95 }
96  
97 2 ▶sub _gather_body { # Gen 25:8
Subroutine _gather_body: called CC=1 SCAR=0.0
98 2 my ($self, $part) = @_;
99 2 push @{ $self->{_body} }, $part->decoded->lines;
100 }
101  
102 4 ▶sub _gather_att {
Subroutine _gather_att: called CC=1 SCAR=0.0
103 4 my ($self, $part) = @_;
104  
105 # stringification is required for safety
106 4 push @{ $self->{_atts} },
107 {
108 content_type => $part->body->mimeType . "",
109 payload => $part->decoded . "",
110 filename => $self->_filename_for($part),
111 };
112 }
113  
114 4 ▶sub _should_recurse {
Subroutine _should_recurse: called CC=3 SCAR=11.0
115 4 my ($self, $part) = @_;
116 4 ▶ return 0 if lc($part->body->mimeType) eq "message/rfc822";
Branch: 1 branch
BranchTrueFalseTotal
if lc $part->body->mimeType eq "message/rfc822" 1 3 4
117 3 ▶ return 1 if $part->isMultipart;
Branch: 1 branch
BranchTrueFalseTotal
if $part->isMultipart 0 3 3
118 3 return 0;
119 }
120  
121 6 ▶sub _is_inline_text {
Subroutine _is_inline_text: called CC=5 SCAR=16.4
122 6 my ($self, $part) = @_;
123 6 ▶ if ($part->body->mimeType eq "text/plain") {
Branch: 1 branch
BranchTrueFalseTotal
if ($part->body->mimeType eq "text/plain") 3 3 6
124 3 my $disp = $part->head->get("Content-Disposition");
125 3 ▶ return 1 if $disp && $disp =~ /inline/;
Branch: 1 branch
BranchTrueFalseTotal
if $disp and $disp =~ /inline/ 2 1 3
Condition66%2/3
$disp and $disp =~ /inline/
!ll&&!rl&&r
012
MC/DC50%1/2
$disp and $disp =~ /inline/
$disp $disp =~ /inline/
Truth table66%2/3
A and B
A= $dispB= $disp =~ /inline/
ABresult
0X0
100
111
126 1 ▶ return 0 if $self->_filename_for($part);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_filename_for($part) 1 0 1
127 0 return 1;
128 }
129 3 return 0;
130 }
131  
132 5 ▶sub _filename_for {
Subroutine _filename_for: called CC=5 SCAR=17.0
133 5 my ($self, $part) = @_;
134 5 my $disp = $part->head->get("Content-Disposition");
135 5 my $type = $part->head->get("Content-Type");
136 5 ▶ return ($disp && $disp->attribute("filename"))
Condition63%7/11
$disp && $disp->attribute('filename') || $type && $type->attribute('name') || ''
l!l
41
$disp and $disp->attribute("filename") or $type and $type->attribute("name")
l!l&&r!l&&!r
401
$disp and $disp->attribute("filename")
!ll&&!rl&&r
014
$type and $type->attribute("name")
!ll&&!rl&&r
010
MC/DC25%1/4
$disp && $disp->attribute('filename') || $type && $type->attribute('name') || ''
$disp $disp->attribute("filename") $type $type->attribute("name")
Truth table28%2/7
A and B or C and D
A= $dispB= $disp->attribute("filename")C= $typeD= $type->attribute("name")
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
137 || ($type && $type->attribute("name"))
138 || "";
139 }
140  
141 =head1 BUGS and QUERIES
142  
143 Please direct all correspondence regarding this module to:
144 bug-Mail-Message-Attachment-Stripper@rt.cpan.org
145  
146 =head1 COPYRIGHT AND LICENSE
147  
148 Copyright (C) 2002-2005 Kasei
149  
150 This program is free software; you can redistribute it and/or modify it under
151 the terms of the GNU General Public License; either version 2 of the License,
152 or (at your option) any later version.
153  
154 This program is distributed in the hope that it will be useful, but WITHOUT
155 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
156 FOR A PARTICULAR PURPOSE.
157  
158 =cut
159  
160  
161 2 1;
↑ Summary