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. |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 11 | # Example (see the test function at the end of this file) |
| 12 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 13 | # Imports |
| 14 | |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 15 | import re, socket |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 16 | |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 17 | __all__ = ["POP3","error_proto"] |
| 18 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 19 | # Exception raised when an error or invalid response is received: |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 20 | |
| 21 | class error_proto(Exception): pass |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 22 | |
| 23 | # Standard Port |
| 24 | POP3_PORT = 110 |
| 25 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 26 | # Line terminators (we always output CRLF, but accept any of CRLF, LFCR, LF) |
| 27 | CR = '\r' |
| 28 | LF = '\n' |
| 29 | CRLF = CR+LF |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class POP3: |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 33 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 34 | """This class supports both the minimal and optional command sets. |
| 35 | Arguments can be strings or integers (where appropriate) |
| 36 | (e.g.: retr(1) and retr('1') both work equally well. |
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 | Minimal Command Set: |
| 39 | USER name user(name) |
| 40 | PASS string pass_(string) |
| 41 | STAT stat() |
| 42 | LIST [msg] list(msg = None) |
| 43 | RETR msg retr(msg) |
| 44 | DELE msg dele(msg) |
| 45 | NOOP noop() |
| 46 | RSET rset() |
| 47 | QUIT quit() |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 48 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 49 | Optional Commands (some servers support these): |
| 50 | RPOP name rpop(name) |
| 51 | APOP name digest apop(name, digest) |
| 52 | TOP msg n top(msg, n) |
| 53 | UIDL [msg] uidl(msg = None) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 54 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 55 | Raises one exception: 'error_proto'. |
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 | Instantiate with: |
| 58 | POP3(hostname, port=110) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 59 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 60 | NB: the POP protocol locks the mailbox from user |
| 61 | authorization until QUIT, so be sure to get in, suck |
| 62 | the messages, and quit, each time you access the |
| 63 | mailbox. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 64 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 65 | POP is a line-based protocol, which means large mail |
| 66 | messages consume lots of python cycles reading them |
| 67 | line-by-line. |
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 | If it's available on your mail server, use IMAP4 |
| 70 | instead, it doesn't suffer from the two problems |
| 71 | above. |
| 72 | """ |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 73 | |
| 74 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 75 | def __init__(self, host, port = POP3_PORT): |
Martin v. Löwis | 4eb5940 | 2001-07-26 13:37:33 +0000 | [diff] [blame] | 76 | self.host = host |
| 77 | self.port = port |
Martin v. Löwis | 2ad2569 | 2001-07-31 08:40:21 +0000 | [diff] [blame] | 78 | msg = "getaddrinfo returns an empty list" |
Martin v. Löwis | 322c0d1 | 2001-10-07 08:53:32 +0000 | [diff] [blame] | 79 | self.sock = None |
Martin v. Löwis | 4eb5940 | 2001-07-26 13:37:33 +0000 | [diff] [blame] | 80 | for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): |
| 81 | af, socktype, proto, canonname, sa = res |
| 82 | try: |
| 83 | self.sock = socket.socket(af, socktype, proto) |
| 84 | self.sock.connect(sa) |
| 85 | except socket.error, msg: |
Martin v. Löwis | 322c0d1 | 2001-10-07 08:53:32 +0000 | [diff] [blame] | 86 | if self.sock: |
| 87 | self.sock.close() |
Martin v. Löwis | 4eb5940 | 2001-07-26 13:37:33 +0000 | [diff] [blame] | 88 | self.sock = None |
| 89 | continue |
| 90 | break |
| 91 | if not self.sock: |
| 92 | raise socket.error, msg |
| 93 | self.file = self.sock.makefile('rb') |
| 94 | self._debugging = 0 |
| 95 | self.welcome = self._getresp() |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 97 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 98 | def _putline(self, line): |
Fred Drake | a16433b | 2001-12-05 22:37:21 +0000 | [diff] [blame^] | 99 | if self._debugging > 1: print '*put*', `line` |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 100 | self.sock.send('%s%s' % (line, CRLF)) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 101 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 102 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 103 | # Internal: send one command to the server (through _putline()) |
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 | def _putcmd(self, line): |
Fred Drake | a16433b | 2001-12-05 22:37:21 +0000 | [diff] [blame^] | 106 | if self._debugging: print '*cmd*', `line` |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 107 | self._putline(line) |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 108 | |
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 | # Internal: return one line from the server, stripping CRLF. |
| 111 | # This is where all the CPU time of this module is consumed. |
| 112 | # Raise error_proto('-ERR EOF') if the connection is closed. |
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 | def _getline(self): |
| 115 | line = self.file.readline() |
Fred Drake | a16433b | 2001-12-05 22:37:21 +0000 | [diff] [blame^] | 116 | if self._debugging > 1: print '*get*', `line` |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 117 | if not line: raise error_proto('-ERR EOF') |
| 118 | octets = len(line) |
| 119 | # server can send any combination of CR & LF |
| 120 | # however, 'readline()' returns lines ending in LF |
| 121 | # so only possibilities are ...LF, ...CRLF, CR...LF |
| 122 | if line[-2:] == CRLF: |
| 123 | return line[:-2], octets |
| 124 | if line[0] == CR: |
| 125 | return line[1:-1], octets |
| 126 | return line[:-1], octets |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 128 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 129 | # Internal: get a response from the server. |
| 130 | # Raise 'error_proto' if the response doesn't start with '+'. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 131 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 132 | def _getresp(self): |
| 133 | resp, o = self._getline() |
Fred Drake | a16433b | 2001-12-05 22:37:21 +0000 | [diff] [blame^] | 134 | if self._debugging > 1: print '*resp*', `resp` |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 135 | c = resp[:1] |
| 136 | if c != '+': |
| 137 | raise error_proto(resp) |
| 138 | return resp |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 140 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 141 | # Internal: get a response plus following text from the server. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 142 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 143 | def _getlongresp(self): |
| 144 | resp = self._getresp() |
| 145 | list = []; octets = 0 |
| 146 | line, o = self._getline() |
| 147 | while line != '.': |
| 148 | if line[:2] == '..': |
| 149 | o = o-1 |
| 150 | line = line[1:] |
| 151 | octets = octets + o |
| 152 | list.append(line) |
| 153 | line, o = self._getline() |
| 154 | return resp, list, octets |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 156 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 157 | # Internal: send a command and get the response |
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 | def _shortcmd(self, line): |
| 160 | self._putcmd(line) |
| 161 | return self._getresp() |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 163 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 164 | # Internal: send a command and get the response plus following text |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 165 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 166 | def _longcmd(self, line): |
| 167 | self._putcmd(line) |
| 168 | return self._getlongresp() |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 169 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 170 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 171 | # These can be useful: |
| 172 | |
| 173 | def getwelcome(self): |
| 174 | return self.welcome |
| 175 | |
| 176 | |
| 177 | def set_debuglevel(self, level): |
| 178 | self._debugging = level |
| 179 | |
| 180 | |
| 181 | # Here are all the POP commands: |
| 182 | |
| 183 | def user(self, user): |
| 184 | """Send user name, return response |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 185 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 186 | (should indicate password required). |
| 187 | """ |
| 188 | return self._shortcmd('USER %s' % user) |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 189 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 190 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 191 | def pass_(self, pswd): |
| 192 | """Send password, return response |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 193 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 194 | (response includes message count, mailbox size). |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 195 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 196 | NB: mailbox is locked by server from here to 'quit()' |
| 197 | """ |
| 198 | return self._shortcmd('PASS %s' % pswd) |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 199 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 200 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 201 | def stat(self): |
| 202 | """Get mailbox status. |
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 | Result is tuple of 2 ints (message count, mailbox size) |
| 205 | """ |
| 206 | retval = self._shortcmd('STAT') |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 207 | rets = retval.split() |
Fred Drake | a16433b | 2001-12-05 22:37:21 +0000 | [diff] [blame^] | 208 | if self._debugging: print '*stat*', `rets` |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 209 | numMessages = int(rets[1]) |
| 210 | sizeMessages = int(rets[2]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 211 | return (numMessages, sizeMessages) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 212 | |
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 | def list(self, which=None): |
| 215 | """Request listing, return result. |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 216 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 217 | Result without a message number argument is in form |
| 218 | ['response', ['mesg_num octets', ...]]. |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 219 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 220 | Result when a message number argument is given is a |
| 221 | single response: the "scan listing" for that message. |
| 222 | """ |
| 223 | if which: |
| 224 | return self._shortcmd('LIST %s' % which) |
| 225 | return self._longcmd('LIST') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 227 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 228 | def retr(self, which): |
| 229 | """Retrieve whole message number 'which'. |
Guido van Rossum | f6ae743 | 1998-09-02 14:42:02 +0000 | [diff] [blame] | 230 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 231 | Result is in form ['response', ['line', ...], octets]. |
| 232 | """ |
| 233 | return self._longcmd('RETR %s' % which) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 235 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 236 | def dele(self, which): |
| 237 | """Delete message number 'which'. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 238 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 239 | Result is 'response'. |
| 240 | """ |
| 241 | return self._shortcmd('DELE %s' % which) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 243 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 244 | def noop(self): |
| 245 | """Does nothing. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 246 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 247 | One supposes the response indicates the server is alive. |
| 248 | """ |
| 249 | return self._shortcmd('NOOP') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 250 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 251 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 252 | def rset(self): |
| 253 | """Not sure what this does.""" |
| 254 | return self._shortcmd('RSET') |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 255 | |
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 | def quit(self): |
| 258 | """Signoff: commit changes on server, unlock mailbox, close connection.""" |
| 259 | try: |
| 260 | resp = self._shortcmd('QUIT') |
| 261 | except error_proto, val: |
| 262 | resp = val |
| 263 | self.file.close() |
| 264 | self.sock.close() |
| 265 | del self.file, self.sock |
| 266 | return resp |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 267 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 268 | #__del__ = quit |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 269 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 270 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 271 | # optional commands: |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 272 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 273 | def rpop(self, user): |
| 274 | """Not sure what this does.""" |
| 275 | return self._shortcmd('RPOP %s' % user) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 276 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 277 | |
Moshe Zadka | ccc2e3d | 2001-01-19 19:56:27 +0000 | [diff] [blame] | 278 | timestamp = re.compile(r'\+OK.*(<[^>]+>)') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 279 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 280 | def apop(self, user, secret): |
| 281 | """Authorisation |
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 | - only possible if server has supplied a timestamp in initial greeting. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 284 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 285 | Args: |
| 286 | user - mailbox user; |
| 287 | secret - secret shared between client and server. |
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 | NB: mailbox is locked by server from here to 'quit()' |
| 290 | """ |
Moshe Zadka | ccc2e3d | 2001-01-19 19:56:27 +0000 | [diff] [blame] | 291 | m = self.timestamp.match(self.welcome) |
| 292 | if not m: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 293 | raise error_proto('-ERR APOP not supported by server') |
| 294 | import md5 |
Moshe Zadka | ccc2e3d | 2001-01-19 19:56:27 +0000 | [diff] [blame] | 295 | digest = md5.new(m.group(1)+secret).digest() |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 296 | digest = ''.join(map(lambda x:'%02x'%ord(x), digest)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 297 | return self._shortcmd('APOP %s %s' % (user, digest)) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 298 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 299 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 300 | def top(self, which, howmuch): |
| 301 | """Retrieve message header of message number 'which' |
| 302 | and first 'howmuch' lines of message body. |
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 | Result is in form ['response', ['line', ...], octets]. |
| 305 | """ |
| 306 | return self._longcmd('TOP %s %s' % (which, howmuch)) |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 307 | |
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 uidl(self, which=None): |
| 310 | """Return message digest (unique id) list. |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 311 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 312 | If 'which', result contains unique id for that message |
| 313 | in the form 'response mesgnum uid', otherwise result is |
| 314 | the list ['response', ['mesgnum uid', ...], octets] |
| 315 | """ |
| 316 | if which: |
| 317 | return self._shortcmd('UIDL %s' % which) |
| 318 | return self._longcmd('UIDL') |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 319 | |
Guido van Rossum | 03774bb | 1998-04-09 13:50:55 +0000 | [diff] [blame] | 320 | |
Guido van Rossum | 484772d | 1998-04-06 18:27:27 +0000 | [diff] [blame] | 321 | if __name__ == "__main__": |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 322 | import sys |
| 323 | a = POP3(sys.argv[1]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 324 | print a.getwelcome() |
Eric S. Raymond | 341f929 | 2001-02-09 06:56:56 +0000 | [diff] [blame] | 325 | a.user(sys.argv[2]) |
| 326 | a.pass_(sys.argv[3]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 327 | a.list() |
| 328 | (numMsgs, totalSize) = a.stat() |
| 329 | for i in range(1, numMsgs + 1): |
| 330 | (header, msg, octets) = a.retr(i) |
| 331 | print "Message ", `i`, ':' |
| 332 | for line in msg: |
| 333 | print ' ' + line |
| 334 | print '-----------------------' |
| 335 | a.quit() |