a
    åzeäN  ã                   @   sÆ   g d ¢Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ dZdZe d¡Ze d	ej¡ZG d
d„ dƒZG dd„ deƒZdZG dd„ deƒZeeejd ƒƒZde ZejZdS ))Ú	GeneratorÚDecodedGeneratorÚBytesGeneratoré    N)Údeepcopy)ÚStringIOÚBytesIO)Ú_has_surrogatesÚ_Ú
z
\r\n|\r|\nz^From c                   @   s¬   e Zd Zd&ddœdd„Zdd„ Z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eZdd„ Zdd„ Zdd„ Zd d!„ Zed(d"d#„ƒZed$d%„ ƒZdS ))r   N©Úpolicyc                C   s6   |d u r|d u rdn|j }|| _|| _|| _|| _d S )NT)Úmangle_from_Ú_fpÚ_mangle_from_Úmaxheaderlenr   )ÚselfÚoutfpr   r   r   © r   ú%/usr/lib/python3.9/email/generator.pyÚ__init__$   s    zGenerator.__init__c                 C   s   | j  |¡ d S ©N)r   Úwrite©r   Úsr   r   r   r   D   s    zGenerator.writeFc                 C   sä   | j d u r|j n| j }|d ur*|j|d}| jd urB|j| jd}|j| _|  | j¡| _d| _|  | j¡| _| j }|j }zX|| _ ||_ |r¸| 	¡ }|s¨dt
 t
 
¡ ¡ }|  || j ¡ |  |¡ W || _ ||_ n|| _ ||_ 0 d S )N)Úlinesep©Zmax_line_lengthÚ zFrom nobody )r   Úcloner   r   Ú_NLÚ_encodeÚ_encoded_NLZ_EMPTYÚ_encoded_EMPTYZget_unixfromÚtimeÚctimer   Ú_write)r   ÚmsgÚunixfromr   r   Zold_gen_policyZold_msg_policyZufromr   r   r   ÚflattenH   s0    
ÿzGenerator.flattenc                 C   s   | j || jd | jdS ©Nr   )Ú	__class__r   r   )r   Úfpr   r   r   r   y   s
    ýzGenerator.clonec                 C   s   t ƒ S r   )r   ©r   r   r   r   Ú_new_buffer   s    zGenerator._new_bufferc                 C   s   |S r   r   r   r   r   r   r   ‘   s    zGenerator._encodec                 C   sT   |sd S t  |¡}|d d… D ]}|  |¡ |  | j¡ q|d rP|  |d ¡ d S )Néÿÿÿÿ)ÚNLCREÚsplitr   r   )r   ÚlinesÚliner   r   r   Ú_write_lines•   s    

zGenerator._write_linesc                 C   sÐ   | j }z2d | _|  ¡  | _ }|  |¡ W || _ | j}| `n|| _ | j}| `0 |r”t|ƒ}| d¡d u rt|d |d< n| d|d ¡ | d|d ¡ t|dd ƒ}|d u r´|  |¡ n|| ƒ | j  	| 
¡ ¡ d S )Núcontent-transfer-encodingr   úContent-Transfer-Encodingúcontent-typeé   Ú_write_headers)r   Ú
_munge_cter,   Ú	_dispatchr   ÚgetZreplace_headerÚgetattrr7   r   Úgetvalue)r   r%   ZoldfpZsfpZ	munge_cteÚmethr   r   r   r$   ¥   s,    þzGenerator._writec                 C   st   |  ¡ }| ¡ }t ||f¡ dd¡}t| d| d ƒ}|d u rh| dd¡}t| d| d ƒ}|d u rh| j}||ƒ d S )Nú-r	   Z_handle_)Úget_content_maintypeÚget_content_subtypeÚ
UNDERSCOREÚjoinÚreplacer;   Ú
_writeBody)r   r%   ÚmainÚsubZspecificr=   Zgenericr   r   r   r9   Ì   s    zGenerator._dispatchc                 C   s6   |  ¡ D ]\}}|  | j ||¡¡ q|  | j¡ d S r   )Ú	raw_itemsr   r   Zfoldr   ©r   r%   ÚhÚvr   r   r   r7   à   s    zGenerator._write_headersc                 C   sž   |  ¡ }|d u rd S t|tƒs.tdt|ƒ ƒ‚t|jƒr~| d¡}|d ur~t|ƒ}|d= | 	||¡ |  ¡ }|d |d f| _
| jrt d|¡}|  |¡ d S )Nzstring payload expected: %sÚcharsetr3   r5   ú>From )Úget_payloadÚ
isinstanceÚstrÚ	TypeErrorÚtyper   Ú_payloadZ	get_paramr   Zset_payloadr8   r   ÚfcrerF   r2   )r   r%   ÚpayloadrK   r   r   r   Ú_handle_textê   s$    


ÿzGenerator._handle_textc                 C   sš  g }|  ¡ }|d u rg }n(t|tƒr2|  |¡ d S t|tƒsB|g}|D ]6}|  ¡ }|  |¡}|j|d| jd | 	| 
¡ ¡ qF| ¡ }|sª| j |¡}|  |¡}| |¡ |jd uræ| jrÊt d|j¡}	n|j}	|  |	¡ |  | j¡ |  d| | j ¡ |r| j | d¡¡ |D ],}
|  | jd | | j ¡ | j |
¡ q|  | jd | d | j ¡ |jd ur–| jr†t d|j¡}n|j}|  |¡ d S )NF©r&   r   rL   z--r   )rM   rN   rO   r   Úlistr,   r   r'   r   Úappendr<   Zget_boundaryr    rB   Ú_make_boundaryZset_boundaryÚpreambler   rS   rF   r2   r   ÚpopÚepilogue)r   r%   ZmsgtextsZsubpartsÚpartr   ÚgÚboundaryZalltextrZ   Z	body_partr\   r   r   r   Ú_handle_multipart  sJ    







