
    =OgY                     X   d Z ddgZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
  ej        d	          Z ej        d
          Z ej        d          Z ej        d
          Z ej        d          ZdZdZ e            Z G d de          Z G d d          Z G d de          ZdS )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 ]) 
c                   N    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd ZdS )BufferedSubFileak  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                 r    t          d          | _        t                      | _        g | _        d| _        d S )Nr	   )newlineF)r   _partialr   _lines	_eofstack_closedselfs    )/usr/lib64/python3.11/email/feedparser.py__init__zBufferedSubFile.__init__4   s3     !,,,gg    c                 :    | j                             |           d S N)r   append)r   preds     r   push_eof_matcherz BufferedSubFile.push_eof_matcher?   s    d#####r   c                 4    | j                                         S r   )r   popr   s    r   pop_eof_matcherzBufferedSubFile.pop_eof_matcherB   s    ~!!###r   c                    | j                             d           |                     | j                                                    | j                             d           | j                                          d| _        d S )Nr   T)r   seek	pushlines	readlinestruncater   r   s    r   closezBufferedSubFile.closeE   sm    1t}..001111   r   c                     | j         s| j        rdS t          S | j                                         }t	          | j                  D ]*} ||          r| j                             |            dS +|S Nr	   )r   r   NeedMoreDatapopleftreversedr   
appendleft)r   lineateofs      r   readlinezBufferedSubFile.readlineM   s    { 	 | r {""$$ dn-- 	 	EuT{{ &&t,,,rr r   c                 :    | j                             |           d S r   )r   r+   r   r,   s     r   
unreadlinezBufferedSubFile.unreadline_   s      	t$$$$$r   c                    | j                             |           d|vrd|vrdS | j                             d           | j                                         }| j                             d           | j                                          |d                             d          s,| j                             |                                           |                     |           dS )z$Push some new data into this object.r
   Nr   )r   writer!   r#   r$   endswithr   r"   )r   datapartss      r   pushzBufferedSubFile.pushd   s    D!!!tD 0 0F 	1''))1    Ry!!$'' 	-M		,,,ur   c                 :    | j                             |           d S r   )r   extend)r   liness     r   r"   zBufferedSubFile.pushlinesy   s    5!!!!!r   c                     | S r    r   s    r   __iter__zBufferedSubFile.__iter__|   s    r   c                 H    |                                  }|dk    rt          |S r'   )r.   StopIterationr0   s     r   __next__zBufferedSubFile.__next__   s#    }}2::r   N)__name__
__module____qualname____doc__r   r   r   r%   r.   r1   r9   r"   r?   rB   r>   r   r   r   r   ,   s         	 	 	$ $ $$ $ $    $% % %
  *" " "      r   r   c                   P    e Zd ZdZdeddZd Zd Zd Zd Z	d	 Z
d
 Zd Zd ZdS )r   zA feed-style parser of email.Npolicyc                f   || _         d| _        |"|j        ddlm} || _        n>|j        | _        n1|| _        	  || j                    n# t          $ r
 d| _        Y nw xY wt                      | _        g | _	        | 
                                j        | _        d| _        d| _        d| _        dS )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   )MessagerH   T)rI   _old_style_factorymessage_factoryemail.messagerK   _factory	TypeErrorr   _input	_msgstack	_parsegenrB   _parse_cur_last_headersonly)r   rO   rI   rK   s       r   r   zFeedParser.__init__   s     "'%-111111 ' & 6$DM/,,,,, / / /*.'''/ &''nn&&/	
!s   A A! A!c                     d| _         d S )NT)rW   r   s    r   _set_headersonlyzFeedParser._set_headersonly   s     r   c                 b    | j                             |           |                                  dS )zPush more data into the parser.N)rQ   r9   _call_parse)r   r7   s     r   feedzFeedParser.feed   s0    r   c                 R    	 |                                   d S # t          $ r Y d S w xY wr   )rT   rA   r   s    r   r[   zFeedParser._call_parse   s:    	KKMMMMM 	 	 	DD	s    
&&c                 J   | j                                          |                                  |                                 }|                                dk    rI|                                s5| j        s.t          j                    }| j	        
                    ||           |S )z<Parse all remaining data and return the root message object.	multipart)rQ   r%   r[   _pop_messageget_content_maintypeis_multipartrW   r   !MultipartInvariantViolationDefectrI   handle_defect)r   rootdefects      r   r%   zFeedParser.close   s      "" $$&&+55((** 6373D 6=??FK%%dF333r   c                    | j         r|                                 }n|                     | j                  }| j        r2| j                                        dk    r|                    d           | j        r | j        d                             |           | j                            |           || _        || _	        d S )NrH   zmultipart/digestzmessage/rfc822r4   )
