imapt : Add --expunge parameter
This commit is contained in:
parent
4b34a426ea
commit
d70fa4f158
1 changed files with 37 additions and 2 deletions
37
imapt
37
imapt
|
@ -98,6 +98,13 @@ parser.add_option('-l',
|
||||||
help="List mailboxes",
|
help="List mailboxes",
|
||||||
default=False)
|
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()
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
if options.verbose:
|
if options.verbose:
|
||||||
|
@ -147,12 +154,40 @@ if options.user:
|
||||||
logging.critical("IMAP Auth error : %s" % e)
|
logging.critical("IMAP Auth error : %s" % e)
|
||||||
sys.exit(3)
|
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:
|
try:
|
||||||
if options.list:
|
if options.list:
|
||||||
logging.info('List mailbox')
|
logging.info('List mailbox')
|
||||||
(status,l) = server.list()
|
(status,l) = server.list()
|
||||||
for d in l:
|
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:
|
else:
|
||||||
logging.debug("Select mailbox")
|
logging.debug("Select mailbox")
|
||||||
ret = server.select(options.mailbox)
|
ret = server.select(options.mailbox)
|
||||||
|
|
Loading…
Reference in a new issue