imaplib.py 35.8 KB
Newer Older
1 2 3 4
"""IMAP4 client.

Based on RFC 2060.

Tim Peters's avatar
Tim Peters committed
5 6 7 8 9 10
Public class:           IMAP4
Public variable:        Debug
Public functions:       Internaldate2tuple
                        Int2AP
                        ParseFlags
                        Time2Internaldate
11
"""
12

13
# Author: Piers Lauder <piers@cs.su.oz.au> December 1997.
Tim Peters's avatar
Tim Peters committed
14
#
15
# Authentication code contributed by Donn Cave <donn@u.washington.edu> June 1998.
16
# String method conversion by ESR, February 2001.
Piers Lauder's avatar
Piers Lauder committed
17
# GET/SETACL contributed by Anthony Baxter <anthony@interlink.com.au> April 2001.
18

Piers Lauder's avatar
Piers Lauder committed
19
__version__ = "2.47"
20

21
import binascii, re, socket, time, random, sys
22

23 24
__all__ = ["IMAP4", "Internaldate2tuple",
           "Int2AP", "ParseFlags", "Time2Internaldate"]
25

Tim Peters's avatar
Tim Peters committed
26
#       Globals
27 28 29 30

CRLF = '\r\n'
Debug = 0
IMAP4_PORT = 143
Tim Peters's avatar
Tim Peters committed
31
AllowedVersions = ('IMAP4REV1', 'IMAP4')        # Most recent first
32

Tim Peters's avatar
Tim Peters committed
33
#       Commands
34 35

Commands = {
Tim Peters's avatar
Tim Peters committed
36 37 38 39 40 41 42 43 44 45 46 47
        # name            valid states
        'APPEND':       ('AUTH', 'SELECTED'),
        'AUTHENTICATE': ('NONAUTH',),
        'CAPABILITY':   ('NONAUTH', 'AUTH', 'SELECTED', 'LOGOUT'),
        'CHECK':        ('SELECTED',),
        'CLOSE':        ('SELECTED',),
        'COPY':         ('SELECTED',),
        'CREATE':       ('AUTH', 'SELECTED'),
        'DELETE':       ('AUTH', 'SELECTED'),
        'EXAMINE':      ('AUTH', 'SELECTED'),
        'EXPUNGE':      ('SELECTED',),
        'FETCH':        ('SELECTED',),
Piers Lauder's avatar
Piers Lauder committed
48
        'GETACL':       ('AUTH', 'SELECTED'),
Tim Peters's avatar
Tim Peters committed
49 50 51 52
        'LIST':         ('AUTH', 'SELECTED'),
        'LOGIN':        ('NONAUTH',),
        'LOGOUT':       ('NONAUTH', 'AUTH', 'SELECTED', 'LOGOUT'),
        'LSUB':         ('AUTH', 'SELECTED'),
Piers Lauder's avatar
Piers Lauder committed
53
        'NAMESPACE':    ('AUTH', 'SELECTED'),
Tim Peters's avatar
Tim Peters committed
54 55 56 57 58
        'NOOP':         ('NONAUTH', 'AUTH', 'SELECTED', 'LOGOUT'),
        'PARTIAL':      ('SELECTED',),
        'RENAME':       ('AUTH', 'SELECTED'),
        'SEARCH':       ('SELECTED',),
        'SELECT':       ('AUTH', 'SELECTED'),
Piers Lauder's avatar
Piers Lauder committed
59 60
        'SETACL':       ('AUTH', 'SELECTED'),
        'SORT':         ('SELECTED',),
Tim Peters's avatar
Tim Peters committed
61 62 63 64 65 66 67 68
        'STATUS':       ('AUTH', 'SELECTED'),
        'STORE':        ('SELECTED',),
        'SUBSCRIBE':    ('AUTH', 'SELECTED'),
        'UID':          ('SELECTED',),
        'UNSUBSCRIBE':  ('AUTH', 'SELECTED'),
        }

#       Patterns to match server responses
69

70
Continuation = re.compile(r'\+( (?P<data>.*))?')
71 72
Flags = re.compile(r'.*FLAGS \((?P<flags>[^\)]*)\)')
InternalDate = re.compile(r'.*INTERNALDATE "'
Tim Peters's avatar
Tim Peters committed
73 74 75 76
        r'(?P<day>[ 123][0-9])-(?P<mon>[A-Z][a-z][a-z])-(?P<year>[0-9][0-9][0-9][0-9])'
        r' (?P<hour>[0-9][0-9]):(?P<min>[0-9][0-9]):(?P<sec>[0-9][0-9])'
        r' (?P<zonen>[-+])(?P<zoneh>[0-9][0-9])(?P<zonem>[0-9][0-9])'
        r'"')
77
Literal = re.compile(r'.*{(?P<size>\d+)}$')
78
Response_code = re.compile(r'\[(?P<type>[A-Z-]+)( (?P<data>[^\]]*))?\]')
79
Untagged_response = re.compile(r'\* (?P<type>[A-Z-]+)( (?P<data>.*))?')
80 81 82 83 84 85
Untagged_status = re.compile(r'\* (?P<data>\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?')



class IMAP4:

Tim Peters's avatar
Tim Peters committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    """IMAP4 client class.

    Instantiate with: IMAP4([host[, port]])

            host - host's name (default: localhost);
            port - port number (default: standard IMAP4 port).

    All IMAP4rev1 commands are supported by methods of the same
    name (in lower-case).

    All arguments to commands are converted to strings, except for
    AUTHENTICATE, and the last argument to APPEND which is passed as
    an IMAP4 literal.  If necessary (the string contains any
    non-printing characters or white-space and isn't enclosed with
    either parentheses or double quotes) each string is quoted.
    However, the 'password' argument to the LOGIN command is always
    quoted.  If you want to avoid having an argument string quoted
    (eg: the 'flags' argument to STORE) then enclose the string in
    parentheses (eg: "(\Deleted)").
105

Tim Peters's avatar
Tim Peters committed
106 107 108
    Each command returns a tuple: (type, [data, ...]) where 'type'
    is usually 'OK' or 'NO', and 'data' is either the text from the
    tagged response, or untagged results from command.
109

Tim Peters's avatar
Tim Peters committed
110 111 112 113 114
    Errors raise the exception class <instance>.error("<reason>").
    IMAP4 server errors raise <instance>.abort("<reason>"),
    which is a sub-class of 'error'. Mailbox status changes
    from READ-WRITE to READ-ONLY raise the exception class
    <instance>.readonly("<reason>"), which is a sub-class of 'abort'.
115

Tim Peters's avatar
Tim Peters committed
116 117 118 119
    "error" exceptions imply a program error.
    "abort" exceptions imply the connection should be reset, and
            the command re-tried.
    "readonly" exceptions imply the command should be re-tried.
Guido van Rossum's avatar
Guido van Rossum committed
120

Tim Peters's avatar
Tim Peters committed
121 122 123 124 125
    Note: to use this module, you must read the RFCs pertaining
    to the IMAP4 protocol, as the semantics of the arguments to
    each IMAP4 command are left to the invoker, not to mention
    the results.
    """
