[Mimedefang] Testing for the existence of a header

Nigel Allen dna at edrs.com.au
Wed Oct 1 03:15:36 EDT 2008


Given my already admitted unfamiliarity with Perl, could anyone please 
tell me what is going wrong with this (general hints and observations 
also happily and readily accepted)

The text file "/etc/staff_codes.txt) that is referenced below and that I 
am using for testing contains just two lines:

988034
988035

and the error message I get every time is:

Oct  1 17:06:55 sydsrv01 sendmail[21520]: m9176ro3021520: Milter: data, 
reject=554 5.7.1 You must enter a valid user code or a valid strata plan 
number. You entered 988034 which is an invalid code

Here is the portion of filter_begin:

    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) {
                   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");
                }
             }
          }
       }
    }
    close(IN);

Thanks to all who have assisted so far. I'm flying solo and suspect I 
need more than one pair of eyes.

Rgds

Nigel.



On 1/10/2008 12:49 PM, David F. Skoll wrote:
> Joseph Brennan wrote:
>
> [code that slurps headers into a hash]
>
>> if (/^(\S+): (.*)/)
>
> I'm not sure a single space is required after the header name.
> I would write this as:
>
>   if (/^(\S+):\s*(.*)/)
>
> The greediness of "*" means that \s* will eat up all whitespace
> before any (possible) non-whitespace value.
>
>> Taking your example, then the contents of a header "X-ABC:" would be
>> stored in $Header{'x-abc'}.  You could test both existence:
>
>>     if ($Header{'x-abc'})
>
> I would write:  if (exists($Header{'x-abc'}))
>
> It is possible, though maybe improbable, to have something like:
>
>    X-ABC: 0
>
> and Perl treats that as false.
>
>> and contents:
>
>>     if ($Header{'x-abc'} =~ /some string/)
>
> If you are using "use strict; use warnings;" this might elicit a
> warning.  I'd write it as:
>
>       if (defined($Header{'x-abc'}) && ($Header{'x-abc'} =~ /some string/))
>
> (Advanced Perl hackers may choose to avoid auto-vivification with an
> extra exists() test first...)
>
> Regards,
>
> David.
> _______________________________________________
> NOTE: If there is a disclaimer or other legal boilerplate in the above
> message, it is NULL AND VOID.  You may ignore it.
>
> Visit http://www.mimedefang.org and http://www.roaringpenguin.com
> MIMEDefang mailing list MIMEDefang at lists.roaringpenguin.com
> http://lists.roaringpenguin.com/mailman/listinfo/mimedefang


More information about the MIMEDefang mailing list