Add display IMAP quota feature

This commit is contained in:
Benjamin Renard 2015-11-16 18:44:16 +01:00
parent e5b04b88d4
commit f9d216e372

33
imapt
View file

@ -30,6 +30,8 @@ import imaplib
import logging import logging
import sys import sys
import re
parser = OptionParser() parser = OptionParser()
@ -98,6 +100,13 @@ parser.add_option('-l',
help="List mailboxes", help="List mailboxes",
default=False) default=False)
parser.add_option('-q',
'--quota',
action="store_true",
dest="quota",
help="Display quota informations",
default=False)
parser.add_option('--expunge', parser.add_option('--expunge',
action="store", action="store",
type="string", type="string",
@ -172,6 +181,12 @@ def expunge(server,mailbox):
except Exception as e: except Exception as e:
logging.error('Error expunging %s mailbox : %s' % (mailbox,e)) logging.error('Error expunging %s mailbox : %s' % (mailbox,e))
def format_quota(num):
num=int(num)
for x in ['KB', 'MB', 'GB', 'TB']:
if num < 1024.0:
return "%3.1f %s" % (num, x)
num /= 1024.0
try: try:
if options.list: if options.list:
@ -188,6 +203,24 @@ try:
expunge(server,dirname) expunge(server,dirname)
else: else:
expunge(server,options.expunge) expunge(server,options.expunge)
elif options.quota:
logging.debug("Get quota usage")
ret=server.getquotaroot(options.mailbox)
logging.debug("IMAP server return : %s" % str(ret))
# Return example on Dovecot
# ('OK', [['"INBOX" "User quota"'], ['"User quota" (STORAGE 0 1024)']])
if isinstance(ret, (list, tuple)):
for quota in ret[1][1]:
if quota is None:
logging.info('No quota defined')
continue
parsequota=re.match('^"([^"]*)" \(STORAGE ([0-9]*) ([0-9]*)\)$',quota)
if parsequota:
logging.info("Quota %s : %s / %s" % (parsequota.group(1),format_quota(parsequota.group(2)),format_quota(parsequota.group(3))))
else:
logging.error("Error parsing quota string from IMAP server : '%s'" % quota)
else:
logging.error("Invalid result getting quotas: %s" % ret)
else: else:
logging.debug("Select mailbox") logging.debug("Select mailbox")
ret = server.select(options.mailbox) ret = server.select(options.mailbox)