f^dZgdZddlZddlZddlZddlZddlmZddlm Z e jZ dZ dZ dZd Zd Zd Zd Ze d Ze dZej*dej,ej.zZej*dZej*dZej6j8ZdZ ddZGddZGddZ Gdde!Z"y)z+Header encoding and decoding functionality.)Header decode_header make_headerN)HeaderParseError)charset   z Nz us-asciizutf-8ai =\? # literal =? (?P[^?]*?) # non-greedy up to the next ? is the charset \? # literal ? (?P[qQbB]) # either a "q" or a "b", case insensitive \? # literal ? (?P.*?) # non-greedy up to the next ?= is the encoded string \?= # literal ?= z[\041-\176]+:$z \n[^ \t]+:c t|drG|jDcgc]/\}}tj|t |t |f1c}}St j |s|dfgSg}|jD]}t j|}d}|s|jd}|r|j}d}|r|j|ddf|rc|jdj}|jdj}|jd} |j| ||f|rg} t|D]K\} } | dkDs | ds|| dz ds|| dz djs8| j| dz Mt| D]} || =g}|D]\}}}||j||f|dk(r3t j"j%|}|j||fU|d k(rOt'|d z}|r |d dd |z z } t j(j+|}|j||ft3d |zg}dx}}|D]Y\}}t5|tr t7|d}||}|})||k7r|j||f|}|}F| |t8|zz }U||z }[|j||f|Scc}}w#t,j.$r t1d wxYw)a;Decode a message header value without converting charset. Returns a list of (string, charset) pairs containing each of the decoded parts of the header. Charset is None for non-encoded parts of the header, otherwise a lower-case string containing the name of the character set specified in the encoded string. header may be a string that may or may not contain RFC2047 encoded words, or it may be a Header object. An email.errors.HeaderParseError may be raised when certain decoding error occurs (e.g. a base64 decoding exception). _chunksNTrFqbz===zBase64 decoding errorzUnexpected encoding: zraw-unicode-escape)hasattrr_charset_encodestrecresearch splitlinessplitpoplstripappendlower enumerateisspacereversedemail quoprimime header_decodelen base64mimedecodebinasciiErrorrAssertionError isinstancebytesBSPACE)headerstringrwordslinepartsfirst unencodedencodingencodeddroplistnwd decoded_wordsencoded_stringwordpaderr collapsed last_word last_charsets #/usr/lib/python3.12/email/header.pyrr;sNvy!+1>>;'!!&#g,7WF; ; ;;v  E!!#; 4  ! I%,,.  it45))A,,,. 99Q<--/))A, gx9:;"H% !1 Q31Q4E!A#JqMeAaCjm.C.C.E OOAaC !h  !H M-2E)'    .'!: ; _##11.AD  $ 1 _(1,F% V"44 6''..~>$$dG_5 !88!CD D%E*I##I &  g dC 34D  I"L  $   i6 7I"L  ! $ &I  I i./ W;d>> @&'>?? @s4K%*K++L ct|||}|D]4\}}|t|ts t|}|j||6|S)aCreate a Header from a sequence of pairs as returned by decode_header() decode_header() takes a header value string and returns a sequence of pairs of the format (decoded_string, charset) where charset is the string name of the character set. This function takes one of those sequence of pairs and returns a Header instance. Optional maxlinelen, header_name, and continuation_ws are as in the Header constructor. ) maxlinelen header_namecontinuation_ws)rr-Charsetr) decoded_seqrFrGrHhsrs rDrrsV *+. 0A! 7  z'7'Cg&G G  HcBeZdZ d dZdZdZd dZdZd dZdZ y) rNc|t}nt|ts t|}||_||_g|_||j ||||t}||_|d|_ yt|dz|_ y)aDCreate a MIME-compliant header that can contain many character sets. Optional s is the initial header value. If None, the initial header value is not set. You can later append to the header with .append() method calls. s may be a byte string or a Unicode string, but see the .append() documentation for semantics. Optional charset serves two purposes: it has the same meaning as the charset argument to the .append() method. It also sets the default character set for all subsequent .append() calls that omit the charset argument. If charset is not provided in the constructor, the us-ascii charset is used both as s's initial charset and as the default for subsequent .append() calls. The maximum line length can be specified explicitly via maxlinelen. For splitting the first line to a shorter value (to account for the field header which isn't included in s, e.g. `Subject') pass in the name of the field in header_name. The default maxlinelen is 78 as recommended by RFC 2822. continuation_ws must be RFC 2822 compliant folding whitespace (usually either a space or a hard tab) which will be prepended to continuation lines. errors is passed through to the .append() call. Nrr) USASCIIr-rIr_continuation_wsrr MAXLINELEN _maxlinelen _headerlenr')selfrLrrFrGrHerrorss rD__init__zHeader.__init__s: ?GGW-g&G / = KK7F +  #J%  DO"+.2DOrMc|jg}d}d}|jD]\}}|}|tjk(r$|j dd}|j dd}|rU|xr|j |d}|dvr|dvr5|s3|jtd}n|dvr|s|jt|xr|j |d}|}|j|tj|S)z&Return the string value of the header.NasciisurrogateescapereplacerNr ) _normalizerr UNKNOWN8BITencoder) _nonctextrSPACE EMPTYSTRINGjoin) rUuchunkslastcs lastspacer1rnextcsoriginal_byteshasspaces rD__str__zHeader.__str__s  #|| #OFGF---!'w8I!J'..w B!?dnnVAY&?!33!33Hu-!%#55iNN5)=4>>&*#=IF NN6 "+ #,((rMc|t|k(SN)r)rUothers rD__eq__z Header.__eq__sD !!rMc| |j}nt|ts t|}t|tsH|jxsd}|tj k(r|j dd}n|j ||}|jxsd}|tj k7r |j|||jj||fy#t$r|dk7rt}Y5wxYw)a.Append a string to the MIME header. Optional charset, if given, should be a Charset instance or the name of a character set (which will be converted to a Charset instance). A value of None (the default) means that the charset given in the constructor is used. s may be a byte string or a Unicode string. If it is a byte string (i.e. isinstance(s, str) is false), then charset is the encoding of that byte string, and a UnicodeError will be raised if the string cannot be decoded with that charset. If s is a Unicode string, then charset is a hint specifying the character set of the characters in the string. In either case, when producing an RFC 2822 compliant header using RFC 2047 rules, the string will be encoded using the output codec of the charset. If the string cannot be encoded to the output codec, a UnicodeError will be raised. Optional `errors' is passed as the errors argument to the decode call if s is a byte string. Nr rZ) rr-rIr input_codecr_r) output_codecr`UnicodeEncodeErrorUTF8rr)rUrLrrV input_charsetoutput_charsets rDrz Header.appends* ?mmGGW-g&G!S!#//=:M 4 44HHZ):;HH]F3!--; X11 1 0 QL) & !:- s'CC/.C/c.|jxs|dvS)z=True if string s is not a ctext character of RFC822. )()\)r")rUrLs rDrazHeader._nonctext0syy{3a#333rMc.|j| |j}|dk(rd}t|j||j|}d}dx}}|j D][\}} |I|xr|j |d}|dvr|r| dvr'|jn| dvr|s|j|xr|j |d}| }d}|j} | r|jd| d| n|jdd| | ddD]} |j| j/|j|jd | jz| N| j} | dt| t| z } |j| | | t| dkDsL|j^|j r|j|j|}tj!|rt#d j%||S) aEncode a message header into an RFC-compliant format. There are many issues involved in converting a given string for use in an email header. Only certain character sets are readable in most email clients, and as header strings can only contain a subset of 7-bit ASCII, care must be taken to properly convert and encode (with Base64 or quoted-printable) header strings. In addition, there is a 75-character length limit on any given encoded header field, so line-wrapping must be performed, even with double-byte character sets. Optional maxlinelen specifies the maximum length of each generated line, exclusive of the linesep string. Individual lines may be longer than maxlinelen if a folding point cannot be found. The first line will be shorter by the length of the header name plus ": " if a header name was specified at Header construction time. The default value for maxlinelen is determined at header construction time. Optional splitchars is a string containing characters which should be given extra weight by the splitting algorithm during normal header wrapping. This is in very rough support of RFC 2822's `higher level syntactic breaks': split points preceded by a splitchar are preferred during line splitting, with the characters preferred in the order in which they appear in the string. Space and tab may be included in the string to indicate whether preference should be given to one over the other as a split point when other split chars do not appear in the line being split. Splitchars does not affect RFC 2047 encoded lines. Optional linesep is a string to be used to separate the lines of the value. The default value is the most useful for typical Python applications, but it can be set to \r\n to produce RFC-compliant line separators when needed. Nri@Br\r]Fr rr z8header value appears to contain an embedded header: {!r})r^rS_ValueFormatterrTrQrraadd_transitionrfeednewlineheader_encodingrr'_str_embedded_headerrrformat)rU splitcharsrFlinesep formatterrfrjrgr1rlinesr3slinefwsvalues rDr`z Header.encode5sB   ))J ? J#DOOZ$($9$9:G ##9#|| $OFG#!?dnnVAY&?!33#w6H'H!002$66y,,.=4>>&*#=IFH%%'Er58W5r2w/ab  8!!#**6NN4#8#8# :M#*,!KKME4D #e* 45CNN3w7 85zA~!!#5 $6 <<  $ $ &w'  " "5 )"$++16%=: : rMcg}d}g}|jD]I\}}||k(r|j||&|jtj||f|g}|}K|r&|jtj||f||_yrm)rrrbrd)rUchunksrC last_chunkr1rs rDr^zHeader._normalizes  #|| 'OFG,&!!&)+MM5::j#9<"HI$X &  '  MM5::j1<@ A rM)NNNNr strict)Nr)z;, Nr) __name__ __module__ __qualname__rWrkrorrar`r^rMrDrrs3'+.2-5-3^)@" )*V4 N`rMrcBeZdZdZdZdZdZdZdZdZ dZ d Z y ) r|c|||_||_t||_||_g|_t ||_yrm)_maxlenrQr'_continuation_ws_len _splitchars_lines _Accumulator _current_line)rU headerlenmaxlenrHrs rDrWz_ValueFormatter.__init__s: /$'$8!% ))4rMcX|j|j|jSrm)rrdr)rUrs rDrz_ValueFormatter._strs ||DKK((rMc,|jtSrm)rNLrUs rDrkz_ValueFormatter.__str__syy}rMc|jj}|dk7r|jj|t|jdkDr|jj r7|j r+|j dxxt |jz cc<n.|j jt |j|jjy)N)r r rr]) rrpushr' is_onlywsrrrreset)rU end_of_lines rDrz_ValueFormatter.newlines((,,. ) # #D   # #[ 1 t!! "Q &!!++-$++ B3t'9'9#:: ""3t'9'9#:;   "rMc<|jjddy)Nr r )rrrs rDr}z_ValueFormatter.add_transitions R(rMc |j|j|||jy|j||j } |j d}||j|| |j }|j|jj|j||D]*}|jj|j|z,y#t $rYywxYw#t $rYywxYwNr)r _ascii_splitrheader_encode_lines _maxlengthsr IndexError _append_chunkrrrrQrr)rUrr1r encoded_lines first_line last_liner3s rDr~z_ValueFormatter.feeds  " " *   c64+;+; <  33FDq@!RH Azz|"003A63q62:!#11!A#6q9HHRL"$4  !..224 T%%33a7LLN"""''T2**33A6I KK  s4#5#56 7    $ $Y /9 2rMN) rrrrWrrkrr}r~rrrrrMrDr|r|s05) #)#=J; *,0rMr|c\eZdZd fd ZdZd dZfdZdZdZd dZ dZ fd Z xZ S) rc0||_t| yrm)rsuperrW)rU initial_size __class__s rDrWz_Accumulator.__init__s) rMc*|j||fyrm)r)rUrr1s rDrz_Accumulator.pushs S&M"rMc||d}g||d|Srmr)rUrpoppeds rDrz_Accumulator.pop_from!sabQR rMcH|jdk(ryt| S)Nr)r r )rrrrUrs rDrz_Accumulator.pop&s! ?? a w{}rMc<td|D|jS)Nc3PK|]\}}t|t|z ywrm)r'.0rrs rD z'_Accumulator.__len__..,s!=93CHSY&=$&)sumrrs rD__len__z_Accumulator.__len__+s ==%%' 'rMc:tjd|DS)Nc3PK|]\}}tj||f ywrmrcrdrs rDrz'_Accumulator.__str__..0s+!7$-C"-!1!13+!>!7rrrs rDrkz_Accumulator.__str__/s"!715!78 8rMc$|g}||ddd|_yr)r)rUstartvals rDrz_Accumulator.reset3s  HQrMc`|jdk(xr| xst|jSr)rrr"rs rDrz_Accumulator.is_onlyws9s,!!1$Jd(*Ic$i6G6G6IJrMc t|Srm)rrrs rDrz_Accumulator.part_count<sw  rM)rrm) rrrrWrrrrrkrrr __classcell__)rs@rDrrs6#  '8 K!!rMr)NNr )#__doc____all__rr*email.quoprimimer$email.base64mime email.errorsrrrrIrrbr/SPACE8rcrRrrPrtcompileVERBOSE MULTILINErfcrerr% _max_appendrrrr|listrrrMrDrs 2   )%           * wrzz zzBLL "rzz#$2::m,** [|;? # ,ffR}0}0@%!4%!rM