U
    aúdüX  ã                   @   s¶   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 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 )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                   @   s`   e Zd 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 )Ú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                 C   s$   t dd| _tƒ | _g | _d| _d S )Nr   )ÚnewlineF)r   Ú_partialr   Ú_linesÚ	_eofstackÚ_closed©Úself© r   ú&/usr/lib/python3.8/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 r   )r   Úpopr   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   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   s   |t k	st‚| j |¡ d S r   )r    ÚAssertionErrorr   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 )z$Push some new data into this object.r	   úNr   éÿÿÿÿ)r   Úwriter   r   r   Úendswithr   r   )r   ÚdataÚpartsr   r   r   Úpushe   s    

zBufferedSubFile.pushc                 C   s   | j  |¡ d S r   )r   Úextend)r   Úlinesr   r   r   r   z   s    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__Ú__doc__r   r   r   r   r%   r(   r/   r   r2   r4   r   r   r   r   r
   -   s   r
   c                   @   s`   e Zd 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   zA feed-style parser of email.N©Úpolicyc                C   sž   || _ d| _|dkr<|jdkr2ddlm} || _qn|j| _n2|| _z|| j d W n tk
rl   d| _Y nX 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   )ÚMessager9   T)r:   Ú_old_style_factoryZmessage_factoryZemail.messager;   Ú_factoryÚ	TypeErrorr
   Ú_inputÚ	_msgstackÚ	_parsegenr4   Ú_parseÚ_curÚ_lastÚ_headersonly)r   r=   r:   r;   r   r   r   r   ‹   s$    

zFeedParser.__init__c                 C   s
   d| _ d S )NT)rE   r   r   r   r   Ú_set_headersonlyª   s    zFeedParser._set_headersonlyc                 C   s   | j  |¡ |  ¡  dS )zPush more data into the parser.N)r?   r/   Ú_call_parse©r   r-   r   r   r   Úfeed­   s    zFeedParser.feedc                 C   s&   z|   ¡  W n tk
r    Y nX d S r   )rB   r3   r   r   r   r   rG   ²   s    zFeedParser._call_parsec                 C   sR   | j  ¡  |  ¡  |  ¡ }| jr$t‚| ¡ dkrN| ¡ sNt 	¡ }| j
 ||¡ |S )z<Parse all remaining data and return the root message object.Ú	multipart)r?   r   rG   Ú_pop_messager@   r&   Ú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rR| jd  |¡ | j |¡ || _|| _	d S )Nr9   zmultipart/digestzmessage/rfc822r*   )
r<   r=   r:   rC   Úget_content_typeZset_default_typer@   Zattachr   rD   )r   Úmsgr   r   r   Ú_new_messageÅ   s    

zFeedParser._new_messagec                 C   s(   | j  ¡ }| j r| j d | _nd | _|S )Nr*   )r@   r   rC   )r   Úretvalr   r   r   rK   Ò   s
    
zFeedParser._pop_messagec                 c   s$  |   ¡  g }| jD ]Z}|tkr&tV  qt |¡sbt |¡s^t ¡ }| j 	| j
|¡ | j |¡  qn| |¡ q|  |¡ | jrÈg }| j ¡ }|tkrœtV  q‚|dkr¦q²| |¡ q‚| j
 t |¡¡ d S | j
 ¡ dkrŠ| j tj¡ |  ¡ D ]}|tkrtV  qî qqî|  ¡ }| j ¡  | j ¡ }|tkrDtV  qqDq| j ¡ }|tkrjtV  qDqjqD|dkrxq†| j |¡ qØd S | j
 ¡ dkrÐ|  ¡ D ] }|tkrºtV  q¢ qÄq¢|  ¡  d S | j
 ¡ dkrÞ| j
 ¡ }|d krRt ¡ }| j 	| j
|¡ g }| jD ]$}|tkr.tV  q| |¡ q| j
 t |¡¡ d S t| j
 dd¡ƒ ¡ dkr†t ¡ }| j 	| j
|¡ d| }t  d	t !|¡ d
 ¡}	d}
