/* a spam filter to be used as a PRMRECV.CMD exit from Post Road Mailer */
/* Copyright (c)1997 Kari Jackson for InnoVal Systems Solutions, Inc. */
call RxFuncAdd "SysLoadFuncs", "RexxUtil", "SysLoadFuncs"
signal on syntax name NoREXX
call SysLoadFuncs
signal off syntax
parse source temp
parse var temp . . program "." .
cfgfile=program||".Cfg"
if stream(cfgfile,"c","query exists")="" then exit
arg filename
inbasket=substr(filename,1,lastpos('\',filename)-1)
do i=1 while lines(filename)
   line.i=linein(filename)
   parse var line.i pop '09'x .
   POPname.i=inbasket'\'pop'.POP'
end
call lineout filename
POPname.0=i-1
if POPname.0<1 then exit
headerlines="NOBODY"
z=0
do while lines(cfgfile)
   line=translate(strip(linein(cfgfile)))
   if line="" then iterate
   if left(line,2)="/*" then iterate
   if left(line,13)="<HEADERLINES>" then do
      parse var line . "=" headerlines
      iterate
   end
   z=z+1
   parse upper var line spammer "09"x headers
   if strip(headers)="" then headers=headerlines
   headers=translate(headers," ",",")
   spammers.z=strip(spammer)||"09"x||space(headers,1)
end
spammers.0=z
call lineout cfgfile
if spammers.0=0 then exit
do v=1 to POPname.0
   infile=POPname.v
   killit=0
   r=0
   do while lines(infile)
      hdrline=translate(translate(linein(infile)," ","09"x))
      select
         when left(hdrline,1)=" " then msghdrs.r=msghdrs.r space(hdrline)
         when strip(hdrline)="" then nop
         otherwise do
            r=r+1
            msghdrs.r=space(hdrline)
         end
      end
      if strip(hdrline)="" then leave
   end
   msghdrs.0=r
   s=0
   do while lines(infile)
      s=s+1
      bodylines.s=translate(linein(infile))
   end
   bodylines.0=s
   do i=1 to spammers.0
      parse var spammers.i spammer "09"x headers
      if headers<>oldheaders & headers<>"NOBODY" & headers<>"ENTIRE" then do
         tags.0=0
         parsethem=headers
         do until parsethem=""
            parse var parsethem this parsethem
            tags.0=tags.0+1
            q=tags.0
            if right(this,1)<>":" then this=this||":"
            tags.q=this
         end
         oldheaders=headers
      end
      do n=1 to msghdrs.0
         parse var msghdrs.n tag content
         hit=0
         if headers<>"ENTIRE" & headers<>"NOBODY" then do
            do j=1 to tags.0
               if tag<>tags.j then iterate
               hit=1
               leave
            end
         end
         if (hit | headers="NOBODY" | headers="ENTIRE") & pos(spammer,content)>0 then killit=1
         if killit then leave
      end
      if killit then leave
      if headers="NOBODY" then iterate
      if wordpos("MSGTEXT",headers)=0 & headers<>"ENTIRE" then iterate
      do t=1 to bodylines.0
         if pos(spammer,bodylines.t)>0 then killit=1
         if killit then leave
      end
      if killit then leave
   end
   call lineout infile
   if killit then call sysfiledelete infile
end
NoREXX:
exit
