a
    ãzeÊ3 ã                   @   s:  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Zd dl
Z
d dlZd dlmZ zd dlZW n eyŽ   dZY n0 g d¢Ze j d¡ZG dd„ dƒZG dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ dejjƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ G dd „ d eƒZ!G d!d"„ d"eƒZ"G d#d$„ d$ƒZ#G d%d&„ d&e#ƒZ$d>d(d)„Z%d*d+„ Z&d,d-„ Z'd.d/„ Z(d0d1„ Z)d2d3„ Z*G d4d5„ d5e+ƒZ,G d6d7„ d7e,ƒZ-G d8d9„ d9e,ƒZ.G d:d;„ d;e,ƒZ/G d<d=„ d=e,ƒZ0dS )?é    N)ÚGenericAlias)ÚMailboxÚMaildirÚmboxÚMHÚBabylÚMMDFÚMessageÚMaildirMessageÚmboxMessageÚ	MHMessageÚBabylMessageÚMMDFMessageÚErrorÚNoSuchMailboxErrorÚNotEmptyErrorÚExternalClashErrorÚFormatErrorÚasciic                   @   s  e Zd ZdBdd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ ZdCd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%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ ZdDd/d0„Zd1d2„ ZdEd3d4„Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Zd=d>„ Z d?Z!dFd@dA„Z"e#e$ƒZ%dS )Gr   NTc                 C   s    t j t j |¡¡| _|| _d S ©N)ÚosÚpathÚabspathÚ
expanduserÚ_pathÚ_factory©Úselfr   ÚfactoryÚcreate© r    ú/usr/lib/python3.9/mailbox.pyÚ__init__%   s    zMailbox.__init__c                 C   s   t dƒ‚d S ©Nz&Method must be implemented by subclass©ÚNotImplementedError©r   Úmessager    r    r!   Úadd*   s    zMailbox.addc                 C   s   t dƒ‚d S r#   r$   ©r   Úkeyr    r    r!   Úremove.   s    zMailbox.removec                 C   s   |   |¡ d S r   )r+   r)   r    r    r!   Ú__delitem__2   s    zMailbox.__delitem__c                 C   s&   z|   |¡ W n ty    Y n0 d S r   )r+   ÚKeyErrorr)   r    r    r!   Údiscard5   s    zMailbox.discardc                 C   s   t dƒ‚d S r#   r$   ©r   r*   r'   r    r    r!   Ú__setitem__<   s    zMailbox.__setitem__c                 C   s(   z|   |¡W S  ty"   | Y S 0 d S r   )Ú__getitem__r-   )r   r*   Údefaultr    r    r!   Úget@   s    zMailbox.getc                 C   sN   | j s|  |¡S t |  |¡¡}|   |¡W  d   ƒ S 1 s@0    Y  d S r   )r   Úget_messageÚ
contextlibÚclosingÚget_file)r   r*   Úfiler    r    r!   r1   G   s    
zMailbox.__getitem__c                 C   s   t dƒ‚d S r#   r$   r)   r    r    r!   r4   O   s    zMailbox.get_messagec                 C   s   t  |  |¡¡ ¡ S r   ©ÚemailÚmessage_from_bytesÚ	get_bytesZ	as_stringr)   r    r    r!   Ú
get_stringS   s    zMailbox.get_stringc                 C   s   t dƒ‚d S r#   r$   r)   r    r    r!   r<   Z   s    zMailbox.get_bytesc                 C   s   t dƒ‚d S r#   r$   r)   r    r    r!   r7   ^   s    zMailbox.get_filec                 C   s   t dƒ‚d S r#   r$   ©r   r    r    r!   Úiterkeysb   s    zMailbox.iterkeysc                 C   s   t |  ¡ ƒS r   )Úlistr?   r>   r    r    r!   Úkeysf   s    zMailbox.keysc              	   c   s<   |   ¡ D ].}z| | }W n ty.   Y qY n0 |V  qd S r   ©r?   r-   ©r   r*   Úvaluer    r    r!   Ú
itervaluesj   s    
zMailbox.itervaluesc                 C   s   |   ¡ S r   )rE   r>   r    r    r!   Ú__iter__s   s    zMailbox.__iter__c                 C   s   t |  ¡ ƒS r   )r@   rE   r>   r    r    r!   Úvaluesv   s    zMailbox.valuesc              	   c   s@   |   ¡ D ]2}z| | }W n ty.   Y qY n0 ||fV  qd S r   rB   rC   r    r    r!   Ú	iteritemsz   s    
zMailbox.iteritemsc                 C   s   t |  ¡ ƒS r   )r@   rH   r>   r    r    r!   Úitemsƒ   s    zMailbox.itemsc                 C   s   t dƒ‚d S r#   r$   r)   r    r    r!   Ú__contains__‡   s    zMailbox.__contains__c                 C   s   t dƒ‚d S r#   r$   r>   r    r    r!   Ú__len__‹   s    zMailbox.__len__c                 C   s   |   ¡ D ]}|  |¡ qd S r   )rA   r.   r)   r    r    r!   Úclear   s    zMailbox.clearc                 C   s2   z| | }W n t y"   | Y S 0 |  |¡ |S r   )r-   r.   )r   r*   r2   Úresultr    r    r!   Úpop”   s    

zMailbox.popc                 C   s*   |   ¡ D ]}||  |¡f  S tdƒ‚d S )NzNo messages in mailbox)r?   rN   r-   r)   r    r    r!   Úpopitem   s    zMailbox.popitemc              	   C   sr   t |dƒr| ¡ }nt |dƒr(| ¡ }n|}d}|D ],\}}z|| |< W q4 ty^   d}Y q40 q4|rntdƒ‚d S )NrH   rI   FTzNo message with key(s))ÚhasattrrH   rI   r-   )r   ÚargÚsourceZbad_keyr*   r'   r    r    r!   Úupdate¤   s    



zMailbox.updatec                 C   s   t dƒ‚d S r#   r$   r>   r    r    r!   Úflushµ   s    zMailbox.flushc                 C   s   t dƒ‚d S r#   r$   r>   r    r    r!   Úlock¹   s    zMailbox.lockc                 C   s   t dƒ‚d S r#   r$   r>   r    r    r!   Úunlock½   s    zMailbox.unlockc                 C   s   t dƒ‚d S r#   r$   r>   r    r    r!   ÚcloseÁ   s    zMailbox.closec                 C   s,   z|  d¡W S  ty&   tdƒ‚Y n0 d S )Nr   z?String input must be ASCII-only; use bytes or a Message instead)ÚencodeÚUnicodeErrorÚ
ValueErrorr&   r    r    r!   Ú_string_to_bytesÅ   s    zMailbox._string_to_bytesFc           	      C   sü  t |tjjƒrvt ¡ }tj ||d¡}| |¡ | 	d¡ | 
¡ }| dt¡}| |¡ | jrr| t¡sr| t¡ n‚t |tttjfƒrt |tjƒr¬t dtd¡ | ¡ }t |tƒrÀ|  |¡}|rÐ| dd¡}| dt¡}| |¡ | jrø| t¡sø| t¡ nòt|dƒrèt|dƒr2t d	td¡ |j}d }| ¡ }| d
¡r\|d d… d }n| d¡rx|d d… d }|s‚qÂ|r¤| d¡r¤d|dd …  }| dt¡}| |¡ |}q6| jrø|rø| t¡sø| t¡ ntdt|ƒ ƒ‚d S )Nr   ó   
ú8Use of StringIO input is deprecated, use BytesIO insteadé   s   
From s   
>From ÚreadÚbufferúDUse of text mode files is deprecated, use a binary mode file insteadó   
éþÿÿÿó   éÿÿÿÿó   From s   >From é   úInvalid message type: %s)Ú
isinstancer:   r'   r	   ÚioÚBytesIOÚ	generatorÚBytesGeneratorÚflattenÚseekr_   ÚreplaceÚlinesepÚwriteÚ_append_newlineÚendswithÚstrÚbytesÚStringIOÚwarningsÚwarnÚDeprecationWarningÚgetvaluer[   rP   r`   ÚreadlineÚ
startswithÚ	TypeErrorÚtype)	r   r'   ÚtargetZmangle_from_r`   ÚgenÚdataZlastlineÚliner    r    r!   Ú_dump_messageÒ   s\    


ÿ


