Debian Codegraph @

Lib/email/_parseaddr.py

raw
1 # Copyright (C) 2002-2007 Python Software Foundation
2 # Contact: [email protected]
3
4 """Email address parsing code.
5
6 Lifted directly from rfc822.py. This should eventually be rewritten.
7 """
8
9 __all__ = [
10 'mktime_tz',
11 'parsedate',
12 'parsedate_tz',
13 'quote',
14 ]
15
16 import time
17
18 SPACE = ' '
19 EMPTYSTRING = ''
20 COMMASPACE = ', '
21
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
45 def parsedate_tz(data):
46 """Convert a date string to a time tuple.
47
48 Accounts for military timezones.
49 """
50 res = _parsedate_tz(data)
51 if not res:
52 return
53 if res[9] is None:
54 res[9] = 0
55 return tuple(res)
56
57 def _parsedate_tz(data):
58 """Convert date to extended time tuple.
59
60 The last (additional) element is the time zone offset in seconds, except if
61 the timezone was specified as -0000. In that case the last element is
62 None. This indicates a UTC timestamp that explicitly disclaims knowledge of
63 the source timezone, as opposed to a +0000 timestamp that indicates the
64 source timezone really was UTC.
65
66 """
67 if not data:
68 return None
69 data = data.split()
70 if not data: # This happens for whitespace-only input.
71 return None
72 # The FWS after the comma after the day-of-week is optional, so search and
73 # adjust for this.
74 if data[0].endswith(',') or data[0].lower() in _daynames:
75 # There's a dayname here. Skip it
76 del data[0]
77 else:
78 i = data[0].rfind(',')
79 if i >= 0:
80 data[0] = data[0][i+1:]
81 if len(data) == 3: # RFC 850 date, deprecated
82 stuff = data[0].split('-')
83 if len(stuff) == 3:
84 data = stuff + data[1:]
85 if len(data) == 4:
86 s = data[3]
87 i = s.find('+')
88 if i == -1:
89 i = s.find('-')
90 if i > 0:
91 data[3:] = [s[:i], s[i:]]
92 else:
93 data.append('') # Dummy tz
94 if len(data) < 5:
95 return None
96 data = data[:5]
97 [dd, mm, yy, tm, tz] = data
98 if not (dd and mm and yy):
99 return None
100 mm = mm.lower()
101 if mm not in _monthnames:
102 dd, mm = mm, dd.lower()
103 if mm not in _monthnames:
104 return None
105 mm = _monthnames.index(mm) + 1
106 if mm > 12:
107 mm -= 12
108 if dd[-1] == ',':
109 dd = dd[:-1]
110 i = yy.find(':')
111 if i > 0:
112 yy, tm = tm, yy
113 if yy[-1] == ',':
114 yy = yy[:-1]
115 if not yy:
116 return None
117 if not yy[0].isdigit():
118 yy, tz = tz, yy
119 if tm[-1] == ',':
120 tm = tm[:-1]
121 tm = tm.split(':')
122 if len(tm) == 2:
123 [thh, tmm] = tm
124 tss = '0'
125 elif len(tm) == 3:
126 [thh, tmm, tss] = tm
127 elif len(tm) == 1 and '.' in tm[0]:
128 # Some non-compliant MUAs use '.' to separate time elements.
129 tm = tm[0].split('.')
130 if len(tm) == 2:
131 [thh, tmm] = tm
132 tss = 0
133 elif len(tm) == 3:
134 [thh, tmm, tss] = tm
135 else:
136 return None
137 else:
138 return None
139 try:
140 yy = int(yy)
141 dd = int(dd)
142 thh = int(thh)
143 tmm = int(tmm)
144 tss = int(tss)
145 except ValueError:
146 return None
147 # Check for a yy specified in two-digit format, then convert it to the
148 # appropriate four-digit format, according to the POSIX standard. RFC 822
149 # calls for a two-digit yy, but RFC 2822 (which obsoletes RFC 822) already
150 # mandated a 4-digit yy, and RFC 5322 (which obsoletes RFC 2822) continues
151 # this requirement. For more information, see the documentation for
152 # the time module.
153 if yy < 100:
154 # The year is between 1969 and 1999 (inclusive).
155 if yy > 68:
156 yy += 1900
157 # The year is between 2000 and 2068 (inclusive).
158 else:
159 yy += 2000
160 tzoffset = None
161 tz = tz.upper()
162 if tz in _timezones:
163 tzoffset = _timezones[tz]
164 else:
165 try:
166 tzoffset = int(tz)
167 except ValueError:
168 pass
169 if tzoffset==0 and tz.startswith('-'):
170 tzoffset = None
171 # Convert a timezone offset into seconds ; -0500 -> -18000
172 if tzoffset:
173 if tzoffset < 0:
174 tzsign = -1
175 tzoffset = -tzoffset
176 else:
177 tzsign = 1
178 tzoffset = tzsign * ( (tzoffset//100)*3600 + (tzoffset % 100)*60)
179 # Daylight Saving Time flag is set to -1, since DST is unknown.
180 return [yy, mm, dd, thh, tmm, tss, 0, 1, -1, tzoffset]
181
182
183 def parsedate(data):
184 """Convert a time string to a time tuple."""
185 t = parsedate_tz(data)
186 if isinstance(t, tuple):
187 return t[:9]
188 else:
189 return t
190
191
192 def mktime_tz(data):
193 """Turn a 10-tuple as returned by parsedate_tz() into a POSIX timestamp."""
194 if data[9] is None:
195 # No zone info, so localtime is better assumption than GMT
196 return time.mktime(data[:8] + (-1,))
197 else:
198 # Delay the import, since mktime_tz is rarely used
199 import calendar
200
201 t = calendar.timegm(data)
202 return t - data[9]
203
204
205 def quote(str):
206 """Prepare string to be used in a quoted string.
207
208 Turns backslash and double quote characters into quoted pairs. These
209 are the only characters that need to be quoted inside a quoted string.
210 Does not add the surrounding double quotes.
211 """
212 return str.replace('\\', '\\\\').replace('"', '\\"')
213
214
215 class AddrlistClass:
216 """Address parser class by Ben Escoto.
217
218 To understand what this class does, it helps to have a copy of RFC 2822 in
219 front of you.
220
221 Note: this class interface is deprecated and may be removed in the future.
222 Use email.utils.AddressList instead.
223 """
224
225 def __init__(self, field):
226 """Initialize a new instance.
227
228 `field' is an unparsed address header field, containing
229 one or more addresses.
230 """
231 self.specials = '()<>@,:;.\"[]'
232 self.pos = 0
233 self.LWS = ' \t'
234 self.CR = '\r\n'
235 self.FWS = self.LWS + self.CR
236 self.atomends = self.specials + self.LWS + self.CR
237 # Note that RFC 2822 section 4.1 introduced '.' as obs-phrase to handle
238 # existing practice (periods in display names), even though it was not
239 # allowed in RFC 822. RFC 5322 section 4.1 (which obsoletes RFC 2822)
240 # continues this requirement. We must recognize obsolete syntax, so
241 # allow dots in phrases.
242 self.phraseends = self.atomends.replace('.', '')
243 self.field = field
244 self.commentlist = []
245
246 def gotonext(self):
247 """Skip white space and extract comments."""
248 wslist = []
249 while self.pos < len(self.field):
250 if self.field[self.pos] in self.LWS + '\n\r':
251 if self.field[self.pos] not in '\n\r':
252 wslist.append(self.field[self.pos])
253 self.pos += 1
254 elif self.field[self.pos] == '(':
255 self.commentlist.append(self.getcomment())
256 else:
257 break
258 return EMPTYSTRING.join(wslist)
259
260 def getaddrlist(self):
261 """Parse all addresses.
262
263 Returns a list containing all of the addresses.
264 """
265 result = []
266 while self.pos < len(self.field):
267 ad = self.getaddress()
268 if ad:
269 result += ad
270 else:
271 result.append(('', ''))
272 return result
273
274 def getaddress(self):
275 """Parse the next address."""
276 self.commentlist = []
277 self.gotonext()
278
279 oldpos = self.pos
280 oldcl = self.commentlist
281 plist = self.getphraselist()
282
283 self.gotonext()
284 returnlist = []
285
286 if self.pos >= len(self.field):
287 # Bad email address technically, no domain.
288 if plist:
289 returnlist = [(SPACE.join(self.commentlist), plist[0])]
290
291 elif self.field[self.pos] in '.@':
292 # email address is just an addrspec
293 # this isn't very efficient since we start over
294 self.pos = oldpos
295 self.commentlist = oldcl
296 addrspec = self.getaddrspec()
297 returnlist = [(SPACE.join(self.commentlist), addrspec)]
298
299 elif self.field[self.pos] == ':':
300 # address is a group
301 returnlist = []
302
303 fieldlen = len(self.field)
304 self.pos += 1
305 while self.pos < len(self.field):
306 self.gotonext()
307 if self.pos < fieldlen and self.field[self.pos] == ';':
308 self.pos += 1
309 break
310 returnlist = returnlist + self.getaddress()
311
312 elif self.field[self.pos] == '<':
313 # Address is a phrase then a route addr
314 routeaddr = self.getrouteaddr()
315
316 if self.commentlist:
317 returnlist = [(SPACE.join(plist) + ' (' +
318 ' '.join(self.commentlist) + ')', routeaddr)]
319 else:
320 returnlist = [(SPACE.join(plist), routeaddr)]
321
322 else:
323 if plist:
324 returnlist = [(SPACE.join(self.commentlist), plist[0])]
325 elif self.field[self.pos] in self.specials:
326 self.pos += 1
327
328 self.gotonext()
329 if self.pos < len(self.field) and self.field[self.pos] == ',':
330 self.pos += 1
331 return returnlist
332
333 def getrouteaddr(self):
334 """Parse a route address (Return-path value).
335
336 This method just skips all the route stuff and returns the addrspec.
337 """
338 if self.field[self.pos] != '<':
339 return
340
341 expectroute = False
342 self.pos += 1
343 self.gotonext()
344 adlist = ''
345 while self.pos < len(self.field):
346 if expectroute:
347 self.getdomain()
348 expectroute = False
349 elif self.field[self.pos] == '>':
350 self.pos += 1
351 break
352 elif self.field[self.pos] == '@':
353 self.pos += 1
354 expectroute = True
355 elif self.field[self.pos] == ':':
356 self.pos += 1
357 else:
358 adlist = self.getaddrspec()
359 self.pos += 1
360 break
361 self.gotonext()
362
363 return adlist
364
365 def getaddrspec(self):
366 """Parse an RFC 2822 addr-spec."""
367 aslist = []
368
369 self.gotonext()
370 while self.pos < len(self.field):
371 preserve_ws = True
372 if self.field[self.pos] == '.':
373 if aslist and not aslist[-1].strip():
374 aslist.pop()
375 aslist.append('.')
376 self.pos += 1
377 preserve_ws = False
378 elif self.field[self.pos] == '"':
379 aslist.append('"%s"' % quote(self.getquote()))
380 elif self.field[self.pos] in self.atomends:
381 if aslist and not aslist[-1].strip():
382 aslist.pop()
383 break
384 else:
385 aslist.append(self.getatom())
386 ws = self.gotonext()
387 if preserve_ws and ws:
388 aslist.append(ws)
389
390 if self.pos >= len(self.field) or self.field[self.pos] != '@':
391 return EMPTYSTRING.join(aslist)
392
393 aslist.append('@')
394 self.pos += 1
395 self.gotonext()
396 domain = self.getdomain()
397 if not domain:
398 # Invalid domain, return an empty address instead of returning a
399 # local part to denote failed parsing.
400 return EMPTYSTRING
401 return EMPTYSTRING.join(aslist) + domain
402
403 def getdomain(self):
404 """Get the complete domain name from an address."""
405 sdlist = []
406 while self.pos < len(self.field):
407 if self.field[self.pos] in self.LWS:
408 self.pos += 1
409 elif self.field[self.pos] == '(':
410 self.commentlist.append(self.getcomment())
411 elif self.field[self.pos] == '[':
412 sdlist.append(self.getdomainliteral())
413 elif self.field[self.pos] == '.':
414 self.pos += 1
415 sdlist.append('.')
416 elif self.field[self.pos] == '@':
417 # bpo-34155: Don't parse domains with two `@` like
418 # `[email protected]@important.com`.
419 return EMPTYSTRING
420 elif self.field[self.pos] in self.atomends:
421 break
422 else:
423 sdlist.append(self.getatom())
424 return EMPTYSTRING.join(sdlist)
425
426 def getdelimited(self, beginchar, endchars, allowcomments=True):
427 """Parse a header fragment delimited by special characters.
428
429 `beginchar' is the start character for the fragment.
430 If self is not looking at an instance of `beginchar' then
431 getdelimited returns the empty string.
432
433 `endchars' is a sequence of allowable end-delimiting characters.
434 Parsing stops when one of these is encountered.
435
436 If `allowcomments' is non-zero, embedded RFC 2822 comments are allowed
437 within the parsed fragment.
438 """
439 if self.field[self.pos] != beginchar:
440 return ''
441
442 slist = ['']
443 quote = False
444 self.pos += 1
445 while self.pos < len(self.field):
446 if quote:
447 slist.append(self.field[self.pos])
448 quote = False
449 elif self.field[self.pos] in endchars:
450 self.pos += 1
451 break
452 elif allowcomments and self.field[self.pos] == '(':
453 slist.append(self.getcomment())
454 continue # have already advanced pos from getcomment
455 elif self.field[self.pos] == '\\':
456 quote = True
457 else:
458 slist.append(self.field[self.pos])
459 self.pos += 1
460
461 return EMPTYSTRING.join(slist)
462
463 def getquote(self):
464 """Get a quote-delimited fragment from self's field."""
465 return self.getdelimited('"', '"\r', False)
466
467 def getcomment(self):
468 """Get a parenthesis-delimited fragment from self's field."""
469 return self.getdelimited('(', ')\r', True)
470
471 def getdomainliteral(self):
472 """Parse an RFC 2822 domain-literal."""
473 return '[%s]' % self.getdelimited('[', ']\r', False)
474
475 def getatom(self, atomends=None):
476 """Parse an RFC 2822 atom.
477
478 Optional atomends specifies a different set of end token delimiters
479 (the default is to use self.atomends). This is used e.g. in
480 getphraselist() since phrase endings must not include the `.' (which
481 is legal in phrases)."""
482 atomlist = ['']
483 if atomends is None:
484 atomends = self.atomends
485
486 while self.pos < len(self.field):
487 if self.field[self.pos] in atomends:
488 break
489 else:
490 atomlist.append(self.field[self.pos])
491 self.pos += 1
492
493 return EMPTYSTRING.join(atomlist)
494
495 def getphraselist(self):
496 """Parse a sequence of RFC 2822 phrases.
497
498 A phrase is a sequence of words, which are in turn either RFC 2822
499 atoms or quoted-strings. Phrases are canonicalized by squeezing all
500 runs of continuous whitespace into one space.
501 """
502 plist = []
503
504 while self.pos < len(self.field):
505 if self.field[self.pos] in self.FWS:
506 self.pos += 1
507 elif self.field[self.pos] == '"':
508 plist.append(self.getquote())
509 elif self.field[self.pos] == '(':
510 self.commentlist.append(self.getcomment())
511 elif self.field[self.pos] in self.phraseends:
512 break
513 else:
514 plist.append(self.getatom(self.phraseends))
515
516 return plist
517
518 class AddressList(AddrlistClass):
519 """An AddressList encapsulates a list of parsed RFC 2822 addresses."""
520 def __init__(self, field):
521 AddrlistClass.__init__(self, field)
522 if field:
523 self.addresslist = self.getaddrlist()
524 else:
525 self.addresslist = []
526
527 def __len__(self):
528 return len(self.addresslist)
529
530 def __add__(self, other):
531 # Set union
532 newaddr = AddressList(None)
533 newaddr.addresslist = self.addresslist[:]
534 for x in other.addresslist:
535 if not x in self.addresslist:
536 newaddr.addresslist.append(x)
537 return newaddr
538
539 def __iadd__(self, other):
540 # Set union, in-place
541 for x in other.addresslist:
542 if not x in self.addresslist:
543 self.addresslist.append(x)
544 return self
545
546 def __sub__(self, other):
547 # Set difference
548 newaddr = AddressList(None)
549 for x in self.addresslist:
550 if not x in other.addresslist:
551 newaddr.addresslist.append(x)
552 return newaddr
553
554 def __isub__(self, other):
555 # Set difference, in-place
556 for x in other.addresslist:
557 if x in self.addresslist:
558 self.addresslist.remove(x)
559 return self
560
561 def __getitem__(self, index):
562 # Make indexing, slices, and 'in' work
563 return self.addresslist[index]