rL   rO   rI   rU   get_content_typeset_default_typerR   attachr   rV   )r   msgs     r   _new_messagezFeedParser._new_message   s    " 	4--//CC--t{-33C9 	333559KKK  !1222> 	+N2%%c***c"""	


r   c                 z    | j                                         }| j         r| j         d         | _        nd | _        |S )Nr4   )rR   r   rU   )r   retvals     r   r`   zFeedParser._pop_message   s;    ##%%> 	r*DIIDIr   c              #     K   |                                   g }| j        D ]}|t          u r
t          V  t                              |          sit
                              |          sMt          j                    }| j        	                    | j
        |           | j                            |            n|                    |           |                     |           | j        rg }	 | j                                        }|t          u r
t          V  -|dk    rn|                    |           I| j
                            t"                              |                     d S | j
                                        dk    r	 | j                            t
          j                   |                                 D ]}|t          u r
t          V   |                                 }| j                                         	 | j                                        }|t          u r
t          V  -	 	 | j                                        }|t          u r
t          V  -	 |dk    rn| j                            |           d S | j
                                        dk    r@|                                 D ]}|t          u r
t          V   |                                  d S | j
                                        dk    r| j
                                        }|t          j                    }| j        	                    | j
        |           g }| j        D ]*}|t          u r
t          V  |                    |           +| j
                            t"                              |                     d S t7          | j
                            dd                                                    dvr3t          j                    }| j        	                    | j
        |           d	|z   }t?          j         d
t?          j!        |          z   dz             }	d}
g }d}d}	 | j                                        }|t          u r
t          V  -|dk    rn|	                    |          }|r|"                    d          rd}|"                    d          }n|
r|rv|d         }tF          $                    |          }|r.|d tK          |"                    d                              |d<   t"                              |          | j
        _&        d}
| j                            |           	 | j                                        }|t          u r
t          V  -|	                    |          }|s| j                            |           n_| j                            |	j                   |                                 D ]}|t          u r
t          V   | j'                                        dk    ru| j'        j(        }|dk    rd | j'        _(        n|StF          $                    |          }|r7tK          |"                    d                    }|d |          | j'        _(        nt| j'        j)        }tU          |t6                    rStF          $                    |          }|r7|d tK          |"                    d                              }|| j'        _)        | j                                         |                                  | j
        | _'        n|                    |           |
rt          j+                    }| j        	                    | j
        |           | j
                            t"                              |                     g }| j        D ]}|t          u r
t          V  t"                              |          | j
        _(        d S |s5t          j,                    }| j        	                    | j
        |           d S |rdg}ng }| j        D ]*}|t          u r
t          V  |                    |           +|rQ|d         }tZ                              |          }|r-|tK          |"                    d                    d          |d<   t"                              |          | j
        _(        d S g }| j        D ]*}|t          u r
t          V  |                    |           +| j
                            t"                              |                     d S )NTr	   zmessage/delivery-statusmessager_   zcontent-transfer-encoding8bit)7bitrq   binaryz--z(?P<sep>z4)(?P<end>--)?(?P<ws>[ \t]*)(?P<linesep>\r\n|\r|\n)?$Fendlinesepr4   r   ).rl   rQ   r(   headerREmatchNLCREr    MissingHeaderBodySeparatorDefectrI   rd   rU   r1   r   _parse_headersrW   r.   set_payloadEMPTYSTRINGjoinrh   r   rS   r`   r   ra   get_boundaryNoBoundaryInMultipartDefectstrgetlower-InvalidMultipartContentTransferEncodingDefectrecompileescapegroup	NLCRE_eolsearchlenpreamblerV   epilogue_payload
