B
    0*™\[  ã               @   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   ú;C:\ALexclude\prg\programme\Python37\Lib\email\feedparser.pyÚ__init__5   s    zBufferedSubFile.__init__c             C   s   | j  |¡ d S )N)r   Úappend)r   Zpredr   r   r   Úpush_eof_matcher@   s    z BufferedSubFile.push_eof_matcherc             C   s
   | j  ¡ S )N)r   Úpop)r   r   r   r   Úpop_eof_matcherC   s    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   sN   | j s| jrdS tS | j  ¡ }x*t| jƒD ]}||ƒr*| j  |¡ dS q*W |S )Nr   )r   r   ÚNeedMoreDataÚpopleftÚreversedr   Ú
appendleft)r   ÚlineZateofr   r   r   ÚreadlineN   s    
zBufferedSubFile.readlinec             C   s   | j  |¡ d S )N)r   r    )r   r!   r   r   r   Ú
unreadline`   s    zBufferedSubFile.unreadlinec             C   sx   | j  |¡ d|kr d|kr d S | j  d¡ | j  ¡ }| j  d¡ | j  ¡  |d  d¡sj| j  | ¡ ¡ |  |¡ d S )Nr	   úr   éÿÿÿÿ)r   Úwriter   r   r   Úendswithr   r   )r   ÚdataÚpartsr   r   r   Úpushe   s    

zBufferedSubFile.pushc             C   s   | j  |¡ d S )N)r   Úextend)r   Úlinesr   r   r   r   z   s    zBufferedSubFile.pushlinesc             C   s   | S )Nr   )r   r   r   r   Ú__iter__}   s    zBufferedSubFile.__iter__c             C   s   |   ¡ }|dkrt‚|S )Nr   )r"   ÚStopIteration)r   r!   r   r   r   Ú__next__€   s    zBufferedSubFile.__next__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r"   r#   r*   r   r-   r/   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 kr<|jd kr2ddlm} || _qn|j| _n2|| _y|| j d W n tk
rl   d| _Y nX tƒ | _g | _	|  
¡ j| _d | _d | _d| _d S )NFr   )ÚMessage)r3   T)r3   Ú_old_style_factoryZmessage_factoryZemail.messager4   Ú_factoryÚ	TypeErrorr
   Ú_inputÚ	_msgstackÚ	_parsegenr/   Ú_parseÚ_curÚ_lastÚ_headersonly)r   r6   r3   r4   r   r   r   r   ‹   s$    

zFeedParser.__init__c             C   s
   d| _ d S )NT)r>   )r   r   r   r   Ú_set_headersonlyª   s    zFeedParser._set_headersonlyc             C   s   | j  |¡ |  ¡  d S )N)r8   r*   Ú_call_parse)r   r(   r   r   r   Úfeed­   s    zFeedParser.feedc             C   s&   y|   ¡  W n tk
r    Y nX d S )N)r;   r.   )r   r   r   r   r@   ²   s    zFeedParser._call_parsec             C   sH   | j  ¡  |  ¡  |  ¡ }| ¡ dkrD| ¡ sDt ¡ }| j 	||¡ |S )NÚ	multipart)
r8   r   r@   Ú_pop_messageÚget_content_maintypeZis_multipartr   Z!MultipartInvariantViolationDefectr3   Ú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rR| jd  |¡ | j |¡ || _|| _	d S )N)r3   zmultipart/digestzmessage/rfc822r%   )
r5   r6   r3   r<   Úget_content_typeZset_default_typer9   Zattachr   r=   )r   Úmsgr   r   r   Ú_new_messageÅ   s    

zFeedParser._new_messagec             C   s(   | j  ¡ }| j r| j d | _nd | _|S )Nr%   )r9   r   r<   )r   Úretvalr   r   r   rC   Ò   s
    
zFeedParser._pop_messagec             c   s6  |   ¡  g }xb| jD ]X}|tkr(tV  qt |¡sbt |¡s`t ¡ }| j 	| j
|¡ | j |¡ P | |¡ qW |  |¡ | jrÎg }x2| j ¡ }|tkr tV  q†|dkrªP | |¡ q†W | j
 t |¡¡ d S | j
 ¡ dkr–x²| j tj¡ x$|  ¡ D ]}|tkrtV  qøP qøW |  ¡ }| j ¡  x&| j ¡ }|tkrFtV  q(P q(W x&| j ¡ }|tkrntV  qPP qPW |dkr‚P | j |¡ qàW d S | j
 ¡ dkrÜx(|  ¡ D ]}|tkrÈtV  q°P q°W |  ¡  d S | j
 ¡ dkrì| j
 ¡ }|d krbt ¡ }| j 	| j
