From b834b36bffa5afe6d01006ceb4a1174b18106b68 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 1 Feb 2011 10:53:04 -0500 Subject: [PATCH] Add imapquota post. --- posts/imapquota.mdwn | 13 ++++++++ posts/imapquota/imapquota.py | 57 ++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 posts/imapquota.mdwn create mode 100755 posts/imapquota/imapquota.py diff --git a/posts/imapquota.mdwn b/posts/imapquota.mdwn new file mode 100644 index 0000000..2e6b89c --- /dev/null +++ b/posts/imapquota.mdwn @@ -0,0 +1,13 @@ +[[imapquota.py]] is a [[Python]] script that checks the space +remaining in an [IMAP][] mail account using [imaplib][]. + + $ imapquota -s imap.mail.drexel.edu xyz12@drexel.edu + Password: + Used 92693 of 102400 KB + 90.5 percent + +[IMAP]: http://en.wikipedia.org/wiki/Internet_Message_Access_Protocol +[imaplib]: http://docs.python.org/library/imaplib.html + +[[!tag tags/bash]] +[[!tag tags/programming]] diff --git a/posts/imapquota/imapquota.py b/posts/imapquota/imapquota.py new file mode 100755 index 0000000..92c0a74 --- /dev/null +++ b/posts/imapquota/imapquota.py @@ -0,0 +1,57 @@ +#!/usr/bin/python +# +# This script is released to the public domain. +# Example usage: +# $ imapquota.py -s imap.mail.drexel.edu xyz12@drexel.edu +# Password: +# Used 92693 of 102400 KB +# 90.5 percent + +import getpass +import imaplib +import re + + +def parse_server(server, ssl=False): + server_bits = server.split(':') + assert len(server_bits) in [1,2], 'invalid server: %s' % server_bits + server = server_bits[0] + if len(server_bits) == 2: + port = server_bits[1] + elif ssl == True: + port = 993 + else: + port = 143 + return (server, port) + +def get_quota(imap_connection): + status,value = imap_connection.getquotaroot('inbox') + #status,value = ('OK', [['inbox user/xyz12'], ['user/xyz12 (STORAGE 80000 102400)']]) + quota = value[1][0] + regexp = re.compile(r'.*STORAGE ([0-9]*) ([0-9]*).*') + m = regexp.match(quota) + used,avail = [int(x) for x in m.groups()] + return (used, avail) + + +if __name__ == '__main__': + import optparse + p = optparse.OptionParser('%prog SERVER[:port] account', + epilog='imapquota -s imap.mail.drexel.edu xyz12@drexel.edu') + p.add_option('-s', '--ssl', dest='ssl', default=False, action='store_true', + help='Connect over an SSL encrypted socket and change default port to 993.') + options,args = p.parse_args() + + server,account = args + server,port = parse_server(server, ssl=options.ssl) + password = getpass.getpass() + + if options.ssl == True: + i = imaplib.IMAP4_SSL(server, port) + else: + i = imaplib.IMAP4(server, port) + i.login(account, password) + used,avail = get_quota(i) + i.logout() + print 'Used %d of %d KB' % (used, avail) + print '%.1f percent' % (float(used)/avail*100.0) -- 2.26.2