'''
Header interscanners for sagator, version 0.6.2

(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 avlib import *
import re
from match import match_any

__all__=['add_header','modify_header','modify_subject','remove_headers']

class add_header(match_any):
  '''
  add_header() - add email header

  usage: add_header('Header_name','header_data',scanners)
     or: add_header(...).onlyif('virname')
  
  where: 'Header_name' is a string, which defines name of the header
         'header_data' is a string, which defines header's content
         'virname' is a string, which defines any virus name.
           By default all virus names at least 1 character long: '.'
         There are some special pattern, which are replaced:
           %V ... virus name
           %S ... scanner name
           %L ... detected level

  This scanner can be used to add an header line to email.
  '''
  name='add_header()'
  def __init__(self,hdr,data,*scanners):
      self.ONLY_IF='.'
      self.HDR=hdr
      self.DATA=data
      match_any.__init__(self,scanners)
  def onlyif(self,pattern='.'):
      self.ONLY_IF=pattern
      return self
  def hdr_replace(self,to,level,detected):
      return re.sub("%V",detected,
               re.sub("%S",globals.found_by.name,
                 re.sub("%L",str(level),
                   to)))
  def scanstream(self,stream):
      level,detected,virlist=match_any.scanstream(self,stream)
      if re.search(self.ONLY_IF,detected,re.IGNORECASE):
        mail.addheader(self.HDR,self.hdr_replace(self.DATA,level,detected))
      return level,detected,virlist

class modify_header(add_header):
  '''
  modify_header() - modifying email headers

  usage: modify_header('^Header_regexp:','Header_regexp: prefix',scanners)
     or: modify_header(...).onlyif('virname')
  
  where: '^Header_regexp:' is a string, which defines regular expression
           to find for this header
         'Header_regexp: prefix' is a string, which defines new header
           data
         'virname' is a string, which defines any virus name.
           By default all virus names at least 1 character long: '.'
         There are some special pattern, which are replaced:
           %V ... virus name
           %S ... scanner name
           %L ... detected level

  This scanner can be used to modify email headers.
  '''
  name='modify_header()'
  def __init__(self,frm,to,*scanners):
      self.ONLY_IF='.'
      self.MOD_FRM=frm
      self.MOD_TO=to
      match_any.__init__(self,scanners)
  def scanstream(self,stream):
      level,detected,virlist=match_any.scanstream(self,stream)
      if re.search(self.ONLY_IF,detected,re.IGNORECASE):
        mail.modheader(self.MOD_FRM,self.hdr_replace(self.MOD_TO,level,detected))
      return level,detected,virlist

class modify_subject(modify_header):
  '''
  modify_subject() - modifying email Subject

  usage: modify_subject('prefix',scanners)
     or: modify_subject(...).onlyif('virname')
  
  where: 'prefix' is an prefix added into Subject.
         'virname' is a string, which defines any virus name.
           By default all virus names at least 1 character long: '.'
         For more information see modify_header() interscanner.

  This scanner can be used to modify email Subject.
  For example original Subject line is:
    Subject: Testing mail
  and i use:
    modify_subject('[SPAM]', spamassassind(['localhost',783]))
  When spam will be found, header will be modifyed to:
    Subject: [SPAM] Testing mail
  '''
  name='modify_subject()'
  def __init__(self,prefix,*scanners):
      modify_header.__init__(self,"^Subject:","Subject: "+prefix,scanners)

class remove_headers(ascanner):
  '''
  remove_headers() - removing email headers

  usage: remove_headers('header name' [,'next header name' [, ...] ])
  
  where: 'header name' is a string, which defines header key to delete

  This scanner can be used to remove email headers.
  '''
  name='remove_headers()'
  def __init__(self,*hdrs):
      self.DEL_HDRS=hdrs
  def scanstream(self,stream):
      for hdr in self.DEL_HDRS:
        mail.delheader(hdr)
      return 0.0,'',[]
