
    e                         d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlZ G d dej2                  j4                        Z G d d      Zy)a  
Base folder support
Copyright (C) 2002-2016 John Goerzen & contributors

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
    N)exc_infopolicy)BytesParser)BytesGenerator)parsedate_tz	mktime_tz)Charset)
threadutil)getglobalui)OfflineImapErrorc                         e Zd Zd fd	Z xZS )EmailMessagec                     t        |d      r;|9t        |t              st        |      }|j                  |j                  d      }d }t
        |   ||       y )Nencodereplace)hasattr
isinstancer
   r   output_charsetsuperset_payload)selfpayloadcharset	__class__s      2/usr/share/offlineimap3/offlineimap/folder/Base.pyr   zEmailMessage.set_payload(   sP    7H%'*=gw/!'*nnW%;%;YGGGGW-    N)__name__
__module____qualname__r   __classcell__)r   s   @r   r   r   '   s    . .r   r   c                      e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
ed	        Ze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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- Z0d. Z1d/ Z2d0 Z3d1 Z4d2 Z5d3 Z6d4 Z7d5 Z8d6 Z9dNd7Z:d8 Z;d9 Z<d: Z=d; Z>d< Z?d= Z@d> ZAd? ZBdOd@ZCdA ZDdB ZEdC ZFdPdDZGdE ZHdF ZIdG ZJdH ZKdI ZLdJ ZMdK ZNdL ZOdM ZPy)Q
BaseFolderz
    Base Folder Class
    Nc           	      .   t               | _        i | _        t        j                  j                  t              }|j                  ddd      |j                  dddd      |j                  dd	d      |j                  dd	dd      d
| _        i | _        | j                  D ](  }t        | j                  |         | j                  |<   * || _	        d| _
        || j                         k(  s|nd| _        d| _        | j                  dk(  r|j                  | _        d| _        d| _        || _        |j#                  |      | _        | j$                  | j                         k(  rd| _        d|j                  z   | _        |j)                         | _        | j*                  j-                  ddd      }| j*                  j-                  | j&                  d|      | _        | j*                  j-                  | j&                  dd	      | _        | j*                  j-                  ddd	      | _        | j*                  j-                  | j&                  dd      | _        |j7                  | j                        | _        | j4                  r.| j                  j;                  dd| j                  d|d       n9| j8                  s-| j                  j;                  dd| j                  d|d       | j<                  | j>                  | j@                  g| _!        y)z
        :param name: Path & name of folder minus root or reference
        :param repository: Repository() in which the folder is.
        )message_factory7bitFnone)cte_typeutf8refold_sourcez
)r)   r*   r+   linesep8bitT)r'   z7bit-RFCr-   z8bit-RFCr   N INBOXRepository generalfilename_use_mail_timestampsync_deletesfsyncdynamic_folderfilterz%Running dynamic folder filtering on 'z'[]zFiltering out 'z] due to folderfilter)"r   uimessagelistr   defaultcloner   parserr   ffilter_namerootgetsepnamenewmail_hookhave_newmailcopy_ignoreUIDs
repository	nametransvisiblenamerepoconfname	getconfigconfiggetdefaultboolean_filename_use_mail_timestamp_sync_deletes_dofsync_dynamic_folderfiltershould_sync_folder
_sync_thisdebug _BaseFolder__syncmessagesto_copy"_BaseFolder__syncmessagesto_delete!_BaseFolder__syncmessagesto_flagssyncmessagesto_passes)r   r?   rC   default_policykey"filename_use_mail_timestamp_globals         r   __init__zBaseFolder.__init__6   s    -  --l-K 

U&

Q


