[Mimedefang] Code fragment works only sometimes

alan premselaar alien at 12inch.com
Mon Jul 14 22:39:01 EDT 2003


On 7/15/03 12:16 PM, "Andrea Venturoli" <ml.ventu at flashnet.it> wrote:

...snip...

> Ok, I've tried these two. Since I'm not a perl guru, I've reported my code
> once again. Could you please give it another
> look?
> 
>   if 
> (open(HDR,"HEADERS")&&open(BLK,"/usr/local/netfence/local/etc/spam_recipients"
> ))
>   {
>       my(@lines)=<HDR>; my(@blk)=<BLK>;
>       close(HDR); close(BLK);
>       foreach $line (@lines)
>       {
>           foreach $blk (@blk)
>           {
>               if (rindex($line,$blk)!=-1)
>               {
>                   md_log($line);
>                   return action_bounce("Spam is not accepted");
>               }
>           }
>       }
>   }
> 
> 
> 
> 
> 
>> You could lowercase everything before comparing, so it's not
>> case-sensitive, if that's desirable.
> 
> For now it doesn't look as if it is needed, but just in case how would I do
> that?
> 

there are numerous ways of doing this in perl.  I, personally use a regex
search instead of using rindex, which can accomodate this on the fly.
(I just woke up and don't have my references nearby, so this might be
slightly incorrect. please to doublecheck)

so, the code might look like:

...
    foreach $blk (@blk) {
        chomp($blk);        # remove trailing \n

        if ($line =~ /$blk/i) {
            #do stuff here
        }
    }

...

which basically says "if $blk is found in $line, case insensitive, then..."

otherwise you could do something like

$line =~ tr/A-Z/a-z/;

and

$blk =~ tr/A-Z/a-z/;

after each of your foreach statements respectively.

there may even be an lcase() function
> 
> 
>> Do chomp($blk) each time to get rid of the newlines.
> 
> Sorry, I didn't understand this. Which newlines? Why?
> 
> 
In perl when dealing with text files, every line is separated by a newline
(\n).  chomp() will remove any trailing newline from a string.

> 
> Another possible improvement would be to read BLK only once, not for each
> message I parse. Can I do this? How?
> 

One advantage to re-reading BLK might be that you can update your blacklist
"on-the-fly" (so to speak), whereas, if you only read it once, then you'll
have to restart the filter (possibly MD) in order to get your changes.


hope this is helpful,

alan




More information about the MIMEDefang mailing list