ÿ
zMailbox._dump_message)NT)N)N)N)F)&Ú__name__Ú
__module__Ú__qualname__r"   r(   r+   r,   r.   r0   r3   r1   r4   r=   r<   r7   r?   rA   rE   rF   rG   rH   rI   rJ   rK   rL   rN   rO   rS   rT   rU   rV   rW   r[   rs   r„   Úclassmethodr   Ú__class_getitem__r    r    r    r!   r   "   sB   

		
	

6r   c                   @   sÖ   e Zd ZdZd5d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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,Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ ZdS )6r   ú:NTc                 C   s¬   t  | |||¡ tj | jd¡tj | jd¡tj | jd¡dœ| _tj | j¡sŠ|r€t | jd¡ | j 	¡ D ]}t |d¡ qln
t
| jƒ‚i | _dddœ| _d| _d| _d S )	NÚtmpÚnewÚcur)r‹   rŒ   r   éÀ  r   )r   rŒ   gš™™™™™¹?)r   r"   r   r   Újoinr   Ú_pathsÚexistsÚmkdirrG   r   Ú_tocÚ_toc_mtimesÚ
_last_readÚ_skewfactor)r   Údirnamer   r   r   r    r    r!   r"     s    ý
zMaildir.__init__c              
   C   s|  |   ¡ }z|  ||¡ W n( ty@   | ¡  t |j¡ ‚ Y n0 t|ƒ t|t	ƒrz| 
¡ }| j| ¡  }|| jkr‚d}nd}d}tj |j¡ | j¡d }tj | j||| ¡}t|t	ƒrÜt |jtj |j¡| ¡ f¡ zJzt |j|¡ W n& ttfy   t |j|¡ Y n0 t |j¡ W nP tyv } z6t |j¡ |jtjkr`td| ƒ‚n‚ W Y d }~n
d }~0 0 |S )NÚ rŒ   r   z$Name clash with existing message: %s)Ú_create_tmpr„   ÚBaseExceptionrW   r   r+   ÚnameÚ_sync_closeri   r
   Ú
get_subdirÚcolonÚget_infor   ÚbasenameÚsplitr   r   ÚutimeÚgetatimeÚget_dateÚlinkÚAttributeErrorÚPermissionErrorÚrenameÚOSErrorÚerrnoÚEEXISTr   )r   r'   Ztmp_fileÚsubdirÚsuffixÚuniqÚdestÚer    r    r!   r(   $  sF    


ÿÿzMaildir.addc                 C   s    t  t j | j|  |¡¡¡ d S r   )r   r+   r   r   r   Ú_lookupr)   r    r    r!   r+   N  s    zMaildir.removec              	   C   s*   z|   |¡ W n ttfy$   Y n0 d S r   )r+   r-   ÚFileNotFoundErrorr)   r    r    r!   r.   R  s    zMaildir.discardc                 C   sÊ   |   |¡}|  |¡}|   |¡}t|tƒr.|}n|}tj |¡}| j|v r`| j| | j¡d  }nd}|  	|¡ tj 
| j|¡}	tj 
| j||| ¡}
t|tƒrºt |	tj |	¡| ¡ f¡ t |	|
¡ d S )Nre   r˜   )r±   r(   ri   r
   r   r   r—   rž   r¡   r.   r   r   r¢   r£   r¤   r¨   )r   r*   r'   Zold_subpathZtemp_keyZtemp_subpathZdominant_subpathr¬   r­   Ztmp_pathÚnew_pathr    r    r!   r0   Z  s$    






ÿzMaildir.__setitem__c                 C   s¶   |   |¡}ttj | j|¡dƒ*}| jr4|  |¡}nt|ƒ}W d   ƒ n1 sP0    Y  tj |¡\}}| 	|¡ | j
|v r”| | | j
¡d ¡ | tj tj | j|¡¡¡ |S )NÚrbre   )r±   Úopenr   r   r   r   r   r
   r¡   Ú
set_subdirrž   Úset_infoÚset_dateÚgetmtime)r   r*   ÚsubpathÚfÚmsgr¬   r›   r    r    r!   r4   u  s    
&

zMaildir.get_messagec                 C   sP   t tj | j|  |¡¡dƒ }| ¡  td¡W  d   ƒ S 1 sB0    Y  d S )Nr´   r\   )	rµ   r   r   r   r   r±   r_   rp   rq   ©r   r*   r»   r    r    r!   r<   „  s    zMaildir.get_bytesc                 C   s$   t tj | j|  |¡¡dƒ}t|ƒS )Nr´   )rµ   r   r   r   r   r±   Ú
_ProxyFiler½   r    r    r!   r7   ‰  s    zMaildir.get_filec              	   c   sD   |   ¡  | jD ]0}z|  |¡ W n ty6   Y qY n0 |V  qd S r   )Ú_refreshr“   r±   r-   r)   r    r    r!   r?   Ž  s    

zMaildir.iterkeysc                 C   s   |   ¡  || jv S r   )r¿   r“   r)   r    r    r!   rJ   ˜  s    zMaildir.__contains__c                 C   s   |   ¡  t| jƒS r   )r¿   Úlenr“   r>   r    r    r!   rK     s    zMaildir.__len__c                 C   s   d S r   r    r>   r    r    r!   rT   ¢  s    zMaildir.flushc                 C   s   d S r   r    r>   r    r    r!   rU   ¨  s    zMaildir.lockc                 C   s   d S r   r    r>   r    r    r!   rV   ¬  s    zMaildir.unlockc                 C   s   d S r   r    r>   r    r    r!   rW   °  s    zMaildir.closec                 C   s\   g }t  | j¡D ]F}t|ƒdkr|d dkrt j t j | j|¡¡r| |dd … ¡ q|S )Né   r   Ú.)r   Úlistdirr   rÀ   r   Úisdirr   Úappend©r   rM   Úentryr    r    r!   Úlist_folders´  s    ÿzMaildir.list_foldersc                 C   s    t tj | jd| ¡| jddS )NrÂ   F©r   r   )r   r   r   r   r   r   ©r   Úfolderr    r    r!   Ú
get_folder½  s    þzMaildir.get_folderc                 C   s\   t j | jd| ¡}t|| jd}t j |d¡}t j |¡sXt  t  |t j	t j
B d¡¡ |S )NrÂ   ©r   Zmaildirfolderé¶  )r   r   r   r   r   r   r‘   rW   rµ   ÚO_CREATÚO_WRONLY)r   rË   r   rM   Zmaildirfolder_pathr    r    r!   Ú
add_folderÃ  s    ÿzMaildir.add_folderc                 C   s  t j | jd| ¡}t  t j |d¡¡t  t j |d¡¡ D ](}t|ƒdk sX|d dkr<td| ƒ‚q<t  |¡D ]B}|dkrp|dkrp|dkrpt j t j ||¡¡rptd||f ƒ‚qpt j|d	d
D ]F\}}}|D ]}t  	t j ||¡¡ qÐ|D ]}t  
t j ||¡¡ qîqÂt  
|¡ d S )NrÂ   rŒ   r   rÁ   r   zFolder contains message(s): %sr‹   z%Folder contains subdirectory '%s': %sF)Útopdown)r   r   r   r   rÃ   rÀ   r   rÄ   Úwalkr+   Úrmdir)r   rË   r   rÇ   ÚrootÚdirsÚfilesr    r    r!   Úremove_folderÍ  s&    ÿÿÿzMaildir.remove_folderc                 C   sX   t   ¡ }t tj | jd¡¡D ]4}tj | jd|¡}|tj |¡ dkrt |¡ qd S )Nr‹   i@ú )Útimer   rÃ   r   r   r   r£   r+   )r   ÚnowrÇ   r   r    r    r!   Úcleanà  s
    zMaildir.cleanrÁ   c                 C   sÔ   t   ¡ }t ¡ }d|v r$| dd¡}d|v r8| dd¡}dt|ƒt|d d ƒt ¡ tj|f }tj	 
| jd|¡}zt |¡ W nB tyÂ   t jd7  _zt|ƒW  Y S  ty¼   Y n0 Y n0 td	| ƒ‚d S )
Nú/z\057rŠ   z\072z%s.M%sP%sQ%s.%srÁ   g    €„.Ar‹   z&Name clash prevented file creation: %s)rÙ   ÚsocketÚgethostnamerp   Úintr   Úgetpidr   Ú_countr   r   r   Ústatr²   Ú_create_carefullyÚFileExistsErrorr   )r   rÚ   Úhostnamer®   r   r    r    r!   r™   ê  s*    ÿÿzMaildir._create_tmpc                 C   sØ   t   ¡ | j d| j kr^d}| jD ]2}tj | j| ¡}|| j| krJd}|| j|< q"|s^d S i | _| jD ]^}| j| }t 	|¡D ]D}tj 
||¡}tj |¡r¢q‚| | j¡d }tj 
||¡| j|< q‚qjt   ¡ | _d S )Né   FTr   )rÙ   r•   r–   r”   r   r   r¹   r   r“   rÃ   r   rÄ   r¡   rž   )r   Zrefreshr¬   Úmtimer   rÇ   Úpr®   r    r    r!   r¿     s&    


