o
     JAfüX  ã                   @   s´   	 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 
d¡Ze 
d	¡Ze 
d
¡Ze 
d	¡Ze 
d¡ZdZdZeƒ ZG dd„ deƒZG dd „ d ƒZG dd„ deƒZdS )Ú
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                   @   s^   e Zd Z	 dd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚBufferedSubFilec                 C   s$   t dd| _tƒ | _g | _d| _d S )Nr   )ÚnewlineF)r   Ú_partialr   Ú_linesÚ	_eofstackÚ_closed©Úself© r   ú'/usr/lib/python3.10/email/feedparser.pyÚ__init__5   s   
zBufferedSubFile.__init__c                 C   ó   | j  |¡ d S ©N)r   Úappend)r   Zpredr   r   r   Úpush_eof_matcher@   ó   z BufferedSubFile.push_eof_matcherc                 C   s
   | j  ¡ S r   )r   Úpopr   r   r   r   Úpop_eof_matcherC   ó   
zBufferedSubFile.pop_eof_matcherc                 C   s<   | j  d¡ |  | j  ¡ ¡ | j  d¡ | j  ¡  d| _d S )Nr   T)r   ÚseekÚ	pushlinesÚ	readlinesÚtruncater   r   r   r   r   ÚcloseF   s
   

