MboxParser/Mail/Body.pm

filter: Statementstmt 42.9% 58 / 135 Branchbran 26.9% 14 / 52 Conditioncond 30.7% 8 / 26 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 55.5% 10 / 18 Podpod 83.3% 5 / 6 totaltotal 38.3% 97 / 253 CC 42 SCAR 36.0
CC
42
Cov
38%
quotes
132.0
as_lines
42.0
extract_urls
30.0
CRAP 36.6
« MboxParser/Mail.pm ↑ Summary MboxParser/Mail/Convertable.pm »
1 # Mail::MboxParser - object-oriented access to UNIX-mailboxes
2 # Body.pm - the (textual) body of an email
3 #
4 # Copyright (C) 2001 Tassilo v. Parseval
5 # This program is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
7  
8 # Version: $Id: Body.pm,v 1.14 2002/02/21 09:06:14 parkerpine Exp $
9  
10 package Mail::MboxParser::Mail::Body;
11  
12 20 require 5.004;
13  
14 20 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 20 ▶use vars qw($VERSION @EXPORT @ISA $AUTOLOAD $_HAVE_NOT_URI_FIND);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 20 $VERSION = "0.15";
20 20 @EXPORT = qw();
21 20 @ISA = qw(Mail::MboxParser::Base Mail::MboxParser::Mail);
22  
23 20 ▶use overload '""' => sub { shift->as_string }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 20 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
26 20 eval { require URI::Find; };
27 20 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 20 0 20
28 20 $_HAVE_NOT_URI_FIND = 1;
29 }
30 }
31  
32 22 ▶sub init(@) {
Subroutine init: called CC=3 SCAR=11.2
Pod undocumented
33 22 my ($self, $ent, $bound, $conf) = @_;
34 22 $self->{CONTENT} = $ent->body;
35 22 $self->{BOUNDARY} = $bound; # the one in Content-type
36 22 $self->{ARGS} = $conf;
37  
38 22 ▶ $self->{ARGS}->{decode} ||= 'NEVER';
Condition50%1/2
$self->{"ARGS"}{"decode"} ||= "NEVER"
l!l
022
MC/DC0%0/1
$self->{"ARGS"}{"decode"} ||= "NEVER"
$self->{"ARGS"}{"decode"}
Truth table50%1/2
A
A= $self->{"ARGS"}{"decode"}
Aresult
11
00
39  
40 $self->_make_decoder($ent->head->mime_encoding)
41 22 ▶ if $self->{ARGS}->{decode} =~ /BODY|ALL/;;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ARGS"}{"decode"} =~ /BODY|ALL/ 0 22 22
42 22 $self;
43 }
44  
45 0 ▶sub _make_decoder {
Subroutine _make_decoder: not called CC=3 SCAR=24.8
46 0 my ($self, $enc) = @_;
47 0 ▶ if ($enc eq 'base64') {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc eq "base64") 0 0 0
48 0 require MIME::Base64;
49 0 ▶ return $self->{DECODER} = sub { MIME::Base64::decode_base64(shift) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 }
51 0 ▶ if ($enc eq 'quoted-printable') {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc eq "quoted-printable") 0 0 0
52 0 require MIME::QuotedPrint;
53 0 ▶ return $self->{DECODER} = sub { MIME::QuotedPrint::decode_qp(shift) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 }
55 0 ▶ $self->{DECODER} = sub { $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 }
57  
58 4 ▶sub as_string {
Subroutine as_string: called CC=4 SCAR=16.3
Pod documented
59 4 my ($self, %args) = @_;
60 4 $self->reset_last;
61 4 ▶ return join "", $self->as_lines(strip_sig => 1) if $args{strip_sig};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"strip_sig"} 0 4 4
62 4 my $decode = $self->{ARGS}->{decode};
63 4 ▶ if ($decode eq 'BODY' || $decode eq 'ALL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($decode eq "BODY" or $decode eq "ALL") 0 4 4
Condition33%1/3
$decode eq "BODY" or $decode eq "ALL"
l!l&&r!l&&!r
004
MC/DC0%0/2
$decode eq "BODY" or $decode eq "ALL"
$decode eq "BODY" $decode eq "ALL"
Truth table33%1/3
A or B
A= $decode eq "BODY"B= $decode eq "ALL"
ABresult
1X1
011
000
64 0 return join "", map { $self->{DECODER}->($_) } @{$self->{CONTENT}};
65 }
66 4 return join "", @{$self->{CONTENT}};
67 }
68
69  
70 0 ▶sub as_lines() {
Subroutine as_lines: not called CC=6 SCAR=37.4
Pod documented
71 0 my ($self, %args) = @_;
72 0 $self->reset_last;
73 0 my $decode = $self->{ARGS}->{decode};
74 0 ▶ if ($decode eq 'BODY' || $decode eq 'ALL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($decode eq "BODY" or $decode eq "ALL") 0 0 0
Condition0%0/3
$decode eq "BODY" or $decode eq "ALL"
l!l&&r!l&&!r
000
MC/DC0%0/2
$decode eq "BODY" or $decode eq "ALL"
$decode eq "BODY" $decode eq "ALL"
Truth table0%0/3
A or B
A= $decode eq "BODY"B= $decode eq "ALL"
ABresult
1X1
011
000
75 0 return map { $self->{DECODER}->($_) } @{$self->{CONTENT}};
76 }
77  
78 0 ▶ return @{$self->{CONTENT}} if ! $args{strip_sig};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"strip_sig"} 0 0 0
79  
80 0 my @lines;
81 0 for (@{ $self->{CONTENT} }) {
82 0 ▶ last if /^--\040?[\r\n]?$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^--\040?[\r\n]?$/ 0 0 0
83 0 push @lines, $_;
84 }
85 0 return @lines;
86 }
87
88
89 18 ▶sub signature() {
Subroutine signature: called CC=13 SCAR=26.7
Pod documented
90 18 my $self = shift;
91 18 $self->reset_last;
92 18 my $decode = $self->{ARGS}->{decode};
93 18 my $bound = $self->{BOUNDARY};
94  
95 18 my @signature;
96 18 my $seperator = 0;
97 18 for (@{$self->{CONTENT}}) {
98  
99 # we are still outside the signature
100 560 ▶ if (! /^--\040?[\r\n]?$/ && not $seperator) {
Branch: 2 branches
BranchTrueFalseTotal
if (not /^--\040?[\r\n]?$/ || $seperator) { } 532 28 560
elsif (not $seperator) { } 6 22 28
Condition100%3/3
/^--\040?[\r\n]?$/ || $seperator
l!l&&r!l&&!r
622532
MC/DC100%2/2
/^--\040?[\r\n]?$/ || $seperator
/^--\040?[\r\n]?$/ $seperator
Truth table100%3/3
A || B
A= /^--\040?[\r\n]?$/B= $seperator
ABresult
1X1
011
000
101 532 next;
102 }
103  
104 # we hit the signature delimiter (--)
105 6 elsif (not $seperator) { $seperator = 1; next }
106  
107 22 chomp;
108  
109 # we are inside signature: is line perhaps MIME-boundary?
110 22 ▶ last if $bound && /^--\Q$bound\E/ && $seperator;
Branch: 1 branch
BranchTrueFalseTotal
if $bound and /^--\Q$bound\E/ and $seperator 0 22 22
Condition33%2/6
$bound and /^--\Q$bound\E/ and $seperator
!ll&&!rl&&r
2200
$bound and /^--\Q$bound\E/
!ll&&!rl&&r
2200
MC/DC0%0/3
$bound and /^--\Q$bound\E/ and $seperator
$bound /^--\Q$bound\E/ $seperator
Truth table25%1/4
A and B and C
A= $boundB= /^--\Q$bound\E/C= $seperator
ABCresult
0XX0
10X0
1100
1111
111  
112 # none of the above => signature line
113 22 push @signature, $_;
114 }
115
116 18 ▶ $self->{LAST_ERR} = "No signature found" if !@signature;
Branch: 1 branch
BranchTrueFalseTotal
unless @signature 12 6 18
117 18 ▶ if ($decode eq 'BODY' || $decode eq 'ALL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($decode eq "BODY" or $decode eq "ALL") 0 18 18
Condition33%1/3
$decode eq "BODY" or $decode eq "ALL"
l!l&&r!l&&!r
0018
MC/DC0%0/2
$decode eq "BODY" or $decode eq "ALL"
$decode eq "BODY" $decode eq "ALL"
Truth table33%1/3
A or B
A= $decode eq "BODY"B= $decode eq "ALL"
ABresult
1X1
011
000
118 0 $_ = $self->{DECODER}->($_) for @signature;
119 }
120 18 ▶ return @signature if $seperator;
Branch: 1 branch
BranchTrueFalseTotal
if $seperator 6 12 18
121 12 return ();
122 }
123  
124 0 ▶sub extract_urls(@) {
Subroutine extract_urls: not called CC=5 SCAR=34.0
Pod documented
125 0 my ($self, %args) = @_;
126 0 $self->reset_last;
127  
128 0 ▶ $args{unique} = 0 if not exists $args{unique};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"unique"} 0 0 0
129  
130 0 ▶ if ($_HAVE_NOT_URI_FIND) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::MboxParser::Mail::Body::_HAVE_NOT_URI_FIND) { } 0 0 0
131 0 carp <<EOW;
132 You need the URI::Find module in order to use extract_urls.
133 EOW
134 0 return;
135 }
136 else {
137 0 my @uris; my %seen;
138  
139 0 for my $line (@{$self->{CONTENT}}) {
140 0 chomp $line;
141 0 ▶ URI::Find::find_uris($line, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
142 0 my (undef, $url) = @_;
143 0 $line =~ s/^\s+|\s+$//;
144 0 ▶ if (not $seen{$url}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$url}) 0 0 0
145 0 push @uris, { url => $url, context => $line };
146 }
147 0 ▶ $seen{$url}++ if $args{unique};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"unique"} 0 0 0
148 }
149 0 );
150 }
151 0 ▶ $self->{LAST_ERR} = "No URLs found" if @uris == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @uris == 0 0 0 0
152  
153 0 return @uris;
154 }
155 }
156  
157 0 ▶sub quotes() {
Subroutine quotes: not called CC=11 SCAR=48.8
Pod documented
158 0 my $self = shift;
159 0 my $decode = $self->{ARGS}->{decode};
160 0 $self->reset_last;
161  
162 0 my %ret;
163 0 my $q = 0; # num of '>'
164 0 my $in = 0; # being inside a quote
165 0 my $last = 0; # num of quotes in last line
166  
167 0 for (@{$self->{CONTENT}}) {
168  
169 0 ▶ if ($decode eq 'ALL' || $decode eq 'BODY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($decode eq "ALL" or $decode eq "BODY") 0 0 0
Condition0%0/3
$decode eq "ALL" or $decode eq "BODY"
l!l&&r!l&&!r
000
MC/DC0%0/2
$decode eq "ALL" or $decode eq "BODY"
$decode eq "ALL" $decode eq "BODY"
Truth table0%0/3
A or B
A= $decode eq "ALL"B= $decode eq "BODY"
ABresult
1X1
011
000
170 0 $_ = $self->{DECODER}->($_);
171 }
172
173 # count quotation signs
174 0 $q = 0;
175 0 my $t = "a" x length;
176 0 for my $c (unpack $t, $_) {
177 0 ▶ if ($c eq '>') { $q++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ">") 0 0 0
178 0 ▶ if ($c ne '>' && $c ne ' ') { last }
Branch: 1 branch
BranchTrueFalseTotal
if ($c ne ">" and $c ne " ") 0 0 0
Condition0%0/3
$c ne ">" and $c ne " "
!ll&&!rl&&r
000
MC/DC0%0/2
$c ne ">" and $c ne " "
$c ne ">" $c ne " "
Truth table0%0/3
A and B
A= $c ne ">"B= $c ne " "
ABresult
0X0
100
111
179 }
180  
181 # first: create a hash-element for level $q
182 0 ▶ if (! exists $ret{$q}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ret{$q}) 0 0 0
183 0 $ret{$q} = [];
184 }
185  
186 # if last line had the same level as current one:
187 # attach the line to the last one
188 0 ▶ if ($last == $q) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last == $q) { } 0 0 0
189 0 ▶ if (@{$ret{$q}} == 0) { $ret{$q}->[$q] .= $_ }
Branch: 1 branch
BranchTrueFalseTotal
if (@{$ret{$q};} == 0) { } 0 0 0
190 0 else { $ret{$q}->[-1] .= $_ }
191 }
192 # if not:
193 # create a new array-element in the appropriate hash-element
194 else {
195 0 push @{$ret{$q}}, $_;
196 }
197 0 $last = $q;
198 }
199 0 return \%ret;
200 }
201  
202  
203 20 1;
204  
205 __END__
« MboxParser/Mail.pm ↑ Summary MboxParser/Mail/Convertable.pm »