zMaildir._refreshc                 C   s|   z.t j t j | j| j| ¡¡r,| j| W S W n ty@   Y n0 |  ¡  z| j| W S  tyv   td| ƒd ‚Y n0 d S ©NúNo message with key: %s)r   r   r‘   r   r   r“   r-   r¿   r)   r    r    r!   r±   &  s    zMaildir._lookupc                 C   sT   t | dƒs|  ¡ | _z| t| jƒ W S  ty8   Y d S  tyL   Y qY q0 qd S )NÚ_onetime_keys)rP   r?   rë   ÚnextÚStopIterationr-   r>   r    r    r!   rì   4  s    

zMaildir.next)NT)r…   r†   r‡   rž   r"   r(   r+   r.   r0   r4   r<   r7   r?   rJ   rK   rT   rU   rV   rW   rÈ   rÌ   rÑ   rØ   rÛ   rá   r™   r¿   r±   rì   r    r    r    r!   r     s4   
*
	
$r   c                   @   s   e Z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dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd$dd „Zd!d"„ ZdS )%Ú_singlefileMailboxNTc              
   C   sÀ   t  | |||¡ zt| jdƒ}W np ty } zX|jtjkrZ|rNt| jdƒ}q|t| jƒ‚n"|jtjtj	fv rzt| jdƒ}n‚ W Y d }~n
d }~0 0 || _
d | _d| _d| _d| _d| _d | _d S )Núrb+zwb+r´   r   F)r   r"   rµ   r   r©   rª   ÚENOENTr   ÚEACCESÚEROFSÚ_filer“   Ú	_next_keyÚ_pendingÚ_pending_syncÚ_lockedÚ_file_length)r   r   r   r   r»   r°   r    r    r!   r"   D  s$    z_singlefileMailbox.__init__c                 C   s8   |   ¡  |  |¡| j| j< |  jd7  _d| _| jd S )NrÁ   T)r±   Ú_append_messager“   rô   rö   r&   r    r    r!   r(   [  s
    z_singlefileMailbox.addc                 C   s   |   |¡ | j|= d| _d S ©NT)r±   r“   rõ   r)   r    r    r!   r+   e  s    
z_singlefileMailbox.removec                 C   s$   |   |¡ |  |¡| j|< d| _d S rú   )r±   rù   r“   rõ   r/   r    r    r!   r0   k  s    
z_singlefileMailbox.__setitem__c                 c   s   |   ¡  | j ¡ E d H  d S r   )r±   r“   rA   r>   r    r    r!   r?   q  s    z_singlefileMailbox.iterkeysc                 C   s   |   ¡  || jv S r   )r±   r“   r)   r    r    r!   rJ   v  s    z_singlefileMailbox.__contains__c                 C   s   |   ¡  t| jƒS r   )r±   rÀ   r“   r>   r    r    r!   rK   {  s    z_singlefileMailbox.__len__c                 C   s   | j st| jƒ d| _ d S rú   )r÷   Ú
_lock_fileró   r>   r    r    r!   rU   €  s    
z_singlefileMailbox.lockc                 C   s   | j rt| jƒ d| _ d S ©NF)r÷   Ú_unlock_fileró   r>   r    r    r!   rV   †  s    
z_singlefileMailbox.unlockc           
      C   sÔ  | j s | jrt| jƒ d| _d S | j dd¡ | j ¡ }|| jkrTtd| j|f ƒ‚t| j	ƒ}z¤i }|  
|¡ t| j ¡ ƒD ]x}| j| \}}| j |¡ |  |¡ | ¡ }| j td|| j ¡  ƒ¡}|sÎqÚ| |¡ q¬|| ¡ f||< |  |¡ q|| ¡ | _W n"   | ¡  t |j¡ ‚ Y n0 t|ƒ | j ¡  t | j	¡j}	t |j|	¡ zt |j| j	¡ W n0 ty˜   t | j	¡ t |j| j	¡ Y n0 t| j	dƒ| _|| _d| _ d| _| jrÐt| jdd d S )NFr   ræ   z4Size of mailbox file changed (expected %i, found %i)é   rï   )Údotlock) rõ   rö   Ú_sync_flushró   ro   Útellrø   r   Ú_create_temporaryr   Ú_pre_mailbox_hookÚsortedr“   rA   Ú_pre_message_hookr_   Úminrr   Ú_post_message_hookrW   r   r+   r›   rœ   râ   Úst_modeÚchmodr¨   rä   rµ   r÷   rû   )
r   Zcur_lenZnew_fileZnew_tocr*   ÚstartÚstopZ	new_startr`   Úmoder    r    r!   rT   Œ  s`    
	

þ



ÿ
z_singlefileMailbox.flushc                 C   s   d S r   r    ©r   r»   r    r    r!   r  Ì  s    z$_singlefileMailbox._pre_mailbox_hookc                 C   s   d S r   r    r  r    r    r!   r  Ð  s    z$_singlefileMailbox._pre_message_hookc                 C   s   d S r   r    r  r    r    r!   r  Ô  s    z%_singlefileMailbox._post_message_hookc                 C   sh   z6|   ¡  W z| jr|  ¡  W | j ¡  qd| j ¡  0 n,z| jrH|  ¡  W | j ¡  n| j ¡  0 0 d S r   )rT   r÷   rV   ró   rW   r>   r    r    r!   rW   Ø  s    

ü
z_singlefileMailbox.closec                 C   sL   | j d u r|  ¡  |d urHz| j | W S  tyF   td| ƒd ‚Y n0 d S ré   )r“   Ú_generate_tocr-   r)   r    r    r!   r±   ã  s    
z_singlefileMailbox._lookupc                 C   sš   | j  dd¡ | j  ¡ }t| jƒdkr8| js8|  | j ¡ z&|  | j ¡ |  |¡}|  	| j ¡ W n  t
y~   | j  |¡ ‚ Y n0 | j  ¡  | j  ¡ | _|S )Nr   ræ   )ró   ro   r  rÀ   r“   rõ   r  r  Ú_install_messager  rš   ÚtruncaterT   rø   )r   r'   ÚbeforeZoffsetsr    r    r!   rù   í  s    


z"_singlefileMailbox._append_message)NT)N)r…   r†   r‡   r"   r(   r+   r0   r?   rJ   rK   rU   rV   rT   r  r  r  rW   r±   rù   r    r    r    r!   rî   A  s    

@

rî   c                   @   s>   e Zd ZdZdd„ Zddd„Zddd„Zdd	d
„Zdd„ ZdS )Ú	_mboxMMDFTc                 C   sp   |   |¡\}}| j |¡ | j ¡  td¡}| j || j ¡  ¡}|  | td¡¡}| 	|dd …  
d¡¡ |S )Nó    r\   rg   r   )r±   ró   ro   r|   rp   rq   r_   r  Ú_message_factoryÚset_fromÚdecode)r   r*   r
  r  Ú	from_lineÚstringr¼   r    r    r!   r4   	  s    z_mboxMMDF.get_messageFc                 C   s   t  |  ||¡¡j|dS )N)Úunixfromr9   )r   r*   Úfrom_r    r    r!   r=     s
    
ÿÿz_mboxMMDF.get_stringc                 C   sJ   |   |¡\}}| j |¡ |s(| j ¡  | j || j ¡  ¡}| td¡S ©Nr\   )r±   ró   ro   r|   r_   r  rp   rq   )r   r*   r  r
  r  r  r    r    r!   r<     s    
z_mboxMMDF.get_bytesc                 C   s<   |   |¡\}}| j |¡ |s(| j ¡  t| j| j ¡ |ƒS r   )r±   ró   ro   r|   Ú_PartialFiler  )r   r*   r  r
  r  r    r    r!   r7   !  s
    
