o w[eX@sdZddgZddlZddlmZddlmZddlmZddl m Z e d Z e d Z e d Ze d Ze d Zd ZdZeZGdddeZGdddZGdddeZdS)aFeedParser - An email feed parser. The feed parser implements an interface for incrementally parsing an email message, line by line. This has advantages for certain applications, such as those reading email messages off a socket. FeedParser.feed() is the primary interface for pushing new data into the parser. It returns when there's nothing more it can do with the available data. When you have no more data to push into the parser, call .close(). This completes the parsing and returns the root message object. The other advantage of this parser is that it will never raise a parsing exception. Instead, when it finds something unexpected, it adds a 'defect' to the current message. Defects are just instances that live on the message object's .defects attribute. FeedParserBytesFeedParserN)errors)compat32)deque)StringIOz \r\n|\r|\nz (\r\n|\r|\n)z(\r\n|\r|\n)\Zz%^(From |[\041-\071\073-\176]*:|[\t ]) c@s`eZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ dS)BufferedSubFileakA file-ish object that can have new data loaded into it. You can also push and pop line-matching predicates onto a stack. When the current predicate matches the current line, a false EOF response (i.e. empty string) is returned instead. This lets the parser adhere to a simple abstraction -- it parses until EOF closes the current message. cCs$tdd|_t|_g|_d|_dS)Nr)newlineF)r_partialr_lines _eofstack_closedselfr'/usr/lib/python3.10/email/feedparser.py__init__5s  zBufferedSubFile.__init__cC|j|dSN)rappend)rpredrrrpush_eof_matcher@z BufferedSubFile.push_eof_matchercCs |jSr)rpoprrrrpop_eof_matcherC zBufferedSubFile.pop_eof_matchercCs<|jd||j|jd|jd|_dS)NrT)r seek pushlines readlinestruncaterrrrrcloseFs    zBufferedSubFile.closecCsL|js |jrdStS|j}t|jD]}||r#|j|dSq|SNr)r r NeedMoreDatapopleftreversedr appendleft)rlineateofrrrreadlineNs  zBufferedSubFile.readlinecCs|tusJ|j|dSr)r$r r'rr(rrr unreadline`s zBufferedSubFile.unreadlinecCsx|j|d|vrd|vrdS|jd|j}|jd|j|dds5|j|||dS)z$Push some new data into this object.r  Nr)r writerr r!endswithrr)rdatapartsrrrpushes     zBufferedSubFile.pushcCrr)r extend)rlinesrrrrzrzBufferedSubFile.pushlinescCs|Srrrrrr__iter__}szBufferedSubFile.__iter__cCs|}|dkr t|Sr#)r* StopIterationr+rrr__next__szBufferedSubFile.__next__N)__name__ __module__ __qualname____doc__rrrr"r*r,r3rr6r8rrrrr -s  r c@s`eZdZdZdedddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ dS)rzA feed-style parser of email.NpolicycCs||_d|_|dur|jdurddlm}||_n|j|_n||_z||jdWn ty5d|_Ynwt|_g|_ | j |_ d|_ d|_d|_dS)a_factory is called with no arguments to create a new message obj The policy keyword specifies a policy object that controls a number of aspects of the parser's operation. The default policy maintains backward compatibility. FNr)Messager=T)r>_old_style_factorymessage_factory email.messager?_factory TypeErrorr _input _msgstack _parsegenr8_parse_cur_last _headersonly)rrCr>r?rrrrs&       zFeedParser.__init__cCs d|_dS)NT)rKrrrr_set_headersonlyrzFeedParser._set_headersonlycCs|j||dS)zPush more data into the parser.N)rEr3 _call_parserr1rrrfeeds  zFeedParser.feedcCs$z|WdStyYdSwr)rHr7rrrrrMs  zFeedParser._call_parsecCsR|j||}|jrJ|dkr'|s't}|j |||S)z handle_defect)rrootdefectrrrr"s   zFeedParser.closecCsn|jr|}n|j|jd}|jr|jdkr|d|jr)|jd||j|||_||_ dS)Nr=zmultipart/digestzmessage/rfc822r.) r@rCr>rIget_content_typeset_default_typerFattachrrJ)rmsgrrr _new_messages    zFeedParser._new_messagecCs*|j}|jr|jd|_|Sd|_|S)Nr.)rFrrI)rretvalrrrrQs  zFeedParser._pop_messageccs|g}|jD]-}|turtVq t|s2t|s0t}|j |j ||j |n| |q | ||jrfg} |j}|turPtVqB|dkrUn| |qC|j t|dS|j dkr |jtj|D] }|turtVqy|}|j |j}|turtVq |j}|turtVq |dkr dS|j |qn|j dkr|D] }|turtVq|dS|j dkr|j }|durt}|j |j |g}|jD]}|turtVq| |q|j t|dSt|j dddvr.t}|j |j |d |}t d t!|d } d} g} d } d } |j}|turVtVqF|dkr\n| |}|r@|"d rrd} |"d} n| r| r| d}t#$|}|r|dt%|"d | d<t| |j _&d } |j |qF |j}|turtVq| |}|s|j |nq|j| j|D] }|turtVq|j'dkr|j'j(}|dkrd|j'_(n@|durt#$|}|rt%|"d}|d| |j'_(n"|j'j)}t*|tr2t#$|}|r2|dt%|"d }||j'_)|j||j |_'n | sEJ| |qG| rt+}|j |j ||j t| g}|jD] }|turutVqiqit||j _(dS| st,}|j |j |dS| rdg}ng}|jD]}|turtVq| |q|r|d}t-|}|r|t%|"dd|d<t||j _(dSg}|jD]}|turtVq| |q|j t|dS)NTrzmessage/delivery-statusmessagerPzcontent-transfer-encoding8bit)7bitr_binaryz--z(?Pz4)(?P--)?(?P[ \t]*)(?P\r\n|\r|\n)?$Fendlinesepr.r).r\rEr$headerREmatchNLCREr MissingHeaderBodySeparatorDefectr>rUrIr,r_parse_headersrKr* set_payload EMPTYSTRINGjoinrXrrGrQrrR get_boundaryNoBoundaryInMultipartDefectstrgetlower-InvalidMultipartContentTransferEncodingDefectrecompileescapegroup NLCRE_eolsearchlenpreamblerJepilogue_payload isinstanceStartBoundaryNotFoundDefectCloseBoundaryNotFoundDefect NLCRE_bol)rheadersr(rWr5r]r[boundary separator boundaryrecapturing_preambleryrcclose_boundary_seenmolastlineeolmorzrbpayload firstlinebolmorrrrGs|             !                               S       zFeedParser._parsegenc Csdd}g}t|D]\}}|ddvr(|s"t|}|j|j|q||q|r9|jj|j|dg}}| drz|dkr\t |}|rU|dt | d }|j|q|t |dkrm|j|dSt|}|jj|q|d}|dkrtd}|jj|q|dksJd|d|}|g}q|r|jj|j|dSdS) Nrrz zFrom :zMissing header name.z3_parse_headers fed line with no : and no leading WS) enumerater#FirstHeaderLineIsContinuationDefectr>rUrIrset_rawheader_source_parse startswithrvrwrxru set_unixfromrEr,MisplacedEnvelopeHeaderDefectdefectsfindInvalidHeaderDefect) rr5 lastheader lastvaluelinenor(rWrirrrrhsJ            zFeedParser._parse_headersr)r9r:r;r<rrrLrOrMr"r\rQrGrhrrrrrs   ~cs eZdZdZfddZZS)rz(Like FeedParser, but feed accepts bytes.cst|dddS)Nasciisurrogateescape)superrOdecoderN __class__rrrOszBytesFeedParser.feed)r9r:r;r<rO __classcell__rrrrrs)r<__all__rremailremail._policybaser collectionsriorrsrfrrv NLCRE_crackrdrjNLobjectr$r rrrrrrs*         [