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