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