126

Tim Peters's avatar
Tim Peters committed
127 128 129
    class error(Exception): pass    # Logical errors - debug required
    class abort(error): pass        # Service errors - close and retry
    class readonly(abort): pass     # Mailbox status changed to READ-ONLY
130

Tim Peters's avatar
Tim Peters committed
131
    mustquote = re.compile(r"[^\w!#$%&'*+,.:;<=>?^`|~-]")
132

Tim Peters's avatar
Tim Peters committed
133 134 135 136 137 138 139 140 141 142 143
    def __init__(self, host = '', port = IMAP4_PORT):
        self.host = host
        self.port = port
        self.debug = Debug
        self.state = 'LOGOUT'
        self.literal = None             # A literal argument to a command
        self.tagged_commands = {}       # Tagged commands awaiting response
        self.untagged_responses = {}    # {typ: [data, ...], ...}
        self.continuation_response = '' # Last continuation response
        self.is_readonly = None         # READ-ONLY desired state
        self.tagnum = 0
144

Tim Peters's avatar
Tim Peters committed
145
        # Open socket to server.
146

Tim Peters's avatar
Tim Peters committed
147
        self.open(host, port)
148

Tim Peters's avatar
Tim Peters committed
149 150
        # Create unique tag for this session,
        # and compile tagged response matcher.
151

Tim Peters's avatar
Tim Peters committed
152 153 154 155
        self.tagpre = Int2AP(random.randint(0, 31999))
        self.tagre = re.compile(r'(?P<tag>'
                        + self.tagpre
                        + r'\d+) (?P<type>[A-Z]+) (?P<data>.*)')
156

Tim Peters's avatar
Tim Peters committed
157 158
        # Get server welcome message,
        # request and store CAPABILITY response.
159

Tim Peters's avatar
Tim Peters committed
160 161
        if __debug__:
            if self.debug >= 1:
Piers Lauder's avatar
Piers Lauder committed
162
                _mesg('imaplib version %s' % __version__)
Tim Peters's avatar
Tim Peters committed
163
                _mesg('new IMAP4 connection, tag=%s' % self.tagpre)
164

Tim Peters's avatar
Tim Peters committed
165 166 167 168 169 170 171
        self.welcome = self._get_response()
        if self.untagged_responses.has_key('PREAUTH'):
            self.state = 'AUTH'
        elif self.untagged_responses.has_key('OK'):
            self.state = 'NONAUTH'
        else:
            raise self.error(self.welcome)
172

Tim Peters's avatar
Tim Peters committed
173 174 175 176
        cap = 'CAPABILITY'
        self._simple_command(cap)
        if not self.untagged_responses.has_key(cap):
            raise self.error('no CAPABILITY response from server')
177
        self.capabilities = tuple(self.untagged_responses[cap][-1].upper().split())
178

Tim Peters's avatar
Tim Peters committed
179 180 181
        if __debug__:
            if self.debug >= 3:
                _mesg('CAPABILITIES: %s' % `self.capabilities`)
182

Tim Peters's avatar
Tim Peters committed
183 184 185 186 187
        for version in AllowedVersions:
            if not version in self.capabilities:
                continue
            self.PROTOCOL_VERSION = version
            return
188

Tim Peters's avatar
Tim Peters committed
189
        raise self.error('server not IMAP4 compliant')
190

191

Tim Peters's avatar
Tim Peters committed
192 193 194
    def __getattr__(self, attr):
        #       Allow UPPERCASE variants of IMAP4 command methods.
        if Commands.has_key(attr):
Piers Lauder's avatar
Piers Lauder committed
195
            return getattr(self, attr.lower())
Tim Peters's avatar
Tim Peters committed
196 197
        raise AttributeError("Unknown IMAP4 command: '%s'" % attr)

198 199


Piers Lauder's avatar
Piers Lauder committed
200
    #       Overridable methods
201 202


Tim Peters's avatar
Tim Peters committed
203
    def open(self, host, port):
Piers Lauder's avatar
Piers Lauder committed
204 205 206 207
        """Setup connection to remote server on "host:port".
        This connection will be used by the routines:
            read, readline, send, shutdown.
        """
Tim Peters's avatar
Tim Peters committed
208 209 210
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((self.host, self.port))
        self.file = self.sock.makefile('r')
211 212


Piers Lauder's avatar
Piers Lauder committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    def read(self, size):
        """Read 'size' bytes from remote."""
        return self.file.read(size)


    def readline(self):
        """Read line from remote."""
        return self.file.readline()


    def send(self, data):
        """Send data to remote."""
        self.sock.send(data)


    def shutdown(self):
        """Close I/O established in "open"."""
        self.file.close()
        self.sock.close()


    def socket(self):
        """Return socket instance used to connect to IMAP4 server.

        socket = <instance>.socket()
        """
        return self.sock



    #       Utility methods


Tim Peters's avatar
Tim Peters committed
246 247 248
    def recent(self):
        """Return most recent 'RECENT' responses if any exist,
        else prompt server for an update using the 'NOOP' command.
249

Tim Peters's avatar
Tim Peters committed
250
        (typ, [data]) = <instance>.recent()
251

Tim Peters's avatar
Tim Peters committed
252 253 254 255 256 257 258 259 260
        'data' is None if no new messages,
        else list of RECENT responses, most recent last.
        """
        name = 'RECENT'
        typ, dat = self._untagged_response('OK', [None], name)
        if dat[-1]:
            return typ, dat
        typ, dat = self.noop()  # Prod server for response
        return self._untagged_response(typ, dat, name)
261 262


Tim Peters's avatar
Tim Peters committed
263 264
    def response(self, code):
        """Return data for response 'code' if received, or None.
265

Tim Peters's avatar
Tim Peters committed
266
        Old value for response 'code' is cleared.
267

Tim Peters's avatar
Tim Peters committed
268 269
        (code, [data]) = <instance>.response(code)
        """
270
        return self._untagged_response(code, [None], code.upper())
271 272 273



Tim Peters's avatar
Tim Peters committed
274
    #       IMAP4 commands
275 276


