'''
ryporter.py - An virtual service for sagator's reporter.

(c) 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.

'''

import re
from aglib import *

__all__=['reporter']

## Report message contains from three variables. First part is message header.
## This part must contain email header described in RFC 2822.
## After this header you can define emails text.

_BEGIN='''\
From: SAGATOR <$SGUSER@$MYHOSTNAME>
To: $REPORT_RECIPIENT
Subject: Summary of undelivered emails: $BEGIN - $END

This email is automaticcaly generated by SAGATOR $VERSION.
It contains summary of emails, which has been rejected or dropped.

---------------------[ beginning of report ]--------------------------------
'''

## Each message contains more message bodys.
_BODY='''
$COUNTER. Sender: $SENDER [$SENTBY_IP] at $DATETIME
     Status: $VIRNAME $STATUS [$SCANNER_NAME=$LEVEL]
'''

## At end of message there is this text.
_END='''
--------------------------------------[ end of report ]---------------------
'''

class reporter(service):
  '''
  Reporter virtual service.
  
  This service is only a virtual service to configure parameters for
  reporter script.
  
  Usage: reporter(...parameters...)

  There you can define some parameters:
  "begin", "body", "end", "include", "exclude", "include_fx" and "exclude_fx".
  First three parameters are email templates. "begin" is report header.
  It must contain RFC2822 headers. "body" is message part displayed for each
  rejected/dropped message. "end" is report's tailer, added to message
  after all "body"s.
  With "include" and "exclude" you can define regular expressions, which
  are tested for each report recipient. For example you can define your
  domain by: "@your-domain.com$". "include_fx" and "exculde_fx" are functions
  with similiar functionality.

  Example: reporter()
  '''
  name='reporter()'
  INCLUDE='.'
  EXCLUDE='^$'
  INCLUDE_FX=re.compile('.').search
  EXCLUDE_FX=re.compile('^$').search
  BEGIN=_BEGIN
  BODY=_BODY
  END=_END
  def __init__(self,**args):
      for key,value in args.items():
        if key=='begin':
          self.BEGIN=value
        elif key=='body':
          self.BODY=value
        elif key=='end':
          self.END=value
        elif key=='include':
          self.INCLUDE_FX=re.compile(value,re.I).search
        elif key=='exclude':
          self.EXCLUDE_FX=re.compile(value,re.I).search
        elif key=='include_fx':
          self.EXCLUDE_FX=value
        elif key=='exclude_fx':
          self.INCLUDE_FX=value
        else:
          raise TypeError,'%s got an unexpected argument %s' % (self.name,key)
  def start(self):
      return []