zBufferedSubFile.closec                 C   sL   | j s
| jrdS tS | j  ¡ }t| jƒD ]}||ƒr#| j  |¡  dS q|S ©Nr   )r   r   ÚNeedMoreDataÚpopleftÚreversedr   Ú
appendleft)r   ÚlineZateofr   r   r   ÚreadlineN   s   
ýzBufferedSubFile.readlinec                 C   r   r   )r   r&   ©r   r'   r   r   r   Ú
unreadline`   s   zBufferedSubFile.unreadlinec                 C   sz   	 | j  |¡ d|vrd|vrd S | j  d¡ | j  ¡ }| j  d¡ | j  ¡  |d  d¡s6| j  | ¡ ¡ |  |¡ d S )Nr	   úr   éÿÿÿÿ)r   Úwriter   r   r    Úendswithr   r   )r   ÚdataÚpartsr   r   r   Úpushe   s   

zBufferedSubFile.pushc                 C   r   r   )r   Úextend)r   Úlinesr   r   r   r   z   r   zBufferedSubFile.pushlinesc                 C   s   | S r   r   r   r   r   r   Ú__iter__}   s   zBufferedSubFile.__iter__c                 C   s   |   ¡ }|dkr
t‚|S r"   )r(   ÚStopIterationr)   r   r   r   Ú__next__€   s   zBufferedSubFile.__next__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r!   r(   r*   r1   r   r4   r6   r   r   r   r   r
   -   s    r
   c                   @   s^   e Zd Z	 dedœdd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )r   N©Úpolicyc                C   sž   	 || _ d| _|d u r|jd u rddlm} || _n|j| _n|| _z|| j d W n ty6   d| _Y nw tƒ | _g | _	|  
¡ j| _d | _d | _d| _d S )NFr   )ÚMessager:   T)r;   Ú_old_style_factoryZmessage_factoryZemail.messager<   Ú_factoryÚ	TypeErrorr
   Ú_inputÚ	_msgstackÚ	_parsegenr6   Ú_parseÚ_curÚ_lastÚ_headersonly)r   r>   r;   r<   r   r   r   r   ‹   s(   


þ
zFeedParser.__init__c                 C   s
   d| _ d S )NT)rF   r   r   r   r   Ú_set_headersonlyª   r   zFeedParser._set_headersonlyc                 C   s   	 | j  |¡ |  ¡  d S r   )r@   r1   Ú_call_parse©r   r/   r   r   r   Úfeed­   s   zFeedParser.feedc                 C   s$   z|   ¡  W d S  ty   Y d S w r   )rC   r5   r   r   r   r   rH   ²   s
   ÿzFeedParser._call_parsec                 C   sJ   	 | j  ¡  |  ¡  |  ¡ }| ¡ dkr#| ¡ s#t ¡ }| j 	||¡ |S )NÚ	multipart)
r@   r!   rH   Ú_pop_messageÚget_content_maintypeZis_multipartr   Z!MultipartInvariantViolationDefectr;   Úhandle_defect)r   ÚrootÚdefectr   r   r   r!   ¸   s   
ÿzFeedParser.closec                 C   sn   | j r|  ¡ }n| j| jd}| jr| j ¡ dkr| d¡ | jr)| jd  |¡ | j |¡ || _|| _	d S )Nr:   zmultipart/digestzmessage/rfc822r,   )
r=   r>   r;   rD   Úget_content_typeZset_default_typerA   Zattachr   rE   )r   Úmsgr   r   r   Ú_new_messageÅ   s   


zFeedParser._new_messagec                 C   s*   | j  ¡ }| j r| j d | _|S d | _|S )Nr,   )rA   r   rD   )r   Úretvalr   r   r   rL   Ò   s   
ÿzFeedParser._pop_messagec                 c   sæ   |   ¡  g }| jD ]-}|tu rtV  q
t |¡s2t |¡s0t ¡ }| j 	| j
|¡ | j |¡  n| |¡ q
|  |¡ | jrfg }	 | j ¡ }|tu rPtV  qB|dkrUn| |¡ qC| j
 t |¡¡ d S | j
 ¡ dkr¹	 | j tj¡ |  ¡ D ]
}|tu rƒtV  qy |  ¡ }| j ¡  	 | j ¡ }|tu r›tV  q	 	 | j ¡ }|tu rªtV  qœ	 |dkr²	 d S | j |¡ qn| j
 ¡ dkrÕ|  ¡ D ]
}|tu rÎtV  qÄ |  ¡  d S | j
 ¡ dkrÐ| j
 ¡ }|d u rt ¡ }| j 	| j
|¡ g }| jD ]}|tu rtV  qø| |¡ qø| j
 t |¡¡ d S t| j
 dd¡ƒ ¡ dvr.t ¡ }| j 	| j
|¡ d	| }t  d
t !|¡ d ¡}	d}
g }d}d}	 | j ¡ }|tu rVtV  qF|dkr\në|	 |¡}|r@| "d¡rrd}| "d¡}nÕ|
r£|r™|d }t# $|¡}|r’|d t%| "d¡ƒ … |d< t |¡| j
_&d}
| j |¡ qF	 | j ¡ }|tu r³tV  q£|	 |¡}|sÂ| j |¡ nq¤| j |	j¡ |  ¡ D ]}|tu rÛtV  qÏ | j' ¡ dkr| j'j(}|dkròd | j'_(n@|d urt# $|¡}|rt%| "d¡ƒ}|d | … | j'_(n"| j'j)}t*|tƒr2t# $|¡}|r2|d t%| "d¡ƒ … }|| j'_)| j ¡  |  ¡  | j
| _'n| |¡ qG|
r{t +¡ }| j 	| j
|¡ | j
 t |¡¡ g }| jD ]}|tu rptV  qdqdt |¡| j
_(d S |sŒt ,¡ }| j 	| j
|¡ d S |r“dg}ng }| jD ]}|tu r¤tV  q˜| |¡ q˜|rÇ|d }t- |¡}|rÇ|t%| "d¡ƒd … |d< t |¡| j
_(d S g }| jD ]}|tu rátV  qÕ| |¡ qÕ| j
 t |¡¡ d S )NTr   zmessage/delivery-statusÚmessagerK   zcontent-transfer-encodingÚ8bit)Z7bitrV   Úbinaryz--z(?P<sep>z4)(?P<end>--)?(?P<ws>[ \t]*)(?P<linesep>\r\n|\r|\n)?$FÚendÚlinesepr,   r   ).rS   r@   r#   ÚheaderREÚmatchÚNLCREr   Z MissingHeaderBodySeparatorDefectr;   rN   rD   r*   r   Ú_parse_headersrF   r(   Zset_payloadÚEMPTYSTRINGÚjoinrQ   r   rB   rL   r   rM   Zget_boundaryZNoBoundaryInMultipartDefectÚstrÚgetÚlowerZ-InvalidMultipartContentTransferEncodingDefectÚreÚcompileÚescapeÚgroupÚ	NLCRE_eolÚsearchÚlenÚpreamblerE   ÚepilogueZ_payloadÚ
isinstanceZStartBoundaryNotFoundDefectZCloseBoundaryNotFoundDefectÚ	NLCRE_bol)r   Zheadersr'   rP   r3   rT   rR   ÚboundaryÚ	separatorZ
boundaryreZcapturing_preamblerj   rY   Zclose_boundary_seenÚmoZlastlineZeolmork   rX   ÚpayloadÚ	firstlineZbolmor   r   r   rB   Ú   sz  €





ù


ÿá!



ÿÿÿ








ø




€



±S

þ




zFeedParser._parsegenc           	      C   sT  d}g }t |ƒD ]‘\}}|d dv r(|s"t |¡}| j | j|¡ q| |¡ q|r9| jj| j |¡Ž  dg }}| 	d¡rz|dkr\t
 |¡}|rU|d t| d¡ƒ … }| j |¡ q|t|ƒd krm| j |¡  d S t |¡}| jj |¡ q| d¡}|dkrt d¡}| jj |¡ q|d |… }|g}q|r¨| jj| j |¡Ž  d S d S )Nr   r   z 	zFrom é   ú:zMissing header name.)Ú	enumerater   Z#FirstHeaderLineIsContinuationDefectr;   rN   rD   r   Zset_rawZheader_source_parseÚ
startswithrg   rh   ri   rf   Zset_unixfromr@   r*   ZMisplacedEnvelopeHeaderDefectZdefectsÚfindZInvalidHeaderDefect)	r   r3   Z
lastheaderZ	lastvalueÚlinenor'   rP   rp   Úir   r   r   r]   ×  sH   







ÿzFeedParser._parse_headersr   )r7   r8   r9   r   r   rG   rJ   rH   r!   rS   rL   rB   r]   r   r   r   r   r   ˆ   s     ~c                       s   e Zd Z	 ‡ fdd„Z‡  ZS )r   c                    s   t ƒ  | dd¡¡ d S )NÚasciiÚsurrogateescape)ÚsuperrJ   ÚdecoderI   ©Ú	__class__r   r   rJ     s   zBytesFeedParser.feed)r7   r8   r9   rJ   Ú__classcell__r   r   r~   r   r     s    )Ú__all__rc   Zemailr   Zemail._policybaser   Úcollectionsr   Úior   rd   r\   rm   rg   ZNLCRE_crackrZ   r^   ÚNLÚobjectr#   r
   r   r   r   r   r   r   Ú<module>   s*   




[   