o w[en4@sTdZgdZddlZddlZddlZddlZddlZddlZddlZ ddl m Z ddl m Z ddl mZddl mZmZmZddlmZd Zd Zd Zd Zd Zed ZedZddZddZd5ddZddZddZ d6ddZ!d7ddZ"d8dd Z#d!d"Z$d#d$Z%d%d&Z&d'd(Z'd8d)d*Z(ed+ej)Z*d,d-Z+ . /d9d0d1Z,d:d3d4Z-dS);zMiscellaneous 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[\\"]cCs$z|WdStyYdSw)z8Return True if s contains surrogate-escaped binary data.FT)encodeUnicodeEncodeError)sr"/usr/lib/python3.10/email/utils.py_has_surrogates3s  rcCs|dd}|ddS)Nutf-8surrogateescapereplace)rdecode)stringoriginal_bytesrrr _sanitize@s  r#rcCs|\}}|d|rGz|dWnty/t|tr"t|}||}d||fYSwd}t|r9d}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>) rr isinstancestrr header_encode specialsresearch escapesresub)paircharsetnameaddress encoded_namequotesrrrrLs"      rcCs"tdd|D}t|}|jS)z7Return a list of (REALNAME, EMAIL) for each fieldvalue.css|]}t|VqdS)Nr').0vrrr pszgetaddresses..) COMMASPACEjoin _AddressList addresslist) fieldvaluesallarrrrnsrc CsHdgd|d|dgd|dd|d|d|d |d |fS) Nz"%s, %02d %s %04d %02d:%02d:%02d %s)MonTueWedThuFriSatSun) JanFebMarAprMayJunJulAugSepOctNovDecrr) timetuplezonerrr_format_timetuple_and_zoneus rYFcCsR|durt}|s |rtj|tjj}ntj|}|r$|}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 localtimeusegmtdtrrrr~s  rcCsV|}|r|jdus|jtjjkrtdd}n |jdur!d}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)rWtzinfor[r]r^ ValueErrorstrftimerY)rdrcnowrXrrrrs   rcCs^ttd}t}td}|durd}nd|}|dur$t}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>)intrZosgetpidrandom getrandbitssocketgetfqdn)idstringdomainrapidrandintmsgidrrrr s  r cCsjt|}|durtdt||^}}|dur"tj|ddStj|dddttj|diS)Nz!Invalid date value or format "%s"rErfseconds)rrgr'r[r] timedelta)dataparsed_date_tzdtupletzrrrrs rcCst|j}|s dS|dS)z 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 ('', ''). )rrr)r9r:)addraddrsrrrr s r cCs`t|dkr.|dr|dr|ddddddS|dr.|dr.|ddS|S) zRemove quotes from a string.rSr%z\\\z\"<>)len startswithendswithrr3rrrrs  rcCs&|td}t|dkrdd|fS|S)z#Decode string according to RFC 2231rFN)splitTICKr)rpartsrrrrs   rcCsDtjj|d|pdd}|dur|dur|S|durd}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)rr.languagerrrrs rz&^(?P\w+)\*((?P[0-9]+)\*?)?$c Cs<|dg}i}|ddD]<\}}|d}t|}t|}|r>|dd\}}|dur1t|}||g|||fq ||dt|fq |r| D]K\}}g}d} | |D]\}} }|rot j j| d d } d } || q^tt |}| rt|\} } }||| | d|ffqP||d|fqP|S) zDecode parameters list according to RFC 2231. params is a sequence of 2-tuples containing (param name, string value). rrSN*r/numz"%s"Fzlatin-1)rT)rrrfc2231_continuationmatchgrouprm setdefaultappendritemssortrr EMPTYSTRINGr8r) params new_paramsrfc2231_paramsr/valueencodedmor continuationsextendedrr.rrrrrs8    rrus-asciicCsht|tr t|dkrt|S|\}}}|dur|}t|d}zt|||WSty3t|YSw)NrTzraw-unicode-escape)r&tuplerrbytesr' LookupError)rerrorsfallback_charsetr.rtextrawbytesrrrr4s    rrc Cs|durtjtjjS|jdur|S|dd|f}t|}t |}ztj |j d}t||j }Wn?t y{|tjt|dd}tjoX|jdk}|r^tjntj }|tj |dkrtt|tj|}nt|}Ynw|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. NrryrEr)rf)r[rir]r^r`rfrWrZmktimerbr{ tm_gmtofftm_zoneAttributeErrorgmtimedaylighttm_isdstaltzonetznamer) rdisdsttmrzlocaltmdeltardstgmtoffrrrrbNs(      rb)r)NFF)F)NN)rr)Nr).__doc____all__rnrerZrprrr[ urllib.parseremail._parseaddrrrr9r r r r email.charsetrr7r UEMPTYSTRINGCRLFrcompiler)r+rr#rrrYrrr rr rrrASCIIrrrrbrrrrsT       "    0