Tim Peters's avatar
Tim Peters committed
277 278
    def append(self, mailbox, flags, date_time, message):
        """Append message to named mailbox.
279

Tim Peters's avatar
Tim Peters committed
280
        (typ, [data]) = <instance>.append(mailbox, flags, date_time, message)
Guido van Rossum's avatar
Guido van Rossum committed
281

Tim Peters's avatar
Tim Peters committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
                All args except `message' can be None.
        """
        name = 'APPEND'
        if not mailbox:
            mailbox = 'INBOX'
        if flags:
            if (flags[0],flags[-1]) != ('(',')'):
                flags = '(%s)' % flags
        else:
            flags = None
        if date_time:
            date_time = Time2Internaldate(date_time)
        else:
            date_time = None
        self.literal = message
        return self._simple_command(name, mailbox, flags, date_time)
298 299


Tim Peters's avatar
Tim Peters committed
300 301
    def authenticate(self, mechanism, authobject):
        """Authenticate command - requires response processing.
302

Tim Peters's avatar
Tim Peters committed
303 304 305
        'mechanism' specifies which authentication mechanism is to
        be used - it must appear in <instance>.capabilities in the
        form AUTH=<mechanism>.
306

Tim Peters's avatar
Tim Peters committed
307
        'authobject' must be a callable object:
308

Tim Peters's avatar
Tim Peters committed
309
                data = authobject(response)
310

Tim Peters's avatar
Tim Peters committed
311 312 313 314 315
        It will be called to process server continuation responses.
        It should return data that will be encoded and sent to server.
        It should return None if the client abort response '*' should
        be sent instead.
        """
316
        mech = mechanism.upper()
Tim Peters's avatar
Tim Peters committed
317 318 319 320 321 322 323 324 325
        cap = 'AUTH=%s' % mech
        if not cap in self.capabilities:
            raise self.error("Server doesn't allow %s authentication." % mech)
        self.literal = _Authenticator(authobject).process
        typ, dat = self._simple_command('AUTHENTICATE', mech)
        if typ != 'OK':
            raise self.error(dat[-1])
        self.state = 'AUTH'
        return typ, dat
326 327


Tim Peters's avatar
Tim Peters committed
328 329
    def check(self):
        """Checkpoint mailbox on server.
330

Tim Peters's avatar
Tim Peters committed
331 332 333
        (typ, [data]) = <instance>.check()
        """
        return self._simple_command('CHECK')
334 335


Tim Peters's avatar
Tim Peters committed
336 337
    def close(self):
        """Close currently selected mailbox.
338

Tim Peters's avatar
Tim Peters committed
339 340
        Deleted messages are removed from writable mailbox.
        This is the recommended command before 'LOGOUT'.
341

Tim Peters's avatar
Tim Peters committed
342 343 344 345 346 347 348
        (typ, [data]) = <instance>.close()
        """
        try:
            typ, dat = self._simple_command('CLOSE')
        finally:
            self.state = 'AUTH'
        return typ, dat
349 350


Tim Peters's avatar
Tim Peters committed
351 352
    def copy(self, message_set, new_mailbox):
        """Copy 'message_set' messages onto end of 'new_mailbox'.
353

Tim Peters's avatar
Tim Peters committed
354 355 356
        (typ, [data]) = <instance>.copy(message_set, new_mailbox)
        """
        return self._simple_command('COPY', message_set, new_mailbox)
357 358


Tim Peters's avatar
Tim Peters committed
359 360
    def create(self, mailbox):
        """Create new mailbox.
361

Tim Peters's avatar
Tim Peters committed
362 363 364
        (typ, [data]) = <instance>.create(mailbox)
        """
        return self._simple_command('CREATE', mailbox)
365 366


Tim Peters's avatar
Tim Peters committed
367 368
    def delete(self, mailbox):
        """Delete old mailbox.
369

Tim Peters's avatar
Tim Peters committed
370 371 372
        (typ, [data]) = <instance>.delete(mailbox)
        """
        return self._simple_command('DELETE', mailbox)
373 374


Tim Peters's avatar
Tim Peters committed
375 376
    def expunge(self):
        """Permanently remove deleted items from selected mailbox.
377

Tim Peters's avatar
Tim Peters committed
378
        Generates 'EXPUNGE' response for each deleted message.
379

Tim Peters's avatar
Tim Peters committed
380
        (typ, [data]) = <instance>.expunge()
381

Tim Peters's avatar
Tim Peters committed
382 383 384 385 386
        'data' is list of 'EXPUNGE'd message numbers in order received.
        """
        name = 'EXPUNGE'
        typ, dat = self._simple_command(name)
        return self._untagged_response(typ, dat, name)
387 388


Tim Peters's avatar
Tim Peters committed
389 390
    def fetch(self, message_set, message_parts):
        """Fetch (parts of) messages.
391

Tim Peters's avatar
Tim Peters committed
392
        (typ, [data, ...]) = <instance>.fetch(message_set, message_parts)
393

Tim Peters's avatar
Tim Peters committed
394 395
        'message_parts' should be a string of selected parts
        enclosed in parentheses, eg: "(UID BODY[TEXT])".
396

Tim Peters's avatar
Tim Peters committed
397 398 399 400 401
        'data' are tuples of message part envelope and data.
        """
        name = 'FETCH'
        typ, dat = self._simple_command(name, message_set, message_parts)
        return self._untagged_response(typ, dat, name)
402 403


Piers Lauder's avatar
Piers Lauder committed
404 405 406 407 408 409 410 411 412
    def getacl(self, mailbox):
        """Get the ACLs for a mailbox.

        (typ, [data]) = <instance>.getacl(mailbox)
        """
        typ, dat = self._simple_command('GETACL', mailbox)
        return self._untagged_response(typ, dat, 'ACL')


Tim Peters's avatar
Tim Peters committed
413 414
    def list(self, directory='""', pattern='*'):
        """List mailbox names in directory matching pattern.
415

Tim Peters's avatar
Tim Peters committed
416
        (typ, [data]) = <instance>.list(directory='""', pattern='*')
417

Tim Peters's avatar
Tim Peters committed
418 419 420 421 422
        'data' is list of LIST responses.
        """
        name = 'LIST'
        typ, dat = self._simple_command(name, directory, pattern)
        return self._untagged_response(typ, dat, name)
423 424


Tim Peters's avatar
Tim Peters committed
425 426
    def login(self, user, password):
        """Identify client using plaintext password.
427

Tim Peters's avatar
Tim Peters committed
428
        (typ, [data]) = <instance>.login(user, password)
Guido van Rossum's avatar
Guido van Rossum committed
429

Tim Peters's avatar
Tim Peters committed
430 431 432 433 434 435 436 437 438
        NB: 'password' will be quoted.
        """
        #if not 'AUTH=LOGIN' in self.capabilities:
        #       raise self.error("Server doesn't allow LOGIN authentication." % mech)
        typ, dat = self._simple_command('LOGIN', user, self._quote(password))
        if typ != 'OK':
            raise self.error(dat[-1])
        self.state = 'AUTH'
        return typ, dat
