From 219eb8c47f23adf457ff52810c5e8c312f8baea8 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 16 Dec 2010 12:43:46 +0000 Subject: [PATCH] Don't call open_new functions outside of any class Move them into the correct classes, overriding the open() function. This is what we intent to do anyway, so do it in a clean way. Signed-off-by: Sebastian Spaeth Signed-off-by: Nicolas Sebrecht --- offlineimap/imaplibutil.py | 71 +++++++++++++++++++------------------- offlineimap/imapserver.py | 9 +---- 2 files changed, 37 insertions(+), 43 deletions(-) diff --git a/offlineimap/imaplibutil.py b/offlineimap/imaplibutil.py index 554fe88..bbcb573 100644 --- a/offlineimap/imaplibutil.py +++ b/offlineimap/imaplibutil.py @@ -110,41 +110,6 @@ def new_mesg(self, s, secs=None): class WrappedIMAP4_SSL(IMAP4_SSL): def open(self, host = '', port = IMAP4_SSL_PORT): - IMAP4_SSL.open(self, host, port) - self.sslobj = sslwrapper(self.sslobj) - - def readline(self): - return self.sslobj.readline() - -def new_open(self, host = '', port = IMAP4_PORT): - """Setup connection to remote server on "host:port" - (default: localhost:standard IMAP4 port). - This connection will be used by the routines: - read, readline, send, shutdown. - """ - self.host = host - self.port = port - res = socket.getaddrinfo(host, port, socket.AF_UNSPEC, - socket.SOCK_STREAM) - - # Try each address returned by getaddrinfo in turn until we - # manage to connect to one. - # Try all the addresses in turn until we connect() - last_error = 0 - for remote in res: - af, socktype, proto, canonname, sa = remote - self.sock = socket.socket(af, socktype, proto) - last_error = self.sock.connect_ex(sa) - if last_error == 0: - break - else: - self.sock.close() - if last_error != 0: - # FIXME - raise socket.error(last_error) - self.file = self.sock.makefile('rb') - -def new_open_ssl(self, host = '', port = IMAP4_SSL_PORT): """Setup connection to remote server on "host:port". (default: localhost:standard IMAP4 SSL port). This connection will be used by the routines: @@ -174,6 +139,42 @@ def new_open_ssl(self, host = '', port = IMAP4_SSL_PORT): self.sslobj = ssl_wrap(self.sock, self.keyfile, self.certfile) self.sslobj = sslwrapper(self.sslobj) + def readline(self): + return self.sslobj.readline() + + +class WrappedIMAP4(IMAP4): + """Improved version of imaplib.IMAP4 that can also connect to IPv6""" + + def open(self, host = '', port = IMAP4_PORT): + """Setup connection to remote server on "host:port" + (default: localhost:standard IMAP4 port). + This connection will be used by the routines: + read, readline, send, shutdown. + """ + self.host = host + self.port = port + res = socket.getaddrinfo(host, port, socket.AF_UNSPEC, + socket.SOCK_STREAM) + + # Try each address returned by getaddrinfo in turn until we + # manage to connect to one. + # Try all the addresses in turn until we connect() + last_error = 0 + for remote in res: + af, socktype, proto, canonname, sa = remote + self.sock = socket.socket(af, socktype, proto) + last_error = self.sock.connect_ex(sa) + if last_error == 0: + break + else: + self.sock.close() + if last_error != 0: + # FIXME + raise socket.error(last_error) + self.file = self.sock.makefile('rb') + + mustquote = re.compile(r"[^\w!#$%&'+,.:;<=>?^`|~-]") def Internaldate2epoch(resp): diff --git a/offlineimap/imapserver.py b/offlineimap/imapserver.py index d9dd6ba..cf1fdc9 100644 --- a/offlineimap/imapserver.py +++ b/offlineimap/imapserver.py @@ -59,15 +59,12 @@ class UsefulIMAPMixIn: def _mesg(self, s, secs=None): imaplibutil.new_mesg(self, s, secs) -class UsefulIMAP4(UsefulIMAPMixIn, imaplib.IMAP4): - def open(self, host = '', port = imaplib.IMAP4_PORT): - imaplibutil.new_open(self, host, port) +class UsefulIMAP4(UsefulIMAPMixIn, imaplibutil.WrappedIMAP4): # This is a hack around Darwin's implementation of realloc() (which # Python uses inside the socket code). On Darwin, we split the # message into 100k chunks, which should be small enough - smaller # might start seriously hurting performance ... - def read(self, size): if (system() == 'Darwin') and (size>0) : read = 0 @@ -81,12 +78,8 @@ class UsefulIMAP4(UsefulIMAPMixIn, imaplib.IMAP4): return imaplib.IMAP4.read (self, size) class UsefulIMAP4_SSL(UsefulIMAPMixIn, imaplibutil.WrappedIMAP4_SSL): - def open(self, host = '', port = imaplib.IMAP4_SSL_PORT): - imaplibutil.new_open_ssl(self, host, port) - # This is the same hack as above, to be used in the case of an SSL # connexion. - def read(self, size): if (system() == 'Darwin') and (size>0) : read = 0