#!/tools/ns/bin/perl5
#
# $Id: email_cleanup.pl,v 1.2 1998/05/21 20:45:50 sheneman Exp $
#
##############################################################################
#
# The contents of this file are subject to the Netscape Public License Version
# 1.0 (the "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at http://www.mozilla.org/NPL/ 
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License. 
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are Copyright (C) 1998 Netscape
# Communications Corporation. All Rights Reserved.
#
##############################################################################
#
# email_cleanup.pl  (OH WHAT GROSS HACKY CODE I HAVE CREATED!!!!)
#
# This script will be used to e-mail all users who have expired but have 
#   (apparently) active mailboxes and LDAP/UNIX accounts.  This script will
#   also be used to mail users who are about to expire, based on specific
#   criteria.
#
# AUTHOR:  Luke Sheneman
#
# (C)opyright 1997, Sheneman Script Factory, Inc.
#

$| = 1; 			# flush all buffers immediately

$BASEPATH = "/u/sheneman/projects/cleanup/current";

$EMAIL        = "/usr/ucb/Mail -s \"Account Expiration Warning\"";
$LOGFILE      = "$BASEPATH/mail.log";

# the mail contacts
$NSCP_MAIL    = "kcaudill elent marichu pauline";
$IC_MAIL      = "champagn";
$VENDOR_MAIL  = "andreak";
$INTERIM_MAIL = "martak lisaliv";
$OEM_MAIL     = "gbeceren";

#$UNKNOWN_MAIL = "$IC_MAIL $VENDOR_MAIL $INTERIM_MAIL $OEM_MAIL";

$nscp_exp_file    = "nscp_expired.txt";
$ic_exp_file      = "ic_expired.txt";
$vendors_exp_file = "vendors_expired.txt";
$interim_exp_file = "interim_expired.txt";
$oem_exp_file     = "oem_expired.txt";
$unknown_exp_file = "unknown_expired.txt";

$nscp_two_file    = "nscp_two.txt";
$ic_two_file      = "ic_two.txt";
$vendors_two_file = "vendors_two.txt";
$interim_two_file = "interim_two.txt";
$oem_two_file     = "oem_two.txt";
$unknown_two_file = "unknown_two.txt";

sub get_record {
	$line = shift(@lines);
	($fluff, $user) = split(/:/,$line);
	chop($user);
	$user =~ s/^[ ]*//;

	$line  = shift(@lines);
	($fluff, $cn)   = split(/:/,$line);
	chop($cn);
	$cn =~ s/^[ ]*//;

	$line = shift(@lines);
	($fluff, $employeetype) = split(/:/,$line);
	chop($employeetype);
	$employeetype =~ s/^[ ]*//;

	$line  = shift(@lines);
	($fluff, $employeenumber)   = split(/:/,$line);
	chop($employeenumber);
	$employeenumber =~ s/^[ ]*//;

	$line = shift(@lines);
	($fluff, $manager) = split(/:/,$line);
	chop($manager);
	$manager =~ s/^[ ]*//;
	($manager,$fluff) = split(',',$manager);
	($fluff,$manager) = split(/=/,$manager);

	$line = shift(@lines);
	($fluff, $mailhost) = split(/:/,$line);
	chop($mailhost);
	$mailhost =~ s/^[ ]*//;

	$line = shift(@lines);
	($fluff, $password) = split(/:/,$line);
	chop($password);
	$password =~ s/^[ ]*//;

	$line  = shift(@lines);
	($fluff, $expiration)   = split(/:/,$line);
	chop($expiration);
	$expiration =~ s/^[ ]*//;

	shift(@lines);  # nothing
	shift(@lines);  # nothing
}


sub print_header {

	print(OUT "\n\n     IMPORTANT WARNING.  PLEASE READ THIS MAIL CAREFULLY.\n");
	print(OUT "     ------------------------------------------------------\n\n\n");
	print(OUT "UID:        $user\n");
	print(OUT "FULL NAME:  $cn\n");
	if($employeetype eq "Contractor") {
	    print(OUT "EMP TYPE:   Independent Contractor\n");
	} elsif ($employeetype eq "Employee") {
	    print(OUT "EMP TYPE:   Netscape Employee\n");
	} else {
	    print(OUT "EMP TYPE:   $employeetype\n");
	}

	if($employeenumber) {
	    print(OUT "EMP NUMBER: $employeenumber\n");
	} else {
	    print(OUT "EMP NUMBER: UNKNOWN\n");
	}

	print(OUT "MANAGER:    $manager\n");
	print(OUT "EXPIRATION: $expiration\n");

	return 0;
}


