#!/usr/bin/python

'''
Sagator Back from Quarantine script.
 
This program can be used to requeue an quarantined mail from
quarantine to queue/user mailbox.
 
(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.

Usage:
  sqback [--to RCPT] quarantined_file1 [quarantined_files...]
'''

import sys,socket,re
from config import SMTP_SERVER
from aglib import send_qfile,getopt

def usage():
    print "sqback usage:"
    print "  sqback [--help] [--to RCPT] quarantined_file1 [quarantined_files...]"
    sys.exit(0)

rcptto=''
try:
  opts,files=getopt.gnu_getopt(sys.argv[1:],'',['help','to='])
except getopt.GetoptError,(msg,opt):
  print "Error:",msg
  sys.exit(1)

for key,value in opts:
  if key=='--help':
    usage()
  if key=='--to':
    rcptto=value

if len(files)==0:
  print "Error: No files defined!"
  usage()

try:
  for fname in files:
    print send_qfile(fname,SMTP_SERVER,rcptto)
except IOError,(ec,es):
  print "IOError:", es+":" , fname