|¡ g }x.| jD ]$}|tkr<tV  q$| |¡ q$W | j
 t |¡¡ d S | j
 dd¡ ¡ dkr’t ¡ }| j 	| j
|¡ d| }t d	t  |¡ d
 ¡}	d}
g }d}d}x| j ¡ }|tkrätV  qÆ|dkrðP |	 |¡}|rÂ| !d¡rd}| !d¡}P |
r~|rj|d }t" #|¡}|r\|d t$| !d¡ƒ … |d< t |¡| j
_%d}
| j |¡ qÆxB| j ¡ }|tkržtV  q€|	 |¡}|s€| j |¡ P q€W | j |	j¡ x(|  ¡ D ]}|tkròtV  qÚP qÚW | j& ¡ dkrb| j&j'}|dkr&d | j&_'n:|d k	r¦t" #|¡}|r¦t$| !d¡ƒ}|d | … | j&_'nD| j&j(}t)|t*ƒr¦t" #|¡}|r¦|d t$| !d¡ƒ … }|| j&_(| j ¡  |  ¡  | j
| _&n
| |¡ qÆW |
r>t +¡ }| j 	| j
|¡ | j
 t |¡¡ g }x$| jD ]}|tkrtV  qqW t |¡| j
_'d S |s`t ,¡ }| j 	| j
|¡ d S |rndg}ng }x.| jD ]$}|tkr’tV  qz| |¡ qzW |rÚ|d }t- |¡}|rÚ|t$| !d¡ƒd … |d< t |¡| j
_'d S g }x.| jD ]$}|tkrtV  qø| |¡ qøW | j
 t |¡¡ d S )Nr   zmessage/delivery-statusÚmessagerB   zcontent-transfer-encodingÚ8bit)Z7bitrM   Zbinaryz--z(?P<sep>z4)(?P<end>--)?(?P<ws>[ \t]*)(?P<linesep>\r\n|\r|\n)?$TFÚendÚlinesepr%   r   ).rJ   r8   r   ÚheaderREÚmatchÚNLCREr   Z MissingHeaderBodySeparatorDefectr3   rE   r<   r#   r   Ú_parse_headersr>   r"   Zset_payloadÚEMPTYSTRINGÚjoinrH   r   r:   rC   r   rD   Zget_boundaryZNoBoundaryInMultipartDefectÚgetÚlowerZ-InvalidMultipartContentTransferEncodingDefectÚreÚcompileÚescapeÚgroupÚ	NLCRE_eolÚsearchÚlenÚpreambler=   ÚepilogueZ_payloadÚ
isinstanceÚstrZStartBoundaryNotFoundDefectZCloseBoundaryNotFoundDefectÚ	NLCRE_bol)r   Zheadersr!   rG   r,   rK   rI   ÚboundaryZ	separatorZ
boundaryreZcapturing_preambler_   rO   Zclose_boundary_seenÚmoZlastlineZeolmor`   rN   ZpayloadÚ	firstlineZbolmor   r   r   r:   Ú   sf   




































zFeedParser._parsegenc       	      C   s\  d}g }x2t |ƒD ]$\}}|d dkrV|sJt |¡}| j | j|¡ q| |¡ q|rx| jj| j |¡Ž  dg  }}| 	d¡rú|dkrÀt
 |¡}|r°|d t| d¡ƒ … }| j |¡ qn:|t|ƒd krà| j |¡ d S t |¡}| jj |¡ q| d¡}|dkr(t d¡}| jj |¡ q|d |… }|g}qW |rX| jj| j |¡Ž  d S )Nr   r   z 	zFrom é   ú:zMissing header name.)Ú	enumerater   Z#FirstHeaderLineIsContinuationDefectr3   rE   r<   r   Zset_rawZheader_source_parseÚ
startswithr\   r]   r^   r[   Zset_unixfromr8   r#   ZMisplacedEnvelopeHeaderDefectZdefectsÚfindZInvalidHeaderDefect)	r   r,   Z
lastheaderZ	lastvalueÚlinenor!   rG   re   Úir   r   r   rS   ×  sF    









zFeedParser._parse_headers)N)r0   r1   r2   r   r   r?   rA   r@   r   rJ   rC   r:   rS   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)ÚsuperrA   Údecode)r   r(   )Ú	__class__r   r   rA     s    zBytesFeedParser.feed)r0   r1   r2   rA   Ú__classcell__r   r   )rr   r   r     s   )Ú__all__rX   Zemailr   Zemail._policybaser   Úcollectionsr   Úior   rY   rR   rc   r\   ZNLCRE_crackrP   rT   ÚNLÚobjectr   r
   r   r   r   r   r   r   Ú<module>   s&   




[   