sub mail {
    $mail_destination = shift(@_);
    
    print("  Mailing $mail_destination\n");
    print(LOG "  Mailing $mail_destination\n");
    system("$EMAIL $mail_destination < /tmp/tmp_mail");

    return 0;
}


sub email_expired_employees {
    print(" *** Emailing expired employees with active LDAP/UNIX/MAIL accounts. ***\n");
    print(LOG " *** Emailing expired employees with active LDAP/UNIX/MAIL accounts. ***\n");

    @lines = `cat $BASEPATH/$nscp_exp_file`;

    $line = " ";
    while($line) {
	&get_record;
	
	if(!$user) {
	    last;
	}

	open(OUT, ">/tmp/tmp_mail");
	&print_header;
	close(OUT);

	system("cat $BASEPATH/nscp_expired.mail >> /tmp/tmp_mail");

	print(LOG "\n$user:\n");
	print("\n$user:\n");
	#&mail($NSCP_MAIL);
	&mail($user);

	if($manager && ($manager ne "jim" && $manager ne "jimb") ) {
	    &mail($manager);
	}

	sleep(1);
    }
    
    print("Done.\n\n");
}




sub email_expired_ic {
    print(" *** Emailing expired independent contractors with active LDAP/UNIX/MAIL accounts. ***\n");
    print(LOG " *** Emailing expired independent contractors with active LDAP/UNIX/MAIL accounts. ***\n");

    @lines = `cat $BASEPATH/$ic_exp_file`;

    $line = " ";
    while($line) {
	&get_record;
	
	if(!$user) {
	    last;
	}

	open(OUT, ">/tmp/tmp_mail");
	&print_header;
	close(OUT);

	system("cat $BASEPATH/ic_expired.mail >> /tmp/tmp_mail");

	print(LOG "\n$user:\n");
	print("\n$user:\n");
	&mail($IC_MAIL);
	&mail($user);

	if($manager && ($manager ne "jim" && $manager ne "jimb") ) {
	    &mail($manager);
	}
    }

    print("Done.\n\n");
}


sub email_expired_vendors {
    print(" *** Emailing expired vendors with active LDAP/UNIX/MAIL accounts. ***\n");
    print(LOG " *** Emailing expired vendors with active LDAP/UNIX/MAIL accounts. *** \n");

    @lines = `cat $BASEPATH/$vendors_exp_file`;

    $line = " ";
    while($line) {
	&get_record;
	
	if(!$user) {
	    last;
	}

	open(OUT, ">/tmp/tmp_mail");
	&print_header;
	close(OUT);

	system("cat $BASEPATH/vendors_expired.mail >> /tmp/tmp_mail");

	print(LOG "\n$user:\n");
	print("\n$user:\n");
	&mail($VENDOR_MAIL);
	&mail($user);
	
	if($manager && ($manager ne "jim" && $manager ne "jimb") ) {
	    &mail($manager);
	}
    }

    print("Done.\n\n");
}


sub email_expired_interim {
    print(" *** Emailing expired interim contractors/temps with active LDAP/UNIX/MAIL accounts. ***\n");
    print(LOG " *** Emailing expired interim contractors/temps with active LDAP/UNIX/MAIL accounts. ***\n");

    @lines = `cat $BASEPATH/$interim_exp_file`;

    $line = " ";
    while($line) {
	&get_record;
	
	if(!$user) {
	    last;
	}

	open(OUT, ">/tmp/tmp_mail");
	&print_header;
	close(OUT);

	system("cat $BASEPATH/interim_expired.mail >> /tmp/tmp_mail");

	print(LOG "\n$user:\n");
	print("\n$user:\n");
	&mail($INTERIM_MAIL);
	&mail($user);

	if($manager && ($manager ne "jim" && $manager ne "jimb") ) {
	    &mail($manager);
	}
    }

    print("Done.\n\n");
}



