
    F\hrY                     v   S r SS/rSSKrSSKJr  SSKJr  SSKJr  SSK	J
r
  \R                  " S	5      r\R                  " S
5      r\R                  " S5      r\R                  " S
5      r\R                  " S5      rSrSr\R                  " S5      r\" 5       r " S S\5      r " S S5      r " S S\5      rg)a  FeedParser - 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.

FeedParserBytesFeedParser    N)errors)compat32)deque)StringIOz
\r\n|\r|\nz(\r\n|\r|\n)z(\r\n|\r|\n)\Zz%^(From |[\041-\071\073-\176]*:|[\t ]) 
z3(?P<end>--)?(?P<ws>[ \t]*)(?P<linesep>\r\n|\r|\n)?$c                   T    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rSrg)BufferedSubFile.   aW  A 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.
c                 Z    [        SS9U l        [        5       U l        / U l        SU l        g )Nr	   )newlineF)r   _partialr   _lines	_eofstack_closedselfs    '/usr/lib/python3.13/email/feedparser.py__init__BufferedSubFile.__init__6   s'     !,g    c                 :    U R                   R                  U5        g N)r   append)r   preds     r   push_eof_matcher BufferedSubFile.push_eof_matcherA   s    d#r   c                 6    U R                   R                  5       $ r   )r   popr   s    r   pop_eof_matcherBufferedSubFile.pop_eof_matcherD   s    ~~!!##r   c                    U R                   R                  S5        U R                  U R                   R                  5       5        U R                   R                  S5        U R                   R	                  5         SU l        g )Nr   T)r   seek	pushlines	readlinestruncater   r   s    r   closeBufferedSubFile.closeG   sV    1t}}..011 r   c                    U R                   (       d  U R                  (       a  g[        $ U R                   R                  5       n[	        U R
                  5       H-  nU" U5      (       d  M  U R                   R                  U5          g   U$ Nr	   )r   r   NeedMoreDatapopleftreversedr   
appendleft)r   lineateofs      r   readlineBufferedSubFile.readlineO   sg    {{|| {{""$ dnn-ET{{&&t,	 .
 r   c                 P    U[         Ld   eU R                  R                  U5        g r   )r-   r   r0   r   r1   s     r   
unreadlineBufferedSubFile.unreadlinea   s"    <'''t$r   c                    U R                   R                  U5        SU;  a  SU;  a  gU R                   R                  S5        U R                   R                  5       nU R                   R                  S5        U R                   R	                  5         US   R                  S5      (       d)  U R                   R                  UR                  5       5        U R                  U5        g)z$Push some new data into this object.r
   Nr   )r   writer%   r'   r(   endswithr!   r&   )r   datapartss      r   pushBufferedSubFile.pushf   s    D!tD 0 	1'')1  Ry!!$''MM		,ur   c                 :    U R                   R                  U5        g r   )r   extend)r   liness     r   r&   BufferedSubFile.pushlines{   s    5!r   c                     U $ r    r   s    r   __iter__BufferedSubFile.__iter__~   s    r   c                 >    U R                  5       nUS:X  a  [        eU$ r,   )r3   StopIterationr6   s     r   __next__BufferedSubFile.__next__   s    }}2:r   )r   r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   r   r"   r)   r3   r7   r@   r&   rH   rL   __static_attributes__rG   r   r   r   r   .   s9    	$$$%
*"r   r   c                   Z    \ rS rSrSrS\S.S jjrS rS rS r	S	 r
S
 rS rS rS rSrg)r      zA feed-style parser of email.Npolicyc                l   X l         SU l        Uc,  UR                  c  SSKJn  X0l        O)UR                  U l        OXl         U" U R                   S9  [        5       U l        / U l	        U R                  5       R                  U l        SU l        SU l        SU l        g! [         a
    SU l         N^f = f)z_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   )MessagerV   T)rW   _old_style_factorymessage_factoryemail.messagerY   _factory	TypeErrorr   _input	_msgstack	_parsegenrL   _parse_cur_last_headersonly)r   r]   rW   rY   s       r   r   FeedParser.__init__   s     "'%%-1 ' & 6 6$M/, &'nn&//	
