blib/lib/Email/Reply.pm

filter: Statementstmt 100% 108 / 108 Branchbran 75.0% 36 / 48 Conditioncond 56.6% 17 / 30 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 100% 16 / 16 Podpod 100% 1 / 1 totaltotal 83.1% 183 / 220 CC 38 SCAR 21.5
CC
38
Cov
87%
_new
14.9
_make_headers
10.1
_quote_body
9.8
CRAP 8.6
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Email::Reply;
4 # ABSTRACT: reply to an email message
5 1 $Email::Reply::VERSION = '1.204';
6 1 ▶use Email::Abstract 2.01;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Email::Address 1.80;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Email::MIME 1.82;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 my $CLASS = __PACKAGE__;
12 1 our @EXPORT = qw[reply];
13 1 my $CRLF = "\x0d\x0a";
14  
15 # Want to subclass and still use the functional interface?
16 # That's cool, just add these lines to your package:
17 # use base qw[Exporter];
18 # use vars qw[@EXPORT $CLASS];
19 # @EXPORT = qw[reply];
20 # $CLASS = __PACKAGE__;
21 # *reply = \&Email::Reply::reply;
22  
23 3 ▶sub reply {
Subroutine reply: called CC=6 SCAR=18.7
Pod documented
24 3 my $reply = $CLASS->_new(@_);
25 3 $reply->_make_headers;
26 3 ▶ $reply->_encapsulate_message if $reply->{attach};
Branch: 1 branch
BranchTrueFalseTotal
if $reply->{"attach"} 1 2 3
27 $reply->_quote_body($reply->{original})
28 3 ▶ if $reply->{quote} || $reply->{top_post};
Branch: 1 branch
BranchTrueFalseTotal
if $reply->{"quote"} or $reply->{"top_post"} 3 0 3
Condition33%1/3
$reply->{"quote"} or $reply->{"top_post"}
l!l&&r!l&&!r
300
MC/DC0%0/2
$reply->{"quote"} or $reply->{"top_post"}
$reply->{"quote"} $reply->{"top_post"}
Truth table33%1/3
A or B
A= $reply->{"quote"}B= $reply->{"top_post"}
ABresult
1X1
011
000
29 3 ▶ $reply->_post_reply if $reply->{body};
Branch: 1 branch
BranchTrueFalseTotal
if $reply->{"body"} 3 0 3
30 3 ▶ return $reply->{message} ? $reply->_mime : $reply->_simple;
Branch: 1 branch
BranchTrueFalseTotal
$reply->{'message'} ? : 1 2 3
31 }
32  
33 3 ▶sub _new {
Subroutine _new: called CC=14 SCAR=27.0
34 3 my ($class, %args) = @_;
35 3 my $self = {};
36 3 $self->{original} = Email::MIME->new(Email::Abstract->as_string($args{to}));
37  
38 ($self->{from}) =
39 3 ▶ Email::Address->parse($args{from} || $self->{original}->header('To'));
Condition66%2/3
$args{'from'} || $self->{'original'}->header('To')
l!l&&r!l&&!r
120
MC/DC0%0/2
$args{'from'} || $self->{'original'}->header('To')
$args{'from'} $self->{'original'}->header('To')
Truth table66%2/3
A || B
A= $args{'from'}B= $self->{'original'}->header('To')
ABresult
1X1
011
000
40  
41 # There are three headers which may give the 'to' address.
42 3 my $addr_to_parse;
43 3 my @headers = qw(Reply-To From Return-Path);
44 3 my $orig = $self->{original};
45 3 foreach (@headers) {
46 6 my $v = $orig->header($_);
47 6 ▶ if (defined $v and $v ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and $v ne "") 3 3 6
Condition66%2/3
defined $v and $v ne ""
!ll&&!rl&&r
303
MC/DC50%1/2
defined $v and $v ne ""
defined $v $v ne ""
Truth table66%2/3
A and B
A= defined $vB= $v ne ""
ABresult
0X0
100
111
48 3 $addr_to_parse = $v;
49 3 last;
50 }
51 }
52 3 ▶ die "did not find any of the headers: @headers" if not defined $addr_to_parse;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr_to_parse 0 3 3
53  
54 # Parse it and check it succeeded.
55 3 my (@parsed) = Email::Address->parse($addr_to_parse);
56 3 ▶ foreach (@parsed) { die if not defined }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 3 3
57 3 ▶ die "failed to parse address '$addr_to_parse'" if not @parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless @parsed 0 3 3
58 3 ▶ die "strange, '$addr_to_parse' parses to more than one address: @parsed" if @parsed != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parsed != 1 0 3 3
59 3 $self->{to} = $parsed[0];
60  
61 $self->{attrib} = $args{attrib}
62 3 ▶ || (($self->{to}->name || join($self->{to}->address, '<', '>')) . ' wrote:');
Condition60%3/5
$args{'attrib'} || ($self->{'to'}->name || join($self->{'to'}->address, '<', '>')) . ' wrote:'
l!l
12
$self->{'to'}->name || join($self->{'to'}->address, '<', '>')
l!l&&r!l&&!r
200
MC/DC100%1/1
$args{'attrib'} || ($self->{'to'}->name || join($self->{'to'}->address, '<', '>')) . ' wrote:'
$args{'attrib'}
Truth table100%2/2
A
A= $args{'attrib'}
Aresult
11
00
63  
64 3 ▶ $self->{prefix} = $args{prefix} || '> ';
Condition100%2/2
$args{'prefix'} || '> '
l!l
12
MC/DC100%1/1
$args{'prefix'} || '> '
$args{'prefix'}
Truth table100%2/2
A
A= $args{'prefix'}
Aresult
11
00
65 3 $self->{top_post} = $args{top_post};
66 3 ▶ $self->{quote} = exists $args{quote} ? $args{quote} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'quote'} ? : 0 3 3
67 3 $self->{all} = $args{all};
68 3 $self->{quoted} = '';
69 3 $self->{body} = $args{body};
70 3 $self->{attach} = $args{attach};
71 3 $self->{keep_sig} = $args{keep_sig};
72  
73 3 return bless $self, $class;
74 }
75  
76 3 ▶sub _make_headers {
Subroutine _make_headers: called CC=10 SCAR=23.1
77 3 my $self = shift;
78  
79 3 my @header = (From => $self->{from},);
80  
81 $self->{to}
82 ->name((Email::Address->parse($self->{original}->header('From')))[0]->name)
83 3 ▶ unless $self->{to}->name;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"to"}->name 0 3 3
84 3 push @header, To => $self->{to};
85  
86 3 ▶ my $subject = $self->{original}->header('Subject') || '';
Condition50%1/2
$self->{'original'}->header('Subject') || ''
l!l
30
MC/DC0%0/1
$self->{'original'}->header('Subject') || ''
$self->{'original'}->header('Subject')
Truth table50%1/2
A
A= $self->{'original'}->header('Subject')
Aresult
11
00
87 3 ▶ $subject = "Re: $subject" unless $subject =~ /\bRe:/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $subject =~ /\bRe:/i 2 1 3
88 3 push @header, Subject => $subject;
89  
90 3 my ($msg_id) = Email::Address->parse($self->{original}->header('Message-ID'));
91 3 push @header, 'In-Reply-To' => $msg_id;
92  
93 3 my @refs = Email::Address->parse($self->{original}->header('References'));
94 3 ▶ @refs = Email::Address->parse($self->{original}->header('In-Reply-To'))
Branch: 1 branch
BranchTrueFalseTotal
unless @refs 2 1 3
95 unless @refs;
96 3 ▶ push @refs, $msg_id if $msg_id;
Branch: 1 branch
BranchTrueFalseTotal
if $msg_id 2 1 3
97 3 ▶ push @header, References => join ' ', @refs if @refs;
Branch: 1 branch
BranchTrueFalseTotal
if @refs 2 1 3
98  
99 3 ▶ if ($self->{all}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"all"}) 1 2 3
100 my @addrs = (
101 Email::Address->parse($self->{original}->header('To')),
102 1 Email::Address->parse($self->{original}->header('Cc')),
103 );
104 1 ▶ unless ($self->{self}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"self"}) 1 0 1
105 1 @addrs = grep { $_->address ne $self->{from}->address } @addrs;
106 }
107 1 ▶ push @header, Cc => join ', ', @addrs if @addrs;
Branch: 1 branch
BranchTrueFalseTotal
if @addrs 1 0 1
108 }
109  
110 3 $self->{header} = \@header;
111 }
112  
113 1 ▶sub _encapsulate_message {
Subroutine _encapsulate_message: called CC=1 SCAR=0.0
114 1 my $self = shift;
115 $self->{message} = Email::MIME->create(
116 attributes => { content_type => 'message/rfc822', },
117 body => $self->{original}->as_string,
118 1 );
119 }
120  
121 1 my $crlf = qr/\x0a\x0d|\x0d\x0a|\x0a|\x0d/;
122  
123 5 ▶sub _quote_body {
Subroutine _quote_body: called CC=9 SCAR=22.8
124 5 my ($self, $part) = @_;
125 5 ▶ return if length $self->{quoted};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"quoted"} 1 4 5
126 4 ▶ return map $self->_quote_body($_), $part->parts if $part->parts > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $part->parts > 1 1 3 4
127 3 ▶ return if $part->content_type && $part->content_type !~ m[\btext/plain\b];
Branch: 1 branch
BranchTrueFalseTotal
if $part->content_type and not $part->content_type =~ m[\btext/plain\b] 0 3 3
Condition33%1/3
$part->content_type and not $part->content_type =~ m[\btext/plain\b]
!ll&&!rl&&r
300
MC/DC0%0/2
$part->content_type and not $part->content_type =~ m[\btext/plain\b]
$part->content_type not $part->content_type =~ m[\btext/plain\b]
Truth table33%1/3
A and not(B)
A= $part->content_typeB= not $part->content_type =~ m[\btext/plain\b]
ABresult
0X0
100
111
128  
129 3 my $body = $part->body;
130  
131 $body = ($self->_strip_sig($body) || $body)
132 3 ▶ if !$self->{keep_sig} && $body =~ /$crlf--\s*$crlf/o;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"keep_sig"} and $body =~ /$crlf--\s*$crlf/o 1 2 3
Condition66%4/6
not $self->{"keep_sig"} and $body =~ /$crlf--\s*$crlf/o
!ll&&!rl&&r
111
$self->_strip_sig($body) || $body
l!l&&r!l&&!r
100
MC/DC100%2/2
not $self->{"keep_sig"} and $body =~ /$crlf--\s*$crlf/o
not $self->{"keep_sig"} $body =~ /$crlf--\s*$crlf/o
MC/DC0%0/2
$self->_strip_sig($body) || $body
$self->_strip_sig($body) $body
Truth table100%3/3
not(A) and B
A= not $self->{"keep_sig"}B= $body =~ /$crlf--\s*$crlf/o
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= $self->_strip_sig($body)B= $body
ABresult
1X1
011
000
133  
134 3 my ($end) = $body =~ /($crlf)/;
135 3 ▶ $end ||= $CRLF;
Condition33%1/3
$end ||= $CRLF
l!l&&r!l&&!r
300
MC/DC0%0/2
$end ||= $CRLF
$end $CRLF
Truth table33%1/3
A ||= B
A= $endB= $CRLF
ABresult
1X1
011
000
136 3 $body =~ s/[\r\n\s]+$//;
137 3 $body = $self->_quote_orig_body($body);
138 3 $body = "$self->{attrib}$end$body$end";
139  
140 3 $self->{crlf} = $end;
141 3 $self->{quoted} = $body;
142 }
143  
144 # Yes, you are witnessing elitism.
145 1 ▶sub _strip_sig { reverse +(split /$crlf\s*--$crlf/o, reverse(pop), 2)[1] }
Subroutine _strip_sig: called CC=1 SCAR=0.0
146  
147 3 ▶sub _quote_orig_body {
Subroutine _quote_orig_body: called CC=1 SCAR=0.0
148 3 my ($self, $body) = @_;
149 3 $body =~ s/($crlf)/$1$self->{prefix}/g;
150 3 "$self->{prefix}$body";
151 }
152  
153 3 ▶sub _post_reply {
Subroutine _post_reply: called CC=3 SCAR=11.0
154 3 my $self = shift;
155 return $self->{reply_body} = $self->{body}
156 3 ▶ unless length $self->{quoted};
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"quoted"} 0 3 3
157 3 my @parts = (@{$self}{qw[quoted body]});
158 3 ▶ @parts = reverse @parts if $self->{top_post};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"top_post"} 1 2 3
159 3 $self->{reply_body} = join $self->{crlf}, @parts;
160 }
161  
162 1 ▶sub _mime {
Subroutine _mime: called CC=1 SCAR=0.0
163 1 my $self = shift;
164 Email::MIME->create(
165 header => $self->{header},
166 parts =>
167 1 [ Email::MIME->create(body => $self->{reply_body}), $self->{message}, ],
168 );
169 }
170  
171 2 ▶sub _simple {
Subroutine _simple: called CC=1 SCAR=0.0
172 2 my $self = shift;
173 Email::Simple->create(
174 header => $self->{header},
175 body => $self->{reply_body},
176 2 );
177 }
178  
179 1 1;
180  
181 #pod =head1 SYNOPSIS
182 #pod
183 #pod use Email::Reply;
184 #pod
185 #pod my $message = Email::Simple->new(join '', <>);
186 #pod my $from = (Email::Address->parse($message->header('From'))[0];
187 #pod
188 #pod my $reply = reply to => $message,
189 #pod from => '"Casey West" <casey@geeknest.com>',
190 #pod all => 1,
191 #pod body => <<__RESPONSE__;
192 #pod Thanks for the message, I'll be glad to explain...
193 #pod __RESPONSE__
194 #pod
195 #pod =head1 DESCRIPTION
196 #pod
197 #pod This software takes the hard out of generating replies to email messages.
198 #pod
199 #pod =func reply
200 #pod
201 #pod my $reply = reply to => $message,
202 #pod from => '"Casey West" <casey@geeknest.com>',
203 #pod all => 1;
204 #pod self => 0,
205 #pod attach => 1,
206 #pod quote => 1,
207 #pod top_post => 0,
208 #pod keep_sig => 1,
209 #pod prefix => ': ',
210 #pod attrib => sprintf("From %s, typer of many words:",
211 #pod $from->name),
212 #pod body => <<__RESPONSE__;
213 #pod Thanks for the message, I'll be glad to explain the picture...
214 #pod __RESPONSE__
215 #pod
216 #pod This function accepts a number of named parameters and returns an email
217 #pod message object of type C<Email::MIME> or C<Email::Simple>, depending
218 #pod on the parameters passed. Lets review those parameters now.
219 #pod
220 #pod =begin :list
221 #pod
222 #pod = C<to>
223 #pod
224 #pod This required parameter is the email message you're replying to. It can
225 #pod represent a number of object types, or a string containing the message. This
226 #pod value is passed directly to C<Email::Abstract> without passing go or collecting
227 #pod $200 so please, read up on its available plugins for what is allowed here.
228 #pod
229 #pod = C<from>
230 #pod
231 #pod This optional parameter specifies an email address to use indicating the sender
232 #pod of the reply message. It can be a string or an C<Email::Address> object. In the
233 #pod absence of this parameter, the first address found in the original message's
234 #pod C<To> header is used. This may not always be what you want, so this parameter
235 #pod comes highly recommended.
236 #pod
237 #pod = C<all>
238 #pod
239 #pod This optional parameter indicates weather or not you'd like to "Reply to All."
240 #pod If true, the reply's C<Cc> header will be populated with all the addresses in
241 #pod the original's C<To> and C<Cc> headers. By default, the parameter is false,
242 #pod indicating "Reply to Sender."
243 #pod
244 #pod = C<self>
245 #pod
246 #pod This optional parameter decides weather or not an address matching the C<from>
247 #pod address will be included in the list of C<all> addresses. If true, your address
248 #pod will be preserved in that list if it is found. If false, as it is by default,
249 #pod your address will be removed from the list. As you might expect, this parameter
250 #pod is only useful if C<all> is true.
251 #pod
252 #pod = C<attach>
253 #pod
254 #pod This optional parameter allows for the original message, in
255 #pod its entirety, to be encapsulated in a MIME part of type C<message/rfc822>.
256 #pod If true, the returned object from C<reply> will be a C<Email::MIME> object
257 #pod whose second part is the encapsulated message. If false, none of this happens.
258 #pod By default, none of this happens.
259 #pod
260 #pod = C<quote>
261 #pod
262 #pod This optional parameter, which is true by default, will quote
263 #pod the original message for your reply. If the original message is a MIME
264 #pod message, the first C<text/plain> type part will be quoted. If it's a Simple
265 #pod message, the body will be quoted. Well, that's only if you keep the
266 #pod parameter true. If you don't, none of this occurs.
267 #pod
268 #pod = C<top_post>
269 #pod
270 #pod This optional parameter, whose use is generally discouraged, will allow top
271 #pod posting when true. It will implicitly set C<quote> to true, and put your
272 #pod C<body> before the quoted text. It is false by default, and you should do your
273 #pod best to keep it that way.
274 #pod
275 #pod = C<keep_sig>
276 #pod
277 #pod This optional parameter toggles the signature stripping mechanism. True by
278 #pod default, the original quoted body will have its signature removed. When false,
279 #pod the signature is left in-tact and will be quoted accordingly. This is only
280 #pod useful when C<quote> is true.
281 #pod
282 #pod = C<prefix>
283 #pod
284 #pod This optional parameter specifies the quoting prefix. By default, it's
285 #pod C<< > >>, but you can change it by setting this parameter. Again, only useful
286 #pod when C<quote> is true.
287 #pod
288 #pod = C<attrib>
289 #pod
290 #pod This optional parameter specifies the attribution line to add to the beginning
291 #pod of quoted text. By default, the name or email address of the original sender is
292 #pod used to replace C<%s> in the string, C<"%s wrote:">. You may change that with
293 #pod this parameter. No special formats, C<sprintf()> or otherwise, are provided for
294 #pod your convenience. Sorry, you'll have to make due. Like C<prefix> and
295 #pod C<keep_sig>, this is only good when C<quote> is true.
296 #pod
297 #pod = C<body>
298 #pod
299 #pod This required parameter contains your prose, your manifesto, your reply.
300 #pod Remember to spell check!
301 #pod
302 #pod =end :list
303 #pod
304 #pod =head1 SEE ALSO
305 #pod
306 #pod L<Email::Abstract>,
307 #pod L<Email::MIME>,
308 #pod L<Email::MIME::Creator>,
309 #pod L<Email::Simple::Creator>,
310 #pod L<Email::Address>,
311 #pod L<perl>.
312  
313 __END__
↑ Summary