| 
									
										
										
										
											2024-11-12 15:59:19 +02:00
										 |  |  | # Copyright (C) 2001 Python Software Foundation | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  | # Author: Barry Warsaw | 
					
						
							|  |  |  | # Contact: email-sig@python.org | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | """Miscellaneous utilities.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | __all__ = [ | 
					
						
							|  |  |  |     'collapse_rfc2231_value', | 
					
						
							|  |  |  |     'decode_params', | 
					
						
							|  |  |  |     'decode_rfc2231', | 
					
						
							|  |  |  |     'encode_rfc2231', | 
					
						
							|  |  |  |     'formataddr', | 
					
						
							|  |  |  |     'formatdate', | 
					
						
							| 
									
										
										
										
											2011-07-20 11:41:21 -04:00
										 |  |  |     'format_datetime', | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     'getaddresses', | 
					
						
							|  |  |  |     'make_msgid', | 
					
						
							| 
									
										
										
										
											2009-11-25 18:45:15 +00:00
										 |  |  |     'mktime_tz', | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     'parseaddr', | 
					
						
							|  |  |  |     'parsedate', | 
					
						
							|  |  |  |     'parsedate_tz', | 
					
						
							| 
									
										
										
										
											2011-07-20 11:41:21 -04:00
										 |  |  |     'parsedate_to_datetime', | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     'unquote', | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import re | 
					
						
							|  |  |  | import time | 
					
						
							| 
									
										
										
										
											2011-07-20 11:41:21 -04:00
										 |  |  | import datetime | 
					
						
							| 
									
										
										
										
											2008-06-18 20:49:58 +00:00
										 |  |  | import urllib.parse | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | from email._parseaddr import quote | 
					
						
							|  |  |  | from email._parseaddr import AddressList as _AddressList | 
					
						
							|  |  |  | from email._parseaddr import mktime_tz | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-22 09:03:56 +02:00
										 |  |  | from email._parseaddr import parsedate, parsedate_tz, _parsedate_tz | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | COMMASPACE = ', ' | 
					
						
							|  |  |  | EMPTYSTRING = '' | 
					
						
							|  |  |  | UEMPTYSTRING = '' | 
					
						
							|  |  |  | CRLF = '\r\n' | 
					
						
							|  |  |  | TICK = "'" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | specialsre = re.compile(r'[][\\()<>@,:;".]') | 
					
						
							| 
									
										
										
										
											2012-03-14 15:31:47 -04:00
										 |  |  | escapesre = re.compile(r'[\\"]') | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-15 16:10:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-26 12:06:21 -04:00
										 |  |  | def _has_surrogates(s): | 
					
						
							| 
									
										
										
										
											2023-12-12 05:21:18 +13:00
										 |  |  |     """Return True if s may contain surrogate-escaped binary data.""" | 
					
						
							| 
									
										
										
										
											2013-06-26 12:06:21 -04:00
										 |  |  |     # This check is based on the fact that unless there are surrogates, utf8 | 
					
						
							|  |  |  |     # (Python's default encoding) can encode any string.  This is the fastest | 
					
						
							| 
									
										
										
										
											2023-12-12 05:21:18 +13:00
										 |  |  |     # way to check for surrogates, see bpo-11454 (moved to gh-55663) for timings. | 
					
						
							| 
									
										
										
										
											2013-06-26 12:06:21 -04:00
										 |  |  |     try: | 
					
						
							|  |  |  |         s.encode() | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  |     except UnicodeEncodeError: | 
					
						
							|  |  |  |         return True | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-25 18:42:14 -04:00
										 |  |  | # How to deal with a string containing bytes before handing it to the | 
					
						
							|  |  |  | # application through the 'normal' interface. | 
					
						
							|  |  |  | def _sanitize(string): | 
					
						
							| 
									
										
										
										
											2013-10-16 22:48:40 -04:00
										 |  |  |     # Turn any escaped bytes into unicode 'unknown' char.  If the escaped | 
					
						
							|  |  |  |     # bytes happen to be utf-8 they will instead get decoded, even if they | 
					
						
							|  |  |  |     # were invalid in the charset the source was supposed to be in.  This | 
					
						
							|  |  |  |     # seems like it is not a bad thing; a defect was still registered. | 
					
						
							|  |  |  |     original_bytes = string.encode('utf-8', 'surrogateescape') | 
					
						
							|  |  |  |     return original_bytes.decode('utf-8', 'replace') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-25 18:42:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-19 17:56:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  | # Helpers | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-06 09:35:57 -04:00
										 |  |  | def formataddr(pair, charset='utf-8'): | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     """The inverse of parseaddr(), this takes a 2-tuple of the form
 | 
					
						
							|  |  |  |     (realname, email_address) and returns the string value suitable | 
					
						
							|  |  |  |     for an RFC 2822 From, To or Cc header. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If the first element of pair is false, then the second element is | 
					
						
							|  |  |  |     returned unmodified. | 
					
						
							| 
									
										
										
										
											2011-04-06 09:35:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-19 13:57:30 +02:00
										 |  |  |     The optional charset is the character set that is used to encode | 
					
						
							| 
									
										
										
										
											2011-04-06 09:35:57 -04:00
										 |  |  |     realname in case realname is not ASCII safe.  Can be an instance of str or | 
					
						
							|  |  |  |     a Charset-like object which has a header_encode method.  Default is | 
					
						
							|  |  |  |     'utf-8'. | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     name, address = pair | 
					
						
							| 
									
										
										
										
											2016-04-15 02:14:19 +00:00
										 |  |  |     # The address MUST (per RFC) be ascii, so raise a UnicodeError if it isn't. | 
					
						
							| 
									
										
										
										
											2011-04-06 09:35:57 -04:00
										 |  |  |     address.encode('ascii') | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     if name: | 
					
						
							| 
									
										
										
										
											2011-04-06 09:35:57 -04:00
										 |  |  |         try: | 
					
						
							|  |  |  |             name.encode('ascii') | 
					
						
							|  |  |  |         except UnicodeEncodeError: | 
					
						
							|  |  |  |             if isinstance(charset, str): | 
					
						
							| 
									
										
										
										
											2023-10-13 00:03:20 +02:00
										 |  |  |                 # lazy import to improve module import time | 
					
						
							|  |  |  |                 from email.charset import Charset | 
					
						
							| 
									
										
										
										
											2011-04-06 09:35:57 -04:00
										 |  |  |                 charset = Charset(charset) | 
					
						
							|  |  |  |             encoded_name = charset.header_encode(name) | 
					
						
							|  |  |  |             return "%s <%s>" % (encoded_name, address) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             quotes = '' | 
					
						
							|  |  |  |             if specialsre.search(name): | 
					
						
							|  |  |  |                 quotes = '"' | 
					
						
							|  |  |  |             name = escapesre.sub(r'\\\g<0>', name) | 
					
						
							|  |  |  |             return '%s%s%s <%s>' % (quotes, name, quotes, address) | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     return address | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-15 16:10:40 +01:00
										 |  |  | def _iter_escaped_chars(addr): | 
					
						
							|  |  |  |     pos = 0 | 
					
						
							|  |  |  |     escape = False | 
					
						
							|  |  |  |     for pos, ch in enumerate(addr): | 
					
						
							|  |  |  |         if escape: | 
					
						
							|  |  |  |             yield (pos, '\\' + ch) | 
					
						
							|  |  |  |             escape = False | 
					
						
							|  |  |  |         elif ch == '\\': | 
					
						
							|  |  |  |             escape = True | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             yield (pos, ch) | 
					
						
							|  |  |  |     if escape: | 
					
						
							|  |  |  |         yield (pos, '\\') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _strip_quoted_realnames(addr): | 
					
						
							|  |  |  |     """Strip real names between quotes.""" | 
					
						
							|  |  |  |     if '"' not in addr: | 
					
						
							|  |  |  |         # Fast path | 
					
						
							|  |  |  |         return addr | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     start = 0 | 
					
						
							|  |  |  |     open_pos = None | 
					
						
							|  |  |  |     result = [] | 
					
						
							|  |  |  |     for pos, ch in _iter_escaped_chars(addr): | 
					
						
							|  |  |  |         if ch == '"': | 
					
						
							|  |  |  |             if open_pos is None: | 
					
						
							|  |  |  |                 open_pos = pos | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 if start != open_pos: | 
					
						
							|  |  |  |                     result.append(addr[start:open_pos]) | 
					
						
							|  |  |  |                 start = pos + 1 | 
					
						
							|  |  |  |                 open_pos = None | 
					
						
							| 
									
										
										
										
											2008-08-19 17:56:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-15 16:10:40 +01:00
										 |  |  |     if start < len(addr): | 
					
						
							|  |  |  |         result.append(addr[start:]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ''.join(result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | supports_strict_parsing = True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def getaddresses(fieldvalues, *, strict=True): | 
					
						
							|  |  |  |     """Return a list of (REALNAME, EMAIL) or ('','') for each fieldvalue.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     When parsing fails for a fieldvalue, a 2-tuple of ('', '') is returned in | 
					
						
							|  |  |  |     its place. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If strict is true, use a strict parser which rejects malformed inputs. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # If strict is true, if the resulting list of parsed addresses is greater | 
					
						
							|  |  |  |     # than the number of fieldvalues in the input list, a parsing error has | 
					
						
							|  |  |  |     # occurred and consequently a list containing a single empty 2-tuple [('', | 
					
						
							|  |  |  |     # '')] is returned in its place. This is done to avoid invalid output. | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Malformed input: getaddresses(['alice@example.com <bob@example.com>']) | 
					
						
							|  |  |  |     # Invalid output: [('', 'alice@example.com'), ('', 'bob@example.com')] | 
					
						
							|  |  |  |     # Safe output: [('', '')] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not strict: | 
					
						
							|  |  |  |         all = COMMASPACE.join(str(v) for v in fieldvalues) | 
					
						
							|  |  |  |         a = _AddressList(all) | 
					
						
							|  |  |  |         return a.addresslist | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fieldvalues = [str(v) for v in fieldvalues] | 
					
						
							|  |  |  |     fieldvalues = _pre_parse_validation(fieldvalues) | 
					
						
							|  |  |  |     addr = COMMASPACE.join(fieldvalues) | 
					
						
							|  |  |  |     a = _AddressList(addr) | 
					
						
							|  |  |  |     result = _post_parse_validation(a.addresslist) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Treat output as invalid if the number of addresses is not equal to the | 
					
						
							|  |  |  |     # expected number of addresses. | 
					
						
							|  |  |  |     n = 0 | 
					
						
							|  |  |  |     for v in fieldvalues: | 
					
						
							|  |  |  |         # When a comma is used in the Real Name part it is not a deliminator. | 
					
						
							|  |  |  |         # So strip those out before counting the commas. | 
					
						
							|  |  |  |         v = _strip_quoted_realnames(v) | 
					
						
							|  |  |  |         # Expected number of addresses: 1 + number of commas | 
					
						
							|  |  |  |         n += 1 + v.count(',') | 
					
						
							|  |  |  |     if len(result) != n: | 
					
						
							|  |  |  |         return [('', '')] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _check_parenthesis(addr): | 
					
						
							|  |  |  |     # Ignore parenthesis in quoted real names. | 
					
						
							|  |  |  |     addr = _strip_quoted_realnames(addr) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     opens = 0 | 
					
						
							|  |  |  |     for pos, ch in _iter_escaped_chars(addr): | 
					
						
							|  |  |  |         if ch == '(': | 
					
						
							|  |  |  |             opens += 1 | 
					
						
							|  |  |  |         elif ch == ')': | 
					
						
							|  |  |  |             opens -= 1 | 
					
						
							|  |  |  |             if opens < 0: | 
					
						
							|  |  |  |                 return False | 
					
						
							|  |  |  |     return (opens == 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _pre_parse_validation(email_header_fields): | 
					
						
							|  |  |  |     accepted_values = [] | 
					
						
							|  |  |  |     for v in email_header_fields: | 
					
						
							|  |  |  |         if not _check_parenthesis(v): | 
					
						
							|  |  |  |             v = "('', '')" | 
					
						
							|  |  |  |         accepted_values.append(v) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return accepted_values | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _post_parse_validation(parsed_email_header_tuples): | 
					
						
							|  |  |  |     accepted_values = [] | 
					
						
							|  |  |  |     # The parser would have parsed a correctly formatted domain-literal | 
					
						
							|  |  |  |     # The existence of an [ after parsing indicates a parsing failure | 
					
						
							|  |  |  |     for v in parsed_email_header_tuples: | 
					
						
							|  |  |  |         if '[' in v[1]: | 
					
						
							|  |  |  |             v = ('', '') | 
					
						
							|  |  |  |         accepted_values.append(v) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return accepted_values | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-20 11:41:21 -04:00
										 |  |  | def _format_timetuple_and_zone(timetuple, zone): | 
					
						
							|  |  |  |     return '%s, %02d %s %04d %02d:%02d:%02d %s' % ( | 
					
						
							|  |  |  |         ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][timetuple[6]], | 
					
						
							|  |  |  |         timetuple[2], | 
					
						
							|  |  |  |         ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', | 
					
						
							|  |  |  |          'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][timetuple[1] - 1], | 
					
						
							|  |  |  |         timetuple[0], timetuple[3], timetuple[4], timetuple[5], | 
					
						
							|  |  |  |         zone) | 
					
						
							| 
									
										
										
										
											2008-08-19 17:56:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  | def formatdate(timeval=None, localtime=False, usegmt=False): | 
					
						
							|  |  |  |     """Returns a date string as specified by RFC 2822, e.g.:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Fri, 09 Nov 2001 01:08:47 -0000 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-19 11:06:02 +03:00
										 |  |  |     Optional timeval if given is a floating-point time value as accepted by | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     gmtime() and localtime(), otherwise the current time is used. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Optional localtime is a flag that when True, interprets timeval, and | 
					
						
							|  |  |  |     returns a date relative to the local timezone instead of UTC, properly | 
					
						
							|  |  |  |     taking daylight savings time into account. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Optional argument usegmt means that the timezone is written out as | 
					
						
							|  |  |  |     an ascii string, not numeric one (so "GMT" instead of "+0000"). This | 
					
						
							|  |  |  |     is needed for HTTP, and is only used when localtime==False. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     # Note: we cannot use strftime() because that honors the locale and RFC | 
					
						
							|  |  |  |     # 2822 requires that day and month names be the English abbreviations. | 
					
						
							|  |  |  |     if timeval is None: | 
					
						
							|  |  |  |         timeval = time.time() | 
					
						
							| 
									
										
										
										
											2023-04-27 11:32:30 -06:00
										 |  |  |     dt = datetime.datetime.fromtimestamp(timeval, datetime.timezone.utc) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-01 08:18:22 +12:00
										 |  |  |     if localtime: | 
					
						
							|  |  |  |         dt = dt.astimezone() | 
					
						
							|  |  |  |         usegmt = False | 
					
						
							| 
									
										
										
										
											2023-04-27 11:32:30 -06:00
										 |  |  |     elif not usegmt: | 
					
						
							|  |  |  |         dt = dt.replace(tzinfo=None) | 
					
						
							| 
									
										
										
										
											2015-08-01 08:18:22 +12:00
										 |  |  |     return format_datetime(dt, usegmt) | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-20 11:41:21 -04:00
										 |  |  | def format_datetime(dt, usegmt=False): | 
					
						
							|  |  |  |     """Turn a datetime into a date string as specified in RFC 2822.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If usegmt is True, dt must be an aware datetime with an offset of zero.  In | 
					
						
							|  |  |  |     this case 'GMT' will be rendered instead of the normal +0000 required by | 
					
						
							|  |  |  |     RFC2822.  This is to support HTTP headers involving date stamps. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     now = dt.timetuple() | 
					
						
							|  |  |  |     if usegmt: | 
					
						
							|  |  |  |         if dt.tzinfo is None or dt.tzinfo != datetime.timezone.utc: | 
					
						
							|  |  |  |             raise ValueError("usegmt option requires a UTC datetime") | 
					
						
							|  |  |  |         zone = 'GMT' | 
					
						
							|  |  |  |     elif dt.tzinfo is None: | 
					
						
							|  |  |  |         zone = '-0000' | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         zone = dt.strftime("%z") | 
					
						
							|  |  |  |     return _format_timetuple_and_zone(now, zone) | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-19 17:56:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-02 21:47:19 +00:00
										 |  |  | def make_msgid(idstring=None, domain=None): | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     """Returns a string suitable for RFC 2822 compliant Message-ID, e.g:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 10:09:42 +03:00
										 |  |  |     <142480216486.20800.16526388040877946887@nightshade.la.mastaler.com> | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Optional idstring if given is a string used to strengthen the | 
					
						
							| 
									
										
										
										
											2010-12-02 21:47:19 +00:00
										 |  |  |     uniqueness of the message id.  Optional domain if given provides the | 
					
						
							|  |  |  |     portion of the message id after the '@'.  It defaults to the locally | 
					
						
							|  |  |  |     defined hostname. | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2023-10-13 00:03:20 +02:00
										 |  |  |     # Lazy imports to speedup module import time | 
					
						
							|  |  |  |     # (no other functions in email.utils need these modules) | 
					
						
							|  |  |  |     import random | 
					
						
							|  |  |  |     import socket | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 10:09:42 +03:00
										 |  |  |     timeval = int(time.time()*100) | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     pid = os.getpid() | 
					
						
							| 
									
										
										
										
											2015-05-19 10:09:42 +03:00
										 |  |  |     randint = random.getrandbits(64) | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     if idstring is None: | 
					
						
							|  |  |  |         idstring = '' | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         idstring = '.' + idstring | 
					
						
							| 
									
										
										
										
											2010-12-02 21:47:19 +00:00
										 |  |  |     if domain is None: | 
					
						
							|  |  |  |         domain = socket.getfqdn() | 
					
						
							| 
									
										
										
										
											2015-05-19 10:09:42 +03:00
										 |  |  |     msgid = '<%d.%d.%d%s@%s>' % (timeval, pid, randint, idstring, domain) | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     return msgid | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-20 11:41:21 -04:00
										 |  |  | def parsedate_to_datetime(data): | 
					
						
							| 
									
										
											  
											
												bpo-30681: Support invalid date format or value in email Date header (GH-22090)
I am re-submitting an older PR which was abandoned but is still relevant, #10783 by @timb07.
The issue being solved () is still relevant. The original PR #10783 was closed as
the final request changes were not applied and since abandoned.
In this new PR I have re-used the original patch plus applied both comments from the review, by @maxking and @pganssle.
For reference, here is the original PR description:
In email.utils.parsedate_to_datetime(), a failure to parse the date, or invalid date components (such as hour outside 0..23) raises an exception. Document this behaviour, and add tests to test_email/test_utils.py to confirm this behaviour.
In email.headerregistry.DateHeader.parse(), check when parsedate_to_datetime() raises an exception and add a new defect InvalidDateDefect; preserve the invalid value as the string value of the header, but set the datetime attribute to None.
Add tests to test_email/test_headerregistry.py to confirm this behaviour; also added test to test_email/test_inversion.py to confirm emails with such defective date headers round trip successfully.
This pull request incorporates feedback gratefully received from @bitdancer, @brettcannon, @Mariatta and @warsaw, and replaces the earlier PR #2254.
Automerge-Triggered-By: GH:warsaw
											
										 
											2020-10-27 01:31:06 +01:00
										 |  |  |     parsed_date_tz = _parsedate_tz(data) | 
					
						
							|  |  |  |     if parsed_date_tz is None: | 
					
						
							|  |  |  |         raise ValueError('Invalid date value or format "%s"' % str(data)) | 
					
						
							|  |  |  |     *dtuple, tz = parsed_date_tz | 
					
						
							| 
									
										
										
										
											2011-07-20 11:41:21 -04:00
										 |  |  |     if tz is None: | 
					
						
							|  |  |  |         return datetime.datetime(*dtuple[:6]) | 
					
						
							|  |  |  |     return datetime.datetime(*dtuple[:6], | 
					
						
							|  |  |  |             tzinfo=datetime.timezone(datetime.timedelta(seconds=tz))) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-15 16:10:40 +01:00
										 |  |  | def parseaddr(addr, *, strict=True): | 
					
						
							| 
									
										
										
										
											2017-09-20 00:40:49 +05:30
										 |  |  |     """
 | 
					
						
							|  |  |  |     Parse addr into its constituent realname and email address parts. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Return a tuple of realname and email address, unless the parse fails, in | 
					
						
							|  |  |  |     which case return a 2-tuple of ('', ''). | 
					
						
							| 
									
										
										
										
											2023-12-15 16:10:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     If strict is True, use a strict parser which rejects malformed inputs. | 
					
						
							| 
									
										
										
										
											2017-09-20 00:40:49 +05:30
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2023-12-15 16:10:40 +01:00
										 |  |  |     if not strict: | 
					
						
							|  |  |  |         addrs = _AddressList(addr).addresslist | 
					
						
							|  |  |  |         if not addrs: | 
					
						
							|  |  |  |             return ('', '') | 
					
						
							|  |  |  |         return addrs[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if isinstance(addr, list): | 
					
						
							|  |  |  |         addr = addr[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not isinstance(addr, str): | 
					
						
							|  |  |  |         return ('', '') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     addr = _pre_parse_validation([addr])[0] | 
					
						
							|  |  |  |     addrs = _post_parse_validation(_AddressList(addr).addresslist) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not addrs or len(addrs) > 1: | 
					
						
							|  |  |  |         return ('', '') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     return addrs[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # rfc822.unquote() doesn't properly de-backslash-ify in Python pre-2.3. | 
					
						
							|  |  |  | def unquote(str): | 
					
						
							|  |  |  |     """Remove quotes from a string.""" | 
					
						
							|  |  |  |     if len(str) > 1: | 
					
						
							|  |  |  |         if str.startswith('"') and str.endswith('"'): | 
					
						
							|  |  |  |             return str[1:-1].replace('\\\\', '\\').replace('\\"', '"') | 
					
						
							|  |  |  |         if str.startswith('<') and str.endswith('>'): | 
					
						
							|  |  |  |             return str[1:-1] | 
					
						
							|  |  |  |     return str | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-19 17:56:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  | # RFC2231-related functions - parameter encoding and decoding | 
					
						
							|  |  |  | def decode_rfc2231(s): | 
					
						
							|  |  |  |     """Decode string according to RFC 2231""" | 
					
						
							|  |  |  |     parts = s.split(TICK, 2) | 
					
						
							|  |  |  |     if len(parts) <= 2: | 
					
						
							|  |  |  |         return None, None, s | 
					
						
							|  |  |  |     return parts | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def encode_rfc2231(s, charset=None, language=None): | 
					
						
							|  |  |  |     """Encode string according to RFC 2231.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If neither charset nor language is given, then s is returned as-is.  If | 
					
						
							|  |  |  |     charset is given but not language, the string is encoded using the empty | 
					
						
							|  |  |  |     string for language. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2008-08-18 21:44:30 +00:00
										 |  |  |     s = urllib.parse.quote(s, safe='', encoding=charset or 'ascii') | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     if charset is None and language is None: | 
					
						
							|  |  |  |         return s | 
					
						
							|  |  |  |     if language is None: | 
					
						
							|  |  |  |         language = '' | 
					
						
							|  |  |  |     return "%s'%s'%s" % (charset, language, s) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-19 17:56:33 +00:00
										 |  |  | rfc2231_continuation = re.compile(r'^(?P<name>\w+)\*((?P<num>[0-9]+)\*?)?$', | 
					
						
							|  |  |  |     re.ASCII) | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def decode_params(params): | 
					
						
							|  |  |  |     """Decode parameters list according to RFC 2231.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     params is a sequence of 2-tuples containing (param name, string value). | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2019-09-19 20:25:55 -07:00
										 |  |  |     new_params = [params[0]] | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     # Map parameter's name to a list of continuations.  The values are a | 
					
						
							|  |  |  |     # 3-tuple of the continuation number, the string value, and a flag | 
					
						
							|  |  |  |     # specifying whether a particular segment is %-encoded. | 
					
						
							|  |  |  |     rfc2231_params = {} | 
					
						
							| 
									
										
										
										
											2019-09-19 20:25:55 -07:00
										 |  |  |     for name, value in params[1:]: | 
					
						
							|  |  |  |         encoded = name.endswith('*') | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |         value = unquote(value) | 
					
						
							|  |  |  |         mo = rfc2231_continuation.match(name) | 
					
						
							|  |  |  |         if mo: | 
					
						
							|  |  |  |             name, num = mo.group('name', 'num') | 
					
						
							|  |  |  |             if num is not None: | 
					
						
							|  |  |  |                 num = int(num) | 
					
						
							|  |  |  |             rfc2231_params.setdefault(name, []).append((num, value, encoded)) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             new_params.append((name, '"%s"' % quote(value))) | 
					
						
							|  |  |  |     if rfc2231_params: | 
					
						
							|  |  |  |         for name, continuations in rfc2231_params.items(): | 
					
						
							|  |  |  |             value = [] | 
					
						
							|  |  |  |             extended = False | 
					
						
							| 
									
										
										
										
											2025-06-08 03:13:21 -04:00
										 |  |  |             # Sort by number, treating None as 0 if there is no 0, | 
					
						
							|  |  |  |             # and ignore it if there is already a 0. | 
					
						
							|  |  |  |             has_zero = any(x[0] == 0 for x in continuations) | 
					
						
							|  |  |  |             if has_zero: | 
					
						
							|  |  |  |                 continuations = [x for x in continuations if x[0] is not None] | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 continuations = [(x[0] or 0, x[1], x[2]) for x in continuations] | 
					
						
							|  |  |  |             continuations.sort(key=lambda x: x[0]) | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |             # And now append all values in numerical order, converting | 
					
						
							|  |  |  |             # %-encodings for the encoded segments.  If any of the | 
					
						
							|  |  |  |             # continuation names ends in a *, then the entire string, after | 
					
						
							|  |  |  |             # decoding segments and concatenating, must have the charset and | 
					
						
							|  |  |  |             # language specifiers at the beginning of the string. | 
					
						
							|  |  |  |             for num, s, encoded in continuations: | 
					
						
							|  |  |  |                 if encoded: | 
					
						
							| 
									
										
										
										
											2008-08-18 21:44:30 +00:00
										 |  |  |                     # Decode as "latin-1", so the characters in s directly | 
					
						
							|  |  |  |                     # represent the percent-encoded octet values. | 
					
						
							|  |  |  |                     # collapse_rfc2231_value treats this as an octet sequence. | 
					
						
							|  |  |  |                     s = urllib.parse.unquote(s, encoding="latin-1") | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |                     extended = True | 
					
						
							|  |  |  |                 value.append(s) | 
					
						
							|  |  |  |             value = quote(EMPTYSTRING.join(value)) | 
					
						
							|  |  |  |             if extended: | 
					
						
							|  |  |  |                 charset, language, value = decode_rfc2231(value) | 
					
						
							|  |  |  |                 new_params.append((name, (charset, language, '"%s"' % value))) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 new_params.append((name, '"%s"' % value)) | 
					
						
							|  |  |  |     return new_params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def collapse_rfc2231_value(value, errors='replace', | 
					
						
							|  |  |  |                            fallback_charset='us-ascii'): | 
					
						
							|  |  |  |     if not isinstance(value, tuple) or len(value) != 3: | 
					
						
							|  |  |  |         return unquote(value) | 
					
						
							|  |  |  |     # While value comes to us as a unicode string, we need it to be a bytes | 
					
						
							|  |  |  |     # object.  We do not want bytes() normal utf-8 decoder, we want a straight | 
					
						
							|  |  |  |     # interpretation of the string as character bytes. | 
					
						
							|  |  |  |     charset, language, text = value | 
					
						
							| 
									
										
										
										
											2014-02-07 15:02:19 -05:00
										 |  |  |     if charset is None: | 
					
						
							|  |  |  |         # Issue 17369: if charset/lang is None, decode_rfc2231 couldn't parse | 
					
						
							|  |  |  |         # the value, so use the fallback_charset. | 
					
						
							|  |  |  |         charset = fallback_charset | 
					
						
							| 
									
										
										
										
											2007-08-30 03:46:43 +00:00
										 |  |  |     rawbytes = bytes(text, 'raw-unicode-escape') | 
					
						
							| 
									
										
										
										
											2007-08-30 01:15:14 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         return str(rawbytes, charset, errors) | 
					
						
							|  |  |  |     except LookupError: | 
					
						
							|  |  |  |         # charset is not a known codec. | 
					
						
							|  |  |  |         return unquote(text) | 
					
						
							| 
									
										
										
										
											2012-05-25 23:22:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # datetime doesn't provide a localtime function yet, so provide one.  Code | 
					
						
							|  |  |  | # adapted from the patch in issue 9527.  This may not be perfect, but it is | 
					
						
							|  |  |  | # better than not having it. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-09 12:17:02 +03:00
										 |  |  | def localtime(dt=None): | 
					
						
							| 
									
										
										
										
											2012-05-25 23:22:59 -04:00
										 |  |  |     """Return local time as an aware datetime object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If called without arguments, return current time.  Otherwise *dt* | 
					
						
							|  |  |  |     argument should be a datetime instance, and it is converted to the | 
					
						
							|  |  |  |     local time zone according to the system time zone database.  If *dt* is | 
					
						
							|  |  |  |     naive (that is, dt.tzinfo is None), it is assumed to be in local time. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     if dt is None: | 
					
						
							| 
									
										
										
										
											2023-03-19 19:20:20 -05:00
										 |  |  |         dt = datetime.datetime.now() | 
					
						
							|  |  |  |     return dt.astimezone() |