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 | |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 18 | __all__ = ["POP3","error_proto","POP3_SSL"] |
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) |
| 31 | CR = '\r' |
| 32 | LF = '\n' |
| 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 | |
| 78 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 79 | def __init__(self, host, port = POP3_PORT): |
Martin v. Löwis | 4eb5940 | 2001-07-26 13:37:33 +0000 | [diff] [blame] | 80 | self.host = host |
| 81 | self.port = port |
Martin v. Löwis | 2ad2569 | 2001-07-31 08:40:21 +0000 | [diff] [blame] | 82 | msg = "getaddrinfo returns an empty list" |
Martin v. Löwis | 322c0d1 | 2001-10-07 08:53:32 +0000 | [diff] [blame] | 83 | self.sock = None |
Martin v. Löwis | 4eb5940 | 2001-07-26 13:37:33 +0000 | [diff] [blame] | 84 | for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): |
| 85 | af, socktype, proto, canonname, sa = res |
| 86 | try: |
| 87 | self.sock = socket.socket(af, socktype, proto) |
| 88 | self.sock.connect(sa) |
| 89 | except socket.error, msg: |
Martin v. Löwis | 322c0d1 | 2001-10-07 08:53:32 +0000 | [diff] [blame] | 90 | if self.sock: |
| 91 | self.sock.close() |
Martin v. Löwis | 4eb5940 | 2001-07-26 13:37:33 +0000 | [diff] [blame] | 92 | self.sock = None |
| 93 | continue |
| 94 | break |
| 95 | if not self.sock: |
| 96 | raise socket.error, msg |
| 97 | self.file = self.sock.makefile('rb') |
| 98 | self._debugging = 0 |
| 99 | self.welcome = self._getresp() |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 100 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 101 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 102 | def _putline(self, line): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 103 | if self._debugging > 1: print '*put*', repr(line) |
Martin v. Löwis | e12454f | 2002-02-16 23:06:19 +0000 | [diff] [blame] | 104 | self.sock.sendall('%s%s' % (line, CRLF)) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 105 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 106 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 107 | # Internal: send one command to the server (through _putline()) |
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 _putcmd(self, line): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 110 | if self._debugging: print '*cmd*', repr(line) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 111 | self._putline(line) |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 113 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 114 | # Internal: return one line from the server, stripping CRLF. |
| 115 | # This is where all the CPU time of this module is consumed. |
| 116 | # Raise error_proto('-ERR EOF') if the connection is closed. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 117 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 118 | def _getline(self): |
| 119 | line = self.file.readline() |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 120 | if self._debugging > 1: print '*get*', repr(line) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 121 | if not line: raise error_proto('-ERR EOF') |
| 122 | octets = len(line) |
| 123 | # server can send any combination of CR & LF |
| 124 | # however, 'readline()' returns lines ending in LF |
| 125 | # so only possibilities are ...LF, ...CRLF, CR...LF |
| 126 | if line[-2:] == CRLF: |
| 127 | return line[:-2], octets |
| 128 | if line[0] == CR: |
| 129 | return line[1:-1], octets |
| 130 | return line[:-1], octets |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 132 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 133 | # Internal: get a response from the server. |
| 134 | # Raise 'error_proto' if the response doesn't start with '+'. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 135 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 136 | def _getresp(self): |
| 137 | resp, o = self._getline() |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 138 | if self._debugging > 1: print '*resp*', repr(resp) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 139 | c = resp[:1] |
| 140 | if c != '+': |
| 141 | raise error_proto(resp) |
| 142 | return resp |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 143 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 144 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 145 | # Internal: get a response plus following text from the server. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 146 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 147 | def _getlongresp(self): |
| 148 | resp = self._getresp() |
| 149 | list = []; octets = 0 |
| 150 | line, o = self._getline() |
| 151 | while line != '.': |
| 152 | if line[:2] == '..': |
| 153 | o = o-1 |
| 154 | line = line[1:] |
| 155 | octets = octets + o |
| 156 | list.append(line) |
| 157 | line, o = self._getline() |
| 158 | return resp, list, octets |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 160 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 161 | # Internal: send a command and get the response |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 162 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 163 | def _shortcmd(self, line): |
| 164 | self._putcmd(line) |
| 165 | return self._getresp() |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 166 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 167 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 168 | # Internal: send a command and get the response plus following text |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 169 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 170 | def _longcmd(self, line): |
| 171 | self._putcmd(line) |
| 172 | return self._getlongresp() |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 173 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 174 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 175 | # These can be useful: |
| 176 | |
| 177 | def getwelcome(self): |
| 178 | return self.welcome |
| 179 | |
| 180 | |
| 181 | def set_debuglevel(self, level): |
| 182 | self._debugging = level |
| 183 | |
| 184 | |
| 185 | # Here are all the POP commands: |
| 186 | |
| 187 | def user(self, user): |
| 188 | """Send user name, return response |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 189 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 190 | (should indicate password required). |
| 191 | """ |
| 192 | return self._shortcmd('USER %s' % user) |
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 pass_(self, pswd): |
| 196 | """Send password, return response |
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 | (response includes message count, mailbox size). |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 199 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 200 | NB: mailbox is locked by server from here to 'quit()' |
| 201 | """ |
| 202 | return self._shortcmd('PASS %s' % pswd) |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 204 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 205 | def stat(self): |
| 206 | """Get mailbox status. |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 207 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 208 | Result is tuple of 2 ints (message count, mailbox size) |
| 209 | """ |
| 210 | retval = self._shortcmd('STAT') |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 211 | rets = retval.split() |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 212 | if self._debugging: print '*stat*', repr(rets) |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 213 | numMessages = int(rets[1]) |
| 214 | sizeMessages = int(rets[2]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 215 | return (numMessages, sizeMessages) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 216 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 217 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 218 | def list(self, which=None): |
| 219 | """Request listing, return result. |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 220 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 221 | Result without a message number argument is in form |
Georg Brandl | 2772c67 | 2005-08-05 21:01:58 +0000 | [diff] [blame] | 222 | ['response', ['mesg_num octets', ...], octets]. |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 223 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 224 | Result when a message number argument is given is a |
| 225 | single response: the "scan listing" for that message. |
| 226 | """ |
Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 227 | if which is not None: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 228 | return self._shortcmd('LIST %s' % which) |
| 229 | return self._longcmd('LIST') |
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 retr(self, which): |
| 233 | """Retrieve whole message number 'which'. |
Guido van Rossum | f6ae743 | 1998-09-02 14:42:02 +0000 | [diff] [blame] | 234 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 235 | Result is in form ['response', ['line', ...], octets]. |
| 236 | """ |
| 237 | return self._longcmd('RETR %s' % which) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 239 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 240 | def dele(self, which): |
| 241 | """Delete message number 'which'. |
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 | Result is 'response'. |
| 244 | """ |
| 245 | return self._shortcmd('DELE %s' % which) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 246 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 247 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 248 | def noop(self): |
| 249 | """Does nothing. |
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 | One supposes the response indicates the server is alive. |
| 252 | """ |
| 253 | return self._shortcmd('NOOP') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 255 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 256 | def rset(self): |
| 257 | """Not sure what this does.""" |
| 258 | return self._shortcmd('RSET') |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 259 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 260 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 261 | def quit(self): |
| 262 | """Signoff: commit changes on server, unlock mailbox, close connection.""" |
| 263 | try: |
| 264 | resp = self._shortcmd('QUIT') |
| 265 | except error_proto, val: |
| 266 | resp = val |
| 267 | self.file.close() |
| 268 | self.sock.close() |
| 269 | del self.file, self.sock |
| 270 | return resp |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 271 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 272 | #__del__ = quit |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 273 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 274 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 275 | # optional commands: |
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 | def rpop(self, user): |
| 278 | """Not sure what this does.""" |
| 279 | return self._shortcmd('RPOP %s' % user) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 280 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 281 | |
Moshe Zadka | ccc2e3d | 2001-01-19 19:56:27 +0000 | [diff] [blame] | 282 | timestamp = re.compile(r'\+OK.*(<[^>]+>)') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 283 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 284 | def apop(self, user, secret): |
| 285 | """Authorisation |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 286 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 287 | - only possible if server has supplied a timestamp in initial greeting. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 288 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 289 | Args: |
| 290 | user - mailbox user; |
| 291 | secret - secret shared between client and server. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 292 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 293 | NB: mailbox is locked by server from here to 'quit()' |
| 294 | """ |
Moshe Zadka | ccc2e3d | 2001-01-19 19:56:27 +0000 | [diff] [blame] | 295 | m = self.timestamp.match(self.welcome) |
| 296 | if not m: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 297 | raise error_proto('-ERR APOP not supported by server') |
| 298 | import md5 |
Moshe Zadka | ccc2e3d | 2001-01-19 19:56:27 +0000 | [diff] [blame] | 299 | digest = md5.new(m.group(1)+secret).digest() |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 300 | digest = ''.join(map(lambda x:'%02x'%ord(x), digest)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 301 | return self._shortcmd('APOP %s %s' % (user, digest)) |
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 top(self, which, howmuch): |
| 305 | """Retrieve message header of message number 'which' |
| 306 | and first 'howmuch' lines of message body. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 307 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 308 | Result is in form ['response', ['line', ...], octets]. |
| 309 | """ |
| 310 | return self._longcmd('TOP %s %s' % (which, howmuch)) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 311 | |
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 | def uidl(self, which=None): |
| 314 | """Return message digest (unique id) list. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 315 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 316 | If 'which', result contains unique id for that message |
| 317 | in the form 'response mesgnum uid', otherwise result is |
| 318 | the list ['response', ['mesgnum uid', ...], octets] |
| 319 | """ |
Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 320 | if which is not None: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 321 | return self._shortcmd('UIDL %s' % which) |
| 322 | return self._longcmd('UIDL') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 323 | |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 324 | class POP3_SSL(POP3): |
| 325 | """POP3 client class over SSL connection |
| 326 | |
| 327 | Instantiate with: POP3_SSL(hostname, port=995, keyfile=None, certfile=None) |
| 328 | |
| 329 | hostname - the hostname of the pop3 over ssl server |
| 330 | port - port number |
| 331 | keyfile - PEM formatted file that countains your private key |
| 332 | certfile - PEM formatted certificate chain file |
| 333 | |
| 334 | See the methods of the parent class POP3 for more documentation. |
| 335 | """ |
| 336 | |
| 337 | def __init__(self, host, port = POP3_SSL_PORT, keyfile = None, certfile = None): |
| 338 | self.host = host |
| 339 | self.port = port |
| 340 | self.keyfile = keyfile |
| 341 | self.certfile = certfile |
| 342 | self.buffer = "" |
| 343 | msg = "getaddrinfo returns an empty list" |
| 344 | self.sock = None |
| 345 | for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): |
| 346 | af, socktype, proto, canonname, sa = res |
| 347 | try: |
| 348 | self.sock = socket.socket(af, socktype, proto) |
| 349 | self.sock.connect(sa) |
| 350 | except socket.error, msg: |
| 351 | if self.sock: |
| 352 | self.sock.close() |
| 353 | self.sock = None |
| 354 | continue |
| 355 | break |
| 356 | if not self.sock: |
| 357 | raise socket.error, msg |
| 358 | self.file = self.sock.makefile('rb') |
| 359 | self.sslobj = socket.ssl(self.sock, self.keyfile, self.certfile) |
| 360 | self._debugging = 0 |
| 361 | self.welcome = self._getresp() |
| 362 | |
| 363 | def _fillBuffer(self): |
| 364 | localbuf = self.sslobj.read() |
| 365 | if len(localbuf) == 0: |
| 366 | raise error_proto('-ERR EOF') |
| 367 | self.buffer += localbuf |
| 368 | |
| 369 | def _getline(self): |
| 370 | line = "" |
| 371 | renewline = re.compile(r'.*?\n') |
| 372 | match = renewline.match(self.buffer) |
| 373 | while not match: |
| 374 | self._fillBuffer() |
| 375 | match = renewline.match(self.buffer) |
| 376 | line = match.group(0) |
| 377 | self.buffer = renewline.sub('' ,self.buffer, 1) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 378 | if self._debugging > 1: print '*get*', repr(line) |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 379 | |
| 380 | octets = len(line) |
| 381 | if line[-2:] == CRLF: |
| 382 | return line[:-2], octets |
| 383 | if line[0] == CR: |
| 384 | return line[1:-1], octets |
| 385 | return line[:-1], octets |
| 386 | |
| 387 | def _putline(self, line): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 388 | if self._debugging > 1: print '*put*', repr(line) |
Martin v. Löwis | 48440b7 | 2003-10-31 12:52:35 +0000 | [diff] [blame] | 389 | line += CRLF |
| 390 | bytes = len(line) |
| 391 | while bytes > 0: |
| 392 | sent = self.sslobj.write(line) |
| 393 | if sent == bytes: |
| 394 | break # avoid copy |
| 395 | line = line[sent:] |
| 396 | bytes = bytes - sent |
| 397 | |
| 398 | def quit(self): |
| 399 | """Signoff: commit changes on server, unlock mailbox, close connection.""" |
| 400 | try: |
| 401 | resp = self._shortcmd('QUIT') |
| 402 | except error_proto, val: |
| 403 | resp = val |
| 404 | self.sock.close() |
| 405 | del self.sslobj, self.sock |
| 406 | return resp |
| 407 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 408 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 409 | if __name__ == "__main__": |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 410 | import sys |
| 411 | a = POP3(sys.argv[1]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 412 | print a.getwelcome() |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 413 | a.user(sys.argv[2]) |
| 414 | a.pass_(sys.argv[3]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 415 | a.list() |
| 416 | (numMsgs, totalSize) = a.stat() |
| 417 | for i in range(1, numMsgs + 1): |
| 418 | (header, msg, octets) = a.retr(i) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 419 | print "Message %d:" % i |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 420 | for line in msg: |
| 421 | print ' ' + line |
| 422 | print '-----------------------' |
| 423 | a.quit() |