3 fJ@sdZddddddddd d d d d ddgZddlZddlZddlZddlZddlZddlZddlZ ddl m Z ddl m Z ddl mZddl mZmZmZddlmZdZdZdZdZdZejdZejdZdZdaddZd d!Zd"d#Z d$d%Z!dId'dZ"d(d)Z#d*d+Z$d,Z%dd-d.dZ&d/d0Z'd1d2Z(d3d4Z)ejd5ej*ej+BZ,d6d7Z-dJd9dZ.dKd:dZ/dLd;d Z0ddZ3d?dZ4dMd@dZ5ejdAej6Z7dBdZ8dNdEdZ9dPdGdHZ:dS)QzMiscellaneous 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.cfgcCstdkrtatS)z5"Cache implementation for _cached_strict_addr_parsingN)_cached_strict_addr_parsing_use_strict_email_parsing_implrr#/usr/lib64/python3.6/email/utils.py_use_strict_email_parsing7srcCsttjjd}|rdSy tt}Wntk r8YnBX|6ddl}|jdd d}|j ||j dddd}WdQRX|rdSd S) z]Returns True if strict email parsing is not disabled by config file or env variable. Z(PYTHON_EMAIL_DISABLE_STRICT_ADDR_PARSINGFrN#)Z interpolationZcomment_prefixesZemail_addr_parsing)ZfallbackT)r) boolosenvirongetopen_EMAIL_CONFIG_FILEFileNotFoundError configparserZ ConfigParserZ read_fileZ getboolean)Zdisabledfiler$configrrrr?s"  rc Cs&y |jdStk r dSXdS)z8Return True if s contains surrogate-escaped binary data.FTN)encodeUnicodeEncodeError)srrr_has_surrogates\s r*cCs|jdd}|jddS)Nzutf-8surrogateescapereplace)r'decode)stringZoriginal_bytesrrr _sanitizeis r/utf-8c Cs|\}}|jd|ry|jdWn8tk r\t|trFt|}|j|}d||fSXd}tj|rpd}tj 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. Optional charset if given 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>) r'r( isinstancestrrZ header_encode specialsresearch escapesresub)ZpaircharsetnameZaddressZ encoded_nameZquotesrrrrus      ccs`d}d}xDt|D]8\}}|r2|d|fVd}q|dkr@d}q||fVqW|r\|dfVdS)NrF\T) enumerate)addrposescapechrrr_iter_escaped_charssrAcCsd|kr |Sd}d}g}xPt|D]D\}}|dkr"|dkr@|}q"||krZ|j||||d}d}q"W|t|kr|j||ddj|S)z Strip real names between quotes.r2rNr)rAappendlenjoin)r=startZopen_posresultr>r@rrr_strip_quoted_realnamess  rHT)strictcCs|dkrt}|s4tjdd|D}t|}|jSdd|D}t|}tj|}t|}t|j}d}x&|D]}t|}|d|jd7}qpWt ||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)N)r4).0vrrr szgetaddresses..cSsg|] }t|qSr)r4)rJrKrrr sz getaddresses..rrB,r)rr) r COMMASPACErE _AddressList addresslist_pre_parse_validation_post_parse_validationrHcountrD)Z fieldvaluesrIallar=rGnrKrrrrs$      cCsXt|}d}xBt|D]6\}}|dkr0|d7}q|dkr|d8}|dkrdSqW|dkS)Nr(rB)F)rHrA)r=Zopensr>r@rrr_check_parenthesiss rZcCs,g}x"|D]}t|sd}|j|q W|S)Nz('', ''))rZrC)Zemail_header_fieldsaccepted_valuesrKrrrrRs  rRcCs0g}x&|D]}d|dkrd}|j|q W|S)N[rBr)rr)rC)Zparsed_email_header_tuplesr[rKrrrrS s   rSa_ =\? # literal =? (?P[^?]*?) # non-greedy up to the next ? is the charset \? # literal ? (?P[qb]) # either a "q" or a "b", case insensitive \? # literal ? (?P.*?) # non-greedy up to the next ?= is the atom \?= # literal ?= cCsfddddddddg|d |d d d d dddddddddg |dd|d|d|d|d|fS)Nz"%s, %02d %s %04d %02d:%02d:%02d %sZMonZTueZWedZThuZFriZSatZSunZJanZFebZMarZAprZMayZJunZJulZAugZSepZOctZNovZDecrBrr) timetuplezonerrr_format_timetuple_and_zone"s rdFcCsR|dkrtj}|s|r,tjj|tjj}n tjj|}|rH|j}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)timedatetimeZ fromtimestamptimezoneutcZutcfromtimestamp astimezoner)timeval localtimeusegmtdtrrrr+s cCsV|j}|r2|jdks$|jtjjkr,tdd}n|jdkrBd}n |jd}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 datetimeZGMTz-0000z%z)rbtzinforfrgrh ValueErrorZstrftimerd)rmrlnowrcrrrrHs  cCs^ttjd}tj}tjd}|dkr0d}nd|}|dkrHtj}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>)intrergetpidrandomZ getrandbitssocketZgetfqdn)ZidstringZdomainrjpidZrandintZmsgidrrrr [s  cCsNt|^}}|dkr(tj|ddStj|dddtjtj|diS)Nr]rn)seconds)rrfrg timedelta)dataZdtupletzrrrrrs cCs|dkrt}|s,t|j}|s$dS|dSt|tr>|d}t|tsLdSt|gd}tt|j}| szt|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. NrrrB)rr)rr)rr) rrPrQr3listr4rRrSrD)r=rIZaddrsrrrr zs    cCs`t|dkr\|jdr<|jdr<|ddjddjddS|jdr\|jdr\|dd S|S) zRemove quotes from a string.rBr2z\\r;z\"<>r)rD startswithendswithr,)r4rrrrs   cCs&|jtd}t|dkr"dd|fS|S)z#Decode string according to RFC 2231r^N)splitTICKrD)r)partsrrrrs   cCsDtjj|d|pdd}|dkr*|dkr*|S|dkr6d}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. rr1)ZsafeencodingNz%s'%s'%s)urllibparser)r)r9languagerrrrs z&^(?P\w+)\*((?P[0-9]+)\*?)?$c Csx|dd}g}i}|jd\}}|j||fx|r|jd\}}|jdrTd}nd}t|}tj|}|r|jdd\}}|dk rt|}|j|gj|||fq2|j|dt |fq2W|rtx|j D]\}}g}d} |j x6|D].\}} }|rt j j| d d } d} |j| qWt tj|}| r^t|\} } }|j|| | d|ffq|j|d|fqW|S) zDecode parameters list according to RFC 2231. params is a sequence of 2-tuples containing (param name, string value). Nr*TFr:numz"%s"zlatin-1)r)poprCrrrfc2231_continuationmatchgrouprt setdefaultritemssortrr EMPTYSTRINGrEr) ZparamsZ new_paramsZrfc2231_paramsr:valueZencodedZmorZ continuationsZextendedr)r9rrrrrsD   r,us-asciic Csjt|t st|dkr t|S|\}}}|dkr6|}t|d}y t|||Stk rdt|SXdS)Nr_zraw-unicode-escape)r3tuplerDrbytesr4 LookupError)rerrorsZfallback_charsetr9rtextZrawbytesrrrrs   rBc Cs|dkrtjjtjjjS|jdk r.|jS|jdd|f}tj|}tj |}y tj |j d}tj||j }Wnt k r|tjtj|dd}tjo|jdk}|rtjntj }|tj |dkrtj|tj|}n tj|}YnX|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. NrB)ryr]r)rnr)rfrprgrhrirnrbreZmktimerkrzZ tm_gmtoffZtm_zoneAttributeErrorZgmtimeZdaylightZtm_isdstZaltzoneZtznamer,) rmZisdstZtmryZlocaltmZdeltar|dstZgmtoffrrrrks$   rk)r0)NFF)F)NN)NN)r,rr)Nr);__doc____all__rrerervrwrfZ urllib.parserZemail._parseaddrrrrPr r r rZ email.charsetrrOrZ UEMPTYSTRINGZCRLFrcompiler5r7r"rrrr*r/rrArHZsupports_strict_parsingrrZrRrSVERBOSE IGNORECASEZecrerdrrr rr rrrASCIIrrrrkrrrrs        !1     $  8