sub email_expired_oem {
    print(" *** Emailing expired OEM Partners with active LDAP/UNIX/MAIL accounts. ***\n");
    print(LOG " *** Emailing expired OEM Partners with active LDAP/UNIX/MAIL accounts. ***\n");

    @lines = `cat $BASEPATH/$oem_exp_file`;

    $line = " ";
    while($line) {
	&get_record;
	
	if(!$user) {
	    last;
	}

	open(OUT, ">/tmp/tmp_mail");
	&print_header;
	close(OUT);

	system("cat $BASEPATH/oem_expired.mail >> /tmp/tmp_mail");

	print(LOG "\n$user:\n");
	print("\n$user:\n");
	&mail($OEM_MAIL);
	&mail($user);

	if($manager && ($manager ne "jim" && $manager ne "jimb") ) {
	    &mail($manager);
	}
    }

    print("Done.\n\n");
}


sub email_expired_unknown {
    print(" *** Emailing expired accounts that are of unknown employee type with active LDAP/UNIX/MAIL accounts. ***\n");
    print(LOG " *** Emailing expired accounts that are of unknown employee type with active LDAP/UNIX/MAIL accounts. ***\n");

    @lines = `cat $BASEPATH/$unknown_exp_file`;

    $line = " ";
    while($line) {
	&get_record;
	
	if(!$user) {
	    last;
	}

	open(OUT, ">/tmp/tmp_mail");
	&print_header;
	close(OUT);

	system("cat $BASEPATH/unknown_expired.mail >> /tmp/tmp_mail");
	#system("$EMAIL cleanup < /tmp/tmp_mail");
	#system("$EMAIL $UNKNOWN_MAIL < /tmp/tmp_mail");

	print(LOG "\n$user:\n");
	print("\n$user:\n");
	#&mail($UNKNOWN_MAIL);
	&mail($user);

	if($manager && ($manager ne "jim" && $manager ne "jimb") ) {
	    &mail($manager);
	}
    }

    print("Done.\n\n");
}



sub email_two_employees {
    print(" *** Emailing NSCP employees who will expire in 2 weeks or less. ***\n");
    print(LOG " *** Emailing NSCP employees who will expire in 2 weeks or less. ***\n");

    @lines = `cat $BASEPATH/$nscp_two_file`;

    $line = " ";
    while($line) {
	&get_record;
	
	if(!$user) {
	    last;
	}

	open(OUT, ">/tmp/tmp_mail");
	&print_header;
	close(OUT);

	system("cat $BASEPATH/nscp_two.mail >> /tmp/tmp_mail");

	print(LOG "\n$user:\n");
	print("\n$user:\n");
	#&mail($NSCP_MAIL);
	&mail($user);

	if($manager && ($manager ne "jim" && $manager ne "jimb") ) {
	    &mail($manager);
	}
    }
    

    print("Done.\n\n");
}


sub email_two_ic {
    print(" *** Emailing independent contractors who will expire in 2 weeks or less. *** \n");
    print(LOG " *** Emailing independent contractors who will expire in 2 weeks or less. *** \n");

    @lines = `cat $BASEPATH/$ic_two_file`;

    $line = " ";
    while($line) {
	&get_record;
	
	if(!$user) {
	    last;
	}

	open(OUT, ">/tmp/tmp_mail");
	&print_header;
	close(OUT);

	system("cat $BASEPATH/ic_two.mail >> /tmp/tmp_mail");

	print(LOG "\n$user:\n");
	print("\n$user:\n");
	&mail($IC_MAIL);
	&mail($user);

	if($manager && ($manager ne "jim" && $manager ne "jimb") ) {
	    &mail($manager);
	}
    }
    
    print("Done.\n\n");
}


sub email_two_vendors {
    print(" *** Emailing vendors who will expire in 2 weeks or less. ***\n");
    print(LOG " *** Emailing vendors who will expire in 2 weeks or less. ***\n");

    @lines = `cat $BASEPATH/$vendors_two_file`;

    $line = " ";
    while($line) {
	&get_record;
	
	if(!$user) {
	    last;
	}

	open(OUT, ">/tmp/tmp_mail");
	&print_header;
	close(OUT);

	system("cat $BASEPATH/vendors_two.mail >> /tmp/tmp_mail");

	print(LOG "\n$user:\n");
	print("\n$user:\n");
	&mail($VENDOR_MAIL);
	&mail($user);

	if($manager && ($manager ne "jim" && $manager ne "jimb") ) {
	    &mail($manager);
	}
    }
    

    print("Done.\n\n");
}



