2 ################################################################################
4 # File: $RCSfile: weed,v $
5 # Revision: $Revision: 1.1 $
6 # Description: Weed out obvious spams from the mail store
7 # Author: Andrew@DeFaria.com
8 # Created: Mon Feb 19 22:37:30 CST 2007
9 # Modified: $Date: 2013/06/12 14:05:47 $
12 # (c) Copyright 2007, Andrew@DeFaria.com, all rights reserved.
14 ################################################################################
18 use lib $FindBin::Bin, '/opt/clearscm/lib';
27 my $mailstore = "/var/spool/exim/input";
30 display "Usage: weed: [ -v|erbose ] [ -d|ebug ]\n";
32 display " -v|erbose\tTurn on verbose mode (default off)";
33 display " -d|ebug\tTurn on debug mode (default off)";
34 display " -u|sage\tDisplay this usage message";
39 my $userid = "andrew";
41 my ($username, $user_email) = SetContext $userid;
43 sub GetEmailsInSpool () {
46 # Open mailstore directory. Note must have read access to this
47 # directory and the files in this directory. IOW we probably need to
48 # be running as root. Also, the MTA should not be running because we
49 # may be removing files...
50 opendir MAILSTORE, $mailstore
51 or error "Unable to open mailstore $mailstore - $!", 1;
54 my @msgs = grep {!/^\./} readdir MAILSTORE;
56 # Don't need the directory opened anymore...
59 # Select only the "-H" header files...
60 @msgs = grep {/-H$/} @msgs;
62 # Now search for "From:" in the header files, extract email address
63 # and put into return hash.
69 my @lines = ReadFile "$mailstore/$msg_nbr";
75 if ($sender =~ /<(\S*)@(\S*)>/) {
76 $sender = lc ("$1\@$2");
77 } elsif ($sender =~ /(\S*)@(\S*)\ /) {
78 $sender = lc ("$1\@$2");
79 } elsif ($sender =~ /(\S*)@(\S*)/) {
80 $sender = lc ("$1\@$2");
83 $emails {$msg_nbr} = $sender;
88 verbose scalar (keys (%emails)) . " emails to process";
93 sub RemoveEmailInSpool ($) {
96 my $datafile = "$mailstore/${msg_nbr}-D";
97 my $header_file = "$mailstore/${msg_nbr}-H";
98 my $j_file = "$mailstore/${msg_nbr}-J";
102 or error "Unable to unlink $datafile - $!";
105 if (-f $header_file) {
107 or error "Unable to unlink $header_file - $!";
112 or error "Unable to unlink $j_file - $!";
114 } # RemoveEmailInSpool
116 sub FilterEmails (%) {
121 foreach (sort (keys (%emails))) {
128 my $sender = $emails {"${msg_nbr}-H"};
130 if ($sender eq "maps\@defaria.com" ||
131 $sender eq "mailer-daemon\@defaria.com" ||
132 $sender =~ /^defaria.*\@defaria.com$/) {
133 verbose "Removing email $msg_nbr with sender of $sender";
134 RemoveEmailInSpool $msg_nbr;
137 # Need to get $sender_long. Should call ReadMsg from maps. Have to
138 # reorganize how this program flows...
140 # } elsif ($sender eq $user_email and
141 # (lc ($sender_long) !~ lc ("\"$username\" <$user_email>") and
142 # lc ($sender_long) !~ lc ("$username <$user_email>"))) {
143 # RemoveEmailInSpool $msg_nbr;
145 } elsif (OnNulllist $sender) {
146 verbose "Nulllist $msg_nbr ($sender)";
149 RemoveEmailInSpool $msg_nbr;
159 my $result = GetOptions (\%opts,
160 "usage" => sub { Usage },
161 "verbose" => sub { set_verbose },
162 "debug" => sub { set_debug },
165 my $removed = FilterEmails (GetEmailsInSpool ());
167 verbose "$removed emails removed from the mail store";