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