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 | |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 16 | import re, socket |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 17 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 18 | __all__ = ["POP3","error_proto"] |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 20 | # Exception raised when an error or invalid response is received: |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 21 | |
| 22 | class error_proto(Exception): pass |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 23 | |
| 24 | # Standard Port |
| 25 | POP3_PORT = 110 |
| 26 | |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 27 | # POP SSL PORT |
| 28 | POP3_SSL_PORT = 995 |
| 29 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 30 | # 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] | 31 | CR = b'\r' |
| 32 | LF = b'\n' |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 33 | CRLF = CR+LF |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 34 | |
| 35 | |
| 36 | class POP3: |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 37 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 38 | """This class supports both the minimal and optional command sets. |
| 39 | Arguments can be strings or integers (where appropriate) |
| 40 | (e.g.: retr(1) and retr('1') both work equally well. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 41 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 42 | Minimal Command Set: |
| 43 | USER name user(name) |
| 44 | PASS string pass_(string) |
| 45 | STAT stat() |
| 46 | LIST [msg] list(msg = None) |
| 47 | RETR msg retr(msg) |
| 48 | DELE msg dele(msg) |
| 49 | NOOP noop() |
| 50 | RSET rset() |
| 51 | QUIT quit() |
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 | Optional Commands (some servers support these): |
| 54 | RPOP name rpop(name) |
| 55 | APOP name digest apop(name, digest) |
| 56 | TOP msg n top(msg, n) |
| 57 | UIDL [msg] uidl(msg = None) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 58 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 59 | Raises one exception: 'error_proto'. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 60 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 61 | Instantiate with: |
| 62 | POP3(hostname, port=110) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 63 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 64 | NB: the POP protocol locks the mailbox from user |
| 65 | authorization until QUIT, so be sure to get in, suck |
| 66 | the messages, and quit, each time you access the |
| 67 | mailbox. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 68 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 69 | POP is a line-based protocol, which means large mail |
| 70 | messages consume lots of python cycles reading them |
| 71 | line-by-line. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 72 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 73 | If it's available on your mail server, use IMAP4 |
| 74 | instead, it doesn't suffer from the two problems |
| 75 | above. |
| 76 | """ |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 77 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 78 | encoding = 'UTF-8' |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 79 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 80 | def __init__(self, host, port=POP3_PORT, |
| 81 | timeout=socket._GLOBAL_DEFAULT_TIMEOUT): |
Martin v. Löwis | 4eb5940 | 2001-07-26 13:37:33 +0000 | [diff] [blame] | 82 | self.host = host |
| 83 | self.port = port |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 84 | self.sock = self._create_socket(timeout) |
Martin v. Löwis | 4eb5940 | 2001-07-26 13:37:33 +0000 | [diff] [blame] | 85 | self.file = self.sock.makefile('rb') |
| 86 | self._debugging = 0 |
| 87 | self.welcome = self._getresp() |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 88 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 89 | def _create_socket(self, timeout): |
| 90 | return socket.create_connection((self.host, self.port), timeout) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 91 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 92 | def _putline(self, line): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 93 | if self._debugging > 1: print('*put*', repr(line)) |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 94 | self.sock.sendall(line + CRLF) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 96 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 97 | # Internal: send one command to the server (through _putline()) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 98 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 99 | def _putcmd(self, line): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 100 | if self._debugging: print('*cmd*', repr(line)) |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 101 | line = bytes(line, self.encoding) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 102 | self._putline(line) |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 103 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 104 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 105 | # Internal: return one line from the server, stripping CRLF. |
| 106 | # This is where all the CPU time of this module is consumed. |
| 107 | # Raise error_proto('-ERR EOF') if the connection is closed. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 108 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 109 | def _getline(self): |
| 110 | line = self.file.readline() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 111 | if self._debugging > 1: print('*get*', repr(line)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 112 | if not line: raise error_proto('-ERR EOF') |
| 113 | octets = len(line) |
| 114 | # server can send any combination of CR & LF |
| 115 | # however, 'readline()' returns lines ending in LF |
| 116 | # so only possibilities are ...LF, ...CRLF, CR...LF |
| 117 | if line[-2:] == CRLF: |
| 118 | return line[:-2], octets |
| 119 | if line[0] == CR: |
| 120 | return line[1:-1], octets |
| 121 | return line[:-1], octets |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 122 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 123 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 124 | # Internal: get a response from the server. |
| 125 | # Raise 'error_proto' if the response doesn't start with '+'. |
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 _getresp(self): |
| 128 | resp, o = self._getline() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 129 | if self._debugging > 1: print('*resp*', repr(resp)) |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 130 | if not resp.startswith(b'+'): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 131 | raise error_proto(resp) |
| 132 | return resp |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 134 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 135 | # Internal: get a response plus following text from the server. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 136 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 137 | def _getlongresp(self): |
| 138 | resp = self._getresp() |
| 139 | list = []; octets = 0 |
| 140 | line, o = self._getline() |
Guido van Rossum | 0ec3477 | 2007-09-10 00:27:13 +0000 | [diff] [blame] | 141 | while line != b'.': |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 142 | if line.startswith(b'..'): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 143 | o = o-1 |
| 144 | line = line[1:] |
| 145 | octets = octets + o |
| 146 | list.append(line) |
| 147 | line, o = self._getline() |
| 148 | return resp, list, octets |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 149 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 150 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 151 | # Internal: send a command and get the response |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 152 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 153 | def _shortcmd(self, line): |
| 154 | self._putcmd(line) |
| 155 | return self._getresp() |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 156 | |
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 | # Internal: send a command and get the response plus following text |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 159 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 160 | def _longcmd(self, line): |
| 161 | self._putcmd(line) |
| 162 | return self._getlongresp() |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 164 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 165 | # These can be useful: |
| 166 | |
| 167 | def getwelcome(self): |
| 168 | return self.welcome |
| 169 | |
| 170 | |
| 171 | def set_debuglevel(self, level): |
| 172 | self._debugging = level |
| 173 | |
| 174 | |
| 175 | # Here are all the POP commands: |
| 176 | |
| 177 | def user(self, user): |
| 178 | """Send user name, return response |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 179 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 180 | (should indicate password required). |
| 181 | """ |
| 182 | return self._shortcmd('USER %s' % user) |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 183 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 184 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 185 | def pass_(self, pswd): |
| 186 | """Send password, return response |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 187 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 188 | (response includes message count, mailbox size). |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 189 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 190 | NB: mailbox is locked by server from here to 'quit()' |
| 191 | """ |
| 192 | return self._shortcmd('PASS %s' % pswd) |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 194 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 195 | def stat(self): |
| 196 | """Get mailbox status. |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 197 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 198 | Result is tuple of 2 ints (message count, mailbox size) |
| 199 | """ |
| 200 | retval = self._shortcmd('STAT') |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 201 | rets = retval.split() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 202 | if self._debugging: print('*stat*', repr(rets)) |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 203 | numMessages = int(rets[1]) |
| 204 | sizeMessages = int(rets[2]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 205 | return (numMessages, sizeMessages) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 206 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 207 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 208 | def list(self, which=None): |
| 209 | """Request listing, return result. |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 210 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 211 | Result without a message number argument is in form |
Georg Brandl | 2772c67 | 2005-08-05 21:01:58 +0000 | [diff] [blame] | 212 | ['response', ['mesg_num octets', ...], octets]. |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 213 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 214 | Result when a message number argument is given is a |
| 215 | single response: the "scan listing" for that message. |
| 216 | """ |
Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 217 | if which is not None: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 218 | return self._shortcmd('LIST %s' % which) |
| 219 | return self._longcmd('LIST') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 220 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 221 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 222 | def retr(self, which): |
| 223 | """Retrieve whole message number 'which'. |
Guido van Rossum | f6ae743 | 1998-09-02 14:42:02 +0000 | [diff] [blame] | 224 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 225 | Result is in form ['response', ['line', ...], octets]. |
| 226 | """ |
| 227 | return self._longcmd('RETR %s' % which) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 228 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 229 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 230 | def dele(self, which): |
| 231 | """Delete message number 'which'. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 232 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 233 | Result is 'response'. |
| 234 | """ |
| 235 | return self._shortcmd('DELE %s' % which) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 236 | |
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 | def noop(self): |
| 239 | """Does nothing. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 240 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 241 | One supposes the response indicates the server is alive. |
| 242 | """ |
| 243 | return self._shortcmd('NOOP') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 244 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 245 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 246 | def rset(self): |
Benjamin Peterson | a37cfc6 | 2008-05-26 13:48:34 +0000 | [diff] [blame] | 247 | """Unmark all messages marked for deletion.""" |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 248 | return self._shortcmd('RSET') |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 250 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 251 | def quit(self): |
| 252 | """Signoff: commit changes on server, unlock mailbox, close connection.""" |
| 253 | try: |
| 254 | resp = self._shortcmd('QUIT') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 255 | except error_proto as val: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 256 | resp = val |
| 257 | self.file.close() |
| 258 | self.sock.close() |
| 259 | del self.file, self.sock |
| 260 | return resp |
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 | #__del__ = quit |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 264 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 265 | # optional commands: |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 266 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 267 | def rpop(self, user): |
| 268 | """Not sure what this does.""" |
| 269 | return self._shortcmd('RPOP %s' % user) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 271 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 272 | timestamp = re.compile(br'\+OK.*(<[^>]+>)') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 273 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 274 | def apop(self, user, password): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 275 | """Authorisation |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 276 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 277 | - only possible if server has supplied a timestamp in initial greeting. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 278 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 279 | Args: |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 280 | user - mailbox user; |
| 281 | password - mailbox password. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 282 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 283 | NB: mailbox is locked by server from here to 'quit()' |
| 284 | """ |
Mark Dickinson | ea1158f | 2009-08-06 16:06:25 +0000 | [diff] [blame] | 285 | secret = bytes(password, self.encoding) |
Moshe Zadka | ccc2e3d | 2001-01-19 19:56:27 +0000 | [diff] [blame] | 286 | m = self.timestamp.match(self.welcome) |
| 287 | if not m: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 288 | raise error_proto('-ERR APOP not supported by server') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 289 | import hashlib |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 290 | digest = m.group(1)+secret |
| 291 | digest = hashlib.md5(digest).hexdigest() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 292 | return self._shortcmd('APOP %s %s' % (user, digest)) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 293 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 294 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 295 | def top(self, which, howmuch): |
| 296 | """Retrieve message header of message number 'which' |
| 297 | and first 'howmuch' lines of message body. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 298 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 299 | Result is in form ['response', ['line', ...], octets]. |
| 300 | """ |
| 301 | return self._longcmd('TOP %s %s' % (which, howmuch)) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 302 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 303 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 304 | def uidl(self, which=None): |
| 305 | """Return message digest (unique id) list. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 306 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 307 | If 'which', result contains unique id for that message |
| 308 | in the form 'response mesgnum uid', otherwise result is |
| 309 | the list ['response', ['mesgnum uid', ...], octets] |
| 310 | """ |
Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 311 | if which is not None: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 312 | return self._shortcmd('UIDL %s' % which) |
| 313 | return self._longcmd('UIDL') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 314 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 315 | try: |
| 316 | import ssl |
| 317 | except ImportError: |
| 318 | pass |
| 319 | else: |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 320 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 321 | class POP3_SSL(POP3): |
| 322 | """POP3 client class over SSL connection |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 323 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 324 | Instantiate with: POP3_SSL(hostname, port=995, keyfile=None, certfile=None) |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 325 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 326 | hostname - the hostname of the pop3 over ssl server |
| 327 | port - port number |
| 328 | keyfile - PEM formatted file that countains your private key |
| 329 | certfile - PEM formatted certificate chain file |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 330 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 331 | See the methods of the parent class POP3 for more documentation. |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 332 | """ |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 333 | |
Giampaolo Rodolà | 42382fe | 2010-08-17 16:09:53 +0000 | [diff] [blame] | 334 | def __init__(self, host, port=POP3_SSL_PORT, keyfile=None, certfile=None, |
| 335 | timeout=socket._GLOBAL_DEFAULT_TIMEOUT, context=None): |
| 336 | if context is not None and keyfile is not None: |
| 337 | raise ValueError("context and keyfile arguments are mutually " |
| 338 | "exclusive") |
| 339 | if context is not None and certfile is not None: |
| 340 | raise ValueError("context and certfile arguments are mutually " |
| 341 | "exclusive") |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 342 | self.keyfile = keyfile |
| 343 | self.certfile = certfile |
Giampaolo Rodolà | 42382fe | 2010-08-17 16:09:53 +0000 | [diff] [blame] | 344 | self.context = context |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 345 | POP3.__init__(self, host, port, timeout) |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 346 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 347 | def _create_socket(self, timeout): |
| 348 | sock = POP3._create_socket(self, timeout) |
Giampaolo Rodolà | 42382fe | 2010-08-17 16:09:53 +0000 | [diff] [blame] | 349 | if self.context is not None: |
| 350 | sock = self.context.wrap_socket(sock) |
| 351 | else: |
| 352 | sock = ssl.wrap_socket(sock, self.keyfile, self.certfile) |
| 353 | return sock |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 354 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 355 | __all__.append("POP3_SSL") |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 356 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 357 | if __name__ == "__main__": |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 358 | import sys |
| 359 | a = POP3(sys.argv[1]) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 360 | print(a.getwelcome()) |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 361 | a.user(sys.argv[2]) |
| 362 | a.pass_(sys.argv[3]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 363 | a.list() |
| 364 | (numMsgs, totalSize) = a.stat() |
| 365 | for i in range(1, numMsgs + 1): |
| 366 | (header, msg, octets) = a.retr(i) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 367 | print("Message %d:" % i) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 368 | for line in msg: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 369 | print(' ' + line) |
| 370 | print('-----------------------') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 371 | a.quit() |