imapt : Add --expunge parameter

This commit is contained in:
Benjamin Renard 2013-12-12 12:46:23 +01:00
parent 4b34a426ea
commit d70fa4f158

39
imapt
View file

@ -98,6 +98,13 @@ parser.add_option('-l',
help="List mailboxes",
default=False)
parser.add_option('--expunge',
action="store",
type="string",
dest="expunge",
help="Expunge mailbox specify (ALL to expunge all mailboxes)",
default=None)
(options, args) = parser.parse_args()
if options.verbose:
@ -146,13 +153,41 @@ if options.user:
except Exception as e:
logging.critical("IMAP Auth error : %s" % e)
sys.exit(3)
def expunge(server,mailbox):
logging.info('Expunge %s mailbox' % mailbox)
try:
(st,c) = server.select(mailbox)
if st!='OK':
logging.error('Error selecting %s mailbox : %s' % (mailbox,st))
return
(st,ex) = server.expunge()
if st=='OK':
if len(ex)==1 and ex[0] is None:
logging.info('Mailbox %s expunged, no mail removed' % mailbox)
else:
logging.info('Mailbox %s expunged : %s mail(s) removed' % (mailbox,len(ex)))
else:
logging.info('Error expunging %s mailbox, return "%s"' % (mailbox,st))
except Exception as e:
logging.error('Error expunging %s mailbox : %s' % (mailbox,e))
try:
if options.list:
logging.info('List mailbox')
(status,l) = server.list()
for d in l:
logging.info(' "%s"' % d)
logging.info(' "%s"' % d.split('"')[3])
elif options.expunge:
if options.expunge=='ALL':
logging.debug("Listing mailboxes")
(status,l) = server.list()
for d in l:
dirname=d.split('"')[3]
expunge(server,dirname)
else:
expunge(server,options.expunge)
else:
logging.debug("Select mailbox")
ret = server.select(options.mailbox)