U&Z`

a


T

P


TY_

`	
 ;; 	DC*$++c2BCDKK	D !	 $ 5D2	 99 * 7 7D!#$%//5 t{{},!D)JOO; **, .2[[-J-J4e.=*,0KK,I,I).-0)
 "[[::~t555i$O &*[[%B%B5u&>"$778I8IJ%%GGMM",,j: ;GGMM",,j: ;
 &&((''&
"r   c                     | j                   S )zReturns namer?   r   s    r   getnamezBaseFolder.getname       yyr   c                     | j                   S r   rZ   r[   s    r   __str__zBaseFolder.__str__   r]   r   c                 8    | j                   j                  d      S )Nzutf-8)r?   decoder[   s    r   __unicode__zBaseFolder.__unicode__   s     yy((r   c                      y)zStarts a transaction. This will postpone (guaranteed) saving to disk
        of all messages saved inside this transaction until its committed.N r[   s    r   	__enter__zBaseFolder.__enter__        	r   c                      y)zmCommits a transaction, all messages saved inside this transaction
        will only now be persisted to disk.Nrd   )r   exc_typeexc_valexc_tbs       r   __exit__zBaseFolder.__exit__   rf   r   c                 .    | j                   j                  S )zAccount name as string)rC   accountnamer[   s    r   rm   zBaseFolder.accountname   s     ***r   c                 |    | j                   s| j                  S | j                  j                  | j                        S )z8Should this folder be synced or is it e.g. filtered out?)rM   rO   rC   rN   r<   r[   s    r   	sync_thiszBaseFolder.sync_this   s2     ))??"??55d6G6GHHr   c                     | j                   S )z\
        Call and returns _dofsync()

        Returns: Call and returns _dofsync()

        )rL   r[   s    r   dofsynczBaseFolder.dofsync   s     }}r   c                      y)zReturns True if this folder suggests using threads for actions.

        Only IMAP returns True. This method must honor any CLI or configuration
        option.Frd   r[   s    r   suggeststhreadszBaseFolder.suggeststhreads   s     r   c                     t         )zzImplements method that waits for thread to be usable.
        Should be implemented only for folders that suggest threads.NotImplementedErrorr[   s    r   waitforthreadzBaseFolder.waitforthread   
     "!r   c                      y)z Runs quick check for folder changes and returns changed
        status: True -- changed, False -- not changed.

        :param statusfolder: keeps track of the last known folder state.
        Trd   )r   statusfolders     r   quickchangedzBaseFolder.quickchanged   s     r   c                     t         )zXFor threading folders, returns the instancelimitname for
        InstanceLimitedThreads.ru   r[   s    r   getinstancelimitnamespacez$BaseFolder.getinstancelimitnamespace   s
     "!r   c                      y)zShould be true for any backend that actually saves message bodies.
        (Almost all of them).  False for the LocalStatus backend.  Saves
        us from having to slurp up messages just for localstatus purposes.   rd   r[   s    r   storesmessageszBaseFolder.storesmessages   s    
 r   c                     | j                   S )z3The nametrans-transposed name of the folder's name.)rE   r[   s    r   getvisiblenamezBaseFolder.getvisiblename   s     r   c                     | j                   | j                  k(  r| j                   S | j                  d| j                   dS )z7Name that shows both real and nametrans-mangled values.z [remote name r6   )r?   rE   r[   s    r   getexplainednamezBaseFolder.getexplainedname   s5     99(((99,0,<,<diiHHr   c                     | j                   S )z9Returns the repository object that this folder is within.)rC   r[   s    r   getrepositoryzBaseFolder.getrepository   s     r   c                     | j                   S )z=Returns the root of the folder, in a folder-specific fashion.)r=   r[   s    r   getrootzBaseFolder.getroot   s     yyr   c                     | j                   S )z+Returns the separator for this folder type.)sepr[   s    r   r>   zBaseFolder.getsep   s     xxr   c                     | j                         r2| j                         | j                         z   | j                         z   S | j                         S )z
        Returns the folder full name, using the getname(). If getroot() is set
        their value is concatenated to getname() using the separator

        Returns: The folder full name

        )r   r>   r\   r[   s    r   getfullnamezBaseFolder.getfullname   s;     <<><<>DKKM1DLLNBB<<>!r   c                     | j                   sd}n| j                   j                  dd      }t        j                  dd|      }|S )z:Return base file name of file to store Status/UID info in../z	(^|\/)\.$z\1dot)r?   r   resub)r   basenames     r   getfolderbasenamezBaseFolder.getfolderbasename   s?     yyHyy((c2H 66+x:r   c                     | j                         !| j                         | j                         k(  S | j                          y)aY  Tests if the cached UIDVALIDITY match the real current one

        If required it saves the UIDVALIDITY value. In this case the
        function is not threadsafe. So don't attempt to call it from
        concurrent threads.

        :returns: Boolean indicating the match. Returns True in case it
            implicitely saved the UIDVALIDITY.T)get_saveduidvalidityget_uidvaliditysave_uidvalidityr[   s    r   check_uidvalidityzBaseFolder.check_uidvalidity  s?     $$&2,,.$2F2F2HHH!!#r   c                     t         j                  j                  | j                  j	                         | j                               S )z@provides UIDVALIDITY cache filename for class internal purposes.)ospathjoinrC   	getuiddirr   r[   s    r   _getuidfilenamezBaseFolder._getuidfilename  s4     ww||DOO557 2246 	6r   c                 ^   t        | d      r| j                  S | j                         }t        j                  j                  |      sd| _        | j                  S t        |d      }t        |j                         j                               | _        |j                          | j                  S )zReturn the previously cached UIDVALIDITY value

        :returns: UIDVALIDITY as (long) number or None, if None had been
            saved yet._base_saved_uidvalidityNrt)r   r   r   r   r   existsopenintreadlinestripclose)r   uidfilenamefiles      r   r   zBaseFolder.get_saveduidvalidity"  s     423///**,ww~~k*+/D(
 +++ T*D+.t}}/D/D/F+GD(JJL+++r   c                 ~   | j                         }| j                         }t        |dz   d      5 }|j                  d|z         ddd       	 t	        j
                  |dz   |       || _        y# 1 sw Y   +xY w# t        $ r8 t	        j                  |       t	        j
                  |dz   |       Y || _        yw xY w)zSave the UIDVALIDITY value of the folder to the cache

        This function is not threadsafe, so don't attempt to call it
        from concurrent threads.z.tmpwtz%d