z_mboxMMDF.get_filec                 C   s  d }t |tƒr|  |¡}t |tƒrf| d¡rf| d¡}|dkr\|d |… }||d d … }q°|}d}nJt |tƒrˆ| ¡  d¡}d| }n(t |t	j
jƒr°| ¡ }|d ur°| d¡}|d u rÎdt t ¡ ¡ ¡  }| j ¡ }| j |t ¡ |  || j| j¡ | j ¡ }||fS )Nrf   r\   re   rÁ   r  r   s   From MAILER-DAEMON )ri   ru   r[   rv   r}   ÚfindÚ_mboxMMDFMessageÚget_fromrX   r:   r'   r	   Úget_unixfromrÙ   ÚasctimeÚgmtimeró   r  rr   rq   r„   Ú_mangle_from_)r   r'   r  ÚnewlineZauthorr
  r  r    r    r!   r  )  s0    







z_mboxMMDF._install_messageN)F)F)F)	r…   r†   r‡   r#  r4   r=   r<   r7   r  r    r    r    r!   r    s   


	
r  c                   @   s.   e Zd ZdZdZd	dd„Zdd„ Zdd„ ZdS )
r   TNc                 C   s   t | _t | |||¡ d S r   )r   r  r  r"   r   r    r    r!   r"   O  s    zmbox.__init__c                 C   s   |  t¡ d S r   ©rr   rq   r  r    r    r!   r  T  s    zmbox._post_message_hookc                 C   sè   g g  }}d}| j  d¡ | j  ¡ }| j  ¡ }| d¡rzt|ƒt|ƒk rj|r`| |ttƒ ¡ n
| |¡ | |¡ d}q|s¤|r–| |ttƒ ¡ q¸| |¡ q¸q|tkr²d}qd}qtt	t
||ƒƒƒ| _t| jƒ| _| j  ¡ | _d S )NFr   rf   T)ró   ro   r  r|   r}   rÀ   rÅ   rq   ÚdictÚ	enumerateÚzipr“   rô   rø   )r   ÚstartsÚstopsZlast_was_emptyÚline_posrƒ   r    r    r!   r  X  s.    






zmbox._generate_toc)NT)r…   r†   r‡   r#  rs   r"   r  r  r    r    r    r!   r   F  s
   
r   c                   @   s.   e Zd Zddd„Zdd„ Zdd„ Zd	d
„ ZdS )r   NTc                 C   s   t | _t | |||¡ d S r   )r   r  r  r"   r   r    r    r!   r"   }  s    zMMDF.__init__c                 C   s   |  dt ¡ d S ©Nó   r%  r  r    r    r!   r  ‚  s    zMMDF._pre_message_hookc                 C   s   |  td t ¡ d S r,  r%  r  r    r    r!   r  †  s    zMMDF._post_message_hookc                 C   sÞ   g g  }}| j  d¡ d}|}| j  ¡ }| j  ¡ }| dt ¡r˜| |¡ |}| j  ¡ }| j  ¡ }|dt kr„| |ttƒ ¡ qžqJ|sJ| |¡ qžqJq|sq qtt	t
||ƒƒƒ| _t| jƒ| _| j  dd¡ | j  ¡ | _d S )Nr   r-  ræ   )ró   ro   r|   r  r}   rq   rÅ   rÀ   r&  r'  r(  r“   rô   rø   )r   r)  r*  Únext_posr+  rƒ   r    r    r!   r  Š  s.    






zMMDF._generate_toc)NT)r…   r†   r‡   r"   r  r  r  r    r    r    r!   r   z  s   
r   c                   @   s¾   e Z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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)d*„ Zd+d,„ Zd-d.„ ZdS )0r   NTc                 C   st   t  | |||¡ tj | j¡sj|r`t | jd¡ t t tj 	| jd¡tj
tjB tjB d¡¡ n
t| jƒ‚d| _d S )NrŽ   ú.mh_sequencesi€  F)r   r"   r   r   r‘   r   r’   rW   rµ   r   rÏ   ÚO_EXCLrÐ   r   r÷   r   r    r    r!   r"   ª  s    ÿ
zMH.__init__c                 C   sü   |   ¡ }t|ƒdkrd}nt|ƒd }tj | jt|ƒ¡}t|ƒ}d}z¢| j	rVt
|ƒ zrz|  ||¡ W n8 ty    | j	r„t|ƒ t|ƒ d}t |¡ ‚ Y n0 t|tƒr¸|  ||¡ W | j	rÚt|ƒ n| j	rØt|ƒ 0 W |søt|ƒ n|söt|ƒ 0 |S )Nr   rÁ   FT)rA   rÀ   Úmaxr   r   r   r   ru   rã   r÷   rû   r„   rš   rý   rœ   r+   ri   r   Ú_dump_sequences)r   r'   rA   Znew_keyr³   r»   Úclosedr    r    r!   r(   ¶  s>    


ÿ
ÿ
zMH.addc              
   C   sz   t j | jt|ƒ¡}zt|dƒ}W n@ tyb } z(|jtjkrLt	d| ƒ‚n‚ W Y d }~nd }~0 0 | 
¡  t  |¡ d S ©Nrï   rê   )r   r   r   r   ru   rµ   r©   rª   rð   r-   rW   r+   )r   r*   r   r»   r°   r    r    r!   r+   Ø  s    z	MH.removec              
   C   sî   t j | jt|ƒ¡}zt|dƒ}W n@ tyb } z(|jtjkrLt	d| ƒ‚n‚ W Y d }~n
d }~0 0 zz| j
rtt|ƒ zNt  t  |t jt jB ¡¡ |  ||¡ t|tƒr²|  ||¡ W | j
rÔt|ƒ n| j
rÒt|ƒ 0 W t|ƒ n
t|ƒ 0 d S r4  )r   r   r   r   ru   rµ   r©   rª   rð   r-   r÷   rû   rW   rÐ   ÚO_TRUNCr„   ri   r   r2  rý   rœ   )r   r*   r'   r   r»   r°   r    r    r!   r0   æ  s(    

ÿzMH.__setitem__c              
   C   s  z@| j r$ttj | jt|ƒ¡dƒ}nttj | jt|ƒ¡dƒ}W n@ ty€ } z(|jtj	krjt
d| ƒ‚n‚ W Y d }~n
d }~0 0 |J | j r–t|ƒ zt|ƒ}W | j rÂt|ƒ n| j rÀt|ƒ 0 W d   ƒ n1 sÖ0    Y  |  ¡  ¡ D ]\}}||v rì| |¡ qì|S )Nrï   r´   rê   )r÷   rµ   r   r   r   r   ru   r©   rª   rð   r-   rû   r   rý   Úget_sequencesrI   Úadd_sequence)r   r*   r»   r°   r¼   r›   Úkey_listr    r    r!   r4   þ  s*    

ÿ(zMH.get_messagec              
   C   sú   z@| j r$ttj | jt|ƒ¡dƒ}nttj | jt|ƒ¡dƒ}W n@ ty€ } z(|jtj	krjt
d| ƒ‚n‚ W Y d }~n
d }~0 0 |` | j r–t|ƒ z0| ¡  td¡W | j r¶t|ƒ W  d   ƒ S n| j rÖt|ƒ 0 W d   ƒ n1 sì0    Y  d S )Nrï   r´   rê   r\   )r÷   rµ   r   r   r   r   ru   r©   rª   rð   r-   rû   r_   rp   rq   rý   ©r   r*   r»   r°   r    r    r!   r<     s(    ý ÿzMH.get_bytesc              
   C   sh   zt tj | jt|ƒ¡dƒ}W n@ ty^ } z(|jtjkrHt	d| ƒ‚n‚ W Y d }~n
d }~0 0 t
|ƒS )Nr´   rê   )rµ   r   r   r   r   ru   r©   rª   rð   r-   r¾   r9  r    r    r!   r7   ,  s    zMH.get_filec                 C   s   t tdd„ t | j¡D ƒƒƒS )Nc                 s   s   | ]}|  ¡ rt|ƒV  qd S r   )Úisdigitrß   )Ú.0rÇ   r    r    r!   Ú	<genexpr>9  s   ÿzMH.iterkeys.<locals>.<genexpr>)Úiterr  r   rÃ   r   r>   r    r    r!   r?   7  s    zMH.iterkeysc                 C   s   t j t j | jt|ƒ¡¡S r   )r   r   r‘   r   r   ru   r)   r    r    r!   rJ   <  s    zMH.__contains__c                 C   s   t t|  ¡ ƒƒS r   )rÀ   r@   r?   r>   r    r    r!   rK   @  s    z
MH.__len__c                 C   s2   | j s.ttj | jd¡dƒ| _t| jƒ d| _ d S )Nr/  rï   T)r÷   rµ   r   r   r   r   ró   rû   r>   r    r    r!   rU   D  s    
zMH.lockc                 C   s(   | j r$t| jƒ t| jƒ | `d| _ d S rü   )r÷   rý   ró   rœ   r>   r    r    r!   rV   K  s
    

