Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 1 | """A POP3 client class. |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 3 | Based on the J. Myers POP3 draft, Jan. 96 |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 4 | """ |
| 5 | |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 6 | # Author: David Ascher <david_ascher@brown.edu> |
| 7 | # [heavily stealing from nntplib.py] |
| 8 | # Updated: Piers Lauder <piers@cs.su.oz.au> [Jul '97] |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 9 | # String method conversion and test jig improvements by ESR, February 2001. |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 10 | # Added the POP3_SSL class. Methods loosely based on IMAP_SSL. Hector Urtubia <urtubia@mrbook.org> Aug 2003 |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 12 | # Example (see the test function at the end of this file) |
| 13 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 14 | # Imports |
| 15 | |
Antoine Pitrou | 7fde5b3 | 2012-11-24 18:14:12 +0100 | [diff] [blame] | 16 | import errno |
| 17 | import re |
| 18 | import socket |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 19 | import sys |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 20 | |
Antoine Pitrou | 8618d74 | 2012-11-23 20:13:48 +0100 | [diff] [blame] | 21 | try: |
| 22 | import ssl |
| 23 | HAVE_SSL = True |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 24 | except ImportError: |
Antoine Pitrou | 8618d74 | 2012-11-23 20:13:48 +0100 | [diff] [blame] | 25 | HAVE_SSL = False |
| 26 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 27 | __all__ = ["POP3","error_proto"] |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 29 | # Exception raised when an error or invalid response is received: |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 30 | |
| 31 | class error_proto(Exception): pass |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 32 | |
| 33 | # Standard Port |
| 34 | POP3_PORT = 110 |
| 35 | |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 36 | # POP SSL PORT |
| 37 | POP3_SSL_PORT = 995 |
| 38 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 39 | # Line terminators (we always output CRLF, but accept any of CRLF, LFCR, LF) |
Jeremy Hylton | 88d06a7 | 2007-08-29 19:08:30 +0000 | [diff] [blame] | 40 | CR = b'\r' |
| 41 | LF = b'\n' |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 42 | CRLF = CR+LF |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 43 | |
Georg Brandl | 7e27abb | 2013-10-27 07:23:53 +0100 | [diff] [blame] | 44 | # maximal line length when calling readline(). This is to prevent |
Berker Peksag | f23530f | 2014-10-19 18:04:38 +0300 | [diff] [blame] | 45 | # reading arbitrary length lines. RFC 1939 limits POP3 line length to |
Georg Brandl | 7e27abb | 2013-10-27 07:23:53 +0100 | [diff] [blame] | 46 | # 512 characters, including CRLF. We have selected 2048 just to be on |
| 47 | # the safe side. |
| 48 | _MAXLINE = 2048 |
| 49 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 50 | |
| 51 | class POP3: |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 52 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 53 | """This class supports both the minimal and optional command sets. |
| 54 | Arguments can be strings or integers (where appropriate) |
| 55 | (e.g.: retr(1) and retr('1') both work equally well. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 56 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 57 | Minimal Command Set: |
| 58 | USER name user(name) |
| 59 | PASS string pass_(string) |
| 60 | STAT stat() |
| 61 | LIST [msg] list(msg = None) |
| 62 | RETR msg retr(msg) |
| 63 | DELE msg dele(msg) |
| 64 | NOOP noop() |
| 65 | RSET rset() |
| 66 | QUIT quit() |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 67 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 68 | Optional Commands (some servers support these): |
| 69 | RPOP name rpop(name) |
| 70 | APOP name digest apop(name, digest) |
| 71 | TOP msg n top(msg, n) |
| 72 | UIDL [msg] uidl(msg = None) |
Antoine Pitrou | 25cee19 | 2012-11-23 20:07:39 +0100 | [diff] [blame] | 73 | CAPA capa() |
Antoine Pitrou | 8618d74 | 2012-11-23 20:13:48 +0100 | [diff] [blame] | 74 | STLS stls() |
R David Murray | b8cd3e4 | 2015-05-16 15:05:53 -0400 | [diff] [blame] | 75 | UTF8 utf8() |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 76 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 77 | Raises one exception: 'error_proto'. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 78 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 79 | Instantiate with: |
| 80 | POP3(hostname, port=110) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 81 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 82 | NB: the POP protocol locks the mailbox from user |
| 83 | authorization until QUIT, so be sure to get in, suck |
| 84 | the messages, and quit, each time you access the |
| 85 | mailbox. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 86 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 87 | POP is a line-based protocol, which means large mail |
| 88 | messages consume lots of python cycles reading them |
| 89 | line-by-line. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 90 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 91 | If it's available on your mail server, use IMAP4 |
| 92 | instead, it doesn't suffer from the two problems |
| 93 | above. |
| 94 | """ |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 95 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 96 | encoding = 'UTF-8' |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 97 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 98 | def __init__(self, host, port=POP3_PORT, |
| 99 | timeout=socket._GLOBAL_DEFAULT_TIMEOUT): |
Martin v. Löwis | 4eb5940 | 2001-07-26 13:37:33 +0000 | [diff] [blame] | 100 | self.host = host |
| 101 | self.port = port |
Antoine Pitrou | 8618d74 | 2012-11-23 20:13:48 +0100 | [diff] [blame] | 102 | self._tls_established = False |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 103 | sys.audit("poplib.connect", self, host, port) |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 104 | self.sock = self._create_socket(timeout) |
Martin v. Löwis | 4eb5940 | 2001-07-26 13:37:33 +0000 | [diff] [blame] | 105 | self.file = self.sock.makefile('rb') |
| 106 | self._debugging = 0 |
| 107 | self.welcome = self._getresp() |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 108 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 109 | def _create_socket(self, timeout): |
| 110 | return socket.create_connection((self.host, self.port), timeout) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 111 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 112 | def _putline(self, line): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 113 | if self._debugging > 1: print('*put*', repr(line)) |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 114 | sys.audit("poplib.putline", self, line) |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 115 | self.sock.sendall(line + CRLF) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 117 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 118 | # Internal: send one command to the server (through _putline()) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 119 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 120 | def _putcmd(self, line): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 121 | if self._debugging: print('*cmd*', repr(line)) |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 122 | line = bytes(line, self.encoding) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 123 | self._putline(line) |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 125 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 126 | # Internal: return one line from the server, stripping CRLF. |
| 127 | # This is where all the CPU time of this module is consumed. |
| 128 | # Raise error_proto('-ERR EOF') if the connection is closed. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 129 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 130 | def _getline(self): |
Georg Brandl | 7e27abb | 2013-10-27 07:23:53 +0100 | [diff] [blame] | 131 | line = self.file.readline(_MAXLINE + 1) |
| 132 | if len(line) > _MAXLINE: |
| 133 | raise error_proto('line too long') |
| 134 | |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 135 | if self._debugging > 1: print('*get*', repr(line)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 136 | if not line: raise error_proto('-ERR EOF') |
| 137 | octets = len(line) |
| 138 | # server can send any combination of CR & LF |
| 139 | # however, 'readline()' returns lines ending in LF |
| 140 | # so only possibilities are ...LF, ...CRLF, CR...LF |
| 141 | if line[-2:] == CRLF: |
| 142 | return line[:-2], octets |
Serhiy Storchaka | 74a49ac | 2015-03-20 16:46:19 +0200 | [diff] [blame] | 143 | if line[:1] == CR: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 144 | return line[1:-1], octets |
| 145 | return line[:-1], octets |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 147 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 148 | # Internal: get a response from the server. |
| 149 | # Raise 'error_proto' if the response doesn't start with '+'. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 150 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 151 | def _getresp(self): |
| 152 | resp, o = self._getline() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 153 | if self._debugging > 1: print('*resp*', repr(resp)) |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 154 | if not resp.startswith(b'+'): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 155 | raise error_proto(resp) |
| 156 | return resp |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 157 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 158 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 159 | # Internal: get a response plus following text from the server. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 160 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 161 | def _getlongresp(self): |
| 162 | resp = self._getresp() |
| 163 | list = []; octets = 0 |
| 164 | line, o = self._getline() |
Guido van Rossum | 0ec3477 | 2007-09-10 00:27:13 +0000 | [diff] [blame] | 165 | while line != b'.': |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 166 | if line.startswith(b'..'): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 167 | o = o-1 |
| 168 | line = line[1:] |
| 169 | octets = octets + o |
| 170 | list.append(line) |
| 171 | line, o = self._getline() |
| 172 | return resp, list, octets |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 173 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 174 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 175 | # Internal: send a command and get the response |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 176 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 177 | def _shortcmd(self, line): |
| 178 | self._putcmd(line) |
| 179 | return self._getresp() |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 181 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 182 | # Internal: send a command and get the response plus following text |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 183 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 184 | def _longcmd(self, line): |
| 185 | self._putcmd(line) |
| 186 | return self._getlongresp() |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 187 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 188 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 189 | # These can be useful: |
| 190 | |
| 191 | def getwelcome(self): |
| 192 | return self.welcome |
| 193 | |
| 194 | |
| 195 | def set_debuglevel(self, level): |
| 196 | self._debugging = level |
| 197 | |
| 198 | |
| 199 | # Here are all the POP commands: |
| 200 | |
| 201 | def user(self, user): |
| 202 | """Send user name, return response |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 203 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 204 | (should indicate password required). |
| 205 | """ |
| 206 | return self._shortcmd('USER %s' % user) |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 208 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 209 | def pass_(self, pswd): |
| 210 | """Send password, return response |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 211 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 212 | (response includes message count, mailbox size). |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 213 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 214 | NB: mailbox is locked by server from here to 'quit()' |
| 215 | """ |
| 216 | return self._shortcmd('PASS %s' % pswd) |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 217 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 218 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 219 | def stat(self): |
| 220 | """Get mailbox status. |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 221 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 222 | Result is tuple of 2 ints (message count, mailbox size) |
| 223 | """ |
| 224 | retval = self._shortcmd('STAT') |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 225 | rets = retval.split() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 226 | if self._debugging: print('*stat*', repr(rets)) |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 227 | numMessages = int(rets[1]) |
| 228 | sizeMessages = int(rets[2]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 229 | return (numMessages, sizeMessages) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 231 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 232 | def list(self, which=None): |
| 233 | """Request listing, return result. |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 234 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 235 | Result without a message number argument is in form |
Georg Brandl | 2772c67 | 2005-08-05 21:01:58 +0000 | [diff] [blame] | 236 | ['response', ['mesg_num octets', ...], octets]. |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 237 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 238 | Result when a message number argument is given is a |
| 239 | single response: the "scan listing" for that message. |
| 240 | """ |
Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 241 | if which is not None: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 242 | return self._shortcmd('LIST %s' % which) |
| 243 | return self._longcmd('LIST') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 244 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 245 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 246 | def retr(self, which): |
| 247 | """Retrieve whole message number 'which'. |
Guido van Rossum | f6ae743 | 1998-09-02 14:42:02 +0000 | [diff] [blame] | 248 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 249 | Result is in form ['response', ['line', ...], octets]. |
| 250 | """ |
| 251 | return self._longcmd('RETR %s' % which) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 253 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 254 | def dele(self, which): |
| 255 | """Delete message number 'which'. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 256 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 257 | Result is 'response'. |
| 258 | """ |
| 259 | return self._shortcmd('DELE %s' % which) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 261 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 262 | def noop(self): |
| 263 | """Does nothing. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 264 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 265 | One supposes the response indicates the server is alive. |
| 266 | """ |
| 267 | return self._shortcmd('NOOP') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 268 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 269 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 270 | def rset(self): |
Benjamin Peterson | a37cfc6 | 2008-05-26 13:48:34 +0000 | [diff] [blame] | 271 | """Unmark all messages marked for deletion.""" |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 272 | return self._shortcmd('RSET') |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 273 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 274 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 275 | def quit(self): |
| 276 | """Signoff: commit changes on server, unlock mailbox, close connection.""" |
Giampaolo Rodolà | 95bcb93 | 2011-02-25 22:28:24 +0000 | [diff] [blame] | 277 | resp = self._shortcmd('QUIT') |
| 278 | self.close() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 279 | return resp |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 280 | |
Giampaolo Rodolà | 95bcb93 | 2011-02-25 22:28:24 +0000 | [diff] [blame] | 281 | def close(self): |
| 282 | """Close the connection without assuming anything about it.""" |
Serhiy Storchaka | 7e7a3db | 2015-04-10 13:24:41 +0300 | [diff] [blame] | 283 | try: |
| 284 | file = self.file |
| 285 | self.file = None |
| 286 | if file is not None: |
| 287 | file.close() |
| 288 | finally: |
| 289 | sock = self.sock |
| 290 | self.sock = None |
| 291 | if sock is not None: |
| 292 | try: |
| 293 | sock.shutdown(socket.SHUT_RDWR) |
Victor Stinner | 83a2c28 | 2017-05-15 17:33:45 +0200 | [diff] [blame] | 294 | except OSError as exc: |
| 295 | # The server might already have closed the connection. |
| 296 | # On Windows, this may result in WSAEINVAL (error 10022): |
| 297 | # An invalid operation was attempted. |
| 298 | if (exc.errno != errno.ENOTCONN |
| 299 | and getattr(exc, 'winerror', 0) != 10022): |
Serhiy Storchaka | 7e7a3db | 2015-04-10 13:24:41 +0300 | [diff] [blame] | 300 | raise |
| 301 | finally: |
| 302 | sock.close() |
Giampaolo Rodolà | 95bcb93 | 2011-02-25 22:28:24 +0000 | [diff] [blame] | 303 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 304 | #__del__ = quit |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 305 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 306 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 307 | # optional commands: |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 308 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 309 | def rpop(self, user): |
| 310 | """Not sure what this does.""" |
| 311 | return self._shortcmd('RPOP %s' % user) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 312 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 313 | |
Jamie Davis | 0e6c8ee | 2018-03-04 00:33:32 -0500 | [diff] [blame] | 314 | timestamp = re.compile(br'\+OK.[^<]*(<.*>)') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 315 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 316 | def apop(self, user, password): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 317 | """Authorisation |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 318 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 319 | - only possible if server has supplied a timestamp in initial greeting. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 320 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 321 | Args: |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 322 | user - mailbox user; |
| 323 | password - mailbox password. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 324 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 325 | NB: mailbox is locked by server from here to 'quit()' |
| 326 | """ |
Mark Dickinson | ea1158f | 2009-08-06 16:06:25 +0000 | [diff] [blame] | 327 | secret = bytes(password, self.encoding) |
Moshe Zadka | ccc2e3d | 2001-01-19 19:56:27 +0000 | [diff] [blame] | 328 | m = self.timestamp.match(self.welcome) |
| 329 | if not m: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 330 | raise error_proto('-ERR APOP not supported by server') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 331 | import hashlib |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 332 | digest = m.group(1)+secret |
| 333 | digest = hashlib.md5(digest).hexdigest() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 334 | return self._shortcmd('APOP %s %s' % (user, digest)) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 335 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 336 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 337 | def top(self, which, howmuch): |
| 338 | """Retrieve message header of message number 'which' |
| 339 | and first 'howmuch' lines of message body. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 340 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 341 | Result is in form ['response', ['line', ...], octets]. |
| 342 | """ |
| 343 | return self._longcmd('TOP %s %s' % (which, howmuch)) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 344 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 345 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 346 | def uidl(self, which=None): |
| 347 | """Return message digest (unique id) list. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 348 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 349 | If 'which', result contains unique id for that message |
| 350 | in the form 'response mesgnum uid', otherwise result is |
| 351 | the list ['response', ['mesgnum uid', ...], octets] |
| 352 | """ |
Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 353 | if which is not None: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 354 | return self._shortcmd('UIDL %s' % which) |
| 355 | return self._longcmd('UIDL') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 356 | |
Antoine Pitrou | 25cee19 | 2012-11-23 20:07:39 +0100 | [diff] [blame] | 357 | |
R David Murray | b8cd3e4 | 2015-05-16 15:05:53 -0400 | [diff] [blame] | 358 | def utf8(self): |
| 359 | """Try to enter UTF-8 mode (see RFC 6856). Returns server response. |
| 360 | """ |
| 361 | return self._shortcmd('UTF8') |
| 362 | |
| 363 | |
Antoine Pitrou | 25cee19 | 2012-11-23 20:07:39 +0100 | [diff] [blame] | 364 | def capa(self): |
| 365 | """Return server capabilities (RFC 2449) as a dictionary |
| 366 | >>> c=poplib.POP3('localhost') |
| 367 | >>> c.capa() |
| 368 | {'IMPLEMENTATION': ['Cyrus', 'POP3', 'server', 'v2.2.12'], |
| 369 | 'TOP': [], 'LOGIN-DELAY': ['0'], 'AUTH-RESP-CODE': [], |
| 370 | 'EXPIRE': ['NEVER'], 'USER': [], 'STLS': [], 'PIPELINING': [], |
| 371 | 'UIDL': [], 'RESP-CODES': []} |
| 372 | >>> |
| 373 | |
| 374 | Really, according to RFC 2449, the cyrus folks should avoid |
Ezio Melotti | 9a3777e | 2013-08-17 15:53:55 +0300 | [diff] [blame] | 375 | having the implementation split into multiple arguments... |
Antoine Pitrou | 25cee19 | 2012-11-23 20:07:39 +0100 | [diff] [blame] | 376 | """ |
| 377 | def _parsecap(line): |
| 378 | lst = line.decode('ascii').split() |
| 379 | return lst[0], lst[1:] |
| 380 | |
| 381 | caps = {} |
| 382 | try: |
| 383 | resp = self._longcmd('CAPA') |
| 384 | rawcaps = resp[1] |
| 385 | for capline in rawcaps: |
| 386 | capnm, capargs = _parsecap(capline) |
| 387 | caps[capnm] = capargs |
| 388 | except error_proto as _err: |
| 389 | raise error_proto('-ERR CAPA not supported by server') |
| 390 | return caps |
| 391 | |
Antoine Pitrou | 8618d74 | 2012-11-23 20:13:48 +0100 | [diff] [blame] | 392 | |
| 393 | def stls(self, context=None): |
| 394 | """Start a TLS session on the active connection as specified in RFC 2595. |
| 395 | |
| 396 | context - a ssl.SSLContext |
| 397 | """ |
| 398 | if not HAVE_SSL: |
| 399 | raise error_proto('-ERR TLS support missing') |
| 400 | if self._tls_established: |
| 401 | raise error_proto('-ERR TLS session already established') |
| 402 | caps = self.capa() |
| 403 | if not 'STLS' in caps: |
| 404 | raise error_proto('-ERR STLS not supported by server') |
| 405 | if context is None: |
Christian Heimes | 67986f9 | 2013-11-23 22:43:47 +0100 | [diff] [blame] | 406 | context = ssl._create_stdlib_context() |
Antoine Pitrou | 8618d74 | 2012-11-23 20:13:48 +0100 | [diff] [blame] | 407 | resp = self._shortcmd('STLS') |
Christian Heimes | 1bc7068 | 2013-12-02 20:10:50 +0100 | [diff] [blame] | 408 | self.sock = context.wrap_socket(self.sock, |
Benjamin Peterson | 7243b57 | 2014-11-23 17:04:34 -0600 | [diff] [blame] | 409 | server_hostname=self.host) |
Antoine Pitrou | 8618d74 | 2012-11-23 20:13:48 +0100 | [diff] [blame] | 410 | self.file = self.sock.makefile('rb') |
| 411 | self._tls_established = True |
| 412 | return resp |
| 413 | |
| 414 | |
| 415 | if HAVE_SSL: |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 416 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 417 | class POP3_SSL(POP3): |
| 418 | """POP3 client class over SSL connection |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 419 | |
Antoine Pitrou | 8618d74 | 2012-11-23 20:13:48 +0100 | [diff] [blame] | 420 | Instantiate with: POP3_SSL(hostname, port=995, keyfile=None, certfile=None, |
| 421 | context=None) |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 422 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 423 | hostname - the hostname of the pop3 over ssl server |
| 424 | port - port number |
Ezio Melotti | 30b9d5d | 2013-08-17 15:50:46 +0300 | [diff] [blame] | 425 | keyfile - PEM formatted file that contains your private key |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 426 | certfile - PEM formatted certificate chain file |
Antoine Pitrou | 8618d74 | 2012-11-23 20:13:48 +0100 | [diff] [blame] | 427 | context - a ssl.SSLContext |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 428 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 429 | See the methods of the parent class POP3 for more documentation. |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 430 | """ |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 431 | |
Giampaolo Rodolà | 42382fe | 2010-08-17 16:09:53 +0000 | [diff] [blame] | 432 | def __init__(self, host, port=POP3_SSL_PORT, keyfile=None, certfile=None, |
| 433 | timeout=socket._GLOBAL_DEFAULT_TIMEOUT, context=None): |
| 434 | if context is not None and keyfile is not None: |
| 435 | raise ValueError("context and keyfile arguments are mutually " |
| 436 | "exclusive") |
| 437 | if context is not None and certfile is not None: |
| 438 | raise ValueError("context and certfile arguments are mutually " |
| 439 | "exclusive") |
Christian Heimes | d048637 | 2016-09-10 23:23:33 +0200 | [diff] [blame] | 440 | if keyfile is not None or certfile is not None: |
| 441 | import warnings |
Pablo Aguiar | 4b5e62d | 2018-11-01 11:33:35 +0100 | [diff] [blame] | 442 | warnings.warn("keyfile and certfile are deprecated, use a " |
Christian Heimes | d048637 | 2016-09-10 23:23:33 +0200 | [diff] [blame] | 443 | "custom context instead", DeprecationWarning, 2) |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 444 | self.keyfile = keyfile |
| 445 | self.certfile = certfile |
Christian Heimes | 67986f9 | 2013-11-23 22:43:47 +0100 | [diff] [blame] | 446 | if context is None: |
| 447 | context = ssl._create_stdlib_context(certfile=certfile, |
| 448 | keyfile=keyfile) |
Giampaolo Rodolà | 42382fe | 2010-08-17 16:09:53 +0000 | [diff] [blame] | 449 | self.context = context |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 450 | POP3.__init__(self, host, port, timeout) |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 451 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 452 | def _create_socket(self, timeout): |
| 453 | sock = POP3._create_socket(self, timeout) |
Christian Heimes | 1bc7068 | 2013-12-02 20:10:50 +0100 | [diff] [blame] | 454 | sock = self.context.wrap_socket(sock, |
Benjamin Peterson | 7243b57 | 2014-11-23 17:04:34 -0600 | [diff] [blame] | 455 | server_hostname=self.host) |
Giampaolo Rodolà | 42382fe | 2010-08-17 16:09:53 +0000 | [diff] [blame] | 456 | return sock |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 457 | |
Antoine Pitrou | 8618d74 | 2012-11-23 20:13:48 +0100 | [diff] [blame] | 458 | def stls(self, keyfile=None, certfile=None, context=None): |
| 459 | """The method unconditionally raises an exception since the |
| 460 | STLS command doesn't make any sense on an already established |
| 461 | SSL/TLS session. |
| 462 | """ |
| 463 | raise error_proto('-ERR TLS session already established') |
| 464 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 465 | __all__.append("POP3_SSL") |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 466 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 467 | if __name__ == "__main__": |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 468 | import sys |
| 469 | a = POP3(sys.argv[1]) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 470 | print(a.getwelcome()) |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 471 | a.user(sys.argv[2]) |
| 472 | a.pass_(sys.argv[3]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 473 | a.list() |
| 474 | (numMsgs, totalSize) = a.stat() |
| 475 | for i in range(1, numMsgs + 1): |
| 476 | (header, msg, octets) = a.retr(i) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 477 | print("Message %d:" % i) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 478 | for line in msg: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 479 | print(' ' + line) |
| 480 | print('-----------------------') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 481 | a.quit() |