439 440


Tim Peters's avatar
Tim Peters committed
441 442
    def logout(self):
        """Shutdown connection to server.
443

Tim Peters's avatar
Tim Peters committed
444
        (typ, [data]) = <instance>.logout()
445

Tim Peters's avatar
Tim Peters committed
446 447 448 449 450
        Returns server 'BYE' response.
        """
        self.state = 'LOGOUT'
        try: typ, dat = self._simple_command('LOGOUT')
        except: typ, dat = 'NO', ['%s: %s' % sys.exc_info()[:2]]
Piers Lauder's avatar
Piers Lauder committed
451
        self.shutdown()
Tim Peters's avatar
Tim Peters committed
452 453 454
        if self.untagged_responses.has_key('BYE'):
            return 'BYE', self.untagged_responses['BYE']
        return typ, dat
455 456


Tim Peters's avatar
Tim Peters committed
457 458
    def lsub(self, directory='""', pattern='*'):
        """List 'subscribed' mailbox names in directory matching pattern.
459

Tim Peters's avatar
Tim Peters committed
460
        (typ, [data, ...]) = <instance>.lsub(directory='""', pattern='*')
461

Tim Peters's avatar
Tim Peters committed
462 463 464 465 466
        'data' are tuples of message part envelope and data.
        """
        name = 'LSUB'
        typ, dat = self._simple_command(name, directory, pattern)
        return self._untagged_response(typ, dat, name)
467 468


Piers Lauder's avatar
Piers Lauder committed
469 470 471 472 473 474 475 476 477 478
    def namespace(self):
        """ Returns IMAP namespaces ala rfc2342

        (typ, [data, ...]) = <instance>.namespace()
        """
        name = 'NAMESPACE'
        typ, dat = self._simple_command(name)
        return self._untagged_response(typ, dat, name)


Tim Peters's avatar
Tim Peters committed
479 480
    def noop(self):
        """Send NOOP command.
481

Tim Peters's avatar
Tim Peters committed
482 483 484 485 486 487
        (typ, data) = <instance>.noop()
        """
        if __debug__:
            if self.debug >= 3:
                _dump_ur(self.untagged_responses)
        return self._simple_command('NOOP')
488 489


Tim Peters's avatar
Tim Peters committed
490 491
    def partial(self, message_num, message_part, start, length):
        """Fetch truncated part of a message.
492

Tim Peters's avatar
Tim Peters committed
493
        (typ, [data, ...]) = <instance>.partial(message_num, message_part, start, length)
494

Tim Peters's avatar
Tim Peters committed
495 496 497 498 499
        'data' is tuple of message part envelope and data.
        """
        name = 'PARTIAL'
        typ, dat = self._simple_command(name, message_num, message_part, start, length)
        return self._untagged_response(typ, dat, 'FETCH')
500 501


Tim Peters's avatar
Tim Peters committed
502 503
    def rename(self, oldmailbox, newmailbox):
        """Rename old mailbox name to new.
504

Tim Peters's avatar
Tim Peters committed
505 506 507
        (typ, data) = <instance>.rename(oldmailbox, newmailbox)
        """
        return self._simple_command('RENAME', oldmailbox, newmailbox)
508 509


Tim Peters's avatar
Tim Peters committed
510 511
    def search(self, charset, *criteria):
        """Search mailbox for matching messages.
512

Tim Peters's avatar
Tim Peters committed
513
        (typ, [data]) = <instance>.search(charset, criterium, ...)
514

Tim Peters's avatar
Tim Peters committed
515 516 517 518
        'data' is space separated list of matching message numbers.
        """
        name = 'SEARCH'
        if charset:
Piers Lauder's avatar
Piers Lauder committed
519 520 521
            typ, dat = apply(self._simple_command, (name, 'CHARSET', charset) + criteria)
        else:
            typ, dat = apply(self._simple_command, (name,) + criteria)
Tim Peters's avatar
Tim Peters committed
522
        return self._untagged_response(typ, dat, name)
523 524


Tim Peters's avatar
Tim Peters committed
525 526
    def select(self, mailbox='INBOX', readonly=None):
        """Select a mailbox.
527

Tim Peters's avatar
Tim Peters committed
528
        Flush all untagged responses.
529

Tim Peters's avatar
Tim Peters committed
530
        (typ, [data]) = <instance>.select(mailbox='INBOX', readonly=None)
531

Tim Peters's avatar
Tim Peters committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
        'data' is count of messages in mailbox ('EXISTS' response).
        """
        # Mandated responses are ('FLAGS', 'EXISTS', 'RECENT', 'UIDVALIDITY')
        self.untagged_responses = {}    # Flush old responses.
        self.is_readonly = readonly
        if readonly:
            name = 'EXAMINE'
        else:
            name = 'SELECT'
        typ, dat = self._simple_command(name, mailbox)
        if typ != 'OK':
            self.state = 'AUTH'     # Might have been 'SELECTED'
            return typ, dat
        self.state = 'SELECTED'
        if self.untagged_responses.has_key('READ-ONLY') \
                and not readonly:
            if __debug__:
                if self.debug >= 1:
                    _dump_ur(self.untagged_responses)
            raise self.readonly('%s is not writable' % mailbox)
        return typ, self.untagged_responses.get('EXISTS', [None])
553 554


Piers Lauder's avatar
Piers Lauder committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568
    def setacl(self, mailbox, who, what):
        """Set a mailbox acl.

        (typ, [data]) = <instance>.create(mailbox, who, what)
        """
        return self._simple_command('SETACL', mailbox, who, what)


    def sort(self, sort_criteria, charset, *search_criteria):
        """IMAP4rev1 extension SORT command.

        (typ, [data]) = <instance>.sort(sort_criteria, charset, search_criteria, ...)
        """
        name = 'SORT'
569 570
        #if not name in self.capabilities:      # Let the server decide!
        #       raise self.error('unimplemented extension command: %s' % name)
Piers Lauder's avatar
Piers Lauder committed
571
        if (sort_criteria[0],sort_criteria[-1]) != ('(',')'):
572
            sort_criteria = '(%s)' % sort_criteria
Piers Lauder's avatar
Piers Lauder committed
573 574 575 576
        typ, dat = apply(self._simple_command, (name, sort_criteria, charset) + search_criteria)
        return self._untagged_response(typ, dat, name)


Tim Peters's avatar
Tim Peters committed
577 578
    def status(self, mailbox, names):
        """Request named status conditions for mailbox.
579

Tim Peters's avatar
Tim Peters committed
580 581 582
        (typ, [data]) = <instance>.status(mailbox, names)
        """
        name = 'STATUS'
583
        #if self.PROTOCOL_VERSION == 'IMAP4':   # Let the server decide!