sub email_two_interim {

    print(" *** Emailing interim hires who will expire in 2 weeks or less. ***\n");
    print(LOG " *** Emailing interim hires who will expire in 2 weeks or less. ***\n");

    @lines = `cat $BASEPATH/$interim_two_file`;

    $line = " ";
    while($line) {
	&get_record;
	
	if(!$user) {
	    last;
	}

	open(OUT, ">/tmp/tmp_mail");
	&print_header;
	close(OUT);

	system("cat $BASEPATH/interim_two.mail >> /tmp/tmp_mail");

	print(LOG "\n$user:\n");
	print("\n$user:\n");
	&mail($INTERIM_MAIL);
	&mail($user);

	if($manager && ($manager ne "jim" && $manager ne "jimb") ) {
	    &mail($manager);
	}
    }
    
    print("Done.\n\n");
}


sub email_two_oem{

    print(" *** Emailing OEM Partners who will expire in 2 weeks or less. ***\n");
    print(LOG " *** Emailing OEM Partners who will expire in 2 weeks or less. ***\n");

    @lines = `cat $BASEPATH/$oem_two_file`;

    $line = " ";
    while($line) {
	&get_record;
	
	if(!$user) {
	    last;
	}

	open(OUT, ">/tmp/tmp_mail");
	&print_header;
	close(OUT);

	system("cat $BASEPATH/oem_two.mail >> /tmp/tmp_mail");

	print(LOG "\n$user:\n");
	print("\n$user:\n");
	&mail($OEM_MAIL);
	&mail($user);

	if($manager && ($manager ne "jim" && $manager ne "jimb") ) {
	    &mail($manager);
	}
    }
    
    print("Done.\n\n");
}



sub email_two_unknown {

    print(" *** Emailing employees of UNKNOWN type who will expire in 2 weeks or less. ***\n");
    print(LOG " *** Emailing employees of UNKNOWN type who will expire in 2 weeks or less. ***\n");

    @lines = `cat $BASEPATH/$unknown_two_file`;

    $line = " ";
    while($line) {
	&get_record;
	
	if(!$user) {
	    last;
	}

	open(OUT, ">/tmp/tmp_mail");
	&print_header;
	close(OUT);

	system("cat $BASEPATH/unknown_two.mail >> /tmp/tmp_mail");

	print(LOG "\n$user:\n");
	print("\n$user:\n");
	#&mail($UNKNOWN_MAIL);
	&mail($user);

	if($manager && ($manager ne "jim" && $manager ne "jimb") ) {
	    &mail($manager);
	}
    }
    
    print("Done.\n\n");
}

# open the log here
open(LOG, ">$LOGFILE");

# print the header for this mailing
$date = `date`;
chop $date;

print("\n\n\n\n ***---> NEW MAILING ($date) <---***\n\n");
print(LOG "\n\n\n\n ***---> NEW MAILING ($date) <---***\n\n");

# email all expired employees who are terminated in PeopleSoft
#&email_expired_employees;

#email all expired independent contractors who are terminated in PeopleSoft
&email_expired_ic;

#email all expired vendors who are terminated in PeopleSoft
&email_expired_vendors;

#email all expired interim hires who are terminated in PeopleSoft
&email_expired_interim;

#email all OEM Partners who are terminated in PeopleSoft
&email_expired_oem;

#email all expired UNKNOWN hires who are terminated in PeopleSoft
&email_expired_unknown;

#email all Netscape employees who will expire in 2 weeks
#&email_two_employees;

#email all independent contractors who will expire in 2 weeks
&email_two_ic;

#email all vendors who will expire in 2 weeks
&email_two_vendors;

#email all interim hires who will expire in two weeks
&email_two_interim;

#email all OEM Partners who will expire in two weeks
&email_two_oem;

#email all employees of unknown types who will expire in 2 weeks
&email_two_unknown;

print(LOG "\n\n ***---> MAILING COMPLETE <---***\n\n");

# mail me a message saying that its done
system("echo \"LDAP PeopleSoft Mailing Complete\" > /tmp/tmp_mail");
system("date >> /tmp/tmp_mail");
&mail("sheneman");

# close the log here
close(LOG);














