Mail/Util.pm

filter: Statementstmt 21.1% 18 / 85 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 50.0% 4 / 8 Podpod 100% 4 / 4 totaltotal 12.9% 26 / 201 CC 41 SCAR 58.1
CC
41
Cov
10%
maildomain
600.0
read_mbox
90.0
mailaddress
87.6
CRAP 335.2
« Mail/Internet.pm ↑ Summary Mail/Header.pm »
1 # Copyrights 1995-2024 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5 # This code is part of the bundle MailTools. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md for Copyright.
7 # Licensed under the same terms as Perl itself.
8  
9 package Mail::Util;{
10 3 our $VERSION = '2.22';
11 }
12  
13 3 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 our @EXPORT_OK = qw(read_mbox maildomain mailaddress);
19  
20 0 ▶sub Version { our $VERSION }
Subroutine Version: not called CC=1 SCAR=6.9
Pod documented
21  
22 3 my ($domain, $mailaddress);
23 3 my @sendmailcf = qw(/etc /etc/sendmail /etc/ucblib
24 /etc/mail /usr/lib /var/adm/sendmail);
25  
26  
27 sub read_mbox($)
28 0 ▶{ my $file = shift;
Subroutine read_mbox: not called CC=9 SCAR=45.0
Pod documented
29  
30 0 local *FH;
31 0 ▶ open FH,'<', $file
Branch: 1 branch
BranchTrueFalseTotal
open FH, "<", $file or croak("cannot open '${file}': $!\n") 0 0 0
32 or croak "cannot open '$file': $!\n";
33  
34 0 local $_;
35 0 my @mbox;
36 0 my $mail = [];
37 0 my $blank = 1;
38  
39 0 ▶ while(<FH>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 0 0 0
40 0 ▶ { if($blank && /^From .*\d{4}/)
Branch: 1 branch
BranchTrueFalseTotal
if ($blank and /^From .*\d{4}/) { } 0 0 0
Condition0%0/3
$blank and /^From .*\d{4}/
!ll&&!rl&&r
000
MC/DC0%0/2
$blank and /^From .*\d{4}/
$blank /^From .*\d{4}/
Truth table0%0/3
A and B
A= $blankB= /^From .*\d{4}/
ABresult
0X0
100
111
41 0 ▶ { push @mbox, $mail if @$mail;
Branch: 1 branch
BranchTrueFalseTotal
if @$mail 0 0 0
42 0 $mail = [ $_ ];
43 0 $blank = 0;
44 }
45 else
46 0 ▶ { $blank = m/^$/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
/^$/ ? : 0 0 0
47 0 push @$mail, $_;
48 }
49 }
50  
51 0 ▶ push @mbox, $mail if @$mail;
Branch: 1 branch
BranchTrueFalseTotal
if @$mail 0 0 0
52 0 close FH;
53  
54 0 ▶ wantarray ? @mbox : \@mbox;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
55 }
56  
57  
58 sub maildomain()
59 0 ▶{ return $domain
Subroutine maildomain: not called CC=24 SCAR=64.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if defined $domain 0 0 0
60 if defined $domain;
61  
62 $domain = $ENV{MAILDOMAIN}
63 0 ▶ and return $domain;
Branch: 1 branch
BranchTrueFalseTotal
$domain = $ENV{"MAILDOMAIN"} and return $domain 0 0 0
64  
65 # Try sendmail configuration file
66  
67 0 my $config = (grep -r, map {"$_/sendmail.cf"} @sendmailcf)[0];
68  
69 0 local *CF;
70 0 local $_;
71 0 ▶ if(defined $config && open CF, '<', $config)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config and open CF, "<", $config) 0 0 0
Condition0%0/3
defined $config and open CF, "<", $config
!ll&&!rl&&r
000
MC/DC0%0/2
defined $config and open CF, "<", $config
defined $config open CF, "<", $config
Truth table0%0/3
A and B
A= defined $configB= open CF, "<", $config
ABresult
0X0
100
111
72 0 { my %var;
73 0 ▶ while(<CF>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CF)) 0 0 0
74 0 ▶ { if(my ($v, $arg) = /^D([a-zA-Z])([\w.\$\-]+)/)
Branch: 1 branch
BranchTrueFalseTotal
if (() = /^D([a-zA-Z])([\w.\$\-]+)/) 0 0 0
75 0 ▶ { $arg =~ s/\$([a-zA-Z])/exists $var{$1} ? $var{$1} : '$'.$1/eg;
Branch: 1 branch
BranchTrueFalseTotal
exists $var{$1} ? : 0 0 0
76 0 $var{$v} = $arg;
77 }
78 }
79 0 close CF;
80 0 ▶ $domain = $var{j} if defined $var{j};
Branch: 1 branch
BranchTrueFalseTotal
if defined $var{"j"} 0 0 0
81 0 ▶ $domain = $var{M} if defined $var{M};
Branch: 1 branch
BranchTrueFalseTotal
if defined $var{"M"} 0 0 0
82  
83 0 ▶ $domain = $1
Branch: 1 branch
BranchTrueFalseTotal
if $domain and $domain =~ /([A-Za-z0-9](?:[\.\-A-Za-z0-9]+))/ 0 0 0
Condition0%0/3
$domain and $domain =~ /([A-Za-z0-9](?:[\.\-A-Za-z0-9]+))/
!ll&&!rl&&r
000
MC/DC0%0/2
$domain and $domain =~ /([A-Za-z0-9](?:[\.\-A-Za-z0-9]+))/
$domain $domain =~ /([A-Za-z0-9](?:[\.\-A-Za-z0-9]+))/
Truth table0%0/3
A and B
A= $domainB= $domain =~ /([A-Za-z0-9](?:[\.\-A-Za-z0-9]+))/
ABresult
0X0
100
111
84 if $domain && $domain =~ m/([A-Za-z0-9](?:[\.\-A-Za-z0-9]+))/;
85  
86 0 ▶ return $domain
Branch: 1 branch
BranchTrueFalseTotal
if defined $domain and not $domain =~ /\$/ 0 0 0
Condition0%0/3
defined $domain and not $domain =~ /\$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $domain and not $domain =~ /\$/
defined $domain not $domain =~ /\$/
Truth table0%0/3
A and not(B)
A= defined $domainB= not $domain =~ /\$/
ABresult
0X0
100
111
87 if defined $domain && $domain !~ /\$/;
88 }
89  
90 # Try smail config file if exists
91  
92 0 ▶ if(open CF, '<', "/usr/lib/smail/config")
Branch: 1 branch
BranchTrueFalseTotal
if (open CF, "<", "/usr/lib/smail/config") 0 0 0
93 0 ▶ { while(<CF>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CF)) 0 0 0
94 0 ▶ { if( /\A\s*hostnames?\s*=\s*(\S+)/ )
Branch: 1 branch
BranchTrueFalseTotal
if (/\A\s*hostnames?\s*=\s*(\S+)/) 0 0 0
95 0 { $domain = (split /\:/,$1)[0];
96 0 last;
97 }
98 }
99 0 close CF;
100  
101 0 ▶ return $domain
Branch: 1 branch
BranchTrueFalseTotal
if defined $domain 0 0 0
102 if defined $domain;
103 }
104  
105 # Try a SMTP connection to 'mailhost'
106  
107 0 ▶ if(eval {require Net::SMTP})
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Net::SMTP } }) 0 0 0
108 0 { foreach my $host (qw(mailhost localhost))
109 { # hosts are local, so short timeout
110 0 my $smtp = eval { Net::SMTP->new($host, Timeout => 5) };
111 0 ▶ if(defined $smtp)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $smtp) 0 0 0
112 0 { $domain = $smtp->domain;
113 0 $smtp->quit;
114 0 last;
115 }
116 }
117 }
118  
119 # Use internet(DNS) domain name, if it can be found
120 $domain = Net::Domain::domainname()
121 0 ▶ if !defined $domain && eval {require Net::Domain};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $domain and eval { do { require Net::Domain } } 0 0 0
Condition0%0/3
not defined $domain and eval { do { require Net::Domain } }
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $domain and eval { do { require Net::Domain } }
not defined $domain eval { do { require Net::Domain } }
Truth table0%0/3
not(A) and B
A= not defined $domainB= eval { do { require Net::Domain } }
ABresult
0X0
100
111
122  
123 0 ▶ $domain ||= "localhost";
Condition0%0/2
$domain ||= "localhost"
l!l
00
MC/DC0%0/1
$domain ||= "localhost"
$domain
Truth table0%0/2
A
A= $domain
Aresult
11
00
124 }
125  
126  
127 sub mailaddress(;$)
128 0 ▶{ $mailaddress = shift if @_;
Subroutine mailaddress: not called CC=10 SCAR=44.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
129  
130 0 ▶ return $mailaddress
Branch: 1 branch
BranchTrueFalseTotal
if defined $mailaddress 0 0 0
131 if defined $mailaddress;
132  
133 # Get user name from environment
134 0 $mailaddress = $ENV{MAILADDRESS};
135  
136 0 ▶ unless($mailaddress || $^O ne 'MacOS')
Branch: 1 branch
BranchTrueFalseTotal
unless ($mailaddress or $^O ne "MacOS") 0 0 0
Condition0%0/3
$mailaddress or $^O ne "MacOS"
l!l&&r!l&&!r
000
MC/DC0%0/2
$mailaddress or $^O ne "MacOS"
$mailaddress $^O ne "MacOS"
Truth table0%0/3
A or B
A= $mailaddressB= $^O ne "MacOS"
ABresult
1X1
011
000
137 0 { require Mac::InternetConfig;
138  
139 3 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 0 Mac::InternetConfig->import;
141 0 $mailaddress = $InternetConfig{kICEmail()};
142 }
143  
144 0 ▶ $mailaddress ||= $ENV{USER} || $ENV{LOGNAME} || eval {getpwuid $>}
Condition0%0/11
$mailaddress ||= $ENV{"USER"} || $ENV{"LOGNAME"} || eval { do { getpwuid $> } } || "postmaster"
l!l&&r!l&&!r
000
$ENV{'USER'} || $ENV{'LOGNAME'} || eval { do { getpwuid $> } } || 'postmaster'
l!l
00
$ENV{"USER"} or $ENV{"LOGNAME"} or eval { do { getpwuid $> } }
l!l&&r!l&&!r
000
$ENV{"USER"} or $ENV{"LOGNAME"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$mailaddress ||= $ENV{"USER"} || $ENV{"LOGNAME"} || eval { do { getpwuid $> } } || "postmaster"
$mailaddress $ENV{"USER"} $ENV{"LOGNAME"} eval { do { getpwuid $> } }
Truth table0%0/5
A ||= B or C or D
A= $mailaddressB= $ENV{"USER"}C= $ENV{"LOGNAME"}D= eval { do { getpwuid $> } }
ABCDresult
1XXX1
01XX1
001X1
00011
00000
145 || "postmaster";
146  
147 # Add domain if it does not exist
148 0 ▶ $mailaddress .= '@' . maildomain
Branch: 1 branch
BranchTrueFalseTotal
unless $mailaddress =~ /\@/ 0 0 0
149 if $mailaddress !~ /\@/;
150  
151 0 $mailaddress =~ s/(^.*<|>.*$)//g;
152 0 $mailaddress;
153 }
154  
155 3 1;
« Mail/Internet.pm ↑ Summary Mail/Header.pm »