#!/usr/bin/perl

###################################################################
#                    Basic Safelist Program                       #
# Kelly Sampson                                                   #
# www.jfkenterprisesgroup.com                                     #
#                                                                 #
# Last Modified Saturday, March 23, 2002 4:45 PM                  #
# Added bouncer.cgi                                               #
# Added vacation mode                                             #
#                                                                 #
# Last Modified   Wednesday, February 13, 2002 8:25 AM            #
# Added domain name banning                                       #
# Added illicit language filter                                   #
#                                                                 #
# Modified   Sunday, January 20, 2002 2:43 AM                     #
# Added txtcolor & lnkcolor variables                             #
# Created addys.txt                                               #
###################################################################
use CGI::Carp qw(fatalsToBrowser);
require "config.cgi";
use File::Path;
use Net::POP3;
($alias,$domain) = split(/\@/, $adress);
sub bouncer {
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$mon++;
$year = $year + 1900;
if ($sec < 10) { $sec = "0$sec"; }
if ($min < 10) { $min = "0$min"; }
if ($hour < 10) { $hour = "0$hour"; }
if ($mday < 10) { $mday = "0$mday"; }
if ($mon < 10) { $mon = "0$mon"; }
$date = "$mon$mday$year";
$time = "$hour:$min:$sec";
$count = 0;
$dcount = 0;
$remcount = 0;
if ( -e "$cgiroot/data/bouncers/failed.txt") {}
else {mkpath("$cgiroot/data/bouncers", 0, 0777)}
  if ( -e "$cgiroot/data/bouncers/$date/log.txt") {}
	else {
opendir (DIR, "$cgiroot/data/bouncers");
@files = grep { // } readdir(DIR);
close (DIR);
foreach $file (@files){
unless ($file eq "" || $file eq "." || $file eq ".."){
unlink "$cgiroot/data/bouncers/$file/log.txt";
rmdir "$cgiroot/data/bouncers/$file";
}}
mkpath("$cgiroot/data/bouncers/$date", 0, 0777);
unlink "$cgiroot/data/bouncers/failed.txt";
open (TEMP, ">>$cgiroot/data/bouncers/failed.txt") || die "Can't create new failed.txt!\n";
close TEMP;
chmod(0777,"$cgiroot/data/bouncers/failed.txt");
  }
open(DATA,">>$cgiroot/data/bouncers/$date/log.txt") || die "Can't open $date.txt!\n";
open (TEMP, "$cgiroot/data/bouncers/failed.txt")  || die "Can't open failed.txt!\n";
&LoginPOP3;
$messages = $pop->list;
foreach $msgid (keys %$messages) {
    $mymessage = $pop->get($msgid);
    $fecount = 0;
$deleteit = "no";  
    if($mymessage) {
        $count++;
        @mess = @$mymessage;
	foreach $line (@mess) {
	   if($line =~ m/Subject: /) {# finds match for "Subject:"
		$line =~ s/Subject: //;# removes "Subject:" from line
		chomp($line);
		@checkforthis = ('failure notice','failure delivery','Returned mail: see transcript for details','Returned mail: Service unavailable','Mail could not be delivered','Mail System Error - Returned Mail','Undeliverable Mail','Returned mail: User unknown', 'Mail delivery failed: returning message to sender','Delivery Status Notification (Failure)','Delivery Status Notification','Nondeliverable mail','Returned mail: Subscriber is over disk quota');
	       foreach $checkfor (@checkforthis) {
		    $dsubject = $line;
		    $dsubject =~ tr/A-Z/a-z/;
		    if ($dsubject =~ /$checkfor/i) { $returned = "yes"; 
				$deleteit = "yes";}
		}# close foreach $checkfor
	    }# close if($line =~ m/Subject: /)
	    if ((($line =~ /To: /) || ($line =~ /Reason: /)) && (($line !~ /$mailuser/i) && ($line !~ /$alias/i) && ($line !~ /Reply-To: /))){   
					@everything = split(/ /, $line);
	        foreach $thingy (@everything) {
	      	  if ($thingy =~ m/\@/) {	
		        $thingy =~ s/\r//g;
		        $thingy =~ s/\n//g;
			      $thingy =~ tr/A-Z/a-z/;
@bouncers = (<TEMP>);
foreach $bouncer(@bouncers){
chop $bouncer;
if ($bouncer =~	/$thingy/i){$bmatch = "yes";}# close if $bouncer...
}# close foreach $bouncer...	
if ($bmatch ne "yes"){
close TEMP;
open (TEMP, ">>$cgiroot/data/bouncers/failed.txt");
print TEMP "$thingy\n";
close TEMP;
open (TEMP, "$cgiroot/data/bouncers/failed.txt");
    &FindMember;
}# close if ($bmatch ne "yes")
			$fecount++;
		    }# close if ($thingy =~ m/\@/)
		}# close foreach $thingy
	    }# close if (($line =~ /Delivered-To: /)  
}# close foreach $line #done message scan
        if ($deleteit eq "yes") {		
	    $deleteit = "no";
	    $pop->delete($msgid); 
	    $dcount++; 
	}# close if ($deleteit eq "yes")
    }# close if($mymessage)
}# close foreach $msgid (keys %$messages)
&LogoffPOP3;
if ($count ne "0"){
  print DATA "$time - $count msgs found, $remcount user accts edited , and $dcount msgs deleted.\n";
  close(DATA);} 
return;
}
1;
#####################################################################
sub LoginPOP3 {

  $scount = 0;
  $uscount = 0;
  $dcount = 0;
  %messages = ();
  $pop = Net::POP3->new($mailserver) || &error("Could not connect to $mailserver mail server ($!)");
 $pop->login($mailuser, $mailpass) || &error("Could not login to $mailserver mail server ($!)");
  return; }
#####################################################################
sub LogoffPOP3 {
  $pop->quit();
  return; }
#####################################################################	
sub error {
  my $error = shift;
  print DATA "Error: $error";
exit; }
####################################################################
sub FindMember{
if ($thingy eq "") {return;}
opendir (DIR, "$cgiroot/data/members");
@files = grep { /.txt/ } readdir(DIR);
close (DIR);
foreach $file (@files){
open (MEMDATA, "$cgiroot/data/members/$file");flock(MEMDATA, 2);(@memlines) = (<MEMDATA>);flock(MEMDATA, 8);close (MEMDATA);
foreach $memline (@memlines){
($mempass,$memfirst,$memlast,$mememail,$memsub,$memjoined,$memlastsend,$memcode,$memconfirm,$status,$extra2,$extra3) = split (/\|/, $memline);($user,@junk) = split (/\.txt/, $file);
}# close foreach $memline (@memlines)
if ($memsub =~ /$thingy/i) {
$newstat = "vacation";
unlink "$cgiroot/data/members/$user.txt";
open (NFILE, ">$cgiroot/data/members/$user.txt");
print NFILE "$mempass|$memfirst|$memlast|$mememail|$memsub|$memjoined|$date|$memcode|$memconfirm|$newstat|$extra2|$extra3|\n";
close (NFILE);
$remcount = $remcount + 1 ;
}# close if ($memsub =~ /$thingy/i)...
}# close foreach $file...  
return;}# close sub
