'''
kav module - Kaspersky support, version 0.2

(c) 2003-2004 Laszlo BALINT (blaci) <balint_laszlo(at)tiszanet.hu>
              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 os,re,string

__all__=['kav','kavclient']

class kav(ascanner):
  '''
  Kaspersky realscanner
  
  Usage: kav(command)

  Where: command is a array which defines command with parameters.
           By default: ['/opt/kav/bin/kavscanner','-j3']

  This scanners is a realscanner, which can be used to scan
  for viruses. It parses Kaspersky antivirus output.
  '''
  name='kav()'
  def __init__(self,command=['/opt/kav/bin/kavscanner','-j3']):
      self.command=command
  def create_args(self,files,dir=''):
      self.args=[]
      for fn in files:
        if dir:
          self.args.append(safe.fn(os.path.join(dir,fn)))
        else:
          self.args.append(safe.fn(fn))
  def scanfile(self,files,dir=''):
      level,detected,ret=0.0,'',[]
      self.create_args(files,dir)
      pf=popen(self.command+self.args)
      pf.tocmd.close()
      rc=pf.wait()
      if rc==1:
        raise ScannerError,'Unable to connect to aveserver.'
      elif rc==9:
        raise ScannerError,'Some of the required parameters are missing from the command line.'
      # read lines from command
      while 1:
        line=pf.readline()
        if line=='': break
        re1=re.search("(INF|SUSP)(ECTED|ICION) (.*)",line)
        if re1:
          debug.echo(3,"INFECTED: kav(): ",line.rstrip())
          if re1.group(1)=="INF": # if it is an indentified virus
            detected=re1.group(3)
            level+=1.0
          else:
            level+=0.75
            if detected=='': # if it is suspected, but no ident. virus
              detected=re1.group(3)
          ret.append(line)
      pf.fromcmd.close()
      debug.echo(4,"KAV: RET: ",[[rc]],ret)
      return level,detected,ret

class kavclient(kav):
  '''
  Kaspersky antivirus client realscanner
  
  Usage: kavclient([socket_path [,command]])

  Where: socket_path is a string, which defines path co aveserver socket
           inside chroot. (default: /var/run/aveserver)
         command is a string, which defines command to run.
           (default: /opt/kav/bin/aveclient)

  This scanners is a realscanner, which can be used to scan
  for viruses. It parses Kaspersky antivirus client output.
  '''
  name='kavclient()'
  def __init__(self,socket_path='/var/run/aveserver',command='/opt/kav/bin/aveclient'):
      self.command=[command,'-s','-p']
      self.command_lastarg=socket_path
  def create_args(self,files,dir=''):
      self.args=[safe.fn(self.command_lastarg)]
      for fn in files:
        if dir:
          self.args.append(safe.fn(os.path.join(dir,fn)))
        else:
          self.args.append(safe.fn(fn))
