
    e7                     Z    d dl Z d dlmZ d dlZd dlmZ d dlmZ ddlmZ  G d de      Z	y)	    N)exc_info)OfflineImapError)imaputil   )MaildirFolderc                   f     e Zd ZdZ fdZd Zd ZddZd Zd Z	 fdZ
dd	Zd fd
	Zd Z xZS )GmailMaildirFolderzHFolder implementation to support adding labels to messages in a Maildir.c                 B   t         t        |   ||||       | j                  j                  j                  dd      | _        | j                  j                  j                  dd      | _        | j                  r&| j                  j                  | j                         y y )Nlabelsheaderz
X-Keywords
synclabelsr   )superr	   __init__
repositoryaccountgetconfr   getconfbooleanr   syncmessagesto_passesappendsyncmessagesto_labels)selfrootnamesepr   	__class__s        :/usr/share/offlineimap3/offlineimap/folder/GmailMaildir.pyr   zGmailMaildirFolder.__init__   s     $0tS*M !OO33;;N<HJ //11@@qQ ??&&--d.H.HI     c                    | j                   du rt        d      | j                          t        | j	                               t        |j	                               k7  ryt        | j                         j                               D ]  \  }}|d   |j                  |      k7  s y t        | j                         j                               D ]  \  }}|d   |j                  |      kD  s y y)zOReturns True if the Maildir has changed.

        Checks uids, flags and mtimesTzMGmailMaildir does not support quick mode when 'utime_from_header' is enabled.flagsmtimeF)
_utime_from_header	Exceptioncachemessagelistsortedgetmessageuidlistlistgetmessagelistitemsgetmessageflagsgetmessagemtime)r   statusfolderuidmessages       r   quickchangedzGmailMaildirFolder.quickchanged,   s    
 ""d* D E E 	$((*+|55789"4#6#6#8#>#>#@A 	NS'w<#?#?#DD	 #4#6#6#8#>#>#@A 	NS'w,">">s"CC	 r   c                 2    t               t               ddddS )NFz/no-dir/no-such-file/r   )r   labelslabels_cachedfilenamer   )setr   r+   s     r   msglist_item_initializerz+GmailMaildirFolder.msglist_item_initializerE   s    #%%3aA 	Ar   c                 |   | j                         r| j                  ||      | _        | j                  rt	        | j                  j                               D ]a  \  }}t        j                  j                  | j                         |d         }t        t        j                  |      j                        |d<   c y y )N)min_datemin_uidr1   r   )ismessagelistempty_scanfoldermessagelistr   r%   r'   ospathjoingetfullnameintstatst_mtime)r   r6   r7   r+   msgfilepaths         r   r"   z#GmailMaildirFolder.cachemessagelistI   s    ""$#//8?  0  AD ?? !1!1!7!7!9: ?S77<<(8(8(:C
OL"2778#4#=#=>G? r   c                    | j                   |   d   s/| j                   |   d   }t        j                  j                  | j	                         |      }t        j                  j                  |      s
t               S t        |d      }| j                  d   j                  |      }|j                          t               | j                   |   d<   | j                  || j                        D ]A  }| j                   |   d   j                  t        j                  | j                  |             C d| j                   |   d<   | j                   |   d   S )Nr0   r1   rb8bitr/   T)r:   r;   r<   r=   r>   existsr2   openparserparseclosegetmessageheaderlistr   updater   labels_from_header)r   r+   r1   rC   fdrB   hstrs          r   getmessagelabelsz#GmailMaildirFolder.getmessagelabelsT   s)   $_5'',Z8Hww||D$4$4$6AH77>>(+uh%B++f%++B/CHHJ.1eDS!(+11#t7H7HI J  %h/66//0A0A4HJJ 6:DS!/2$X..r   c                 J    d| j                   |   vry| j                   |   d   S )Nr   r   )r:   r3   s     r   r)   z"GmailMaildirFolder.getmessagemtimei   s.    $**3//##C(11r   c                 @   | j                   st        t        |   ||||      S t	               }| j                  || j                        D ]1  }|j                  t        j                  | j                  |             3 t        t        |   ||||      }| j                  |   d   }t        j                  j                  | j                         |      }	t        t        j                   |	      j"                        | j                  |   d<   || j                  |   d<   |S )zWrites a new message, with the specified uid.

        See folder/Base for detail. Note that savemessage() does not
        check against dryrun settings, so you need to ensure that
        savemessage is never called in a dryrun mode.r1   r   r/   )r   r   r	   savemessager2   rL   r   rM   r   rN   r:   r;   r<   r=   r>   r?   r@   rA   )r   r+   rB   r   rtimer/   rP   retr1   rC   r   s             r   rT   zGmailMaildirFolder.savemessageo   s    +T>sC?DeM M --c43D3DE 	PDMM(55d6G6GNO	P &9#sE:?A ##C(477<< 0 0 2H=),RWWX->-G-G)Hg&*0h'
r   c           	      r   |
t               }| j                  |   d   }t        j                  j	                  | j                         |      }t        |d      }| j                  d   j                  |      }|j                          t               }| j                  || j                        D ]1  }	|j                  t        j                  | j                  |	             3 ||z
  }||z  }
