1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
# Mail::Util.pm
#
# Copyright (c) 1995-2001 Graham Barr <gbarr@pobox.com>. All rights reserved.
# Copyright (c) 2002-2003 Mark Overmeer <mailtools@overmeer.net>
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
package Mail::Util;
use strict;
use vars qw($VERSION @ISA @EXPORT_OK);
use AutoLoader ();
use Exporter ();
BEGIN {
require 5.000;
$VERSION = "1.60";
*AUTOLOAD = \&AutoLoader::AUTOLOAD;
@ISA = qw(Exporter);
@EXPORT_OK = qw(read_mbox maildomain mailaddress);
}
1;
sub Version { $VERSION }
=head1 NAME
Mail::Util - mail utility functions
=head1 SYNOPSIS
use Mail::Util qw( ... );
=head1 DESCRIPTION
This package provides several mail related utility functions. Any function
required must by explicitly listed on the use line to be exported into
the calling package.
=head2 read_mbox( $file )
Read C<$file>, a binmail mailbox file, and return a list of references.
Each reference is a reference to an array containg one message.
=head2 maildomain()
Attempt to determine the current uers mail domain string via the following
methods
=over 4
=item * Look for the MAILDOMAIN enviroment variable, which can be set from outside the program.
=item * Look for a sendmail.cf file and extract DH parameter
=item * Look for a smail config file and usr the first host defined in hostname(s)
=item * Try an SMTP connect (if Net::SMTP exists) first to mailhost then localhost
=item * Use value from Net::Domain::domainname (if Net::Domain exists)
=back
=head2 mailaddress()
Return a guess at the current users mail address. The user can force
the return value by setting the MAILADDRESS environment variable.
=head1 AUTHOR
Graham Barr.
Maintained by Mark Overmeer <mailtools@overmeer.net>
=head1 COPYRIGHT
Copyright (c) 2002-2003 Mark Overmeer, 1995-2001 Graham Barr. All rights
reserved. This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
__END__
sub read_mbox {
my $file = shift;
my @mail = ();
my $mail = [];
my $blank = 1;
local *FH;
local $_;
open(FH,"< $file") or
do {
require Carp;
Carp::croak("cannot open '$file': $!\n");
};
while(<FH>) {
if($blank && /\AFrom .*\d{4}/) {
push(@mail, $mail) if scalar(@{$mail});
$mail = [ $_ ];
$blank = 0;
}
else {
$blank = m#\A\Z#o ? 1 : 0;
push(@{$mail}, $_);
}
}
push(@mail, $mail) if scalar(@{$mail});
close(FH);
return wantarray ? @mail : \@mail;
}
sub maildomain {
##
## return imediately if already found
##
return $domain
if(defined $domain);
##
## Get mail domain from environment
##
$domain = $ENV{MAILDOMAIN};
return $domain
if(defined $domain);
##
## Try sendmail config file if exists
##
local *CF;
local $_;
my @sendmailcf = qw(/etc
/etc/sendmail
/etc/ucblib
/etc/mail
/usr/lib
/var/adm/sendmail);
my $config = (grep(-r, map("$_/sendmail.cf", @sendmailcf)))[0];
if(defined $config && open(CF,$config)) {
my %var;
while(<CF>) {
if(my ($v, $arg) = /^D([a-zA-Z])([\w.\$\-]+)/) {
$arg =~ s/\$([a-zA-Z])/exists $var{$1} ? $var{$1} : '$'.$1/eg;
$var{$v} = $arg;
}
}
close(CF);
$domain = $var{j} if defined $var{j};
$domain = $var{M} if defined $var{M};
$domain = $1
if($domain && $domain =~ m/([A-Za-z0-9](?:[\.\-A-Za-z0-9]+))/ );
return $domain
if(defined $domain);
}
##
## Try smail config file if exists
##
if(open(CF,"/usr/lib/smail/config")) {
while(<CF>) {
if(/\A\s*hostnames?\s*=\s*(\S+)/) {
$domain = (split(/:/,$1))[0];
last;
}
}
close(CF);
return $domain
if(defined $domain);
}
##
## Try a SMTP connection to 'mailhost'
##
if(eval { require Net::SMTP }) {
my $host;
foreach $host (qw(mailhost localhost)) {
my $smtp = eval { Net::SMTP->new($host) };
if(defined $smtp) {
$domain = $smtp->domain;
$smtp->quit;
last;
}
}
}
##
## Use internet(DNS) domain name, if it can be found
##
unless(defined $domain) {
if(eval { require Net::Domain } ) {
$domain = Net::Domain::domainname();
}
}
$domain = "localhost"
unless(defined $domain);
return $domain;
}
sub mailaddress {
##
## Return imediately if already found
##
return $mailaddress
if(defined $mailaddress);
##
## Get user name from environment
##
$mailaddress = $ENV{MAILADDRESS};
unless ($mailaddress || $^O ne 'MacOS') {
require Mac::InternetConfig;
Mac::InternetConfig->import();
$mailaddress = $InternetConfig{kICEmail()};
}
$mailaddress ||= $ENV{USER} ||
$ENV{LOGNAME} ||
eval {getpwuid($>)} ||
"postmaster";
##
## Add domain if it does not exist
##
$mailaddress .= '@' . maildomain()
unless($mailaddress =~ /\@/);
$mailaddress =~ s/(^.*<|>.*$)//g;
$mailaddress;
}
|