line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyrights 1995-2017 by [Mark Overmeer ]. |
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.02. |
5
|
|
|
|
|
|
|
package Mail::Util; |
6
|
3
|
|
|
3
|
|
20
|
use vars '$VERSION'; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
175
|
|
7
|
|
|
|
|
|
|
$VERSION = '2.19'; |
8
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
22
|
use base 'Exporter'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
272
|
|
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
|
23
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
72
|
|
12
|
3
|
|
|
3
|
|
15
|
use Carp; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
2735
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our @EXPORT_OK = qw(read_mbox maildomain mailaddress); |
15
|
|
|
|
|
|
|
|
16
|
0
|
|
|
0
|
1
|
|
sub Version { our $VERSION } |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my ($domain, $mailaddress); |
19
|
|
|
|
|
|
|
my @sendmailcf = qw(/etc /etc/sendmail /etc/ucblib |
20
|
|
|
|
|
|
|
/etc/mail /usr/lib /var/adm/sendmail); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub read_mbox($) |
24
|
0
|
|
|
0
|
1
|
|
{ my $file = shift; |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
local *FH; |
27
|
0
|
0
|
|
|
|
|
open FH,'<', $file |
28
|
|
|
|
|
|
|
or croak "cannot open '$file': $!\n"; |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
local $_; |
31
|
0
|
|
|
|
|
|
my @mbox; |
32
|
0
|
|
|
|
|
|
my $mail = []; |
33
|
0
|
|
|
|
|
|
my $blank = 1; |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
while() |
36
|
0
|
0
|
0
|
|
|
|
{ if($blank && /^From .*\d{4}/) |
37
|
0
|
0
|
|
|
|
|
{ push @mbox, $mail if @$mail; |
38
|
0
|
|
|
|
|
|
$mail = [ $_ ]; |
39
|
0
|
|
|
|
|
|
$blank = 0; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
else |
42
|
0
|
0
|
|
|
|
|
{ $blank = m/^$/ ? 1 : 0; |
43
|
0
|
|
|
|
|
|
push @$mail, $_; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
0
|
0
|
|
|
|
|
push @mbox, $mail if @$mail; |
48
|
0
|
|
|
|
|
|
close FH; |
49
|
|
|
|
|
|
|
|
50
|
0
|
0
|
|
|
|
|
wantarray ? @mbox : \@mbox; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub maildomain() |
55
|
0
|
0
|
|
0
|
1
|
|
{ return $domain |
56
|
|
|
|
|
|
|
if defined $domain; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$domain = $ENV{MAILDOMAIN} |
59
|
0
|
0
|
|
|
|
|
and return $domain; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# Try sendmail configuration file |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
my $config = (grep -r, map {"$_/sendmail.cf"} @sendmailcf)[0]; |
|
0
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
local *CF; |
66
|
0
|
|
|
|
|
|
local $_; |
67
|
0
|
0
|
0
|
|
|
|
if(defined $config && open CF, '<', $config) |
68
|
0
|
|
|
|
|
|
{ my %var; |
69
|
0
|
|
|
|
|
|
while() |
70
|
0
|
0
|
|
|
|
|
{ if(my ($v, $arg) = /^D([a-zA-Z])([\w.\$\-]+)/) |
71
|
0
|
0
|
|
|
|
|
{ $arg =~ s/\$([a-zA-Z])/exists $var{$1} ? $var{$1} : '$'.$1/eg; |
|
0
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
$var{$v} = $arg; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
0
|
|
|
|
|
|
close CF; |
76
|
0
|
0
|
|
|
|
|
$domain = $var{j} if defined $var{j}; |
77
|
0
|
0
|
|
|
|
|
$domain = $var{M} if defined $var{M}; |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
0
|
|
|
|
$domain = $1 |
80
|
|
|
|
|
|
|
if $domain && $domain =~ m/([A-Za-z0-9](?:[\.\-A-Za-z0-9]+))/; |
81
|
|
|
|
|
|
|
|
82
|
0
|
0
|
0
|
|
|
|
return $domain |
83
|
|
|
|
|
|
|
if defined $domain && $domain !~ /\$/; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Try smail config file if exists |
87
|
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
if(open CF, '<', "/usr/lib/smail/config") |
89
|
0
|
|
|
|
|
|
{ while() |
90
|
0
|
0
|
|
|
|
|
{ if( /\A\s*hostnames?\s*=\s*(\S+)/ ) |
91
|
0
|
|
|
|
|
|
{ $domain = (split /\:/,$1)[0]; |
92
|
0
|
|
|
|
|
|
last; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
0
|
|
|
|
|
|
close CF; |
96
|
|
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
|
return $domain |
98
|
|
|
|
|
|
|
if defined $domain; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# Try a SMTP connection to 'mailhost' |
102
|
|
|
|
|
|
|
|
103
|
0
|
0
|
|
|
|
|
if(eval {require Net::SMTP}) |
|
0
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
{ foreach my $host (qw(mailhost localhost)) |
105
|
|
|
|
|
|
|
{ # hosts are local, so short timeout |
106
|
0
|
|
|
|
|
|
my $smtp = eval { Net::SMTP->new($host, Timeout => 5) }; |
|
0
|
|
|
|
|
|
|
107
|
0
|
0
|
|
|
|
|
if(defined $smtp) |
108
|
0
|
|
|
|
|
|
{ $domain = $smtp->domain; |
109
|
0
|
|
|
|
|
|
$smtp->quit; |
110
|
0
|
|
|
|
|
|
last; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Use internet(DNS) domain name, if it can be found |
116
|
|
|
|
|
|
|
$domain = Net::Domain::domainname() |
117
|
0
|
0
|
0
|
|
|
|
if !defined $domain && eval {require Net::Domain}; |
|
0
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
0
|
|
0
|
|
|
|
$domain ||= "localhost"; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub mailaddress(;$) |
124
|
0
|
0
|
|
0
|
1
|
|
{ $mailaddress = shift if @_; |
125
|
|
|
|
|
|
|
|
126
|
0
|
0
|
|
|
|
|
return $mailaddress |
127
|
|
|
|
|
|
|
if defined $mailaddress; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# Get user name from environment |
130
|
0
|
|
|
|
|
|
$mailaddress = $ENV{MAILADDRESS}; |
131
|
|
|
|
|
|
|
|
132
|
0
|
0
|
0
|
|
|
|
unless($mailaddress || $^O ne 'MacOS') |
133
|
0
|
|
|
|
|
|
{ require Mac::InternetConfig; |
134
|
|
|
|
|
|
|
|
135
|
3
|
|
|
3
|
|
24
|
no strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
505
|
|
136
|
0
|
|
|
|
|
|
Mac::InternetConfig->import; |
137
|
0
|
|
|
|
|
|
$mailaddress = $InternetConfig{kICEmail()}; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
0
|
|
|
|
$mailaddress ||= $ENV{USER} || $ENV{LOGNAME} || eval {getpwuid $>} |
|
|
|
0
|
|
|
|
|
141
|
|
|
|
|
|
|
|| "postmaster"; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# Add domain if it does not exist |
144
|
0
|
0
|
|
|
|
|
$mailaddress .= '@' . maildomain |
145
|
|
|
|
|
|
|
if $mailaddress !~ /\@/; |
146
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
|
$mailaddress =~ s/(^.*<|>.*$)//g; |
148
|
0
|
|
|
|
|
|
$mailaddress; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
1; |