Piers Lauder's avatar
Piers Lauder committed
584
        #    raise self.error('%s unimplemented in IMAP4 (obtain IMAP4rev1 server, or re-code)' % name)
Tim Peters's avatar
Tim Peters committed
585 586
        typ, dat = self._simple_command(name, mailbox, names)
        return self._untagged_response(typ, dat, name)
587 588


Tim Peters's avatar
Tim Peters committed
589 590
    def store(self, message_set, command, flags):
        """Alters flag dispositions for messages in mailbox.
591

Tim Peters's avatar
Tim Peters committed
592 593 594 595 596 597
        (typ, [data]) = <instance>.store(message_set, command, flags)
        """
        if (flags[0],flags[-1]) != ('(',')'):
            flags = '(%s)' % flags  # Avoid quoting the flags
        typ, dat = self._simple_command('STORE', message_set, command, flags)
        return self._untagged_response(typ, dat, 'FETCH')
598 599


Tim Peters's avatar
Tim Peters committed
600 601
    def subscribe(self, mailbox):
        """Subscribe to new mailbox.
602

Tim Peters's avatar
Tim Peters committed
603 604 605
        (typ, [data]) = <instance>.subscribe(mailbox)
        """
        return self._simple_command('SUBSCRIBE', mailbox)
606 607


Tim Peters's avatar
Tim Peters committed
608 609 610
    def uid(self, command, *args):
        """Execute "command arg ..." with messages identified by UID,
                rather than message number.
611

Tim Peters's avatar
Tim Peters committed
612
        (typ, [data]) = <instance>.uid(command, arg1, arg2, ...)
613

Tim Peters's avatar
Tim Peters committed
614 615
        Returns response appropriate to 'command'.
        """
616
        command = command.upper()
Tim Peters's avatar
Tim Peters committed
617 618 619 620 621 622 623
        if not Commands.has_key(command):
            raise self.error("Unknown IMAP4 UID command: %s" % command)
        if self.state not in Commands[command]:
            raise self.error('command %s illegal in state %s'
                                    % (command, self.state))
        name = 'UID'
        typ, dat = apply(self._simple_command, (name, command) + args)
Piers Lauder's avatar
Piers Lauder committed
624 625
        if command in ('SEARCH', 'SORT'):
            name = command
Tim Peters's avatar
Tim Peters committed
626 627 628
        else:
            name = 'FETCH'
        return self._untagged_response(typ, dat, name)
629 630


Tim Peters's avatar
Tim Peters committed
631 632
    def unsubscribe(self, mailbox):
        """Unsubscribe from old mailbox.
633

Tim Peters's avatar
Tim Peters committed
634 635 636 637 638 639 640 641
        (typ, [data]) = <instance>.unsubscribe(mailbox)
        """
        return self._simple_command('UNSUBSCRIBE', mailbox)


    def xatom(self, name, *args):
        """Allow simple extension commands
                notified by server in CAPABILITY response.
642

Piers Lauder's avatar
Piers Lauder committed
643 644
        Assumes command is legal in current state.

Tim Peters's avatar
Tim Peters committed
645
        (typ, [data]) = <instance>.xatom(name, arg, ...)
Piers Lauder's avatar
Piers Lauder committed
646 647

        Returns response appropriate to extension command `name'.
Tim Peters's avatar
Tim Peters committed
648
        """
Piers Lauder's avatar
Piers Lauder committed
649
        name = name.upper()
650
        #if not name in self.capabilities:      # Let the server decide!
Piers Lauder's avatar
Piers Lauder committed
651 652 653
        #    raise self.error('unknown extension command: %s' % name)
        if not Commands.has_key(name):
            Commands[name] = (self.state,)
Tim Peters's avatar
Tim Peters committed
654
        return apply(self._simple_command, (name,) + args)
655 656


Tim Peters's avatar
Tim Peters committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672

    #       Private methods


    def _append_untagged(self, typ, dat):

        if dat is None: dat = ''
        ur = self.untagged_responses
        if __debug__:
            if self.debug >= 5:
                _mesg('untagged_responses[%s] %s += ["%s"]' %
                        (typ, len(ur.get(typ,'')), dat))
        if ur.has_key(typ):
            ur[typ].append(dat)
        else:
            ur[typ] = [dat]
673 674


Tim Peters's avatar
Tim Peters committed
675 676 677 678
    def _check_bye(self):
        bye = self.untagged_responses.get('BYE')
        if bye:
            raise self.abort(bye[-1])
Guido van Rossum's avatar
Guido van Rossum committed
679 680


Tim Peters's avatar
Tim Peters committed
681
    def _command(self, name, *args):
682

Tim Peters's avatar
Tim Peters committed
683 684 685 686
        if self.state not in Commands[name]:
            self.literal = None
            raise self.error(
            'command %s illegal in state %s' % (name, self.state))
687

Tim Peters's avatar
Tim Peters committed
688 689 690
        for typ in ('OK', 'NO', 'BAD'):
            if self.untagged_responses.has_key(typ):
                del self.untagged_responses[typ]
691

Tim Peters's avatar
Tim Peters committed
692 693 694
        if self.untagged_responses.has_key('READ-ONLY') \
        and not self.is_readonly:
            raise self.readonly('mailbox status changed to READ-ONLY')
695

Tim Peters's avatar
Tim Peters committed
696 697 698 699 700
        tag = self._new_tag()
        data = '%s %s' % (tag, name)
        for arg in args:
            if arg is None: continue
            data = '%s %s' % (data, self._checkquote(arg))
701

Tim Peters's avatar
Tim Peters committed
702 703 704 705 706 707 708 709
        literal = self.literal
        if literal is not None:
            self.literal = None
            if type(literal) is type(self._command):
                literator = literal
            else:
                literator = None
                data = '%s {%s}' % (data, len(literal))
710

Tim Peters's avatar
Tim Peters committed
711 712 713 714 715
        if __debug__:
            if self.debug >= 4:
                _mesg('> %s' % data)
            else:
                _log('> %s' % data)
Guido van Rossum's avatar
Guido van Rossum committed
716

Tim Peters's avatar
Tim Peters committed
717
        try:
Piers Lauder's avatar
Piers Lauder committed
718 719
            self.send('%s%s' % (data, CRLF))
        except (socket.error, OSError), val:
Tim Peters's avatar
Tim Peters committed
720
            raise self.abort('socket error: %s' % val)
721

Tim Peters's avatar
Tim Peters committed
722 723
        if literal is None:
            return tag
724

Tim Peters's avatar
Tim Peters committed
725 726
        while 1:
            # Wait for continuation response
727

Tim Peters's avatar
Tim Peters committed
728 729 730
            while self._get_response():
                if self.tagged_commands[tag]:   # BAD/NO?
                    return tag