z	MH.unlockc                 C   s   d S r   r    r>   r    r    r!   rT   S  s    zMH.flushc                 C   s   | j r|  ¡  d S r   )r÷   rV   r>   r    r    r!   rW   W  s    zMH.closec                 C   s<   g }t  | j¡D ]&}t j t j | j|¡¡r| |¡ q|S r   )r   rÃ   r   r   rÄ   r   rÅ   rÆ   r    r    r!   rÈ   \  s
    zMH.list_foldersc                 C   s   t tj | j|¡| jddS )NFrÉ   ©r   r   r   r   r   r   rÊ   r    r    r!   rÌ   d  s    ÿzMH.get_folderc                 C   s   t tj | j|¡| jdS )NrÍ   r>  rÊ   r    r    r!   rÑ   i  s    ÿzMH.add_folderc                 C   s`   t j | j|¡}t  |¡}|dgkr:t  t j |d¡¡ n|g krDntd| j ƒ‚t  |¡ d S )Nr/  zFolder not empty: %s)r   r   r   r   rÃ   r+   r   rÔ   )r   rË   r   Úentriesr    r    r!   rØ   n  s    

zMH.remove_folderc           
   
      s  i }t tj | jd¡dddä}t|  ¡ ƒ‰ |D ]Â}zš| d¡\}}tƒ }| ¡ D ]H}| ¡ rn| 	t
|ƒ¡ qRdd„ | d¡D ƒ\}}	| t||	d	 ƒ¡ qR‡ fd
d„t|ƒD ƒ||< t|| ƒdkrÌ||= W q0 tyð   td| ¡  ƒ‚Y q00 q0W d   ƒ n1 s
0    Y  |S )Nr/  ÚrÚASCII©ÚencodingrŠ   c                 s   s   | ]}t |ƒV  qd S r   )rß   )r;  Úxr    r    r!   r<  ‡  r  z#MH.get_sequences.<locals>.<genexpr>ú-rÁ   c                    s   g | ]}|ˆ v r|‘qS r    r    )r;  r*   ©Zall_keysr    r!   Ú
<listcomp>‰  s   ÿz$MH.get_sequences.<locals>.<listcomp>r   z"Invalid sequence specification: %s)rµ   r   r   r   r   ÚsetrA   r¡   r:  r(   rß   rS   Úranger  rÀ   rZ   r   Úrstrip)
r   Úresultsr»   rƒ   r›   ÚcontentsrA   Úspecr
  r  r    rF  r!   r6  z  s(    
ÿ.zMH.get_sequencesc                 C   s
  t tj | jd¡ddd}zàt t  |jtjtjB ¡¡ | 	¡ D ]®\}}t
|ƒdkrVq@| |d ¡ d }d}tt|ƒƒD ]R}|d |krœ|sÆd	}| d
¡ n*|r¸d}| d||f ¡ n| d| ¡ |}qx|rä| t|ƒd ¡ q@| d¡ q@W t|ƒ n
t|ƒ 0 d S )Nr/  zr+rA  rB  r   rŠ   FrÁ   TrE  z%s %sz %sÚ
)rµ   r   r   r   r   rW   r›   rÐ   r5  rI   rÀ   rr   r  rH  ru   rœ   )r   Ú	sequencesr»   r›   rA   ÚprevZ
completingr*   r    r    r!   Úset_sequences’  s.    zMH.set_sequencesc           	      C   s<  |   ¡ }d}g }|  ¡ D ]À}|d |krÐ| ||d f¡ z4t tj | jt|ƒ¡tj | jt|d ƒ¡¡ W nF t	t
fy´   t tj | jt|ƒ¡tj | jt|d ƒ¡¡ Y n0 t tj | jt|ƒ¡¡ |d7 }q|d | _t|ƒdkrôd S | ¡ D ]0\}}|D ]"\}}||v r||| |¡< qqü|  |¡ d S )Nr   rÁ   )r6  r?   rÅ   r   r¥   r   r   r   ru   r¦   r§   r¨   Úunlinkrô   rÀ   rI   ÚindexrQ  )	r   rO  rP  Zchangesr*   r›   r8  ÚoldrŒ   r    r    r!   Úpack¯  s0    ÿÿ



zMH.packc                 C   st   |  ¡ }|   ¡ }| ¡ D ]0\}}||v r4| |¡ q||v r|| |¡= q|D ]}||vrN|g||< qN|  |¡ d S r   )r6  rI   rÅ   rS  rQ  )r   r'   r*   Zpending_sequencesZall_sequencesr›   r8  Úsequencer    r    r!   r2  É  s    zMH._dump_sequences)NT)r…   r†   r‡   r"   r(   r+   r0   r4   r<   r7   r?   rJ   rK   rU   rV   rT   rW   rÈ   rÌ   rÑ   rØ   r6  rQ  rU  r2  r    r    r    r!   r   §  s,   
"r   c                   @   s‚   e Zd Zeh 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dd„ Zdd„ Zdd„ Zdd„ ZdS )r   >   ÚunseenÚdeletedÚ	forwardedÚansweredÚresentZeditedZfiledNTc                 C   s   t  | |||¡ i | _d S r   )rî   r"   Ú_labelsr   r    r    r!   r"   Þ  s    zBabyl.__init__c                 C   s(   t  | |¡}t|tƒr$| ¡ | j|< |S r   )rî   r(   ri   r   Ú
get_labelsr\  )r   r'   r*   r    r    r!   r(   ã  s    
z	Babyl.addc                 C   s"   t  | |¡ || jv r| j|= d S r   )rî   r+   r\  r)   r    r    r!   r+   ê  s    
zBabyl.removec                 C   s*   t  | ||¡ t|tƒr&| ¡ | j|< d S r   )rî   r0   ri   r   r]  r\  r/   r    r    r!   r0   ð  s    
zBabyl.__setitem__c           
      C   sò   |   |¡\}}| j |¡ | j ¡  t ¡ }| j ¡ }|dt ks\|sHq\| | td¡¡ q,t ¡ }| j ¡ }|tks|s|q| | td¡¡ qd|| j 	¡  }| j 
|¡}| td¡}t| ¡ | ƒ}	|	 | ¡ ¡ || jv rî|	 | j| ¡ |	S ©Nó   *** EOOH ***r\   )r±   ró   ro   r|   rj   rk   rq   rr   rp   r  r_   r   r{   Úset_visibler\  Ú
set_labels)
r   r*   r
  r  Úoriginal_headersrƒ   Zvisible_headersÚnÚbodyr¼   r    r    r!   r4   ö  s*    



zBabyl.get_messagec           	      C   s¬   |   |¡\}}| j |¡ | j ¡  t ¡ }| j ¡ }|dt ks\|sHq\| | td¡¡ q,| j ¡ }|tksv|s\qvq\| 	¡ }|| j 
¡  }| j |¡}| td¡}|| S r^  )r±   ró   ro   r|   rj   rk   rq   rr   rp   r{   r  r_   )	r   r*   r
  r  rb  rƒ   Zheadersrc  r‚   r    r    r!   r<     s     


zBabyl.get_bytesc                 C   s   t  |  |¡ dt¡¡S r  )rj   rk   r<   rp   rq   r)   r    r    r!   r7   (  s    zBabyl.get_filec                 C   s<   |   ¡  tƒ }| j ¡ D ]}| |¡ q| | j¡ t|ƒS r   )r±   rH  r\  rG   rS   Údifference_updateÚ_special_labelsr@   )r   ÚlabelsZ
label_listr    r    r!   r]  ,  s    zBabyl.get_labelsc                 C   s:  g g  }}| j  d¡ d}g }|}| j  ¡ }| j  ¡ }|dt krœt|ƒt|ƒk rd| |ttƒ ¡ | |¡ dd„ | j  ¡ dd …  d¡D ƒ}| |¡ q|dks°|dt krÔt|ƒt|ƒk rì| |ttƒ ¡ q|s| |ttƒ ¡ qîqtt	t
||ƒƒƒ| _tt	|ƒƒ| _t| jƒ| _| j  dd¡ | j  ¡ | _d S )	Nr   s   c                 S   s   g | ]}|  ¡ r|  ¡ ‘qS r    )Ústrip©r;  Úlabelr    r    r!   rG  C  s   þz'Babyl._generate_toc.<locals>.<listcomp>rÁ   ó   ,ó   ræ   )ró   ro   r|   r  rq   rÀ   rÅ   r¡   r&  r'  r(  r“   r\  rô   rø   )r   r)  r*  r.  Zlabel_listsr+  rƒ   rg  r    r    r!   r  5  s4    



ÿzBabyl._generate_tocc                 C   sV   dt  }|dt  7 }|  ¡ }dd„ |D ƒ}|dd |¡ t  7 }|d7 }| |¡ d S )Ns   BABYL OPTIONS:s
   Version: 5c                 s   s   | ]}|  ¡ V  qd S r   )rX   ri  r    r    r!   r<  X  r  z*Babyl._pre_mailbox_hook.<locals>.<genexpr>s   Labels:rk  rl  )rq   r]  r   rr   )r   r»   Zbabylrg  r    r    r!   r  S  s    zBabyl._pre_mailbox_hookc                 C   s   |  dt ¡ d S )Nó   r%  r  r    r    r!   r  ]  s    zBabyl._pre_message_hookc                 C   s   |  td ¡ d S )Nrl  r%  r  r    r    r!   r  a  s    zBabyl._post_message_hookc                 C   sx  | j  ¡ }t|tƒr°g }g }| ¡ D ]$}|| jv r>| |¡ q$| |¡ q$| j  d¡ |D ]}| j  d| ¡  ¡ qZ| j  d¡ |D ]}| j  d| ¡  d ¡ q„| j  t	¡ n| j  dt	 ¡ t|t
jjƒrt ¡ }t
j |dd¡}| |¡ | d¡ | ¡ }| j  | d	t	¡¡ |d	ks,|süq,qü| j  d
t	 ¡ t|tƒr¤t ¡ }	t
j |	dd¡}
|
 | ¡ ¡ |	 ¡ }| j  | d	t	¡¡ |d	ks¢|snqâqnn>| d¡ | ¡ }| j  | d	t	¡¡ |d	ksâ|s®qâq®| d¡}|söqf| j  | d	t	¡¡ qânTt|tttjfƒrt|tjƒrJt dtd¡ | ¡ }t|tƒr`|  |¡}| d¡d }|d dkrâ| j  |d |…  d	t	¡¡ | j  d
