Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1 | """IMAP4 client. |
| 2 | |
| 3 | Based on RFC 2060. |
| 4 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 5 | Public class: IMAP4 |
| 6 | Public variable: Debug |
| 7 | Public functions: Internaldate2tuple |
| 8 | Int2AP |
| 9 | ParseFlags |
| 10 | Time2Internaldate |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 11 | """ |
Guido van Rossum | b1f0812 | 1998-06-25 02:22:16 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 13 | # Author: Piers Lauder <piers@cs.su.oz.au> December 1997. |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 14 | # |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 15 | # Authentication code contributed by Donn Cave <donn@u.washington.edu> June 1998. |
Eric S. Raymond | 25a0cbc | 2001-02-09 06:50:21 +0000 | [diff] [blame] | 16 | # String method conversion by ESR, February 2001. |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 17 | # GET/SETACL contributed by Anthony Baxter <anthony@interlink.com.au> April 2001. |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame] | 18 | # IMAP4_SSL contributed by Tino Lange <Tino.Lange@isg.de> March 2002. |
Piers Lauder | 3fca291 | 2002-06-17 07:07:20 +0000 | [diff] [blame] | 19 | # GET/SETQUOTA contributed by Andreas Zeidler <az@kreativkombinat.de> June 2002. |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 20 | # PROXYAUTH contributed by Rick Holbert <holbert.13@osu.edu> November 2002. |
Piers Lauder | d80ef02 | 2005-06-01 23:50:52 +0000 | [diff] [blame] | 21 | # GET/SETANNOTATION contributed by Tomas Lindroos <skitta@abo.fi> June 2005. |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 22 | |
Piers Lauder | be5615e | 2005-08-31 10:50:03 +0000 | [diff] [blame] | 23 | __version__ = "2.58" |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 24 | |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 25 | import binascii, os, random, re, socket, sys, time |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 26 | |
Piers Lauder | 0659452 | 2004-06-02 00:42:36 +0000 | [diff] [blame] | 27 | __all__ = ["IMAP4", "IMAP4_SSL", "IMAP4_stream", "Internaldate2tuple", |
Barry Warsaw | f449391 | 2001-01-24 04:16:09 +0000 | [diff] [blame] | 28 | "Int2AP", "ParseFlags", "Time2Internaldate"] |
Skip Montanaro | 2dd4276 | 2001-01-23 15:35:05 +0000 | [diff] [blame] | 29 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 30 | # Globals |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 31 | |
| 32 | CRLF = '\r\n' |
| 33 | Debug = 0 |
| 34 | IMAP4_PORT = 143 |
Piers Lauder | 95f8495 | 2002-03-08 09:05:12 +0000 | [diff] [blame] | 35 | IMAP4_SSL_PORT = 993 |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 36 | AllowedVersions = ('IMAP4REV1', 'IMAP4') # Most recent first |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 37 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 38 | # Commands |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 39 | |
| 40 | Commands = { |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 41 | # name valid states |
| 42 | 'APPEND': ('AUTH', 'SELECTED'), |
| 43 | 'AUTHENTICATE': ('NONAUTH',), |
| 44 | 'CAPABILITY': ('NONAUTH', 'AUTH', 'SELECTED', 'LOGOUT'), |
| 45 | 'CHECK': ('SELECTED',), |
| 46 | 'CLOSE': ('SELECTED',), |
| 47 | 'COPY': ('SELECTED',), |
| 48 | 'CREATE': ('AUTH', 'SELECTED'), |
| 49 | 'DELETE': ('AUTH', 'SELECTED'), |
Martin v. Löwis | 7b9190b | 2004-07-27 05:07:19 +0000 | [diff] [blame] | 50 | 'DELETEACL': ('AUTH', 'SELECTED'), |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 51 | 'EXAMINE': ('AUTH', 'SELECTED'), |
| 52 | 'EXPUNGE': ('SELECTED',), |
| 53 | 'FETCH': ('SELECTED',), |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 54 | 'GETACL': ('AUTH', 'SELECTED'), |
Piers Lauder | d80ef02 | 2005-06-01 23:50:52 +0000 | [diff] [blame] | 55 | 'GETANNOTATION':('AUTH', 'SELECTED'), |
Piers Lauder | 3fca291 | 2002-06-17 07:07:20 +0000 | [diff] [blame] | 56 | 'GETQUOTA': ('AUTH', 'SELECTED'), |
| 57 | 'GETQUOTAROOT': ('AUTH', 'SELECTED'), |
Martin v. Löwis | 7b9190b | 2004-07-27 05:07:19 +0000 | [diff] [blame] | 58 | 'MYRIGHTS': ('AUTH', 'SELECTED'), |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 59 | 'LIST': ('AUTH', 'SELECTED'), |
| 60 | 'LOGIN': ('NONAUTH',), |
| 61 | 'LOGOUT': ('NONAUTH', 'AUTH', 'SELECTED', 'LOGOUT'), |
| 62 | 'LSUB': ('AUTH', 'SELECTED'), |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 63 | 'NAMESPACE': ('AUTH', 'SELECTED'), |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 64 | 'NOOP': ('NONAUTH', 'AUTH', 'SELECTED', 'LOGOUT'), |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 65 | 'PARTIAL': ('SELECTED',), # NB: obsolete |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 66 | 'PROXYAUTH': ('AUTH',), |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 67 | 'RENAME': ('AUTH', 'SELECTED'), |
| 68 | 'SEARCH': ('SELECTED',), |
| 69 | 'SELECT': ('AUTH', 'SELECTED'), |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 70 | 'SETACL': ('AUTH', 'SELECTED'), |
Piers Lauder | d80ef02 | 2005-06-01 23:50:52 +0000 | [diff] [blame] | 71 | 'SETANNOTATION':('AUTH', 'SELECTED'), |
Piers Lauder | 3fca291 | 2002-06-17 07:07:20 +0000 | [diff] [blame] | 72 | 'SETQUOTA': ('AUTH', 'SELECTED'), |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 73 | 'SORT': ('SELECTED',), |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 74 | 'STATUS': ('AUTH', 'SELECTED'), |
| 75 | 'STORE': ('SELECTED',), |
| 76 | 'SUBSCRIBE': ('AUTH', 'SELECTED'), |
Martin v. Löwis | d892137 | 2003-11-10 06:44:44 +0000 | [diff] [blame] | 77 | 'THREAD': ('SELECTED',), |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 78 | 'UID': ('SELECTED',), |
| 79 | 'UNSUBSCRIBE': ('AUTH', 'SELECTED'), |
| 80 | } |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 81 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 82 | # Patterns to match server responses |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 84 | Continuation = re.compile(r'\+( (?P<data>.*))?') |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 85 | Flags = re.compile(r'.*FLAGS \((?P<flags>[^\)]*)\)') |
| 86 | InternalDate = re.compile(r'.*INTERNALDATE "' |
Piers Lauder | 8659d90 | 2005-03-02 09:13:45 +0000 | [diff] [blame] | 87 | r'(?P<day>[ 0123][0-9])-(?P<mon>[A-Z][a-z][a-z])-(?P<year>[0-9][0-9][0-9][0-9])' |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 88 | r' (?P<hour>[0-9][0-9]):(?P<min>[0-9][0-9]):(?P<sec>[0-9][0-9])' |
| 89 | r' (?P<zonen>[-+])(?P<zoneh>[0-9][0-9])(?P<zonem>[0-9][0-9])' |
| 90 | r'"') |
Guido van Rossum | f36b182 | 2000-02-17 17:12:39 +0000 | [diff] [blame] | 91 | Literal = re.compile(r'.*{(?P<size>\d+)}$') |
Piers Lauder | 533366b | 2003-04-29 23:58:08 +0000 | [diff] [blame] | 92 | MapCRLF = re.compile(r'\r\n|\r|\n') |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 93 | Response_code = re.compile(r'\[(?P<type>[A-Z-]+)( (?P<data>[^\]]*))?\]') |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 94 | Untagged_response = re.compile(r'\* (?P<type>[A-Z-]+)( (?P<data>.*))?') |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 95 | Untagged_status = re.compile(r'\* (?P<data>\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?') |
| 96 | |
| 97 | |
| 98 | |
| 99 | class IMAP4: |
| 100 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 101 | """IMAP4 client class. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 102 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 103 | Instantiate with: IMAP4([host[, port]]) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 104 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 105 | host - host's name (default: localhost); |
| 106 | port - port number (default: standard IMAP4 port). |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 107 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 108 | All IMAP4rev1 commands are supported by methods of the same |
| 109 | name (in lower-case). |
Guido van Rossum | 6884af7 | 1998-05-29 13:34:03 +0000 | [diff] [blame] | 110 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 111 | All arguments to commands are converted to strings, except for |
| 112 | AUTHENTICATE, and the last argument to APPEND which is passed as |
| 113 | an IMAP4 literal. If necessary (the string contains any |
| 114 | non-printing characters or white-space and isn't enclosed with |
| 115 | either parentheses or double quotes) each string is quoted. |
| 116 | However, the 'password' argument to the LOGIN command is always |
| 117 | quoted. If you want to avoid having an argument string quoted |
| 118 | (eg: the 'flags' argument to STORE) then enclose the string in |
| 119 | parentheses (eg: "(\Deleted)"). |
Guido van Rossum | 6884af7 | 1998-05-29 13:34:03 +0000 | [diff] [blame] | 120 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 121 | Each command returns a tuple: (type, [data, ...]) where 'type' |
| 122 | is usually 'OK' or 'NO', and 'data' is either the text from the |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 123 | tagged response, or untagged results from command. Each 'data' |
| 124 | is either a string, or a tuple. If a tuple, then the first part |
| 125 | is the header of the response, and the second part contains |
| 126 | the data (ie: 'literal' value). |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 127 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 128 | Errors raise the exception class <instance>.error("<reason>"). |
| 129 | IMAP4 server errors raise <instance>.abort("<reason>"), |
| 130 | which is a sub-class of 'error'. Mailbox status changes |
| 131 | from READ-WRITE to READ-ONLY raise the exception class |
| 132 | <instance>.readonly("<reason>"), which is a sub-class of 'abort'. |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 133 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 134 | "error" exceptions imply a program error. |
| 135 | "abort" exceptions imply the connection should be reset, and |
| 136 | the command re-tried. |
| 137 | "readonly" exceptions imply the command should be re-tried. |
Guido van Rossum | 8c06221 | 1999-12-13 23:27:45 +0000 | [diff] [blame] | 138 | |
Piers Lauder | d80ef02 | 2005-06-01 23:50:52 +0000 | [diff] [blame] | 139 | Note: to use this module, you must read the RFCs pertaining to the |
| 140 | IMAP4 protocol, as the semantics of the arguments to each IMAP4 |
| 141 | command are left to the invoker, not to mention the results. Also, |
| 142 | most IMAP servers implement a sub-set of the commands available here. |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 143 | """ |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 144 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 145 | class error(Exception): pass # Logical errors - debug required |
| 146 | class abort(error): pass # Service errors - close and retry |
| 147 | class readonly(abort): pass # Mailbox status changed to READ-ONLY |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 148 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 149 | mustquote = re.compile(r"[^\w!#$%&'*+,.:;<=>?^`|~-]") |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 150 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 151 | def __init__(self, host = '', port = IMAP4_PORT): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 152 | self.debug = Debug |
| 153 | self.state = 'LOGOUT' |
| 154 | self.literal = None # A literal argument to a command |
| 155 | self.tagged_commands = {} # Tagged commands awaiting response |
| 156 | self.untagged_responses = {} # {typ: [data, ...], ...} |
| 157 | self.continuation_response = '' # Last continuation response |
Piers Lauder | 14f3940 | 2005-08-31 10:46:29 +0000 | [diff] [blame] | 158 | self.is_readonly = False # READ-ONLY desired state |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 159 | self.tagnum = 0 |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 160 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 161 | # Open socket to server. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 162 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 163 | self.open(host, port) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 164 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 165 | # Create unique tag for this session, |
| 166 | # and compile tagged response matcher. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 167 | |
Piers Lauder | 2dfc168 | 2005-07-05 04:20:07 +0000 | [diff] [blame] | 168 | self.tagpre = Int2AP(random.randint(4096, 65535)) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 169 | self.tagre = re.compile(r'(?P<tag>' |
| 170 | + self.tagpre |
| 171 | + r'\d+) (?P<type>[A-Z]+) (?P<data>.*)') |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 172 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 173 | # Get server welcome message, |
| 174 | # request and store CAPABILITY response. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 175 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 176 | if __debug__: |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 177 | self._cmd_log_len = 10 |
| 178 | self._cmd_log_idx = 0 |
| 179 | self._cmd_log = {} # Last `_cmd_log_len' interactions |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 180 | if self.debug >= 1: |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 181 | self._mesg('imaplib version %s' % __version__) |
| 182 | self._mesg('new IMAP4 connection, tag=%s' % self.tagpre) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 183 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 184 | self.welcome = self._get_response() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 185 | if 'PREAUTH' in self.untagged_responses: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 186 | self.state = 'AUTH' |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 187 | elif 'OK' in self.untagged_responses: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 188 | self.state = 'NONAUTH' |
| 189 | else: |
| 190 | raise self.error(self.welcome) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 191 | |
Piers Lauder | d80ef02 | 2005-06-01 23:50:52 +0000 | [diff] [blame] | 192 | typ, dat = self.capability() |
| 193 | if dat == [None]: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 194 | raise self.error('no CAPABILITY response from server') |
Piers Lauder | d80ef02 | 2005-06-01 23:50:52 +0000 | [diff] [blame] | 195 | self.capabilities = tuple(dat[-1].upper().split()) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 196 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 197 | if __debug__: |
| 198 | if self.debug >= 3: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 199 | self._mesg('CAPABILITIES: %r' % (self.capabilities,)) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 200 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 201 | for version in AllowedVersions: |
| 202 | if not version in self.capabilities: |
| 203 | continue |
| 204 | self.PROTOCOL_VERSION = version |
| 205 | return |
Guido van Rossum | b1f0812 | 1998-06-25 02:22:16 +0000 | [diff] [blame] | 206 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 207 | raise self.error('server not IMAP4 compliant') |
Guido van Rossum | 38d8f4e | 1998-04-11 01:22:34 +0000 | [diff] [blame] | 208 | |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 209 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 210 | def __getattr__(self, attr): |
| 211 | # Allow UPPERCASE variants of IMAP4 command methods. |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 212 | if attr in Commands: |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 213 | return getattr(self, attr.lower()) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 214 | raise AttributeError("Unknown IMAP4 command: '%s'" % attr) |
Guido van Rossum | 26367a0 | 1998-09-28 15:34:46 +0000 | [diff] [blame] | 215 | |
| 216 | |
| 217 | |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 218 | # Overridable methods |
Guido van Rossum | 26367a0 | 1998-09-28 15:34:46 +0000 | [diff] [blame] | 219 | |
| 220 | |
Piers Lauder | f97b2d7 | 2002-06-05 22:31:57 +0000 | [diff] [blame] | 221 | def open(self, host = '', port = IMAP4_PORT): |
| 222 | """Setup connection to remote server on "host:port" |
| 223 | (default: localhost:standard IMAP4 port). |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 224 | This connection will be used by the routines: |
| 225 | read, readline, send, shutdown. |
| 226 | """ |
Piers Lauder | f97b2d7 | 2002-06-05 22:31:57 +0000 | [diff] [blame] | 227 | self.host = host |
| 228 | self.port = port |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 229 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Piers Lauder | f97b2d7 | 2002-06-05 22:31:57 +0000 | [diff] [blame] | 230 | self.sock.connect((host, port)) |
Guido van Rossum | c0f1bfe | 2001-10-15 13:47:08 +0000 | [diff] [blame] | 231 | self.file = self.sock.makefile('rb') |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 232 | |
| 233 | |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 234 | def read(self, size): |
| 235 | """Read 'size' bytes from remote.""" |
| 236 | return self.file.read(size) |
| 237 | |
| 238 | |
| 239 | def readline(self): |
| 240 | """Read line from remote.""" |
| 241 | return self.file.readline() |
| 242 | |
| 243 | |
| 244 | def send(self, data): |
| 245 | """Send data to remote.""" |
Martin v. Löwis | e12454f | 2002-02-16 23:06:19 +0000 | [diff] [blame] | 246 | self.sock.sendall(data) |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 247 | |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 248 | |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 249 | def shutdown(self): |
| 250 | """Close I/O established in "open".""" |
| 251 | self.file.close() |
| 252 | self.sock.close() |
| 253 | |
| 254 | |
| 255 | def socket(self): |
| 256 | """Return socket instance used to connect to IMAP4 server. |
| 257 | |
| 258 | socket = <instance>.socket() |
| 259 | """ |
| 260 | return self.sock |
| 261 | |
| 262 | |
| 263 | |
| 264 | # Utility methods |
| 265 | |
| 266 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 267 | def recent(self): |
| 268 | """Return most recent 'RECENT' responses if any exist, |
| 269 | else prompt server for an update using the 'NOOP' command. |
Guido van Rossum | 26367a0 | 1998-09-28 15:34:46 +0000 | [diff] [blame] | 270 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 271 | (typ, [data]) = <instance>.recent() |
Guido van Rossum | 26367a0 | 1998-09-28 15:34:46 +0000 | [diff] [blame] | 272 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 273 | 'data' is None if no new messages, |
| 274 | else list of RECENT responses, most recent last. |
| 275 | """ |
| 276 | name = 'RECENT' |
| 277 | typ, dat = self._untagged_response('OK', [None], name) |
| 278 | if dat[-1]: |
| 279 | return typ, dat |
| 280 | typ, dat = self.noop() # Prod server for response |
| 281 | return self._untagged_response(typ, dat, name) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 282 | |
| 283 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 284 | def response(self, code): |
| 285 | """Return data for response 'code' if received, or None. |
Guido van Rossum | 26367a0 | 1998-09-28 15:34:46 +0000 | [diff] [blame] | 286 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 287 | Old value for response 'code' is cleared. |
Guido van Rossum | 26367a0 | 1998-09-28 15:34:46 +0000 | [diff] [blame] | 288 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 289 | (code, [data]) = <instance>.response(code) |
| 290 | """ |
Eric S. Raymond | 25a0cbc | 2001-02-09 06:50:21 +0000 | [diff] [blame] | 291 | return self._untagged_response(code, [None], code.upper()) |
Guido van Rossum | 26367a0 | 1998-09-28 15:34:46 +0000 | [diff] [blame] | 292 | |
| 293 | |
Guido van Rossum | 26367a0 | 1998-09-28 15:34:46 +0000 | [diff] [blame] | 294 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 295 | # IMAP4 commands |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 296 | |
| 297 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 298 | def append(self, mailbox, flags, date_time, message): |
| 299 | """Append message to named mailbox. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 300 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 301 | (typ, [data]) = <instance>.append(mailbox, flags, date_time, message) |
Guido van Rossum | 8c06221 | 1999-12-13 23:27:45 +0000 | [diff] [blame] | 302 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 303 | All args except `message' can be None. |
| 304 | """ |
| 305 | name = 'APPEND' |
| 306 | if not mailbox: |
| 307 | mailbox = 'INBOX' |
| 308 | if flags: |
| 309 | if (flags[0],flags[-1]) != ('(',')'): |
| 310 | flags = '(%s)' % flags |
| 311 | else: |
| 312 | flags = None |
| 313 | if date_time: |
| 314 | date_time = Time2Internaldate(date_time) |
| 315 | else: |
| 316 | date_time = None |
Piers Lauder | 47404ff | 2003-04-29 23:40:59 +0000 | [diff] [blame] | 317 | self.literal = MapCRLF.sub(CRLF, message) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 318 | return self._simple_command(name, mailbox, flags, date_time) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 319 | |
| 320 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 321 | def authenticate(self, mechanism, authobject): |
| 322 | """Authenticate command - requires response processing. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 323 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 324 | 'mechanism' specifies which authentication mechanism is to |
| 325 | be used - it must appear in <instance>.capabilities in the |
| 326 | form AUTH=<mechanism>. |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 327 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 328 | 'authobject' must be a callable object: |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 329 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 330 | data = authobject(response) |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 331 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 332 | It will be called to process server continuation responses. |
| 333 | It should return data that will be encoded and sent to server. |
| 334 | It should return None if the client abort response '*' should |
| 335 | be sent instead. |
| 336 | """ |
Eric S. Raymond | 25a0cbc | 2001-02-09 06:50:21 +0000 | [diff] [blame] | 337 | mech = mechanism.upper() |
Neal Norwitz | b207170 | 2003-06-29 04:21:43 +0000 | [diff] [blame] | 338 | # XXX: shouldn't this code be removed, not commented out? |
| 339 | #cap = 'AUTH=%s' % mech |
Tim Peters | 77c06fb | 2002-11-24 02:35:35 +0000 | [diff] [blame] | 340 | #if not cap in self.capabilities: # Let the server decide! |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 341 | # raise self.error("Server doesn't allow %s authentication." % mech) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 342 | self.literal = _Authenticator(authobject).process |
| 343 | typ, dat = self._simple_command('AUTHENTICATE', mech) |
| 344 | if typ != 'OK': |
| 345 | raise self.error(dat[-1]) |
| 346 | self.state = 'AUTH' |
| 347 | return typ, dat |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 348 | |
| 349 | |
Piers Lauder | d80ef02 | 2005-06-01 23:50:52 +0000 | [diff] [blame] | 350 | def capability(self): |
| 351 | """(typ, [data]) = <instance>.capability() |
| 352 | Fetch capabilities list from server.""" |
| 353 | |
| 354 | name = 'CAPABILITY' |
| 355 | typ, dat = self._simple_command(name) |
| 356 | return self._untagged_response(typ, dat, name) |
| 357 | |
| 358 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 359 | def check(self): |
| 360 | """Checkpoint mailbox on server. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 361 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 362 | (typ, [data]) = <instance>.check() |
| 363 | """ |
| 364 | return self._simple_command('CHECK') |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 365 | |
| 366 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 367 | def close(self): |
| 368 | """Close currently selected mailbox. |
Guido van Rossum | eeec0af | 1998-04-09 14:20:31 +0000 | [diff] [blame] | 369 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 370 | Deleted messages are removed from writable mailbox. |
| 371 | This is the recommended command before 'LOGOUT'. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 372 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 373 | (typ, [data]) = <instance>.close() |
| 374 | """ |
| 375 | try: |
| 376 | typ, dat = self._simple_command('CLOSE') |
| 377 | finally: |
| 378 | self.state = 'AUTH' |
| 379 | return typ, dat |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 380 | |
| 381 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 382 | def copy(self, message_set, new_mailbox): |
| 383 | """Copy 'message_set' messages onto end of 'new_mailbox'. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 384 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 385 | (typ, [data]) = <instance>.copy(message_set, new_mailbox) |
| 386 | """ |
| 387 | return self._simple_command('COPY', message_set, new_mailbox) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 388 | |
| 389 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 390 | def create(self, mailbox): |
| 391 | """Create new mailbox. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 392 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 393 | (typ, [data]) = <instance>.create(mailbox) |
| 394 | """ |
| 395 | return self._simple_command('CREATE', mailbox) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 396 | |
| 397 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 398 | def delete(self, mailbox): |
| 399 | """Delete old mailbox. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 400 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 401 | (typ, [data]) = <instance>.delete(mailbox) |
| 402 | """ |
| 403 | return self._simple_command('DELETE', mailbox) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 404 | |
Martin v. Löwis | 7b9190b | 2004-07-27 05:07:19 +0000 | [diff] [blame] | 405 | def deleteacl(self, mailbox, who): |
| 406 | """Delete the ACLs (remove any rights) set for who on mailbox. |
| 407 | |
| 408 | (typ, [data]) = <instance>.deleteacl(mailbox, who) |
| 409 | """ |
| 410 | return self._simple_command('DELETEACL', mailbox, who) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 411 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 412 | def expunge(self): |
| 413 | """Permanently remove deleted items from selected mailbox. |
Guido van Rossum | eeec0af | 1998-04-09 14:20:31 +0000 | [diff] [blame] | 414 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 415 | Generates 'EXPUNGE' response for each deleted message. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 416 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 417 | (typ, [data]) = <instance>.expunge() |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 418 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 419 | 'data' is list of 'EXPUNGE'd message numbers in order received. |
| 420 | """ |
| 421 | name = 'EXPUNGE' |
| 422 | typ, dat = self._simple_command(name) |
| 423 | return self._untagged_response(typ, dat, name) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 424 | |
| 425 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 426 | def fetch(self, message_set, message_parts): |
| 427 | """Fetch (parts of) messages. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 428 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 429 | (typ, [data, ...]) = <instance>.fetch(message_set, message_parts) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 430 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 431 | 'message_parts' should be a string of selected parts |
| 432 | enclosed in parentheses, eg: "(UID BODY[TEXT])". |
Fred Drake | fd267d9 | 2000-05-25 03:25:26 +0000 | [diff] [blame] | 433 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 434 | 'data' are tuples of message part envelope and data. |
| 435 | """ |
| 436 | name = 'FETCH' |
| 437 | typ, dat = self._simple_command(name, message_set, message_parts) |
| 438 | return self._untagged_response(typ, dat, name) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 439 | |
| 440 | |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 441 | def getacl(self, mailbox): |
| 442 | """Get the ACLs for a mailbox. |
| 443 | |
| 444 | (typ, [data]) = <instance>.getacl(mailbox) |
| 445 | """ |
| 446 | typ, dat = self._simple_command('GETACL', mailbox) |
| 447 | return self._untagged_response(typ, dat, 'ACL') |
| 448 | |
| 449 | |
Piers Lauder | d80ef02 | 2005-06-01 23:50:52 +0000 | [diff] [blame] | 450 | def getannotation(self, mailbox, entry, attribute): |
| 451 | """(typ, [data]) = <instance>.getannotation(mailbox, entry, attribute) |
| 452 | Retrieve ANNOTATIONs.""" |
| 453 | |
| 454 | typ, dat = self._simple_command('GETANNOTATION', mailbox, entry, attribute) |
| 455 | return self._untagged_response(typ, dat, 'ANNOTATION') |
| 456 | |
| 457 | |
Piers Lauder | 3fca291 | 2002-06-17 07:07:20 +0000 | [diff] [blame] | 458 | def getquota(self, root): |
| 459 | """Get the quota root's resource usage and limits. |
| 460 | |
Neal Norwitz | 1ed564a | 2002-06-17 12:43:20 +0000 | [diff] [blame] | 461 | Part of the IMAP4 QUOTA extension defined in rfc2087. |
Piers Lauder | 3fca291 | 2002-06-17 07:07:20 +0000 | [diff] [blame] | 462 | |
| 463 | (typ, [data]) = <instance>.getquota(root) |
Neal Norwitz | 1ed564a | 2002-06-17 12:43:20 +0000 | [diff] [blame] | 464 | """ |
Piers Lauder | 3fca291 | 2002-06-17 07:07:20 +0000 | [diff] [blame] | 465 | typ, dat = self._simple_command('GETQUOTA', root) |
| 466 | return self._untagged_response(typ, dat, 'QUOTA') |
| 467 | |
| 468 | |
| 469 | def getquotaroot(self, mailbox): |
| 470 | """Get the list of quota roots for the named mailbox. |
| 471 | |
| 472 | (typ, [[QUOTAROOT responses...], [QUOTA responses]]) = <instance>.getquotaroot(mailbox) |
Neal Norwitz | 1ed564a | 2002-06-17 12:43:20 +0000 | [diff] [blame] | 473 | """ |
Piers Lauder | 6a4e635 | 2004-08-10 01:24:54 +0000 | [diff] [blame] | 474 | typ, dat = self._simple_command('GETQUOTAROOT', mailbox) |
Piers Lauder | 3fca291 | 2002-06-17 07:07:20 +0000 | [diff] [blame] | 475 | typ, quota = self._untagged_response(typ, dat, 'QUOTA') |
| 476 | typ, quotaroot = self._untagged_response(typ, dat, 'QUOTAROOT') |
Neal Norwitz | 1ed564a | 2002-06-17 12:43:20 +0000 | [diff] [blame] | 477 | return typ, [quotaroot, quota] |
Piers Lauder | 3fca291 | 2002-06-17 07:07:20 +0000 | [diff] [blame] | 478 | |
| 479 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 480 | def list(self, directory='""', pattern='*'): |
| 481 | """List mailbox names in directory matching pattern. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 482 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 483 | (typ, [data]) = <instance>.list(directory='""', pattern='*') |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 484 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 485 | 'data' is list of LIST responses. |
| 486 | """ |
| 487 | name = 'LIST' |
| 488 | typ, dat = self._simple_command(name, directory, pattern) |
| 489 | return self._untagged_response(typ, dat, name) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 490 | |
| 491 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 492 | def login(self, user, password): |
| 493 | """Identify client using plaintext password. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 494 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 495 | (typ, [data]) = <instance>.login(user, password) |
Guido van Rossum | 8c06221 | 1999-12-13 23:27:45 +0000 | [diff] [blame] | 496 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 497 | NB: 'password' will be quoted. |
| 498 | """ |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 499 | typ, dat = self._simple_command('LOGIN', user, self._quote(password)) |
| 500 | if typ != 'OK': |
| 501 | raise self.error(dat[-1]) |
| 502 | self.state = 'AUTH' |
| 503 | return typ, dat |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 504 | |
| 505 | |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 506 | def login_cram_md5(self, user, password): |
| 507 | """ Force use of CRAM-MD5 authentication. |
| 508 | |
| 509 | (typ, [data]) = <instance>.login_cram_md5(user, password) |
| 510 | """ |
| 511 | self.user, self.password = user, password |
| 512 | return self.authenticate('CRAM-MD5', self._CRAM_MD5_AUTH) |
| 513 | |
| 514 | |
| 515 | def _CRAM_MD5_AUTH(self, challenge): |
| 516 | """ Authobject to use with CRAM-MD5 authentication. """ |
| 517 | import hmac |
| 518 | return self.user + " " + hmac.HMAC(self.password, challenge).hexdigest() |
| 519 | |
| 520 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 521 | def logout(self): |
| 522 | """Shutdown connection to server. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 523 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 524 | (typ, [data]) = <instance>.logout() |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 525 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 526 | Returns server 'BYE' response. |
| 527 | """ |
| 528 | self.state = 'LOGOUT' |
| 529 | try: typ, dat = self._simple_command('LOGOUT') |
| 530 | except: typ, dat = 'NO', ['%s: %s' % sys.exc_info()[:2]] |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 531 | self.shutdown() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 532 | if 'BYE' in self.untagged_responses: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 533 | return 'BYE', self.untagged_responses['BYE'] |
| 534 | return typ, dat |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 535 | |
| 536 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 537 | def lsub(self, directory='""', pattern='*'): |
| 538 | """List 'subscribed' mailbox names in directory matching pattern. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 539 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 540 | (typ, [data, ...]) = <instance>.lsub(directory='""', pattern='*') |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 541 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 542 | 'data' are tuples of message part envelope and data. |
| 543 | """ |
| 544 | name = 'LSUB' |
| 545 | typ, dat = self._simple_command(name, directory, pattern) |
| 546 | return self._untagged_response(typ, dat, name) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 547 | |
Martin v. Löwis | 7b9190b | 2004-07-27 05:07:19 +0000 | [diff] [blame] | 548 | def myrights(self, mailbox): |
| 549 | """Show my ACLs for a mailbox (i.e. the rights that I have on mailbox). |
| 550 | |
| 551 | (typ, [data]) = <instance>.myrights(mailbox) |
| 552 | """ |
| 553 | typ,dat = self._simple_command('MYRIGHTS', mailbox) |
| 554 | return self._untagged_response(typ, dat, 'MYRIGHTS') |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 555 | |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 556 | def namespace(self): |
| 557 | """ Returns IMAP namespaces ala rfc2342 |
| 558 | |
| 559 | (typ, [data, ...]) = <instance>.namespace() |
| 560 | """ |
| 561 | name = 'NAMESPACE' |
| 562 | typ, dat = self._simple_command(name) |
| 563 | return self._untagged_response(typ, dat, name) |
| 564 | |
| 565 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 566 | def noop(self): |
| 567 | """Send NOOP command. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 568 | |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 569 | (typ, [data]) = <instance>.noop() |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 570 | """ |
| 571 | if __debug__: |
| 572 | if self.debug >= 3: |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 573 | self._dump_ur(self.untagged_responses) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 574 | return self._simple_command('NOOP') |
Guido van Rossum | 6884af7 | 1998-05-29 13:34:03 +0000 | [diff] [blame] | 575 | |
| 576 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 577 | def partial(self, message_num, message_part, start, length): |
| 578 | """Fetch truncated part of a message. |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 579 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 580 | (typ, [data, ...]) = <instance>.partial(message_num, message_part, start, length) |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 581 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 582 | 'data' is tuple of message part envelope and data. |
| 583 | """ |
| 584 | name = 'PARTIAL' |
| 585 | typ, dat = self._simple_command(name, message_num, message_part, start, length) |
| 586 | return self._untagged_response(typ, dat, 'FETCH') |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 587 | |
| 588 | |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 589 | def proxyauth(self, user): |
| 590 | """Assume authentication as "user". |
| 591 | |
| 592 | Allows an authorised administrator to proxy into any user's |
| 593 | mailbox. |
| 594 | |
| 595 | (typ, [data]) = <instance>.proxyauth(user) |
| 596 | """ |
| 597 | |
| 598 | name = 'PROXYAUTH' |
| 599 | return self._simple_command('PROXYAUTH', user) |
| 600 | |
| 601 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 602 | def rename(self, oldmailbox, newmailbox): |
| 603 | """Rename old mailbox name to new. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 604 | |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 605 | (typ, [data]) = <instance>.rename(oldmailbox, newmailbox) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 606 | """ |
| 607 | return self._simple_command('RENAME', oldmailbox, newmailbox) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 608 | |
| 609 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 610 | def search(self, charset, *criteria): |
| 611 | """Search mailbox for matching messages. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 612 | |
Martin v. Löwis | 3ae0f7a | 2003-03-27 16:59:38 +0000 | [diff] [blame] | 613 | (typ, [data]) = <instance>.search(charset, criterion, ...) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 614 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 615 | 'data' is space separated list of matching message numbers. |
| 616 | """ |
| 617 | name = 'SEARCH' |
| 618 | if charset: |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 619 | typ, dat = self._simple_command(name, 'CHARSET', charset, *criteria) |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 620 | else: |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 621 | typ, dat = self._simple_command(name, *criteria) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 622 | return self._untagged_response(typ, dat, name) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 623 | |
| 624 | |
Piers Lauder | 14f3940 | 2005-08-31 10:46:29 +0000 | [diff] [blame] | 625 | def select(self, mailbox='INBOX', readonly=False): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 626 | """Select a mailbox. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 627 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 628 | Flush all untagged responses. |
Guido van Rossum | 4658682 | 1998-05-18 14:39:42 +0000 | [diff] [blame] | 629 | |
Piers Lauder | 14f3940 | 2005-08-31 10:46:29 +0000 | [diff] [blame] | 630 | (typ, [data]) = <instance>.select(mailbox='INBOX', readonly=False) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 631 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 632 | 'data' is count of messages in mailbox ('EXISTS' response). |
Piers Lauder | 6a4e635 | 2004-08-10 01:24:54 +0000 | [diff] [blame] | 633 | |
| 634 | Mandated responses are ('FLAGS', 'EXISTS', 'RECENT', 'UIDVALIDITY'), so |
| 635 | other responses should be obtained via <instance>.response('FLAGS') etc. |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 636 | """ |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 637 | self.untagged_responses = {} # Flush old responses. |
| 638 | self.is_readonly = readonly |
Piers Lauder | 14f3940 | 2005-08-31 10:46:29 +0000 | [diff] [blame] | 639 | if readonly: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 640 | name = 'EXAMINE' |
| 641 | else: |
| 642 | name = 'SELECT' |
| 643 | typ, dat = self._simple_command(name, mailbox) |
| 644 | if typ != 'OK': |
| 645 | self.state = 'AUTH' # Might have been 'SELECTED' |
| 646 | return typ, dat |
| 647 | self.state = 'SELECTED' |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 648 | if 'READ-ONLY' in self.untagged_responses \ |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 649 | and not readonly: |
| 650 | if __debug__: |
| 651 | if self.debug >= 1: |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 652 | self._dump_ur(self.untagged_responses) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 653 | raise self.readonly('%s is not writable' % mailbox) |
| 654 | return typ, self.untagged_responses.get('EXISTS', [None]) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 655 | |
| 656 | |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 657 | def setacl(self, mailbox, who, what): |
| 658 | """Set a mailbox acl. |
| 659 | |
Piers Lauder | f167dc3 | 2004-03-25 00:12:21 +0000 | [diff] [blame] | 660 | (typ, [data]) = <instance>.setacl(mailbox, who, what) |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 661 | """ |
| 662 | return self._simple_command('SETACL', mailbox, who, what) |
| 663 | |
| 664 | |
Piers Lauder | d80ef02 | 2005-06-01 23:50:52 +0000 | [diff] [blame] | 665 | def setannotation(self, *args): |
| 666 | """(typ, [data]) = <instance>.setannotation(mailbox[, entry, attribute]+) |
| 667 | Set ANNOTATIONs.""" |
| 668 | |
| 669 | typ, dat = self._simple_command('SETANNOTATION', *args) |
| 670 | return self._untagged_response(typ, dat, 'ANNOTATION') |
| 671 | |
| 672 | |
Piers Lauder | 3fca291 | 2002-06-17 07:07:20 +0000 | [diff] [blame] | 673 | def setquota(self, root, limits): |
| 674 | """Set the quota root's resource limits. |
| 675 | |
| 676 | (typ, [data]) = <instance>.setquota(root, limits) |
Neal Norwitz | 1ed564a | 2002-06-17 12:43:20 +0000 | [diff] [blame] | 677 | """ |
Piers Lauder | 3fca291 | 2002-06-17 07:07:20 +0000 | [diff] [blame] | 678 | typ, dat = self._simple_command('SETQUOTA', root, limits) |
| 679 | return self._untagged_response(typ, dat, 'QUOTA') |
| 680 | |
| 681 | |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 682 | def sort(self, sort_criteria, charset, *search_criteria): |
| 683 | """IMAP4rev1 extension SORT command. |
| 684 | |
| 685 | (typ, [data]) = <instance>.sort(sort_criteria, charset, search_criteria, ...) |
| 686 | """ |
| 687 | name = 'SORT' |
Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 688 | #if not name in self.capabilities: # Let the server decide! |
| 689 | # raise self.error('unimplemented extension command: %s' % name) |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 690 | if (sort_criteria[0],sort_criteria[-1]) != ('(',')'): |
Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 691 | sort_criteria = '(%s)' % sort_criteria |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 692 | typ, dat = self._simple_command(name, sort_criteria, charset, *search_criteria) |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 693 | return self._untagged_response(typ, dat, name) |
| 694 | |
| 695 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 696 | def status(self, mailbox, names): |
| 697 | """Request named status conditions for mailbox. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 698 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 699 | (typ, [data]) = <instance>.status(mailbox, names) |
| 700 | """ |
| 701 | name = 'STATUS' |
Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 702 | #if self.PROTOCOL_VERSION == 'IMAP4': # Let the server decide! |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 703 | # raise self.error('%s unimplemented in IMAP4 (obtain IMAP4rev1 server, or re-code)' % name) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 704 | typ, dat = self._simple_command(name, mailbox, names) |
| 705 | return self._untagged_response(typ, dat, name) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 706 | |
| 707 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 708 | def store(self, message_set, command, flags): |
| 709 | """Alters flag dispositions for messages in mailbox. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 710 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 711 | (typ, [data]) = <instance>.store(message_set, command, flags) |
| 712 | """ |
| 713 | if (flags[0],flags[-1]) != ('(',')'): |
| 714 | flags = '(%s)' % flags # Avoid quoting the flags |
| 715 | typ, dat = self._simple_command('STORE', message_set, command, flags) |
| 716 | return self._untagged_response(typ, dat, 'FETCH') |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 717 | |
| 718 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 719 | def subscribe(self, mailbox): |
| 720 | """Subscribe to new mailbox. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 721 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 722 | (typ, [data]) = <instance>.subscribe(mailbox) |
| 723 | """ |
| 724 | return self._simple_command('SUBSCRIBE', mailbox) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 725 | |
| 726 | |
Martin v. Löwis | d892137 | 2003-11-10 06:44:44 +0000 | [diff] [blame] | 727 | def thread(self, threading_algorithm, charset, *search_criteria): |
| 728 | """IMAPrev1 extension THREAD command. |
| 729 | |
| 730 | (type, [data]) = <instance>.thread(threading_alogrithm, charset, search_criteria, ...) |
| 731 | """ |
| 732 | name = 'THREAD' |
| 733 | typ, dat = self._simple_command(name, threading_algorithm, charset, *search_criteria) |
| 734 | return self._untagged_response(typ, dat, name) |
| 735 | |
| 736 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 737 | def uid(self, command, *args): |
| 738 | """Execute "command arg ..." with messages identified by UID, |
| 739 | rather than message number. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 740 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 741 | (typ, [data]) = <instance>.uid(command, arg1, arg2, ...) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 742 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 743 | Returns response appropriate to 'command'. |
| 744 | """ |
Eric S. Raymond | 25a0cbc | 2001-02-09 06:50:21 +0000 | [diff] [blame] | 745 | command = command.upper() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 746 | if not command in Commands: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 747 | raise self.error("Unknown IMAP4 UID command: %s" % command) |
| 748 | if self.state not in Commands[command]: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 749 | raise self.error("command %s illegal in state %s, " |
| 750 | "only allowed in states %s" % |
| 751 | (command, self.state, |
| 752 | ', '.join(Commands[command]))) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 753 | name = 'UID' |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 754 | typ, dat = self._simple_command(name, command, *args) |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 755 | if command in ('SEARCH', 'SORT'): |
| 756 | name = command |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 757 | else: |
| 758 | name = 'FETCH' |
| 759 | return self._untagged_response(typ, dat, name) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 760 | |
| 761 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 762 | def unsubscribe(self, mailbox): |
| 763 | """Unsubscribe from old mailbox. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 764 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 765 | (typ, [data]) = <instance>.unsubscribe(mailbox) |
| 766 | """ |
| 767 | return self._simple_command('UNSUBSCRIBE', mailbox) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 768 | |
| 769 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 770 | def xatom(self, name, *args): |
| 771 | """Allow simple extension commands |
| 772 | notified by server in CAPABILITY response. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 773 | |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 774 | Assumes command is legal in current state. |
| 775 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 776 | (typ, [data]) = <instance>.xatom(name, arg, ...) |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 777 | |
| 778 | Returns response appropriate to extension command `name'. |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 779 | """ |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 780 | name = name.upper() |
Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 781 | #if not name in self.capabilities: # Let the server decide! |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 782 | # raise self.error('unknown extension command: %s' % name) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 783 | if not name in Commands: |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 784 | Commands[name] = (self.state,) |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 785 | return self._simple_command(name, *args) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 786 | |
| 787 | |
| 788 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 789 | # Private methods |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 790 | |
| 791 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 792 | def _append_untagged(self, typ, dat): |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 793 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 794 | if dat is None: dat = '' |
| 795 | ur = self.untagged_responses |
| 796 | if __debug__: |
| 797 | if self.debug >= 5: |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 798 | self._mesg('untagged_responses[%s] %s += ["%s"]' % |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 799 | (typ, len(ur.get(typ,'')), dat)) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 800 | if typ in ur: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 801 | ur[typ].append(dat) |
| 802 | else: |
| 803 | ur[typ] = [dat] |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 804 | |
| 805 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 806 | def _check_bye(self): |
| 807 | bye = self.untagged_responses.get('BYE') |
| 808 | if bye: |
| 809 | raise self.abort(bye[-1]) |
Guido van Rossum | 8c06221 | 1999-12-13 23:27:45 +0000 | [diff] [blame] | 810 | |
| 811 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 812 | def _command(self, name, *args): |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 813 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 814 | if self.state not in Commands[name]: |
| 815 | self.literal = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 816 | raise self.error("command %s illegal in state %s, " |
| 817 | "only allowed in states %s" % |
| 818 | (name, self.state, |
| 819 | ', '.join(Commands[name]))) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 820 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 821 | for typ in ('OK', 'NO', 'BAD'): |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 822 | if typ in self.untagged_responses: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 823 | del self.untagged_responses[typ] |
Guido van Rossum | 26367a0 | 1998-09-28 15:34:46 +0000 | [diff] [blame] | 824 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 825 | if 'READ-ONLY' in self.untagged_responses \ |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 826 | and not self.is_readonly: |
| 827 | raise self.readonly('mailbox status changed to READ-ONLY') |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 828 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 829 | tag = self._new_tag() |
| 830 | data = '%s %s' % (tag, name) |
| 831 | for arg in args: |
| 832 | if arg is None: continue |
| 833 | data = '%s %s' % (data, self._checkquote(arg)) |
Guido van Rossum | 6884af7 | 1998-05-29 13:34:03 +0000 | [diff] [blame] | 834 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 835 | literal = self.literal |
| 836 | if literal is not None: |
| 837 | self.literal = None |
| 838 | if type(literal) is type(self._command): |
| 839 | literator = literal |
| 840 | else: |
| 841 | literator = None |
| 842 | data = '%s {%s}' % (data, len(literal)) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 843 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 844 | if __debug__: |
| 845 | if self.debug >= 4: |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 846 | self._mesg('> %s' % data) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 847 | else: |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 848 | self._log('> %s' % data) |
Guido van Rossum | 8c06221 | 1999-12-13 23:27:45 +0000 | [diff] [blame] | 849 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 850 | try: |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 851 | self.send('%s%s' % (data, CRLF)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 852 | except (socket.error, OSError) as val: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 853 | raise self.abort('socket error: %s' % val) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 854 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 855 | if literal is None: |
| 856 | return tag |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 857 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 858 | while 1: |
| 859 | # Wait for continuation response |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 860 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 861 | while self._get_response(): |
| 862 | if self.tagged_commands[tag]: # BAD/NO? |
| 863 | return tag |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 864 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 865 | # Send literal |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 866 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 867 | if literator: |
| 868 | literal = literator(self.continuation_response) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 869 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 870 | if __debug__: |
| 871 | if self.debug >= 4: |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 872 | self._mesg('write literal size %s' % len(literal)) |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 873 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 874 | try: |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 875 | self.send(literal) |
| 876 | self.send(CRLF) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 877 | except (socket.error, OSError) as val: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 878 | raise self.abort('socket error: %s' % val) |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 879 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 880 | if not literator: |
| 881 | break |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 882 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 883 | return tag |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 884 | |
| 885 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 886 | def _command_complete(self, name, tag): |
| 887 | self._check_bye() |
| 888 | try: |
| 889 | typ, data = self._get_tagged_response(tag) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 890 | except self.abort as val: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 891 | raise self.abort('command: %s => %s' % (name, val)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 892 | except self.error as val: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 893 | raise self.error('command: %s => %s' % (name, val)) |
| 894 | self._check_bye() |
| 895 | if typ == 'BAD': |
| 896 | raise self.error('%s command error: %s %s' % (name, typ, data)) |
| 897 | return typ, data |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 898 | |
| 899 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 900 | def _get_response(self): |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 901 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 902 | # Read response and store. |
| 903 | # |
| 904 | # Returns None for continuation responses, |
| 905 | # otherwise first response line received. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 906 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 907 | resp = self._get_line() |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 908 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 909 | # Command completion response? |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 910 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 911 | if self._match(self.tagre, resp): |
| 912 | tag = self.mo.group('tag') |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 913 | if not tag in self.tagged_commands: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 914 | raise self.abort('unexpected tagged response: %s' % resp) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 915 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 916 | typ = self.mo.group('type') |
| 917 | dat = self.mo.group('data') |
| 918 | self.tagged_commands[tag] = (typ, [dat]) |
| 919 | else: |
| 920 | dat2 = None |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 921 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 922 | # '*' (untagged) responses? |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 923 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 924 | if not self._match(Untagged_response, resp): |
| 925 | if self._match(Untagged_status, resp): |
| 926 | dat2 = self.mo.group('data2') |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 927 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 928 | if self.mo is None: |
| 929 | # Only other possibility is '+' (continuation) response... |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 930 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 931 | if self._match(Continuation, resp): |
| 932 | self.continuation_response = self.mo.group('data') |
| 933 | return None # NB: indicates continuation |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 934 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 935 | raise self.abort("unexpected response: '%s'" % resp) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 936 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 937 | typ = self.mo.group('type') |
| 938 | dat = self.mo.group('data') |
| 939 | if dat is None: dat = '' # Null untagged response |
| 940 | if dat2: dat = dat + ' ' + dat2 |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 941 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 942 | # Is there a literal to come? |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 943 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 944 | while self._match(Literal, dat): |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 945 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 946 | # Read literal direct from connection. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 947 | |
Eric S. Raymond | 25a0cbc | 2001-02-09 06:50:21 +0000 | [diff] [blame] | 948 | size = int(self.mo.group('size')) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 949 | if __debug__: |
| 950 | if self.debug >= 4: |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 951 | self._mesg('read literal size %s' % size) |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 952 | data = self.read(size) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 953 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 954 | # Store response with literal as tuple |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 955 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 956 | self._append_untagged(typ, (dat, data)) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 957 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 958 | # Read trailer - possibly containing another literal |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 959 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 960 | dat = self._get_line() |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 961 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 962 | self._append_untagged(typ, dat) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 963 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 964 | # Bracketed response information? |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 965 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 966 | if typ in ('OK', 'NO', 'BAD') and self._match(Response_code, dat): |
| 967 | self._append_untagged(self.mo.group('type'), self.mo.group('data')) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 968 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 969 | if __debug__: |
| 970 | if self.debug >= 1 and typ in ('NO', 'BAD', 'BYE'): |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 971 | self._mesg('%s response: %s' % (typ, dat)) |
Guido van Rossum | 26367a0 | 1998-09-28 15:34:46 +0000 | [diff] [blame] | 972 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 973 | return resp |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 974 | |
| 975 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 976 | def _get_tagged_response(self, tag): |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 977 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 978 | while 1: |
| 979 | result = self.tagged_commands[tag] |
| 980 | if result is not None: |
| 981 | del self.tagged_commands[tag] |
| 982 | return result |
Guido van Rossum | f36b182 | 2000-02-17 17:12:39 +0000 | [diff] [blame] | 983 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 984 | # Some have reported "unexpected response" exceptions. |
| 985 | # Note that ignoring them here causes loops. |
| 986 | # Instead, send me details of the unexpected response and |
| 987 | # I'll update the code in `_get_response()'. |
Guido van Rossum | f36b182 | 2000-02-17 17:12:39 +0000 | [diff] [blame] | 988 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 989 | try: |
| 990 | self._get_response() |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 991 | except self.abort as val: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 992 | if __debug__: |
| 993 | if self.debug >= 1: |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 994 | self.print_log() |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 995 | raise |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 996 | |
| 997 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 998 | def _get_line(self): |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 999 | |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 1000 | line = self.readline() |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1001 | if not line: |
| 1002 | raise self.abort('socket error: EOF') |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1003 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1004 | # Protocol mandates all lines terminated by CRLF |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1005 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1006 | line = line[:-2] |
| 1007 | if __debug__: |
| 1008 | if self.debug >= 4: |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 1009 | self._mesg('< %s' % line) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1010 | else: |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 1011 | self._log('< %s' % line) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1012 | return line |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1013 | |
| 1014 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1015 | def _match(self, cre, s): |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1016 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1017 | # Run compiled regular expression match method on 's'. |
| 1018 | # Save result, return success. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1019 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1020 | self.mo = cre.match(s) |
| 1021 | if __debug__: |
| 1022 | if self.mo is not None and self.debug >= 5: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 1023 | self._mesg("\tmatched r'%s' => %r" % (cre.pattern, self.mo.groups())) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1024 | return self.mo is not None |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1025 | |
| 1026 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1027 | def _new_tag(self): |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1028 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1029 | tag = '%s%s' % (self.tagpre, self.tagnum) |
| 1030 | self.tagnum = self.tagnum + 1 |
| 1031 | self.tagged_commands[tag] = None |
| 1032 | return tag |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1033 | |
| 1034 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1035 | def _checkquote(self, arg): |
Guido van Rossum | 8c06221 | 1999-12-13 23:27:45 +0000 | [diff] [blame] | 1036 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1037 | # Must quote command args if non-alphanumeric chars present, |
| 1038 | # and not already quoted. |
Guido van Rossum | 8c06221 | 1999-12-13 23:27:45 +0000 | [diff] [blame] | 1039 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1040 | if type(arg) is not type(''): |
| 1041 | return arg |
Piers Lauder | c09acfd | 2004-10-08 04:05:39 +0000 | [diff] [blame] | 1042 | if len(arg) >= 2 and (arg[0],arg[-1]) in (('(',')'),('"','"')): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1043 | return arg |
Piers Lauder | c09acfd | 2004-10-08 04:05:39 +0000 | [diff] [blame] | 1044 | if arg and self.mustquote.search(arg) is None: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1045 | return arg |
| 1046 | return self._quote(arg) |
Guido van Rossum | 8c06221 | 1999-12-13 23:27:45 +0000 | [diff] [blame] | 1047 | |
| 1048 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1049 | def _quote(self, arg): |
Guido van Rossum | 8c06221 | 1999-12-13 23:27:45 +0000 | [diff] [blame] | 1050 | |
Eric S. Raymond | 25a0cbc | 2001-02-09 06:50:21 +0000 | [diff] [blame] | 1051 | arg = arg.replace('\\', '\\\\') |
| 1052 | arg = arg.replace('"', '\\"') |
Guido van Rossum | 8c06221 | 1999-12-13 23:27:45 +0000 | [diff] [blame] | 1053 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1054 | return '"%s"' % arg |
Guido van Rossum | 8c06221 | 1999-12-13 23:27:45 +0000 | [diff] [blame] | 1055 | |
| 1056 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1057 | def _simple_command(self, name, *args): |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1058 | |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 1059 | return self._command_complete(name, self._command(name, *args)) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1060 | |
| 1061 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1062 | def _untagged_response(self, typ, dat, name): |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1063 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1064 | if typ == 'NO': |
| 1065 | return typ, dat |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1066 | if not name in self.untagged_responses: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1067 | return typ, [None] |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 1068 | data = self.untagged_responses.pop(name) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1069 | if __debug__: |
| 1070 | if self.debug >= 5: |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 1071 | self._mesg('untagged_responses[%s] => %s' % (name, data)) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1072 | return typ, data |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1073 | |
| 1074 | |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 1075 | if __debug__: |
| 1076 | |
| 1077 | def _mesg(self, s, secs=None): |
| 1078 | if secs is None: |
| 1079 | secs = time.time() |
| 1080 | tm = time.strftime('%M:%S', time.localtime(secs)) |
| 1081 | sys.stderr.write(' %s.%02d %s\n' % (tm, (secs*100)%100, s)) |
| 1082 | sys.stderr.flush() |
| 1083 | |
| 1084 | def _dump_ur(self, dict): |
| 1085 | # Dump untagged responses (in `dict'). |
| 1086 | l = dict.items() |
| 1087 | if not l: return |
| 1088 | t = '\n\t\t' |
| 1089 | l = map(lambda x:'%s: "%s"' % (x[0], x[1][0] and '" "'.join(x[1]) or ''), l) |
| 1090 | self._mesg('untagged responses dump:%s%s' % (t, t.join(l))) |
| 1091 | |
| 1092 | def _log(self, line): |
| 1093 | # Keep log of last `_cmd_log_len' interactions for debugging. |
| 1094 | self._cmd_log[self._cmd_log_idx] = (line, time.time()) |
| 1095 | self._cmd_log_idx += 1 |
| 1096 | if self._cmd_log_idx >= self._cmd_log_len: |
| 1097 | self._cmd_log_idx = 0 |
| 1098 | |
| 1099 | def print_log(self): |
| 1100 | self._mesg('last %d IMAP4 interactions:' % len(self._cmd_log)) |
| 1101 | i, n = self._cmd_log_idx, self._cmd_log_len |
| 1102 | while n: |
| 1103 | try: |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 1104 | self._mesg(*self._cmd_log[i]) |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 1105 | except: |
| 1106 | pass |
| 1107 | i += 1 |
| 1108 | if i >= self._cmd_log_len: |
| 1109 | i = 0 |
| 1110 | n -= 1 |
| 1111 | |
| 1112 | |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1113 | |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame] | 1114 | class IMAP4_SSL(IMAP4): |
| 1115 | |
| 1116 | """IMAP4 client class over SSL connection |
| 1117 | |
Piers Lauder | 95f8495 | 2002-03-08 09:05:12 +0000 | [diff] [blame] | 1118 | Instantiate with: IMAP4_SSL([host[, port[, keyfile[, certfile]]]]) |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame] | 1119 | |
| 1120 | host - host's name (default: localhost); |
| 1121 | port - port number (default: standard IMAP4 SSL port). |
| 1122 | keyfile - PEM formatted file that contains your private key (default: None); |
| 1123 | certfile - PEM formatted certificate chain file (default: None); |
| 1124 | |
| 1125 | for more documentation see the docstring of the parent class IMAP4. |
| 1126 | """ |
| 1127 | |
| 1128 | |
| 1129 | def __init__(self, host = '', port = IMAP4_SSL_PORT, keyfile = None, certfile = None): |
| 1130 | self.keyfile = keyfile |
| 1131 | self.certfile = certfile |
| 1132 | IMAP4.__init__(self, host, port) |
| 1133 | |
| 1134 | |
Piers Lauder | f97b2d7 | 2002-06-05 22:31:57 +0000 | [diff] [blame] | 1135 | def open(self, host = '', port = IMAP4_SSL_PORT): |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame] | 1136 | """Setup connection to remote server on "host:port". |
Piers Lauder | f97b2d7 | 2002-06-05 22:31:57 +0000 | [diff] [blame] | 1137 | (default: localhost:standard IMAP4 SSL port). |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame] | 1138 | This connection will be used by the routines: |
| 1139 | read, readline, send, shutdown. |
| 1140 | """ |
Piers Lauder | f97b2d7 | 2002-06-05 22:31:57 +0000 | [diff] [blame] | 1141 | self.host = host |
| 1142 | self.port = port |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame] | 1143 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Piers Lauder | f97b2d7 | 2002-06-05 22:31:57 +0000 | [diff] [blame] | 1144 | self.sock.connect((host, port)) |
| 1145 | self.sslobj = socket.ssl(self.sock, self.keyfile, self.certfile) |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame] | 1146 | |
| 1147 | |
| 1148 | def read(self, size): |
| 1149 | """Read 'size' bytes from remote.""" |
Piers Lauder | 0c09293 | 2002-06-23 10:47:13 +0000 | [diff] [blame] | 1150 | # sslobj.read() sometimes returns < size bytes |
Piers Lauder | 8f2b244 | 2004-05-20 01:16:14 +0000 | [diff] [blame] | 1151 | chunks = [] |
| 1152 | read = 0 |
| 1153 | while read < size: |
| 1154 | data = self.sslobj.read(size-read) |
| 1155 | read += len(data) |
Piers Lauder | c1e32b6 | 2004-05-20 11:32:35 +0000 | [diff] [blame] | 1156 | chunks.append(data) |
Piers Lauder | 0c09293 | 2002-06-23 10:47:13 +0000 | [diff] [blame] | 1157 | |
Piers Lauder | 8f2b244 | 2004-05-20 01:16:14 +0000 | [diff] [blame] | 1158 | return ''.join(chunks) |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame] | 1159 | |
| 1160 | |
| 1161 | def readline(self): |
| 1162 | """Read line from remote.""" |
Piers Lauder | 0c09293 | 2002-06-23 10:47:13 +0000 | [diff] [blame] | 1163 | # NB: socket.ssl needs a "readline" method, or perhaps a "makefile" method. |
Piers Lauder | 8f2b244 | 2004-05-20 01:16:14 +0000 | [diff] [blame] | 1164 | line = [] |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame] | 1165 | while 1: |
| 1166 | char = self.sslobj.read(1) |
Piers Lauder | 8f2b244 | 2004-05-20 01:16:14 +0000 | [diff] [blame] | 1167 | line.append(char) |
| 1168 | if char == "\n": return ''.join(line) |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame] | 1169 | |
| 1170 | |
| 1171 | def send(self, data): |
| 1172 | """Send data to remote.""" |
Piers Lauder | 0c09293 | 2002-06-23 10:47:13 +0000 | [diff] [blame] | 1173 | # NB: socket.ssl needs a "sendall" method to match socket objects. |
| 1174 | bytes = len(data) |
| 1175 | while bytes > 0: |
| 1176 | sent = self.sslobj.write(data) |
| 1177 | if sent == bytes: |
| 1178 | break # avoid copy |
| 1179 | data = data[sent:] |
| 1180 | bytes = bytes - sent |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame] | 1181 | |
| 1182 | |
| 1183 | def shutdown(self): |
| 1184 | """Close I/O established in "open".""" |
| 1185 | self.sock.close() |
| 1186 | |
| 1187 | |
| 1188 | def socket(self): |
| 1189 | """Return socket instance used to connect to IMAP4 server. |
| 1190 | |
| 1191 | socket = <instance>.socket() |
| 1192 | """ |
| 1193 | return self.sock |
| 1194 | |
| 1195 | |
| 1196 | def ssl(self): |
| 1197 | """Return SSLObject instance used to communicate with the IMAP4 server. |
| 1198 | |
| 1199 | ssl = <instance>.socket.ssl() |
| 1200 | """ |
| 1201 | return self.sslobj |
| 1202 | |
| 1203 | |
| 1204 | |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 1205 | class IMAP4_stream(IMAP4): |
| 1206 | |
| 1207 | """IMAP4 client class over a stream |
| 1208 | |
| 1209 | Instantiate with: IMAP4_stream(command) |
| 1210 | |
| 1211 | where "command" is a string that can be passed to os.popen2() |
| 1212 | |
| 1213 | for more documentation see the docstring of the parent class IMAP4. |
| 1214 | """ |
| 1215 | |
| 1216 | |
| 1217 | def __init__(self, command): |
| 1218 | self.command = command |
| 1219 | IMAP4.__init__(self) |
| 1220 | |
| 1221 | |
| 1222 | def open(self, host = None, port = None): |
| 1223 | """Setup a stream connection. |
| 1224 | This connection will be used by the routines: |
| 1225 | read, readline, send, shutdown. |
| 1226 | """ |
| 1227 | self.host = None # For compatibility with parent class |
| 1228 | self.port = None |
| 1229 | self.sock = None |
| 1230 | self.file = None |
| 1231 | self.writefile, self.readfile = os.popen2(self.command) |
| 1232 | |
| 1233 | |
| 1234 | def read(self, size): |
| 1235 | """Read 'size' bytes from remote.""" |
| 1236 | return self.readfile.read(size) |
| 1237 | |
| 1238 | |
| 1239 | def readline(self): |
| 1240 | """Read line from remote.""" |
| 1241 | return self.readfile.readline() |
| 1242 | |
| 1243 | |
| 1244 | def send(self, data): |
| 1245 | """Send data to remote.""" |
| 1246 | self.writefile.write(data) |
| 1247 | self.writefile.flush() |
| 1248 | |
| 1249 | |
| 1250 | def shutdown(self): |
| 1251 | """Close I/O established in "open".""" |
| 1252 | self.readfile.close() |
| 1253 | self.writefile.close() |
| 1254 | |
| 1255 | |
| 1256 | |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 1257 | class _Authenticator: |
| 1258 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1259 | """Private class to provide en/decoding |
| 1260 | for base64-based authentication conversation. |
| 1261 | """ |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 1262 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1263 | def __init__(self, mechinst): |
| 1264 | self.mech = mechinst # Callable object to provide/process data |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 1265 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1266 | def process(self, data): |
| 1267 | ret = self.mech(self.decode(data)) |
| 1268 | if ret is None: |
| 1269 | return '*' # Abort conversation |
| 1270 | return self.encode(ret) |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 1271 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1272 | def encode(self, inp): |
| 1273 | # |
| 1274 | # Invoke binascii.b2a_base64 iteratively with |
| 1275 | # short even length buffers, strip the trailing |
| 1276 | # line feed from the result and append. "Even" |
| 1277 | # means a number that factors to both 6 and 8, |
| 1278 | # so when it gets to the end of the 8-bit input |
| 1279 | # there's no partial 6-bit output. |
| 1280 | # |
| 1281 | oup = '' |
| 1282 | while inp: |
| 1283 | if len(inp) > 48: |
| 1284 | t = inp[:48] |
| 1285 | inp = inp[48:] |
| 1286 | else: |
| 1287 | t = inp |
| 1288 | inp = '' |
| 1289 | e = binascii.b2a_base64(t) |
| 1290 | if e: |
| 1291 | oup = oup + e[:-1] |
| 1292 | return oup |
| 1293 | |
| 1294 | def decode(self, inp): |
| 1295 | if not inp: |
| 1296 | return '' |
| 1297 | return binascii.a2b_base64(inp) |
| 1298 | |
Guido van Rossum | eda960a | 1998-06-18 14:24:28 +0000 | [diff] [blame] | 1299 | |
| 1300 | |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1301 | Mon2num = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1302 | 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12} |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1303 | |
| 1304 | def Internaldate2tuple(resp): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1305 | """Convert IMAP4 INTERNALDATE to UT. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1306 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1307 | Returns Python time module tuple. |
| 1308 | """ |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1309 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1310 | mo = InternalDate.match(resp) |
| 1311 | if not mo: |
| 1312 | return None |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1313 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1314 | mon = Mon2num[mo.group('mon')] |
| 1315 | zonen = mo.group('zonen') |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1316 | |
Jeremy Hylton | f5d3ea0 | 2001-02-22 13:24:27 +0000 | [diff] [blame] | 1317 | day = int(mo.group('day')) |
| 1318 | year = int(mo.group('year')) |
| 1319 | hour = int(mo.group('hour')) |
| 1320 | min = int(mo.group('min')) |
| 1321 | sec = int(mo.group('sec')) |
| 1322 | zoneh = int(mo.group('zoneh')) |
| 1323 | zonem = int(mo.group('zonem')) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1324 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1325 | # INTERNALDATE timezone must be subtracted to get UT |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1326 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1327 | zone = (zoneh*60 + zonem)*60 |
| 1328 | if zonen == '-': |
| 1329 | zone = -zone |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1330 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1331 | tt = (year, mon, day, hour, min, sec, -1, -1, -1) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1332 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1333 | utc = time.mktime(tt) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1334 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1335 | # Following is necessary because the time module has no 'mkgmtime'. |
| 1336 | # 'mktime' assumes arg in local timezone, so adds timezone/altzone. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1337 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1338 | lt = time.localtime(utc) |
| 1339 | if time.daylight and lt[-1]: |
| 1340 | zone = zone + time.altzone |
| 1341 | else: |
| 1342 | zone = zone + time.timezone |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1343 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1344 | return time.localtime(utc - zone) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1345 | |
| 1346 | |
| 1347 | |
| 1348 | def Int2AP(num): |
| 1349 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1350 | """Convert integer to A-P string representation.""" |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1351 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1352 | val = ''; AP = 'ABCDEFGHIJKLMNOP' |
| 1353 | num = int(abs(num)) |
| 1354 | while num: |
| 1355 | num, mod = divmod(num, 16) |
| 1356 | val = AP[mod] + val |
| 1357 | return val |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1358 | |
| 1359 | |
| 1360 | |
| 1361 | def ParseFlags(resp): |
| 1362 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1363 | """Convert IMAP4 flags response to python tuple.""" |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1364 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1365 | mo = Flags.match(resp) |
| 1366 | if not mo: |
| 1367 | return () |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1368 | |
Eric S. Raymond | 25a0cbc | 2001-02-09 06:50:21 +0000 | [diff] [blame] | 1369 | return tuple(mo.group('flags').split()) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1370 | |
| 1371 | |
| 1372 | def Time2Internaldate(date_time): |
| 1373 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1374 | """Convert 'date_time' to IMAP4 INTERNALDATE representation. |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1375 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1376 | Return string in form: '"DD-Mmm-YYYY HH:MM:SS +HHMM"' |
| 1377 | """ |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1378 | |
Fred Drake | db51920 | 2002-01-05 17:17:09 +0000 | [diff] [blame] | 1379 | if isinstance(date_time, (int, float)): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1380 | tt = time.localtime(date_time) |
Fred Drake | db51920 | 2002-01-05 17:17:09 +0000 | [diff] [blame] | 1381 | elif isinstance(date_time, (tuple, time.struct_time)): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1382 | tt = date_time |
Piers Lauder | 3fca291 | 2002-06-17 07:07:20 +0000 | [diff] [blame] | 1383 | elif isinstance(date_time, str) and (date_time[0],date_time[-1]) == ('"','"'): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1384 | return date_time # Assume in correct format |
Fred Drake | db51920 | 2002-01-05 17:17:09 +0000 | [diff] [blame] | 1385 | else: |
| 1386 | raise ValueError("date_time not of a known type") |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1387 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1388 | dt = time.strftime("%d-%b-%Y %H:%M:%S", tt) |
| 1389 | if dt[0] == '0': |
| 1390 | dt = ' ' + dt[1:] |
| 1391 | if time.daylight and tt[-1]: |
| 1392 | zone = -time.altzone |
| 1393 | else: |
| 1394 | zone = -time.timezone |
Raymond Hettinger | ffdb8bb | 2004-09-27 15:29:05 +0000 | [diff] [blame] | 1395 | return '"' + dt + " %+03d%02d" % divmod(zone//60, 60) + '"' |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1396 | |
| 1397 | |
| 1398 | |
Guido van Rossum | 8c06221 | 1999-12-13 23:27:45 +0000 | [diff] [blame] | 1399 | if __name__ == '__main__': |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1400 | |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 1401 | # To test: invoke either as 'python imaplib.py [IMAP4_server_hostname]' |
| 1402 | # or 'python imaplib.py -s "rsh IMAP4_server_hostname exec /etc/rimapd"' |
| 1403 | # to test the IMAP4_stream class |
| 1404 | |
Guido van Rossum | a79bbac | 2001-08-13 15:34:41 +0000 | [diff] [blame] | 1405 | import getopt, getpass |
Guido van Rossum | d659693 | 1998-05-29 18:08:48 +0000 | [diff] [blame] | 1406 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1407 | try: |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 1408 | optlist, args = getopt.getopt(sys.argv[1:], 'd:s:') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1409 | except getopt.error as val: |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 1410 | optlist, args = (), () |
Guido van Rossum | 66d4513 | 2000-03-28 20:20:53 +0000 | [diff] [blame] | 1411 | |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 1412 | stream_command = None |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1413 | for opt,val in optlist: |
| 1414 | if opt == '-d': |
| 1415 | Debug = int(val) |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 1416 | elif opt == '-s': |
| 1417 | stream_command = val |
| 1418 | if not args: args = (stream_command,) |
Guido van Rossum | 66d4513 | 2000-03-28 20:20:53 +0000 | [diff] [blame] | 1419 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1420 | if not args: args = ('',) |
Guido van Rossum | 66d4513 | 2000-03-28 20:20:53 +0000 | [diff] [blame] | 1421 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1422 | host = args[0] |
Guido van Rossum | b1f0812 | 1998-06-25 02:22:16 +0000 | [diff] [blame] | 1423 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1424 | USER = getpass.getuser() |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 1425 | PASSWD = getpass.getpass("IMAP password for %s on %s: " % (USER, host or "localhost")) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1426 | |
Piers Lauder | 47404ff | 2003-04-29 23:40:59 +0000 | [diff] [blame] | 1427 | test_mesg = 'From: %(user)s@localhost%(lf)sSubject: IMAP4 test%(lf)s%(lf)sdata...%(lf)s' % {'user':USER, 'lf':'\n'} |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1428 | test_seq1 = ( |
| 1429 | ('login', (USER, PASSWD)), |
| 1430 | ('create', ('/tmp/xxx 1',)), |
| 1431 | ('rename', ('/tmp/xxx 1', '/tmp/yyy')), |
| 1432 | ('CREATE', ('/tmp/yyz 2',)), |
| 1433 | ('append', ('/tmp/yyz 2', None, None, test_mesg)), |
| 1434 | ('list', ('/tmp', 'yy*')), |
| 1435 | ('select', ('/tmp/yyz 2',)), |
| 1436 | ('search', (None, 'SUBJECT', 'test')), |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 1437 | ('fetch', ('1', '(FLAGS INTERNALDATE RFC822)')), |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1438 | ('store', ('1', 'FLAGS', '(\Deleted)')), |
Piers Lauder | 15e5d53 | 2001-07-20 10:52:06 +0000 | [diff] [blame] | 1439 | ('namespace', ()), |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1440 | ('expunge', ()), |
| 1441 | ('recent', ()), |
| 1442 | ('close', ()), |
| 1443 | ) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1444 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1445 | test_seq2 = ( |
| 1446 | ('select', ()), |
| 1447 | ('response',('UIDVALIDITY',)), |
| 1448 | ('uid', ('SEARCH', 'ALL')), |
| 1449 | ('response', ('EXISTS',)), |
| 1450 | ('append', (None, None, None, test_mesg)), |
| 1451 | ('recent', ()), |
| 1452 | ('logout', ()), |
| 1453 | ) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1454 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1455 | def run(cmd, args): |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 1456 | M._mesg('%s %s' % (cmd, args)) |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 1457 | typ, dat = getattr(M, cmd)(*args) |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 1458 | M._mesg('%s => %s %s' % (cmd, typ, dat)) |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 1459 | if typ == 'NO': raise dat[0] |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1460 | return dat |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1461 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1462 | try: |
Piers Lauder | e0273de | 2002-11-22 05:53:04 +0000 | [diff] [blame] | 1463 | if stream_command: |
| 1464 | M = IMAP4_stream(stream_command) |
| 1465 | else: |
| 1466 | M = IMAP4(host) |
| 1467 | if M.state == 'AUTH': |
Tim Peters | 77c06fb | 2002-11-24 02:35:35 +0000 | [diff] [blame] | 1468 | test_seq1 = test_seq1[1:] # Login not needed |
Piers Lauder | f2d7d15 | 2002-02-22 01:15:17 +0000 | [diff] [blame] | 1469 | M._mesg('PROTOCOL_VERSION = %s' % M.PROTOCOL_VERSION) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 1470 | M._mesg('CAPABILITIES = %r' % (M.capabilities,)) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1471 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1472 | for cmd,args in test_seq1: |
| 1473 | run(cmd, args) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1474 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1475 | for ml in run('list', ('/tmp/', 'yy%')): |
| 1476 | mo = re.match(r'.*"([^"]+)"$', ml) |
| 1477 | if mo: path = mo.group(1) |
Eric S. Raymond | 25a0cbc | 2001-02-09 06:50:21 +0000 | [diff] [blame] | 1478 | else: path = ml.split()[-1] |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1479 | run('delete', (path,)) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1480 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1481 | for cmd,args in test_seq2: |
| 1482 | dat = run(cmd, args) |
Guido van Rossum | c2c07fa | 1998-04-09 13:51:46 +0000 | [diff] [blame] | 1483 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1484 | if (cmd,args) != ('uid', ('SEARCH', 'ALL')): |
| 1485 | continue |
Guido van Rossum | 38d8f4e | 1998-04-11 01:22:34 +0000 | [diff] [blame] | 1486 | |
Eric S. Raymond | 25a0cbc | 2001-02-09 06:50:21 +0000 | [diff] [blame] | 1487 | uid = dat[-1].split() |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1488 | if not uid: continue |
| 1489 | run('uid', ('FETCH', '%s' % uid[-1], |
| 1490 | '(FLAGS INTERNALDATE RFC822.SIZE RFC822.HEADER RFC822.TEXT)')) |
Guido van Rossum | 66d4513 | 2000-03-28 20:20:53 +0000 | [diff] [blame] | 1491 | |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1492 | print('\nAll tests OK.') |
Guido van Rossum | 66d4513 | 2000-03-28 20:20:53 +0000 | [diff] [blame] | 1493 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1494 | except: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1495 | print('\nTests failed.') |
Guido van Rossum | 66d4513 | 2000-03-28 20:20:53 +0000 | [diff] [blame] | 1496 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1497 | if not Debug: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1498 | print(''' |
Guido van Rossum | 66d4513 | 2000-03-28 20:20:53 +0000 | [diff] [blame] | 1499 | If you would like to see debugging output, |
| 1500 | try: %s -d5 |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1501 | ''' % sys.argv[0]) |
Guido van Rossum | 66d4513 | 2000-03-28 20:20:53 +0000 | [diff] [blame] | 1502 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 1503 | raise |