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