N)	r   r   r   writer   renameWindowsErrorremover   )r   newvalr   uidfiles       r   r   zBaseFolder.save_uidvalidity3  s     %%'**,+&- 	+MM&6/*	+
	9IIkF*K8
 (.$	+ 	+  	9IIk"IIkF*K8'-$		9s   A/A; /A8;7B<;B<c                     t         )zRetrieve the current connections UIDVALIDITY value

        This function needs to be implemented by each Backend
        :returns: UIDVALIDITY as a (long) number.ru   r[   s    r   r   zBaseFolder.get_uidvalidityI  s
     "!r   c                     t         )zCache the list of messages.

        Reads the message list from disk or network and stores it in memory for
        later use.  This list will not be re-read from disk or memory unless
        this function is called again.ru   r[   s    r   cachemessagelistzBaseFolder.cachemessagelistQ  
     "!r   c                 d    t        t        | j                  j                                     dk  ryy)zIs the list of messages empty.r   TF)lenlistr8   keysr[   s    r   ismessagelistemptyzBaseFolder.ismessagelistemptyZ  s+     tD$$))+,-1r   c                     i | _         y)z)Empty everythings we know about messages.Nr8   r[   s    r   dropmessagelistcachezBaseFolder.dropmessagelistcachea  s     r   c                     | j                   S )zfGets the current message list.

        You must call cachemessagelist() before calling this function!r   r[   s    r   getmessagelistzBaseFolder.getmessagelistf  s    
 r   c                     t         )a3  Returns value for empty messagelist element with given UID.

        This function must initialize all fields of messagelist item
        and must be called every time when one creates new messagelist
        entry to ensure that all fields that must be present are present.

        :param uid: Message UIDru   r   uids     r   msglist_item_initializerz#BaseFolder.msglist_item_initializerm  s
     "!r   c                 &    || j                         v S )zReturns True if uid exists.)r   r   s     r   	uidexistszBaseFolder.uidexistsx  s     d))+++r   c                 P    t        | j                         j                               S )zcGets a list of UIDs.

        You may have to call cachemessagelist() before calling this function!)sortedr   r   r[   s    r   getmessageuidlistzBaseFolder.getmessageuidlist}  s!    
 d))+00233r   c                 4    t        | j                               S )zGets the number of messages.)r   r   r[   s    r   getmessagecountzBaseFolder.getmessagecount  s     4&&())r   c                     t         )z Returns an email message object.ru   r   s     r   
getmessagezBaseFolder.getmessage  rx   r   c                    | j                   j                  d| j                  z  dd      }|y	 t        |      }|dk  r't	        d|z  t        j
                  j                        t        j                  t        j                         d|z  z
        S # t        $ r Y nw xY w	 t        j                  |d      }|d   d	k  r*t	        d
|d   z  t        j
                  j                        t        j                  |      t        j                  t        j                               z
  dkD  r't	        d|z  t        j
                  j                        |S # t        $ r( t	        d|z  t        j
                  j                        w xY w)zReturn maxage.

        maxage is allowed to be either an integer or a date of the form
        YYYY-mm-dd. This returns a time_struct.
Account %smaxageNr   zinvalid maxage value %diQ %Y-%m-%dr   l  z%maxage led to year %d. Abort syncing.z,maxage led to future date %s. Abort syncing.zinvalid maxage value %s)rH   
getdefaultrm   r   r   ERRORMESSAGEtimegmtime
ValueErrorstrptimemktime	localtime)r   	maxagestrr   dates       r   	getmaxagezBaseFolder.getmaxage  s    KK**<+/+;+;,<=EtM		^Fz&'@6'I'7'='='E'EG G;;tyy{\F-BBCC 			C==J7DAw~& (8:>q'(B'7'='='E'EG G D!DKK0@$AAQF& (8:C(D'7'='='E'EG G K 	C"#<y#H#3#9#9#A#AC C	Cs   A#B 	BB#B0E 1Fc                 V    | j                   j                  d| j                  z  dd      S )z
        Get the maxsize for account name. If not found, returns None.

        Returns: A string with the maxise of the account name

        r   maxsizeN)rH   getdefaultintrm   r[   s    r   
getmaxsizezBaseFolder.getmaxsize  s2     {{(()-)9)9*:;DdL 	Lr   c                 6   | j                   j                  d| j                  j                  z   dd      }	 |syt	        j
                  |d      }|d   dk  r*t        d|d   z  t        j                  j                        t	        j                  |      t	        j                  t	        j                               z
  dkD  r't        d|z  t        j                  j                        |S # t        $ r% t        d	t        j                  j                        w xY w)
