1616# String method conversion by ESR, February 2001.
1717# GET/SETACL contributed by Anthony Baxter <[email protected] > April 2001. 1818
19- __version__ = "2.49 "
19+ __version__ = "2.50 "
2020
2121import binascii , re , socket , time , random , sys
2222
5252 'LSUB' : ('AUTH' , 'SELECTED' ),
5353 'NAMESPACE' : ('AUTH' , 'SELECTED' ),
5454 'NOOP' : ('NONAUTH' , 'AUTH' , 'SELECTED' , 'LOGOUT' ),
55- 'PARTIAL' : ('SELECTED' ,),
55+ 'PARTIAL' : ('SELECTED' ,), # NB: obsolete
5656 'RENAME' : ('AUTH' , 'SELECTED' ),
5757 'SEARCH' : ('SELECTED' ,),
5858 'SELECT' : ('AUTH' , 'SELECTED' ),
@@ -158,9 +158,12 @@ def __init__(self, host = '', port = IMAP4_PORT):
158158 # request and store CAPABILITY response.
159159
160160 if __debug__ :
161+ self ._cmd_log_len = 10
162+ self ._cmd_log_idx = 0
163+ self ._cmd_log = {} # Last `_cmd_log_len' interactions
161164 if self .debug >= 1 :
162- _mesg ('imaplib version %s' % __version__ )
163- _mesg ('new IMAP4 connection, tag=%s' % self .tagpre )
165+ self . _mesg ('imaplib version %s' % __version__ )
166+ self . _mesg ('new IMAP4 connection, tag=%s' % self .tagpre )
164167
165168 self .welcome = self ._get_response ()
166169 if self .untagged_responses .has_key ('PREAUTH' ):
@@ -178,7 +181,7 @@ def __init__(self, host = '', port = IMAP4_PORT):
178181
179182 if __debug__ :
180183 if self .debug >= 3 :
181- _mesg ('CAPABILITIES: %s' % `self.capabilities` )
184+ self . _mesg ('CAPABILITIES: %s' % `self.capabilities` )
182185
183186 for version in AllowedVersions :
184187 if not version in self .capabilities :
@@ -224,6 +227,7 @@ def send(self, data):
224227 """Send data to remote."""
225228 self .sock .sendall (data )
226229
230+
227231 def shutdown (self ):
228232 """Close I/O established in "open"."""
229233 self .file .close ()
@@ -482,7 +486,7 @@ def noop(self):
482486 """
483487 if __debug__ :
484488 if self .debug >= 3 :
485- _dump_ur (self .untagged_responses )
489+ self . _dump_ur (self .untagged_responses )
486490 return self ._simple_command ('NOOP' )
487491
488492
@@ -546,7 +550,7 @@ def select(self, mailbox='INBOX', readonly=None):
546550 and not readonly :
547551 if __debug__ :
548552 if self .debug >= 1 :
549- _dump_ur (self .untagged_responses )
553+ self . _dump_ur (self .untagged_responses )
550554 raise self .readonly ('%s is not writable' % mailbox )
551555 return typ , self .untagged_responses .get ('EXISTS' , [None ])
552556
@@ -663,7 +667,7 @@ def _append_untagged(self, typ, dat):
663667 ur = self .untagged_responses
664668 if __debug__ :
665669 if self .debug >= 5 :
666- _mesg ('untagged_responses[%s] %s += ["%s"]' %
670+ self . _mesg ('untagged_responses[%s] %s += ["%s"]' %
667671 (typ , len (ur .get (typ ,'' )), dat ))
668672 if ur .has_key (typ ):
669673 ur [typ ].append (dat )
@@ -709,9 +713,9 @@ def _command(self, name, *args):
709713
710714 if __debug__ :
711715 if self .debug >= 4 :
712- _mesg ('> %s' % data )
716+ self . _mesg ('> %s' % data )
713717 else :
714- _log ('> %s' % data )
718+ self . _log ('> %s' % data )
715719
716720 try :
717721 self .send ('%s%s' % (data , CRLF ))
@@ -735,7 +739,7 @@ def _command(self, name, *args):
735739
736740 if __debug__ :
737741 if self .debug >= 4 :
738- _mesg ('write literal size %s' % len (literal ))
742+ self . _mesg ('write literal size %s' % len (literal ))
739743
740744 try :
741745 self .send (literal )
@@ -814,7 +818,7 @@ def _get_response(self):
814818 size = int (self .mo .group ('size' ))
815819 if __debug__ :
816820 if self .debug >= 4 :
817- _mesg ('read literal size %s' % size )
821+ self . _mesg ('read literal size %s' % size )
818822 data = self .read (size )
819823
820824 # Store response with literal as tuple
@@ -834,7 +838,7 @@ def _get_response(self):
834838
835839 if __debug__ :
836840 if self .debug >= 1 and typ in ('NO' , 'BAD' , 'BYE' ):
837- _mesg ('%s response: %s' % (typ , dat ))
841+ self . _mesg ('%s response: %s' % (typ , dat ))
838842
839843 return resp
840844
@@ -857,7 +861,7 @@ def _get_tagged_response(self, tag):
857861 except self .abort , val :
858862 if __debug__ :
859863 if self .debug >= 1 :
860- print_log ()
864+ self . print_log ()
861865 raise
862866
863867
@@ -872,9 +876,9 @@ def _get_line(self):
872876 line = line [:- 2 ]
873877 if __debug__ :
874878 if self .debug >= 4 :
875- _mesg ('< %s' % line )
879+ self . _mesg ('< %s' % line )
876880 else :
877- _log ('< %s' % line )
881+ self . _log ('< %s' % line )
878882 return line
879883
880884
@@ -886,7 +890,7 @@ def _match(self, cre, s):
886890 self .mo = cre .match (s )
887891 if __debug__ :
888892 if self .mo is not None and self .debug >= 5 :
889- _mesg ("\t matched r'%s' => %s" % (cre .pattern , `self.mo.groups()` ))
893+ self . _mesg ("\t matched r'%s' => %s" % (cre .pattern , `self.mo.groups()` ))
890894 return self .mo is not None
891895
892896
@@ -934,11 +938,49 @@ def _untagged_response(self, typ, dat, name):
934938 data = self .untagged_responses [name ]
935939 if __debug__ :
936940 if self .debug >= 5 :
937- _mesg ('untagged_responses[%s] => %s' % (name , data ))
941+ self . _mesg ('untagged_responses[%s] => %s' % (name , data ))
938942 del self .untagged_responses [name ]
939943 return typ , data
940944
941945
946+ if __debug__ :
947+
948+ def _mesg (self , s , secs = None ):
949+ if secs is None :
950+ secs = time .time ()
951+ tm = time .strftime ('%M:%S' , time .localtime (secs ))
952+ sys .stderr .write (' %s.%02d %s\n ' % (tm , (secs * 100 )% 100 , s ))
953+ sys .stderr .flush ()
954+
955+ def _dump_ur (self , dict ):
956+ # Dump untagged responses (in `dict').
957+ l = dict .items ()
958+ if not l : return
959+ t = '\n \t \t '
960+ l = map (lambda x :'%s: "%s"' % (x [0 ], x [1 ][0 ] and '" "' .join (x [1 ]) or '' ), l )
961+ self ._mesg ('untagged responses dump:%s%s' % (t , t .join (l )))
962+
963+ def _log (self , line ):
964+ # Keep log of last `_cmd_log_len' interactions for debugging.
965+ self ._cmd_log [self ._cmd_log_idx ] = (line , time .time ())
966+ self ._cmd_log_idx += 1
967+ if self ._cmd_log_idx >= self ._cmd_log_len :
968+ self ._cmd_log_idx = 0
969+
970+ def print_log (self ):
971+ self ._mesg ('last %d IMAP4 interactions:' % len (self ._cmd_log ))
972+ i , n = self ._cmd_log_idx , self ._cmd_log_len
973+ while n :
974+ try :
975+ apply (self ._mesg , self ._cmd_log [i ])
976+ except :
977+ pass
978+ i += 1
979+ if i >= self ._cmd_log_len :
980+ i = 0
981+ n -= 1
982+
983+
942984
943985class _Authenticator :
944986
@@ -1082,39 +1124,6 @@ def Time2Internaldate(date_time):
10821124
10831125
10841126
1085- if __debug__ :
1086-
1087- def _mesg (s , secs = None ):
1088- if secs is None :
1089- secs = time .time ()
1090- tm = time .strftime ('%M:%S' , time .localtime (secs ))
1091- sys .stderr .write (' %s.%02d %s\n ' % (tm , (secs * 100 )% 100 , s ))
1092- sys .stderr .flush ()
1093-
1094- def _dump_ur (dict ):
1095- # Dump untagged responses (in `dict').
1096- l = dict .items ()
1097- if not l : return
1098- t = '\n \t \t '
1099- l = map (lambda x :'%s: "%s"' % (x [0 ], x [1 ][0 ] and '" "' .join (x [1 ]) or '' ), l )
1100- _mesg ('untagged responses dump:%s%s' % (t , t .join (l )))
1101-
1102- _cmd_log = [] # Last `_cmd_log_len' interactions
1103- _cmd_log_len = 10
1104-
1105- def _log (line ):
1106- # Keep log of last `_cmd_log_len' interactions for debugging.
1107- if len (_cmd_log ) == _cmd_log_len :
1108- del _cmd_log [0 ]
1109- _cmd_log .append ((time .time (), line ))
1110-
1111- def print_log ():
1112- _mesg ('last %d IMAP4 interactions:' % len (_cmd_log ))
1113- for secs ,line in _cmd_log :
1114- _mesg (line , secs )
1115-
1116-
1117-
11181127if __name__ == '__main__' :
11191128
11201129 import getopt , getpass
@@ -1145,7 +1154,7 @@ def print_log():
11451154 ('list' , ('/tmp' , 'yy*' )),
11461155 ('select' , ('/tmp/yyz 2' ,)),
11471156 ('search' , (None , 'SUBJECT' , 'test' )),
1148- ('partial ' , ('1' , 'RFC822' , 1 , 1024 )),
1157+ ('fetch ' , ('1' , '(FLAGS INTERNALDATE RFC822)' )),
11491158 ('store' , ('1' , 'FLAGS' , '(\Deleted)' )),
11501159 ('namespace' , ()),
11511160 ('expunge' , ()),
@@ -1164,15 +1173,15 @@ def print_log():
11641173 )
11651174
11661175 def run (cmd , args ):
1167- _mesg ('%s %s' % (cmd , args ))
1176+ M . _mesg ('%s %s' % (cmd , args ))
11681177 typ , dat = apply (getattr (M , cmd ), args )
1169- _mesg ('%s => %s %s' % (cmd , typ , dat ))
1178+ M . _mesg ('%s => %s %s' % (cmd , typ , dat ))
11701179 return dat
11711180
11721181 try :
11731182 M = IMAP4 (host )
1174- _mesg ('PROTOCOL_VERSION = %s' % M .PROTOCOL_VERSION )
1175- _mesg ('CAPABILITIES = %s' % `M.capabilities` )
1183+ M . _mesg ('PROTOCOL_VERSION = %s' % M .PROTOCOL_VERSION )
1184+ M . _mesg ('CAPABILITIES = %s' % `M.capabilities` )
11761185
11771186 for cmd ,args in test_seq1 :
11781187 run (cmd , args )
0 commit comments