procmail
[Top] [All Lists]

Re: Searchhing for words in external word list?

2008-12-11 10:28:05
On Thu, 11 Dec 2008, Dave Wood wrote:
I would like to know if it's possible to search for words contained in an
external file. Something like:
BADWORDS=`cat $HOME/.badwords`

You need something easy, which automaticly runs. The following is a
bit complicated to look at, but running it is simplicity.
All you do is update the file $HOME/mailfilter.whitelist .....
Making it automatic requires several pieces of code....

#########################################################################
######################### In procmailrc:
#########################################################################
# This code insures that expensive recompiling of the lists only occurs
# when they have actually changed. Users's filttest.recipe must be newest.
# Note upgrades to the system files (including this one!) will trigger an
# update too!
# SYSTEMFILE can be any system diretory you like, /etc, /cgi-bin....
SYSTEMFILE="/etc/mail"
:1 c
? ( shudbeone=`ls -t $HOME/mailfilter.whitelist $HOME/mailfilter.blacklist \
         $HOME/.mailfilter.recipe $SYSTEMFILE/filtgen.pl \
         awk '/^.*.mailfilter.recipe$/ {print NR}'` ;\
  if [ "$shudbeone" != "1" ] ; then \
    $SYSTEMFILE/filtgen.pl ;\
    cp $HOME/.mailfilter.recipe.new $HOME/.mailfilter.recipe ;\
    rm $HOME/.mailfilter.recipe.new ;\
    chmod go-rwx $HOME/.mailfilterrc ;\
    chmod go-rwx $HOME/mailfilter.whitelist ;\
    chmod go-rwx $HOME/.mailfilter.recipe ;\
  fi )
/dev/null
# Now include the user's recipe file which may or
# may not have just been updated by the above code.
# if you want to test the filter generation, comment out this line.
INCLUDERC=$HOME/.mailfilter.recipe
#########################################################################

#########################################################################
######################### The filtgen.pl program (adjust paths 4 ur sys):
#########################################################################
# again, note the use of \ in multi-line prints.....
#!/your/path/to/perl
$HOME=$ENV{'HOME'};
$ENV{'PATH'}='/bin'; ##YOUR SYSTEM PATHS
#
# THE NEW .mailfilter.recipe FILE WILL BE CREATED SEPARATELY THEN RENAMED
# AS THE "LIVE" FILE BY "filttest.recipe" AFTER THIS ROUTINE TERMINATES.
#
open (WH,">$HOME/.mailfilter.recipe.new");
# CONSTRUCT THE WHITELIST RECIPES
# SPECIFICATIONS MUST CONTAIN 5 CONSECUTIVE NON-BLANK CHARACTERS SOMEWHERE
# MATCHING MAIL IS DIRECTED TO THE USER'S DEFAULT MAILBOX
#
# START WITH A SUBJECT THAT IS A SYSTEM BYPASS FOR ADMIN MAIL....

print WH "\n# WHITELIST RECIPE" ;
print WH "\n:0" ;
print WH "\n* ^(From|Subject).*\\/(ADMIN_MAIL";
if (open (RH,"$HOME/mailfilter.whitelist")) {
   while (<RH>) {
      chomp($_) ;
      $SAFEREP=quotemeta($_) ;  # add escaping to special chars...
      $SAFEREP=~s/\\\t//g;  # remove escaped tabs
      $SAFEREP=~s/\t//g;    # remove tabs
      $SAFEREP=~s/\\</</g;  # remove escaping on <
      $SAFEREP=~s/\\>/>/g;  # remove escaping on >
      $SAFEREP=~s/\\\r//g;  # remove CR from files uploaded from windows
# The test for 'E-mail Addresses' was thrown in because I had users
# uploading their outlook addressbooks, with those useless lines...
      if ( ( $_ =~ /^.*[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9].*$/  )
           && ( $_ !~ /E\-mail\ Address\(es\)\:/ )
           && ( length($SAFEREP) < 70 ) ) {
         $SAFEREP=~s/^(\\ )*//; # remove leading spaces
         print WH "|\\\n${SAFEREP}";
      }
   }
   close(RH) ;
}
print WH ")" ;
print WH "\n{\
WLMATCH=\$MATCH\
:0fw\
* ^Subject: \\/.*";
# Before delivering, insert a whitelisted header....
# If you want to flag your whitelisted mail with a subject line tag...
if ( $TAGSUBJECT =~ /YES/ ) {
   print WH "\
| formail -I\"Subject: [WL] \$MATCH\" -I\"X-Whitelist-Match: \$WLMATCH\"";
} else {
   print WH "\
| formail -I\"X-Whitelist-Match: \$WLMATCH\"";
}
print WH "\
\
:0\
\$DEFAULT\
}\n" ;

######################################################################
And that's it. For the sake of brevity I've omitted the code for
generating the blacklist, but the principle is the same, just with
slightly different actions at the end (you can deliver to a folder
other than default or just /dev/null the mail if you are confident).
Be sure to NOT use the 'ADMIN_MAIL' on the blacklist spec! LOL

As written this will work for *all* users if installed in /etc/procmailrc.
To make use of it, the user simply creates a whitelist or blacklist file
with one entry. Put in a personal rc just for your own use (in which case,
modify the SYSTEMFILE location appropriately)....

-- Hope this makes it through the list without losing characters....

- Charles

____________________________________________________________
procmail mailing list   Procmail homepage: http://www.procmail.org/
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail