ó
Gh\c           @   s”   d  d l  Z d  d l Z d  d l m Z m Z d  d l m Z d  d l m Z d  d l	 Z	 d  d l
 m Z d d l m Z d e f d	 „  ƒ  YZ d S(
   iÿÿÿÿN(   t   fsynct   unlink(   t   exc_info(   t   Lock(   t   OfflineImapErrori   (   t
   IMAPFoldert   MappedIMAPFolderc           B   sï   e  Z d  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d d d „ Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s,  IMAP class to map between Folder() instances where both side assign a uid

    This Folder is used on the local side, while the remote side should
    be an IMAPFolder.

    Instance variables (self.):
      dryrun: boolean.
      r2l: dict mapping message uids: self.r2l[remoteuid]=localuid
      l2r: dict mapping message uids: self.r2l[localuid]=remoteuid
      #TODO: what is the difference, how are they used?
      diskr2l: dict mapping message uids: self.r2l[remoteuid]=localuid
      diskl2r: dict mapping message uids: self.r2l[localuid]=remoteuidc         C   sŒ   t  j |  | | | d t ƒ|  j j d d t ƒ |  _ t ƒ  |  _ |  j	 ƒ  \ |  _
 |  _ d \ |  _ |  _ t  | | | d t ƒ|  _ d  S(   Nt   decodet   generals   dry-run(   NN(   R   t   __init__t   Falset   configt   getdefaultbooleant   Truet   dryrunR   t   maplockt	   _loadmapst   diskr2lt   diskl2rt   Nonet   r2lt   l2rt   _mb(   t   selft
   imapservert   namet
   repositoryR   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyR	   +   s    c         C   s"   t  j j |  j j ƒ  |  j ƒ  ƒ S(   N(   t   ost   patht   joinR   t	   getmapdirt   getfolderbasename(   R   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyt   _getmapfilename6   s    c      
   C   s˜  |  j  ƒ  } d | } d | } |  j o5 t | d ƒ Z} y t j | t j ƒ Wn t k
 re n Xt j j	 | ƒ r• |  j
 j d ƒ t | ƒ n  t j j	 | ƒ s± i  i  f St | d ƒ } i  } i  } xµ t rƒ| j ƒ  } t | ƒ sñ Pn  y | j ƒ  } Wn; t k
 r>t j t t d | | f ƒ t ƒ  d ƒ n X| j d ƒ \ }	 }
 t |	 ƒ } t |
 ƒ } | | | <| | | <qÏ W| | f SWd  QXd  S(	   Ns   %s.tmps   %s.lockt   ws–   a previous run might have leave the UIDMaps file in incorrect state; some sync operations might be done again and some emails might become duplicated.t   rts*   Corrupt line '%s' in UID mapping file '%s'i   t   :(   R    R   t   opent   fnctlt   lockft   LOCK_EXt	   NameErrorR   R   t   existst   uit   warnR   R   t   readlinet   lent   stript
   ValueErrort   sixt   reraiset	   ExceptionR   t   splitt   int(   R   t   mapfilenamet   mapfilenametmpt   mapfilenamelockt   mapfilelockt   fileR   R   t   linet   str1t   str2t   loct   rem(    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyR   :   sD    


		
c         C   sû   |  j  t k r d  S|  j ƒ  } d | } d | } |  j oH t | d ƒ ª } y t j | t j ƒ Wn t k
 rx n Xt | d ƒ W } x4 |  j	 j
 ƒ  D]# \ } } | j d | | f ƒ q› W|  j ƒ  rÛ t | ƒ n  Wd  QXt j | | ƒ Wd  QXd  S(   Ns   %s.tmps   %s.lockR!   t   wts   %d:%d
