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