t	 ¡ | j  |d |…  d	t	¡¡ | j  ||d …  d	t	¡¡ n(| j  d
t	 t	 ¡ | j  | d	t	¡¡ nXt |dƒrVt |dƒr:t dtd¡ |j!}| ¡ }d}| ¡ }| "d¡rl|d d… d	 }n| "d¡rˆ|d d… d	 }| j  | d	t	¡¡ |d	ks¬|sF|rÚd}| j  d
t	 ¡ | |¡ nqÚqF| ¡ }|sìqf| "d¡r
|d d… t	 }n:| "d¡r(|d d… t	 }n| "d	¡rD|d d… t	 }| j  |¡ qÚnt#dt$|ƒ ƒ‚| j  ¡ }||fS )Nó   1s   , s   ,,ó    rk  s   1,,Fr   r\   r_  rþ   r]   r^   s   

ræ   re   r|   r`   ra   Trb   rc   rd   rh   )%ró   r  ri   r   r]  rf  rÅ   rr   rX   rq   r:   r'   r	   rj   rk   rl   rm   rn   ro   r|   rp   Úget_visibler_   rv   ru   rw   rx   ry   rz   r{   r[   r  rP   r`   rt   r~   r   )r   r'   r
  Zspecial_labelsrg  rj  Zorig_bufferZorig_generatorrƒ   Z
vis_bufferZvis_generatorr`   Z
body_startZoriginal_posZ
first_passr  r    r    r!   r  e  s¸    







ÿ
ÿ
zBabyl._install_message)NT)r…   r†   r‡   Ú	frozensetrf  r"   r(   r+   r0   r4   r<   r7   r]  r  r  r  r  r  r    r    r    r!   r   Ø  s   
	
r   c                   @   s&   e Zd Zddd„Zdd„ Zdd„ ZdS )	r	   Nc                 C   sÒ   t |tjjƒr4|  t |¡¡ t |tƒrÎ| | ¡ nšt |tƒrP|  t 	|¡¡ n~t |t
ƒrl|  t |¡¡ nbt |tjƒrŠ|  t |¡¡ nDt|dƒr¦|  t |¡¡ n(|d u r¾tjj | ¡ ntdt|ƒ ƒ‚d S )Nr_   rh   )ri   r:   r'   r	   Ú_become_messageÚcopyÚdeepcopyÚ_explain_torv   r;   ru   Zmessage_from_stringrj   ÚTextIOWrapperZmessage_from_filerP   Zmessage_from_binary_filer"   r~   r   r&   r    r    r!   r"   Ô  s    



zMessage.__init__c                 C   s4   t |dg ƒ}|jD ]}||vr|j| | j|< qd S )NÚ_type_specific_attributes)ÚgetattrÚ__dict__)r   r'   Ztype_specificr›   r    r    r!   rr  ç  s    
zMessage._become_messagec                 C   s   t |tƒrd S tdƒ‚d S )Nz Cannot convert to specified type)ri   r	   r~   r&   r    r    r!   ru  î  s    
zMessage._explain_to)N)r…   r†   r‡   r"   rr  ru  r    r    r    r!   r	   Ñ  s   
r	   c                   @   sv   e Zd Zg 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dd„ Zdd„ Zdd„ ZdS )r
   )Ú_subdirÚ_infoÚ_dateNc                 C   s&   d| _ d| _t ¡ | _t | |¡ d S )NrŒ   r˜   )rz  r{  rÙ   r|  r	   r"   r&   r    r    r!   r"   û  s    
zMaildirMessage.__init__c                 C   s   | j S r   )rz  r>   r    r    r!   r     s    zMaildirMessage.get_subdirc                 C   s(   |dks|dkr|| _ ntd| ƒ‚d S )NrŒ   r   z!subdir must be 'new' or 'cur': %s)rz  rZ   )r   r¬   r    r    r!   r¶     s    zMaildirMessage.set_subdirc                 C   s"   | j  d¡r| j dd … S dS d S )Nú2,ræ   r˜   )r{  r}   r>   r    r    r!   Ú	get_flags  s    zMaildirMessage.get_flagsc                 C   s   dd  t|ƒ¡ | _d S )Nr}  r˜   )r   r  r{  )r   Úflagsr    r    r!   Ú	set_flags  s    zMaildirMessage.set_flagsc                 C   s$   |   d t|  ¡ ƒt|ƒB ¡¡ d S ©Nr˜   ©r€  r   rH  r~  ©r   Úflagr    r    r!   Úadd_flag  s    zMaildirMessage.add_flagc                 C   s,   |   ¡ r(|  d t|   ¡ ƒt|ƒ ¡¡ d S r  )r~  r€  r   rH  rƒ  r    r    r!   Úremove_flag  s    zMaildirMessage.remove_flagc                 C   s   | j S r   )r|  r>   r    r    r!   r¤   !  s    zMaildirMessage.get_datec                 C   s4   zt |ƒ| _W n  ty.   td| ƒd ‚Y n0 d S )Nzcan't convert to float: %s)Úfloatr|  rZ   r~   )r   Údater    r    r!   r¸   %  s    zMaildirMessage.set_datec                 C   s   | j S r   )r{  r>   r    r    r!   rŸ   ,  s    zMaildirMessage.get_infoc                 C   s&   t |tƒr|| _ntdt|ƒ ƒ‚d S )Nzinfo must be a string: %s)ri   ru   r{  r~   r   )r   Úinfor    r    r!   r·   0  s    
zMaildirMessage.set_infoc                 C   s¤  t |tƒr8| |  ¡ ¡ | |  ¡ ¡ | |  ¡ ¡ nht |tƒrÄt	|  ¡ ƒ}d|v r`| 
d¡ |  ¡ dkrv| 
d¡ d|v rˆ| 
d¡ d|v rš| 
d¡ d|v r¬| 
d¡ | d	t |  ¡ ¡¡ nÜt |tƒrt	|  ¡ ƒ}d|vrî| d
¡ d|v r| d¡ d|v r | d¡ nˆt |tƒr‚t	|  ¡ ƒ}d|vrD| d
¡ d|v rX| d¡ d|v rl| d¡ d|v r | d¡ nt |tƒrntdt|ƒ ƒ‚d S )NÚSÚRr   ÚOÚTÚDÚFÚAúMAILER-DAEMONrW  ÚrepliedÚflaggedrX  rZ  ÚPrY  ú$Cannot convert to specified type: %s)ri   r
   r€  r~  r¶   r   r¸   r¤   r  rH  r…  r  rÙ   r"  r   r7  r   Ú	add_labelr	   r~   r   )r   r'   r  r    r    r!   ru  7  sP    

