z: Retrieve the value of the configuration option startdate r0   	startdateNr   r   r   z(startdate led to year %d. Abort syncing.z/startdate led to future date %s. Abort syncing.zinvalid startdate value %s)rH   r   rC   r?   r   r   r   r   r   r   r   r   )r   datestrr   s      r   getstartdatezBaseFolder.getstartdate  s   ++((9M9M)M)4d<	C==*5DAw~& (8:>q'(B'7'='='E'EG G D!DKK0@$AAQF& (8:A(B'7'='='E'EG G K 	C"#?#3#9#9#A#AC C	Cs   C* B0C* *.Dc                 h   t         j                  j                  | j                  j	                         d| j
                  j                  z   d      }t         j                  j                  |      st        j                  |d       t         j                  j                  || j                               S )zj
        Get the min UID file name. Create it if not found.

        Returns: Min UID file name.

        zRepository-StartUIDi  )
r   r   r   rH   getmetadatadirrC   r?   r   mkdirr   )r   startuiddirs     r   get_min_uid_filezBaseFolder.get_min_uid_file  sx     ggll4;;#=#=#?#04??3G3G#G#-/ ww~~k*HH[%(ww||K)?)?)ABBr   c                     | j                         }t        |d      }|j                  t        |      dz          |j	                          y)z
        Save the min UID in the min uid file

        Args:
            min_uid: min_uid to save

        Returns: None

        r   
N)r   r   r   strr   )r   min_uidr   fds       r   save_min_uidzBaseFolder.save_min_uid  s<     '')'4 
W$%

r   c                    | j                         }t        j                  j                  |      sy	 t	        |d      }t        |j                         j                               }|j                          |S #  t        d|z        xY w)zN
        Retrieve the min UID file

        Returns: min UID of file

        Nr   zCan't read %s)
