o ,]_@sRdZddlmZddlmZddlmZddlmZmZmZm Z m Z gdZ ddl Z ddl Z ddlmZdd lmZdd lmZddlmmmZdd lmZmZejZd Zd ZdZdZdZ dZ!dZ"edZ#edZ$e %de j&e j'Be j(BZ)e %dZ*e %dZ+ddZ,  d"ddZ-Gddde.Z/Gddde.Z0Gd d!d!e1Z2dS)#z+Header encoding and decoding functionality.)unicode_literals)division)absolute_import)bytesrangestrsuperzip)Header decode_header make_headerN)email) base64mime)HeaderParseError) _max_append header_decode   z Nz us-asciizutf-8ai =\? # 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 encoded string \?= # literal ?= z[\041-\176]+:$z \n[^ \t]+:c Cst|dr dd|jDSt|s|dfgSg}|D]A}t|}d}|r^|d}|r5|}d}|r?||ddf|r\|d }|d }|d}||||f|s(qddl } g} t |D]%\} } | dkr| dr|| d dr|| dd r| | dqit | D]} || =qg}|D]X\}}}|dur|||fq|d krt|}|||fq|d krt|d }|r|d dd |7}zt|}Wn tjytdw|||fqtd|g}d}}|D]:\}}t|tr t|d}|dur|}|}q||kr(|||f|}|}q|dur4|t|7}q||7}q|||f|S)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). _chunkscSs(g|]\}}t|t|t|fqS)_charset_encoder).0stringcharsetrr?/usr/lib/python3/dist-packages/future/backports/email/header.py Nsz!decode_header..NTrFqbz===zBase64 decoding errorzUnexpected encoding: zraw-unicode-escape)hasattrrecresearch splitlinessplitpoplstripappendlowersys enumerateisspacereversedrlenrdecodebinasciiErrorrAssertionError isinstancerrBSPACE)headerwordslinepartsfirst unencodedrencodingencodedr/droplistnwd decoded_wordsencoded_stringwordpaderr collapsed last_word last_charsetrrrr >s       4          r cCsFt|||d}|D]\}}|durt|tst|}|||q |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_wsN)r r8Charsetr-) decoded_seqrMrNrOhsrrrrr s  r c@sXeZdZ   dddZddZdd Zd d Zdd d ZddZdddZ ddZ dS)r NrstrictcCs~|durt}n t|tst|}||_||_g|_|dur$|||||dur*t}||_|dur6d|_ dSt |d|_ dS)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") USASCIIr8rPr_continuation_wsrr- MAXLINELEN _maxlinelen _headerlenr3)selfrSrrMrNrOerrorsrrr__init__s  zHeader.__init__c Cs|g}d}d}|jD]O\}}|}|tjkr$|dd}|dd}|rL|o.||d}|dvrA|dvr@|s@|td}n |dvrL|sL|t|oT||d}|}||q t |S)z&Return the string value of the header.NasciisurrogateescapereplacerNr) _normalizerr UNKNOWN8BITencoder4 _nonctextr-SPACE EMPTYSTRINGjoin) rZuchunkslastcs lastspacerrnextcsoriginal_byteshasspacerrr__str__s,         zHeader.__str__cCs |t|kSN)rrZotherrrr__eq__s z Header.__eq__cCs ||k Srprrqrrr__ne__  z Header.__ne__cCs|dur|j}n t|tst|}t|ts-|jpd}|tjkr'|dd}n|||}|jp1d}|tjkrPz|||Wnt yO|dkrKt }Ynw|j ||fdS)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. Nrr^) rr8rPr input_codecrcr4 output_codecrdUnicodeEncodeErrorUTF8rr-)rZrSrr[ input_charsetoutput_charsetrrrr- s&        z Header.appendcCs|p|dvS)z=True if string s is not a ctext character of RFC822. )()\)r1)rZrSrrrre7szHeader._nonctext;, rcCs||dur |j}|dkrd}t|j||j|}d}d}}|jD]\}} |durQ|o3||d}ddl} |dvrG|rB| dvrF|n | dvrQ|sQ||oY||d}| }d}| } | rn| d| d| n| dd| | ddD]/} | | j dur| |jd | | q{| } | dt| t| }| || | q{t| dkr| q#|jr|||}t|rtd ||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`raFrr!rz8header value appears to contain an embedded header: {!r})rbrX_ValueFormatterrYrVrrer/add_transitionr)feednewlineheader_encodingr,r3_str_embeded_headerr(rformat)rZ splitcharsrMlinesep formatterrjrnrkrrr/linesr<slinefwsvaluerrrrd<s^!      z Header.encodecCsxg}d}g}|jD]!\}}||kr||q |dur%|t||f|g}|}q |r7|t||f||_dSrp)rr-rfrh)rZchunksrL last_chunkrrrrrrbs  zHeader._normalize)NNNNrrT)NrT)rNr) __name__ __module__ __qualname__r\rorsrtr-rerdrbrrrrr s /  +  Qr c@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS)rcCs0||_||_t||_||_g|_t||_dSrp)_maxlenrVr3_continuation_ws_len _splitchars_lines _Accumulator _current_line)rZ headerlenmaxlenrOrrrrr\s  z_ValueFormatter.__init__cCs|||jSrp)rrhr)rZrrrrrs z_ValueFormatter._strcCs |tSrp)rNLrZrrrroruz_ValueFormatter.__str__cCsp|j}|dkr|jj|t|jdkr1|jr(|jdt|j7<n |jt|j|jdS)N)rrrra) rr+pushr3 is_onlywsrrr-reset)rZ end_of_linerrrrs   z_ValueFormatter.newlinecCs|jdddS)Nrr)rrrrrrrz_ValueFormatter.add_transitioncCs|jdur||||jdS|||}z|d}Wn ty(YdSw|dur3|||z|}Wn tyCYdSw||j |j ||D] }|j |j |qRdSNr)r _ascii_splitrheader_encode_lines _maxlengthsr+ IndexError _append_chunkrrrrVrr-)rZrrr encoded_lines first_line last_liner<rrrrs*     z_ValueFormatter.feedccs&|jt|jV |j|jVq rp)rr3rrrrrrrs z_ValueFormatter._maxlengthscCsftdtd||}|drdg|dd<n|dtt|gdD] \}}|||q&dS)Nz([z]+)rrr")rer*FWSr+r iterr)rZrrrr=partrrrrs z_ValueFormatter._ascii_splitcCs|j||t|j|jkr|jD]:}t|jdddD]*}|r6|j|d}|r6|d|kr6n|j|dd}|rI|d|krInqqn|j\}}|jj dkrb| |sbd}|j||dS|j |}|j t|j|j|dSdS)Nr!rrar)rrr3rrr part_countr1r+ _initial_sizerpop_fromrr-rr)rZrrchiprevpartr remainderrrrrs2   z_ValueFormatter._append_chunkN) rrrr\rrorrrrrrrrrrrs % rcsjeZdZdfdd ZddZdddZfdd Zd d Zd d ZdddZ ddZ fddZ Z S)rrcs||_tdSrp)rrr\)rZ initial_size __class__rrr\"sz_Accumulator.__init__cCs|||fdSrp)r-)rZrrrrrr&rz_Accumulator.pushcCs||d}g||d<|Srpr)rZrpoppedrrrr)s  z_Accumulator.pop_fromcs|dkrdStS)Nr)rr)rrr+rrrrr+.s  z_Accumulator.popcCstdd|D|jS)Ncss$|] \}}t|t|VqdSrp)r3rrrrrr 4s"z'_Accumulator.__len__..)sumrrrrr__len__3sz_Accumulator.__len__cCstdd|DS)Ncss"|] \}}t||fVqdSrprgrhrrrrr8sz'_Accumulator.__str__..rrrrrro7s z_Accumulator.__str__NcCs"|durg}||dd<d|_dSr)r)rZstartvalrrrr;s  z_Accumulator.resetcCs|jdko | p t|Sr)rrr1rrrrrAsz_Accumulator.is_onlywscs tSrp)rrrrrrrDruz_Accumulator.part_count)rrp) rrrr\rrr+rrorrr __classcell__rrrrr s   r)NNr)3__doc__ __future__rrrfuture.builtinsrrrrr __all__rr5future.backportsr future.backports.emailrfuture.backports.email.errorsrfuture.backports.email.charset backportsrr!future.backports.email.quoprimimerrrPrrfr9SPACE8rgrWrrUrycompileVERBOSE IGNORECASE MULTILINEr'fcrerr r objectr rlistrrrrrsJ       _ n