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