<?xml version="1.0" encoding="UTF-8"?>
<?python
  import template, os
  from cStringIO import StringIO
  from email import message_from_string
  from aglib import qfile, send_qfile
  from avlib import safe, _
  from etc import SMTP_SERVER
  
  def utf_header(headers, hdr, i18n):
      if headers[hdr]==None:
        return []
      else:
        return (i18n+' '+unicode(headers[hdr], errors='replace')).split('\n')
?>
<html xmlns:py="http://purl.org/kid/ns#"
      py:layout="template">

<div py:def="body(_)">
<form action="action.kid">
  <p>
  <input name="view" class="submit" type="submit"
         py:attrs="{'value': _('View')}"/>
  <input name="recheck" class="submit" type="submit"
         py:attrs="{'value': _('Recheck')}"/>
  <input name="deliver" class="submit" type="submit"
         py:attrs="{'value': _('Deliver')}"/>
  <input name="qname" type="hidden"
         py:for="qname in REQUESTS.get('qname', [])" value="${qname}"/>
  </p>
</form>
<hr/>

<div py:if="not REQUESTS.has_key('qname')"
     py:content="_('ERROR: Nothing selected!')" />

<div py:for="qn in REQUESTS.get('qname', [])" py:strip="">

<?python
  VIEW = True
  try:
    error = ""
    qname = safe.fn(qn)
    if 'A' in PERMS:
      DB.query("SELECT qname FROM log WHERE qname=%s", [qn])
    else:
      DB.query("SELECT qname FROM log WHERE qname=%s AND recipient=%s",
               [qn, REMOTE_USER])
    if DB.rowcount==0:
      error = "%s: Permission denied or no quarantined file found!" % qname
    if not os.path.isfile(qname):
      error = "File %s does not exist!" % qname
  except Exception, e:
    error = "Internal error occurred [%s]!" % e
?>

<h3 py:if="error" py:content="error"/>

<div py:if="not error" py:strip="">

<div py:if="REQ.has_key('recheck')">
<?python
  VIEW = False
  # Recheck quarantined file
  error = None
  level = None
  try:
    email = qfile()
    email.parse(open(qname, 'rb'))
    level, virname, status = SCANNER.scanbuffer(email.message())
    SCANNER.destroy()
    if not virname:
      virname = _("CLEAN")
      div_class = "clean"
    else:
      div_class = "infected"
  except Exception, error:
    pass
?>
<p py:if="error" py:content="error"></p>
<div py:if="level!=None" class="${div_class}">
${qname}: ${virname} (${level})<br/>
<pre py:if="status" py:content="status"/>
</div>
</div>

<div py:if="REQ.has_key('deliver')">
<?python
  VIEW = False
  # Deliver quarantined file
  status = ''
  error = None
  try:
    status = send_qfile(qname, SMTP_SERVER)
  except Exception, error:
    pass
?>
<p py:if="error" py:content="error"></p>
<pre py:if="status" py:content="status"/>
</div>

<div py:if="REQ.has_key('view') or VIEW">
<?python
  # View quarantined file
  headers = None
  error = None
  try:
    email=qfile()
    email.parse(open(qname, 'rb'))
    # parse headers
    headers = message_from_string(email.message())
    show_headers = \
        utf_header(headers, 'From', _('From:')) + \
        utf_header(headers, 'To', _('To:')) + \
        utf_header(headers, 'Date', _('Date:')) + \
        utf_header(headers, 'Subject', _('Subject:'))
    for key in headers.keys():
      if key[:7]=='X-Spam-' or key[:10]=='X-Sagator-':
        show_headers.extend(utf_header(headers, key, key+':'))
  except IOError, error:
    pass
  except Exception, e:
    print "action.kid:", e
?>
<p py:if="error" py:content="error"></p>
<pre py:if="headers" class="header">
${'\n'.join(show_headers)}
</pre>
<pre py:if="headers" py:content="unicode(email.body(), errors='replace')"/>
<hr/>
</div>

</div>

</div>

</div>

</html>
