--- /dev/null
+[[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]]
--- /dev/null
+#!/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)