||z
  }||k(  ryt        j                  | j                  t!        ||
z              }| j#                  || j                         | j%                  || j                  |       t'        t        j(                  |      j*                        }| j-                  |t                     }| j/                  ||      }t        j                  j	                  | j                         |      }	 t        j0                  ||       | j>                  r|rt        j@                  |||f       t'        t        j(                  |      j*                        | j                  |   d	<   || j                  |   d
<   y# t2        $ rH}t5        d|d|d|j6                  t4        j8                  j:                  t=               d         d}~ww xY w)zChange a message's labels to `labels`.

        Note that this function does not check against dryrun settings,
        so you need to ensure that it is never called in a dryrun mode.Nr1   rE   rF   zCan't rename file 'z' to 'z':    r   r/   )!r2   r:   r;   r<   r=   r>   rH   rI   rJ   rK   rL   r   rM   r   rN   format_labels_stringr#   deletemessageheadersaddmessageheaderr?   r@   rA   new_message_filenamesave_to_tmp_filerenameOSErrorr   errnoERRORFOLDERr   r    utime)r   r+   r/   ignorelabelsr1   rC   rO   rB   	oldlabelsrP   ignoredlabels
labels_strr   messagenametmpnametmppathes                    r   savemessagelabelsz$GmailMaildirFolder.savemessagelabels   s`    5L##C(477<< 0 0 2H=(D!kk&!''+

E	--c43D3DE 	@DX889J9J9=? @	@ ,&!L0,	 Y 2243D3D39&=:P3QS
 	!!#t'8'89c4#4#4jABGGH%../ //SU;''S9'',,t//17;	2IIgx( ""uHHXu~. *-RWWX->-G-G)Hg&*0h'  	2"$+Xqww$@#3#9#9#@#@#+:a=2 2	2s   I% %	J6.AJ11J6c                    |dkD  xr |j                  |       }t        t        |   ||||       |rC| j                  r6	 |j                  |      }|j                  ||| j                  |             yyy# t        $ r Y yw xY w)a  Copies a message from self to dst if needed, updating the status

        Note that this function does not check against dryrun settings,
        so you need to ensure that it is never called in a
        dryrun mode.

        :param uid: uid of the message to be copied.
        :param dstfolder: A BaseFolder-derived instance
        :param statusfolder: A LocalStatusFolder instance
        :param register: whether we should register a new thread."
        :returns: Nothing on success, or raises an Exception.r   )r   N)		uidexistsr   r	   copymessagetor   rQ   rl   r)   NotImplementedError)r   r+   	dstfolderr*   registerrealcopyr/   r   s          r   ro   z GmailMaildirFolder.copymessageto   s     7;9#6#6s#;; 	 $5c96BH	N "33C8..sF595I5I#5N / P (8 ' s    4A7 7	BBc                 n   i }i }g }	 | j                         D ]  }t        j                  j                  j                  j                         r nf|dk  s|j                  |      sN| j                  |      }|j                  |      r|j                  |      }nd}||kD  s|j                  |        | j                  j                  ||        |D ]  }t        j                  j                  j                  j                         r n| j                  |      }	|j                  |      r|j                  |      }
n
t               }
|	|
z
  }|
|	z
  }|D ]  }||vrg ||<   ||   j                  |       ! |D ]  }||vrg ||<   ||   j                  |       !  t        |j                               D ]  \  }}t        j                  j                  j                  j                         r nx| j                  j                  |||       | j                   j"                  j$                  rx|j'                  |t        |g             |j'                  |t        |g              t        |j                               D ]  \  }}t        j                  j                  j                  j                         r nx| j                  j)                  |||       | j                   j"                  j$                  rx|j+                  |t        |g             |j+                  |t        |g              i }|D ]  }t        j                  j                  j                  j                         r n| j                   j"                  j$                  rX| j,                  |   d   }t.        j0                  j3                  | j5                         |      }t7        t/        j8                  |      j:                        ||<    |j=                  |       y# t>        $ r | j                  jA                  d       Y yw xY w)a  Pass 4: Label Synchronization (Gmail only)

        Compare label mismatches in self with those in statusfolder. If
        msg has a valid UID and exists on dstfolder (has not e.g. been
        deleted there), sync the labels change to both dstfolder and
        statusfolder.

        Also skips messages whose mtime remains the same as statusfolder, as the
        contents have not changed.

        This function checks and protects us from action in ryrun mode.
        r   r1   zKCan't sync labels. You need to configure a remote repository of type Gmail.N)!r$   offlineimapaccountsAccountabort_NOW_signalis_setrn   r)   r   uicollectingdatarQ   r2   r%   r'   addinglabelsr   r   dryrunaddmessageslabelsdeletinglabelsdeletemessageslabelsr:   r;   r<   r=   r>   r?   r@   rA   savemessagesmtimebulkrp   warn)r   rq   r*   addlabellistdellabellistuidlistr+   	selfmtimestatusmtime
selflabelsstatuslabels	addlabels	dellabelslbuidsmtimesr1   rC   s                     r   r   z(GmailMaildirFolder.syncmessagesto_labels   s     \	6--/ (''//@@GGI 7)"5"5c": 005	))#.".">">s"CK"#K{*NN3'%(( GG""7D1 1''//@@GGI!2237
))#.#/#@#@#EL#&5L&5	(:5	# 1B-+-R( $++C01
 $ 1B-+-R( $++C01)12 !!3!3!56 	@D''//@@GGI$$T2y9??**11++D#rd)<..tS"Y?	@ !!3!3!56 	CD''//@@GGI&&tR;??**11..tS"Y?11$RD	B	C F 
>''//@@GGI??**11++C0<77<<(8(8(:HE!"''("3"<"<=s
> ..v6" 	6GGLL 5 6	6s   BP "M*P $P43P4)NN)N)r   )__name__
__module____qualname____doc__r   r-   r4   r"   rQ   r)   rT   rl   ro   r   __classcell__)r   s   @r   r	   r	      s=    RJ2A	?/*24;1z Dp6r   r	   )
r;   sysr   offlineimap.accountsru   r   r   Maildirr   r	    r   r   <module>r      s'   & 
   (   "}6 }6r   