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