[Mimedefang] Testing for the existence of a header

Steffen Kaiser skmimedefang at smail.inf.fh-bonn-rhein-sieg.de
Wed Oct 1 06:10:46 EDT 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, 1 Oct 2008, Nigel Allen wrote:

>   if (open(IN,"<./HEADERS")) {
>      while(<IN>) {
>         chomp;
>         if (/^(\S+): (.*)/) {
>            my $label = $1;
>            my $data  = $2;
>            $label = lc($label);
>            $Header{$label} = $data;
>            if (exists($Header{'x-strata-plan'})) {
>               my $spno = $Header{'x-strata-plan'};
>               open CODES, "</etc/staff_codes.txt";
>               my @codes = <CODES>;
>               for (@codes) {
$_ still containts the newline character, that's why there is the chomp 
after the while(<IN>).
Also, the while() parses all headers into the Headers hash, hence,
the test has to be moved below the while block.
>                  if ($_ =~ /$spno/){
>                     my $Is_Staff = ($_ =~ /$spno/);
>                  }
>               }

>               close(CODES);
>               # if it's a valid staff code bail out
>               if ($Is_Staff) {
>                  last;
>               }
>               # pad with spaces to left to at least 6 characters
>               my $pad_spno = sprintf("%6s", $spno);
>               $pad_spno =~ s/ /\\ /g;
>               if  (! -d $pad_spno) {
>                  action_bounce ("You must enter a valid user code or a valid 
> strata plan number. You entered $spno which is an invalid code");
Missing return before action_bounce().
>               }
>            }
>         }
>      }
>   }
>   close(IN);
>

However: How often does the file change? Wouldn't it better to pre-parse 
the content during the init phase? A MIMEDefang instance's lifetime is 
limited by number of actions passed to it, hence, unless you have a very
low-volume host, the instances re-init in regular intervals anyway.
In this case it would be more efficient to use a HASH, e.g.:
# init hash in filter_initialize()
# or near top of script
my %codes = ( );	# stores all staff IDs
if(open(CODES, '<', '/etc/staff')) {
  while(<>) {
   chop; # remove last char, usually newline
   $codes{$_} = 1;
  }
  close CODES;
}

in filter function after while(<IN>) block - or better after close IN:

if (exists($Header{'x-strata-plan'})) {
    if(! exists($codes{$Header{'x-strata-plan'}})) {
 	# not staff
 	return action_bounce("...");
    }
    # is staff
} else {
    # header does not exist
}

Bye,

- -- 
Steffen Kaiser
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFI40yo5ThHZhj8SBwRAlHJAJ9+IhfyfBQalEGhoGEhrj6+I7r38QCfaY8/
xVzVncxFquoL0mNRN4rqMUk=
=NnW+
-----END PGP SIGNATURE-----



More information about the MIMEDefang mailing list