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