'''
avfilter.py - SMTP daemon service for sagator.

(c) 2003-2004 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 aglib import *
import sys

__all__=['avfilter']

class avfilter(service):
  '''
  AV filter service for sagator.
  
  This service can be used to filter an email through sagator.
  Some headers will be added to filtered email.
  
  Usage: avfilter(scanners)
  
  Where: scanners is an array of scanners (see README.scanners for more info)
  
  Example: avfilter(SCANNERS)
  
  You can use this service for example with procmail.
  Example .procmailrc:
    # filter through sagator
    :0fw
    |sagator --logfile /var/log/sagator.log
    
    # move identified emails to quarantine
    :0
    * ^X-Sagator-Status: .
    $HOME/mail/quarantine
  '''
  name='avfilter()'
  def __init__(self,scanners):
      self.SCANNERS=scanners
  def main(self):
      for scnr in self.SCANNERS:
        scnr.reinit()
      globals.pid_file=None
      try:
        # read stdin
        while 1:
          line=sys.stdin.readline()
          if not line: break
          mail.df.write(line)
        mail.close()
        stdout=os.dup(1)
        debug.dup()
        status,virname=checkvir(self.SCANNERS)
        os.write(stdout, mail.xheader)
        os.write(stdout, mail.header)
        os.write(stdout, mail.data[mail.bodypos:])
        sys.exit(0)
      except (KeyboardInterrupt,SystemExit):
        raise
      except:
        debug.echo(1,"avfilter(): Unknow error!")
        debug.traceback(0,"avfilter(): ")
        sys.exit(1)