r   r   r   r   r   r   r   r   r   IOError)r   r   r   r   s       r   retrieve_min_uidzBaseFolder.retrieve_min_uid  st     '')ww~~g&	5gt$B"++---/0GHHJN	5/G344s   AA7 7Bc                     t         )az  Writes a new message, with the specified uid.

        If the uid is < 0: The backend should assign a new uid and
           return it.  In case it cannot assign a new uid, it returns
           the negative uid passed in WITHOUT saving the message.

           If the backend CAN assign a new uid, but cannot find out what
           this UID is (as is the case with some IMAP servers), it
           returns 0 but DOES save the message.

           IMAP backend should be the only one that can assign a new
           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.

        Note that savemessage() does not check against dryrun settings,
        so you need to ensure that savemessage is never called in a
        dryrun mode.ru   )r   r   msgflagsrtimes        r   savemessagezBaseFolder.savemessage  s
    , "!r   c                     t         )z3Return the received time for the specified message.ru   r   s     r   getmessagetimezBaseFolder.getmessagetime  rx   r   c                     t         )z?Returns the message modification time of the specified message.ru   r   s     r   getmessagemtimezBaseFolder.getmessagemtime  rx   r   c                     t         )z,Returns the flags for the specified message.ru   r   s     r   getmessageflagszBaseFolder.getmessageflags!  rx   r   c                     t         )z/Returns the keywords for the specified message.ru   r   s     r   getmessagekeywordszBaseFolder.getmessagekeywords&  rx   r   c                     t         )zSets the specified message's flags to the given set.

        Note that this function does not check against dryrun settings,
        so you need to ensure that it is never called in a
        dryrun mode.ru   )r   r   r   s      r   savemessageflagszBaseFolder.savemessageflags+  r   r   c                 P    | j                  |      |z  }| j                  ||       y)a\  Adds the specified flags to the message's flag set.

        If a given flag is already present, it will not be duplicated.

        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: Message UID
        :param flags: A set() of flagsNr   r  r   r   r   newflagss       r   addmessageflagszBaseFolder.addmessageflags4  s)     '',u4c8,r   c                 Z    |D ]&  }| j                  |      s| j                  ||       ( y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   r  r   uidlistr   r   s       r   addmessagesflagszBaseFolder.addmessagesflagsC  s/    
  	1C~~c"$$S%0	1r   c                 P    | j                  |      |z
  }| j                  ||       y)a)  Removes each flag given from the message's flag set.

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

        If a given flag is already removed, no action will be taken for that
        flag.Nr  r  s       r   deletemessageflagszBaseFolder.deletemessageflagsL  s)     '',u4c8,r   c                 6    |D ]  }| j                  ||        yz
        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  r  s       r   deletemessagesflagszBaseFolder.deletemessagesflagsY  s#      	0C##C/	0r   c                     t         )z-Returns the labels for the specified message.ru   r   s     r   getmessagelabelszBaseFolder.getmessagelabelsb  rx   r   c                     	 t         )zSets the specified message's labels to the given set.

        Note that this function does not check against dryrun settings,
        so you need to ensure that it is never called in a
        dryrun mode.ru   )r   r   labelsignorelabelsmtimes        r   savemessagelabelszBaseFolder.savemessagelabelsg  s    	 "!r   c                 P    | j                  |      |z  }| j                  ||       y)ab  Adds the specified labels to the message's labels set.  If a given
        label is already present, it will not be duplicated.

        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: Message UID
        :param labels: A set() of labelsNr  r  r   r   r  	newlabelss       r   addmessagelabelszBaseFolder.addmessagelabelsx  s)     ))#.7	sI.r   c                 6    |D ]  }| j                  ||        y)zNote that this function does not check against dryrun settings,
        so you need to ensure that it is never called in a
        dryrun mode.

        :param uidlist: Message UID
        :param labels: Labels to addN)r  r   r  r  r   s       r   addmessageslabelszBaseFolder.addmessageslabels  s#      	/C!!#v.	/r   c                 P    | j                  |      |z
  }| j                  ||       y)av  Removes each label given from the message's label set.

        If a given label is already removed, no action will be taken for that
        label.

        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: Message uid
        :param labels: Labels to deleteNr  r  s       r   deletemessagelabelszBaseFolder.deletemessagelabels  s)     ))#.7	sI.r   c                 6    |D ]  }| j                  ||        yr  )r$  r!  s       r   deletemessageslabelszBaseFolder.deletemessageslabels  s#      	2C$$S&1	2r   c                 l    | j                   j                  dd|d|       |j                  ||       y)zAdds new header to the provided message.

        Arguments:
        - msg: message object
        - headername: name of the header to add
        - headervalue: value of the header to add

        Returns: None

        r.   z addmessageheader: called to add z: N)r7   rP   
add_header)r   r   
headernameheadervalues       r   addmessageheaderzBaseFolder.addmessageheader  s3     	b!;0 	1 	z;/r   c                 b    | j                   j                  dd|z         |j                  |      S )a   Return the value of an undefined occurence of the given header.

        Header name is case-insensitive.

        Arguments:
        - msg: message object
        - headername: name of the header to be searched

        Returns: header value or None if no such header was found.
        r.   z"getmessageheader: called to get %s)r7   rP   getr   r   r)  s      r   getmessageheaderzBaseFolder.getmessageheader  s+     	b>KLwwz""r   c                 d    | j                   j                  dd|z         |j                  |g       S )a%  Return a list of values for the given header.

        Header name is case-insensitive.

        Arguments:
        - msg: message object
        - headername: name of the header to be searched

        Returns: list of header values or empty list if no such header was
        found.
        r.   z&getmessageheaderlist: called to get %s)r7   rP   get_allr.  s      r   getmessageheaderlistzBaseFolder.getmessageheaderlist  s-     	bBZOP{{:r**r   c                     t        |      t        g       k7  r|g}| j                  j                  dd|z         |D ]  }||=  y)zDeletes headers in the given list from the message.

        Arguments:
        - msg: message object
        - header_list: list of headers to be deleted or just the header name

        r.   z)deletemessageheaders: called to delete %sN)typer7   rP   )r   r   header_lisths       r   deletemessageheaderszBaseFolder.deletemessageheaders  sT     R(&-KbAKO	Q  	AA	 	r   c                 R    t        |j                  |            }|yt        |      S )a  Returns the Unix timestamp of the email message, derived from the
        Date field header by default.

        Arguments:
        - msg: message object
        - header: header to extract the date from 

        Returns: timestamp or `None` in the case of failure.
        N)r   r-  r	   )r   r   header	datetuples       r   get_message_datezBaseFolder.get_message_date  s*     !1	##r   c                     t         )aA  Change the message from existing uid to new_uid.

        If the backend supports it (IMAP does not).

        :param uid: Message UID
        :param new_uid: (optional) If given, the old UID will be changed
            to a new UID. This allows backends efficient renaming of
            messages if the UID has changed.ru   )r   r   new_uids      r   change_message_uidzBaseFolder.change_message_uid  s
     "!r   c                     t         )r  ru   r   s     r   deletemessagezBaseFolder.deletemessage  s
    
 "!r   c                 4    |D ]  }| j                  |        yr
  )r@  )r   r  r   s      r   deletemessageszBaseFolder.deletemessages  s!    
  	$Cs#	$r   c           
         |r/| j                   j                  | j                  j                         	 d}| j	                  |      }| j                  |      }|j                         r| j                  |      }|j                  ||||      }|dkD  rH||k7  r#| j                  ||       |j                  |       |j                  ||||       d|vrYd| _        y|dk(  r| j                  |       yd||j                         |fz  }	t        |	t        j                  j                        y# t         $ r  t        $ rY}