!  /*.'/s   B B32B3c                     SU l         g )NT)re   r   s    r   _set_headersonlyFeedParser._set_headersonly   s
     r   c                 Z    U R                   R                  U5        U R                  5         g)zPush more data into the parser.N)r_   r@   _call_parse)r   r>   s     r   feedFeedParser.feed   s     r   c                 F     U R                  5         g ! [         a     g f = fr   )rb   rK   r   s    r   rk   FeedParser._call_parse   s"    	KKM 		s    
  c                 t   U R                   R                  5         U R                  5         U R                  5       nU R                  (       a   eUR                  5       S:X  aV  UR                  5       (       dA  U R                  (       d0  [        R                  " 5       nU R                  R                  X5        U$ )z<Parse all remaining data and return the root message object.	multipart)r_   r)   rk   _pop_messager`   get_content_maintypeis_multipartre   r   !MultipartInvariantViolationDefectrW   handle_defect)r   rootdefects      r   r)   FeedParser.close   s      ">>!!$$&+5((**43D3D==?FKK%%d3r   c                    U R                   (       a  U R                  5       nOU R                  U R                  S9nU R                  (       a/  U R                  R	                  5       S:X  a  UR                  S5        U R                  (       a  U R                  S   R                  U5        U R                  R                  U5        Xl        Xl	        g )NrV   zmultipart/digestzmessage/rfc822r;   )
rZ   r]   rW   rc   get_content_typeset_default_typer`   attachr   rd   )r   msgs     r   _new_messageFeedParser._new_message   s    ""--/C--t{{-3C993359KK  !12>>NN2%%c*c"	
r   c                     U R                   R                  5       nU R                   (       a  U R                   S   U l        U$ S U l        U$ )Nr;   )r`   r!   rc   )r   retvals     r   rr   FeedParser._pop_message   s@    ##%>>r*DI  DIr   c              #     ^#    U R                  5         / nU R                   H  nU[        L a
  [        v   M  [        R	                  U5      (       dr  [
        R	                  U5      (       dV  [        R                  " 5       nU R                  R                  U R                  U5        U R                  R                  U5          OUR                  U5        M     U R                  U5        U R                  (       ay  / n U R                  R                  5       nU[        L a
  [        v   M.  US:X  a  OUR                  U5        MG  U R                  R!                  ["        R%                  U5      5        g U R                  R'                  5       S:X  Ga   U R                  R)                  [
        R                  5        U R+                  5        H  nU[        L a
  [        v   M    O   U R-                  5         U R                  R/                  5          U R                  R                  5       nU[        L a
  [        v   M.    U R                  R                  5       nU[        L a
  [        v   M.   US:X  a   g U R                  R                  U5        GM  U R                  R1                  5       S:X  a;  U R+                  5        H  nU[        L a
  [        v   M    O   U R-                  5         g U R                  R1                  5       S:X  Ga  U R                  R3                  5       nUc  [        R4                  " 5       nU R                  R                  U R                  U5        / nU R                   H'  nU[        L a
  [        v   M  UR                  U5        M)     U R                  R!                  ["        R%                  U5      5        g [7        U R                  R9                  SS5      5      R;                  5       S;  a;  [        R<                  " 5       nU R                  R                  U R                  U5        S	U-   mU4S
 jnSn/ n	Sn
Sn U R                  R                  5       nU[        L a
  [        v   M.  US:X  a  GOU" U5      nU(       Ga  UR?                  S5      (       a  SnUR?                  S5      n
GOU(       a  U	(       af  U	S   n[@        RC                  U5      nU(       a!  US [E        UR?                  S5      5      *  U	S'   ["        R%                  U	5      U R                  l#        SnU R                  R                  U5        GM   U R                  R                  5       nU[        L a
  [        v   M.  U" U5      nU(       d  U R                  R                  U5        OMZ  U R                  R)                  U5        U R+                  5        H  nU[        L a
  [        v   M    O   U RH                  R1                  5       S:X  a}  U RH                  RJ                  nUS:X  a  S U RH                  l%        OUbK  [@        RC                  U5      nU(       a/  [E        UR?                  S5      5      nUS U*  U RH                  l%        OvU RH                  RL                  n[O        U[6        5      (       aK  [@        RC                  U5      nU(       a/  US [E        UR?                  S5      5      *  nUU RH                  l&        U R                  R/                  5         U R-                  5         U R                  U l$        OU(       d   eU	R                  U5        GM  U(       a  [        RP                  " 5       nU R                  R                  U R                  U5        U R                  R!                  ["        R%                  U	5      5        / nU R                   H  nU[        L d  M  [        v   M     ["        R%                  U5      U R                  l%        g U(       d<  [        RR                  " 5       nU R                  R                  U R                  U5        g U
(       a  S/nO/ nU R                   H'  nU[        L a
  [        v   M  UR                  U5        M)     U(       aA  US   n[T        R	                  U5      nU(       a   U[E        UR?                  S5      5      S  US'   ["        R%                  U5      U R                  l%        g / nU R                   H'  nU[        L a
  [        v   M  UR                  U5        M)     U R                  R!                  ["        R%                  U5      5        g 7f)NTr	   zmessage/delivery-statusmessagerq   zcontent-transfer-encoding8bit)7bitr   binaryz--c                 p   > U R                  T5      (       d  g [        R                  U [        T5      5      $ r   )
startswithboundaryendREmatchlen)r1   	separators    r   boundarymatch+FeedParser._parsegen.<locals>.boundarymatchL  s,    y11$**4Y@@r   Fendlinesepr;   r   )+r   r_   r-   headerREr   NLCREr    MissingHeaderBodySeparatorDefectrW   rv   rc   r7   r   _parse_headersre   r3   set_payloadEMPTYSTRINGjoinr{   r   ra   rr   r"   rs   get_boundaryNoBoundaryInMultipartDefectstrgetlower-InvalidMultipartContentTransferEncodingDefectgroup	NLCRE_eolsearchr   preamblerd   epilogue_payload
isinstanceStartBoundaryNotFoundDefectCloseBoundaryNotFoundDefect	NLCRE_bol)r   headersr1   rx   rD   r   boundaryr   capturing_preambler   r   close_boundary_seenmolastlineeolmor   r   payload	firstlinebolmor   s                       @r   ra   FeedParser._parsegen   s     KKD|#"">>$'' {{4((#DDFFKK--dii@KK**40NN4     	G$ E{{++-<'&&2:T"  II!!+"2"25"9:99%%'+DD ,,U[[9"nn.F-** 	 /
 !!# ++-
 ;;//1D|+** ;;//1D|+** 2:  &&t,? B 99))+y8 ..*\)&&	 +
 99))+{:yy--/H
  ;;=))$))V< KKD|+** LL&	 (
 		%%k&6&6u&=>DIIMM"=vFGMMO56MMO))$))V<
 xIA "&HG"'{{++-<'&&2:"4(
 xx.2+"$((9"5)# (0|H$-$4$4X$>E$/78M#ekk!n:M9M/N1<1A1A(1KDII.-2*..t4 
 #{{335</"..$*40! KK2248!  KK00?"&.."2!\1"..$	 #3 zz668KG#'::#6#6#r>26DJJ/%1!*!1!1(!;B!&)"((1+&66>uo

 3"&**"5"5%gs33!*!1!1'!:B!*12DC4D3D*E6=

 3KK//1%%' "&DJ .--OOD)_ f ";;=))$))V<		%%k&6&6x&@A KKD|+**  ( &1%5%5h%?		" ';;=))$))V< 4<'&&%	 $ $QK	!	2"+CA,?,@"AHQK!,!1!1(!;DII KKD|#""LL	  
 			k..u56s   _&e:-Fe:c                 |   Sn/ n[        U5       GH  u  pEUS   S;   aX  U(       d>  [        R                  " U5      nU R                  R	                  U R
                  U5        MT  UR                  U5        Mg  U(       a5  U R
                  R                  " U R                  R                  U5      6   S/ p2UR                  S5      (       a  US:X  aX  [        R                  U5      nU(       a  US [        UR                  S5      5      *  nU R
                  R                  U5        GM  U[        U5      S-
  :X  a  U R                  R!                  U5          g [        R"                  " U5      nU R
                  R$                  R                  U5        GM  UR'                  S5      nUS:X  a>  [        R(                  " S5      nU R
                  R$                  R                  U5        GM  US:  d   S5       eUS U nU/nGM     U(       a3  U R
                  R                  " U R                  R                  U5      6   g g )	Nr	   r   z 	zFrom    :zMissing header name.z3_parse_headers fed line with no : and no leading WS)	enumerater   #FirstHeaderLineIsContinuationDefectrW   rv   rc   r   set_rawheader_source_parser   r   r   r   r   set_unixfromr_   r7   MisplacedEnvelopeHeaderDefectdefectsfindInvalidHeaderDefect)	r   rD   
lastheader	lastvaluelinenor1   rx   r   is	            r   r   FeedParser._parse_headers  s   
	%e,LFAw%! $GGMFKK--dii@  &		!!4;;#B#B9#MN(*BIw''Q;"))$/B#$6c"((1+&6%67II**40s5zA~- KK**40 $AA$GFII%%,,V4 		#A
 Av334JK		!!((0Q3MMM3bqJIg -j IIt{{>>yIJ r   )	rc   r]   re   r_   rd   r`   rZ   rb   rW   r   )rN   rO   rP   rQ   rR   r   r   rh   rl   rk   r)   r   rr   ra   r   rS   rG   r   r   r   r      s<    '" ">!
|7|:Kr   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )r   i  z(Like FeedParser, but feed accepts bytes.c                 D   > [         TU ]  UR                  SS5      5        g )Nasciisurrogateescape)superrl   decode)r   r>   	__class__s     r   rl   BytesFeedParser.feed  s    T[[*;<=r   rG   )rN   rO   rP   rQ   rR   rl   rS   __classcell__)r   s   @r   r   r     s    2> >r   )rR   __all__reemailr   email._policybaser   collectionsr   ior   compiler   r   r   NLCRE_crackr   r   NLr   objectr-   r   r   r   rG   r   r   <module>r      s   
" *
+ 	  &  


=!JJ'	JJ()	jj) ::>?	

:< xWf WtJK JKZ>j >r   