imapt : Add --expunge parameter
Benjamin Renard

Benjamin Renard commited on 2013-12-12 11:47:36
Showing 1 changed files, with 36 additions and 1 deletions.

... ...
@@ -98,6 +98,13 @@ parser.add_option('-l',
98 98
                   help="List mailboxes",
99 99
                   default=False)
100 100
 
101
+parser.add_option('--expunge',
102
+                  action="store",
103
+                  type="string",
104
+                  dest="expunge",
105
+                  help="Expunge mailbox specify (ALL to expunge all mailboxes)",
106
+                  default=None)
107
+
101 108
 (options, args) = parser.parse_args()
102 109
 
103 110
 if options.verbose:
... ...
@@ -147,12 +154,40 @@ if options.user:
147 154
 		logging.critical("IMAP Auth error : %s" % e)
148 155
 		sys.exit(3)
149 156
 
157
+def expunge(server,mailbox):
158
+	logging.info('Expunge %s mailbox' % mailbox)
159
+	try:
160
+		(st,c) = server.select(mailbox)
161
+		if st!='OK':
162
+			logging.error('Error selecting %s mailbox : %s' % (mailbox,st))
163
+			return
164
+		(st,ex) = server.expunge()
165
+		if st=='OK':
166
+			if len(ex)==1 and ex[0] is None:
167
+				logging.info('Mailbox %s expunged, no mail removed' % mailbox)
168
+			else:
169
+				logging.info('Mailbox %s expunged : %s mail(s) removed' % (mailbox,len(ex)))
170
+		else:
171
+			logging.info('Error expunging %s mailbox, return "%s"' % (mailbox,st))
172
+	except Exception as e:
173
+		logging.error('Error expunging %s mailbox : %s' % (mailbox,e))
174
+
175
+
150 176
 try:
151 177
 	if options.list:
152 178
 		logging.info('List mailbox')
153 179
 		(status,l) = server.list()
154 180
 		for d in l:
155
-			logging.info('   "%s"' % d)
181
+			logging.info('   "%s"' % d.split('"')[3])
182
+	elif options.expunge:
183
+		if options.expunge=='ALL':
184
+			logging.debug("Listing mailboxes")
185
+			(status,l) = server.list()
186
+			for d in l:
187
+				dirname=d.split('"')[3]
188
+				expunge(server,dirname)
189
+		else:
190
+			expunge(server,options.expunge)
156 191
 	else:
157 192
 		logging.debug("Select mailbox")
158 193
 		ret = server.select(options.mailbox)
159 194