#add the shebang if running on unix: #!/usr/local/bin/perl ########################################################## # SpamPop.pl ~~ version 3.02 ~~ rev. March 15, 2004 # ########################################################## use Mail::POP3Client; #perl mod for getting email require "spampopcfg.pl"; #configuration/setup file sub connect { #subroutine connects to mail server when asked by using &connect; #---------------------------------------------- $Client = new Mail::POP3Client( USER => "$userName", PASSWORD => "$passWord", HOST => "$mailServer" ); $authState = $Client->State; if ($authState eq 'AUTHORIZATION') { die "Invalid username or password.\n" } elsif ($authState eq 'DEAD') { die "Mail server unreachable or unavailable.\n" } } #open spam filter list and get items open(FILTER, "<$filterFile") or die "Cannot find $filterFile - no such file or invalid path\n"; @check = ; shift(@check); #remove first line (comment line) $Filter = join "",@check; #add individual lines together $Filter =~ s/\n+/\|/g; #remove new lines, blank lines and insert pipes $Filter =~ s/([^\w\s.|])/\\$1/g; #escape any special characters $Filter =~ s/^\|//g; #remove beginning pipe (if there is one) $Filter =~ s/\|$//g; #remove ending pipe (if there is one) close(FILTER); #open okay domains list and get items open(DOMAINS, "<$okayFile") or die "Cannot find $okayFile - no such file or invalid path\n"; @okdomains = ; shift(@okdomains); #remove first line (comment line) $Allow = join "",@okdomains; #add individual lines together $Allow =~ s/\n+/\|/g; #remove new lines, blank lines and insert pipes $Allow =~ s/([^\w\s.|])/\\$1/g; #escape any special characters $Allow =~ s/^\|//g; #remove beginning pipe (if there is one) $Allow =~ s/\|$//g; #remove ending pipe (if there is one) close(DOMAINS); #open connection to mail server &connect; #count the numbers of messages in inbox $NumMsg = $Client->Count; #loop through the messages and tag any matches for deletion for ($i = 1; $i<=$NumMsg; $i +=1) { #gets header information $Headers = $Client->Head($i); @HeadList = split(/\n/, $Headers); #gets list of "From" address contained in header info to run against okay domains #also assigns subject line to a variable to match various spam subject messages foreach $FromDomain (@HeadList) { if ($FromDomain =~ /^From/i) {$CheckFrom = $FromDomain} if ($FromDomain =~ /^Subject\:/i) {$Subject = $FromDomain} } #part of email to check (with failsafe default to head and body) if ($searchType == "1") {$eMail = $Client->Head($i);} elsif ($searchType == "2") {$eMail = $Client->Body($i);} else {$eMail = $Client->HeadAndBody($i);} #match filter list - weed out okay domains #this section: ($Subject =~ /(Re|re|RE)\:\s[A-Z]{3,}\,/) matches emails with gibberish in subject line if ((($eMail =~ /($Filter)/i) || ($Subject =~ /(Re|re|RE)\:\s[A-Z]{3,}\,/) || ($Subject =~ /$emPrefix/i)) && (!($CheckFrom =~ /($Allow)/i))) { #shows which emails are being deleted - makes end-user more comfortable print "\n\n"; foreach $Line (@HeadList) { if ($Line =~ /^From/i) {print $Line . "\n";} if ($Line =~ /^Subject/i) {print $Line . "\n\n";} } #run program in interactive mode ############################################# if ($interActive == "1") { print "this email appears to be spam... delete? (Y/N/Q): "; $userIn = ; #wait for input chomp($userIn); #strip off return $userIn =~ tr/A-Z/a-z/; #let user use Y or y (not case sensitive) if ($userIn eq "y") {$Client->Delete($i);} #if yes - tag email for deletion if ($userIn eq "q") {last} #if quit - break out of loop } #otherwise assume answer is no and go on to next email #don't run interactively - just report what is happening ############################################# else { print "flagged as spam: deleting...\n"; $Client->Delete($i); #tag email for deletion } #close loops }} #close connection so tagged messages will be deleted $Client->Close; #if there are no messages stop, else keep going if ($NumMsg > 0) { #open connection and count number of messages left after deletion &connect; #get number of messages left (not spam) $NewMsg = $Client->Count; $Client->Close; #subtract number of original messages from messages left $DelMsg = $NumMsg - $NewMsg; #print out based on what's happening print "\n\nsearched $NumMsg email(s).\n"; print "deleted $DelMsg.\n"; } else { print "\nThere are no messages at this time.\n" } #leave window open so end-user can read screen sleep($sleepTimer);