#!/usr/bin/perl

use strict;
use warnings;
my ($line, @array, $spammer);
my $mainlog = "/var/log/exim/mainlog";
my $counter = 0;
my $counter_tally = 0;
# The following variable determines how often to update the display
my $counter_inc = 1000;

sub usage {
  print "clean_smtpauth.pl <email address> [clean]\n";
  exit 1;
}

sub clean_queue {
  $spammer = $ARGV[0];
  open LOG, $mainlog or return "Could not open $mainlog: $!";
    while ($line = <LOG>) {
      if ($line =~ /<= $spammer/) {
        @array = split(/ +/, $line);
	if ($array[9] =~ /A=.*$spammer$/) {
	  if ((defined $ARGV[1]) && ($ARGV[1] == "clean")) {
			system "/usr/local/bin/exim -Mrm $array[2]";
		} else {
			print "Did not clean message: $array[2]\n";
		}
	  $counter++; $counter_tally++;
	}
	if ($counter >= $counter_inc) {
	  print $counter_tally, "\n";
	  $counter = 0;
	}
      }
    }
  print "Total number of messages from $spammer deleted: $counter_tally\n";
  close LOG;
}

usage() unless defined $ARGV[0];
usage() unless $ARGV[0] =~ /\@/;

clean_queue();