ÿzMaildirMessage._explain_to)N)r…   r†   r‡   rw  r"   r   r¶   r~  r€  r…  r†  r¤   r¸   rŸ   r·   ru  r    r    r    r!   r
   ö  s   
r
   c                   @   sV   e Zd ZdgZ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S )r  Ú_fromNc                 C   sV   |   dd¡ t|tjjƒrF| ¡ }|d urF| d¡rF|   |dd … ¡ t | |¡ d S )Nr‘  TzFrom rg   )r  ri   r:   r'   r	   r   r}   r"   )r   r'   r  r    r    r!   r"   h  s    z_mboxMMDFMessage.__init__c                 C   s   | j S r   )r—  r>   r    r    r!   r  q  s    z_mboxMMDFMessage.get_fromc                 C   s4   |d ur*|du rt  ¡ }|dt  |¡ 7 }|| _d S )NTú )rÙ   r"  r!  r—  )r   r  Ztime_r    r    r!   r  u  s
    z_mboxMMDFMessage.set_fromc                 C   s   |   dd¡|   dd¡ S )NÚStatusr˜   úX-Status)r3   r>   r    r    r!   r~  }  s    z_mboxMMDFMessage.get_flagsc                 C   sÎ   t |ƒ}d\}}dD ]}||v r||7 }| |¡ qdD ]}||v r8||7 }| |¡ q8|d t|ƒ¡7 }z|  d|¡ W n ty˜   |  d|¡ Y n0 z|  d|¡ W n tyÈ   |  d|¡ Y n0 d S )N)r˜   r˜   )r‹  rŒ  )rŽ  r  r  r˜   r™  rš  )rH  r+   r   r  Úreplace_headerr-   Z
add_header)r   r  Zstatus_flagsZxstatus_flagsr„  r    r    r!   r€    s&    z_mboxMMDFMessage.set_flagsc                 C   s$   |   d t|  ¡ ƒt|ƒB ¡¡ d S r  r‚  rƒ  r    r    r!   r…  —  s    z_mboxMMDFMessage.add_flagc                 C   s4   d| v sd| v r0|   d t|  ¡ ƒt|ƒ ¡¡ d S )Nr™  rš  r˜   r‚  rƒ  r    r    r!   r†  ›  s    z_mboxMMDFMessage.remove_flagc              	   C   sÜ  t |tƒrÎt|  ¡ ƒ}d|v r(| d¡ d|v r:| d¡ d|v rL| d¡ d|v r^| d¡ d|v rp| d¡ |d	= |d
= d |  ¡  ¡ dd … ¡}z| 	t
 t |d¡¡¡ W n ttfyÈ   Y n0 n
t |tƒrö| |  ¡ ¡ | |  ¡ ¡ nât |tƒrXt|  ¡ ƒ}d|vr"| d¡ d|v r6| d¡ d|v rJ| d¡ |d	= |d
= n€t |tƒrºt|  ¡ ƒ}d|vr„| d¡ d|v r˜| d¡ d|v r¬| d¡ |d	= |d
= nt |tƒrÈntdt|ƒ ƒ‚d S )NrŒ  r   r  r  r‹  rŠ  rŽ  r  Ústatuszx-statusr˜  éûÿÿÿz%a %b %d %H:%M:%S %YrW  r’  r“  rX  rZ  r•  )ri   r
   rH  r~  r¶   r…  r   r  r¡   r¸   ÚcalendarZtimegmrÙ   ÚstrptimerZ   ÚOverflowErrorr  r€  r  r   r7  r   r–  r	   r~   r   )r   r'   r  Z
maybe_dater    r    r!   ru     sb    





ÿ













ÿz_mboxMMDFMessage._explain_to)N)N)r…   r†   r‡   rw  r"   r  r  r~  r€  r…  r†  ru  r    r    r    r!   r  c  s   
	
r  c                   @   s   e Zd ZdS )r   N©r…   r†   r‡   r    r    r    r!   r   Ô  s   r   c                   @   sD   e Zd ZdgZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )r   Ú
_sequencesNc                 C   s   g | _ t | |¡ d S r   )r¢  r	   r"   r&   r    r    r!   r"   Ý  s    zMHMessage.__init__c                 C   s   | j d d … S r   )r¢  r>   r    r    r!   r6  â  s    zMHMessage.get_sequencesc                 C   s   t |ƒ| _d S r   )r@   r¢  )r   rO  r    r    r!   rQ  æ  s    zMHMessage.set_sequencesc                 C   s6   t |tƒr"|| jvr2| j |¡ ntdt|ƒ ƒ‚d S )Nzsequence type must be str: %s)ri   ru   r¢  rÅ   r~   r   ©r   rV  r    r    r!   r7  ê  s    

zMHMessage.add_sequencec                 C   s(   z| j  |¡ W n ty"   Y n0 d S r   )r¢  r+   rZ   r£  r    r    r!   Úremove_sequenceò  s    zMHMessage.remove_sequencec                 C   sF  t |tƒrdt|  ¡ ƒ}d|v r*| d¡ n| d¡ | d¡ d|v rP| d¡ d|v rb| d¡ nÞt |tƒr¾t|  ¡ ƒ}d|vrŽ| d¡ n
| d	¡ d|v rª| d¡ d|v r¼| d
¡ n„t |tƒrâ|  ¡ D ]}| |¡ qÐn`t |t	ƒr$t|  ¡ ƒ}d|v r| 
d¡ d|v rB| 
d¡ nt |tƒr2ntdt|ƒ ƒ‚d S )NrW  r   rŠ  r“  r  r’  r‹  ÚROrŒ  r  rZ  r•  )ri   r
   rH  r6  r¶   r…  r  r   r7  r   r–  r	   r~   r   )r   r'   rO  rV  r    r    r!   ru  ù  sB    










ÿzMHMessage._explain_to)N)
r…   r†   r‡   rw  r"   r6  rQ  r7  r¤  ru  r    r    r    r!   r   Ø  s   
r   c                   @   s^   e Zd Zddg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dS )r   r\  Ú_visibleNc                 C   s   g | _ tƒ | _t | |¡ d S r   )r\  r	   r¦  r"   r&   r    r    r!   r"   %  s    zBabylMessage.__init__c                 C   s   | j d d … S r   )r\  r>   r    r    r!   r]  +  s    zBabylMessage.get_labelsc                 C   s   t |ƒ| _d S r   )r@   r\  )r   rg  r    r    r!   ra  /  s    zBabylMessage.set_labelsc                 C   s6   t |tƒr"|| jvr2| j |¡ ntdt|ƒ ƒ‚d S )Nzlabel must be a string: %s)ri   ru   r\  rÅ   r~   r   ©r   rj  r    r    r!   r–  3  s    

zBabylMessage.add_labelc                 C   s(   z| j  |¡ W n ty"   Y n0 d S r   )r\  r+   rZ   r§  r    r    r!   Úremove_label;  s    zBabylMessage.remove_labelc                 C   s
   t | jƒS r   ©r	   r¦  r>   r    r    r!   rp  B  s    zBabylMessage.get_visiblec                 C   s   t |ƒ| _d S r   r©  )r   Zvisibler    r    r!   r`  F  s    zBabylMessage.set_visiblec                 C   sb   | j  ¡ D ](}|| v r*| j  || | ¡ q