isinstanceStartBoundaryNotFoundDefectCloseBoundaryNotFoundDefect	NLCRE_bol)r   headersr,   rf   r<   rn   rk   boundary	separator
boundaryrecapturing_preambler   ru   close_boundary_seenmolastlineeolmor   rt   payload	firstlinebolmos                         r   rS   zFeedParser._parsegen   s	      K 	! 	!D|##"""">>$''  {{4(( 1#DFFFK--di@@@K**4000NN4     	G$$$  	E#{++--<''&&&&2::T"""# I!!+"2"25"9"9:::F9%%''+DDD-,,U[999"nn..  F--**** '')) ++---
;//11D|++**** ;//11D|++**** 2::&&t,,,?-@ F9))++y88 ..**  \))&&&&F9))++{::y--//H
  ;==))$)V<<< K ' 'D|++**** LL&&&&	%%k&6&6u&=&=>>>DIMM"=vFFGGMMOO56 6MOO))$)V<<<
 xIRYy111GHI IJ "&HG"'O*{++--<''&&&&2::%%d++ G*
 xx .2+"$((9"5"5) !# L (0|H$-$4$4X$>$>E$ O/78M#ekk!nn:M:M9M8M/N1<1A1A(1K1KDI.-2*..t444 
"#{3355<//"....$'--d33! " K224888!" K001ABBB"&.."2"2  !\11"....$
 z6688KGG#':#6#r>>26DJ//%1!*!1!1(!;!;B! F&)"((1++&6&66>uuo
 3"&*"5%gs33 >!*!1!1'!:!:B! >*12DC4D4D3D2D*E6=
 3K//111%%''' "&DJJ OOD)))_O*f " 
;==))$)V<<<	%%k&6&6x&@&@AAA K ! !D|++****  , &1%5%5h%?%?	" ' ;==))$)V<<<  4 & &<''&&&&%%%%  B$QK	!	22 B"+CA,?,?,@,@"AHQK!,!1!1(!;!;DIF K 	 	D|##""""LL	k..u5566666r   c                     d}g }t          |          D ]\  }}|d         dv rM|s5t          j        |          }| j                            | j        |           G|                    |           ]|r+ | j        j        | j                            |            dg }}|	                    d          r|dk    rct                              |          }|r+|d t          |                    d                              }| j                            |           |t          |          dz
  k    r| j                            |            d S t          j        |          }| j        j                            |           p|                    d          }|dk    r5t          j        d          }| j        j                            |           |d |         }|g}|r) | j        j        | j                            |            d S d S )Nr	   r   z 	zFrom    :zMissing header name.)	enumerater   #FirstHeaderLineIsContinuationDefectrI   rd   rU   r   set_rawheader_source_parse
startswithr   r   r   r   set_unixfromrQ   r1   MisplacedEnvelopeHeaderDefectdefectsfindInvalidHeaderDefect)	r   r<   
lastheader	lastvaluelinenor,   rf   r   is	            r   rz   zFeedParser._parse_headers  s-   
	%e,, 3	 3	LFDAw%!  $GMMFK--di@@@  &&& /!	!4;#B#B9#M#MNN(*BI
w'' Q;;"))$//B 8#$6c"((1++&6&6%6$67I**4000s5zzA~-- K**4000FF $A$GGFI%,,V444 		#A
 Avv34JKK	!((000 bqbJII 	KDIt{>>yIIJJJJ	K 	Kr   r   )rC   rD   rE   rF   r   r   rY   r\   r[   r%   rl   r`   rS   rz   r>   r   r   r   r      s        ''" " " " " ">! ! !  
        {7 {7 {7z:K :K :K :K :Kr   c                   "     e Zd ZdZ fdZ xZS )r   z(Like FeedParser, but feed accepts bytes.c                 r    t                                          |                    dd                     d S )Nasciisurrogateescape)superr\   decode)r   r7   	__class__s     r   r\   zBytesFeedParser.feed  s.    T[[*;<<=====r   )rC   rD   rE   rF   r\   __classcell__)r   s   @r   r   r     s>        22> > > > > > > > >r   )rF   __all__r   emailr   email._policybaser   collectionsr   ior   r   rx   r   r   NLCRE_crackrv   r|   NLobjectr(   r   r   r   r>   r   r   <module>r      s{  
 " *
+ 				       & & & & & &            
=!!BJ''	BJ())	bj)) 2:>??	vxxW W W W Wf W W WtIK IK IK IK IK IK IK IKX> > > > >j > > > > >r   