Re: Safely executing your mail filter?

Jeff Bisbee <[email protected]> Mon, 19 Jan 2004 16:50:06 -0500
Newsgroups gmane.comp.lang.perl.modules.mail-audit
Message-ID <20040119215006.GB1733@biz>
* Fred Chagnon ([email protected]) wrote:
> Are you talking like syntax errors in the script, when a new rule is
> fat-fingered? I test this by simply executing the script from the command
> line. If all's good it should just wait on stdin.

Because of 'fat-fingering' when entering new rules into my filter, I created 
Email::Filter::Rules.  Here is a simple example of how it works

FILTER CODE (basic filter using Email::Filter::Rules)

  #!/usr/bin/perl -w
  use strict;
  use Email::Filter;
  use Email::Filter::Rules;

  my $maildir = '/home/jbisbee/mail/';
  my $msg = Email::Filter->new(emergency => $maildir . 'emergency');

  my $mail_lists = Email::Filter::Rules->new(
      rules => '/home/jbisbee/bin/mail_lists'
  );

  if (my $mail_list_folder = $mail_lists->apply_rules($msg)) {
      $msg->accept($maildir . $mail_list_folder);
  }

  $msg->accept($maildir . 'inbox');

RULES FILE (/home/jbisbee/bin/mail_lists)

  # -- RULE FORMAT -- has 5 elements
  # 
  #  1.  DESTINATION FOLDER 
  #  2.  <whitespace> 
  #  3.  Mail::Audit->$METHOD(S)
  #  4.  <whitespace> 
  #  5.  REGEX*

  #  * Its important to note that 'quotemeta' is called on 
  #    the regex that it matches.  So don't try anything fancy :)
  
  # Linux
  lists/linux/flux/linux        to:cc [email protected]
  lists/linux/flux/talk         to:cc [email protected]
  lists/linux/flux/website      to:cc [email protected]
  lists/linux/flux/announce     to    [email protected]
  
  # Linux - Mozilla
  lists/linux/mozilla-bugs      to    mozilla-bugs
  
  # Perl
  lists/perl/useperl            to:cc use-perl
  
  lists/perl/pm/southflorida    to:cc [email protected]
  
  # Word of the Day
  lists/word-of-the-day         from  [email protected]


I've screwed up my filter countless times and figured enough was 
enough, why even chance it.  Now I have a foolproof/simple way to 
add new rules.

Also because Email::Filter::Rules just returns a the mail folder to 
save it in, it works with either Mail::Audit or Email::Filter.

-- Jeff Bisbee / [email protected] / jbisbee.com