(   R   R   R    R   R$   R%   R&   R'   R(   R   t   itemst   writet   dofsyncR    t   shutilt   move(   R   R5   R6   R7   R8   t	   mapfilefdt   keyt   value(    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyt	   _savemaps`   s     

c         C   su   y g  | D] } | | ^ q
 SWnO t  k
 rp } t j t t d j | j d |  ƒ t j j ƒ t ƒ  d ƒ n Xd  S(   Nsr   Could not find UID for msg '{0}' (f:'{1}'. This is usually a bad thing and should be reported on the mailing list.i    i   (	   t   KeyErrorR0   R1   R   t   formatt   argst   ERRORt   MESSAGER   (   R   t   mappingR@   t   xt   e(    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyt   _uidlistx   s    	c   	   
   C   sˆ  |  j  j d | d | ƒ |  j  j ƒ  } |  j  j |  _ |  j Bx³ |  j j ƒ  D]¢ } | | k rQ |  j | } d } y |  j | =Wn, t k
 r² } |  j	 j
 | j | ƒ ƒ n Xy |  j | =Wqó t k
 rï } |  j	 j
 | j | ƒ ƒ qó XqQ qQ W|  j ƒ  d } |  j j ƒ  |  _ |  j j ƒ  |  _ xP | j ƒ  D]B } | |  j k r8| } | d 8} | |  j | <| |  j | <q8q8WWd  QXd  S(   Nt   min_datet   min_uidsy   unexpected error: key {} was not found in memory, see https://github.com/OfflineIMAP/offlineimap/issues/445 to know more.iÿÿÿÿi   (   R   t   cachemessagelistt   getmessagelistt   messagelistR   R   t   keysR   RI   R*   R+   RJ   RH   t   copyR   R   (	   R   RR   RS   t   reallistt   luidt   ruidt   errorMessageRP   t   nextneg(    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyRT   †   s4    
$

c         C   s   |  j  j ƒ  d  S(   N(   R   t   dropmessagelistcache(   R   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyR^   °   s    c         C   s   | |  j  k S(   s0   Checks if the (remote) UID exists in this Folder(   R   (   R   R[   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyt	   uidexists´   s    c         C   s   |  j  j ƒ  S(   sl   Gets a list of (remote) UIDs.

        You may have to call cachemessagelist() before calling this function!(   R   RW   (   R   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyt   getmessageuidlist¼   s    c         C   s   t  |  j ƒ S(   sz   Gets the number of messages in this folder.

        You may have to call cachemessagelist() before calling this function!(   R-   R   (   R   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyt   getmessagecountÆ   s    c      
   C   s£   i  } |  j  j ƒ  } |  j  xs t | j ƒ  ƒ D]_ \ } } y |  j | } Wn t k
 re q2 n X| j ƒ  } |  j | d | d <| | | <q2 W| SWd QXd S(   sº   Gets the current message list.

        This function's implementation is quite expensive for the mapped UID
        case.  You must call cachemessagelist() before calling this function!t   uidN(   R   RU   R   t   listR@   R   RI   RX   (   R   t   retvalt	   localhashRF   RG   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyRU   Ð   s    
c         C   s   |  j  j |  j | ƒ S(   s-   Returns the content of the specified message.(   R   t
   getmessageR   (   R   Rb   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyRf   è   s    c         C   sü   |  j  j d | | |  ƒ | d k  r) | S| |  j k rL |  j | | ƒ | S|  j j d | | | ƒ } | d k  rª t d |  j j ƒ  | | |  j ƒ  f t j j ƒ ‚ n  |  j	 C | |  j
 | <| |  j | <| |  j | <| |  j | <|  j ƒ  Wd QX| S(   s   Writes a new message, with the specified uid.

        The UIDMaps class will not return a newly assigned uid, as it
        internally maps different uids between IMAP servers. So a
        successful savemessage() invocation will return the same uid it
        has been invoked with. As it maps between 2 IMAP servers which
        means the source message must already have an uid, it requires a
        positive uid to be passed in. Passing in a message with a
        negative uid will do nothing and return the negative uid.

        If the uid is > 0, the backend should set the uid to this, if it can.
        If it cannot set the uid to that, it will save it anyway.
        It will return the uid assigned in any case.

        See folder/Base for details. Note that savemessage() does not
        check against dryrun settings, so you need to ensure that
        savemessage is never called in a dryrun mode.
        t   imapi    iÿÿÿÿi   sV   server of repository '%s' did not return a valid UID (got '%s') for UID '%s' from '%s'N(   R*   t   savemessageR   t   savemessageflagsR   R   t   getnameRL   RM   R   R   R   R   RH   (   R   Rb   t   contentt   flagst   rtimet   newluid(    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyRh   í   s$    
c         C   s   |  j  j |  j | ƒ S(   N(   R   t   getmessageflagsR   (   R   Rb   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyRo     s    c         C   s   d  S(   N(   R   (   R   Rb   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyt   getmessagetime!  s    c         C   s   |  j  j |  j | | ƒ d S(   s   Note that this function does not check against dryrun settings,
        so you need to ensure that it is never called in a
        dryrun mode.N(   R   Ri   R   (   R   Rb   Rl   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyRi   %  s    c         C   s   |  j  j |  j | | ƒ d  S(   N(   R   t   addmessageflagsR   (   R   Rb   Rl   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyRq   -  s    c         C   s&   |  j  j |  j |  j | ƒ | ƒ d  S(   N(   R   t   addmessagesflagsRQ   R   (   R   t   uidlistRl   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyRr   1  s    c         C   sÛ   | |  j  k r+ t d | t j j ƒ ‚ n  | | k r; d S|  j ‘ |  j  | } | |  j | <|  j  | =| |  j  | <| d k r’ | |  j | <n  | d k r« |  j | =n  | d k rÇ | |  j | <n  |  j ƒ  Wd QXd S(   sã   Change the message from existing ruid to new_ruid

        :param new_uid: The old remote UID will be changed to a new
            UID. The UIDMaps case handles this efficiently by simply
            changing the mappings file.s$   Cannot change unknown Maildir UID %sNi    (	   R   R   RL   RM   R   R   R   R   RH   (   R   R[   t   new_ruidRZ   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyt   change_message_uid6  s"     

   c         C   sˆ   |  j  y d } xX | D]P } |  j | } |  j | =|  j | =| d k r |  j | =|  j | =d } q q W| r~ |  j ƒ  n  Wd  QXd  S(   Ni    i   (   R   R   R   R   R   RH   (   R   Rs   t	   needssaveR[   RZ   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyt   _mapped_deleteM  s    




c         C   s   |  j  j |  j | | ƒ d  S(   N(   R   t   deletemessageflagsR   (   R   Rb   Rl   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyRx   \  s    c         C   s&   |  j  j |  j |  j | ƒ | ƒ d  S(   N(   R   t   deletemessagesflagsRQ   R   (   R   Rs   Rl   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyRy   `  s    c         C   s+   |  j  j |  j | ƒ |  j | g ƒ d  S(   N(   R   t   deletemessageR   Rw   (   R   Rb   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyRz   e  s    c         C   s0   |  j  j |  j |  j | ƒ ƒ |  j | ƒ d  S(   N(   R   t   deletemessagesRQ   R   Rw   (   R   Rs   (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyR{   j  s    N(   t   __name__t
   __module__t   __doc__R   R	   R    R   RH   RQ   R   RT   R^   R_   R`   Ra   RU   Rf   Rh   Ro   Rp   Ri   Rq   Rr   Ru   Rw   Rx   Ry   Rz   R{   (    (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyR      s2   		&		*			
	
			0										(   t   os.pathR   RC   R    R   t   sysR   t	   threadingR   R0   t   offlineimapR   t   IMAPR   R   (    (    (    s4   /usr/share/offlineimap/offlineimap/folder/UIDMaps.pyt   <module>   s   