a �DOg�3�@s>dZddlZddlZddlZddlZddlZddlZddlZddlZddl Zddl Zddl Z ddl Z ddl mZz ddlZWney�dZYn0gd�Zej�d�ZGdd�d�ZGdd �d e�ZGd d �d e�ZGd d �d e�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�dejj�ZGdd�de�ZGdd�de�ZGdd�de�Z Gdd�de�Z!Gd d!�d!e�Z"Gd"d#�d#e�Z#Gd$d%�d%�Z$Gd&d'�d'e$�Z%d?d)d*�Z&d+d,�Z'd-d.�Z(d/d0�Z)d1d2�Z*d3d4�Z+Gd5d6�d6e,�Z-Gd7d8�d8e-�Z.Gd9d:�d:e-�Z/Gd;d<�d�d>e-�Z1dS)@zDRead/write support for Maildir, mbox, MH, Babyl, and MMDF mailboxes.�N)� GenericAlias)�Mailbox�Maildir�mbox�MH�Babyl�MMDF�Message�MaildirMessage� mboxMessage� MHMessage� BabylMessage� MMDFMessage�Error�NoSuchMailboxError� NotEmptyError�ExternalClashError� FormatError�asciic@seZdZdZdCdd�Zdd�Zdd �Zd d �Zd d �Zdd�Z dDdd�Z dd�Z dd�Z dd�Z dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�ZdEd0d1�Zd2d3�ZdFd4d5�Zd6d7�Zd8d9�Zd:d;�Zdd?�Z!d@Z"dGdAdB�Z#e$e%�Z&dS)Hrz*A group of messages in a particular place.NTcCs tj�tj�|��|_||_dS)zInitialize a Mailbox instance.N)�os�path�abspath� expanduser�_path�_factory��selfr�factory�create�r�/usr/lib64/python3.9/mailbox.py�__init__%szMailbox.__init__cCs td��dS)�$Add message and return assigned key.�&Method must be implemented by subclassN��NotImplementedError�r�messagerrr �add*sz Mailbox.addcCs td��dS)�=Remove the keyed message; raise KeyError if it doesn't exist.r#Nr$�r�keyrrr �remove.szMailbox.removecCs|�|�dS�N)r,r*rrr � __delitem__2szMailbox.__delitem__cCs&z|�|�Wnty Yn0dS�z'If the keyed message exists, remove it.N)r,�KeyErrorr*rrr �discard5s zMailbox.discardcCs td��dS)�>Replace the keyed message; raise KeyError if it doesn't exist.r#Nr$�rr+r'rrr � __setitem__<szMailbox.__setitem__cCs(z |�|�WSty"|YS0dS)z9Return the keyed message, or default if it doesn't exist.N)� __getitem__r0)rr+�defaultrrr �get@s  z Mailbox.getcCsN|js|�|�St�|�|���}|�|�Wd�S1s@0YdS)z=Return the keyed message; raise KeyError if it doesn't exist.N)r� get_message� contextlib�closing�get_file)rr+�filerrr r5Gs zMailbox.__getitem__cCs td��dS)�4Return a Message representation or raise a KeyError.r#Nr$r*rrr r8OszMailbox.get_messagecCst�|�|����S)z�Return a string representation or raise a KeyError. Uses email.message.Message to create a 7bit clean string representation of the message.��email�message_from_bytes� get_bytesZ as_stringr*rrr � get_stringSszMailbox.get_stringcCs td��dS)z8Return a byte string representation or raise a KeyError.r#Nr$r*rrr rAZszMailbox.get_bytescCs td��dS)�6Return a file-like representation or raise a KeyError.r#Nr$r*rrr r;^szMailbox.get_filecCs td��dS)�Return an iterator over keys.r#Nr$�rrrr �iterkeysbszMailbox.iterkeyscCs t|���S)zReturn a list of keys.)�listrFrErrr �keysfsz Mailbox.keysc cs<|��D].}z ||}Wnty.YqYn0|VqdS)z%Return an iterator over all messages.N�rFr0�rr+�valuerrr � itervaluesjs     zMailbox.itervaluescCs|��Sr-)rLrErrr �__iter__sszMailbox.__iter__cCs t|���S)z,Return a list of messages. Memory intensive.)rGrLrErrr �valuesvszMailbox.valuesc cs@|��D]2}z ||}Wnty.YqYn0||fVqdS)z.Return an iterator over (key, message) tuples.NrIrJrrr � iteritemszs     zMailbox.iteritemscCs t|���S)z9Return a list of (key, message) tuples. Memory intensive.)rGrOrErrr �items�sz Mailbox.itemscCs td��dS)�9Return True if the keyed message exists, False otherwise.r#Nr$r*rrr � __contains__�szMailbox.__contains__cCs td��dS)�*Return a count of messages in the mailbox.r#Nr$rErrr �__len__�szMailbox.__len__cCs|��D]}|�|�qdS)zDelete all messages.N)rHr1r*rrr �clear�s z Mailbox.clearcCs2z ||}Wnty"|YS0|�|�|S)z3Delete the keyed message and return it, or default.)r0r1)rr+r6�resultrrr �pop�s     z Mailbox.popcCs*|��D]}||�|�fStd��dS)z6Delete an arbitrary (key, message) pair and return it.zNo messages in mailboxN)rFrWr0r*rrr �popitem�s zMailbox.popitemc Csrt|d�r|��}nt|d�r(|��}n|}d}|D],\}}z |||<Wq4ty^d}Yq40q4|rntd��dS)z4Change the messages that correspond to certain keys.rOrPFTzNo message with key(s)N)�hasattrrOrPr0)r�arg�sourceZbad_keyr+r'rrr �update�s        zMailbox.updatecCs td��dS)�&Write any pending changes to the disk.r#Nr$rErrr �flush�sz Mailbox.flushcCs td��dS)�Lock the mailbox.r#Nr$rErrr �lock�sz Mailbox.lockcCs td��dS)�#Unlock the mailbox if it is locked.r#Nr$rErrr �unlock�szMailbox.unlockcCs td��dS)�Flush and close the mailbox.r#Nr$rErrr �close�sz Mailbox.closecCs,z |�d�WSty&td��Yn0dS)Nrz?String input must be ASCII-only; use bytes or a Message instead)�encode� UnicodeError� ValueErrorr&rrr �_string_to_bytes�s  zMailbox._string_to_bytesFc Cs�t|tjj�rvt��}tj�||d�}|�|�|� d�|� �}|� dt �}|� |�|jrr|�t �sr|� t ��n�t|tttjf��rt|tj�r�t�dtd�|��}t|t�r�|�|�}|r�|� dd�}|� dt �}|� |�|j�r�|�t ��s�|� t �n�t|d��r�t|d��r2t�d td�|j}d }|��}|�d ��r\|d d �d}n|�d ��rx|d d�d}|�s��q�|�r�|�d��r�d|dd �}|� dt �}|� |�|}�q6|j�r�|�r�|�t ��s�|� t �ntdt|���d S)z%Dump message contents to target file.r� �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 insteadN� ������ ������From s>From ��Invalid message type: %s)� isinstancer?r'r �io�BytesIO� generator�BytesGenerator�flatten�seekrl�replace�linesep�write�_append_newline�endswith�str�bytes�StringIO�warnings�warn�DeprecationWarning�getvaluerhrYrm�readline� startswith� TypeError�type) rr'�targetZ mangle_from_rm�gen�dataZlastline�linerrr � _dump_message�s\     �        �     zMailbox._dump_message)NT)N)N)N)F)'�__name__� __module__� __qualname__�__doc__r!r(r,r.r1r4r7r5r8rBrAr;rFrHrLrMrNrOrPrRrTrUrWrXr\r^r`rbrdrhr�r�� classmethodr�__class_getitem__rrrr r"sD        6rc@s�eZdZdZdZd6dd�Zdd�Zd d �Zd d �Zd d�Z dd�Z dd�Z dd�Z dd�Z dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-Zd.d/�Zd0d1�Zd2d3�Zd4d5�ZdS)7rzA qmail-style Maildir mailbox.�:NTcCs�t�||||�tj�|jd�tj�|jd�tj�|jd�d�|_tj�|j�s�|r�t�|jd�|j� �D]}t�|d�qln t |j��i|_ ddd�|_ d|_ d|_d S) zInitialize a Maildir instance.�tmp�new�cur)r�r�r���r)r�r�g�������?N)rr!rr�joinr�_paths�exists�mkdirrNr�_toc� _toc_mtimes� _last_read� _skewfactor)r�dirnamerrrrrr r!s�  zMaildir.__init__c Cs||��}z|�||�Wn(ty@|��t�|j��Yn0t|�t|t �rz|� �}|j |� �}||j kr�d}nd}d}tj �|j��|j �d}tj �|j|||�}t|t �r�t�|jtj �|j�|��f�zJzt�|j|�Wn&ttf�yt�|j|�Yn0t�|j�WnPt�yv}z6t�|j�|jtjk�r`td|��n�WYd}~n d}~00|S)r"�r�rz$Name clash with existing message: %sN)� _create_tmpr�� BaseExceptionrdrr,�name� _sync_closervr � get_subdir�colon�get_infor�basename�splitr�r�utime�getatime�get_date�link�AttributeError�PermissionError�rename�OSError�errno�EEXISTr)rr'Ztmp_file�subdir�suffix�uniq�dest�errr r($sF     � �z Maildir.addcCs t�tj�|j|�|���dS�r)N)rr,rr�r�_lookupr*rrr r,NszMaildir.removec Cs*z|�|�Wnttfy$Yn0dSr/)r,r0�FileNotFoundErrorr*rrr r1RszMaildir.discardc Cs�|�|�}|�|�}|�|�}t|t�r.|}n|}tj�|�}|j|vr`|j|�|j�d}nd}|� |�tj� |j |�} tj� |j |||�} t|t�r�t� | tj� | �|��f�t�| | �dS)r2rrr�N)r�r(rvr rrr�r�r�r1r�rr�r�r�r�) rr+r'Z old_subpathZtemp_keyZ temp_subpathZdominant_subpathr�r�Ztmp_path�new_pathrrr r4Zs$        �zMaildir.__setitem__cCs�|�|�}ttj�|j|�d��*}|jr4|�|�}nt|�}Wd�n1sP0Ytj�|�\}}|� |�|j |vr�|� |�|j �d�|� tj� tj�|j|���|S)r=�rbNrr)r��openrrr�rrr r�� set_subdirr��set_info�set_date�getmtime)rr+�subpath�f�msgr�r�rrr r8us  &  zMaildir.get_messagecCsPttj�|j|�|��d�� }|���td�Wd�S1sB0YdS)�2Return a bytes representation or raise a KeyError.r�riN) r�rrr�rr�rlr}r~�rr+r�rrr rA�szMaildir.get_bytescCs$ttj�|j|�|��d�}t|�S)rCr�)r�rrr�rr�� _ProxyFiler�rrr r;�szMaildir.get_filec csD|��|jD]0}z|�|�Wnty6YqYn0|VqdS�rDN)�_refreshr�r�r0r*rrr rF�s   zMaildir.iterkeyscCs|��||jvS�rQ)r�r�r*rrr rR�szMaildir.__contains__cCs|��t|j�S�rS)r��lenr�rErrr rT�szMaildir.__len__cCsdS)�"Write any pending changes to disk.NrrErrr r^�sz Maildir.flushcCsdS)r_NrrErrr r`�sz Maildir.lockcCsdS)raNrrErrr rb�szMaildir.unlockcCsdS�rcNrrErrr rd�sz Maildir.closecCs\g}t�|j�D]F}t|�dkr|ddkrtj�tj�|j|��r|�|dd��q|S)�Return a list of folder names.�r�.N)r�listdirrr�r�isdirr��append�rrV�entryrrr � list_folders�s�zMaildir.list_folderscCs ttj�|jd|�|jdd�S)z/Return a Maildir instance for the named folder.r�F�rr)rrrr�rr�r�folderrrr � get_folder�s�zMaildir.get_foldercCs\tj�|jd|�}t||jd�}tj�|d�}tj�|�sXt�t�|tj tj Bd��|S)z>Create a folder and return a Maildir instance representing it.r��rZ maildirfolder�) rrr�rrrr�rdr��O_CREAT�O_WRONLY)rr�rrVZmaildirfolder_pathrrr � add_folder�s �zMaildir.add_foldercCstj�|jd|�}t�tj�|d��t�tj�|d��D](}t|�dksX|ddkr)rr+�from_rrr rBs  ��z_mboxMMDF.get_stringcCsJ|�|�\}}|j�|�|s(|j��|j�||j���}|�td�S)r2ri)r�r r|r�rlrr}r~)rr+r4rr r1rrr rAs   z_mboxMMDF.get_bytescCs<|�|�\}}|j�|�|s(|j��t|j|j��|�S)rC)r�r r|r�� _PartialFiler)rr+r4rr rrr r;!s   z_mboxMMDF.get_filecCs d}t|t�r|�|�}t|t�rf|�d�rf|�d�}|dkr\|d|�}||dd�}q�|}d}nJt|t�r�|���d�}d|}n(t|t j j �r�|� �}|dur�|�d�}|dur�dt �t �����}|j��}|j�|t�|�||j|j�|j��}||fS) z1Format a message and blindly write to self._file.Nrsrirrr�r,rsFrom MAILER-DAEMON )rvr�rhr�r��find�_mboxMMDFMessage�get_fromrer?r'r � get_unixfromr��asctime�gmtimer rrr~r�� _mangle_from_)rr'r0�newlineZauthorrr rrr r()s0         z_mboxMMDF._install_messageN)F)F)F) r�r�r�r�r<r8rBrAr;r(rrrr r+s  r+c@s2eZdZdZdZdZd dd�Zdd�Zdd �ZdS) rzA classic mbox mailbox.TNcCst|_t�||||�dS)zInitialize an mbox mailbox.N)r r-r+r!rrrr r!Osz mbox.__init__cCs|�t�dSr%�rr~r#rrr rTszmbox._post_message_hookcCs�gg}}d}|j�d�|j��}|j��}|�d�rzt|�t|�krj|r`|�|tt��n |�|�|�|�d}q|s�|r�|�|tt��q�|�|�q�q|tkr�d}qd}qtt t ||���|_ t|j �|_ |j��|_ dS)�0Generate key-to-(start, stop) table of contents.FrrsTN)r r|rr�r�r�r�r~�dict� enumerate�zipr�r r)r�starts�stopsZlast_was_empty�line_posr�rrr r'Xs.         zmbox._generate_toc)NT) r�r�r�r�r<r�r!rr'rrrr rFs  rc@s2eZdZdZd dd�Zdd�Zdd �Zd d �ZdS) rzAn MMDF mailbox.NTcCst|_t�||||�dS)zInitialize an MMDF mailbox.N)rr-r+r!rrrr r!}sz MMDF.__init__cCs|�dt�dS)r$�Nr>r#rrr r�szMMDF._pre_message_hookcCs|�tdt�dS)r&rFNr>r#rrr r�szMMDF._post_message_hookcCs�gg}}|j�d�d}|}|j��}|j��}|�dt�r�|�|�|}|j��}|j��}|dtkr�|�|tt��q�qJ|sJ|�|�q�qJq|sq�qtt t ||���|_ t|j �|_ |j�dd�|j��|_ dS)r?rrFr�N)r r|r�rr�r~r�r�r@rArBr�r r)rrCrD�next_posrEr�rrr r'�s.          zMMDF._generate_toc)NT)r�r�r�r�r!rrr'rrrr rzs  rc@s�eZdZdZd0dd�Zdd�Zdd �Zd d �Zd d �Zdd�Z dd�Z dd�Z dd�Z dd�Z dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�ZdS)1rzAn MH mailbox.NTcCstt�||||�tj�|j�sj|r`t�|jd�t�t�tj� |jd�tj tj Btj Bd��n t |j��d|_dS)zInitialize an MH instance.r�� .mh_sequencesi�FN)rr!rrr�rr�rdr�r�r��O_EXCLr�rr rrrr r!�s� z MH.__init__cCs�|��}t|�dkrd}n t|�d}tj�|jt|��}t|�}d}z�|j rVt |�zrz|� ||�Wn8t y�|j r�t |�t|�d}t�|��Yn0t|t�r�|�||�W|j r�t |�n|j r�t |�0W|s�t|�n|s�t|�0|S)r"rr�FT)rHr��maxrrr�rr�r�r rr�r�rr�r,rvr �_dump_sequences)rr'rHZnew_keyr�r��closedrrr r(�s>      �  � zMH.addc Csztj�|jt|��}zt|d�}Wn@tyb}z(|jtjkrLt d|��n�WYd}~nd}~00|� �t� |�dS)r)rrN) rrr�rr�r�r�r�rr0rdr,)rr+rr�r�rrr r,�s z MH.removec Cs�tj�|jt|��}zt|d�}Wn@tyb}z(|jtjkrLt d|��n�WYd}~n d}~00zz|j rtt |�zNt� t�|tj tjB��|�||�t|t�r�|�||�W|j r�t|�n|j r�t|�0Wt|�n t|�0dS)r2rrN)rrr�rr�r�r�r�rr0r rrdr��O_TRUNCr�rvr rKrr�)rr+r'rr�r�rrr r4�s(    � zMH.__setitem__c Cs z@|jr$ttj�|jt|��d�}nttj�|jt|��d�}Wn@ty�}z(|jtj krjt d|��n�WYd}~n d}~00|�J|jr�t |�zt |�}W|jr�t |�n|jr�t |�0Wd�n1s�0Y|����D]\}}||vr�|�|�q�|S)r=rr�rN)r r�rrr�rr�r�r�rr0rr r� get_sequencesrP� add_sequence)rr+r�r�r�r��key_listrrr r8�s*   �( zMH.get_messagec Cs�z@|jr$ttj�|jt|��d�}nttj�|jt|��d�}Wn@ty�}z(|jtj krjt d|��n�WYd}~n d}~00|�`|jr�t |�z0|� �� td�W|jr�t|�Wd�Sn|jr�t|�0Wd�n1s�0YdS)r�rr�rNri)r r�rrr�rr�r�r�rr0rrlr}r~r�rr+r�r�rrr rAs( ��z MH.get_bytesc Cshzttj�|jt|��d�}Wn@ty^}z(|jtjkrHt d|��n�WYd}~n d}~00t |�S)rCr�rN) r�rrr�rr�r�r�rr0r�rQrrr r;,s z MH.get_filecCsttdd�t�|j�D���S)rDcss|]}|��rt|�VqdSr-)�isdigitr�)�.0r�rrr � 9s�zMH.iterkeys..)�iterrrr�rrErrr rF7sz MH.iterkeyscCstj�tj�|jt|���Sr�)rrr�r�rr�r*rrr rR<szMH.__contains__cCstt|����Sr�)r�rGrFrErrr rT@sz MH.__len__cCs2|js.ttj�|jd�d�|_t|j�d|_dS)r_rHrTN)r r�rrr�rr rrErrr r`Ds zMH.lockcCs(|jr$t|j�t|j�|`d|_dSr)r rr r�rErrr rbKs   z MH.unlockcCsdS)r]NrrErrr r^SszMH.flushcCs|jr|��dSr�)r rbrErrr rdWszMH.closecCs<g}t�|j�D]&}tj�tj�|j|��r|�|�q|S)r�)rr�rrr�r�r�r�rrr r�\s  zMH.list_folderscCsttj�|j|�|jdd�S)z+Return an MH instance for the named folder.Fr��rrrr�rrr�rrr r�ds�z MH.get_foldercCsttj�|j|�|jd�S)z:Create a folder and return an MH instance representing it.r�rVr�rrr r�is�z MH.add_foldercCs`tj�|j|�}t�|�}|dgkr:t�tj�|d��n|gkrDntd|j��t�|�dS)r�rHzFolder not empty: %sN)rrr�rr�r,rr�)rr�r�entriesrrr r�ns  zMH.remove_folderc si}ttj�|jd�ddd���}t|����|D]�}z�|�d�\}}t�}|��D]H}|��rn|� t |��qRdd�|�d�D�\}} |� t || d ��qR�fd d �t |�D�||<t||�d kr�||=Wq0ty�td |����Yq00q0Wd�n1�s 0Y|S)z=Return a name-to-key-list dictionary to define each sequence.rH�r�ASCII��encodingr�css|]}t|�VqdSr-)r�)rS�xrrr rT�r,z#MH.get_sequences..�-r�csg|]}|�vr|�qSrr)rSr+�Zall_keysrr � �s�z$MH.get_sequences..rz"Invalid sequence specification: %sN)r�rrr�r�setrHr�rRr(r�r\�rangerr�rgr�rstrip) r�resultsr�r�r��contentsrH�specrr rr^r rNzs(    �.zMH.get_sequencescCs ttj�|jd�ddd�}z�t�t�|jtjtjB��|� �D]�\}}t |�dkrVq@|� |d�d}d}t t |��D]R}|d |kr�|s�d }|� d �n*|r�d}|� d ||f�n|� d |�|}qx|r�|� t|�d�q@|� d�q@Wt|�n t|�0dS)z:Set sequences using the given name-to-key-list dictionary.rHzr+rYrZrr�NFr�Tr]z%s %sz %s� )r�rrr�rrdr�r�rMrPr�rrr`r�r�)r� sequencesr�r�rH�prevZ completingr+rrr � set_sequences�s.   zMH.set_sequencesc Cs<|��}d}g}|��D]�}|d|kr�|�||df�z4t�tj�|jt|��tj�|jt|d���WnFt t fy�t� tj�|jt|��tj�|jt|d���Yn0t� tj�|jt|���|d7}q|d|_ t|�dkr�dS|��D]0\}}|D]"\}}||v�r|||�|�<�qq�|�|�dS)z?Re-name messages to eliminate numbering gaps. Invalidates keys.rr�N)rNrFr�rr�rr�rr�r�r�r��unlinkr r�rP�indexri) rrgrhZchangesr+r�rP�oldr�rrr �pack�s0  ��      zMH.packcCst|��}|��}|��D]0\}}||vr4|�|�q||vr||�|�=q|D]}||vrN|g||<qN|�|�dS)z;Inspect a new MHMessage and update sequences appropriately.N)rNrPr�rkri)rr'r+Zpending_sequencesZ all_sequencesr�rP�sequencerrr rK�s  zMH._dump_sequences)NT)r�r�r�r�r!r(r,r4r8rAr;rFrRrTr`rbr^rdr�r�r�r�rNrirmrKrrrr r�s. "  rc@s�eZdZdZehd��Zddd�Zdd�Zd d �Zd d �Z d d�Z dd�Z dd�Z dd�Z dd�Zdd�Zdd�Zdd�Zdd�ZdS) rzAn Rmail-style Babyl mailbox.>Zedited�resentZfiled�deleted�answered�unseen� forwardedNTcCst�||||�i|_dS)zInitialize a Babyl mailbox.N)rr!�_labelsrrrr r!�szBabyl.__init__cCs(t�||�}t|t�r$|��|j|<|S)r")rr(rvr � get_labelsrt)rr'r+rrr r(�s  z Babyl.addcCs"t�||�||jvr|j|=dSr�)rr,rtr*rrr r,�s  z Babyl.removecCs*t�|||�t|t�r&|��|j|<dS)r2N)rr4rvr rurtr3rrr r4�s zBabyl.__setitem__c Cs�|�|�\}}|j�|�|j��t��}|j��}|dtks\|sHq\|�|�td��q,t��}|j��}|tks�|s|q�|�|�td��qd||j� �}|j� |�}|�td�}t |� �|�} | � |� ��||jvr�| �|j|�| S)r=� *** EOOH ***ri)r�r r|r�rwrxr~rr}rrlr r�� set_visiblert� set_labels) rr+rr �original_headersr�Zvisible_headers�n�bodyr�rrr r8�s*        zBabyl.get_messagec Cs�|�|�\}}|j�|�|j��t��}|j��}|dtks\|sHq\|�|�td��q,|j��}|tksv|s\qvq\|� �}||j� �}|j� |�}|�td�}||S)r2rvri) r�r r|r�rwrxr~rr}r�rrl) rr+rr ryr�Zheadersrzr�rrr rAs        zBabyl.get_bytescCst�|�|��dt��S)rCri)rwrxrAr}r~r*rrr r;(szBabyl.get_filecCs<|��t�}|j��D]}|�|�q|�|j�t|�S)z4Return a list of user-defined labels in the mailbox.)r�r`rtrNr\�difference_update�_special_labelsrG)r�labelsZ label_listrrr ru,s   zBabyl.get_labelscCs:gg}}|j�d�d}g}|}|j��}|j��}|dtkr�t|�t|�krd|�|tt��|�|�dd�|j��dd��d�D�}|�|�q|dks�|dtkr�t|�t|�kr�|�|tt��q|s|�|tt��q�qtt t ||���|_ tt |��|_ t|j �|_ |j�dd �|j��|_dS) r?rs cSsg|]}|��r|���qSr)�strip�rS�labelrrr r_Cs�z'Babyl._generate_toc..r�N�,�r�)r r|r�rr~r�r�r�r@rArBr�rtr r)rrCrDrGZ label_listsrEr�r~rrr r'5s4      �  zBabyl._generate_toccCsVdt}|dt7}|��}dd�|D�}|dd�|�t7}|d7}|�|�dS) r"sBABYL OPTIONS:s Version: 5css|]}|��VqdSr-)rer�rrr rTXr,z*Babyl._pre_mailbox_hook..sLabels:r�r�N)r~rur�r)rr�Zbabylr~rrr rSs zBabyl._pre_mailbox_hookcCs|�dt�dS)r$� Nr>r#rrr r]szBabyl._pre_message_hookcCs|�td�dS)r&r�Nr>r#rrr raszBabyl._post_message_hookcCsx|j��}t|t�r�g}g}|��D]$}||jvr>|�|�q$|�|�q$|j�d�|D]}|j�d|���qZ|j�d�|D]}|j�d|��d�q�|j�t �n|j�dt �t|t j j ��rt ��}t j�|dd�}|�|�|�d�|��}|j�|�d t ��|d k�s,|s��q,q�|j�d t �t|t��r�t ��} t j�| dd�} | �|���| ��}|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|ttt jf��rt|t j��rJt�d td �|��}t|t��r`|�|�}|�d�d} | ddk�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�dtd �|j!}|��} d}|��}|�"d��rl|dd�d }n|�"d��r�|dd�d }|j�|�d t ��|d k�s�|�sF|�r�d}|j�d t �|�| �n�qڐqF|��}|�s�qf|�"d��r |dd�t }n:|�"d��r(|dd�t }n|�"d ��rD|dd�t }|j�|��q�nt#dt$|���|j��}||fS)z0Write message contents and return (start, stop).�1s, s,,� r�s1,,Frrirvrrjrks r�rrNr�rmrnTrorprqru)%r rrvr rur}r�rrer~r?r'r rwrxryrzr{r|r�r}� get_visiblerlr�r�r�r�r�r�r�rhr6rYrmr�r�r�)rr'rZspecial_labelsr~r�Z orig_bufferZorig_generatorr�Z vis_bufferZ vis_generatorrmZ body_startZ original_posZ first_passr rrr r(es�              �    �       zBabyl._install_message)NT)r�r�r�r�� frozensetr}r!r(r,r4r8rAr;rur'rrrr(rrrr r�s    rc@s*eZdZdZd dd�Zdd�Zdd�ZdS) r z0Message with mailbox-format-specific properties.NcCs�t|tjj�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(|dur�tjj�|�ntdt|���dS)zInitialize a Message instance.rlNru)rvr?r'r �_become_message�copy�deepcopy� _explain_tor�r@r�Zmessage_from_stringrw� TextIOWrapperZmessage_from_filerYZmessage_from_binary_filer!r�r�r&rrr r!�s      zMessage.__init__cCs4t|dg�}|jD]}||vr|j||j|<qdS)z0Assume the non-format-specific state of message.�_type_specific_attributesN)�getattr�__dict__)rr'Z type_specificr�rrr r��s  zMessage._become_messagecCst|t�rdStd��dS)z:Copy format-specific state to message insofar as possible.Nz Cannot convert to specified type)rvr r�r&rrr r��s zMessage._explain_to)N)r�r�r�r�r!r�r�rrrr r �s r c@szeZdZdZgd�Zddd�Zdd�Zdd �Zd d �Zd d �Z dd�Z dd�Z dd�Z dd�Z dd�Zdd�Zdd�ZdS)r z)Message with Maildir-specific properties.)�_subdir�_info�_dateNcCs&d|_d|_t��|_t�||�dS)z%Initialize a MaildirMessage instance.r�r�N)r�r�r�r�r r!r&rrr r!�s zMaildirMessage.__init__cCs|jS)zReturn 'new' or 'cur'.)r�rErrr r�szMaildirMessage.get_subdircCs(|dks|dkr||_n td|��dS)zSet subdir to 'new' or 'cur'.r�r�z!subdir must be 'new' or 'cur': %sN)r�rg)rr�rrr r�szMaildirMessage.set_subdircCs"|j�d�r|jdd�SdSdS)�*Return as a string the flags that are set.�2,r�Nr�)r�r�rErrr � get_flags s zMaildirMessage.get_flagscCsdd�t|��|_dS)�)Set the given flags and unset all others.r�r�N)r�rr�)r�flagsrrr � set_flagsszMaildirMessage.set_flagscCs$|�d�t|���t|�B��dS�z.Set the given flag(s) without changing others.r�N�r�r�r`r��r�flagrrr �add_flagszMaildirMessage.add_flagcCs,|��r(|�d�t|���t|���dS)�7Unset the given string flag(s) without changing others.r�N)r�r�r�r`r�rrr � remove_flagszMaildirMessage.remove_flagcCs|jS)zSet "From " line, formatting and appending time_ if specified.NT� )r�r;r:r�)rr4Ztime_rrr r.us z_mboxMMDFMessage.set_fromcCs|�dd�|�dd�S)r��Statusr��X-Status)r7rErrr r�}sz_mboxMMDFMessage.get_flagscCs�t|�}d\}}dD]}||vr||7}|�|�qdD]}||vr8||7}|�|�q8|d�t|��7}z|�d|�Wnty�|�d|�Yn0z|�d|�Wnty�|�d|�Yn0dS)r�)r�r�)r�r�)r�r�r�r�r�r�N)r`r,r�r�replace_headerr0Z add_header)rr�Z status_flagsZ xstatus_flagsr�rrr r��s&    z_mboxMMDFMessage.set_flagscCs$|�d�t|���t|�B��dSr�r�r�rrr r��sz_mboxMMDFMessage.add_flagcCs4d|vsd|vr0|�d�t|���t|���dS)r�r�r�r�Nr�r�rrr r��sz_mboxMMDFMessage.remove_flagc Cs�t|t�r�t|���}d|vr(|�d�d|vr:|�d�d|vrL|�d�d|vr^|�d�d|vrp|�d�|d =|d =d �|����d d ��}z|� t � t � |d���Wnttfy�Yn0�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 =nt|t��r�ntdt|���d S)zACopy mbox- or MMDF-specific state to message insofar as possible.r�r�r�r�r�r�r�r��statuszx-statusr������Nz%a %b %d %H:%M:%S %Yrrr�r�rprqr�)rvr r`r�r�r�r�r8r�r��calendarZtimegmr��strptimerg� OverflowErrorr7r�r.r rOr r�r r�r�)rr'r�Z maybe_daterrr r��sb       �                    �z_mboxMMDFMessage._explain_to)N)N) r�r�r�r�r�r!r8r.r�r�r�r�r�rrrr r7cs  r7c@seZdZdZdS)r z&Message with mbox-specific properties.N�r�r�r�r�rrrr r �sr c@sHeZdZdZdgZddd�Zdd�Zdd �Zd d �Zd d �Z dd�Z dS)r z$Message with MH-specific properties.� _sequencesNcCsg|_t�||�dS)z!Initialize an MHMessage instance.N)r�r r!r&rrr r!�szMHMessage.__init__cCs|jdd�S)z4Return a list of sequences that include the message.N)r�rErrr rN�szMHMessage.get_sequencescCst|�|_dS)z3Set the list of sequences that include the message.N)rGr�)rrgrrr ri�szMHMessage.set_sequencescCs6t|t�r"||jvr2|j�|�ntdt|���dS)z8Add sequence to list of sequences including the message.zsequence type must be str: %sN)rvr�r�r�r�r��rrnrrr rO�s  zMHMessage.add_sequencecCs(z|j�|�Wnty"Yn0dS)zARemove sequence from the list of sequences including the message.N)r�r,rgr�rrr �remove_sequence�s zMHMessage.remove_sequencecCsFt|t�rdt|���}d|vr*|�d�n|�d�|�d�d|vrP|�d�d|vrb|�d�n�t|t�r�t|���}d|vr�|�d�n |�d �d|vr�|�d�d|vr�|�d �n�t|t�r�|��D]}|�|�q�n`t|t ��r$t|���}d|v�r|� d�d|v�rB|� d �nt|t ��r2nt d t |���d S)z6Copy MH-specific state to message insofar as possible.rrr�r�r�r�r�r��ROr�r�rqr�N)rvr r`rNr�r�r7r rOr r�r r�r�)rr'rgrnrrr r��sB                      �zMHMessage._explain_to)N) r�r�r�r�r�r!rNrirOr�r�rrrr r �s r c@sbeZdZdZddgZddd�Zdd�Zd d �Zd d �Zd d�Z dd�Z dd�Z dd�Z dd�Z dS)r z'Message with Babyl-specific properties.rt�_visibleNcCsg|_t�|_t�||�dS)z#Initialize a BabylMessage instance.N)rtr r�r!r&rrr r!%szBabylMessage.__init__cCs|jdd�S)z'Return a list of labels on the message.N)rtrErrr ru+szBabylMessage.get_labelscCst|�|_dS)z&Set the list of labels on the message.N)rGrt)rr~rrr rx/szBabylMessage.set_labelscCs6t|t�r"||jvr2|j�|�ntdt|���dS)z+Add label to list of labels on the message.zlabel must be a string: %sN)rvr�rtr�r�r��rr�rrr r�3s  zBabylMessage.add_labelcCs(z|j�|�Wnty"Yn0dS)z4Remove label from the list of labels on the message.N)rtr,rgr�rrr � remove_label;s zBabylMessage.remove_labelcCs t|j�S)z3Return a Message representation of visible headers.�r r�rErrr r�BszBabylMessage.get_visiblecCst|�|_dS)z2Set the Message representation of visible headers.Nr�)rZvisiblerrr rwFszBabylMessage.set_visiblecCsb|j��D](}||vr*|j�|||�q |j|=q dD]$}||vr8||jvr8|||j|<q8dS)z9Update and/or sensibly generate a set of visible headers.)ZDateZFromzReply-ToZToZCCZSubjectN)r�rHr�)r�headerrrr �update_visibleJs zBabylMessage.update_visiblecCsrt|t�r~t|���}d|vr*|�d�n|�d�|�d�d|vsNd|vrX|�d�d|vrj|�d�d |vr||�d �n�t|t�r�t|���}d|vr�|�d �n |�d �d |vr�|�d �d|vr�|�d�n�t|t��rt|���}d|v�r|�d�d|v�rn|�d�nTt|t ��rP|� |� ��|��D]}|� |��qThe specified mailbox is not empty and deletion was requested.Nr�rrrr r`src@seZdZdZdS)rz)Another process caused an action to fail.Nr�rrrr rcsrc@seZdZdZdS)rz)A file appears to have an invalid format.Nr�rrrr rfsr)T)2r�rr�r�r�r�r�r�r?Z email.messageZemail.generatorrwr9�typesrr�� ImportError�__all__r~rerrrr+rrrrr'r r r7r r r rr�r5rrr�rrr�� Exceptionrrrrrrrrr �sl     j8DB4-3z%mqH_e' *