731

Tim Peters's avatar
Tim Peters committed
732
            # Send literal
733

Tim Peters's avatar
Tim Peters committed
734 735
            if literator:
                literal = literator(self.continuation_response)
736

Tim Peters's avatar
Tim Peters committed
737 738 739
            if __debug__:
                if self.debug >= 4:
                    _mesg('write literal size %s' % len(literal))
740

Tim Peters's avatar
Tim Peters committed
741
            try:
Piers Lauder's avatar
Piers Lauder committed
742 743 744
                self.send(literal)
                self.send(CRLF)
            except (socket.error, OSError), val:
Tim Peters's avatar
Tim Peters committed
745
                raise self.abort('socket error: %s' % val)
746

Tim Peters's avatar
Tim Peters committed
747 748
            if not literator:
                break
749

Tim Peters's avatar
Tim Peters committed
750
        return tag
751 752


Tim Peters's avatar
Tim Peters committed
753 754 755 756 757 758 759 760 761 762 763 764
    def _command_complete(self, name, tag):
        self._check_bye()
        try:
            typ, data = self._get_tagged_response(tag)
        except self.abort, val:
            raise self.abort('command: %s => %s' % (name, val))
        except self.error, val:
            raise self.error('command: %s => %s' % (name, val))
        self._check_bye()
        if typ == 'BAD':
            raise self.error('%s command error: %s %s' % (name, typ, data))
        return typ, data
765 766


Tim Peters's avatar
Tim Peters committed
767
    def _get_response(self):
768

Tim Peters's avatar
Tim Peters committed
769 770 771 772
        # Read response and store.
        #
        # Returns None for continuation responses,
        # otherwise first response line received.
773

Tim Peters's avatar
Tim Peters committed
774
        resp = self._get_line()
775

Tim Peters's avatar
Tim Peters committed
776
        # Command completion response?
777

Tim Peters's avatar
Tim Peters committed
778 779 780 781
        if self._match(self.tagre, resp):
            tag = self.mo.group('tag')
            if not self.tagged_commands.has_key(tag):
                raise self.abort('unexpected tagged response: %s' % resp)
782

Tim Peters's avatar
Tim Peters committed
783 784 785 786 787
            typ = self.mo.group('type')
            dat = self.mo.group('data')
            self.tagged_commands[tag] = (typ, [dat])
        else:
            dat2 = None
788

Tim Peters's avatar
Tim Peters committed
789
            # '*' (untagged) responses?
790

Tim Peters's avatar
Tim Peters committed
791 792 793
            if not self._match(Untagged_response, resp):
                if self._match(Untagged_status, resp):
                    dat2 = self.mo.group('data2')
794

Tim Peters's avatar
Tim Peters committed
795 796
            if self.mo is None:
                # Only other possibility is '+' (continuation) response...
797

Tim Peters's avatar
Tim Peters committed
798 799 800
                if self._match(Continuation, resp):
                    self.continuation_response = self.mo.group('data')
                    return None     # NB: indicates continuation
801

Tim Peters's avatar
Tim Peters committed
802
                raise self.abort("unexpected response: '%s'" % resp)
803

Tim Peters's avatar
Tim Peters committed
804 805 806 807
            typ = self.mo.group('type')
            dat = self.mo.group('data')
            if dat is None: dat = ''        # Null untagged response
            if dat2: dat = dat + ' ' + dat2
808

Tim Peters's avatar
Tim Peters committed
809
            # Is there a literal to come?
810

Tim Peters's avatar
Tim Peters committed
811
            while self._match(Literal, dat):
812

Tim Peters's avatar
Tim Peters committed
813
                # Read literal direct from connection.
814

815
                size = int(self.mo.group('size'))
Tim Peters's avatar
Tim Peters committed
816 817 818
                if __debug__:
                    if self.debug >= 4:
                        _mesg('read literal size %s' % size)
Piers Lauder's avatar
Piers Lauder committed
819
                data = self.read(size)
820

Tim Peters's avatar
Tim Peters committed
821
                # Store response with literal as tuple
822

Tim Peters's avatar
Tim Peters committed
823
                self._append_untagged(typ, (dat, data))
824

Tim Peters's avatar
Tim Peters committed
825
                # Read trailer - possibly containing another literal
826

Tim Peters's avatar
Tim Peters committed
827
                dat = self._get_line()
828

Tim Peters's avatar
Tim Peters committed
829
            self._append_untagged(typ, dat)
830

Tim Peters's avatar
Tim Peters committed
831
        # Bracketed response information?
832

Tim Peters's avatar
Tim Peters committed
833 834
        if typ in ('OK', 'NO', 'BAD') and self._match(Response_code, dat):
            self._append_untagged(self.mo.group('type'), self.mo.group('data'))
835

Tim Peters's avatar
Tim Peters committed
836 837 838
        if __debug__:
            if self.debug >= 1 and typ in ('NO', 'BAD', 'BYE'):
                _mesg('%s response: %s' % (typ, dat))
839

Tim Peters's avatar
Tim Peters committed
840
        return resp
841 842


Tim Peters's avatar
Tim Peters committed
843
    def _get_tagged_response(self, tag):
844

Tim Peters's avatar
Tim Peters committed
845 846 847 848 849
        while 1:
            result = self.tagged_commands[tag]
            if result is not None:
                del self.tagged_commands[tag]
                return result
850

Tim Peters's avatar
Tim Peters committed
851 852 853 854
            # Some have reported "unexpected response" exceptions.
            # Note that ignoring them here causes loops.
            # Instead, send me details of the unexpected response and
            # I'll update the code in `_get_response()'.
855

Tim Peters's avatar
Tim Peters committed
856 857 858 859 860 861 862
            try:
                self._get_response()
            except self.abort, val:
                if __debug__:
                    if self.debug >= 1:
                        print_log()
                raise
863 864


Tim Peters's avatar
Tim Peters committed
865
    def _get_line(self):
866

Piers Lauder's avatar
Piers Lauder committed
867
        line = self.readline()
Tim Peters's avatar
Tim Peters committed
868 869
        if not line:
            raise self.abort('socket error: EOF')
870

Tim Peters's avatar
Tim Peters committed
871
        # Protocol mandates all lines terminated by CRLF
872

Tim Peters's avatar
Tim Peters committed
873 874 875 876 877 878 879
        line = line[:-2]
        if __debug__:
            if self.debug >= 4:
                _mesg('< %s' % line)
            else:
                _log('< %s' % line)
        return line
880 881


Tim Peters's avatar
Tim Peters committed
882
    def _match(self, cre, s):
883

Tim Peters's avatar
Tim Peters committed
884 885
        # Run compiled regular expression match method on 's'.
        # Save result, return success.
