From fd808e85822f928d2ab758bb947555d9e78d8e26 Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Thu, 26 Mar 2009 18:59:13 +0000 Subject: [PATCH] Added SSL support --- TODO | 4 ++-- imapclient/imapclient.py | 17 ++++++++++++++--- livetest.py | 19 +++++++++++-------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/TODO b/TODO index c2370ad..ed52d98 100644 --- a/TODO +++ b/TODO @@ -1,13 +1,13 @@ +- transparent "&" escaping - automatic changelog generation - support for more IMAP functions: sort, examine etc + - someone sent a patch for this? - write a comparision of imaplib vs imapclient.py (presentation is a good start) - abstract out UID handling if possible - higher level fetch methods for common attributes, flattened - handling read-only access -- transparent "&" escaping - more unit tests using mocking -- SSL support - allow per-call uid overrides - more docs - better namespace support diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index b16d646..c314a39 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -67,14 +67,25 @@ class IMAPClient: re_status = re.compile(r'^\s*"(?P[^"]+)"\s+' r'\((?P.*)\)$') - def __init__(self, host, port=143, use_uid=True): + def __init__(self, host, port=None, use_uid=True, ssl=False): '''Initialise object instance and connect to the remote IMAP server. @param host: The IMAP server address/hostname to connect to. - @param port: The port number to use (default is 143). + @param port: The port number to use (default is 143, 993 for SSL). @param use_uid: Should message UIDs be used (default is True). + @param ssl: Make an SSL connection (default is False) ''' - self._imap = imaplib.IMAP4(host, port) + if ssl: + ImapClass = imaplib.IMAP4_SSL + default_port = 993 + else: + ImapClass = imaplib.IMAP4 + default_port = 143 + + if port is None: + port = default_port + + self._imap = ImapClass(host, port) self.use_uid = use_uid def login(self, username, password): diff --git a/livetest.py b/livetest.py index 8890546..8a524fd 100644 --- a/livetest.py +++ b/livetest.py @@ -235,16 +235,18 @@ def clear_folder(server, folder): def command_line(): p = OptionParser() p.add_option('-H', '--host', dest='host', action='store', - help='IMAP host connect to') + help='IMAP host connect to') p.add_option('-P', '--port', dest='port', action='store', - default=143, help='IMAP port to use (default is 143)') + default=143, help='IMAP port to use (default is 143)') + p.add_option('-s', '--ssl', dest='ssl', action='store_true', default=False, + help='Use SSL connection') p.add_option('-u', '--username', dest='username', action='store', - help='Username to login with') + help='Username to login with') p.add_option('-p', '--password', dest='password', action='store', - help='Password to login with') + help='Password to login with') p.add_option('', '--clobber', dest='clobber', action='store_true', - default=False, help='These tests are destructive. Use this ' - 'option to bypass the confirmation prompt.') + default=False, help='These tests are destructive. Use this ' + 'option to bypass the confirmation prompt.') options, args = p.parse_args() @@ -257,6 +259,7 @@ def command_line(): return options + def user_confirm(): print """\ WARNING: These tests are destructive. @@ -276,9 +279,9 @@ def main(): # Test with use_uid on and off for use_uid in (True, False): print '-'*60 - print "Testing with use_uid=%r..." % use_uid + print "Testing with use_uid=%r, ssl=%r..." % (use_uid, options.ssl) print '-'*60 - server = imapclient.IMAPClient(options.host, use_uid=use_uid) + server = imapclient.IMAPClient(options.host, use_uid=use_uid, ssl=options.ssl) server.login(options.username, options.password) runtests(server) print 'SUCCESS' -- 2.43.0