|
j"                  t        j                  j                  kD  r | j                   j%                  |
t'               d          Y d}
~
yd}
~
wt(        $ r@}
| j                   j%                  |
t'               d   d|d| j*                  d	
        d}
~
w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.Nr   STz@Trying to save msg (uid %d) on folder %s returned invalid uid %d   zCopying message z [acc: r6   )r   )r7   registerthreadrC   accountr   r   r   r   r   r>  r@  rA   r   r   r   r   KeyboardInterruptseverityerrorr   	Exceptionrm   )r   r   	dstfolderrz   registermessager   r   r=  r   es              r   copymessagetozBaseFolder.copymessageto  s   $ GG""4??#:#:;0	G((-E'',E '')//#.
  ++C%GG{c>++C9 ..s3 (('5%He#(,D%A ""3'3I446@A 's,<,B,B,J,JKK $ ! 	 	,zz,22:::GGMM!XZ]++ 	GGMM!XZ]"D$4$46  7 		s1   B%D+ D+ 0:D+ +G=AFG;GGc                 R   t        j                  d|      d   }	 t        j                  d|t         j                        j	                  d      }|dfS # t
        $ rJ |j                  d      }|dkD  r1|j                  d|dz         }||dz   | j                         }|d	fcY S Y y
w xY w)a  Extract the Message-ID from a bytes object containing a raw message.

        This function attempts to find the Message-ID for a message that has not
        been processed by the built-in email library, and is therefore NOT an
        email object.  If parsing the message fails (or is otherwise not
        needed), this utility can be useful to help provide a (hopefully) unique
        identifier in log messages to facilitate locating the message on disk.

        :param raw_msg_bytes: bytes object containing the raw email message.
        :returns: A tuple containing the contents of the Message-ID header if
        found (or <Unknown Message-ID> if not found) and a flag which is True if
        the Message-ID was in proper RFC format or False if it contained
        defects.
        
   []?
[]?
r   s7   
message-id:[\s]+(<[A-Za-z0-9!#$%&'*+-/=?^_`{}|~.@ ]+>)r   s   
Message-ID:   
      F)s   <Unknown Message-ID>FT)r   splitsearch
IGNORECASEgroupAttributeErrorfindr   )r   raw_msg_bytes
msg_headermsg_id
_start_pos_end_poss         r   _extract_message_idzBaseFolder._extract_message_ida  s     XX/?B
	8YYZBMM++058  ~  		8 $)9:JA~%??5B?#JrM(;AAC&7		8s   4A AB&%B&c                    	 t        j                  dt        j                  d|      d   t         j                  t         j                  z        j                  d      }|j                  dd      \  }}|j                         }|d   |d   cxk(  rdk(  r|S  dj                  |d	|d
g      }|j                  ||d      S # t        $ r |cY S w xY w)a  Modify a raw message to quote the boundary separator for multipart messages.

        This function quotes only the first occurrence of the boundary field in
        the email header, and quotes any boundary value.  Improperly quoted
        boundary fields can give the internal python email library issues.

        :param raw_msg_bytes: bytes object containing the raw email message.
        :returns: The raw byte stream containing the quoted boundary
        s;   content-type:.*(boundary=["]?[A-Za-z0-9'()+_,-./:=? ]+["]?)rR  r   r      ="   r   s   ="   ")
r   rW  rV  rX  DOTALLrY  rZ  rstripr   r   )r   r\  boundary_fieldboundaryvalue	new_fields         r   _quote_boundary_fixzBaseFolder._quote_boundary_fix  s     	!IIVhh(-8;}}RYY&)).q  )..tQ7%8uRy&B& !  ' (E5$!?@I ((AFG#  	!  	!s   AB? ?CCc           
         d| _         g }| j                         D cg c]  }|j                  |      s| }}t        |      }| j                  D| j                  D ]5  }||v s|j                  |       | j                  j                  || |       7 |dkD  rb| j                  j                  j                  rB| j                  j                  dj                  || | j                  |j                               y| 5  t        |      D ]i  \  }}t        j                  j                   j"                  j%                         r n1|dk(  rd}| j                  j'                  |       _|dkD  rH|j                  |      r7| j)                  |      }	| j+                  |      }