zGenerator._handle_multipartc                 C   s6   | j }|jdd| _ z|  |¡ W || _ n|| _ 0 d S )Nr   r   )r   r   r`   )r   r%   Úpr   r   r   Ú_handle_multipart_signed<  s
    z"Generator._handle_multipart_signedc                 C   sš   g }|  ¡ D ]t}|  ¡ }|  |¡}|j|d| jd | ¡ }| | j¡}|rv|d | jkrv| 	| j 
|d d… ¡¡ q| 	|¡ q| j | j 
|¡¡ d S )NFrV   r-   )rM   r,   r   r'   r   r<   r/   r    r!   rX   rB   r   r   )r   r%   Zblocksr]   r   r^   Útextr0   r   r   r   Ú_handle_message_delivery_statusG  s    
z)Generator._handle_message_delivery_statusc                 C   s^   |   ¡ }|  |¡}|j}t|tƒrD|j| d¡d| jd | ¡ }n
|  	|¡}| j
 |¡ d S )Nr   FrV   )r,   r   rR   rN   rW   r'   rM   r   r<   r   r   r   )r   r%   r   r^   rT   r   r   r   Ú_handle_message\  s    




zGenerator._handle_messagec                 C   sv   t  tj¡}dt|  d }|d u r(|S |}d}|  dt |¡ d tj¡}| 	|¡sXqr|d t
|ƒ }|d7 }q0|S )Nz===============z==r   z^--z(--)?$Ú.r6   )ÚrandomÚ	randrangeÚsysÚmaxsizeÚ_fmtÚ_compile_reÚreÚescapeÚ	MULTILINEÚsearchrO   )Úclsrc   Útokenr_   ÚbZcounterZcrer   r   r   rY   u  s    

zGenerator._make_boundaryc                 C   s   t  ||¡S r   )rm   Úcompile©rq   r   Úflagsr   r   r   rl   ‡  s    zGenerator._compile_re)NN)FN)N)Ú__name__Ú
__module__Ú__qualname__r   r   r'   r   r,   r   r2   r$   r9   r7   rU   rD   r`   rb   rd   re   ÚclassmethodrY   rl   r   r   r   r   r      s,   
ÿ 
1'
:r   c                       sL   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Z‡ fd	d
„ZeZe	dd„ ƒZ
‡  ZS )r   c                 C   s   | j  | dd¡¡ d S )NÚasciiÚsurrogateescape)r   r   Úencoder   r   r   r   r   ™  s    zBytesGenerator.writec                 C   s   t ƒ S r   )r   r+   r   r   r   r,   œ  s    zBytesGenerator._new_bufferc                 C   s
   |  d¡S ©Nr{   )r}   r   r   r   r   r   Ÿ  s    zBytesGenerator._encodec                 C   s8   |  ¡ D ]\}}| j | j ||¡¡ q|  | j¡ d S r   )rG   r   r   r   Zfold_binaryr   rH   r   r   r   r7   ¢  s    zBytesGenerator._write_headersc                    s\   |j d u rd S t|j ƒrH| jjdksH| jr:t d|j ¡|_ |  |j ¡ ntt	| ƒ 
|¡ d S )NZ7bitrL   )rR   r   r   Zcte_typer   rS   rF   r2   Úsuperr   rU   )r   r%   ©r)   r   r   rU   ª  s    
zBytesGenerator._handle_textc                 C   s   t  | d¡|¡S r~   )rm   rt   r}   ru   r   r   r   rl   ¹  s    zBytesGenerator._compile_re)rw   rx   ry   r   r,   r   r7   rU   rD   rz   rl   Ú__classcell__r   r   r€   r   r   Œ  s   r   zD[Non-text (%(type)s) part of message omitted, filename %(filename)s]c                   @   s$   e Zd Zdddœdd„Zdd„ ZdS )r   Nr   c                C   s.   t j| ||||d |d u r$t| _n|| _d S r(   )r   r   Ú_FMTrk   )r   r   r   r   Úfmtr   r   r   r   r   Ç  s    ÿzDecodedGenerator.__init__c                 C   s„   |  ¡ D ]v}| ¡ }|dkr2t|jdd| d q|dkr<qt| j| ¡ | ¡ | ¡ | d¡| dd¡| d	d
¡dœ | d qd S )Nrc   F)Údecode)ÚfileZ	multipartz[no filename]zContent-Descriptionz[no description]r4   z[no encoding])rQ   ÚmaintypeZsubtypeÚfilenameÚdescriptionÚencoding)	Úwalkr?   ÚprintrM   rk   Zget_content_typer@   Úget_filenamer:   )r   r%   r]   r†   r   r   r   r9   å  s(    ÿÿù	÷zDecodedGenerator._dispatch)NNN)rw   rx   ry   r   r9   r   r   r   r   r   Á  s   ÿr   r6   z%%0%dd)Ú__all__rm   ri   r"   rg   Úcopyr   Úior   r   Zemail.utilsr   rA   ÚNLrt   r.   ro   rS   r   r   r‚   r   ÚlenÚreprrj   Ú_widthrk   rY   r   r   r   r   Ú<module>   s(   
  t3;