From f36b1823c404f138b9853a5ab77fba07b9e3a8e4 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 17 Feb 2000 17:12:39 +0000 Subject: [PATCH] Patches by Piers Lauder. Reasons for patches: 1st patch (15,21): version change 2nd patch (66,72): This is a patch I found in a Zope product release (quite by accident!). It relaxes the conditions for matching a literal. I've looked over the logic, and tested it, and it seems sensible. 3rd patch (117,123): It appears the quoting matcher was too general, and that the IMAP4 protocol requires characters like ':' in commands to be unquoted. (This is the patch already sent to Guido.) 4th patch (699,705): Spelling correction in comment. 5th patch (753,761): Another patch from the Zope product. It seems that some IMAP4 servers produce unexpected responses in the middle of valid command/response sequences. This patch ignores the unexpected responses in this situation. (How I wish users would send me bug reports with examples!). last 2 patches: (1015,1028) (1038,1044): Minor improvements to test code. --- Lib/imaplib.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/Lib/imaplib.py b/Lib/imaplib.py index 0275571323..c65cc90332 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -15,7 +15,7 @@ Public functions: Internaldate2tuple Time2Internaldate """ -__version__ = "2.16" +__version__ = "2.30" import binascii, re, socket, string, time, random, sys @@ -66,7 +66,7 @@ InternalDate = re.compile(r'.*INTERNALDATE "' r' (?P[0-9][0-9]):(?P[0-9][0-9]):(?P[0-9][0-9])' r' (?P[-+])(?P[0-9][0-9])(?P[0-9][0-9])' r'"') -Literal = re.compile(r'(?P.*) {(?P\d+)}$') +Literal = re.compile(r'.*{(?P\d+)}$') Response_code = re.compile(r'\[(?P[A-Z-]+)( (?P[^\]]*))?\]') Untagged_response = re.compile(r'\* (?P[A-Z-]+)( (?P.*))?') Untagged_status = re.compile(r'\* (?P\d+) (?P[A-Z-]+)( (?P.*))?') @@ -117,7 +117,7 @@ class IMAP4: class abort(error): pass # Service errors - close and retry class readonly(abort): pass # Mailbox status changed to READ-ONLY - mustquote = re.compile(r'\W') # Match any non-alphanumeric character + mustquote = re.compile(r"[^\w!#$%&'*+,.:;<=>?^`|~-]") def __init__(self, host = '', port = IMAP4_PORT): self.host = host @@ -699,7 +699,7 @@ class IMAP4: dat2 = self.mo.group('data2') if self.mo is None: - # Only other possibility is '+' (continuation) rsponse... + # Only other possibility is '+' (continuation) response... if self._match(Continuation, resp): self.continuation_response = self.mo.group('data') @@ -753,7 +753,19 @@ class IMAP4: if result is not None: del self.tagged_commands[tag] return result - self._get_response() + + # Some have reported "unexpected response" exceptions. + # (Isn't this non-IMAP4-compliant behaviour? + # Please mail me details printed below!) + # Anyway, ignore them here. + + try: + self._get_response() + except self.abort, val: + if __debug__: + if self.debug >= 1: + _mesg('abort exception ignored: %s' % val) + print_log() def _get_line(self): @@ -1015,14 +1027,15 @@ if __name__ == '__main__': if sys.argv[1:]: host = sys.argv[1] USER = getpass.getuser() - PASSWD = getpass.getpass("IMAP password for %s: " % (host or "localhost")) + PASSWD = getpass.getpass("IMAP password for %s on %s" % (USER, host or "localhost")) + test_mesg = 'From: %s@localhost\nSubject: IMAP4 test\n\ndata...\n' % USER test_seq1 = ( ('login', (USER, PASSWD)), ('create', ('/tmp/xxx 1',)), ('rename', ('/tmp/xxx 1', '/tmp/yyy')), ('CREATE', ('/tmp/yyz 2',)), - ('append', ('/tmp/yyz 2', None, None, 'From: anon@x.y.z\n\ndata...')), + ('append', ('/tmp/yyz 2', None, None, test_mesg)), ('list', ('/tmp', 'yy*')), ('select', ('/tmp/yyz 2',)), ('search', (None, '(TO zork)')), @@ -1038,7 +1051,7 @@ if __name__ == '__main__': ('response',('UIDVALIDITY',)), ('uid', ('SEARCH', 'ALL')), ('response', ('EXISTS',)), - ('append', (None, None, None, 'From: anon@x.y.z\n\ndata...')), + ('append', (None, None, None, test_mesg)), ('recent', ()), ('logout', ()), ) -- 2.40.0