|j-                  |d|	|
       | j                  j/                  ||dz   || |       | j1                         rw| j3                          t5        j6                  | j9                         | j:                  d| j                  d| |||f	      }|j=                          |j?                  |       U| j;                  |||d
       l |D ]  }|jA                           	 ddd       | j                   r| jB                  | jC                          yyyc c}w # 1 sw Y   9xY w)a  Pass1: Copy locally existing messages not on the other side.

        This will copy messages to dstfolder that exist locally but are
        not in the statusfolder yet. The strategy is:

        1) Look for messages present in self but not in statusfolder.
        2) invoke copymessageto() on those which:
           - If dstfolder doesn't have it yet, add them to dstfolder.
           - Update statusfolder.

        This function checks and protects us from action in dryrun mode.FNr   z+[DRYRUN] Copy {} messages from {}[{}] to {}z1Assertion that UID != 0 failed; ignoring message.r   zCopy message from :)targetr?   args)rM  )"rA   r   r   r   rB   r   r7   ignorecopyingmessagerC   rG  dryruninfoformat	enumerateofflineimapaccountsAccountabort_NOW_signalis_setwarnr   r   r   copyingmessagers   rw   r   InstanceLimitedThreadr}   rP  startappendr   r@   )r   rL  rz   threadsr   copylistnum_to_copynumr   r   r   threads               r   __syncmessagesto_copyz BaseFolder.__syncmessagesto_copy  s    "#'#9#9#; 8C'11#6  8 8(m +++ G(?OOC(GG00dIFG
 ?t66==GGLLFMMT4??I4H4HJ   $	%h/ !QS''//@@GGI!8MCGGLL%7y2237 !005E //4E ,,S$uE&&sC!G[$'02 '')&&('==668#11:>//:>@!9l;F LLNNN6*&&sI|a&PC!QD " G$	N   ,!!# - q8"$	 $	s   K <FKKc                 *   |j                         D cg c]7  }|dk\  r0| j                  |      s| j                  s|j                  |      s|9 }}t        |      r| j                  j
                  j                  s|j                  |       |D cg c]  }|j                  |      s| }}t        |      rP| j                  j                  ||g       | j                  j
                  j                  s|j                  |       yyyyc c}w c c}w )a$  Pass 2: Remove locally deleted messages on dst.

        Get all UIDs in statusfolder but not self. These are messages
        that were deleted in 'self'. Delete those from dstfolder and
        statusfolder.

        This function checks and protects us from action in dryrun mode.
        r   N)
r   r   rK   r   rC   rG  rs  rB  r7   deletingmessages)r   rL  rz   r   
deletelists        r   __syncmessagesto_deletez"BaseFolder.__syncmessagesto_delete  s     &2%C%C%E Lc..-))1D1DS1I  L
 L
 z?
 ??**11++J7)3P#y7J7J37O#PJP:((i[A..55,,Z8 6  L Qs   <DD(Dc                    t        | j                  |            }	 |j                         j                         }||S t        |j	                               }| j                  |      }||k\  s3t        ||z
        }||z  }d|z  }| j                  j                  |       t        |D 	cg c]  }	||	   	 c}	      }
||
z  }|S c c}	w # t        $ r Y |S w xY w)z^Combine the message's flags and keywords using the mapping for the
        destination folder.zXUnknown keywords skipped: %s
You may want to change your configuration to include those
)
setr   r   getkeywordmapr   r  r   r7   r|  rv   )r   r   rL  	selfflags
keywordmapknownkeywordsselfkeywordsskipped_keywordsr   keywkeywordlettersets              r   combine_flags_and_keywordsz%BaseFolder.combine_flags_and_keywords  s     ,,S12		"002@@BJ!  
 12M2237L L0 $(}(D#E - "23 S!"#NJt$4#NO ))I  $O # 		s)   !C A+C )C5C C 	CCc                    i }i }| j                         D ]  }|dk  s|j                  |      s|j                  |      r|j                  |      }n
t               }| j	                  ||      }||z
  }||z
  }	|D ]  }
|
|vrg ||
<   ||
   j                  |       ! |	D ]  }