| j |= q
dD ]$}|| v r8|| j vr8| | | j |< q8d S )N)ZDateZFromzReply-ToZToZCCZSubject)r¦  rA   r›  )r   Úheaderr    r    r!   Úupdate_visibleJ  s    
zBabylMessage.update_visiblec                 C   sr  t |tƒr~t|  ¡ ƒ}d|v r*| d¡ n| d¡ | d¡ d|v sNd|v rX| d¡ d|v rj| d¡ d	|v r|| d
¡ nðt |tƒrØt|  ¡ ƒ}d|vr¨| d¡ n
| d¡ d	|v rÄ| d¡ d|v rÖ| d¡ n–t |tƒrt|  ¡ ƒ}d|v r| d¡ d|v rn| d¡ nTt |t	ƒrP| 
|  ¡ ¡ |  ¡ D ]}| |¡ q<nt |tƒr^ntdt|ƒ ƒ‚d S )NrW  r   rŠ  rY  r[  r”  rZ  r‹  rX  r  r¥  rŒ  rŽ  r  r’  r•  )ri   r
   rH  r]  r¶   r…  r  r   r7  r   r`  rp  r–  r	   r~   r   )r   r'   rg  rj  r    r    r!   ru  U  sH    










ÿzBabylMessage._explain_to)N)r…   r†   r‡   rw  r"   r]  ra  r–  r¨  rp  r`  r«  ru  r    r    r    r!   r      s   
r   c                   @   s   e Zd ZdS )r   Nr¡  r    r    r    r!   r     s   r   c                   @   s¬   e Zd Zd%dd„Zd&dd„Zd'dd„Zd(dd	„Zd)d
d„Z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d#d$„ ƒZeeƒZdS )+r¾   Nc                 C   s$   || _ |d u r| ¡ | _n|| _d S r   )ró   r  Ú_pos)r   r»   Úposr    r    r!   r"   †  s    z_ProxyFile.__init__c                 C   s   |   || jj¡S r   )Ú_readró   r_   ©r   Úsizer    r    r!   r_   Ž  s    z_ProxyFile.readc                 C   s   |   || jj¡S r   )r®  ró   Úread1r¯  r    r    r!   r±  ’  s    z_ProxyFile.read1c                 C   s   |   || jj¡S r   )r®  ró   r|   r¯  r    r    r!   r|   –  s    z_ProxyFile.readlinec                 C   s<   g }| D ].}|  |¡ |d ur|t|ƒ8 }|dkr q8q|S )Nr   )rÅ   rÀ   )r   ÚsizehintrM   rƒ   r    r    r!   Ú	readlinesš  s    
z_ProxyFile.readlinesc                 c   s   |   ¡ }|sd S |V  q d S r   )r|   )r   rƒ   r    r    r!   rF   ¥  s    z_ProxyFile.__iter__c                 C   s   | j S r   )r¬  r>   r    r    r!   r  ­  s    z_ProxyFile.tellr   c                 C   s4   |dkr| j  | j¡ | j  ||¡ | j  ¡ | _d S )NrÁ   ©ró   ro   r¬  r  ©r   ÚoffsetÚwhencer    r    r!   ro   ±  s    z_ProxyFile.seekc                 C   s4   t | dƒr0zt | jdƒr"| j ¡  W | `n| `0 d S )Nró   rW   )rP   ró   rW   r>   r    r    r!   rW   ¸  s
    
z_ProxyFile.closec                 C   s2   |d u rd}| j  | j¡ ||ƒ}| j  ¡ | _|S )Nre   r´  )r   r°  Úread_methodrM   r    r    r!   r®  Á  s    z_ProxyFile._readc                 C   s   | S r   r    r>   r    r    r!   Ú	__enter__Ê  s    z_ProxyFile.__enter__c                 G   s   |   ¡  d S r   )rW   )r   Úexcr    r    r!   Ú__exit__Î  s    z_ProxyFile.__exit__c                 C   s
   | j  ¡ S r   )ró   Úreadabler>   r    r    r!   r¼  Ñ  s    z_ProxyFile.readablec                 C   s
   | j  ¡ S r   )ró   Úwritabler>   r    r    r!   r½  Ô  s    z_ProxyFile.writablec                 C   s
   | j  ¡ S r   )ró   Úseekabler>   r    r    r!   r¾  ×  s    z_ProxyFile.seekablec                 C   s
   | j  ¡ S r   )ró   rT   r>   r    r    r!   rT   Ú  s    z_ProxyFile.flushc                 C   s&   t | dƒsdS t | jdƒsdS | jjS )Nró   Tr3  F)rP   ró   r3  r>   r    r    r!   r3  Ý  s
    
z_ProxyFile.closed)N)N)N)N)N)r   )r…   r†   r‡   r"   r_   r±  r|   r³  rF   r  ro   rW   r®  r¹  r»  r¼  r½  r¾  rT   Úpropertyr3  rˆ   r   r‰   r    r    r    r!   r¾   ƒ  s&   





		
r¾   c                   @   s8   e Zd Zddd„Zdd„ Zddd„Zd	d
„ Zdd„ ZdS )r  Nc                 C   s   t  | ||¡ || _|| _d S r   )r¾   r"   Ú_startÚ_stop)r   r»   r
  r  r    r    r!   r"   ë  s    z_PartialFile.__init__c                 C   s   t  | ¡| j S r   )r¾   r  rÀ  r>   r    r    r!   r  ñ  s    z_PartialFile.tellr   c                 C   s<   |dkr| j | _d}n|dkr*| j| _d}t | ||¡ d S )Nr   rÁ   ræ   )rÀ  r¬  rÁ  r¾   ro   rµ  r    r    r!   ro   õ  s    z_PartialFile.seekc                 C   sB   | j | j }|dkrdS |d u s0|dk s0||kr4|}t | ||¡S )Nr   r  )rÁ  r¬  r¾   r®  )r   r°  r¸  Ú	remainingr    r    r!   r®  ÿ  s    z_PartialFile._readc                 C   s   t | dƒr| `d S )Nró   )rP   ró   r>   r    r    r!   rW     s    
z_PartialFile.close)NN)r   )r…   r†   r‡   r"   r  ro   r®  rW   r    r    r    r!   r  è  s
   


	r  Tc              
   C   sª  d}zft rrzt  | t jt jB ¡ W nL typ } z4|jtjtjtjfv rZt	d| j
 ƒ‚n‚ W Y d }~n
d }~0 0 |rjzt| j
d ƒ}| ¡  W nH tyÚ } z0|jtjtjfv rÄW Y d }~W d S ‚ W Y d }~n
d }~0 0 z^zt |j
| j
d ¡ d}W n0 ttfy*   t |j
| j
d ¡ d}Y n0 t |j
¡ W n. tyh   t |j
¡ t	d| j
 ƒ‚Y n0 W n8   t rˆt  | t j¡ |ržt | j
d ¡ ‚ Y n0 d S )NFzlockf: lock unavailable: %sú.lockTzdot lock unavailable: %s)ÚfcntlÚlockfZLOCK_EXZLOCK_NBr©   rª   ÚEAGAINrñ   rò   r   r›   r  rW   r   r¥   r¦   r§   r¨   rR  rä   r+   ÚLOCK_UN)r»   rÿ   Zdotlock_doner°   Zpre_lockr    r    r!   rû     sL    ÿ
ÿrû   c                 C   s8   t rt  | t j¡ tj | jd ¡r4t | jd ¡ d S )NrÃ  )rÄ  rÅ  rÇ  r   r   r‘   r›   r+   ©r»   r    r    r!   rý   9  s    rý   c              	   C   sD   t  | t jt jB t jB d¡}zt| dƒW t  |¡ S t  |¡ 0 d S )NrÎ   rï   )r   rµ   rÏ   r0  ÚO_RDWRrW   )r   Úfdr    r    r!   rã   @  s    

þrã   c                 C   s$   t d| tt ¡ ƒt ¡ t ¡ f ƒS )Nz%s.%s.%s.%s)rã   rß   rÙ   rÝ   rÞ   r   rà   )r   r    r    r!   r  H  s    þr  c                 C   s$   |   ¡  ttdƒr t |  ¡ ¡ d S )NÚfsync)rT   rP   r   rË  ÚfilenorÈ  r    r    r!   r   N  s    
r   c                 C   s   t | ƒ |  ¡  d S r   )r   rW   rÈ  r    r    r!   rœ   T  s    rœ   c                   @   s   e Zd ZdS )r   Nr¡  r    r    r    r!   r   Z  s   r   c                   @   s   e Zd ZdS )r   Nr¡  r    r    r    r!   r   ]  s   r   c                   @   s   e Zd ZdS )r   Nr¡  r    r    r    r!   r   `  s   r   c                   @   s   e Zd ZdS )r   Nr¡  r    r    r    r!   r   c  s   r   c                   @   s   e Zd ZdS )r   Nr¡  r    r    r    r!   r   f  s   r   )T)1r   rÙ   rž  rÝ   rª   rs  rx   r:   Zemail.messageZemail.generatorrj   r5   Útypesr   rÄ  ÚImportErrorÚ__all__rq   rX   r   r   rî   r  r   r   r   r   r'   r	   r
   r  r   r   r   r   r¾   r  rû   rý   rã   r  r   rœ   Ú	Exceptionr   r   r   r   r   r    r    r    r!   Ú<module>	   sj   
 j  8 DB4-  3 z%mqH_e'
*