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