a y4fYH@sdZgdZddlZddlZddlZddlZddlZddlZddlZ ddl m Z ddl m Z ddl mZddl mZmZmZddlmZd Zd Zd Zd Zd Zed ZedZdZdaddZddZddZ ddZ!dFddZ"ddZ#ddZ$dZ%dd d!d"Z&d#d$Z'd%d&Z(d'd(Z)d)d*Z*dGd,d-Z+dHd.d/Z,dId0d1Z-d2d3Z.dd d4d5Z/d6d7Z0d8d9Z1dJd:d;Z2edZ5dKdAdBZ6dLdDdEZ7dS)MzMiscellaneous utilities.)collapse_rfc2231_value decode_paramsdecode_rfc2231encode_rfc2231 formataddr formatdateformat_datetime getaddresses make_msgid mktime_tz parseaddr parsedate parsedate_tzparsedate_to_datetimeunquoteN)quote) AddressList)r )r r _parsedate_tz)Charsetz, z 'z[][\\()<>@,:;".]z[\\"]z/etc/python/email.cfgcCstdurtatS)z5"Cache implementation for _cached_strict_addr_parsingN)_cached_strict_addr_parsing_use_strict_email_parsing_implrr#/usr/lib64/python3.9/email/utils.py_use_strict_email_parsing7srcCsttjd}|rdSz tt}Wnty6YnV0|@ddl}|jddd}| ||j dddd}Wdn1s0Y|rdSd S) z]Returns True if strict email parsing is not disabled by config file or env variable. (PYTHON_EMAIL_DISABLE_STRICT_ADDR_PARSINGFrN)#) interpolationcomment_prefixesemail_addr_parsing)fallbackT) boolosenvirongetopen_EMAIL_CONFIG_FILEFileNotFoundError configparser ConfigParser read_file getboolean)disabledfiler)configrrrr?s$   .rcCs(z|WdSty"YdS0dS)z8Return True if s contains surrogate-escaped binary data.FTN)encodeUnicodeEncodeError)srrr_has_surrogates\s  r3cCs|dd}|ddS)Nutf-8surrogateescapereplace)r0decode)stringoriginal_bytesrrr _sanitizeis r:r4cCs|\}}|d|rz|dWn:ty^t|trDt|}||}d||fYS0d}t|rrd}t d|}d||||fS|S)aThe 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. The optional charset is the character set that is used to encode 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'. asciiz%s <%s>r"z\\\g<0>z %s%s%s <%s>) r0r1 isinstancestrr header_encode specialsresearch escapesresub)paircharsetnameaddress encoded_namequotesrrrrus       rccs\d}d}t|D]8\}}|r0|d|fVd}q|dkr>d}q||fVq|rX|dfVdS)NrF\T) enumerate)addrposescapechrrr_iter_escaped_charss rPcCsd|vr |Sd}d}g}t|D]D\}}|dkr |dur>|}q ||krX|||||d}d}q |t|kr|||dd|S)z Strip real names between quotes.r<rNr)rPappendlenjoin)rLstartopen_posresultrMrOrrr_strip_quoted_realnamess  rXT)strictcCs|dkrt}|s4tdd|D}t|}|jSdd|D}t|}t|}t|}t|j}d}|D]}t|}|d|d7}qnt ||krd gS|S) zReturn 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. Ncss|]}t|VqdS)Nr>.0vrrr zgetaddresses..cSsg|] }t|qSrrZr[rrr r_z getaddresses..rrQ,rr) r COMMASPACErT _AddressList addresslist_pre_parse_validation_post_parse_validationrXcountrS) fieldvaluesrYallarLrWnr]rrrrs$     rcCsVt|}d}t|D]8\}}|dkr.|d7}q|dkr|d8}|dkrdSq|dkS)Nr(rQ)F)rXrP)rLopensrMrOrrr_check_parenthesiss rpcCs(g}|D]}t|sd}||q|S)Nz('', ''))rprR)email_header_fieldsaccepted_valuesr]rrrrfs  rfcCs,g}|D]}d|dvrd}||q|S)N[rQrb)rR)parsed_email_header_tuplesrrr]rrrrg s   rgc CsHdgd|d|dgd|dd|d|d|d |d |fS) Nz"%s, %02d %s %04d %02d:%02d:%02d %s)MonTueWedThuFriSatSun) JanFebMarAprMayJunJulAugSepOctNovDecrQrr) timetuplezonerrr_format_timetuple_and_zones rFcCsR|durt}|s|r,tj|tjj}n tj|}|rH|}d}t||S)aReturns a date string as specified by RFC 2822, e.g.: Fri, 09 Nov 2001 01:08:47 -0000 Optional timeval if given is a floating point time value as accepted by 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. NF)timedatetime fromtimestamptimezoneutcutcfromtimestamp astimezoner)timeval localtimeusegmtdtrrrrs rcCsV|}|r2|jdus$|jtjjkr,tdd}n|jdurBd}n |d}t||S)a$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. Nz%usegmt option requires a UTC datetimeGMTz-0000z%z)rtzinforrr ValueErrorstrftimer)rrnowrrrrr<s  rcCs^ttd}t}td}|dur0d}nd|}|durHt}d|||||f}|S)a{Returns a string suitable for RFC 2822 compliant Message-ID, e.g: <142480216486.20800.16526388040877946887@nightshade.la.mastaler.com> Optional idstring if given is a string used to strengthen the 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. d@Nr.z<%d.%d.%d%s@%s>)intrr#getpidrandom getrandbitssocketgetfqdn)idstringdomainrpidrandintmsgidrrrr Os  r cCsNt|^}}|dur(tj|ddStj|dddttj|diS)Nr|rseconds)rrr timedelta)datadtupletzrrrrfs rcCs|dkrt}|s,t|j}|s$dS|dSt|tr>|d}t|tsLdSt|gd}tt|j}|rxt|dkr|dS|dS)a 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 ('', ''). If strict is True, use a strict parser which rejects malformed inputs. NrbrrQ) rrdrer=listr>rfrgrS)rLrYaddrsrrrr ns    r cCs`t|dkr\|dr<|dr<|ddddddS|dr\|dr\|ddS|S) zRemove quotes from a string.rQr<z\\rJz\"<>)rS startswithendswithr6rZrrrrs   rcCs&|td}t|dkr"dd|fS|S)z#Decode string according to RFC 2231r}N)splitTICKrS)r2partsrrrrs   rcCsDtjj|d|pdd}|dur*|dur*|S|dur6d}d|||fS)zEncode 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. rr;)safeencodingNz%s'%s'%s)urllibparser)r2rElanguagerrrrs rz&^(?P\w+)\*((?P[0-9]+)\*?)?$c Cs@|dg}i}|ddD]x\}}|d}t|}t|}|r||dd\}}|durbt|}||g|||fq||dt|fq|r<| D]\}}g}d} | |D],\}} }|rt j j| d d } d } || qtt |}| r(t|\} } }||| | d|ffq||d|fq|S) zDecode parameters list according to RFC 2231. params is a sequence of 2-tuples containing (param name, string value). rrQN*rFnumz"%s"Fzlatin-1)rT)rrrfc2231_continuationmatchgroupr setdefaultrRritemssortrr EMPTYSTRINGrTr) params new_paramsrfc2231_paramsrFvalueencodedmor continuationsextendedr2rErrrrrs8    rr6us-asciicCslt|trt|dkrt|S|\}}}|dur4|}t|d}zt|||WStyft|YS0dS)Nrzraw-unicode-escape)r=tuplerSrbytesr> LookupError)rerrorsfallback_charsetrErtextrawbytesrrrrs   rrc Cs|durtjtjjS|jdur.|S|dd|f}t|}t |}z tj |j d}t||j }Wn~t y|tjt|dd}tjo|jdk}|rtjntj }|tj |dkrt|tj|}n t|}Yn0|j|dS)aReturn 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. In this case, a positive or zero value for *isdst* causes localtime to presume initially that summer time (for example, Daylight Saving Time) is or is not (respectively) in effect for the specified time. A negative value for *isdst* causes the localtime() function to attempt to divine whether summer time is in effect for the specified time. Nrrr|r)r)rrrrrrrrmktimerr tm_gmtofftm_zoneAttributeErrorgmtimedaylighttm_isdstaltzonetznamer6) risdsttmrlocaltmdeltardstgmtoffrrrrs$    r)r4)NFF)F)NN)NN)r6r)Nr)8__doc____all__r#rerrrr urllib.parseremail._parseaddrrrrdr r r r email.charsetrrcr UEMPTYSTRINGCRLFrcompiler@rBr'rrrr3r:rrPrXsupports_strict_parsingrrprfrgrrrr rr rrrASCIIrrrrrrrrsf        !1     $  0