blib/lib/Mail/DWIM.pm

filter: Statementstmt 64.1% 143 / 223 Branchbran 48.7% 38 / 78 Conditioncond 66.6% 8 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 80.6% 25 / 31 Podpod 0.0% 0 / 20 totaltotal 58.0% 216 / 372 CC 53 SCAR 24.7
CC
53
Cov
60%
cmd_line_mail
30.0
send
25.3
html_msg
20.0
CRAP 11.8
↑ Summary
1 ###########################################
2 package Mail::DWIM;
3 ###########################################
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 our @ISA = qw(Exporter);
8 4 our @EXPORT_OK = qw(mail);
9 4 our $VERSION = "0.12";
10 4 our @HTML_MODULES = qw(HTML::FormatText HTML::TreeBuilder MIME::Lite);
11 4 our @ATTACH_MODULES = qw(MIME::Lite MIME::Types);
12  
13 4 ▶use YAML qw(LoadFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Mail::Mailer;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 my $error;
23  
24 ###########################################
25 4 ▶sub mail {
Subroutine mail: called CC=1 SCAR=0.0
Pod undocumented
26 ###########################################
27 4 my(@params) = @_;
28  
29 4 my $mailer = Mail::DWIM->new(@params);
30 4 $mailer->send();
31 }
32  
33 ###########################################
34 9 ▶sub new {
Subroutine new: called CC=8 SCAR=20.9
Pod undocumented
35 ###########################################
36 9 my($class, %options) = @_;
37  
38 4 ▶ my($homedir) = glob "~";
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 9 my %defaults;
41  
42 9 my $self = {
43 global_cfg_file => "/etc/maildwim",
44 user_cfg_file => "$homedir/.maildwim",
45 transport => "sendmail",
46 raise_error => 1,
47 %options,
48 };
49  
50 # Guess the 'from' address
51 9 ▶ if(! exists $self->{from}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"from"}) 4 5 9
52 4 my $user = scalar getpwuid($<);
53 4 my $domain = domain();
54 4 $self->{from} = "$user\@$domain";
55 }
56  
57 # Guess the 'date'
58 9 ▶ if (!exists $self->{date}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"date"}) 9 0 9
59 9 $self->{date} = strftime("%a, %e %b %Y %H:%M:%S %Z", localtime(time));
60 }
61  
62 9 for my $cfg (qw(global_cfg_file user_cfg_file)) {
63 18 ▶ if(-f $self->{$cfg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{$cfg}) 8 10 18
64 8 my $yml = LoadFile( $self->{$cfg} );
65 8 ▶ if(defined $yml and ref $yml ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $yml and ref $yml ne "HASH") 0 8 8
Condition66%2/3
defined $yml and ref $yml ne "HASH"
!ll&&!rl&&r
350
MC/DC0%0/2
defined $yml and ref $yml ne "HASH"
defined $yml ref $yml ne "HASH"
Truth table66%2/3
A and B
A= defined $ymlB= ref $yml ne "HASH"
ABresult
0X0
100
111
66 # Needs to be a hash, but YAML file can be empty (undef)
67 0 LOGDIE "YAML file $self->{$cfg} format not a hash";
68 }
69 # merge with existing hash
70 8 ▶ %defaults = (%defaults, %$yml) if defined $yml;
Branch: 1 branch
BranchTrueFalseTotal
if defined $yml 5 3 8
71 }
72 }
73  
74 9 %$self = (%$self, %defaults, %options);
75  
76 9 bless $self, $class;
77 }
78  
79 ###########################################
80 0 ▶sub cmd_line_mail {
Subroutine cmd_line_mail: not called CC=5 SCAR=34.0
Pod undocumented
81 ###########################################
82 0 my($self) = @_;
83  
84 0 ▶ $self->{subject} = 'no subject' unless defined $self->{subject};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"subject"} 0 0 0
85  
86 0 my $mailer;
87 0 ▶ $mailer = $self->{program} if defined $self->{program};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"program"} 0 0 0
88 0 ▶ $mailer = bin_find("mail") unless defined $mailer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mailer 0 0 0
89  
90 open(PIPE, "|-", $mailer,
91 "-s", $self->{subject}, $self->{to},
92 0 ▶ ) or LOGDIE "Opening $mailer failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
open PIPE, "|-", $mailer, "-s", $self->{"subject"}, $self->{"to"} or LOGDIE("Opening $mailer failed: $!") 0 0 0
93  
94 0 print PIPE $self->{text};
95 0 return close PIPE;
96 }
97  
98 ###########################################
99 6 ▶sub send {
Subroutine send: called CC=17 SCAR=32.3
Pod undocumented
100 ###########################################
101 6 my($self, $evaled) = @_;
102  
103 6 ▶ if(!$self->{raise_error} && ! $evaled) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"raise_error"} or $evaled) 1 5 6
Condition100%3/3
$self->{"raise_error"} or $evaled
l!l&&r!l&&!r
411
MC/DC100%2/2
$self->{"raise_error"} or $evaled
$self->{"raise_error"} $evaled
Truth table100%3/3
A or B
A= $self->{"raise_error"}B= $evaled
ABresult
1X1
011
000
104 1 return $self->send_evaled();
105 }
106  
107 my $msg =
108 "Sending from=$self->{from} to=$self->{to} " .
109 "subj=" . snip($self->{subject}, 20) . " " .
110 5 "text=" . snip($self->{text}, 20) .
111 "";
112  
113 5 ▶ if($self->{dryrun}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dryrun"}) 1 4 5
114 1 print "$msg\n";
115 1 return 1;
116 }
117  
118 4 my @options = ();
119  
120 4 ▶ if(0) {
Branch: 3 branches
BranchTrueFalseTotal
if (do { $self->{'transport'} eq 'sendmail' }) { } 2 2 4
elsif ($self->{'transport'} eq 'smtp') { } 2 0 2
elsif ($self->{'transport'} eq 'mail') { } 0 0 0
121 } elsif($self->{transport} eq "sendmail") {
122 2 @options = ();
123 } elsif($self->{transport} eq "smtp") {
124 # Mail::SMTP likes it that way
125 2 $ENV{ MAILADDRESS } = $self->{ from };
126  
127 2 ▶ LOGDIE "No smtp_server set" unless defined $self->{smtp_server};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"smtp_server"} 1 1 2
128 1 @options = ("smtp", Server => $self->{smtp_server});
129 push @options, (Port => $self->{smtp_port})
130 1 ▶ if exists $self->{smtp_port};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"smtp_port"} 0 1 1
131 1 $self->{to} = [split /\s*,\s*/, $self->{to}];
132  
133 # some smtp servers want SASL auth
134 1 ▶ if( defined $self->{user} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"user"}) 1 0 1
135 1 require Authen::SASL;
136 push @options, (
137 0 Auth => [ $self->{user}, $self->{password} ],
138 SSL => 1,
139 );
140 }
141 } elsif($self->{transport} eq "mail") {
142 0 return $self->cmd_line_mail();
143 } else {
144 0 LOGDIE "Unknown transport '$self->{transport}'";
145 }
146  
147 2 my $mailer = Mail::Mailer->new(@options);
148 2 my %headers;
149 2 for (qw(from to cc bcc subject date)) {
150 12 ▶ $headers{ucfirst($_)} = $self->{$_} if exists $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$_} 8 4 12
151 }
152  
153 2 my $text = $self->{text};
154 2 ▶ if($self->{html_compat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"html_compat"}) 0 2 2
155 0 my $h;
156 0 ($h, $text) = html_msg($text);
157 0 %headers = (%headers, %$h);
158 }
159  
160 2 ▶ if($self->{attach}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"attach"}) 0 2 2
161 0 my $h;
162 0 ($h, $text) = attach_msg($text, @{$self->{attach}});
163 0 %headers = (%headers, %$h);
164 }
165  
166 2 ▶ if($ENV{MAIL_DWIM_TEST}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'MAIL_DWIM_TEST'}) { } 2 0 2
167 2 DEBUG "Appending to test file $ENV{MAIL_DWIM_TEST}";
168 2 my $txt;
169 2 for (keys %headers) {
170 8 ▶ $txt .= "$_: $headers{$_}\n" if defined $headers{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $headers{$_} 8 0 8
171 }
172 2 $txt .= "\noptions @options\n\n";
173  
174 2 test_file_append($txt . $text);
175 2 return 1;
176 } else {
177 0 DEBUG $msg;
178 }
179  
180 0 $mailer->open(\%headers);
181 0 print $mailer $text;
182 0 $mailer->close();
183 }
184  
185 ###########################################
186 1 ▶sub send_evaled {
Subroutine send_evaled: called CC=2 SCAR=7.0
Pod undocumented
187 ###########################################
188 1 my($self) = @_;
189  
190 1 eval {
191 1 return $self->send(1);
192 };
193  
194 1 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
195 1 error($@);
196 1 return undef;
197 }
198 }
199  
200 ###########################################
201 2 ▶sub error {
Subroutine error: called CC=2 SCAR=6.9
Pod undocumented
202 ###########################################
203 2 my($text) = @_;
204  
205 2 ▶ if(defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 1 1 2
206 1 $error = $text;
207 }
208  
209 2 return $error;
210 }
211  
212 ###########################################
213 2 ▶sub test_file_append {
Subroutine test_file_append: called CC=2 SCAR=7.0
Pod undocumented
214 ###########################################
215 2 my($msg) = @_;
216  
217 2 ▶ open FILE, ">>$ENV{MAIL_DWIM_TEST}" or
Branch: 1 branch
BranchTrueFalseTotal
open FILE, qq[>>$ENV{"MAIL_DWIM_TEST"}] or LOGDIE(qq[Cannot open $ENV{"MAIL_DWIM_TEST"} ($!)]) 2 0 2
218 LOGDIE "Cannot open $ENV{MAIL_DWIM_TEST} ($!)";
219 2 print FILE $msg, "\n\n";
220 2 close FILE;
221 }
222  
223 ###########################################
224 2 ▶sub html_requirements {
Subroutine html_requirements: called CC=3 SCAR=11.1
Pod undocumented
225 ###########################################
226  
227 2 for (@HTML_MODULES) {
228 6 eval "require $_";
229 6 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 4 6
230 2 return undef;
231 }
232 }
233  
234 0 1;
235 }
236  
237 ###########################################
238 4 ▶sub attach_requirements {
Subroutine attach_requirements: called CC=3 SCAR=11.7
Pod undocumented
239 ###########################################
240  
241 4 for (@ATTACH_MODULES) {
242 4 eval "require $_";
243 4 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 0 4
244 4 return undef;
245 }
246 }
247  
248 0 1;
249 }
250  
251 ###########################################
252 0 ▶sub html_msg {
Subroutine html_msg: not called CC=4 SCAR=30.0
Pod undocumented
253 ###########################################
254 0 my($htmltext) = @_;
255  
256 0 ▶ if(! html_requirements()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (html_requirements()) 0 0 0
257 0 LOGDIE "Please install ",
258 join(" ", @HTML_MODULES), " from CPAN";
259 }
260  
261 0 my $tree = HTML::TreeBuilder->new();
262 0 $tree->parse($htmltext);
263 0 $tree->eof();
264 0 my $formatter = HTML::FormatText->new();
265 0 my $plaintext = $formatter->format($tree);
266  
267 0 my $msg = MIME::Lite->new(
268 Type => 'multipart/alternative',
269 );
270  
271 0 $msg->attach(
272 Type => 'text/plain',
273 Encoding => 'quoted-printable',
274 Data => $plaintext
275 );
276  
277 0 $msg->attach(
278 Type => 'text/html',
279 Encoding => 'quoted-printable',
280 Data => $htmltext,
281 );
282  
283 0 my %headers;
284  
285 0 for (qw(Content-Transfer-Encoding Content-Type
286 MIME-version)) {
287 0 $headers{$_} = $msg->attr($_);
288 }
289  
290 # evil hack to compensate for MIME::Lite's shortcomings
291 0 ▶ if( $headers{ "Content-Type" } !~ /boundary/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers{"Content-Type"} =~ /boundary/) 0 0 0
292 $headers{ "Content-Type" } =
293 $headers{ "Content-Type" } .
294 qq{; boundary="} .
295 $msg->{ SubAttrs }->{ "content-type" }->{ boundary } .
296 0 qq{"};
297 }
298  
299 0 return \%headers, $msg->body_as_string;
300 }
301  
302 ###########################################
303 0 ▶sub attach_msg {
Subroutine attach_msg: not called CC=4 SCAR=30.0
Pod undocumented
304 ###########################################
305 0 my($text, @files) = @_;
306  
307 0 ▶ if(! attach_requirements()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (attach_requirements()) 0 0 0
308 0 LOGDIE "Please install ",
309 join(" ", @ATTACH_MODULES), " from CPAN";
310 }
311  
312 0 my $msg = MIME::Lite->new(
313 Type => "multipart/mixed"
314 );
315  
316 0 $msg->attach(Type => "TEXT",
317 Encoding => 'quoted-printable',
318 Data => $text,
319 );
320  
321 0 my $mime_types = MIME::Types->new();
322  
323 0 for my $file (@files) {
324 0 my $type = $mime_types->mimeTypeOf($file);
325 # MIME::Lite expects a string, not a MIME::Type object.
326 0 ▶ $type = defined $type ? $type->type : "application/octet-stream";
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 0 0 0
327  
328 0 $msg->attach(Type => $type,
329 Path => $file,
330 Filename => basename($file),
331 Disposition => "attachment",
332 );
333 }
334  
335 0 my $headers = mime_lite_headers($msg);
336  
337 0 return $headers, $msg->body_as_string;
338 }
339  
340 ###########################################
341 0 ▶sub mime_lite_headers {
Subroutine mime_lite_headers: not called CC=3 SCAR=24.8
Pod undocumented
342 ###########################################
343 0 my($mlite) = @_;
344  
345 0 my %wanted = map { lc($_) => $_ }
346 qw(Content-Transfer-Encoding Content-Type
347 MIME-version);
348 0 my %headers = ();
349  
350 0 for my $field (@{$mlite->fields}) {
351 0 ▶ if(exists $wanted{$field->[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $wanted{$field->[0]}) 0 0 0
352 0 my($name, $value) = split /:\s*/,
353 $mlite->fields_as_string([$field]), 2;
354 0 $headers{$name} = $value;
355 }
356 }
357  
358 0 return(\%headers);
359 }
360  
361 ###########################################
362 0 ▶sub header_ucfirst {
Subroutine header_ucfirst: not called CC=1 SCAR=6.9
Pod undocumented
363 ###########################################
364 0 my($name) = @_;
365  
366 0 $name =~ s/^(\w)/uc($1)/g;
367 0 $name =~ s/-(\w)/uc($1)/g;
368  
369 0 return $name;
370 }
371  
372 ###########################################
373 10 ▶sub snip {
Subroutine snip: called CC=3 SCAR=11.0
Pod undocumented
374 ###########################################
375 10 my($data, $maxlen) = @_;
376  
377 10 ▶ if(length $data <= $maxlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data <= $maxlen) 8 2 10
378 8 return lenformat($data);
379 }
380  
381 2 ▶ $maxlen = 12 if $maxlen < 12;
Branch: 1 branch
BranchTrueFalseTotal
if $maxlen < 12 0 2 2
382 2 my $sniplen = int(($maxlen - 8) / 2);
383  
384 2 my $start = substr($data, 0, $sniplen);
385 2 my $end = substr($data, -$sniplen);
386 2 my $snipped = length($data) - 2*$sniplen;
387  
388 2 return lenformat("$start\[...]$end", length $data);
389 }
390  
391 ###########################################
392 10 ▶sub lenformat {
Subroutine lenformat: called CC=2 SCAR=7.1
Pod undocumented
393 ###########################################
394 10 my($data, $orglen) = @_;
395  
396 10 ▶ return "(" . ($orglen || length($data)) . ")[" .
Condition66%2/3
$orglen || length $data
l!l&&r!l&&!r
280
MC/DC0%0/2
$orglen || length $data
$orglen length $data
Truth table66%2/3
A || B
A= $orglenB= length $data
ABresult
1X1
011
000
397 printable($data) . "]";
398 }
399  
400 ###########################################
401 10 ▶sub printable {
Subroutine printable: called CC=1 SCAR=0.0
Pod undocumented
402 ###########################################
403 10 my($data) = @_;
404  
405 10 $data =~ s/[^ \w.;!?@#$%^&*()+\\|~`'-,><[\]{}="]/./g;
406 10 return $data;
407 }
408  
409 ###########################################
410 10 ▶sub blurt {
Subroutine blurt: called CC=2 SCAR=7.0
Pod undocumented
411 ###########################################
412 10 my($data, $file) = @_;
413  
414 10 ▶ open FILE, ">$file" or die "Cannot open $file";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">$file" or die "Cannot open $file" 10 0 10
415 10 print FILE $data;
416 10 close FILE;
417 }
418  
419 ###########################################
420 2 ▶sub slurp {
Subroutine slurp: called CC=2 SCAR=7.0
Pod undocumented
421 ###########################################
422 2 my($file) = @_;
423  
424 2 local($/);
425 2 $/ = undef;
426  
427 2 ▶ open FILE, "<$file" or die "Cannot open $file";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$file" or die "Cannot open $file" 2 0 2
428 2 my $data = <FILE>;
429 2 close FILE;
430 2 return $data;
431 }
432  
433 ###########################################
434 4 ▶sub domain {
Subroutine domain: called CC=4 SCAR=21.7
Pod undocumented
435 ###########################################
436  
437 4 my $domain = $Config{mydomain};
438  
439 4 ▶ if(defined $domain and length($domain)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $domain and length $domain) 4 0 4
Condition33%1/3
defined $domain and length $domain
!ll&&!rl&&r
004
MC/DC0%0/2
defined $domain and length $domain
defined $domain length $domain
Truth table33%1/3
A and B
A= defined $domainB= length $domain
ABresult
0X0
100
111
440 4 $domain =~ s/^\.//;
441 4 return $domain;
442 }
443  
444 0 eval { require Sys::Hostname; };
445 0 ▶ if(! $@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
446 0 $domain = hostname();
447 0 return $domain;
448 }
449  
450 0 $domain = "localhost";
451  
452 0 return $domain;
453 }
454  
455 ######################################
456 0 ▶sub bin_find {
Subroutine bin_find: not called CC=3 SCAR=24.8
Pod undocumented
457 ######################################
458 0 my($exe) = @_;
459  
460 0 for my $path (split /:/, $ENV{PATH}) {
461 0 my $full = File::Spec->catfile($path, $exe);
462  
463 0 ▶ return $full if -x $full;
Branch: 1 branch
BranchTrueFalseTotal
if -x $full 0 0 0
464 }
465  
466 0 return undef;
467 }
468  
469 4 1;
470  
471 __END__
↑ Summary