File: //proc/self/root/scripts.20110531.215904.25158/addfpmail
#!/usr/bin/perl
# cpanel - addfpmail Copyright(c) 2010 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
BEGIN { unshift @INC, '/usr/local/cpanel'; }
use strict;
use Cpanel::AcctUtils::Owner ();
use Cpanel::AcctUtils::DomainOwner ();
my $matchdomain;
my $quiet = 0;
if ( @ARGV && $ARGV[0] =~ m/quiet/i ) {
$quiet = 1;
shift @ARGV;
}
if ( @ARGV && $ARGV[0] =~ /\-\-domain/i ) {
shift @ARGV;
$matchdomain = shift @ARGV;
}
print "Searching for domains without mail extensions...\n" if !$quiet;
my $reseller = $ARGV[0];
my $fpv = !-e "/usr/local/frontpage/version5.0" ? 4 : 5;
my @files;
opendir( my $fp_fh, '/usr/local/frontpage' );
while ( my $fname = readdir($fp_fh) ) {
if ( $fname =~ /\.cnf$/ ) {
push @files, '/usr/local/frontpage/' . $fname;
}
}
closedir($fp_fh);
my @add;
my @addsender;
foreach my $file (@files) {
if ( $matchdomain && $file !~ /\/(?:www\.)?\Q$matchdomain\E/ ) { next(); }
next if -l $file;
if ( ( stat(_) )[7] > 10000 ) {
cleanupfpfile($file);
}
$file =~ m/([\w\-\.]+):80.cnf/i;
my $domain = $1;
my $good = 0;
my $hassender = 0;
my $dumpold = 0;
my $mscount = 0;
my $killsender = 0;
my $smscount = 0;
my $killsmtp = 0;
if ( open my $file_fh, '<', $file ) {
while ( my $line = readline $file_fh ) {
if ( $line =~ m/^MailSender:/i ) {
$mscount++;
if ( $mscount > 1 ) {
$dumpold = 1;
$hassender = 0;
$killsender = 1;
}
}
elsif ( $line =~ m/^SMTPHost:/i ) {
$smscount++;
if ( $smscount > 1 ) {
$dumpold = 1;
$good = 0;
$killsmtp = 1;
}
}
if ( $line =~ m/Sendmail/i && $line =~ m/MailSender/i ) {
print "Dumping Old settings\n";
$dumpold = 1;
}
if ( $fpv == 4 ) {
if ( $line =~ m/Sendmail/i ) {
$good = 1;
}
if ( $line =~ m/MailSender/i ) {
$hassender = 1;
}
}
else {
if ( $line =~ m/MailSender/i && $line =~ m/\@www\./ ) {
$dumpold = 1;
}
if ( $line =~ m/MailSender/i && $line !~ m/\@www\./ ) {
if ( !$dumpold ) { $hassender = 1; }
}
if ( $line =~ m/Sendmail/i && $line !~ m/\%r/ ) {
if ( !$dumpold ) { $good = 1; }
}
if ( $line =~ m/Sendmail/i && $line =~ m/\%r/ ) {
$dumpold = 1;
}
}
}
close FILE;
if ($dumpold) {
cleanupfpfile( $file, 1, $killsender, $killsmtp );
}
if ( !$good ) {
push @add, $file;
}
if ( !$hassender ) {
push @addsender, $file;
}
}
}
foreach my $file (@add) {
$file =~ m/([\w\-\.]+):(\d+).cnf/i;
my $domain = $1;
my $owner;
$domain =~ s/^www\.//;
my $user = Cpanel::AcctUtils::DomainOwner::getdomainowner( $domain, { 'default' => '' } );
next if !$user;
my $owner = Cpanel::AcctUtils::Owner::getowner($user);
next if !$user;
next if ( $reseller && $reseller ne 'root' && $reseller ne $owner );
if ( open my $fp_fh, '>>', $file ) {
print {$fp_fh} "SMTPHost:127.0.0.1\n";
if ( $fpv == 4 ) {
print {$fp_fh} "SendmailCommand:/usr/sbin/sendmail %r\n";
}
else {
print {$fp_fh} "SendmailCommand:/usr/sbin/sendmail\n";
}
close $fp_fh;
print "Added FP Mail for $domain ($user)\n" if !$quiet;
}
else {
next;
}
}
foreach my $file (@addsender) {
$file =~ /([\w\-\.]+):(\d+).cnf/i;
my $domain = $1;
$domain =~ s/^www\.//;
if ( open my $fp_fh, '>>', $file ) {
print {$fp_fh} "MailSender:webmaster\@${domain}\n";
close $fp_fh;
print "Added FP Mail Sender for $domain\n" if !$quiet;
}
else {
next;
}
}
if ( !$quiet ) {
print "Checking again to ensure all domains have been updated.....\n";
if ($matchdomain) {
system '/scripts/addfpmail', '--quiet', '--domain', $matchdomain;
}
else {
system '/scripts/addfpmail', '--quiet';
}
print "Search Complete! All domains now have frontpage mail!\n";
}
sub cleanupfpfile {
my ( $file, $killsendmail, $killsender, $killsmtp ) = @_;
my %FP;
print "$file has grown too big... cleaning up useless information...." if !$quiet;
if ( open my $file_fh, '<', $file ) {
while ( readline $file_fh ) {
chomp;
my ( $name, $value ) = split( /:/, $_ );
$FP{$name} = $value;
}
close $file_fh;
if ( open my $file_fh, '>', $file ) {
foreach my $key ( sort keys %FP ) {
if ( $killsendmail == 1 && $key =~ /^sendmailcommand$/i ) {
print "Nuking old sendmail style\n" if !$quiet;
next;
}
next if ( $killsender == 1 && $key =~ /^mailsender$/i );
next if ( $killsmtp == 1 && $key =~ /^smtphost$/i );
print {$file_fh} "$key:$FP{$key}\n";
}
close $file_fh;
}
}
print "Done\n";
}