886

Tim Peters's avatar
Tim Peters committed
887 888 889 890 891
        self.mo = cre.match(s)
        if __debug__:
            if self.mo is not None and self.debug >= 5:
                _mesg("\tmatched r'%s' => %s" % (cre.pattern, `self.mo.groups()`))
        return self.mo is not None
892 893


Tim Peters's avatar
Tim Peters committed
894
    def _new_tag(self):
895

Tim Peters's avatar
Tim Peters committed
896 897 898 899
        tag = '%s%s' % (self.tagpre, self.tagnum)
        self.tagnum = self.tagnum + 1
        self.tagged_commands[tag] = None
        return tag
900 901


Tim Peters's avatar
Tim Peters committed
902
    def _checkquote(self, arg):
Guido van Rossum's avatar
Guido van Rossum committed
903

Tim Peters's avatar
Tim Peters committed
904 905
        # Must quote command args if non-alphanumeric chars present,
        # and not already quoted.
Guido van Rossum's avatar
Guido van Rossum committed
906

Tim Peters's avatar
Tim Peters committed
907 908 909 910 911 912 913
        if type(arg) is not type(''):
            return arg
        if (arg[0],arg[-1]) in (('(',')'),('"','"')):
            return arg
        if self.mustquote.search(arg) is None:
            return arg
        return self._quote(arg)
Guido van Rossum's avatar
Guido van Rossum committed
914 915


Tim Peters's avatar
Tim Peters committed
916
    def _quote(self, arg):
Guido van Rossum's avatar
Guido van Rossum committed
917

918 919
        arg = arg.replace('\\', '\\\\')
        arg = arg.replace('"', '\\"')
Guido van Rossum's avatar
Guido van Rossum committed
920

Tim Peters's avatar
Tim Peters committed
921
        return '"%s"' % arg
Guido van Rossum's avatar
Guido van Rossum committed
922 923


Tim Peters's avatar
Tim Peters committed
924
    def _simple_command(self, name, *args):
925

Tim Peters's avatar
Tim Peters committed
926
        return self._command_complete(name, apply(self._command, (name,) + args))
927 928


Tim Peters's avatar
Tim Peters committed
929
    def _untagged_response(self, typ, dat, name):
930

Tim Peters's avatar
Tim Peters committed
931 932 933 934 935 936 937 938 939 940
        if typ == 'NO':
            return typ, dat
        if not self.untagged_responses.has_key(name):
            return typ, [None]
        data = self.untagged_responses[name]
        if __debug__:
            if self.debug >= 5:
                _mesg('untagged_responses[%s] => %s' % (name, data))
        del self.untagged_responses[name]
        return typ, data
941 942 943



944 945
class _Authenticator:

Tim Peters's avatar
Tim Peters committed
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
    """Private class to provide en/decoding
            for base64-based authentication conversation.
    """

    def __init__(self, mechinst):
        self.mech = mechinst    # Callable object to provide/process data

    def process(self, data):
        ret = self.mech(self.decode(data))
        if ret is None:
            return '*'      # Abort conversation
        return self.encode(ret)

    def encode(self, inp):
        #
        #  Invoke binascii.b2a_base64 iteratively with
        #  short even length buffers, strip the trailing
        #  line feed from the result and append.  "Even"
        #  means a number that factors to both 6 and 8,
        #  so when it gets to the end of the 8-bit input
        #  there's no partial 6-bit output.
        #
        oup = ''
        while inp:
            if len(inp) > 48:
                t = inp[:48]
                inp = inp[48:]
            else:
                t = inp
                inp = ''
            e = binascii.b2a_base64(t)
            if e:
                oup = oup + e[:-1]
        return oup

    def decode(self, inp):
        if not inp:
            return ''
        return binascii.a2b_base64(inp)

986 987


988
Mon2num = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
Tim Peters's avatar
Tim Peters committed
989
        'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
990 991

def Internaldate2tuple(resp):
Tim Peters's avatar
Tim Peters committed
992
    """Convert IMAP4 INTERNALDATE to UT.
993

Tim Peters's avatar
Tim Peters committed
994 995
    Returns Python time module tuple.
    """
996

Tim Peters's avatar
Tim Peters committed
997 998 999
    mo = InternalDate.match(resp)
    if not mo:
        return None
1000

Tim Peters's avatar
Tim Peters committed
1001 1002
    mon = Mon2num[mo.group('mon')]
    zonen = mo.group('zonen')
1003

1004 1005 1006 1007 1008 1009 1010
    day = int(mo.group('day'))
    year = int(mo.group('year'))
    hour = int(mo.group('hour'))
    min = int(mo.group('min'))
    sec = int(mo.group('sec'))
    zoneh = int(mo.group('zoneh'))
    zonem = int(mo.group('zonem'))
1011

Tim Peters's avatar
Tim Peters committed
1012
    # INTERNALDATE timezone must be subtracted to get UT
1013

Tim Peters's avatar
Tim Peters committed
1014 1015 1016
    zone = (zoneh*60 + zonem)*60
    if zonen == '-':
        zone = -zone
1017

Tim Peters's avatar
Tim Peters committed
1018
    tt = (year, mon, day, hour, min, sec, -1, -1, -1)
1019

Tim Peters's avatar
Tim Peters committed
1020
    utc = time.mktime(tt)
1021

Tim Peters's avatar
Tim Peters committed
1022 1023
    # Following is necessary because the time module has no 'mkgmtime'.
    # 'mktime' assumes arg in local timezone, so adds timezone/altzone.
1024

Tim Peters's avatar
Tim Peters committed
1025 1026 1027 1028 1029
    lt = time.localtime(utc)
    if time.daylight and lt[-1]:
        zone = zone + time.altzone
    else:
        zone = zone + time.timezone
1030

Tim Peters's avatar
Tim Peters committed
1031
    return time.localtime(utc - zone)
1032 1033 1034 1035 1036



def Int2AP(num):

Tim Peters's avatar
Tim Peters committed
1037
    """Convert integer to A-P string representation."""
1038

Tim Peters's avatar
Tim Peters committed
1039 1040 1041 1042 1043 1044
    val = ''; AP = 'ABCDEFGHIJKLMNOP'
    num = int(abs(num))
    while num:
        num, mod = divmod(num, 16)
        val = AP[mod] + val
    return val
1045 1046 1047 1048 1049



def ParseFlags(resp):

Tim Peters's avatar
Tim Peters committed
1050
    """Convert IMAP4 flags response to python tuple."""
1051

Tim Peters's avatar
Tim Peters committed
1052 1053 1054
    mo = Flags.match(resp)
    if not mo:
        return ()
1055

1056
    return tuple(mo.group('flags').split())
1057 1058 1059 1060


def Time2Internaldate(date_time):

