Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 1 | """RFC 2822 message manipulation. |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 2 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 3 | Note: This is only a very rough sketch of a full RFC-822 parser; in particular |
| 4 | the tokenizing of addresses does not adhere to all the quoting rules. |
| 5 | |
| 6 | Note: RFC 2822 is a long awaited update to RFC 822. This module should |
| 7 | conform to RFC 2822, and is thus mis-named (it's not worth renaming it). Some |
| 8 | effort at RFC 2822 updates have been made, but a thorough audit has not been |
| 9 | performed. Consider any RFC 2822 non-conformance to be a bug. |
| 10 | |
| 11 | RFC 2822: http://www.faqs.org/rfcs/rfc2822.html |
Barry Warsaw | b8a55c0 | 2001-07-16 20:41:40 +0000 | [diff] [blame] | 12 | RFC 822 : http://www.faqs.org/rfcs/rfc822.html (obsolete) |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 13 | |
| 14 | Directions for use: |
| 15 | |
| 16 | To create a Message object: first open a file, e.g.: |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 18 | fp = open(file, 'r') |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | c7bb857 | 1998-06-10 21:31:01 +0000 | [diff] [blame] | 20 | You can use any other legal way of getting an open file object, e.g. use |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 21 | sys.stdin or call os.popen(). Then pass the open file object to the Message() |
| 22 | constructor: |
| 23 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 24 | m = Message(fp) |
| 25 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 26 | This class can work with any input object that supports a readline method. If |
| 27 | the input object has seek and tell capability, the rewindbody method will |
| 28 | work; also illegal lines will be pushed back onto the input stream. If the |
| 29 | input object lacks seek but has an `unread' method that can push back a line |
| 30 | of input, Message will use that to push back illegal lines. Thus this class |
| 31 | can be used to parse messages coming from a buffered stream. |
Guido van Rossum | c7bb857 | 1998-06-10 21:31:01 +0000 | [diff] [blame] | 32 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 33 | The optional `seekable' argument is provided as a workaround for certain stdio |
| 34 | libraries in which tell() discards buffered data before discovering that the |
| 35 | lseek() system call doesn't work. For maximum portability, you should set the |
| 36 | seekable argument to zero to prevent that initial \code{tell} when passing in |
| 37 | an unseekable object such as a a file object created from a socket object. If |
| 38 | it is 1 on entry -- which it is by default -- the tell() method of the open |
| 39 | file object is called once; if this raises an exception, seekable is reset to |
| 40 | 0. For other nonzero values of seekable, this test is not made. |
Guido van Rossum | c7bb857 | 1998-06-10 21:31:01 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 42 | To get the text of a particular header there are several methods: |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 44 | str = m.getheader(name) |
| 45 | str = m.getrawheader(name) |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 46 | |
| 47 | where name is the name of the header, e.g. 'Subject'. The difference is that |
| 48 | getheader() strips the leading and trailing whitespace, while getrawheader() |
| 49 | doesn't. Both functions retain embedded whitespace (including newlines) |
| 50 | exactly as they are specified in the header, and leave the case of the text |
| 51 | unchanged. |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 52 | |
| 53 | For addresses and address lists there are functions |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 54 | |
| 55 | realname, mailaddress = m.getaddr(name) |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 56 | list = m.getaddrlist(name) |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 58 | where the latter returns a list of (realname, mailaddr) tuples. |
| 59 | |
| 60 | There is also a method |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 62 | time = m.getdate(name) |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 64 | which parses a Date-like field and returns a time-compatible tuple, |
| 65 | i.e. a tuple such as returned by time.localtime() or accepted by |
| 66 | time.mktime(). |
| 67 | |
| 68 | See the class definition for lower level access methods. |
| 69 | |
| 70 | There are also some utility functions here. |
| 71 | """ |
Guido van Rossum | 4d4ab92 | 1998-06-16 22:27:09 +0000 | [diff] [blame] | 72 | # Cleanup and extensions by Eric S. Raymond <esr@thyrsus.com> |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 74 | import time |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 75 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 76 | __all__ = ["Message","AddressList","parsedate","parsedate_tz","mktime_tz"] |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 78 | _blanklines = ('\r\n', '\n') # Optimization for islast() |
Guido van Rossum | 92457b9 | 1995-06-22 19:06:57 +0000 | [diff] [blame] | 79 | |
| 80 | |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 81 | class Message: |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 82 | """Represents a single RFC 2822-compliant message.""" |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 84 | def __init__(self, fp, seekable = 1): |
| 85 | """Initialize the class instance and read the headers.""" |
Guido van Rossum | c7bb857 | 1998-06-10 21:31:01 +0000 | [diff] [blame] | 86 | if seekable == 1: |
| 87 | # Exercise tell() to make sure it works |
| 88 | # (and then assume seek() works, too) |
| 89 | try: |
| 90 | fp.tell() |
unknown | 67bbd7a | 2001-07-04 07:07:33 +0000 | [diff] [blame] | 91 | except (AttributeError, IOError): |
Guido van Rossum | c7bb857 | 1998-06-10 21:31:01 +0000 | [diff] [blame] | 92 | seekable = 0 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 93 | self.fp = fp |
| 94 | self.seekable = seekable |
| 95 | self.startofheaders = None |
| 96 | self.startofbody = None |
| 97 | # |
| 98 | if self.seekable: |
| 99 | try: |
| 100 | self.startofheaders = self.fp.tell() |
| 101 | except IOError: |
| 102 | self.seekable = 0 |
| 103 | # |
| 104 | self.readheaders() |
| 105 | # |
| 106 | if self.seekable: |
| 107 | try: |
| 108 | self.startofbody = self.fp.tell() |
| 109 | except IOError: |
| 110 | self.seekable = 0 |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 111 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 112 | def rewindbody(self): |
| 113 | """Rewind the file to the start of the body (if seekable).""" |
| 114 | if not self.seekable: |
| 115 | raise IOError, "unseekable file" |
| 116 | self.fp.seek(self.startofbody) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 117 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 118 | def readheaders(self): |
| 119 | """Read header lines. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 120 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 121 | Read header lines up to the entirely blank line that terminates them. |
| 122 | The (normally blank) line that ends the headers is skipped, but not |
| 123 | included in the returned list. If a non-header line ends the headers, |
| 124 | (which is an error), an attempt is made to backspace over it; it is |
| 125 | never included in the returned list. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 126 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 127 | The variable self.status is set to the empty string if all went well, |
| 128 | otherwise it is an error message. The variable self.headers is a |
| 129 | completely uninterpreted list of lines contained in the header (so |
| 130 | printing them will reproduce the header exactly as it appears in the |
| 131 | file). |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 132 | """ |
| 133 | self.dict = {} |
| 134 | self.unixfrom = '' |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 135 | self.headers = lst = [] |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 136 | self.status = '' |
| 137 | headerseen = "" |
| 138 | firstline = 1 |
Guido van Rossum | 052969a | 1998-07-21 14:24:04 +0000 | [diff] [blame] | 139 | startofline = unread = tell = None |
| 140 | if hasattr(self.fp, 'unread'): |
| 141 | unread = self.fp.unread |
| 142 | elif self.seekable: |
| 143 | tell = self.fp.tell |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 144 | while 1: |
Guido van Rossum | 052969a | 1998-07-21 14:24:04 +0000 | [diff] [blame] | 145 | if tell: |
Guido van Rossum | a66eed6 | 2000-11-09 18:05:24 +0000 | [diff] [blame] | 146 | try: |
| 147 | startofline = tell() |
| 148 | except IOError: |
| 149 | startofline = tell = None |
| 150 | self.seekable = 0 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 151 | line = self.fp.readline() |
| 152 | if not line: |
| 153 | self.status = 'EOF in headers' |
| 154 | break |
| 155 | # Skip unix From name time lines |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 156 | if firstline and line.startswith('From '): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 157 | self.unixfrom = self.unixfrom + line |
| 158 | continue |
| 159 | firstline = 0 |
Guido van Rossum | e894fc0 | 1998-06-11 13:58:40 +0000 | [diff] [blame] | 160 | if headerseen and line[0] in ' \t': |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 161 | # It's a continuation line. |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 162 | lst.append(line) |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 163 | x = (self.dict[headerseen] + "\n " + line.strip()) |
| 164 | self.dict[headerseen] = x.strip() |
Guido van Rossum | e894fc0 | 1998-06-11 13:58:40 +0000 | [diff] [blame] | 165 | continue |
Guido van Rossum | c7bb857 | 1998-06-10 21:31:01 +0000 | [diff] [blame] | 166 | elif self.iscomment(line): |
Guido van Rossum | e894fc0 | 1998-06-11 13:58:40 +0000 | [diff] [blame] | 167 | # It's a comment. Ignore it. |
| 168 | continue |
| 169 | elif self.islast(line): |
| 170 | # Note! No pushback here! The delimiter line gets eaten. |
| 171 | break |
| 172 | headerseen = self.isheader(line) |
| 173 | if headerseen: |
| 174 | # It's a legal header line, save it. |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 175 | lst.append(line) |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 176 | self.dict[headerseen] = line[len(headerseen)+1:].strip() |
Guido van Rossum | e894fc0 | 1998-06-11 13:58:40 +0000 | [diff] [blame] | 177 | continue |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 178 | else: |
Guido van Rossum | e894fc0 | 1998-06-11 13:58:40 +0000 | [diff] [blame] | 179 | # It's not a header line; throw it back and stop here. |
| 180 | if not self.dict: |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 181 | self.status = 'No headers' |
| 182 | else: |
Guido van Rossum | e894fc0 | 1998-06-11 13:58:40 +0000 | [diff] [blame] | 183 | self.status = 'Non-header line where header expected' |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 184 | # Try to undo the read. |
Guido van Rossum | 052969a | 1998-07-21 14:24:04 +0000 | [diff] [blame] | 185 | if unread: |
| 186 | unread(line) |
| 187 | elif tell: |
| 188 | self.fp.seek(startofline) |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 189 | else: |
Guido van Rossum | c7bb857 | 1998-06-10 21:31:01 +0000 | [diff] [blame] | 190 | self.status = self.status + '; bad seek' |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 191 | break |
Guido van Rossum | e894fc0 | 1998-06-11 13:58:40 +0000 | [diff] [blame] | 192 | |
| 193 | def isheader(self, line): |
| 194 | """Determine whether a given line is a legal header. |
| 195 | |
| 196 | This method should return the header name, suitably canonicalized. |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 197 | You may override this method in order to use Message parsing on tagged |
| 198 | data in RFC 2822-like formats with special header formats. |
Guido van Rossum | e894fc0 | 1998-06-11 13:58:40 +0000 | [diff] [blame] | 199 | """ |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 200 | i = line.find(':') |
Guido van Rossum | e894fc0 | 1998-06-11 13:58:40 +0000 | [diff] [blame] | 201 | if i > 0: |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 202 | return line[:i].lower() |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 203 | return None |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 205 | def islast(self, line): |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 206 | """Determine whether a line is a legal end of RFC 2822 headers. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 207 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 208 | You may override this method if your application wants to bend the |
| 209 | rules, e.g. to strip trailing whitespace, or to recognize MH template |
| 210 | separators ('--------'). For convenience (e.g. for code reading from |
| 211 | sockets) a line consisting of \r\n also matches. |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 212 | """ |
| 213 | return line in _blanklines |
Guido van Rossum | c7bb857 | 1998-06-10 21:31:01 +0000 | [diff] [blame] | 214 | |
| 215 | def iscomment(self, line): |
| 216 | """Determine whether a line should be skipped entirely. |
| 217 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 218 | You may override this method in order to use Message parsing on tagged |
| 219 | data in RFC 2822-like formats that support embedded comments or |
| 220 | free-text data. |
Guido van Rossum | c7bb857 | 1998-06-10 21:31:01 +0000 | [diff] [blame] | 221 | """ |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 222 | return False |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 223 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 224 | def getallmatchingheaders(self, name): |
| 225 | """Find all header lines matching a given header name. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 226 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 227 | Look through the list of headers and find all lines matching a given |
| 228 | header name (and their continuation lines). A list of the lines is |
| 229 | returned, without interpretation. If the header does not occur, an |
| 230 | empty list is returned. If the header occurs multiple times, all |
| 231 | occurrences are returned. Case is not important in the header name. |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 232 | """ |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 233 | name = name.lower() + ':' |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 234 | n = len(name) |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 235 | lst = [] |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 236 | hit = 0 |
| 237 | for line in self.headers: |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 238 | if line[:n].lower() == name: |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 239 | hit = 1 |
Guido van Rossum | 352ca8c | 2001-01-02 20:36:32 +0000 | [diff] [blame] | 240 | elif not line[:1].isspace(): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 241 | hit = 0 |
| 242 | if hit: |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 243 | lst.append(line) |
| 244 | return lst |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 246 | def getfirstmatchingheader(self, name): |
| 247 | """Get the first header line matching name. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 248 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 249 | This is similar to getallmatchingheaders, but it returns only the |
| 250 | first matching header (and its continuation lines). |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 251 | """ |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 252 | name = name.lower() + ':' |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 253 | n = len(name) |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 254 | lst = [] |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 255 | hit = 0 |
| 256 | for line in self.headers: |
| 257 | if hit: |
Guido van Rossum | 352ca8c | 2001-01-02 20:36:32 +0000 | [diff] [blame] | 258 | if not line[:1].isspace(): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 259 | break |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 260 | elif line[:n].lower() == name: |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 261 | hit = 1 |
| 262 | if hit: |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 263 | lst.append(line) |
| 264 | return lst |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 266 | def getrawheader(self, name): |
| 267 | """A higher-level interface to getfirstmatchingheader(). |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 268 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 269 | Return a string containing the literal text of the header but with the |
| 270 | keyword stripped. All leading, trailing and embedded whitespace is |
| 271 | kept in the string, however. Return None if the header does not |
| 272 | occur. |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 273 | """ |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 274 | |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 275 | lst = self.getfirstmatchingheader(name) |
| 276 | if not lst: |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 277 | return None |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 278 | lst[0] = lst[0][len(name) + 1:] |
| 279 | return ''.join(lst) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 280 | |
Guido van Rossum | c7bb857 | 1998-06-10 21:31:01 +0000 | [diff] [blame] | 281 | def getheader(self, name, default=None): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 282 | """Get the header value for a name. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 283 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 284 | This is the normal interface: it returns a stripped version of the |
| 285 | header value for a given header name, or None if it doesn't exist. |
| 286 | This uses the dictionary version which finds the *last* such header. |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 287 | """ |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 288 | return self.dict.get(name.lower(), default) |
Guido van Rossum | c7bb857 | 1998-06-10 21:31:01 +0000 | [diff] [blame] | 289 | get = getheader |
Fred Drake | ddf22c4 | 1999-04-28 21:17:38 +0000 | [diff] [blame] | 290 | |
| 291 | def getheaders(self, name): |
| 292 | """Get all values for a header. |
| 293 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 294 | This returns a list of values for headers given more than once; each |
| 295 | value in the result list is stripped in the same way as the result of |
| 296 | getheader(). If the header is not given, return an empty list. |
Fred Drake | ddf22c4 | 1999-04-28 21:17:38 +0000 | [diff] [blame] | 297 | """ |
| 298 | result = [] |
| 299 | current = '' |
| 300 | have_header = 0 |
| 301 | for s in self.getallmatchingheaders(name): |
Guido van Rossum | 352ca8c | 2001-01-02 20:36:32 +0000 | [diff] [blame] | 302 | if s[0].isspace(): |
Fred Drake | ddf22c4 | 1999-04-28 21:17:38 +0000 | [diff] [blame] | 303 | if current: |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 304 | current = "%s\n %s" % (current, s.strip()) |
Fred Drake | ddf22c4 | 1999-04-28 21:17:38 +0000 | [diff] [blame] | 305 | else: |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 306 | current = s.strip() |
Fred Drake | ddf22c4 | 1999-04-28 21:17:38 +0000 | [diff] [blame] | 307 | else: |
| 308 | if have_header: |
| 309 | result.append(current) |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 310 | current = s[s.find(":") + 1:].strip() |
Fred Drake | ddf22c4 | 1999-04-28 21:17:38 +0000 | [diff] [blame] | 311 | have_header = 1 |
| 312 | if have_header: |
| 313 | result.append(current) |
Fred Drake | cbfa5cb | 1999-06-14 15:40:23 +0000 | [diff] [blame] | 314 | return result |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 316 | def getaddr(self, name): |
| 317 | """Get a single address from a header, as a tuple. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 318 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 319 | An example return value: |
| 320 | ('Guido van Rossum', 'guido@cwi.nl') |
| 321 | """ |
| 322 | # New, by Ben Escoto |
| 323 | alist = self.getaddrlist(name) |
| 324 | if alist: |
| 325 | return alist[0] |
| 326 | else: |
| 327 | return (None, None) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 328 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 329 | def getaddrlist(self, name): |
| 330 | """Get a list of addresses from a header. |
Barry Warsaw | 8a57843 | 1999-01-14 19:59:58 +0000 | [diff] [blame] | 331 | |
| 332 | Retrieves a list of addresses from a header, where each address is a |
| 333 | tuple as returned by getaddr(). Scans all named headers, so it works |
| 334 | properly with multiple To: or Cc: headers for example. |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 335 | """ |
Barry Warsaw | 8a57843 | 1999-01-14 19:59:58 +0000 | [diff] [blame] | 336 | raw = [] |
| 337 | for h in self.getallmatchingheaders(name): |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 338 | if h[0] in ' \t': |
| 339 | raw.append(h) |
| 340 | else: |
| 341 | if raw: |
| 342 | raw.append(', ') |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 343 | i = h.find(':') |
Barry Warsaw | 8a57843 | 1999-01-14 19:59:58 +0000 | [diff] [blame] | 344 | if i > 0: |
| 345 | addr = h[i+1:] |
| 346 | raw.append(addr) |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 347 | alladdrs = ''.join(raw) |
Barry Warsaw | 56cdf11 | 2002-04-12 20:55:31 +0000 | [diff] [blame] | 348 | a = AddressList(alladdrs) |
Barry Warsaw | 0a8d4d5 | 2002-05-21 19:46:13 +0000 | [diff] [blame] | 349 | return a.addresslist |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 350 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 351 | def getdate(self, name): |
| 352 | """Retrieve a date field from a header. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 353 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 354 | Retrieves a date field from the named header, returning a tuple |
| 355 | compatible with time.mktime(). |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 356 | """ |
| 357 | try: |
| 358 | data = self[name] |
| 359 | except KeyError: |
| 360 | return None |
| 361 | return parsedate(data) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 362 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 363 | def getdate_tz(self, name): |
| 364 | """Retrieve a date field from a header as a 10-tuple. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 365 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 366 | The first 9 elements make up a tuple compatible with time.mktime(), |
| 367 | and the 10th is the offset of the poster's time zone from GMT/UTC. |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 368 | """ |
| 369 | try: |
| 370 | data = self[name] |
| 371 | except KeyError: |
| 372 | return None |
| 373 | return parsedate_tz(data) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 374 | |
| 375 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 376 | # Access as a dictionary (only finds *last* header of each type): |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 377 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 378 | def __len__(self): |
| 379 | """Get the number of headers in a message.""" |
| 380 | return len(self.dict) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 381 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 382 | def __getitem__(self, name): |
| 383 | """Get a specific header, as from a dictionary.""" |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 384 | return self.dict[name.lower()] |
Guido van Rossum | e894fc0 | 1998-06-11 13:58:40 +0000 | [diff] [blame] | 385 | |
| 386 | def __setitem__(self, name, value): |
Guido van Rossum | 4d4ab92 | 1998-06-16 22:27:09 +0000 | [diff] [blame] | 387 | """Set the value of a header. |
| 388 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 389 | Note: This is not a perfect inversion of __getitem__, because any |
| 390 | changed headers get stuck at the end of the raw-headers list rather |
| 391 | than where the altered header was. |
Guido van Rossum | 4d4ab92 | 1998-06-16 22:27:09 +0000 | [diff] [blame] | 392 | """ |
Guido van Rossum | e894fc0 | 1998-06-11 13:58:40 +0000 | [diff] [blame] | 393 | del self[name] # Won't fail if it doesn't exist |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 394 | self.dict[name.lower()] = value |
Guido van Rossum | e894fc0 | 1998-06-11 13:58:40 +0000 | [diff] [blame] | 395 | text = name + ": " + value |
Raymond Hettinger | 508e81e | 2005-02-08 15:39:11 +0000 | [diff] [blame] | 396 | for line in text.split("\n"): |
| 397 | self.headers.append(line + "\n") |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 398 | |
Guido van Rossum | 75d92c1 | 1998-04-02 21:33:20 +0000 | [diff] [blame] | 399 | def __delitem__(self, name): |
| 400 | """Delete all occurrences of a specific header, if it is present.""" |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 401 | name = name.lower() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 402 | if not name in self.dict: |
Guido van Rossum | f3c5f5c | 1999-09-15 22:15:23 +0000 | [diff] [blame] | 403 | return |
| 404 | del self.dict[name] |
| 405 | name = name + ':' |
Guido van Rossum | 75d92c1 | 1998-04-02 21:33:20 +0000 | [diff] [blame] | 406 | n = len(name) |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 407 | lst = [] |
Guido van Rossum | 75d92c1 | 1998-04-02 21:33:20 +0000 | [diff] [blame] | 408 | hit = 0 |
| 409 | for i in range(len(self.headers)): |
| 410 | line = self.headers[i] |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 411 | if line[:n].lower() == name: |
Guido van Rossum | 75d92c1 | 1998-04-02 21:33:20 +0000 | [diff] [blame] | 412 | hit = 1 |
Guido van Rossum | 352ca8c | 2001-01-02 20:36:32 +0000 | [diff] [blame] | 413 | elif not line[:1].isspace(): |
Guido van Rossum | 75d92c1 | 1998-04-02 21:33:20 +0000 | [diff] [blame] | 414 | hit = 0 |
| 415 | if hit: |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 416 | lst.append(i) |
| 417 | for i in reversed(lst): |
Guido van Rossum | 75d92c1 | 1998-04-02 21:33:20 +0000 | [diff] [blame] | 418 | del self.headers[i] |
| 419 | |
Fred Drake | 233226e | 2001-05-22 19:36:50 +0000 | [diff] [blame] | 420 | def setdefault(self, name, default=""): |
Fred Drake | 0295929 | 2001-05-22 14:58:10 +0000 | [diff] [blame] | 421 | lowername = name.lower() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 422 | if lowername in self.dict: |
Fred Drake | 0295929 | 2001-05-22 14:58:10 +0000 | [diff] [blame] | 423 | return self.dict[lowername] |
| 424 | else: |
Fred Drake | 233226e | 2001-05-22 19:36:50 +0000 | [diff] [blame] | 425 | text = name + ": " + default |
Raymond Hettinger | 508e81e | 2005-02-08 15:39:11 +0000 | [diff] [blame] | 426 | for line in text.split("\n"): |
| 427 | self.headers.append(line + "\n") |
Fred Drake | 233226e | 2001-05-22 19:36:50 +0000 | [diff] [blame] | 428 | self.dict[lowername] = default |
Fred Drake | 0295929 | 2001-05-22 14:58:10 +0000 | [diff] [blame] | 429 | return default |
| 430 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 431 | def __contains__(self, name): |
| 432 | """Determine whether a message contains the named header.""" |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 433 | return name.lower() in self.dict |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 434 | |
Raymond Hettinger | ce96d8b | 2004-09-22 17:17:32 +0000 | [diff] [blame] | 435 | def __iter__(self): |
| 436 | return iter(self.dict) |
| 437 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 438 | def keys(self): |
| 439 | """Get all of a message's header field names.""" |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 440 | return list(self.dict.keys()) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 441 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 442 | def values(self): |
| 443 | """Get all of a message's header field values.""" |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 444 | return list(self.dict.values()) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 445 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 446 | def items(self): |
| 447 | """Get all of a message's headers. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 448 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 449 | Returns a list of name, value tuples. |
| 450 | """ |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 451 | return list(self.dict.items()) |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 452 | |
Guido van Rossum | c7bb857 | 1998-06-10 21:31:01 +0000 | [diff] [blame] | 453 | def __str__(self): |
Neil Schemenauer | 767126d | 2003-11-11 19:39:17 +0000 | [diff] [blame] | 454 | return ''.join(self.headers) |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 455 | |
| 456 | |
| 457 | # Utility functions |
| 458 | # ----------------- |
| 459 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 460 | # XXX Should fix unquote() and quote() to be really conformant. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 461 | # XXX The inverses of the parse functions may also be useful. |
| 462 | |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 463 | |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 464 | def unquote(s): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 465 | """Remove quotes from a string.""" |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 466 | if len(s) > 1: |
| 467 | if s.startswith('"') and s.endswith('"'): |
| 468 | return s[1:-1].replace('\\\\', '\\').replace('\\"', '"') |
| 469 | if s.startswith('<') and s.endswith('>'): |
| 470 | return s[1:-1] |
| 471 | return s |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 472 | |
| 473 | |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 474 | def quote(s): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 475 | """Add quotes around a string.""" |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 476 | return s.replace('\\', '\\\\').replace('"', '\\"') |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 477 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 478 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 479 | def parseaddr(address): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 480 | """Parse an address into a (realname, mailaddr) tuple.""" |
Barry Warsaw | 56cdf11 | 2002-04-12 20:55:31 +0000 | [diff] [blame] | 481 | a = AddressList(address) |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 482 | lst = a.addresslist |
| 483 | if not lst: |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 484 | return (None, None) |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 485 | return lst[0] |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 486 | |
| 487 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 488 | class AddrlistClass: |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 489 | """Address parser class by Ben Escoto. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 490 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 491 | To understand what this class does, it helps to have a copy of |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 492 | RFC 2822 in front of you. |
| 493 | |
| 494 | http://www.faqs.org/rfcs/rfc2822.html |
Guido van Rossum | 4d4ab92 | 1998-06-16 22:27:09 +0000 | [diff] [blame] | 495 | |
| 496 | Note: this class interface is deprecated and may be removed in the future. |
| 497 | Use rfc822.AddressList instead. |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 498 | """ |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 499 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 500 | def __init__(self, field): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 501 | """Initialize a new instance. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 502 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 503 | `field' is an unparsed address header field, containing one or more |
| 504 | addresses. |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 505 | """ |
| 506 | self.specials = '()<>@,:;.\"[]' |
| 507 | self.pos = 0 |
| 508 | self.LWS = ' \t' |
Barry Warsaw | 8a57843 | 1999-01-14 19:59:58 +0000 | [diff] [blame] | 509 | self.CR = '\r\n' |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 510 | self.atomends = self.specials + self.LWS + self.CR |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 511 | # Note that RFC 2822 now specifies `.' as obs-phrase, meaning that it |
| 512 | # is obsolete syntax. RFC 2822 requires that we recognize obsolete |
| 513 | # syntax, so allow dots in phrases. |
| 514 | self.phraseends = self.atomends.replace('.', '') |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 515 | self.field = field |
| 516 | self.commentlist = [] |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 517 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 518 | def gotonext(self): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 519 | """Parse up to the start of the next address.""" |
| 520 | while self.pos < len(self.field): |
| 521 | if self.field[self.pos] in self.LWS + '\n\r': |
| 522 | self.pos = self.pos + 1 |
| 523 | elif self.field[self.pos] == '(': |
| 524 | self.commentlist.append(self.getcomment()) |
| 525 | else: break |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 526 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 527 | def getaddrlist(self): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 528 | """Parse all addresses. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 529 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 530 | Returns a list containing all of the addresses. |
| 531 | """ |
Barry Warsaw | f1fd282 | 2001-11-13 21:30:37 +0000 | [diff] [blame] | 532 | result = [] |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 533 | ad = self.getaddress() |
| 534 | while ad: |
| 535 | result += ad |
Barry Warsaw | f1fd282 | 2001-11-13 21:30:37 +0000 | [diff] [blame] | 536 | ad = self.getaddress() |
Barry Warsaw | f1fd282 | 2001-11-13 21:30:37 +0000 | [diff] [blame] | 537 | return result |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 538 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 539 | def getaddress(self): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 540 | """Parse the next address.""" |
| 541 | self.commentlist = [] |
| 542 | self.gotonext() |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 543 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 544 | oldpos = self.pos |
| 545 | oldcl = self.commentlist |
| 546 | plist = self.getphraselist() |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 547 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 548 | self.gotonext() |
| 549 | returnlist = [] |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 550 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 551 | if self.pos >= len(self.field): |
| 552 | # Bad email address technically, no domain. |
| 553 | if plist: |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 554 | returnlist = [(' '.join(self.commentlist), plist[0])] |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 555 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 556 | elif self.field[self.pos] in '.@': |
| 557 | # email address is just an addrspec |
| 558 | # this isn't very efficient since we start over |
| 559 | self.pos = oldpos |
| 560 | self.commentlist = oldcl |
| 561 | addrspec = self.getaddrspec() |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 562 | returnlist = [(' '.join(self.commentlist), addrspec)] |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 563 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 564 | elif self.field[self.pos] == ':': |
| 565 | # address is a group |
| 566 | returnlist = [] |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 567 | |
Barry Warsaw | 96e9bf4 | 1999-07-12 18:37:02 +0000 | [diff] [blame] | 568 | fieldlen = len(self.field) |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 569 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 570 | while self.pos < len(self.field): |
| 571 | self.gotonext() |
Barry Warsaw | 96e9bf4 | 1999-07-12 18:37:02 +0000 | [diff] [blame] | 572 | if self.pos < fieldlen and self.field[self.pos] == ';': |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 573 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 574 | break |
| 575 | returnlist = returnlist + self.getaddress() |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 576 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 577 | elif self.field[self.pos] == '<': |
| 578 | # Address is a phrase then a route addr |
| 579 | routeaddr = self.getrouteaddr() |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 580 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 581 | if self.commentlist: |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 582 | returnlist = [(' '.join(plist) + ' (' + \ |
| 583 | ' '.join(self.commentlist) + ')', routeaddr)] |
| 584 | else: returnlist = [(' '.join(plist), routeaddr)] |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 585 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 586 | else: |
| 587 | if plist: |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 588 | returnlist = [(' '.join(self.commentlist), plist[0])] |
Barry Warsaw | 8a57843 | 1999-01-14 19:59:58 +0000 | [diff] [blame] | 589 | elif self.field[self.pos] in self.specials: |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 590 | self.pos += 1 |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 591 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 592 | self.gotonext() |
| 593 | if self.pos < len(self.field) and self.field[self.pos] == ',': |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 594 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 595 | return returnlist |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 596 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 597 | def getrouteaddr(self): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 598 | """Parse a route address (Return-path value). |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 599 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 600 | This method just skips all the route stuff and returns the addrspec. |
| 601 | """ |
| 602 | if self.field[self.pos] != '<': |
| 603 | return |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 604 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 605 | expectroute = 0 |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 606 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 607 | self.gotonext() |
Guido van Rossum | f830a52 | 2001-12-20 15:54:48 +0000 | [diff] [blame] | 608 | adlist = "" |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 609 | while self.pos < len(self.field): |
| 610 | if expectroute: |
| 611 | self.getdomain() |
| 612 | expectroute = 0 |
| 613 | elif self.field[self.pos] == '>': |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 614 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 615 | break |
| 616 | elif self.field[self.pos] == '@': |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 617 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 618 | expectroute = 1 |
| 619 | elif self.field[self.pos] == ':': |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 620 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 621 | else: |
| 622 | adlist = self.getaddrspec() |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 623 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 624 | break |
| 625 | self.gotonext() |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 626 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 627 | return adlist |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 628 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 629 | def getaddrspec(self): |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 630 | """Parse an RFC 2822 addr-spec.""" |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 631 | aslist = [] |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 632 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 633 | self.gotonext() |
| 634 | while self.pos < len(self.field): |
| 635 | if self.field[self.pos] == '.': |
| 636 | aslist.append('.') |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 637 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 638 | elif self.field[self.pos] == '"': |
Guido van Rossum | b184487 | 1999-06-15 18:06:20 +0000 | [diff] [blame] | 639 | aslist.append('"%s"' % self.getquote()) |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 640 | elif self.field[self.pos] in self.atomends: |
| 641 | break |
| 642 | else: aslist.append(self.getatom()) |
| 643 | self.gotonext() |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 644 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 645 | if self.pos >= len(self.field) or self.field[self.pos] != '@': |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 646 | return ''.join(aslist) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 647 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 648 | aslist.append('@') |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 649 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 650 | self.gotonext() |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 651 | return ''.join(aslist) + self.getdomain() |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 652 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 653 | def getdomain(self): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 654 | """Get the complete domain name from an address.""" |
| 655 | sdlist = [] |
| 656 | while self.pos < len(self.field): |
| 657 | if self.field[self.pos] in self.LWS: |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 658 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 659 | elif self.field[self.pos] == '(': |
| 660 | self.commentlist.append(self.getcomment()) |
| 661 | elif self.field[self.pos] == '[': |
| 662 | sdlist.append(self.getdomainliteral()) |
| 663 | elif self.field[self.pos] == '.': |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 664 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 665 | sdlist.append('.') |
| 666 | elif self.field[self.pos] in self.atomends: |
| 667 | break |
| 668 | else: sdlist.append(self.getatom()) |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 669 | return ''.join(sdlist) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 670 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 671 | def getdelimited(self, beginchar, endchars, allowcomments = 1): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 672 | """Parse a header fragment delimited by special characters. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 673 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 674 | `beginchar' is the start character for the fragment. If self is not |
| 675 | looking at an instance of `beginchar' then getdelimited returns the |
| 676 | empty string. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 677 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 678 | `endchars' is a sequence of allowable end-delimiting characters. |
| 679 | Parsing stops when one of these is encountered. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 680 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 681 | If `allowcomments' is non-zero, embedded RFC 2822 comments are allowed |
| 682 | within the parsed fragment. |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 683 | """ |
| 684 | if self.field[self.pos] != beginchar: |
| 685 | return '' |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 686 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 687 | slist = [''] |
| 688 | quote = 0 |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 689 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 690 | while self.pos < len(self.field): |
| 691 | if quote == 1: |
| 692 | slist.append(self.field[self.pos]) |
| 693 | quote = 0 |
| 694 | elif self.field[self.pos] in endchars: |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 695 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 696 | break |
| 697 | elif allowcomments and self.field[self.pos] == '(': |
| 698 | slist.append(self.getcomment()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 699 | continue # have already advanced pos from getcomment |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 700 | elif self.field[self.pos] == '\\': |
| 701 | quote = 1 |
| 702 | else: |
| 703 | slist.append(self.field[self.pos]) |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 704 | self.pos += 1 |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 705 | |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 706 | return ''.join(slist) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 707 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 708 | def getquote(self): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 709 | """Get a quote-delimited fragment from self's field.""" |
| 710 | return self.getdelimited('"', '"\r', 0) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 711 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 712 | def getcomment(self): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 713 | """Get a parenthesis-delimited fragment from self's field.""" |
| 714 | return self.getdelimited('(', ')\r', 1) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 715 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 716 | def getdomainliteral(self): |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 717 | """Parse an RFC 2822 domain-literal.""" |
Barry Warsaw | 2ea2b11 | 2000-09-25 15:08:27 +0000 | [diff] [blame] | 718 | return '[%s]' % self.getdelimited('[', ']\r', 0) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 719 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 720 | def getatom(self, atomends=None): |
| 721 | """Parse an RFC 2822 atom. |
| 722 | |
| 723 | Optional atomends specifies a different set of end token delimiters |
| 724 | (the default is to use self.atomends). This is used e.g. in |
| 725 | getphraselist() since phrase endings must not include the `.' (which |
| 726 | is legal in phrases).""" |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 727 | atomlist = [''] |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 728 | if atomends is None: |
| 729 | atomends = self.atomends |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 730 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 731 | while self.pos < len(self.field): |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 732 | if self.field[self.pos] in atomends: |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 733 | break |
| 734 | else: atomlist.append(self.field[self.pos]) |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 735 | self.pos += 1 |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 736 | |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 737 | return ''.join(atomlist) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 738 | |
Guido van Rossum | be7c45e | 1997-11-22 21:49:19 +0000 | [diff] [blame] | 739 | def getphraselist(self): |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 740 | """Parse a sequence of RFC 2822 phrases. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 741 | |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 742 | A phrase is a sequence of words, which are in turn either RFC 2822 |
| 743 | atoms or quoted-strings. Phrases are canonicalized by squeezing all |
| 744 | runs of continuous whitespace into one space. |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 745 | """ |
| 746 | plist = [] |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 747 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 748 | while self.pos < len(self.field): |
| 749 | if self.field[self.pos] in self.LWS: |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 750 | self.pos += 1 |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 751 | elif self.field[self.pos] == '"': |
| 752 | plist.append(self.getquote()) |
| 753 | elif self.field[self.pos] == '(': |
| 754 | self.commentlist.append(self.getcomment()) |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 755 | elif self.field[self.pos] in self.phraseends: |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 756 | break |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 757 | else: |
| 758 | plist.append(self.getatom(self.phraseends)) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 759 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 760 | return plist |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 761 | |
Guido van Rossum | 4d4ab92 | 1998-06-16 22:27:09 +0000 | [diff] [blame] | 762 | class AddressList(AddrlistClass): |
Barry Warsaw | 9ec58aa | 2001-07-16 20:40:35 +0000 | [diff] [blame] | 763 | """An AddressList encapsulates a list of parsed RFC 2822 addresses.""" |
Guido van Rossum | 4d4ab92 | 1998-06-16 22:27:09 +0000 | [diff] [blame] | 764 | def __init__(self, field): |
| 765 | AddrlistClass.__init__(self, field) |
| 766 | if field: |
| 767 | self.addresslist = self.getaddrlist() |
| 768 | else: |
| 769 | self.addresslist = [] |
| 770 | |
| 771 | def __len__(self): |
| 772 | return len(self.addresslist) |
| 773 | |
| 774 | def __str__(self): |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 775 | return ", ".join(map(dump_address_pair, self.addresslist)) |
Guido van Rossum | 4d4ab92 | 1998-06-16 22:27:09 +0000 | [diff] [blame] | 776 | |
| 777 | def __add__(self, other): |
| 778 | # Set union |
| 779 | newaddr = AddressList(None) |
| 780 | newaddr.addresslist = self.addresslist[:] |
| 781 | for x in other.addresslist: |
| 782 | if not x in self.addresslist: |
| 783 | newaddr.addresslist.append(x) |
| 784 | return newaddr |
| 785 | |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 786 | def __iadd__(self, other): |
| 787 | # Set union, in-place |
| 788 | for x in other.addresslist: |
| 789 | if not x in self.addresslist: |
| 790 | self.addresslist.append(x) |
| 791 | return self |
| 792 | |
Guido van Rossum | 4d4ab92 | 1998-06-16 22:27:09 +0000 | [diff] [blame] | 793 | def __sub__(self, other): |
| 794 | # Set difference |
| 795 | newaddr = AddressList(None) |
| 796 | for x in self.addresslist: |
| 797 | if not x in other.addresslist: |
| 798 | newaddr.addresslist.append(x) |
| 799 | return newaddr |
| 800 | |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 801 | def __isub__(self, other): |
| 802 | # Set difference, in-place |
| 803 | for x in other.addresslist: |
| 804 | if x in self.addresslist: |
| 805 | self.addresslist.remove(x) |
| 806 | return self |
| 807 | |
Guido van Rossum | 81d10b4 | 1998-06-16 22:29:03 +0000 | [diff] [blame] | 808 | def __getitem__(self, index): |
| 809 | # Make indexing, slices, and 'in' work |
Guido van Rossum | a07934e | 1999-09-03 13:23:49 +0000 | [diff] [blame] | 810 | return self.addresslist[index] |
Guido van Rossum | 81d10b4 | 1998-06-16 22:29:03 +0000 | [diff] [blame] | 811 | |
Guido van Rossum | 4d4ab92 | 1998-06-16 22:27:09 +0000 | [diff] [blame] | 812 | def dump_address_pair(pair): |
| 813 | """Dump a (name, address) pair in a canonicalized form.""" |
| 814 | if pair[0]: |
| 815 | return '"' + pair[0] + '" <' + pair[1] + '>' |
| 816 | else: |
| 817 | return pair[1] |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 818 | |
| 819 | # Parse a date field |
| 820 | |
Guido van Rossum | db01ee0 | 1998-12-23 22:22:10 +0000 | [diff] [blame] | 821 | _monthnames = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', |
| 822 | 'aug', 'sep', 'oct', 'nov', 'dec', |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 823 | 'january', 'february', 'march', 'april', 'may', 'june', 'july', |
Guido van Rossum | db01ee0 | 1998-12-23 22:22:10 +0000 | [diff] [blame] | 824 | 'august', 'september', 'october', 'november', 'december'] |
| 825 | _daynames = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'] |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 826 | |
Guido van Rossum | 27cb8a4 | 1996-11-20 22:12:26 +0000 | [diff] [blame] | 827 | # The timezone table does not include the military time zones defined |
| 828 | # in RFC822, other than Z. According to RFC1123, the description in |
| 829 | # RFC822 gets the signs wrong, so we can't rely on any such time |
| 830 | # zones. RFC1123 recommends that numeric timezone indicators be used |
| 831 | # instead of timezone names. |
| 832 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 833 | _timezones = {'UT':0, 'UTC':0, 'GMT':0, 'Z':0, |
Guido van Rossum | 67133e2 | 1998-05-18 16:09:10 +0000 | [diff] [blame] | 834 | 'AST': -400, 'ADT': -300, # Atlantic (used in Canada) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 835 | 'EST': -500, 'EDT': -400, # Eastern |
Guido van Rossum | 67133e2 | 1998-05-18 16:09:10 +0000 | [diff] [blame] | 836 | 'CST': -600, 'CDT': -500, # Central |
| 837 | 'MST': -700, 'MDT': -600, # Mountain |
| 838 | 'PST': -800, 'PDT': -700 # Pacific |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 839 | } |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 840 | |
Guido van Rossum | 27cb8a4 | 1996-11-20 22:12:26 +0000 | [diff] [blame] | 841 | |
| 842 | def parsedate_tz(data): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 843 | """Convert a date string to a time tuple. |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 844 | |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 845 | Accounts for military timezones. |
| 846 | """ |
Barry Warsaw | 4a106ee | 2001-11-13 18:00:40 +0000 | [diff] [blame] | 847 | if not data: |
| 848 | return None |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 849 | data = data.split() |
| 850 | if data[0][-1] in (',', '.') or data[0].lower() in _daynames: |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 851 | # There's a dayname here. Skip it |
| 852 | del data[0] |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 853 | else: |
| 854 | # no space after the "weekday,"? |
| 855 | i = data[0].rfind(',') |
| 856 | if i >= 0: |
| 857 | data[0] = data[0][i+1:] |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 858 | if len(data) == 3: # RFC 850 date, deprecated |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 859 | stuff = data[0].split('-') |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 860 | if len(stuff) == 3: |
| 861 | data = stuff + data[1:] |
| 862 | if len(data) == 4: |
| 863 | s = data[3] |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 864 | i = s.find('+') |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 865 | if i > 0: |
| 866 | data[3:] = [s[:i], s[i+1:]] |
| 867 | else: |
| 868 | data.append('') # Dummy tz |
| 869 | if len(data) < 5: |
| 870 | return None |
| 871 | data = data[:5] |
| 872 | [dd, mm, yy, tm, tz] = data |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 873 | mm = mm.lower() |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 874 | if not mm in _monthnames: |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 875 | dd, mm = mm, dd.lower() |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 876 | if not mm in _monthnames: |
| 877 | return None |
| 878 | mm = _monthnames.index(mm)+1 |
Guido van Rossum | b08f51b | 1999-04-29 12:50:36 +0000 | [diff] [blame] | 879 | if mm > 12: mm = mm - 12 |
Guido van Rossum | db01ee0 | 1998-12-23 22:22:10 +0000 | [diff] [blame] | 880 | if dd[-1] == ',': |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 881 | dd = dd[:-1] |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 882 | i = yy.find(':') |
Guido van Rossum | db01ee0 | 1998-12-23 22:22:10 +0000 | [diff] [blame] | 883 | if i > 0: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 884 | yy, tm = tm, yy |
Guido van Rossum | db01ee0 | 1998-12-23 22:22:10 +0000 | [diff] [blame] | 885 | if yy[-1] == ',': |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 886 | yy = yy[:-1] |
Guido van Rossum | 352ca8c | 2001-01-02 20:36:32 +0000 | [diff] [blame] | 887 | if not yy[0].isdigit(): |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 888 | yy, tz = tz, yy |
Guido van Rossum | db01ee0 | 1998-12-23 22:22:10 +0000 | [diff] [blame] | 889 | if tm[-1] == ',': |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 890 | tm = tm[:-1] |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 891 | tm = tm.split(':') |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 892 | if len(tm) == 2: |
| 893 | [thh, tmm] = tm |
| 894 | tss = '0' |
Guido van Rossum | 99e1131 | 1998-12-23 21:58:38 +0000 | [diff] [blame] | 895 | elif len(tm) == 3: |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 896 | [thh, tmm, tss] = tm |
Guido van Rossum | 99e1131 | 1998-12-23 21:58:38 +0000 | [diff] [blame] | 897 | else: |
| 898 | return None |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 899 | try: |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 900 | yy = int(yy) |
| 901 | dd = int(dd) |
| 902 | thh = int(thh) |
| 903 | tmm = int(tmm) |
| 904 | tss = int(tss) |
| 905 | except ValueError: |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 906 | return None |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 907 | tzoffset = None |
| 908 | tz = tz.upper() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 909 | if tz in _timezones: |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 910 | tzoffset = _timezones[tz] |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 911 | else: |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 912 | try: |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 913 | tzoffset = int(tz) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 914 | except ValueError: |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 915 | pass |
| 916 | # Convert a timezone offset into seconds ; -0500 -> -18000 |
Guido van Rossum | a73033f | 1998-02-19 00:28:58 +0000 | [diff] [blame] | 917 | if tzoffset: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 918 | if tzoffset < 0: |
| 919 | tzsign = -1 |
| 920 | tzoffset = -tzoffset |
| 921 | else: |
| 922 | tzsign = 1 |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 923 | tzoffset = tzsign * ( (tzoffset//100)*3600 + (tzoffset % 100)*60) |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 924 | return (yy, mm, dd, thh, tmm, tss, 0, 1, 0, tzoffset) |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 925 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 926 | |
Guido van Rossum | 27cb8a4 | 1996-11-20 22:12:26 +0000 | [diff] [blame] | 927 | def parsedate(data): |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 928 | """Convert a time string to a time tuple.""" |
Guido van Rossum | c80f182 | 2000-12-15 15:37:48 +0000 | [diff] [blame] | 929 | t = parsedate_tz(data) |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 930 | if t is None: |
| 931 | return t |
| 932 | return t[:9] |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 933 | |
Guido van Rossum | 27cb8a4 | 1996-11-20 22:12:26 +0000 | [diff] [blame] | 934 | |
Guido van Rossum | 6cdd7a0 | 1996-12-12 18:39:54 +0000 | [diff] [blame] | 935 | def mktime_tz(data): |
Guido van Rossum | 67133e2 | 1998-05-18 16:09:10 +0000 | [diff] [blame] | 936 | """Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp.""" |
Guido van Rossum | a73033f | 1998-02-19 00:28:58 +0000 | [diff] [blame] | 937 | if data[9] is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 938 | # No zone info, so localtime is better assumption than GMT |
| 939 | return time.mktime(data[:8] + (-1,)) |
Guido van Rossum | a73033f | 1998-02-19 00:28:58 +0000 | [diff] [blame] | 940 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 941 | t = time.mktime(data[:8] + (0,)) |
| 942 | return t - data[9] - time.timezone |
Guido van Rossum | 6cdd7a0 | 1996-12-12 18:39:54 +0000 | [diff] [blame] | 943 | |
Guido van Rossum | 247a78a | 1999-04-19 18:04:38 +0000 | [diff] [blame] | 944 | def formatdate(timeval=None): |
| 945 | """Returns time format preferred for Internet standards. |
| 946 | |
| 947 | Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123 |
Jeremy Hylton | 6d8c1aa | 2001-08-27 20:16:53 +0000 | [diff] [blame] | 948 | |
| 949 | According to RFC 1123, day and month names must always be in |
| 950 | English. If not for that, this code could use strftime(). It |
| 951 | can't because strftime() honors the locale and could generated |
| 952 | non-English names. |
Guido van Rossum | 247a78a | 1999-04-19 18:04:38 +0000 | [diff] [blame] | 953 | """ |
| 954 | if timeval is None: |
| 955 | timeval = time.time() |
Jeremy Hylton | 6d8c1aa | 2001-08-27 20:16:53 +0000 | [diff] [blame] | 956 | timeval = time.gmtime(timeval) |
| 957 | return "%s, %02d %s %04d %02d:%02d:%02d GMT" % ( |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 958 | ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")[timeval[6]], |
Jeremy Hylton | 6d8c1aa | 2001-08-27 20:16:53 +0000 | [diff] [blame] | 959 | timeval[2], |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 960 | ("Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 961 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")[timeval[1]-1], |
Tim Peters | 83e7ccc | 2001-09-04 06:37:28 +0000 | [diff] [blame] | 962 | timeval[0], timeval[3], timeval[4], timeval[5]) |
Guido van Rossum | 247a78a | 1999-04-19 18:04:38 +0000 | [diff] [blame] | 963 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 964 | |
| 965 | # When used as script, run a small test program. |
| 966 | # The first command line argument must be a filename containing one |
| 967 | # message in RFC-822 format. |
| 968 | |
| 969 | if __name__ == '__main__': |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 970 | import sys, os |
| 971 | file = os.path.join(os.environ['HOME'], 'Mail/inbox/1') |
| 972 | if sys.argv[1:]: file = sys.argv[1] |
| 973 | f = open(file, 'r') |
| 974 | m = Message(f) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 975 | print('From:', m.getaddr('from')) |
| 976 | print('To:', m.getaddrlist('to')) |
| 977 | print('Subject:', m.getheader('subject')) |
| 978 | print('Date:', m.getheader('date')) |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 979 | date = m.getdate_tz('date') |
Guido van Rossum | 1d2b23e | 2000-01-17 14:11:04 +0000 | [diff] [blame] | 980 | tz = date[-1] |
| 981 | date = time.localtime(mktime_tz(date)) |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 982 | if date: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 983 | print('ParsedDate:', time.asctime(date), end=' ') |
Guido van Rossum | 1d2b23e | 2000-01-17 14:11:04 +0000 | [diff] [blame] | 984 | hhmmss = tz |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 985 | hhmm, ss = divmod(hhmmss, 60) |
| 986 | hh, mm = divmod(hhmm, 60) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 987 | print("%+03d%02d" % (hh, mm), end=' ') |
| 988 | if ss: print(".%02d" % ss, end=' ') |
| 989 | print() |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 990 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 991 | print('ParsedDate:', None) |
Guido van Rossum | 9ab94c1 | 1997-12-10 16:17:39 +0000 | [diff] [blame] | 992 | m.rewindbody() |
| 993 | n = 0 |
| 994 | while f.readline(): |
Raymond Hettinger | bb5fbc4 | 2005-02-08 08:05:13 +0000 | [diff] [blame] | 995 | n += 1 |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 996 | print('Lines:', n) |
| 997 | print('-'*70) |
| 998 | print('len =', len(m)) |
| 999 | if 'Date' in m: print('Date =', m['Date']) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1000 | if 'X-Nonsense' in m: pass |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1001 | print('keys =', m.keys()) |
| 1002 | print('values =', m.values()) |
| 1003 | print('items =', m.items()) |