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