|
|vrg ||
<   ||
   j                  |       !  t        |j                               D ]y  \  }
}| j                  j                  ||
|       | j                  j                  j                  rD|j                  |t        |
             |j                  |t        |
             { t        |j                               D ]y  \  }
}| j                  j                  ||
|       | j                  j                  j                  rD|j                  |t        |
             |j                  |t        |
             { y)aW  Pass 3: Flag synchronization.

        Compare flag 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 flag change to both dstfolder and
        statusfolder.

        This function checks and protects us from action in ryrun mode.
        r   N)r   r   r   r  r  r  r   itemsr7   addingflagsrC   rG  rs  r  deletingflagsr  )r   rL  rz   addflaglistdelflaglistr   statusflagsr  addflagsdelflagsflaguidss               r   __syncmessagesto_flagsz!BaseFolder.__syncmessagesto_flags9  s    ))+ 	.C Qwi11#6%%c**::3?!e77YGI ;.H"Y.H  .{*(*K%D!((-.
 ! .{*(*K%D!((-.+	.4 {0023 	;JD$GGdI6&&--&&tSY7))$D	:	; {0023 	>JD$GG!!$i8&&--))$D	:,,T3t9=	>r   c                 6   | j                   D ]@  }t        j                  j                  j                  j                         r y	  |||       B y# t        $ r  t        $ rm}|j                  t        j                  j                  kD  r d| d| j                  d}| j                  j                  |t               d   |       Y d}~d}~wt        $ rA}d| d| j                  d}| j                  j                  |t               d   |        d}~ww xY w)a  Syncs messages in this folder to the destination dstfolder.

        This is the high level entry for syncing messages in one direction.
        Syncsteps are:

        Pass1: Copy locally existing messages
         Copy messages in self, but not statusfolder to dstfolder if not
         already in dstfolder. dstfolder might assign a new UID (e.g. if
         uploading to IMAP). Update statusfolder.

        Pass2: Remove locally deleted messages
         Get all UIDS in statusfolder but not self. These are messages
         that were deleted in 'self'. Delete those from dstfolder and
         statusfolder.

         After this pass, the message lists should be identical wrt the
         uids present (except for potential negative uids that couldn't
         be placed anywhere).

        Pass3: Synchronize flag changes
         Compare flag 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 flag change to both dstfolder and
         statusfolder.

        Pass4: Synchronize label changes (Gmail only)
         Compares label mismatches in self with those in statusfolder.
         If msg has a valid UID and exists on dstfolder, syncs the labels
         to both dstfolder and statusfolder.

        :param dstfolder: Folderinstance to sync the msgs to.
        :param statusfolder: LocalStatus instance to sync against.
        zwhile syncing z
 [account r6   rE  N)rT   rw  rx  ry  rz  r{  rH  r   rI  r   FOLDERrm   r7   rJ  r   rK  )r   rL  rz   actionrO  r   s         r   syncmessagestozBaseFolder.syncmessagestoq  s    F 00 	F##++<<CCEy,/	 % # 5:: 0 6 6 = == >9=t?O?OPaA44 9=t?O?OPaA4s%   	AD#A#CD<DDc                 n    t        |t              r|| j                  k(  S t        |       t        |      k(  S )ao  Comparisons work either on string comparing folder names or
        on the same instance.

        MailDirFolder('foo') == 'foo' --> True
        a = MailDirFolder('foo'); a == b --> True
        MailDirFolder('foo') == 'moo' --> False
        MailDirFolder('foo') == IMAPFolder('foo') --> False
        MailDirFolder('foo') == MaildirFolder('foo') --> False
        )r   r   r?   idr   others     r   __eq__zBaseFolder.__eq__  s0     eS!DII%%$x2e9$$r   c                 &    | j                  |       S r   )r  r  s     r   __ne__zBaseFolder.__ne__  s    ;;u%%%r   )Nr   )Date)r   )Qr   r    r!   __doc____hash__rX   r\   r_   rb   re   rk   propertyrm   ro   rq   rs   rw   r{   r}   r   r   r   r   r   r>   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r  r"  r$  r&  r+  r/  r2  r7  r;  r>  r@  rB  rP  ra  rm  rQ   rR   r  rS   r  r  r  rd   r   r   r$   r$   0   s    HM
^)


 + +
 I I"
" 
I


"
 6,".,""
 	",
4*
"
!CFLC*C5$"0"
"
"
"
"-1-0"
""/	// 2$#+ &$"
""$EN>%HNL$\9>#J6>p3j%&r   r$   )r  emailos.pathr   r   r   sysr   r   email.parserr   email.generatorr   email.utilsr   r	   email.charsetr
   rw  r   offlineimap.uir   offlineimap.errorr   offlineimap.accountsrN  r   r$   rd   r   r   <module>r     sX   &   	    $ * / ! " & . .5==-- .F& F&r   