blob: 5821ddf09058d5a3261fe17c35df9535e2fbf93d [file] [log] [blame]
Barry Warsaw602426e2006-02-03 04:44:52 +00001# Copyright (C) 2002-2006 Python Software Foundation
Barry Warsawbb113862004-10-03 03:16:19 +00002# Contact: email-sig@python.org
Barry Warsaw030ddf72002-11-05 19:54:52 +00003
4"""Email address parsing code.
5
6Lifted directly from rfc822.py. This should eventually be rewritten.
7"""
8
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009__all__ = [
10 'mktime_tz',
11 'parsedate',
12 'parsedate_tz',
13 'quote',
14 ]
15
Barry Warsaw030ddf72002-11-05 19:54:52 +000016import time
Barry Warsaw5c8fef92002-12-30 16:43:42 +000017
18SPACE = ' '
19EMPTYSTRING = ''
20COMMASPACE = ', '
Barry Warsaw030ddf72002-11-05 19:54:52 +000021
22# Parse a date field
23_monthnames = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul',
24 'aug', 'sep', 'oct', 'nov', 'dec',
25 'january', 'february', 'march', 'april', 'may', 'june', 'july',
26 'august', 'september', 'october', 'november', 'december']
27
28_daynames = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
29
30# The timezone table does not include the military time zones defined
31# in RFC822, other than Z. According to RFC1123, the description in
32# RFC822 gets the signs wrong, so we can't rely on any such time
33# zones. RFC1123 recommends that numeric timezone indicators be used
34# instead of timezone names.
35
36_timezones = {'UT':0, 'UTC':0, 'GMT':0, 'Z':0,
37 'AST': -400, 'ADT': -300, # Atlantic (used in Canada)
38 'EST': -500, 'EDT': -400, # Eastern
39 'CST': -600, 'CDT': -500, # Central
40 'MST': -700, 'MDT': -600, # Mountain
41 'PST': -800, 'PDT': -700 # Pacific
42 }
43
44
45def parsedate_tz(data):
46 """Convert a date string to a time tuple.
47
48 Accounts for military timezones.
49 """
50 data = data.split()
Barry Warsawba976592002-12-30 17:21:36 +000051 # The FWS after the comma after the day-of-week is optional, so search and
52 # adjust for this.
53 if data[0].endswith(',') or data[0].lower() in _daynames:
Barry Warsaw030ddf72002-11-05 19:54:52 +000054 # There's a dayname here. Skip it
55 del data[0]
Barry Warsawba976592002-12-30 17:21:36 +000056 else:
57 i = data[0].rfind(',')
Barry Warsawb5dc39f2003-05-08 03:33:15 +000058 if i >= 0:
59 data[0] = data[0][i+1:]
Barry Warsaw030ddf72002-11-05 19:54:52 +000060 if len(data) == 3: # RFC 850 date, deprecated
61 stuff = data[0].split('-')
62 if len(stuff) == 3:
63 data = stuff + data[1:]
64 if len(data) == 4:
65 s = data[3]
66 i = s.find('+')
67 if i > 0:
68 data[3:] = [s[:i], s[i+1:]]
69 else:
70 data.append('') # Dummy tz
71 if len(data) < 5:
72 return None
73 data = data[:5]
74 [dd, mm, yy, tm, tz] = data
75 mm = mm.lower()
Barry Warsaw5c8fef92002-12-30 16:43:42 +000076 if mm not in _monthnames:
Barry Warsaw030ddf72002-11-05 19:54:52 +000077 dd, mm = mm, dd.lower()
Barry Warsaw5c8fef92002-12-30 16:43:42 +000078 if mm not in _monthnames:
Barry Warsaw030ddf72002-11-05 19:54:52 +000079 return None
Barry Warsaw5c8fef92002-12-30 16:43:42 +000080 mm = _monthnames.index(mm) + 1
81 if mm > 12:
82 mm -= 12
Barry Warsaw030ddf72002-11-05 19:54:52 +000083 if dd[-1] == ',':
84 dd = dd[:-1]
85 i = yy.find(':')
86 if i > 0:
87 yy, tm = tm, yy
88 if yy[-1] == ',':
89 yy = yy[:-1]
90 if not yy[0].isdigit():
91 yy, tz = tz, yy
92 if tm[-1] == ',':
93 tm = tm[:-1]
94 tm = tm.split(':')
95 if len(tm) == 2:
96 [thh, tmm] = tm
97 tss = '0'
98 elif len(tm) == 3:
99 [thh, tmm, tss] = tm
100 else:
101 return None
102 try:
103 yy = int(yy)
104 dd = int(dd)
105 thh = int(thh)
106 tmm = int(tmm)
107 tss = int(tss)
108 except ValueError:
109 return None
110 tzoffset = None
111 tz = tz.upper()
112 if _timezones.has_key(tz):
113 tzoffset = _timezones[tz]
114 else:
115 try:
116 tzoffset = int(tz)
117 except ValueError:
118 pass
119 # Convert a timezone offset into seconds ; -0500 -> -18000
120 if tzoffset:
121 if tzoffset < 0:
122 tzsign = -1
123 tzoffset = -tzoffset
124 else:
125 tzsign = 1
Barry Warsawbb113862004-10-03 03:16:19 +0000126 tzoffset = tzsign * ( (tzoffset//100)*3600 + (tzoffset % 100)*60)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000127 # Daylight Saving Time flag is set to -1, since DST is unknown.
128 return yy, mm, dd, thh, tmm, tss, 0, 1, -1, tzoffset
Barry Warsaw030ddf72002-11-05 19:54:52 +0000129
130
131def parsedate(data):
132 """Convert a time string to a time tuple."""
133 t = parsedate_tz(data)
Barry Warsaw24f79762004-05-09 03:55:11 +0000134 if isinstance(t, tuple):
Barry Warsaw030ddf72002-11-05 19:54:52 +0000135 return t[:9]
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000136 else:
137 return t
Barry Warsaw030ddf72002-11-05 19:54:52 +0000138
139
140def mktime_tz(data):
141 """Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp."""
142 if data[9] is None:
143 # No zone info, so localtime is better assumption than GMT
144 return time.mktime(data[:8] + (-1,))
145 else:
146 t = time.mktime(data[:8] + (0,))
147 return t - data[9] - time.timezone
148
149
150def quote(str):
151 """Add quotes around a string."""
152 return str.replace('\\', '\\\\').replace('"', '\\"')
153
154
155class AddrlistClass:
156 """Address parser class by Ben Escoto.
157
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000158 To understand what this class does, it helps to have a copy of RFC 2822 in
159 front of you.
Barry Warsaw030ddf72002-11-05 19:54:52 +0000160
161 Note: this class interface is deprecated and may be removed in the future.
162 Use rfc822.AddressList instead.
163 """
164
165 def __init__(self, field):
166 """Initialize a new instance.
167
168 `field' is an unparsed address header field, containing
169 one or more addresses.
170 """
171 self.specials = '()<>@,:;.\"[]'
172 self.pos = 0
173 self.LWS = ' \t'
174 self.CR = '\r\n'
175 self.atomends = self.specials + self.LWS + self.CR
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000176 # Note that RFC 2822 now specifies `.' as obs-phrase, meaning that it
177 # is obsolete syntax. RFC 2822 requires that we recognize obsolete
178 # syntax, so allow dots in phrases.
179 self.phraseends = self.atomends.replace('.', '')
Barry Warsaw030ddf72002-11-05 19:54:52 +0000180 self.field = field
181 self.commentlist = []
182
183 def gotonext(self):
184 """Parse up to the start of the next address."""
185 while self.pos < len(self.field):
186 if self.field[self.pos] in self.LWS + '\n\r':
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000187 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000188 elif self.field[self.pos] == '(':
189 self.commentlist.append(self.getcomment())
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000190 else:
191 break
Barry Warsaw030ddf72002-11-05 19:54:52 +0000192
193 def getaddrlist(self):
194 """Parse all addresses.
195
196 Returns a list containing all of the addresses.
197 """
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000198 result = []
Barry Warsawfa348c82003-03-17 18:35:42 +0000199 while self.pos < len(self.field):
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000200 ad = self.getaddress()
201 if ad:
202 result += ad
203 else:
Barry Warsawfa348c82003-03-17 18:35:42 +0000204 result.append(('', ''))
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000205 return result
Barry Warsaw030ddf72002-11-05 19:54:52 +0000206
207 def getaddress(self):
208 """Parse the next address."""
209 self.commentlist = []
210 self.gotonext()
211
212 oldpos = self.pos
213 oldcl = self.commentlist
214 plist = self.getphraselist()
215
216 self.gotonext()
217 returnlist = []
218
219 if self.pos >= len(self.field):
220 # Bad email address technically, no domain.
221 if plist:
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000222 returnlist = [(SPACE.join(self.commentlist), plist[0])]
Barry Warsaw030ddf72002-11-05 19:54:52 +0000223
224 elif self.field[self.pos] in '.@':
225 # email address is just an addrspec
226 # this isn't very efficient since we start over
227 self.pos = oldpos
228 self.commentlist = oldcl
229 addrspec = self.getaddrspec()
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000230 returnlist = [(SPACE.join(self.commentlist), addrspec)]
Barry Warsaw030ddf72002-11-05 19:54:52 +0000231
232 elif self.field[self.pos] == ':':
233 # address is a group
234 returnlist = []
235
236 fieldlen = len(self.field)
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000237 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000238 while self.pos < len(self.field):
239 self.gotonext()
240 if self.pos < fieldlen and self.field[self.pos] == ';':
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000241 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000242 break
243 returnlist = returnlist + self.getaddress()
244
245 elif self.field[self.pos] == '<':
246 # Address is a phrase then a route addr
247 routeaddr = self.getrouteaddr()
248
249 if self.commentlist:
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000250 returnlist = [(SPACE.join(plist) + ' (' +
251 ' '.join(self.commentlist) + ')', routeaddr)]
252 else:
253 returnlist = [(SPACE.join(plist), routeaddr)]
Barry Warsaw030ddf72002-11-05 19:54:52 +0000254
255 else:
256 if plist:
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000257 returnlist = [(SPACE.join(self.commentlist), plist[0])]
Barry Warsaw030ddf72002-11-05 19:54:52 +0000258 elif self.field[self.pos] in self.specials:
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000259 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000260
261 self.gotonext()
262 if self.pos < len(self.field) and self.field[self.pos] == ',':
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000263 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000264 return returnlist
265
266 def getrouteaddr(self):
267 """Parse a route address (Return-path value).
268
269 This method just skips all the route stuff and returns the addrspec.
270 """
271 if self.field[self.pos] != '<':
272 return
273
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000274 expectroute = False
275 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000276 self.gotonext()
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000277 adlist = ''
Barry Warsaw030ddf72002-11-05 19:54:52 +0000278 while self.pos < len(self.field):
279 if expectroute:
280 self.getdomain()
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000281 expectroute = False
Barry Warsaw030ddf72002-11-05 19:54:52 +0000282 elif self.field[self.pos] == '>':
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000283 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000284 break
285 elif self.field[self.pos] == '@':
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000286 self.pos += 1
287 expectroute = True
Barry Warsaw030ddf72002-11-05 19:54:52 +0000288 elif self.field[self.pos] == ':':
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000289 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000290 else:
291 adlist = self.getaddrspec()
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000292 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000293 break
294 self.gotonext()
295
296 return adlist
297
298 def getaddrspec(self):
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000299 """Parse an RFC 2822 addr-spec."""
Barry Warsaw030ddf72002-11-05 19:54:52 +0000300 aslist = []
301
302 self.gotonext()
303 while self.pos < len(self.field):
304 if self.field[self.pos] == '.':
305 aslist.append('.')
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000306 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000307 elif self.field[self.pos] == '"':
308 aslist.append('"%s"' % self.getquote())
309 elif self.field[self.pos] in self.atomends:
310 break
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000311 else:
312 aslist.append(self.getatom())
Barry Warsaw030ddf72002-11-05 19:54:52 +0000313 self.gotonext()
314
315 if self.pos >= len(self.field) or self.field[self.pos] != '@':
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000316 return EMPTYSTRING.join(aslist)
Barry Warsaw030ddf72002-11-05 19:54:52 +0000317
318 aslist.append('@')
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000319 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000320 self.gotonext()
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000321 return EMPTYSTRING.join(aslist) + self.getdomain()
Barry Warsaw030ddf72002-11-05 19:54:52 +0000322
323 def getdomain(self):
324 """Get the complete domain name from an address."""
325 sdlist = []
326 while self.pos < len(self.field):
327 if self.field[self.pos] in self.LWS:
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000328 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000329 elif self.field[self.pos] == '(':
330 self.commentlist.append(self.getcomment())
331 elif self.field[self.pos] == '[':
332 sdlist.append(self.getdomainliteral())
333 elif self.field[self.pos] == '.':
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000334 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000335 sdlist.append('.')
336 elif self.field[self.pos] in self.atomends:
337 break
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000338 else:
339 sdlist.append(self.getatom())
340 return EMPTYSTRING.join(sdlist)
Barry Warsaw030ddf72002-11-05 19:54:52 +0000341
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000342 def getdelimited(self, beginchar, endchars, allowcomments=True):
Barry Warsaw030ddf72002-11-05 19:54:52 +0000343 """Parse a header fragment delimited by special characters.
344
345 `beginchar' is the start character for the fragment.
346 If self is not looking at an instance of `beginchar' then
347 getdelimited returns the empty string.
348
349 `endchars' is a sequence of allowable end-delimiting characters.
350 Parsing stops when one of these is encountered.
351
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000352 If `allowcomments' is non-zero, embedded RFC 2822 comments are allowed
353 within the parsed fragment.
Barry Warsaw030ddf72002-11-05 19:54:52 +0000354 """
355 if self.field[self.pos] != beginchar:
356 return ''
357
358 slist = ['']
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000359 quote = False
360 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000361 while self.pos < len(self.field):
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000362 if quote:
Barry Warsaw030ddf72002-11-05 19:54:52 +0000363 slist.append(self.field[self.pos])
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000364 quote = False
Barry Warsaw030ddf72002-11-05 19:54:52 +0000365 elif self.field[self.pos] in endchars:
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000366 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000367 break
368 elif allowcomments and self.field[self.pos] == '(':
369 slist.append(self.getcomment())
370 elif self.field[self.pos] == '\\':
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000371 quote = True
Barry Warsaw030ddf72002-11-05 19:54:52 +0000372 else:
373 slist.append(self.field[self.pos])
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000374 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000375
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000376 return EMPTYSTRING.join(slist)
Barry Warsaw030ddf72002-11-05 19:54:52 +0000377
378 def getquote(self):
379 """Get a quote-delimited fragment from self's field."""
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000380 return self.getdelimited('"', '"\r', False)
Barry Warsaw030ddf72002-11-05 19:54:52 +0000381
382 def getcomment(self):
383 """Get a parenthesis-delimited fragment from self's field."""
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000384 return self.getdelimited('(', ')\r', True)
Barry Warsaw030ddf72002-11-05 19:54:52 +0000385
386 def getdomainliteral(self):
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000387 """Parse an RFC 2822 domain-literal."""
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000388 return '[%s]' % self.getdelimited('[', ']\r', False)
Barry Warsaw030ddf72002-11-05 19:54:52 +0000389
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000390 def getatom(self, atomends=None):
391 """Parse an RFC 2822 atom.
392
393 Optional atomends specifies a different set of end token delimiters
394 (the default is to use self.atomends). This is used e.g. in
395 getphraselist() since phrase endings must not include the `.' (which
396 is legal in phrases)."""
Barry Warsaw030ddf72002-11-05 19:54:52 +0000397 atomlist = ['']
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000398 if atomends is None:
399 atomends = self.atomends
Barry Warsaw030ddf72002-11-05 19:54:52 +0000400
401 while self.pos < len(self.field):
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000402 if self.field[self.pos] in atomends:
Barry Warsaw030ddf72002-11-05 19:54:52 +0000403 break
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000404 else:
405 atomlist.append(self.field[self.pos])
406 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000407
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000408 return EMPTYSTRING.join(atomlist)
Barry Warsaw030ddf72002-11-05 19:54:52 +0000409
410 def getphraselist(self):
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000411 """Parse a sequence of RFC 2822 phrases.
Barry Warsaw030ddf72002-11-05 19:54:52 +0000412
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000413 A phrase is a sequence of words, which are in turn either RFC 2822
414 atoms or quoted-strings. Phrases are canonicalized by squeezing all
415 runs of continuous whitespace into one space.
Barry Warsaw030ddf72002-11-05 19:54:52 +0000416 """
417 plist = []
418
419 while self.pos < len(self.field):
420 if self.field[self.pos] in self.LWS:
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000421 self.pos += 1
Barry Warsaw030ddf72002-11-05 19:54:52 +0000422 elif self.field[self.pos] == '"':
423 plist.append(self.getquote())
424 elif self.field[self.pos] == '(':
425 self.commentlist.append(self.getcomment())
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000426 elif self.field[self.pos] in self.phraseends:
Barry Warsaw030ddf72002-11-05 19:54:52 +0000427 break
Barry Warsaw5c8fef92002-12-30 16:43:42 +0000428 else:
429 plist.append(self.getatom(self.phraseends))
Barry Warsaw030ddf72002-11-05 19:54:52 +0000430
431 return plist
432
433class AddressList(AddrlistClass):
Barry Warsaw1fb22bb2002-12-30 16:21:07 +0000434 """An AddressList encapsulates a list of parsed RFC 2822 addresses."""
Barry Warsaw030ddf72002-11-05 19:54:52 +0000435 def __init__(self, field):
436 AddrlistClass.__init__(self, field)
437 if field:
438 self.addresslist = self.getaddrlist()
439 else:
440 self.addresslist = []
441
442 def __len__(self):
443 return len(self.addresslist)
444
Barry Warsaw030ddf72002-11-05 19:54:52 +0000445 def __add__(self, other):
446 # Set union
447 newaddr = AddressList(None)
448 newaddr.addresslist = self.addresslist[:]
449 for x in other.addresslist:
450 if not x in self.addresslist:
451 newaddr.addresslist.append(x)
452 return newaddr
453
454 def __iadd__(self, other):
455 # Set union, in-place
456 for x in other.addresslist:
457 if not x in self.addresslist:
458 self.addresslist.append(x)
459 return self
460
461 def __sub__(self, other):
462 # Set difference
463 newaddr = AddressList(None)
464 for x in self.addresslist:
465 if not x in other.addresslist:
466 newaddr.addresslist.append(x)
467 return newaddr
468
469 def __isub__(self, other):
470 # Set difference, in-place
471 for x in other.addresslist:
472 if x in self.addresslist:
473 self.addresslist.remove(x)
474 return self
475
476 def __getitem__(self, index):
477 # Make indexing, slices, and 'in' work
478 return self.addresslist[index]