blob: 0ec6f03dd95a185bce57237025cc0c82bb4733b5 [file] [log] [blame]
Guido van Rossumc2c07fa1998-04-09 13:51:46 +00001"""IMAP4 client.
2
3Based on RFC 2060.
4
5Author: Piers Lauder <piers@cs.su.oz.au> December 1997.
6
7Public class: IMAP4
8Public variable: Debug
9Public functions: Internaldate2tuple
10 Int2AP
11 ParseFlags
12 Time2Internaldate
13"""
14
Guido van Rossumeeec0af1998-04-09 14:20:31 +000015import re, socket, string, time, whrandom
Guido van Rossumc2c07fa1998-04-09 13:51:46 +000016
17# Globals
18
19CRLF = '\r\n'
20Debug = 0
21IMAP4_PORT = 143
22
23# Commands
24
25Commands = {
26 # name valid states
27 'APPEND': ('AUTH', 'SELECTED'),
28 'AUTHENTICATE': ('NONAUTH',),
29 'CAPABILITY': ('NONAUTH', 'AUTH', 'SELECTED', 'LOGOUT'),
30 'CHECK': ('SELECTED',),
31 'CLOSE': ('SELECTED',),
32 'COPY': ('SELECTED',),
33 'CREATE': ('AUTH', 'SELECTED'),
34 'DELETE': ('AUTH', 'SELECTED'),
35 'EXAMINE': ('AUTH', 'SELECTED'),
36 'EXPUNGE': ('SELECTED',),
37 'FETCH': ('SELECTED',),
38 'LIST': ('AUTH', 'SELECTED'),
39 'LOGIN': ('NONAUTH',),
40 'LOGOUT': ('NONAUTH', 'AUTH', 'SELECTED', 'LOGOUT'),
41 'LSUB': ('AUTH', 'SELECTED'),
42 'NOOP': ('NONAUTH', 'AUTH', 'SELECTED', 'LOGOUT'),
43 'RENAME': ('AUTH', 'SELECTED'),
44 'SEARCH': ('SELECTED',),
45 'SELECT': ('AUTH', 'SELECTED'),
46 'STATUS': ('AUTH', 'SELECTED'),
47 'STORE': ('SELECTED',),
48 'SUBSCRIBE': ('AUTH', 'SELECTED'),
49 'UID': ('SELECTED',),
50 'UNSUBSCRIBE': ('AUTH', 'SELECTED'),
51 }
52
53# Patterns to match server responses
54
55Continuation = re.compile(r'\+ (?P<data>.*)')
56Flags = re.compile(r'.*FLAGS \((?P<flags>[^\)]*)\)')
57InternalDate = re.compile(r'.*INTERNALDATE "'
58 r'(?P<day>[ 123][0-9])-(?P<mon>[A-Z][a-z][a-z])-(?P<year>[0-9][0-9][0-9][0-9])'
59 r' (?P<hour>[0-9][0-9]):(?P<min>[0-9][0-9]):(?P<sec>[0-9][0-9])'
60 r' (?P<zonen>[-+])(?P<zoneh>[0-9][0-9])(?P<zonem>[0-9][0-9])'
61 r'"')
62Literal = re.compile(r'(?P<data>.*) {(?P<size>\d+)}$')
63Response_code = re.compile(r'\[(?P<type>[A-Z-]+)( (?P<data>[^\]]*))?\]')
64Untagged_response = re.compile(r'\* (?P<type>[A-Z-]+) (?P<data>.*)')
65Untagged_status = re.compile(r'\* (?P<data>\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?')
66
67
68
69class IMAP4:
70
71 """IMAP4 client class.
72
73 Instantiate with: IMAP4([host[, port]])
74
75 host - host's name (default: localhost);
76 port - port number (default: standard IMAP4 port).
77
78 All IMAP4rev1 commands are supported by methods of the same
79 name (in lower-case). Each command returns a tuple: (type, [data, ...])
80 where 'type' is usually 'OK' or 'NO', and 'data' is either the
81 text from the tagged response, or untagged results from command.
82
83 Errors raise the exception class <instance>.error("<reason>").
84 IMAP4 server errors raise <instance>.abort("<reason>"),
85 which is a sub-class of 'error'.
86 """
87
88 class error(Exception): pass # Logical errors - debug required
89 class abort(error): pass # Service errors - close and retry
Guido van Rossumc2c07fa1998-04-09 13:51:46 +000090
91
92 def __init__(self, host = '', port = IMAP4_PORT):
93 self.host = host
94 self.port = port
95 self.debug = Debug
96 self.state = 'LOGOUT'
97 self.tagged_commands = {} # Tagged commands awaiting response
98 self.untagged_responses = {} # {typ: [data, ...], ...}
99 self.continuation_response = '' # Last continuation response
100 self.tagnum = 0
101
102 # Open socket to server.
103
104 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
105 self.sock.connect(self.host, self.port)
106 self.file = self.sock.makefile('r')
107
108 # Create unique tag for this session,
109 # and compile tagged response matcher.
110
Guido van Rossumeeec0af1998-04-09 14:20:31 +0000111 self.tagpre = Int2AP(whrandom.random()*32000)
Guido van Rossumc2c07fa1998-04-09 13:51:46 +0000112 self.tagre = re.compile(r'(?P<tag>'
113 + self.tagpre
114 + r'\d+) (?P<type>[A-Z]+) (?P<data>.*)')
115
116 # Get server welcome message,
117 # request and store CAPABILITY response.
118
119 if __debug__ and self.debug >= 1:
120 print '\tnew IMAP4 connection, tag=%s' % self.tagpre
121
122 self.welcome = self._get_response()
123 if self.untagged_responses.has_key('PREAUTH'):
124 self.state = 'AUTH'
125 elif self.untagged_responses.has_key('OK'):
126 self.state = 'NONAUTH'
127# elif self.untagged_responses.has_key('BYE'):
128 else:
129 raise self.error(self.welcome)
130
131 cap = 'CAPABILITY'
132 self._simple_command(cap)
133 if not self.untagged_responses.has_key(cap):
134 raise self.error('no CAPABILITY response from server')
135 self.capabilities = tuple(string.split(self.untagged_responses[cap][-1]))
136 if not 'IMAP4REV1' in self.capabilities:
137 raise self.error('server not IMAP4REV1 compliant')
138
139 if __debug__ and self.debug >= 3:
140 print '\tCAPABILITIES: %s' % `self.capabilities`
141
142
143 def __getattr__(self, attr):
144 """Allow UPPERCASE variants of all following IMAP4 commands."""
145 if Commands.has_key(attr):
146 return eval("self.%s" % string.lower(attr))
147 raise AttributeError("Unknown IMAP4 command: '%s'" % attr)
148
149
150 # Public methods
151
152
153 def append(self, mailbox, flags, date_time, message):
154 """Append message to named mailbox.
155
156 (typ, [data]) = <instance>.append(mailbox, flags, date_time, message)
157 """
158 name = 'APPEND'
159 if flags:
160 flags = '(%s)' % flags
161 else:
162 flags = None
163 if date_time:
164 date_time = Time2Internaldate(date_time)
165 else:
166 date_time = None
167 tag = self._command(name, mailbox, flags, date_time, message)
168 return self._command_complete(name, tag)
169
170
171 def authenticate(self, func):
172 """Authenticate command - requires response processing.
173
174 UNIMPLEMENTED
175 """
176 raise self.error('UNIMPLEMENTED')
177
178
179 def check(self):
180 """Checkpoint mailbox on server.
181
182 (typ, [data]) = <instance>.check()
183 """
184 return self._simple_command('CHECK')
185
186
187 def close(self):
188 """Close currently selected mailbox.
Guido van Rossumeeec0af1998-04-09 14:20:31 +0000189
Guido van Rossumc2c07fa1998-04-09 13:51:46 +0000190 Deleted messages are removed from writable mailbox.
191 This is the recommended command before 'LOGOUT'.
192
193 (typ, [data]) = <instance>.close()
194 """
195 try:
196 try: typ, dat = self._simple_command('CLOSE')
197 except EOFError: typ, dat = None, [None]
198 finally:
199 self.state = 'AUTH'
200 return typ, dat
201
202
203 def copy(self, message_set, new_mailbox):
204 """Copy 'message_set' messages onto end of 'new_mailbox'.
205
206 (typ, [data]) = <instance>.copy(message_set, new_mailbox)
207 """
208 return self._simple_command('COPY', message_set, new_mailbox)
209
210
211 def create(self, mailbox):
212 """Create new mailbox.
213
214 (typ, [data]) = <instance>.create(mailbox)
215 """
216 return self._simple_command('CREATE', mailbox)
217
218
219 def delete(self, mailbox):
220 """Delete old mailbox.
221
222 (typ, [data]) = <instance>.delete(mailbox)
223 """
224 return self._simple_command('DELETE', mailbox)
225
226
227 def expunge(self):
228 """Permanently remove deleted items from selected mailbox.
Guido van Rossumeeec0af1998-04-09 14:20:31 +0000229
Guido van Rossumc2c07fa1998-04-09 13:51:46 +0000230 Generates 'EXPUNGE' response for each deleted message.
231
232 (typ, [data]) = <instance>.expunge()
233
234 'data' is list of 'EXPUNGE'd message numbers in order received.
235 """
236 name = 'EXPUNGE'
237 typ, dat = self._simple_command(name)
238 return self._untagged_response(typ, name)
239
240
241 def fetch(self, message_set, message_parts):
242 """Fetch (parts of) messages.
243
244 (typ, [data, ...]) = <instance>.fetch(message_set, message_parts)
245
246 'data' are tuples of message part envelope and data.
247 """
248 name = 'FETCH'
249 typ, dat = self._simple_command(name, message_set, message_parts)
250 return self._untagged_response(typ, name)
251
252
253 def list(self, directory='""', pattern='*'):
254 """List mailbox names in directory matching pattern.
255
256 (typ, [data]) = <instance>.list(directory='""', pattern='*')
257
258 'data' is list of LIST responses.
259 """
260 name = 'LIST'
261 typ, dat = self._simple_command(name, directory, pattern)
262 return self._untagged_response(typ, name)
263
264
265 def login(self, user, password):
266 """Identify client using plaintext password.
267
268 (typ, [data]) = <instance>.list(user, password)
269 """
270 if not 'AUTH=LOGIN' in self.capabilities:
271 raise self.error("server doesn't allow LOGIN authorisation")
272 typ, dat = self._simple_command('LOGIN', user, password)
273 if typ != 'OK':
274 raise self.error(dat)
275 self.state = 'AUTH'
276 return typ, dat
277
278
279 def logout(self):
280 """Shutdown connection to server.
281
282 (typ, [data]) = <instance>.logout()
283
284 Returns server 'BYE' response.
285 """
286 self.state = 'LOGOUT'
287 try: typ, dat = self._simple_command('LOGOUT')
288 except EOFError: typ, dat = None, [None]
289 self.file.close()
290 self.sock.close()
291 if self.untagged_responses.has_key('BYE'):
292 return 'BYE', self.untagged_responses['BYE']
293 return typ, dat
294
295
296 def lsub(self, directory='""', pattern='*'):
297 """List 'subscribed' mailbox names in directory matching pattern.
298
299 (typ, [data, ...]) = <instance>.lsub(directory='""', pattern='*')
300
301 'data' are tuples of message part envelope and data.
302 """
303 name = 'LSUB'
304 typ, dat = self._simple_command(name, directory, pattern)
305 return self._untagged_response(typ, name)
306
307
308 def recent(self):
309 """Prompt server for an update.
310
311 (typ, [data]) = <instance>.recent()
312
313 'data' is None if no new messages,
314 else value of RECENT response.
315 """
316 name = 'RECENT'
317 typ, dat = self._untagged_response('OK', name)
318 if dat[-1]:
319 return typ, dat
320 typ, dat = self._simple_command('NOOP')
321 return self._untagged_response(typ, name)
322
323
324 def rename(self, oldmailbox, newmailbox):
325 """Rename old mailbox name to new.
326
327 (typ, data) = <instance>.rename(oldmailbox, newmailbox)
328 """
329 return self._simple_command('RENAME', oldmailbox, newmailbox)
330
331
332 def response(self, code):
333 """Return data for response 'code' if received, or None.
334
335 (code, [data]) = <instance>.response(code)
336 """
337 return code, self.untagged_responses.get(code, [None])
338
339
340 def search(self, charset, criteria):
341 """Search mailbox for matching messages.
342
343 (typ, [data]) = <instance>.search(charset, criteria)
344
345 'data' is space separated list of matching message numbers.
346 """
347 name = 'SEARCH'
348 if charset:
349 charset = 'CHARSET ' + charset
350 typ, dat = self._simple_command(name, charset, criteria)
351 return self._untagged_response(typ, name)
352
353
354 def select(self, mailbox='INBOX', readonly=None):
355 """Select a mailbox.
356
357 (typ, [data]) = <instance>.select(mailbox='INBOX', readonly=None)
358
359 'data' is count of messages in mailbox ('EXISTS' response).
360 """
361 # Mandated responses are ('FLAGS', 'EXISTS', 'RECENT', 'UIDVALIDITY')
362 # Remove immediately interesting responses
363 for r in ('EXISTS', 'READ-WRITE'):
364 if self.untagged_responses.has_key(r):
365 del self.untagged_responses[r]
366 if readonly:
367 name = 'EXAMINE'
368 else:
369 name = 'SELECT'
370 typ, dat = self._simple_command(name, mailbox)
371 if typ == 'OK':
372 self.state = 'SELECTED'
373 elif typ == 'NO':
374 self.state = 'AUTH'
375 if not readonly and not self.untagged_responses.has_key('READ-WRITE'):
376 raise self.error('%s is not writable' % mailbox)
377 return typ, self.untagged_responses.get('EXISTS', [None])
378
379
380 def status(self, mailbox, names):
381 """Request named status conditions for mailbox.
382
383 (typ, [data]) = <instance>.status(mailbox, names)
384 """
385 name = 'STATUS'
386 typ, dat = self._simple_command(name, mailbox, names)
387 return self._untagged_response(typ, name)
388
389
390 def store(self, message_set, command, flag_list):
391 """Alters flag dispositions for messages in mailbox.
392
393 (typ, [data]) = <instance>.store(message_set, command, flag_list)
394 """
395 command = '%s %s' % (command, flag_list)
396 typ, dat = self._simple_command('STORE', message_set, command)
397 return self._untagged_response(typ, 'FETCH')
398
399
400 def subscribe(self, mailbox):
401 """Subscribe to new mailbox.
402
403 (typ, [data]) = <instance>.subscribe(mailbox)
404 """
405 return self._simple_command('SUBSCRIBE', mailbox)
406
407
408 def uid(self, command, args):
Guido van Rossumeeec0af1998-04-09 14:20:31 +0000409 """Execute "command args" with messages identified by UID, rather than message number.
Guido van Rossumc2c07fa1998-04-09 13:51:46 +0000410
411 (typ, [data]) = <instance>.uid(command, args)
412
413 Returns response appropriate to 'command'.
414 """
415 name = 'UID'
416 typ, dat = self._simple_command('UID', command, args)
417 if command == 'SEARCH':
418 name = 'SEARCH'
419 else:
420 name = 'FETCH'
421 typ, dat2 = self._untagged_response(typ, name)
422 if dat2[-1]: dat = dat2
423 return typ, dat
424
425
426 def unsubscribe(self, mailbox):
427 """Unsubscribe from old mailbox.
428
429 (typ, [data]) = <instance>.unsubscribe(mailbox)
430 """
431 return self._simple_command('UNSUBSCRIBE', mailbox)
432
433
434 def xatom(self, name, arg1=None, arg2=None):
Guido van Rossumeeec0af1998-04-09 14:20:31 +0000435 """Allow simple extension commands notified by server in CAPABILITY response.
Guido van Rossumc2c07fa1998-04-09 13:51:46 +0000436
437 (typ, [data]) = <instance>.xatom(name, arg1=None, arg2=None)
438 """
439 if name[0] != 'X' or not name in self.capabilities:
440 raise self.error('unknown extension command: %s' % name)
441 return self._simple_command(name, arg1, arg2)
442
443
444
445 # Private methods
446
447
448 def _append_untagged(self, typ, dat):
449
450 if self.untagged_responses.has_key(typ):
451 self.untagged_responses[typ].append(dat)
452 else:
453 self.untagged_responses[typ] = [dat]
454
455 if __debug__ and self.debug >= 5:
456 print '\tuntagged_responses[%s] += %.20s..' % (typ, `dat`)
457
458
459 def _command(self, name, dat1=None, dat2=None, dat3=None, literal=None):
460
461 if self.state not in Commands[name]:
462 raise self.error(
463 'command %s illegal in state %s' % (name, self.state))
464
465 tag = self._new_tag()
466 data = '%s %s' % (tag, name)
467 for d in (dat1, dat2, dat3):
468 if d is not None: data = '%s %s' % (data, d)
469 if literal is not None:
470 data = '%s {%s}' % (data, len(literal))
471
472 try:
473 self.sock.send('%s%s' % (data, CRLF))
474 except socket.error, val:
475 raise self.abort('socket error: %s' % val)
476
477 if __debug__ and self.debug >= 4:
478 print '\t> %s' % data
479
480 if literal is None:
481 return tag
482
483 # Wait for continuation response
484
485 while self._get_response():
486 if self.tagged_commands[tag]: # BAD/NO?
487 return tag
488
489 # Send literal
490
491 if __debug__ and self.debug >= 4:
492 print '\twrite literal size %s' % len(literal)
493
494 try:
495 self.sock.send(literal)
496 self.sock.send(CRLF)
497 except socket.error, val:
498 raise self.abort('socket error: %s' % val)
499
500 return tag
501
502
503 def _command_complete(self, name, tag):
504 try:
505 typ, data = self._get_tagged_response(tag)
506 except self.abort, val:
507 raise self.abort('command: %s => %s' % (name, val))
508 except self.error, val:
509 raise self.error('command: %s => %s' % (name, val))
510 if self.untagged_responses.has_key('BYE') and name != 'LOGOUT':
511 raise self.abort(self.untagged_responses['BYE'][-1])
512 if typ == 'BAD':
513 raise self.error('%s command error: %s %s' % (name, typ, data))
514 return typ, data
515
516
517 def _get_response(self):
518
519 # Read response and store.
520 #
521 # Returns None for continuation responses,
522 # otherwise first response line received
523
524 # Protocol mandates all lines terminated by CRLF.
525
526 resp = self._get_line()[:-2]
527
528 # Command completion response?
529
530 if self._match(self.tagre, resp):
531 tag = self.mo.group('tag')
532 if not self.tagged_commands.has_key(tag):
533 raise self.abort('unexpected tagged response: %s' % resp)
534
535 typ = self.mo.group('type')
536 dat = self.mo.group('data')
537 self.tagged_commands[tag] = (typ, [dat])
538 else:
539 dat2 = None
540
541 # '*' (untagged) responses?
542
543 if not self._match(Untagged_response, resp):
544 if self._match(Untagged_status, resp):
545 dat2 = self.mo.group('data2')
546
547 if self.mo is None:
548 # Only other possibility is '+' (continuation) rsponse...
549
550 if self._match(Continuation, resp):
551 self.continuation_response = self.mo.group('data')
552 return None # NB: indicates continuation
553
554 raise self.abort('unexpected response: %s' % resp)
555
556 typ = self.mo.group('type')
557 dat = self.mo.group('data')
558 if dat2: dat = dat + ' ' + dat2
559
560 # Is there a literal to come?
561
562 while self._match(Literal, dat):
563
564 # Read literal direct from connection.
565
566 size = string.atoi(self.mo.group('size'))
567 if __debug__ and self.debug >= 4:
568 print '\tread literal size %s' % size
569 data = self.file.read(size)
570
571 # Store response with literal as tuple
572
573 self._append_untagged(typ, (dat, data))
574
575 # Read trailer - possibly containing another literal
576
577 dat = self._get_line()[:-2]
578
579 self._append_untagged(typ, dat)
580
581 # Bracketed response information?
582
583 if typ in ('OK', 'NO', 'BAD') and self._match(Response_code, dat):
584 self._append_untagged(self.mo.group('type'), self.mo.group('data'))
585
586 return resp
587
588
589 def _get_tagged_response(self, tag):
590
591 while 1:
592 result = self.tagged_commands[tag]
593 if result is not None:
594 del self.tagged_commands[tag]
595 return result
596 self._get_response()
597
598
599 def _get_line(self):
600
601 line = self.file.readline()
602 if not line:
603 raise EOFError
604
605 # Protocol mandates all lines terminated by CRLF
606
607 if __debug__ and self.debug >= 4:
608 print '\t< %s' % line[:-2]
609 return line
610
611
612 def _match(self, cre, s):
613
614 # Run compiled regular expression match method on 's'.
615 # Save result, return success.
616
617 self.mo = cre.match(s)
618 if __debug__ and self.mo is not None and self.debug >= 5:
619 print "\tmatched r'%s' => %s" % (cre.pattern, `self.mo.groups()`)
620 return self.mo is not None
621
622
623 def _new_tag(self):
624
625 tag = '%s%s' % (self.tagpre, self.tagnum)
626 self.tagnum = self.tagnum + 1
627 self.tagged_commands[tag] = None
628 return tag
629
630
631 def _simple_command(self, name, dat1=None, dat2=None):
632
633 return self._command_complete(name, self._command(name, dat1, dat2))
634
635
636 def _untagged_response(self, typ, name):
637
638 if not self.untagged_responses.has_key(name):
639 return typ, [None]
640 data = self.untagged_responses[name]
641 del self.untagged_responses[name]
642 return typ, data
643
644
645
646Mon2num = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
647 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
648
649def Internaldate2tuple(resp):
650
Guido van Rossumeeec0af1998-04-09 14:20:31 +0000651 """Convert IMAP4 INTERNALDATE to UT.
Guido van Rossumc2c07fa1998-04-09 13:51:46 +0000652
Guido van Rossumeeec0af1998-04-09 14:20:31 +0000653 Returns Python time module tuple.
Guido van Rossumc2c07fa1998-04-09 13:51:46 +0000654 """
655
656 mo = InternalDate.match(resp)
657 if not mo:
658 return None
659
660 mon = Mon2num[mo.group('mon')]
661 zonen = mo.group('zonen')
662
663 for name in ('day', 'year', 'hour', 'min', 'sec', 'zoneh', 'zonem'):
664 exec "%s = string.atoi(mo.group('%s'))" % (name, name)
665
666 # INTERNALDATE timezone must be subtracted to get UT
667
668 zone = (zoneh*60 + zonem)*60
669 if zonen == '-':
670 zone = -zone
671
672 tt = (year, mon, day, hour, min, sec, -1, -1, -1)
673
674 utc = time.mktime(tt)
675
676 # Following is necessary because the time module has no 'mkgmtime'.
677 # 'mktime' assumes arg in local timezone, so adds timezone/altzone.
678
679 lt = time.localtime(utc)
680 if time.daylight and lt[-1]:
681 zone = zone + time.altzone
682 else:
683 zone = zone + time.timezone
684
685 return time.localtime(utc - zone)
686
687
688
689def Int2AP(num):
690
Guido van Rossumeeec0af1998-04-09 14:20:31 +0000691 """Convert integer to A-P string representation."""
Guido van Rossumc2c07fa1998-04-09 13:51:46 +0000692
693 val = ''; AP = 'ABCDEFGHIJKLMNOP'
Guido van Rossumeeec0af1998-04-09 14:20:31 +0000694 num = int(abs(num))
Guido van Rossumc2c07fa1998-04-09 13:51:46 +0000695 while num:
696 num, mod = divmod(num, 16)
697 val = AP[mod] + val
698 return val
699
700
701
702def ParseFlags(resp):
703
Guido van Rossumeeec0af1998-04-09 14:20:31 +0000704 """Convert IMAP4 flags response to python tuple."""
Guido van Rossumc2c07fa1998-04-09 13:51:46 +0000705
706 mo = Flags.match(resp)
707 if not mo:
708 return ()
709
710 return tuple(string.split(mo.group('flags')))
711
712
713def Time2Internaldate(date_time):
714
715 """Convert 'date_time' to IMAP4 INTERNALDATE representation.
716
717 Return string in form: '"DD-Mmm-YYYY HH:MM:SS +HHMM"'
718 """
719
720 dttype = type(date_time)
721 if dttype is type(1):
722 tt = time.localtime(date_time)
723 elif dttype is type(()):
724 tt = date_time
725 elif dttype is type(""):
726 return date_time # Assume in correct format
727 else: raise ValueError
728
729 dt = time.strftime("%d-%b-%Y %H:%M:%S", tt)
730 if dt[0] == '0':
731 dt = ' ' + dt[1:]
732 if time.daylight and tt[-1]:
733 zone = -time.altzone
734 else:
735 zone = -time.timezone
736 return '"' + dt + " %+02d%02d" % divmod(zone/60, 60) + '"'
737
738
739
740if __debug__ and __name__ == '__main__':
741
742 import getpass
743 USER = getpass.getuser()
744 PASSWD = getpass.getpass()
745
746 test_seq1 = (
747 ('login', (USER, PASSWD)),
748 ('create', ('/tmp/xxx',)),
749 ('rename', ('/tmp/xxx', '/tmp/yyy')),
750 ('CREATE', ('/tmp/yyz',)),
751 ('append', ('/tmp/yyz', None, None, 'From: anon@x.y.z\n\ndata...')),
752 ('select', ('/tmp/yyz',)),
753 ('recent', ()),
754 ('uid', ('SEARCH', 'ALL')),
755 ('fetch', ('1', '(INTERNALDATE RFC822)')),
756 ('store', ('1', 'FLAGS', '(\Deleted)')),
757 ('expunge', ()),
758 ('close', ()),
759 )
760
761 test_seq2 = (
762 ('select', ()),
763 ('response',('UIDVALIDITY',)),
764 ('uid', ('SEARCH', 'ALL')),
765 ('recent', ()),
766 ('response', ('EXISTS',)),
767 ('logout', ()),
768 )
769
770 def run(cmd, args):
771 typ, dat = apply(eval('M.%s' % cmd), args)
772 print ' %s %s\n => %s %s' % (cmd, args, typ, dat)
773 return dat
774
775 Debug = 4
776 M = IMAP4()
777
778 for cmd,args in test_seq1:
779 run(cmd, args)
780
781 for ml in M.list('/tmp/', 'yy%')[1]:
782 path = string.split(ml)[-1]
783 print '%s %s' % M.delete(path)
784
785 for cmd,args in test_seq2:
786 dat = run(cmd, args)
787
788 if (cmd,args) == ('uid', ('SEARCH', 'ALL')):
789 uid = string.split(dat[0])[-1]
790 run('uid', ('FETCH', '%s (FLAGS INTERNALDATE RFC822.SIZE RFC822.HEADER RFC822)' % uid))