Tim Peters's avatar
Tim Peters committed
1061
    """Convert 'date_time' to IMAP4 INTERNALDATE representation.
1062

Tim Peters's avatar
Tim Peters committed
1063 1064
    Return string in form: '"DD-Mmm-YYYY HH:MM:SS +HHMM"'
    """
1065

Tim Peters's avatar
Tim Peters committed
1066 1067 1068 1069 1070 1071 1072 1073
    dttype = type(date_time)
    if dttype is type(1) or dttype is type(1.1):
        tt = time.localtime(date_time)
    elif dttype is type(()):
        tt = date_time
    elif dttype is type(""):
        return date_time        # Assume in correct format
    else: raise ValueError
1074

Tim Peters's avatar
Tim Peters committed
1075 1076 1077 1078 1079 1080 1081 1082
    dt = time.strftime("%d-%b-%Y %H:%M:%S", tt)
    if dt[0] == '0':
        dt = ' ' + dt[1:]
    if time.daylight and tt[-1]:
        zone = -time.altzone
    else:
        zone = -time.timezone
    return '"' + dt + " %+02d%02d" % divmod(zone/60, 60) + '"'
1083 1084 1085



1086 1087
if __debug__:

Tim Peters's avatar
Tim Peters committed
1088 1089 1090 1091 1092 1093
    def _mesg(s, secs=None):
        if secs is None:
            secs = time.time()
        tm = time.strftime('%M:%S', time.localtime(secs))
        sys.stderr.write('  %s.%02d %s\n' % (tm, (secs*100)%100, s))
        sys.stderr.flush()
1094

Tim Peters's avatar
Tim Peters committed
1095 1096 1097 1098 1099
    def _dump_ur(dict):
        # Dump untagged responses (in `dict').
        l = dict.items()
        if not l: return
        t = '\n\t\t'
1100
        l = map(lambda x:'%s: "%s"' % (x[0], x[1][0] and '" "'.join(x[1]) or ''), l)
1101
        _mesg('untagged responses dump:%s%s' % (t, t.join(l)))
1102

Tim Peters's avatar
Tim Peters committed
1103 1104
    _cmd_log = []           # Last `_cmd_log_len' interactions
    _cmd_log_len = 10
Guido van Rossum's avatar
Guido van Rossum committed
1105

Tim Peters's avatar
Tim Peters committed
1106 1107 1108 1109 1110
    def _log(line):
        # Keep log of last `_cmd_log_len' interactions for debugging.
        if len(_cmd_log) == _cmd_log_len:
            del _cmd_log[0]
        _cmd_log.append((time.time(), line))
Guido van Rossum's avatar
Guido van Rossum committed
1111

Tim Peters's avatar
Tim Peters committed
1112 1113 1114 1115
    def print_log():
        _mesg('last %d IMAP4 interactions:' % len(_cmd_log))
        for secs,line in _cmd_log:
            _mesg(line, secs)
Guido van Rossum's avatar
Guido van Rossum committed
1116

1117 1118


Guido van Rossum's avatar
Guido van Rossum committed
1119
if __name__ == '__main__':
1120

1121
    import getopt, getpass
Tim Peters's avatar
Tim Peters committed
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136

    try:
        optlist, args = getopt.getopt(sys.argv[1:], 'd:')
    except getopt.error, val:
        pass

    for opt,val in optlist:
        if opt == '-d':
            Debug = int(val)

    if not args: args = ('',)

    host = args[0]

    USER = getpass.getuser()
Piers Lauder's avatar
Piers Lauder committed
1137
    PASSWD = getpass.getpass("IMAP password for %s on %s: " % (USER, host or "localhost"))
Tim Peters's avatar
Tim Peters committed
1138

1139
    test_mesg = 'From: %(user)s@localhost%(lf)sSubject: IMAP4 test%(lf)s%(lf)sdata...%(lf)s' % {'user':USER, 'lf':CRLF}
Tim Peters's avatar
Tim Peters committed
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
    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, test_mesg)),
    ('list', ('/tmp', 'yy*')),
    ('select', ('/tmp/yyz 2',)),
    ('search', (None, 'SUBJECT', 'test')),
    ('partial', ('1', 'RFC822', 1, 1024)),
    ('store', ('1', 'FLAGS', '(\Deleted)')),
Piers Lauder's avatar
Piers Lauder committed
1151
    ('namespace', ()),
Tim Peters's avatar
Tim Peters committed
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
    ('expunge', ()),
    ('recent', ()),
    ('close', ()),
    )

    test_seq2 = (
    ('select', ()),
    ('response',('UIDVALIDITY',)),
    ('uid', ('SEARCH', 'ALL')),
    ('response', ('EXISTS',)),
    ('append', (None, None, None, test_mesg)),
    ('recent', ()),
    ('logout', ()),
    )

    def run(cmd, args):
        _mesg('%s %s' % (cmd, args))
Piers Lauder's avatar
Piers Lauder committed
1169
        typ, dat = apply(getattr(M, cmd), args)
Tim Peters's avatar
Tim Peters committed
1170 1171 1172 1173 1174 1175
        _mesg('%s => %s %s' % (cmd, typ, dat))
        return dat

    try:
        M = IMAP4(host)
        _mesg('PROTOCOL_VERSION = %s' % M.PROTOCOL_VERSION)
Piers Lauder's avatar
Piers Lauder committed
1176
        _mesg('CAPABILITIES = %s' % `M.capabilities`)
Tim Peters's avatar
Tim Peters committed
1177 1178 1179 1180 1181 1182 1183

        for cmd,args in test_seq1:
            run(cmd, args)

        for ml in run('list', ('/tmp/', 'yy%')):
            mo = re.match(r'.*"([^"]+)"$', ml)
            if mo: path = mo.group(1)
1184
            else: path = ml.split()[-1]
Tim Peters's avatar
Tim Peters committed
1185 1186 1187 1188 1189 1190 1191 1192
            run('delete', (path,))

        for cmd,args in test_seq2:
            dat = run(cmd, args)

            if (cmd,args) != ('uid', ('SEARCH', 'ALL')):
                continue

1193
            uid = dat[-1].split()
Tim Peters's avatar
Tim Peters committed
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
            if not uid: continue
            run('uid', ('FETCH', '%s' % uid[-1],
                    '(FLAGS INTERNALDATE RFC822.SIZE RFC822.HEADER RFC822.TEXT)'))

        print '\nAll tests OK.'

    except:
        print '\nTests failed.'

        if not Debug:
            print '''
Guido van Rossum's avatar
Guido van Rossum committed
1205 1206 1207
If you would like to see debugging output,
try: %s -d5
''' % sys.argv[0]
1208

Tim Peters's avatar
Tim Peters committed
1209
        raise