g }d}d}| j ¡ }|tkrÔtV  q¶|dkrâqÌ|	 |¡}|r´| "d¡rd}| "d¡}qÌ|
rr|r^|d }t# $|¡}|rP|d t%| "d¡ƒ … |d< t |¡| j
_&d}
| j |¡ q¶| j ¡ }|tkrtV  qr|	 |¡}|sr| j |¡ q´qr| j |	j¡ |  ¡ D ] }|tkrâtV  qÊ qìqÊ| j' ¡ dkrT| 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|
s¾t+‚| |¡ q¶|
r4t ,¡ }| j 	| j
|¡ | j
 t |¡¡ g }| jD ]}|tkrtV  qqt |¡| j
_(d S |sVt -¡ }| j 	| j
|¡ d S |rddg}ng }| jD ]$}|tkr†tV  qn| |¡ qn|rÌ|d }t. |¡}|rÌ|t%| "d¡ƒd … |d< t |¡| j
_(d S g }| jD ]$}|tkr tV  qè| |¡ qè| j
 t |¡¡ d S )Nr   zmessage/delivery-statusÚmessagerJ   zcontent-transfer-encodingÚ8bit)Z7bitrU   Zbinaryz--z(?P<sep>z4)(?P<end>--)?(?P<ws>[ \t]*)(?P<linesep>\r\n|\r|\n)?$TFÚendÚlinesepr*   r   )/rR   r?   r    ÚheaderREÚmatchÚNLCREr   Z MissingHeaderBodySeparatorDefectr:   rM   rC   r(   r   Ú_parse_headersrE   r%   Zset_payloadÚEMPTYSTRINGÚjoinrP   r   rA   rK   r   rL   Zget_boundaryZNoBoundaryInMultipartDefectÚstrÚgetÚlowerZ-InvalidMultipartContentTransferEncodingDefectÚreÚcompileÚescapeÚgroupÚ	NLCRE_eolÚsearchÚlenÚpreamblerD   ÚepilogueZ_payloadÚ
isinstancer&   ZStartBoundaryNotFoundDefectZCloseBoundaryNotFoundDefectÚ	NLCRE_bol)r   Zheadersr$   rO   r1   rS   rQ   ÚboundaryZ	separatorZ
boundaryreZcapturing_preamblerh   rW   Zclose_boundary_seenÚmoZlastlineZeolmori   rV   ZpayloadÚ	firstlineZbolmor   r   r   rA   Ú   sb   

















ÿÿÿ

























zFeedParser._parsegenc           	      C   sj  d}g }t |ƒD ]8\}}|d dkrR|sFt |¡}| j | j|¡ q| |¡ q|rt| 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ks8tdƒ‚|d |… }|g}q|rf| jj| j |¡Ž  d S )	Nr   r   z 	zFrom é   ú:zMissing header name.z3_parse_headers fed line with no : and no leading WS)Ú	enumerater   Z#FirstHeaderLineIsContinuationDefectr:   rM   rC   r   Zset_rawZheader_source_parseÚ
startswithre   rf   rg   rd   Zset_unixfromr?   r(   ZMisplacedEnvelopeHeaderDefectZdefectsÚfindZInvalidHeaderDefectr&   )	r   r1   Z
lastheaderZ	lastvalueÚlinenor$   rO   rm   Úir   r   r   r[   ×  sH    








zFeedParser._parse_headers)N)r5   r6   r7   r8   r   r   rF   rI   rG   r   rR   rK   rA   r[   r   r   r   r   r   ˆ   s    ~c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )r   z(Like FeedParser, but feed accepts bytes.c                    s   t ƒ  | dd¡¡ d S )NÚasciiÚsurrogateescape)ÚsuperrI   ÚdecoderH   ©Ú	__class__r   r   rI     s    zBytesFeedParser.feed)r5   r6   r7   r8   rI   Ú__classcell__r   r   rz   r   r     s   )r8   Ú__all__ra   Zemailr   Zemail._policybaser   Úcollectionsr   Úior   rb   rZ   rk   re   ZNLCRE_crackrX   r\   ÚNLÚobjectr    r
   r   r   r   r   r   r   Ú<module>   s(   




[   