'''
filter module, version 0.1

(c) 2003 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
                                                                                
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

'''

from avlib import *
import re

__all__=['filter']

class filter(ascanner):
  '''
  Filter scanner
  
  Usage: filter(['/path/command_name','-options',...])
  
  Where: in [] is a path and argumets of filter binary

  This scanner can be used to filter email through an external program.
  It's return status is always clean.
  '''
  is_spamscan=1
  name='filter()'
  def __init__(self,arg,clean_status=[0]):
      self.arg=arg
      self.clean_status=clean_status
  def scanstream(self,stream):
      pf=popen(self.arg)
      pf.write(stream)
      pf.tocmd.close()
      while True:
        try:
          data=pf.read()
          pf.wait()
          if pf.exitstatus in self.clean_status:
            mail.data=data
            mail.findbody()
          break
        except IOError,(ec,es):
          if ec==4: #Interrupted system call
            continue
          debug.echo(1,"filter(): an IOError occured during data read: ",es)
          pf.wait()
      debug.echo(4,"filter(): exit status: ",pf.exitstatus)
      return 0.0,'',[]
