Message/Construct/Reply.pm

filter: Statementstmt 92.1% 94 / 102 Branchbran 63.3% 57 / 90 Conditioncond 47.7% 32 / 67 MC/DCmcdc 17.5% 7 / 40 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 65.0% 203 / 312 CC 74 SCAR 47.5
CC
74
Cov
71%
reply
173.7
replyPrelude
15.9
replySubject
14.1
CRAP 115.1
« Message/Construct/Build.pm ↑ Summary Box/Parser/Perl.pm »
1 # This code is part of Perl distribution Mail-Message version 4.05.
2 # The POD got stripped from this file by OODoc version 3.06.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2001-2026 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package Mail::Message;{
13 3 our $VERSION = '4.05';
14 }
15  
16  
17 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use Log::Report 'mail-message', import => [ qw/__x error info trace/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 ▶use Mail::Message::Body::Multipart ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Mail::Address ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 3 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #--------------------
28  
29 # tests in t/55reply1r.t, demo in the examples/ directory
30  
31 sub reply(@)
32 3 ▶{ my ($self, %args) = @_;
Subroutine reply: called CC=53 SCAR=51.6
Pod documented
33  
34 3 my $body = $args{body};
35 3 ▶ my $strip = !exists $args{strip_signature} || $args{strip_signature};
Condition66%2/3
!exists $args{'strip_signature'} || $args{'strip_signature'}
l!l&&r!l&&!r
201
MC/DC50%1/2
!exists $args{'strip_signature'} || $args{'strip_signature'}
!exists $args{'strip_signature'} $args{'strip_signature'}
Truth table66%2/3
not(A) || B
A= !exists $args{'strip_signature'}B= $args{'strip_signature'}
ABresult
1X1
011
000
36 3 ▶ my $include = $args{include} || 'INLINE';
Condition50%1/2
$args{'include'} || 'INLINE'
l!l
03
MC/DC0%0/1
$args{'include'} || 'INLINE'
$args{'include'}
Truth table50%1/2
A
A= $args{'include'}
Aresult
11
00
37  
38 3 ▶ if($include eq 'NO')
Branch: 2 branches
BranchTrueFalseTotal
if ($include eq 'NO') { } 0 3 3
elsif ($include eq 'INLINE' or $include eq 'ATTACH') { } 3 0 3
39 { # Throw away real body.
40 0 ▶ $body //= Mail::Message::Body->new(data => ["\n[The original message is not included]\n\n"]);
Condition0%0/3
$body //= "Mail::Message::Body"->new("data", ["\n[The original message is not included]\n\n"])
l!l&&r!l&&!r
000
MC/DC0%0/2
$body //= "Mail::Message::Body"->new("data", ["\n[The original message is not included]\n\n"])
$body "Mail::Message::Body"->new("data", ["\n[The original message is not included]\n\n"])
Truth table0%0/3
A //= B
A= $bodyB= "Mail::Message::Body"->new("data", ["\n[The original message is not included]\n\n"])
ABresult
1X1
011
000
41 }
42 ▶ elsif($include eq 'INLINE' || $include eq 'ATTACH')
Condition33%1/3
$include eq "INLINE" or $include eq "ATTACH"
l!l&&r!l&&!r
300
MC/DC0%0/2
$include eq "INLINE" or $include eq "ATTACH"
$include eq "INLINE" $include eq "ATTACH"
Truth table33%1/3
A or B
A= $include eq "INLINE"B= $include eq "ATTACH"
ABresult
1X1
011
000
43 {
44 3 ▶ unless(defined $body)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 3 0 3
45 { # text attachment
46 3 $body = $self->body;
47 3 ▶ $body = $body->part(0)->body if $body->isMultipart && $body->parts==1;
Branch: 1 branch
BranchTrueFalseTotal
if $body->isMultipart and $body->parts == 1 0 3 3
Condition33%1/3
$body->isMultipart and $body->parts == 1
!ll&&!rl&&r
300
MC/DC0%0/2
$body->isMultipart and $body->parts == 1
$body->isMultipart $body->parts == 1
Truth table33%1/3
A and B
A= $body->isMultipartB= $body->parts == 1
ABresult
0X0
100
111
48 3 ▶ $body = $body->nested->body if $body->isNested;
Branch: 1 branch
BranchTrueFalseTotal
if $body->isNested 0 3 3
49  
50 $body
51 = $strip && ! $body->isMultipart && !$body->isBinary
52 ? $body->decoded->stripSignature(pattern => $args{strip_signature}, max_lines => $args{max_signature})
53 3 ▶ : $body->decoded;
Branch: 1 branch
BranchTrueFalseTotal
$strip && !$body->isMultipart && !$body->isBinary ? : 2 1 3
Condition66%4/6
$strip and not $body->isMultipart and not $body->isBinary
!ll&&!rl&&r
102
$strip and not $body->isMultipart
!ll&&!rl&&r
102
MC/DC33%1/3
$strip and not $body->isMultipart and not $body->isBinary
$strip not $body->isMultipart not $body->isBinary
Truth table50%2/4
A and not(B) and not(C)
A= $stripB= not $body->isMultipartC= not $body->isBinary
ABCresult
0XX0
10X0
1100
1111
54 }
55  
56 0 ▶ if($include eq 'INLINE' && $body->isMultipart) { $include = 'ATTACH' }
Branch: 2 branches
BranchTrueFalseTotal
if ($include eq 'INLINE' and $body->isMultipart) { } 0 3 3
elsif ($include eq 'INLINE' and $body->isBinary) { } 0 3 3
Condition33%1/3
$include eq "INLINE" and $body->isMultipart
!ll&&!rl&&r
030
MC/DC0%0/2
$include eq "INLINE" and $body->isMultipart
$include eq "INLINE" $body->isMultipart
Truth table33%1/3
A and B
A= $include eq "INLINE"B= $body->isMultipart
ABresult
0X0
100
111
57 ▶ elsif($include eq 'INLINE' && $body->isBinary)
Condition33%1/3
$include eq "INLINE" and $body->isBinary
!ll&&!rl&&r
030
MC/DC0%0/2
$include eq "INLINE" and $body->isBinary
$include eq "INLINE" $body->isBinary
Truth table33%1/3
A and B
A= $include eq "INLINE"B= $body->isBinary
ABresult
0X0
100
111
58 0 { $include = 'ATTACH';
59 0 $body = Mail::Message::Body::Multipart->new(parts => [$body]);
60 }
61  
62 3 ▶ if($include eq 'INLINE')
Branch: 1 branch
BranchTrueFalseTotal
if ($include eq "INLINE") 3 0 3
63 3 ▶ { my $quote = defined $args{quote} ? $args{quote} : exists $args{quote} ? undef : '> ';
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'quote'} ? : 2 1 3
exists $args{'quote'} ? : 1 0 1
64 3 ▶ if(defined $quote)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $quote) 2 1 3
65 2 ▶ { my $quoting = ref $quote ? $quote : sub { $quote . $_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
ref $quote ? : 1 1 2
66 2 $body = $body->foreachLine($quoting);
67 }
68 }
69 }
70 else
71 0 { error __x"cannot include reply source as {kind}.", kind => $include;
72 }
73  
74 #
75 # Collect header info
76 #
77  
78 3 my $mainhead = $self->toplevel->head;
79  
80 # Where it comes from
81 3 my $from = delete $args{From};
82 3 ▶ unless(defined $from)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $from) 3 0 3
83 3 { my @from = $self->to;
84 3 ▶ $from = \@from if @from;
Branch: 1 branch
BranchTrueFalseTotal
if @from 3 0 3
85 }
86  
87 # To whom to send
88 3 ▶ my $to = delete $args{To} || $mainhead->get('reply-to') || $mainhead->get('from');
Condition33%2/6
delete $args{'To'} || $mainhead->get('reply-to') || $mainhead->get('from')
l!l&&r!l&&!r
030
delete $args{"To"} or $mainhead->get("reply-to")
l!l&&r!l&&!r
003
MC/DC0%0/3
delete $args{'To'} || $mainhead->get('reply-to') || $mainhead->get('from')
delete $args{"To"} $mainhead->get("reply-to") $mainhead->get('from')
Truth table25%1/4
A or B || C
A= delete $args{"To"}B= $mainhead->get("reply-to")C= $mainhead->get('from')
ABCresult
1XX1
01X1
0011
0000
89 3 ▶ defined $to or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $to or return 3 0 3
90  
91 # Add Cc
92 3 my $cc = delete $args{Cc};
93 3 ▶ if(!defined $cc && $args{group_reply})
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cc and $args{"group_reply"}) 1 2 3
Condition66%2/3
not defined $cc and $args{"group_reply"}
!ll&&!rl&&r
021
MC/DC50%1/2
not defined $cc and $args{"group_reply"}
not defined $cc $args{"group_reply"}
Truth table66%2/3
not(A) and B
A= not defined $ccB= $args{"group_reply"}
ABresult
0X0
100
111
94 1 { my @cc = $self->cc;
95 1 ▶ $cc = [ $self->cc ] if @cc;
Branch: 1 branch
BranchTrueFalseTotal
if @cc 1 0 1
96 }
97  
98 # Create a subject
99 3 my $srcsub = delete $args{Subject};
100 3 ▶ my $subject
Branch: 2 branches
BranchTrueFalseTotal
!defined($srcsub) ? : 3 0 3
ref $srcsub ? : 0 0 0
101 = ! defined $srcsub ? $self->replySubject($self->subject)
102 : ref $srcsub ? $srcsub->($self->subject)
103 : $srcsub;
104  
105 # Create a nice message-id
106 3 my $msgid = delete $args{'Message-ID'};
107 3 ▶ $msgid = "<$msgid>" if $msgid && $msgid !~ /^\s*\<.*\>\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $msgid and not $msgid =~ /^\s*\<.*\>\s*$/ 0 3 3
Condition33%1/3
$msgid and not $msgid =~ /^\s*\<.*\>\s*$/
!ll&&!rl&&r
300
MC/DC0%0/2
$msgid and not $msgid =~ /^\s*\<.*\>\s*$/
$msgid not $msgid =~ /^\s*\<.*\>\s*$/
Truth table33%1/3
A and not(B)
A= $msgidB= not $msgid =~ /^\s*\<.*\>\s*$/
ABresult
0X0
100
111
108  
109 # Thread information
110 3 my $origid = '<'.$self->messageId.'>';
111 3 my $refs = $mainhead->get('references');
112  
113 # Prelude
114 my $prelude
115 = defined $args{prelude} ? $args{prelude}
116 : exists $args{prelude} ? undef
117 3 ▶ : [ $self->replyPrelude($to) ];
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'prelude'} ? : 0 3 3
exists $args{'prelude'} ? : 1 2 3
118  
119 3 ▶ $prelude = Mail::Message::Body->new(data => $prelude)
Branch: 1 branch
BranchTrueFalseTotal
if defined $prelude and not builtin::blessed($prelude) 2 1 3
Condition66%2/3
defined $prelude and not builtin::blessed($prelude)
!ll&&!rl&&r
102
MC/DC50%1/2
defined $prelude and not builtin::blessed($prelude)
defined $prelude not builtin::blessed($prelude)
Truth table66%2/3
A and not(B)
A= defined $preludeB= not builtin::blessed($prelude)
ABresult
0X0
100
111
120 if defined $prelude && ! blessed $prelude;
121  
122 3 my $postlude = $args{postlude};
123 3 ▶ $postlude = Mail::Message::Body->new(data => $postlude)
Branch: 1 branch
BranchTrueFalseTotal
if defined $postlude and not builtin::blessed($postlude) 0 3 3
Condition66%2/3
defined $postlude and not builtin::blessed($postlude)
!ll&&!rl&&r
120
MC/DC0%0/2
defined $postlude and not builtin::blessed($postlude)
defined $postlude not builtin::blessed($postlude)
Truth table66%2/3
A and not(B)
A= defined $postludeB= not builtin::blessed($postlude)
ABresult
0X0
100
111
124 if defined $postlude && ! blessed $postlude;
125  
126 #
127 # Create the message.
128 #
129  
130 3 my $total;
131 0 ▶ if($include eq 'NO') {$total = $body}
Branch: 3 branches
BranchTrueFalseTotal
if ($include eq 'NO') { } 0 3 3
elsif ($include eq 'INLINE') { } 3 0 3
elsif ($include eq 'ATTACH') { } 0 0 0
132 elsif($include eq 'INLINE')
133 3 { my $signature = $args{signature};
134 3 ▶ $signature = $signature->body
Branch: 1 branch
BranchTrueFalseTotal
if defined $signature and $signature->isa("Mail::Message") 0 3 3
Condition33%1/3
defined $signature and $signature->isa("Mail::Message")
!ll&&!rl&&r
300
MC/DC0%0/2
defined $signature and $signature->isa("Mail::Message")
defined $signature $signature->isa("Mail::Message")
Truth table33%1/3
A and B
A= defined $signatureB= $signature->isa("Mail::Message")
ABresult
0X0
100
111
135 if defined $signature && $signature->isa('Mail::Message');
136  
137 3 ▶ $total = $body->concatenate($prelude, $body, $postlude, (defined $signature ? "-- \n$signature" : undef));
Branch: 1 branch
BranchTrueFalseTotal
defined $signature ? : 0 3 3
138 }
139 elsif($include eq 'ATTACH')
140 0 { my $intro = $prelude->concatenate($prelude, ["\n", "[Your message is attached]\n"], $postlude);
141 0 $total = Mail::Message::Body::Multipart->new(parts => [ $intro, $body, $args{signature} ]);
142 }
143  
144 3 ▶ my $msgtype = $args{message_type} || 'Mail::Message';
Condition50%1/2
$args{'message_type'} || 'Mail::Message'
l!l
03
MC/DC0%0/1
$args{'message_type'} || 'Mail::Message'
$args{'message_type'}
Truth table50%1/2
A
A= $args{'message_type'}
Aresult
11
00
145 3 ▶ my $reply = $msgtype->buildFromBody(
Branch: 1 branch
BranchTrueFalseTotal
$refs ? : 0 3 3
Condition50%1/2
$from || 'Undisclosed senders:;'
l!l
30
MC/DC0%0/1
$from || 'Undisclosed senders:;'
$from
Truth table50%1/2
A
A= $from
Aresult
11
00
146 $total,
147 From => $from || 'Undisclosed senders:;',
148 To => $to,
149 Subject => $subject,
150 'In-Reply-To' => $origid,
151 References => ($refs ? "$refs $origid" : $origid),
152 );
153  
154 3 my $newhead = $reply->head;
155 3 ▶ $newhead->set(Cc => $cc) if $cc;
Branch: 1 branch
BranchTrueFalseTotal
if $cc 1 2 3
156 3 ▶ $newhead->set(Bcc => delete $args{Bcc}) if $args{Bcc};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"Bcc"} 1 2 3
157 $newhead->add($_ => $args{$_})
158 3 for sort grep /^[A-Z]/, keys %args;
159  
160 # Ready
161 3 trace "Reply created from $origid";
162 3 $self->label(replied => 1);
163 3 $reply;
164 }
165  
166  
167 # tests in t/35reply1rs.t
168  
169 sub replySubject($)
170 24 ▶{ my ($thing, $subject) = @_;
Subroutine replySubject: called CC=14 SCAR=26.5
Pod documented
171 24 ▶ $subject = 'your mail' unless defined $subject && length $subject;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subject and length $subject 2 22 24
Condition100%3/3
defined $subject and length $subject
!ll&&!rl&&r
1122
MC/DC100%2/2
defined $subject and length $subject
defined $subject length $subject
Truth table100%3/3
A and B
A= defined $subjectB= length $subject
ABresult
0X0
100
111
172 24 my @subject = split /\:/, $subject;
173 24 my $re_count = 1;
174  
175 # Strip multiple Re's from the start.
176  
177 24 ▶ while(@subject)
Branch: 1 branch
BranchTrueFalseTotal
if (@subject) 38 0 38
178 38 ▶ { last if $subject[0] =~ /[A-QS-Za-qs-z][A-DF-Za-df-z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $subject[0] =~ /[A-QS-Za-qs-z][A-DF-Za-df-z]/ 24 14 38
179  
180 14 for(shift @subject)
181 14 ▶ { while( /\bRe(?:\[\s*(\d+)\s*\]|\b)/g )
Branch: 1 branch
BranchTrueFalseTotal
if (/\bRe(?:\[\s*(\d+)\s*\]|\b)/g) 17 14 31
182 17 ▶ { $re_count += ($1 // 1);
Condition100%2/2
$1 // 1
l!l
413
MC/DC100%1/1
$1 // 1
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
183 }
184 }
185 }
186  
187 # Strip multiple Re's from the end.
188  
189 24 ▶ if(@subject)
Branch: 1 branch
BranchTrueFalseTotal
if (@subject) 24 0 24
190 24 { for($subject[-1])
191 24 ▶ { $re_count++ while s/\s*\(\s*(re|forw)\W*\)\s*$//i;
Branch: 1 branch
BranchTrueFalseTotal
s/\s*\(\s*(re|forw)\W*\)\s*$//i and do { ++$re_count } 9 24 33
192 }
193 }
194  
195 # Create the new subject string.
196  
197 24 ▶ my $text = (join ':', @subject) || 'your mail';
Condition50%1/2
join(':', @subject) || 'your mail'
l!l
240
MC/DC0%0/1
join(':', @subject) || 'your mail'
join(':', @subject)
Truth table50%1/2
A
A= join(':', @subject)
Aresult
11
00
198 24 s/^\s+//, s/\s+$// for $text;
199  
200 24 ▶ $re_count==1 ? "Re: $text" : "Re[$re_count]: $text";
Branch: 1 branch
BranchTrueFalseTotal
$re_count == 1 ? : 8 16 24
201 }
202  
203  
204  
205 sub replyPrelude($)
206 2 ▶{ my ($self, $who) = @_;
Subroutine replyPrelude: called CC=9 SCAR=27.7
Pod documented
207 2 ▶ $who = $who->[0] if ref $who eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $who eq "ARRAY" 0 2 2
208  
209 2 ▶ my $user
Branch: 3 branches
BranchTrueFalseTotal
!defined($who) ? : 0 2 2
!builtin::blessed($who) ? : 0 2 2
$who->isa('Mail::Message::Field') ? : 2 0 2
210 = ! defined $who ? undef
211 : ! blessed $who ? (Mail::Address->parse($who))[0]
212 : $who->isa('Mail::Message::Field') ? ($who->addresses)[0]
213 : $who;
214  
215 2 ▶ my $from = blessed $user && $user->isa('Mail::Address') ? ($user->name || $user->address || $user->format) : 'someone';
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($user) && $user->isa('Mail::Address') ? : 2 0 2
Condition33%3/9
builtin::blessed($user) and $user->isa("Mail::Address")
!ll&&!rl&&r
002
$user->name or $user->address or $user->format
l!l&&r!l&&!r
200
$user->name or $user->address
l!l&&r!l&&!r
200
MC/DC0%0/2
builtin::blessed($user) and $user->isa("Mail::Address")
builtin::blessed($user) $user->isa("Mail::Address")
MC/DC0%0/3
$user->name or $user->address or $user->format
$user->name $user->address $user->format
Truth table33%1/3
A and B
A= builtin::blessed($user)B= $user->isa("Mail::Address")
ABresult
0X0
100
111
Truth table25%1/4
A or B or C
A= $user->nameB= $user->addressC= $user->format
ABCresult
1XX1
01X1
0011
0000
216 2 my $time = gmtime $self->timestamp;
217 2 "On $time, $from wrote:\n";
218 }
219  
220 3 1;
« Message/Construct/Build.pm ↑ Summary Box/Parser/Perl.pm »