Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 1 | # RFC-822 message manipulation class. |
| 2 | # |
| 3 | # XXX This is only a very rough sketch of a full RFC-822 parser; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4 | # in particular the tokenizing of addresses does not adhere to all the |
| 5 | # quoting rules. |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 6 | # |
| 7 | # Directions for use: |
| 8 | # |
| 9 | # To create a Message object: first open a file, e.g.: |
| 10 | # fp = open(file, 'r') |
| 11 | # (or use any other legal way of getting an open file object, e.g. use |
| 12 | # sys.stdin or call os.popen()). |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 13 | # Then pass the open file object to the Message() constructor: |
| 14 | # m = Message(fp) |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 15 | # |
| 16 | # To get the text of a particular header there are several methods: |
| 17 | # str = m.getheader(name) |
| 18 | # str = m.getrawheader(name) |
| 19 | # where name is the name of the header, e.g. 'Subject'. |
| 20 | # The difference is that getheader() strips the leading and trailing |
| 21 | # whitespace, while getrawheader() doesn't. Both functions retain |
| 22 | # embedded whitespace (including newlines) exactly as they are |
| 23 | # specified in the header, and leave the case of the text unchanged. |
| 24 | # |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 25 | # For addresses and address lists there are functions |
| 26 | # realname, mailaddress = m.getaddr(name) and |
| 27 | # list = m.getaddrlist(name) |
| 28 | # where the latter returns a list of (realname, mailaddr) tuples. |
| 29 | # |
| 30 | # There is also a method |
| 31 | # time = m.getdate(name) |
| 32 | # which parses a Date-like field and returns a time-compatible tuple, |
| 33 | # i.e. a tuple such as returned by time.localtime() or accepted by |
| 34 | # time.mktime(). |
| 35 | # |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 36 | # See the class definition for lower level access methods. |
| 37 | # |
| 38 | # There are also some utility functions here. |
| 39 | |
| 40 | |
| 41 | import regex |
| 42 | import string |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 43 | import time |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 44 | |
| 45 | |
Guido van Rossum | 92457b9 | 1995-06-22 19:06:57 +0000 | [diff] [blame] | 46 | _blanklines = ('\r\n', '\n') # Optimization for islast() |
| 47 | |
| 48 | |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 49 | class Message: |
| 50 | |
| 51 | # Initialize the class instance and read the headers. |
| 52 | |
Guido van Rossum | 92457b9 | 1995-06-22 19:06:57 +0000 | [diff] [blame] | 53 | def __init__(self, fp, seekable = 1): |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 54 | self.fp = fp |
Guido van Rossum | 92457b9 | 1995-06-22 19:06:57 +0000 | [diff] [blame] | 55 | self.seekable = seekable |
| 56 | self.startofheaders = None |
| 57 | self.startofbody = None |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 58 | # |
Guido van Rossum | 92457b9 | 1995-06-22 19:06:57 +0000 | [diff] [blame] | 59 | if self.seekable: |
| 60 | try: |
| 61 | self.startofheaders = self.fp.tell() |
| 62 | except IOError: |
| 63 | self.seekable = 0 |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 64 | # |
| 65 | self.readheaders() |
| 66 | # |
Guido van Rossum | 92457b9 | 1995-06-22 19:06:57 +0000 | [diff] [blame] | 67 | if self.seekable: |
| 68 | try: |
| 69 | self.startofbody = self.fp.tell() |
| 70 | except IOError: |
| 71 | self.seekable = 0 |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 72 | |
| 73 | |
| 74 | # Rewind the file to the start of the body (if seekable). |
| 75 | |
| 76 | def rewindbody(self): |
Guido van Rossum | 92457b9 | 1995-06-22 19:06:57 +0000 | [diff] [blame] | 77 | if not self.seekable: |
| 78 | raise IOError, "unseekable file" |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 79 | self.fp.seek(self.startofbody) |
| 80 | |
| 81 | |
| 82 | # Read header lines up to the entirely blank line that |
| 83 | # terminates them. The (normally blank) line that ends the |
| 84 | # headers is skipped, but not included in the returned list. |
| 85 | # If a non-header line ends the headers, (which is an error), |
| 86 | # an attempt is made to backspace over it; it is never |
| 87 | # included in the returned list. |
| 88 | # |
| 89 | # The variable self.status is set to the empty string if all |
| 90 | # went well, otherwise it is an error message. |
| 91 | # The variable self.headers is a completely uninterpreted list |
| 92 | # of lines contained in the header (so printing them will |
| 93 | # reproduce the header exactly as it appears in the file). |
| 94 | |
| 95 | def readheaders(self): |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 96 | self.dict = {} |
Guido van Rossum | 92457b9 | 1995-06-22 19:06:57 +0000 | [diff] [blame] | 97 | self.unixfrom = '' |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 98 | self.headers = list = [] |
| 99 | self.status = '' |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 100 | headerseen = "" |
Jack Jansen | 3a15dca | 1995-06-13 11:19:48 +0000 | [diff] [blame] | 101 | firstline = 1 |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 102 | while 1: |
| 103 | line = self.fp.readline() |
| 104 | if not line: |
| 105 | self.status = 'EOF in headers' |
| 106 | break |
Jack Jansen | 3a15dca | 1995-06-13 11:19:48 +0000 | [diff] [blame] | 107 | # Skip unix From name time lines |
Jack Jansen | e5e2cdd | 1995-06-16 10:57:14 +0000 | [diff] [blame] | 108 | if firstline and line[:5] == 'From ': |
Guido van Rossum | 92457b9 | 1995-06-22 19:06:57 +0000 | [diff] [blame] | 109 | self.unixfrom = self.unixfrom + line |
Jack Jansen | 3a15dca | 1995-06-13 11:19:48 +0000 | [diff] [blame] | 110 | continue |
| 111 | firstline = 0 |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 112 | if self.islast(line): |
| 113 | break |
| 114 | elif headerseen and line[0] in ' \t': |
| 115 | # It's a continuation line. |
| 116 | list.append(line) |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 117 | x = (self.dict[headerseen] + "\n " + |
| 118 | string.strip(line)) |
| 119 | self.dict[headerseen] = string.strip(x) |
| 120 | elif ':' in line: |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 121 | # It's a header line. |
| 122 | list.append(line) |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 123 | i = string.find(line, ':') |
| 124 | headerseen = string.lower(line[:i]) |
| 125 | self.dict[headerseen] = string.strip( |
| 126 | line[i+1:]) |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 127 | else: |
| 128 | # It's not a header line; stop here. |
| 129 | if not headerseen: |
| 130 | self.status = 'No headers' |
| 131 | else: |
| 132 | self.status = 'Bad header' |
| 133 | # Try to undo the read. |
Guido van Rossum | 92457b9 | 1995-06-22 19:06:57 +0000 | [diff] [blame] | 134 | if self.seekable: |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 135 | self.fp.seek(-len(line), 1) |
Guido van Rossum | 92457b9 | 1995-06-22 19:06:57 +0000 | [diff] [blame] | 136 | else: |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 137 | self.status = \ |
| 138 | self.status + '; bad seek' |
| 139 | break |
| 140 | |
| 141 | |
| 142 | # Method to determine whether a line is a legal end of |
| 143 | # RFC-822 headers. You may override this method if your |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 144 | # application wants to bend the rules, e.g. to strip trailing |
| 145 | # whitespace, or to recognise MH template separators |
| 146 | # ('--------'). For convenience (e.g. for code reading from |
| 147 | # sockets) a line consisting of \r\n also matches. |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 148 | |
| 149 | def islast(self, line): |
Guido van Rossum | 92457b9 | 1995-06-22 19:06:57 +0000 | [diff] [blame] | 150 | return line in _blanklines |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 151 | |
| 152 | |
| 153 | # Look through the list of headers and find all lines matching |
| 154 | # a given header name (and their continuation lines). |
| 155 | # A list of the lines is returned, without interpretation. |
| 156 | # If the header does not occur, an empty list is returned. |
| 157 | # If the header occurs multiple times, all occurrences are |
| 158 | # returned. Case is not important in the header name. |
| 159 | |
| 160 | def getallmatchingheaders(self, name): |
| 161 | name = string.lower(name) + ':' |
| 162 | n = len(name) |
| 163 | list = [] |
| 164 | hit = 0 |
| 165 | for line in self.headers: |
| 166 | if string.lower(line[:n]) == name: |
| 167 | hit = 1 |
| 168 | elif line[:1] not in string.whitespace: |
| 169 | hit = 0 |
| 170 | if hit: |
| 171 | list.append(line) |
| 172 | return list |
| 173 | |
| 174 | |
| 175 | # Similar, but return only the first matching header (and its |
| 176 | # continuation lines). |
| 177 | |
| 178 | def getfirstmatchingheader(self, name): |
| 179 | name = string.lower(name) + ':' |
| 180 | n = len(name) |
| 181 | list = [] |
| 182 | hit = 0 |
| 183 | for line in self.headers: |
Guido van Rossum | 3f9a6ec | 1994-08-12 13:16:50 +0000 | [diff] [blame] | 184 | if hit: |
| 185 | if line[:1] not in string.whitespace: |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 186 | break |
Guido van Rossum | 3f9a6ec | 1994-08-12 13:16:50 +0000 | [diff] [blame] | 187 | elif string.lower(line[:n]) == name: |
| 188 | hit = 1 |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 189 | if hit: |
| 190 | list.append(line) |
| 191 | return list |
| 192 | |
| 193 | |
| 194 | # A higher-level interface to getfirstmatchingheader(). |
| 195 | # Return a string containing the literal text of the header |
| 196 | # but with the keyword stripped. All leading, trailing and |
| 197 | # embedded whitespace is kept in the string, however. |
| 198 | # Return None if the header does not occur. |
| 199 | |
| 200 | def getrawheader(self, name): |
| 201 | list = self.getfirstmatchingheader(name) |
| 202 | if not list: |
| 203 | return None |
| 204 | list[0] = list[0][len(name) + 1:] |
| 205 | return string.joinfields(list, '') |
| 206 | |
| 207 | |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 208 | # The normal interface: return a stripped version of the |
| 209 | # header value with a name, or None if it doesn't exist. This |
| 210 | # uses the dictionary version which finds the *last* such |
| 211 | # header. |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 212 | |
| 213 | def getheader(self, name): |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 214 | try: |
| 215 | return self.dict[string.lower(name)] |
| 216 | except KeyError: |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 217 | return None |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 218 | |
| 219 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 220 | # Retrieve a single address from a header as a tuple, e.g. |
| 221 | # ('Guido van Rossum', 'guido@cwi.nl'). |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 222 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 223 | def getaddr(self, name): |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 224 | try: |
| 225 | data = self[name] |
| 226 | except KeyError: |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 227 | return None, None |
| 228 | return parseaddr(data) |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 230 | # Retrieve a list of addresses from a header, where each |
| 231 | # address is a tuple as returned by getaddr(). |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 233 | def getaddrlist(self, name): |
| 234 | # XXX This function is not really correct. The split |
| 235 | # on ',' might fail in the case of commas within |
| 236 | # quoted strings. |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 237 | try: |
| 238 | data = self[name] |
| 239 | except KeyError: |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 240 | return [] |
| 241 | data = string.splitfields(data, ',') |
| 242 | for i in range(len(data)): |
| 243 | data[i] = parseaddr(data[i]) |
| 244 | return data |
| 245 | |
| 246 | # Retrieve a date field from a header as a tuple compatible |
| 247 | # with time.mktime(). |
| 248 | |
| 249 | def getdate(self, name): |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 250 | try: |
| 251 | data = self[name] |
| 252 | except KeyError: |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 253 | return None |
| 254 | return parsedate(data) |
| 255 | |
Guido van Rossum | 27cb8a4 | 1996-11-20 22:12:26 +0000 | [diff] [blame] | 256 | # Retrieve a date field from a header as a 10-tuple. |
| 257 | # The first 9 elements make up a tuple compatible |
| 258 | # with time.mktime(), and the 10th is the offset |
| 259 | # of the poster's time zone from GMT/UTC. |
| 260 | |
| 261 | def getdate_tz(self, name): |
| 262 | try: |
| 263 | data = self[name] |
| 264 | except KeyError: |
| 265 | return None |
| 266 | return parsedate_tz(data) |
| 267 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 269 | # Access as a dictionary (only finds *last* header of each type): |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 270 | |
| 271 | def __len__(self): |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 272 | return len(self.dict) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 273 | |
| 274 | def __getitem__(self, name): |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 275 | return self.dict[string.lower(name)] |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 276 | |
| 277 | def has_key(self, name): |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 278 | return self.dict.has_key(string.lower(name)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 279 | |
| 280 | def keys(self): |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 281 | return self.dict.keys() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 282 | |
| 283 | def values(self): |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 284 | return self.dict.values() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 285 | |
| 286 | def items(self): |
Guido van Rossum | a13edb4 | 1996-05-28 23:08:25 +0000 | [diff] [blame] | 287 | return self.dict.items() |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 288 | |
| 289 | |
| 290 | |
| 291 | # Utility functions |
| 292 | # ----------------- |
| 293 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 294 | # XXX Should fix these to be really conformant. |
| 295 | # XXX The inverses of the parse functions may also be useful. |
| 296 | |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 297 | |
| 298 | # Remove quotes from a string. |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 299 | |
| 300 | def unquote(str): |
| 301 | if len(str) > 1: |
| 302 | if str[0] == '"' and str[-1:] == '"': |
| 303 | return str[1:-1] |
| 304 | if str[0] == '<' and str[-1:] == '>': |
| 305 | return str[1:-1] |
| 306 | return str |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 307 | |
| 308 | |
| 309 | # Parse an address into (name, address) tuple |
| 310 | |
| 311 | def parseaddr(address): |
Guido van Rossum | 3534a89 | 1996-07-30 16:29:16 +0000 | [diff] [blame] | 312 | import string |
| 313 | str = '' |
| 314 | email = '' |
| 315 | comment = '' |
| 316 | backslash = 0 |
| 317 | dquote = 0 |
| 318 | space = 0 |
| 319 | paren = 0 |
| 320 | bracket = 0 |
| 321 | seen_bracket = 0 |
| 322 | for c in address: |
| 323 | if backslash: |
| 324 | str = str + c |
| 325 | backslash = 0 |
| 326 | continue |
| 327 | if c == '\\': |
| 328 | backslash = 1 |
| 329 | continue |
| 330 | if dquote: |
| 331 | if c == '"': |
| 332 | dquote = 0 |
| 333 | else: |
| 334 | str = str + c |
| 335 | continue |
| 336 | if c == '"': |
| 337 | dquote = 1 |
| 338 | continue |
| 339 | if c in string.whitespace: |
| 340 | space = 1 |
| 341 | continue |
| 342 | if space: |
| 343 | str = str + ' ' |
| 344 | space = 0 |
| 345 | if paren: |
| 346 | if c == '(': |
| 347 | paren = paren + 1 |
| 348 | str = str + c |
| 349 | continue |
| 350 | if c == ')': |
| 351 | paren = paren - 1 |
| 352 | if paren == 0: |
| 353 | comment = comment + str |
| 354 | str = '' |
| 355 | continue |
| 356 | if c == '(': |
| 357 | paren = paren + 1 |
| 358 | if bracket: |
| 359 | email = email + str |
| 360 | str = '' |
| 361 | elif not seen_bracket: |
| 362 | email = email + str |
| 363 | str = '' |
| 364 | continue |
| 365 | if bracket: |
| 366 | if c == '>': |
| 367 | bracket = 0 |
| 368 | email = email + str |
| 369 | str = '' |
| 370 | continue |
| 371 | if c == '<': |
| 372 | bracket = 1 |
| 373 | seen_bracket = 1 |
| 374 | comment = comment + str |
| 375 | str = '' |
| 376 | email = '' |
| 377 | continue |
| 378 | if c == '#' and not bracket and not paren: |
| 379 | # rest is comment |
| 380 | break |
| 381 | str = str + c |
| 382 | if str: |
| 383 | if seen_bracket: |
| 384 | if bracket: |
| 385 | email = str |
| 386 | else: |
| 387 | comment = comment + str |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 388 | else: |
Guido van Rossum | 3534a89 | 1996-07-30 16:29:16 +0000 | [diff] [blame] | 389 | if paren: |
| 390 | comment = comment + str |
| 391 | else: |
| 392 | email = email + str |
| 393 | return string.strip(comment), string.strip(email) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 394 | |
| 395 | |
| 396 | # Parse a date field |
| 397 | |
| 398 | _monthnames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', |
| 399 | 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] |
Guido van Rossum | 9a876a4 | 1997-07-25 15:20:52 +0000 | [diff] [blame] | 400 | _daynames = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 401 | |
Guido van Rossum | 27cb8a4 | 1996-11-20 22:12:26 +0000 | [diff] [blame] | 402 | # The timezone table does not include the military time zones defined |
| 403 | # in RFC822, other than Z. According to RFC1123, the description in |
| 404 | # RFC822 gets the signs wrong, so we can't rely on any such time |
| 405 | # zones. RFC1123 recommends that numeric timezone indicators be used |
| 406 | # instead of timezone names. |
| 407 | |
| 408 | _timezones = {'UT':0, 'UTC':0, 'GMT':0, 'Z':0, |
| 409 | 'AST': -400, 'ADT': -300, # Atlantic standard |
| 410 | 'EST': -500, 'EDT': -400, # Eastern |
| 411 | 'CST': -600, 'CDT':-500, # Centreal |
| 412 | 'MST':-700, 'MDT':-600, # Mountain |
| 413 | 'PST':-800, 'PDT':-700 # Pacific |
| 414 | } |
| 415 | |
| 416 | def parsedate_tz(data): |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 417 | data = string.split(data) |
Guido van Rossum | 9a876a4 | 1997-07-25 15:20:52 +0000 | [diff] [blame] | 418 | if data[0][-1] == ',' or data[0] in _daynames: |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 419 | # There's a dayname here. Skip it |
| 420 | del data[0] |
Guido van Rossum | c17a268 | 1996-12-27 15:42:35 +0000 | [diff] [blame] | 421 | if len(data) == 3: # RFC 850 date, deprecated |
| 422 | stuff = string.split(data[0], '-') |
| 423 | if len(stuff) == 3: |
| 424 | data = stuff + data[1:] |
Guido van Rossum | 8534741 | 1994-09-09 11:10:15 +0000 | [diff] [blame] | 425 | if len(data) == 4: |
| 426 | s = data[3] |
| 427 | i = string.find(s, '+') |
| 428 | if i > 0: |
| 429 | data[3:] = [s[:i], s[i+1:]] |
| 430 | else: |
| 431 | data.append('') # Dummy tz |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 432 | if len(data) < 5: |
| 433 | return None |
| 434 | data = data[:5] |
| 435 | [dd, mm, yy, tm, tz] = data |
| 436 | if not mm in _monthnames: |
Guido van Rossum | 9a876a4 | 1997-07-25 15:20:52 +0000 | [diff] [blame] | 437 | dd, mm, yy, tm, tz = mm, dd, tm, yy, tz |
| 438 | if not mm in _monthnames: |
| 439 | return None |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 440 | mm = _monthnames.index(mm)+1 |
| 441 | tm = string.splitfields(tm, ':') |
| 442 | if len(tm) == 2: |
| 443 | [thh, tmm] = tm |
| 444 | tss = '0' |
| 445 | else: |
| 446 | [thh, tmm, tss] = tm |
| 447 | try: |
| 448 | yy = string.atoi(yy) |
| 449 | dd = string.atoi(dd) |
| 450 | thh = string.atoi(thh) |
| 451 | tmm = string.atoi(tmm) |
| 452 | tss = string.atoi(tss) |
| 453 | except string.atoi_error: |
| 454 | return None |
Guido van Rossum | 27cb8a4 | 1996-11-20 22:12:26 +0000 | [diff] [blame] | 455 | tzoffset=0 |
| 456 | tz=string.upper(tz) |
| 457 | if _timezones.has_key(tz): |
| 458 | tzoffset=_timezones[tz] |
| 459 | else: |
| 460 | try: |
| 461 | tzoffset=string.atoi(tz) |
| 462 | except string.atoi_error: |
| 463 | pass |
| 464 | # Convert a timezone offset into seconds ; -0500 -> -18000 |
| 465 | if tzoffset<0: tzsign=-1 |
| 466 | else: tzsign=1 |
| 467 | tzoffset=tzoffset*tzsign |
| 468 | tzoffset = tzsign * ( (tzoffset/100)*3600 + (tzoffset % 100)*60) |
| 469 | tuple = (yy, mm, dd, thh, tmm, tss, 0, 0, 0, tzoffset) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 470 | return tuple |
| 471 | |
Guido van Rossum | 27cb8a4 | 1996-11-20 22:12:26 +0000 | [diff] [blame] | 472 | def parsedate(data): |
| 473 | t=parsedate_tz(data) |
| 474 | if type(t)==type( () ): |
| 475 | return t[:9] |
| 476 | else: return t |
| 477 | |
Guido van Rossum | 6cdd7a0 | 1996-12-12 18:39:54 +0000 | [diff] [blame] | 478 | def mktime_tz(data): |
| 479 | """Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp. |
| 480 | |
| 481 | Minor glitch: this first interprets the first 8 elements as a |
| 482 | local time and then compensates for the timezone difference; |
| 483 | this may yield a slight error around daylight savings time |
| 484 | switch dates. Not enough to worry about for common use. |
| 485 | |
| 486 | """ |
| 487 | t = time.mktime(data[:8] + (0,)) |
| 488 | return t + data[9] - time.timezone |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 489 | |
| 490 | # When used as script, run a small test program. |
| 491 | # The first command line argument must be a filename containing one |
| 492 | # message in RFC-822 format. |
| 493 | |
| 494 | if __name__ == '__main__': |
Guido van Rossum | 3534a89 | 1996-07-30 16:29:16 +0000 | [diff] [blame] | 495 | import sys, os |
Guido van Rossum | 27cb8a4 | 1996-11-20 22:12:26 +0000 | [diff] [blame] | 496 | file = os.path.join(os.environ['HOME'], 'Mail/inbox/1') |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 497 | if sys.argv[1:]: file = sys.argv[1] |
| 498 | f = open(file, 'r') |
| 499 | m = Message(f) |
| 500 | print 'From:', m.getaddr('from') |
| 501 | print 'To:', m.getaddrlist('to') |
| 502 | print 'Subject:', m.getheader('subject') |
| 503 | print 'Date:', m.getheader('date') |
Guido van Rossum | 27cb8a4 | 1996-11-20 22:12:26 +0000 | [diff] [blame] | 504 | date = m.getdate_tz('date') |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 505 | if date: |
Guido van Rossum | 27cb8a4 | 1996-11-20 22:12:26 +0000 | [diff] [blame] | 506 | print 'ParsedDate:', time.asctime(date[:-1]), |
| 507 | hhmmss = date[-1] |
| 508 | hhmm, ss = divmod(hhmmss, 60) |
| 509 | hh, mm = divmod(hhmm, 60) |
| 510 | print "%+03d%02d" % (hh, mm), |
| 511 | if ss: print ".%02d" % ss, |
| 512 | print |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 513 | else: |
| 514 | print 'ParsedDate:', None |
| 515 | m.rewindbody() |
| 516 | n = 0 |
| 517 | while f.readline(): |
| 518 | n = n + 1 |
| 519 | print 'Lines:', n |
| 520 | print '-'*70 |
| 521 | print 'len =', len(m) |
| 522 | if m.has_key('Date'): print 'Date =', m['Date'] |
| 523 | if m.has_key('X-Nonsense'): pass |
| 524 | print 'keys =', m.keys() |
| 525 | print 'values =', m.values() |
| 526 | print 'items =', m.items() |
| 527 | |