+

imapt : Add --expunge parameter

Benjamin Renard [2013-12-12 11:46:23]
imapt : Add --expunge parameter
Filename
imapt
diff --git a/imapt b/imapt
index ffdf5b7..c8281c2 100755
--- a/imapt
+++ b/imapt
@@ -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)
ViewGit