3 \ 3@sRdZddlZddlZddlZddlZddlZddlZddlZddlZddl Zddl Zddl Z ddl Z y ddl Z Wnek rdZ YnXddddddd d d d d ddddddgZejjdZGdddZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdd d ejjZGd d d eZGd!d"d"eZGd#d d eZGd$d d eZGd%d d eZ Gd&ddeZ!Gd'd(d(Z"Gd)d*d*e"Z#d=d,d-Z$d.d/Z%d0d1Z&d2d3Z'd4d5Z(d6d7Z)Gd8dde*Z+Gd9dde+Z,Gd:dde+Z-Gd;dde+Z.GdzDRead/write support for Maildir, mbox, MH, Babyl, and MMDF mailboxes.NMailboxMaildirmboxMHBabylMMDFMessageMaildirMessage mboxMessage MHMessage BabylMessage MMDFMessageErrorNoSuchMailboxError NotEmptyErrorExternalClashError FormatErrorasciic@seZdZdZdCddZddZdd Zd d Zd d ZddZ dDddZ ddZ ddZ ddZ ddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/ZdEd0d1Zd2d3ZdFd4d5Zd6d7Zd8d9Zd:d;Zdd?Z!d@Z"dGdAdBZ#dS)Hrz*A group of messages in a particular place.NTcCs tjjtjj||_||_dS)zInitialize a Mailbox instance.N)ospathabspath expanduser_path_factory)selfrfactorycreater/usr/lib64/python3.6/mailbox.py__init__$szMailbox.__init__cCs tddS)z$Add message and return assigned key.z&Method must be implemented by subclassN)NotImplementedError)rmessagerrradd)sz Mailbox.addcCs tddS)z=Remove the keyed message; raise KeyError if it doesn't exist.z&Method must be implemented by subclassN)r )rkeyrrrremove-szMailbox.removecCs|j|dS)N)r$)rr#rrr __delitem__1szMailbox.__delitem__c Cs(y|j|Wntk r"YnXdS)z'If the keyed message exists, remove it.N)r$KeyError)rr#rrrdiscard4szMailbox.discardcCs tddS)z>Replace the keyed message; raise KeyError if it doesn't exist.z&Method must be implemented by subclassN)r )rr#r!rrr __setitem__;szMailbox.__setitem__c Cs$y |j|Stk r|SXdS)z9Return the keyed message, or default if it doesn't exist.N) __getitem__r&)rr#defaultrrrget?s z Mailbox.getc Cs6|js|j|Stj|j| }|j|SQRXdS)z=Return the keyed message; raise KeyError if it doesn't exist.N)r get_message contextlibclosingget_file)rr#filerrrr)Fs zMailbox.__getitem__cCs tddS)z4Return a Message representation or raise a KeyError.z&Method must be implemented by subclassN)r )rr#rrrr,NszMailbox.get_messagecCstj|j|jS)zReturn a string representation or raise a KeyError. Uses email.message.Message to create a 7bit clean string representation of the message.)emailmessage_from_bytes get_bytes as_string)rr#rrr get_stringRszMailbox.get_stringcCs tddS)z8Return a byte string representation or raise a KeyError.z&Method must be implemented by subclassN)r )rr#rrrr3YszMailbox.get_bytescCs tddS)z6Return a file-like representation or raise a KeyError.z&Method must be implemented by subclassN)r )rr#rrrr/]szMailbox.get_filecCs tddS)zReturn an iterator over keys.z&Method must be implemented by subclassN)r )rrrriterkeysaszMailbox.iterkeyscCs t|jS)zReturn a list of keys.)listr6)rrrrkeysesz Mailbox.keysc cs@x:|jD].}y ||}Wntk r0w YnX|Vq WdS)z%Return an iterator over all messages.N)r6r&)rr#valuerrr itervaluesis  zMailbox.itervaluescCs|jS)N)r:)rrrr__iter__rszMailbox.__iter__cCs t|jS)z,Return a list of messages. Memory intensive.)r7r:)rrrrvaluesuszMailbox.valuesc csDx>|jD]2}y ||}Wntk r0w YnX||fVq WdS)z.Return an iterator over (key, message) tuples.N)r6r&)rr#r9rrr iteritemsys  zMailbox.iteritemscCs t|jS)z9Return a list of (key, message) tuples. Memory intensive.)r7r=)rrrritemssz Mailbox.itemscCs tddS)z9Return True if the keyed message exists, False otherwise.z&Method must be implemented by subclassN)r )rr#rrr __contains__szMailbox.__contains__cCs tddS)z*Return a count of messages in the mailbox.z&Method must be implemented by subclassN)r )rrrr__len__szMailbox.__len__cCs x|jD]}|j|q WdS)zDelete all messages.N)r8r')rr#rrrclearsz Mailbox.clearc Cs0y ||}Wntk r |SX|j||S)z3Delete the keyed message and return it, or default.)r&r')rr#r*resultrrrpops   z Mailbox.popcCs*x$|jD]}||j|fSWtddS)z6Delete an arbitrary (key, message) pair and return it.zNo messages in mailboxN)r6rCr&)rr#rrrpopitemszMailbox.popitemc Csxt|dr|j}nt|dr(|j}n|}d}x6|D].\}}y |||<Wq6tk rbd}Yq6Xq6W|rttddS)z4Change the messages that correspond to certain keys.r=r>FTzNo message with key(s)N)hasattrr=r>r&)rargsourceZbad_keyr#r!rrrupdates     zMailbox.updatecCs tddS)z&Write any pending changes to the disk.z&Method must be implemented by subclassN)r )rrrrflushsz Mailbox.flushcCs tddS)zLock the mailbox.z&Method must be implemented by subclassN)r )rrrrlocksz Mailbox.lockcCs tddS)z#Unlock the mailbox if it is locked.z&Method must be implemented by subclassN)r )rrrrunlockszMailbox.unlockcCs tddS)zFlush and close the mailbox.z&Method must be implemented by subclassN)r )rrrrclosesz Mailbox.closec Cs,y |jdStk r&tdYnXdS)Nrz?String input must be ASCII-only; use bytes or a Message instead)encode UnicodeError ValueError)rr!rrr_string_to_bytess zMailbox._string_to_bytesFc Cst|tjjrxtj}tjj||d}|j||j d|j }|j dt }|j ||jrt|jt  rt|j t nt|tttjfrt|tjrtjdtd|j}t|tr|j|}|r|j dd}|j dt }|j ||jo|jt  r|j t nt|drt|dr4tjd td|j}d }x|j}|jd r`|d dd}n|jd r||d dd}|sP|r|jdrd|dd }|j dt }|j ||}q:W|jr|r|jt  r|j t ntdt|d S)z%Dump message contents to target file.r z8Use of StringIO input is deprecated, use BytesIO insteads From s >From readbufferzDUse of text mode files is deprecated, use a binary mode file insteadNs  sFrom s>From zInvalid message type: %s) isinstancer1r!rioBytesIO generatorBytesGeneratorflattenseekrSreplacelinesepwrite_append_newlineendswithstrbytesStringIOwarningswarnDeprecationWarninggetvaluerPrErTreadline startswith TypeErrortype) rr!targetZ mangle_from_rTgendataZlastlinelinerrr _dump_messagesZ                   zMailbox._dump_message)NT)N)N)N)F)$__name__ __module__ __qualname____doc__rr"r$r%r'r(r+r)r,r5r3r/r6r8r:r;r<r=r>r?r@rArCrDrHrIrJrKrLrPrervrrrrr!sB       c@seZdZdZdZd6ddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-Zd.d/Zd0d1Zd2d3Zd4d5ZdS)7rzA qmail-style Maildir mailbox.:NTcCstj||||tjj|jdtjj|jdtjj|jdd|_tjj|js|rtj|jdx*|jj D]}tj|dqnWn t |ji|_ ddd|_ d|_ d|_d S) zInitialize a Maildir instance.tmpnewcur)r|r}r~ir)r~r}g?N)rrrrjoinr_pathsexistsmkdirr<r_toc _toc_mtimes _last_read _skewfactor)rdirnamerrrrrrr s  zMaildir.__init__cCs~|j}y|j||Wn*tk rB|jtj|jYnXt|t|t r||j }|j |j }||j krd}nd}d}tj j|jj|j d}tj j|j|||}t|t rtj|jtj j|j|jfyLytj|j|Wn(ttfk rtj|j|YnXtj|jWnNtk rx}z0tj|j|jtjkrftd|nWYdd}~XnX|S)z$Add message and return assigned key.r}rz$Name clash with existing message: %sN) _create_tmprv BaseExceptionrLrr$name _sync_closer[r get_subdircolonget_inforbasenamesplitrrutimegetatimeget_datelinkAttributeErrorPermissionErrorrenameOSErrorerrnoZEEXISTr)rr!Ztmp_filesubdirsuffixuniqdesterrrr"!sB      z Maildir.addcCs tjtjj|j|j|dS)z=Remove the keyed message; raise KeyError if it doesn't exist.N)rr$rrr_lookup)rr#rrrr$KszMaildir.removec Cs,y|j|Wnttfk r&YnXdS)z'If the keyed message exists, remove it.N)r$r&FileNotFoundError)rr#rrrr'OszMaildir.discardc Cs|j|}|j|}|j|}t|tr.|}n|}tjj|}|j|kr`|j|j|jd}nd}|j |tjj |j |} tjj |j |||} t|trtj | tjj | |jftj| | dS)z>Replace the keyed message; raise KeyError if it doesn't exist.rWrNrZ)rr"r[r rrrrrr'rrrrrr) rr#r!Z old_subpathZtemp_keyZ temp_subpathZdominant_subpathrrZtmp_pathnew_pathrrrr(Ws"        zMaildir.__setitem__c Cs|j|}ttjj|j|d }|jr4|j|}nt|}WdQRXtjj|\}}|j ||j |kr|j |j|j d|j tjj tjj|j||S)z4Return a Message representation or raise a KeyError.rbNrWrZ)ropenrrrrrr r set_subdirrset_infoset_dategetmtime)rr#Zsubpathfmsgrrrrrr,rs    zMaildir.get_messagec Cs8ttjj|j|j|d}|jjtdSQRXdS)z2Return a bytes representation or raise a KeyError.rrQN) rrrrrrrSrbrc)rr#rrrrr3szMaildir.get_bytescCs$ttjj|j|j|d}t|S)z6Return a file-like representation or raise a KeyError.r)rrrrrr _ProxyFile)rr#rrrrr/szMaildir.get_filec csH|jx:|jD]0}y|j|Wntk r8wYnX|VqWdS)zReturn an iterator over keys.N)_refreshrrr&)rr#rrrr6s zMaildir.iterkeyscCs|j||jkS)z9Return True if the keyed message exists, False otherwise.)rr)rr#rrrr?szMaildir.__contains__cCs|jt|jS)z*Return a count of messages in the mailbox.)rlenr)rrrrr@szMaildir.__len__cCsdS)z"Write any pending changes to disk.Nr)rrrrrIsz Maildir.flushcCsdS)zLock the mailbox.Nr)rrrrrJsz Maildir.lockcCsdS)z#Unlock the mailbox if it is locked.Nr)rrrrrKszMaildir.unlockcCsdS)zFlush and close the mailbox.Nr)rrrrrLsz Maildir.closecCs`g}xVtj|jD]F}t|dkr|ddkrtjjtjj|j|r|j|ddqW|S)zReturn a list of folder names.rWr.N)rlistdirrrrisdirrappend)rrBentryrrr list_folderss zMaildir.list_folderscCs ttjj|jd||jddS)z/Return a Maildir instance for the named folder.rF)rr)rrrrrr)rfolderrrr get_folderszMaildir.get_foldercCs\tjj|jd|}t||jd}tjj|d}tjj|sXtjtj|tj tj Bd|S)z>Create a folder and return a Maildir instance representing it.r)rZ maildirfolderi) rrrrrrrrLrO_CREATO_WRONLY)rrrrBZmaildirfolder_pathrrr add_folders zMaildir.add_foldercCs,tjj|jd|}xTtjtjj|dtjtjj|dD](}t|dksZ|ddkr>td|q>WxPtj|D]B}|dkrv|dkrv|dkrvtjjtjj||rvtd||fqvWx`tj|d d D]N\}}}x |D]}tj tjj||qWx |D]}tj tjj||qWqWtj |d S) z-Delete the named folder, which must be empty.rr}r~rWrzFolder contains message(s): %sr|z%Folder contains subdirectory '%s': %sF)topdownN) rrrrrrrrwalkr$rmdir)rrrrrootdirsfilesrrr remove_folders   zMaildir.remove_foldercCs\tj}xNtjtjj|jdD]4}tjj|jd|}|tjj|dkr tj|q WdS)zDelete old files in "tmp".r|i@N)timerrrrrrr$)rnowrrrrrcleans z Maildir.cleanrWcCstj}tj}d|kr$|jdd}d|kr8|jdd}dt|t|ddtjtj|f}tj j |j d|}ytj |Wn@t k rtjd7_yt|Stk rYnXYnXtd |d S) z=Create a file in the tmp subdirectory and open and return it./z\057r{z\072z%s.M%sP%sQ%s.%srWg.Ar|z&Name clash prevented file creation: %sN)rsocket gethostnamerbintrgetpidr_countrrrstatr_create_carefullyFileExistsErrorr)rrZhostnamerrrrrrs&    zMaildir._create_tmpcCstj|jd|jkrbd}x<|jD]2}tjj|j|}||j|krLd}||j|<q$W|sbdSi|_xl|jD]b}|j|}xRtj |D]D}tjj ||}tjj |rq|j |j d}tjj |||j|<qWqpWtj|_dS)z!Update table of contents mapping.rUFTNr)rrrrrrrrrrrrrr)rZrefreshrmtimerrprrrrrs&    zMaildir._refreshcCszy,tjjtjj|j|j|r*|j|SWntk r@YnX|jy |j|Stk rttd|YnXdS)z=Use TOC to return subpath for given key, or raise a KeyError.zNo message with key: %sN)rrrrrrr&r)rr#rrrr#s zMaildir._lookupc CsVt|ds|j|_xReplace the keyed message; raise KeyError if it doesn't exist.TN)rrrr)rr#r!rrrr(hs z_singlefileMailbox.__setitem__ccs|j|jjEdHdS)zReturn an iterator over keys.N)rrr8)rrrrr6nsz_singlefileMailbox.iterkeyscCs|j||jkS)z9Return True if the keyed message exists, False otherwise.)rr)rr#rrrr?ssz_singlefileMailbox.__contains__cCs|jt|jS)z*Return a count of messages in the mailbox.)rrr)rrrrr@xsz_singlefileMailbox.__len__cCs|jst|jd|_dS)zLock the mailbox.TN)r _lock_filer)rrrrrJ}s z_singlefileMailbox.lockcCs|jrt|jd|_dS)z#Unlock the mailbox if it is locked.FN)r _unlock_filer)rrrrrKs z_singlefileMailbox.unlockc Cs|js |jrt|jd|_dS|jjdd|jj}||jkrTtd|j|ft|j }yi}|j |xt |j j D]|}|j |\}}|jj||j||j}x0|jjtd||jj}|sP|j|qW||jf||<|j|q~W|j|_Wn"|jtj|jYnXt||jjtj|j j} tj|j| ytj|j|j Wn2tk rtj|j tj|j|j YnXt|j d|_||_ d|_d|_|jrt|jdddS) z"Write any pending changes to disk.FNrrUz4Size of mailbox file changed (expected %i, found %i)izrb+)dotlock) rr _sync_flushrratellrr_create_temporaryr_pre_mailbox_hooksortedrr8_pre_message_hookrSminrd_post_message_hookrLrr$rrrst_modechmodrrrrr) rZcur_lenZnew_fileZnew_tocr#startstopZ new_startrTmoderrrrIs^            z_singlefileMailbox.flushcCsdS)z,Called before writing the mailbox to file f.Nr)rrrrrrsz$_singlefileMailbox._pre_mailbox_hookcCsdS)z-Called before writing each message to file f.Nr)rrrrrrsz$_singlefileMailbox._pre_message_hookcCsdS)z,Called after writing each message to file f.Nr)rrrrrrsz%_singlefileMailbox._post_message_hookcCs4z |jWdz|jr|jWd|jjXXdS)zFlush and close the mailbox.N)rIrrKrrL)rrrrrLs   z_singlefileMailbox.closec CsJ|jdkr|j|dk rFy |j|Stk rDtd|YnXdS)z'Return (start, stop) or raise KeyError.NzNo message with key: %s)r _generate_tocr&)rr#rrrrs  z_singlefileMailbox._lookupc Cs|jjdd|jj}t|jdkr:|j r:|j|jy&|j|j|j|}|j |jWn"t k r|jj |YnX|jj |jj|_ |S)z;Append message to mailbox and return (start, stop) offsets.rrU)rrarrrrrr_install_messagerrtruncaterIr)rr!ZbeforeZoffsetsrrrrs       z"_singlefileMailbox._append_message)NT)N)rwrxryrzrr"r$r(r6r?r@rJrKrIrrrrLrrrrrrr>s"  @ rc@sBeZdZdZdZddZdddZddd Zdd d Zd d Z dS) _mboxMMDFzAn mbox or MMDF mailbox.TcCsp|j|\}}|jj||jjjtd}|jj||jj}|j|jtd}|j |ddj d|S)z4Return a Message representation or raise a KeyError.rQrXNr) rrrarnrbrcrSr_message_factoryset_fromdecode)rr#rr from_linestringrrrrr,s z_mboxMMDF.get_messageFcCstj|j|j|dS)z3Return a string representation or raise a KeyError.)unixfrom)r1r2r3r4)rr#from_rrrr5sz_mboxMMDF.get_stringcCsJ|j|\}}|jj||s(|jj|jj||jj}|jtdS)z3Return a string representation or raise a KeyError.rQ)rrrarnrSrrbrc)rr#rrrrrrrr3s   z_mboxMMDF.get_bytescCs<|j|\}}|jj||s(|jjt|j|jj|S)z6Return a file-like representation or raise a KeyError.)rrrarn _PartialFiler)rr#rrrrrrr/s   z_mboxMMDF.get_filecCs d}t|tr|j|}t|trf|jdrf|jd}|dkr\|d|}||dd}q|}d}nJt|tr|jjd}d|}n(t|t j j r|j }|dk r|jd}|dkrdt jt jj}|jj}|jj|t|j||j|j|jj}||fS) z1Format a message and blindly write to self._file.NsFrom rQrWrrsFrom MAILER-DAEMON rZ)r[rgrPrhrofind_mboxMMDFMessageget_fromrMr1r!r get_unixfromrasctimegmtimerrrdrcrv _mangle_from_)rr!rnewlineZauthorrrrrrr&s0         z_mboxMMDF._install_messageN)F)F)F) rwrxryrzrr,r5r3r/rrrrrrs  rc@s2eZdZdZdZdZd ddZddZdd ZdS) rzA classic mbox mailbox.TNcCst|_tj||||dS)zInitialize an mbox mailbox.N)r rrr)rrrrrrrrLsz mbox.__init__cCs|jtdS)z,Called after writing each message to file f.N)rdrc)rrrrrrQszmbox._post_message_hookcCsgg}}d}|jjdx|jj}|jj}|jdr|t|t|krl|rb|j|ttn |j||j|d}q|s|r|j|ttn |j|Pq|tkrd}qd}qWtt t |||_ t|j |_ |jj|_ dS)z0Generate key-to-(start, stop) table of contents.FrsFrom TN)rrarrnrorrrcdict enumerateziprrr)rstartsstopsZlast_was_emptyline_posrurrrrUs0         zmbox._generate_toc)NT) rwrxryrzrrerrrrrrrrCs  c@s2eZdZdZd ddZddZdd Zd d ZdS) rzAn MMDF mailbox.NTcCst|_tj||||dS)zInitialize an MMDF mailbox.N)r rrr)rrrrrrrrzsz MMDF.__init__cCs|jdtdS)z-Called before writing each message to file f.sN)rdrc)rrrrrrszMMDF._pre_message_hookcCs|jtdtdS)z,Called after writing each message to file f.sN)rdrc)rrrrrrszMMDF._post_message_hookcCsgg}}|jjdd}x|}|jj}|jj}|jdtr|j|xV|}|jj}|jj}|dtkr|j|ttPqN|sN|j|PqNWq|sPqWtt t |||_ t|j |_ |jjdd|jj|_ dS)z0Generate key-to-(start, stop) table of contents.rsrUN)rrarnrrorcrrrrrrrr)rrrnext_posrrurrrrs2          zMMDF._generate_toc)NT)rwrxryrzrrrrrrrrrws  c@seZdZdZd0ddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/ZdS)1rzAn MH mailbox.NTcCsttj||||tjj|jsj|r`tj|jdtjtjtjj |jdtj tj Btj Bdn t |jd|_dS)zInitialize an MH instance.iz .mh_sequencesiFN)rrrrrrrrLrrrO_EXCLrrr)rrrrrrrrs z MH.__init__cCs|j}t|dkrd}n t|d}tjj|jt|}t|}d}z|j rVt |zfy|j ||Wn:t k r|j rt |t|d}tj|YnXt|tr|j||Wd|j rt |XWd|st|X|S)z$Add message and return assigned key.rrWFTN)r8rmaxrrrrrgrrrrvrrrr$r[r _dump_sequences)rr!r8Znew_keyrrclosedrrrr"s6     zMH.addcCsxtjj|jt|}yt|d}Wn>tk r`}z"|jtjkrNt d|nWYdd}~XnX|j tj |dS)z=Remove the keyed message; raise KeyError if it doesn't exist.zrb+zNo message with key: %sN) rrrrrgrrrrr&rLr$)rr#rrrrrrr$s z MH.removecCstjj|jt|}yt|d}Wn>tk r`}z"|jtjkrNt d|nWYdd}~XnXzd|j rrt |z@tj tj|tj tjB|j||t|tr|j||Wd|j rt|XWdt|XdS)z>Replace the keyed message; raise KeyError if it doesn't exist.zrb+zNo message with key: %sN)rrrrrgrrrrr&rrrLrO_TRUNCrvr[r r rr)rr#r!rrrrrrr(s$   zMH.__setitem__c Csy@|jr$ttjj|jt|d}nttjj|jt|d}Wn>tk r~}z"|jtj krlt d|nWYdd}~XnX|2|jrt |z t |}Wd|jrt |XWdQRXx*|jjD]\}}||kr|j|qW|S)z4Return a Message representation or raise a KeyError.zrb+rzNo message with key: %sN)rrrrrrrgrrrr&rr r get_sequencesr> add_sequence)rr#rrrrkey_listrrrr,s&  zMH.get_messagec Csy@|jr$ttjj|jt|d}nttjj|jt|d}Wn>tk r~}z"|jtj krlt d|nWYdd}~XnX|6|jrt |z|j j tdS|jrt|XWdQRXdS)z2Return a bytes representation or raise a KeyError.zrb+rzNo message with key: %sNrQ)rrrrrrrgrrrr&rrSrbrcr)rr#rrrrrr3s z MH.get_bytescCsfyttjj|jt|d}Wn>tk r\}z"|jtjkrJt d|nWYdd}~XnXt |S)z6Return a file-like representation or raise a KeyError.rzNo message with key: %sN) rrrrrrgrrrr&r)rr#rrrrrr/)s z MH.get_filecCsttddtj|jDS)zReturn an iterator over keys.css|]}|jrt|VqdS)N)isdigitr).0rrrr 6szMH.iterkeys..)iterrrrr)rrrrr64sz MH.iterkeyscCstjjtjj|jt|S)z9Return True if the keyed message exists, False otherwise.)rrrrrrg)rr#rrrr?9szMH.__contains__cCstt|jS)z*Return a count of messages in the mailbox.)rr7r6)rrrrr@=sz MH.__len__cCs2|js.ttjj|jdd|_t|jd|_dS)zLock the mailbox.z .mh_sequenceszrb+TN)rrrrrrrr)rrrrrJAs zMH.lockcCs(|jr$t|jt|j|`d|_dS)z#Unlock the mailbox if it is locked.FN)rrrr)rrrrrKHs   z MH.unlockcCsdS)z&Write any pending changes to the disk.Nr)rrrrrIPszMH.flushcCs|jr|jdS)zFlush and close the mailbox.N)rrK)rrrrrLTszMH.closecCs@g}x6tj|jD]&}tjjtjj|j|r|j|qW|S)zReturn a list of folder names.)rrrrrrr)rrBrrrrrYs zMH.list_folderscCsttjj|j||jddS)z+Return an MH instance for the named folder.F)rr)rrrrrr)rrrrrrasz MH.get_foldercCsttjj|j||jdS)z:Create a folder and return an MH instance representing it.)r)rrrrrr)rrrrrrfsz MH.add_foldercCs`tjj|j|}tj|}|dgkr:tjtjj|dn|gkrDntd|jtj|dS)z-Delete the named folder, which must be empty.z .mh_sequenceszFolder not empty: %sN)rrrrrr$rr)rrrZentriesrrrrks  zMH.remove_folderc s i}ttjj|jdddd}t|jx|D]}y|jd\}}t}xT|jD]H}|jrr|j t |qVdd|jdD\}} |j t || d qVWfd d t |D||<t||d kr||=Wq2tk rtd |jYq2Xq2WWdQRX|S)z=Return a name-to-key-list dictionary to define each sequence.z .mh_sequencesrASCII)encodingr{css|]}t|VqdS)N)r)rxrrrrsz#MH.get_sequences..-rWcsg|]}|kr|qSrr)rr#)all_keysrr sz$MH.get_sequences..rz"Invalid sequence specification: %sN)rrrrrsetr8rrr"rrHrangerrrOrrstrip) rresultsrrurcontentsr8specrrr)rrr ws&    zMH.get_sequencesc Cs ttjj|jdddd}ztjtj|jtjtjBx|j D]\}}t |dkrXqB|j |dd}d}xbt t |D]R}|d |kr|sd }|j d n*|rd}|j d ||fn|j d ||}q|W|r|j t|dqB|j dqBWWdt|XdS)z:Set sequences using the given name-to-key-list dictionary.z .mh_sequenceszr+r)rrr{NFrWTrz%s %sz %s )rrrrrrLrrr r>rrdrrrgr)r sequencesrrr8prevZ completingr#rrr set_sequencess.   zMH.set_sequencesc CsL|j}d}g}x|jD]}|d|kr|j||dfy4tjtjj|jt|tjj|jt|dWnHt t fk rtj tjj|jt|tjj|jt|dYnXtj tjj|jt||d7}qW|d|_ t|dkrdSxB|jD]6\}}x*|D]"\}}||kr|||j|<qWqW|j|dS)z?Re-name messages to eliminate numbering gaps. Invalidates keys.rrWN)r r6rrrrrrrgrrrunlinkrrr>indexr$) rr"r#Zchangesr#rroldr}rrrpacks,      zMH.packcCs||j}|j}x<|jD]0\}}||kr6|j|q||kr||j|=qWx|D]}||krT|g||<qTW|j|dS)z;Inspect a new MHMessage and update sequences appropriately.N)r r>rr&r$)rr!r#Zpending_sequencesZ all_sequencesrrsequencerrrr s  zMH._dump_sequences)NT)rwrxryrzrr"r$r(r,r3r/r6r?r@rJrKrIrLrrrrr r$r(r rrrrrs. "  c@seZdZdZedddddddhZd%d d Zd dZddZddZ ddZ ddZ ddZ ddZ ddZddZdd Zd!d"Zd#d$Zd S)&rzAn Rmail-style Babyl mailbox.unseendeletedZfiledanswered forwardedZeditedresentNTcCstj||||i|_dS)zInitialize a Babyl mailbox.N)rr_labels)rrrrrrrrszBabyl.__init__cCs(tj||}t|tr$|j|j|<|S)z$Add message and return assigned key.)rr"r[r get_labelsr/)rr!r#rrrr"s  z Babyl.addcCs"tj||||jkr|j|=dS)z=Remove the keyed message; raise KeyError if it doesn't exist.N)rr$r/)rr#rrrr$s  z Babyl.removecCs*tj|||t|tr&|j|j|<dS)z>Replace the keyed message; raise KeyError if it doesn't exist.N)rr(r[r r0r/)rr#r!rrrr(s zBabyl.__setitem__c Cs|j|\}}|jj||jjtj}x4|jj}|dtksJ| rLP|j|jtdq.Wtj}x0|jj}|tks| rP|j|jtdqlW||jj }|jj |}|jtd}t |j |} | j |j ||jkr| j|j|| S)z4Return a Message representation or raise a KeyError.s *** EOOH ***rQ)rrrarnr\r]rcrdrbrrSr rm set_visibler/ set_labels) rr#rroriginal_headersruZvisible_headersnZbodyrrrrr,s.       zBabyl.get_messagec Cs|j|\}}|jj||jjtj}x4|jj}|dtksJ| rLP|j|jtdq.Wx|jj}|tks|| rdPqdW|j }||jj }|jj |}|jtd}||S)z3Return a string representation or raise a KeyError.s *** EOOH ***rQ) rrrarnr\r]rcrdrbrmrrS) rr#rrr3ruZheadersr4rtrrrr3s$      zBabyl.get_bytescCstj|j|jdtS)z6Return a file-like representation or raise a KeyError.rQ)r\r]r3rbrc)rr#rrrr/%szBabyl.get_filecCs@|jt}x|jjD]}|j|qW|j|jt|S)z4Return a list of user-defined labels in the mailbox.)rrr/r<rHdifference_update_special_labelsr7)rlabelsZ label_listrrrr0)s  zBabyl.get_labelscCs>gg}}|jjdd}g}x|}|jj}|jj}|dtkrt|t|krf|j|tt|j|dd|jjddjdD}|j|q |dks|dtkrt|t|kr|j|ttq |s |j|ttPq Wtt t |||_ tt ||_ t|j |_ |jjdd |jj|_dS) z0Generate key-to-(start, stop) table of contents.rs cSsg|]}|jr|jqSr)strip)rlabelrrrr@sz'Babyl._generate_toc..rWN,rU)rrarnrrcrrrrrrrr/rr)rrrrZ label_listsrrur7rrrr2s4        zBabyl._generate_toccCsVdt}|dt7}|j}dd|D}|ddj|t7}|d7}|j|dS) z,Called before writing the mailbox to file f.sBABYL OPTIONS:s Version: 5css|]}|jVqdS)N)rM)rr9rrrrUsz*Babyl._pre_mailbox_hook..sLabels:r:r;N)rcr0rrd)rrZbabylr7rrrrPs zBabyl._pre_mailbox_hookcCs|jdtdS)z-Called before writing each message to file f. N)rdrc)rrrrrrZszBabyl._pre_message_hookcCs|jtddS)z,Called after writing each message to file f.r;N)rdrc)rrrrrr^szBabyl._post_message_hookcCs|jj}t|trg}g}x0|jD]$}||jkr@|j|q&|j|q&W|jjdx |D]}|jjd|jq`W|jjdx$|D]}|jjd|jdqW|jjt n|jjdt t|t j j r0t j}t jj|dd}|j||jdx6|j}|jj|jd t |d ks8| r Pq W|jjd t t|trt j} t jj| dd} | j|jxz| j}|jj|jd t |d ks| rPqWnB|jdx6|j}|jj|jd t |d ks| rPqWx,|jd } | sP|jj| jd t qWnZt|ttt jfr,t|t jrhtjd td |j}t|tr~|j|}|jdd} | ddkr|jj|d| jd t |jjd t |jj|d| jd t |jj|| djd t n(|jjd t t |jj|jd t n^t |drzt |drXtjdtd |j!}|j} d}x|j}|j"dr|ddd }n|j"dr|ddd }|jj|jd t |d ks| rf|rd}|jjd t |j| nPqfWx|j}|sP|j"dr,|ddt }n:|j"drJ|ddt }n|j"d rf|ddt }|jj|qWnt#dt$||jj}||fS)z0Write message contents and return (start, stop).1s, s,, r:s1,,FrrQs *** EOOH ***iz8Use of StringIO input is deprecated, use BytesIO insteadrRs rUrWNrnrTzDUse of text mode files is deprecated, use a binary mode file insteadTs rVzInvalid message type: %srZrYrZrYrZrZ)%rrr[r r0r6rrdrMrcr1r!rr\r]r^r_r`rarnrb get_visiblerSrhrgrirjrkrlrmrPrrErTrfrprq)rr!rZspecial_labelsr7r9Z orig_bufferZorig_generatorruZ vis_bufferZ vis_generatorrTZ body_startZ original_posZ first_passrrrrrbs                         zBabyl._install_message)NT)rwrxryrz frozensetr6rr"r$r(r,r3r/r0rrrrrrrrrrs      c@s*eZdZdZd ddZddZddZdS) rz0Message with mailbox-format-specific properties.NcCst|tjjr4|jtj|t|tr|j|nt|trP|jtj |n~t|t rl|jtj |nbt|t j r|jtj|nDt|dr|jtj|n(|dkrtjjj|ntdt|dS)zInitialize a Message instance.rSNzInvalid message type: %s)r[r1r!r_become_messagecopydeepcopy _explain_torhr2rgZmessage_from_stringr\ TextIOWrapperZmessage_from_filerEZmessage_from_binary_filerrprq)rr!rrrrs      zMessage.__init__cCs8t|dg}x&|jD]}||kr|j||j|<qWdS)z0Assume the non-format-specific state of message._type_specific_attributesN)getattr__dict__)rr!Z type_specificrrrrrAs  zMessage._become_messagecCst|trdStddS)z:Copy format-specific state to message insofar as possible.Nz Cannot convert to specified type)r[rrp)rr!rrrrDs zMessage._explain_to)N)rwrxryrzrrArDrrrrrs c@s|eZdZdZdddgZdddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZddZdS)r z)Message with Maildir-specific properties._subdir_info_dateNcCs&d|_d|_tj|_tj||dS)z%Initialize a MaildirMessage instance.r}rN)rIrJrrKrr)rr!rrrrs zMaildirMessage.__init__cCs|jS)zReturn 'new' or 'cur'.)rI)rrrrrszMaildirMessage.get_subdircCs(|dks|dkr||_n td|dS)zSet subdir to 'new' or 'cur'.r}r~z!subdir must be 'new' or 'cur': %sN)rIrO)rrrrrrszMaildirMessage.set_subdircCs"|jjdr|jddSdSdS)z*Return as a string the flags that are set.z2,rUNr)rJro)rrrr get_flags s zMaildirMessage.get_flagscCsddjt||_dS)z)Set the given flags and unset all others.z2,rN)rrrJ)rflagsrrr set_flagsszMaildirMessage.set_flagscCs$|jdjt|jt|BdS)z.Set the given flag(s) without changing others.rN)rNrrrL)rflagrrradd_flagszMaildirMessage.add_flagcCs,|jr(|jdjt|jt|dS)z7Unset the given string flag(s) without changing others.rN)rLrNrr)rrOrrr remove_flagszMaildirMessage.remove_flagcCs|jS)zSet "From " line, formatting and appending time_ if specified.NT )rrrr_)rrZtime_rrrrrs z_mboxMMDFMessage.set_fromcCs|jdd|jddS)z*Return as a string the flags that are set.StatusrzX-Status)r+)rrrrrLzsz_mboxMMDFMessage.get_flagscCst|}d \}}x&d D]}||kr||7}|j|qWx&d D]}||kr>||7}|j|q>W|djt|7}y|jd|Wn tk r|jd|YnXy|jd|Wn tk r|jd|YnXd S) z)Set the given flags and unset all others.rrUrVrXrYrZrazX-StatusN)rr)rUrV)rXrYrZ)rr$rrreplace_headerr&Z add_header)rrMZ status_flagsZ xstatus_flagsrOrrrrN~s&  z_mboxMMDFMessage.set_flagscCs$|jdjt|jt|BdS)z.Set the given flag(s) without changing others.rN)rNrrrL)rrOrrrrPsz_mboxMMDFMessage.add_flagcCs4d|ksd|kr0|jdjt|jt|dS)z7Unset the given string flag(s) without changing others.razX-StatusrN)rNrrrL)rrOrrrrQsz_mboxMMDFMessage.remove_flagc Cst|trt|j}d|kr(|jdd|kr:|jdd|krL|jdd|kr^|jdd|krp|jd|d =|d =d j|jjdd }y|j t j t j |dWnttfk rYnXn t|tr|j|j|j|jnt|trZt|j}d|kr$|jdd|kr8|jdd|krL|jd|d =|d =nt|trt|j}d|kr|jdd|kr|jdd|kr|jd|d =|d =nt|trntdt|d S)zACopy mbox- or MMDF-specific state to message insofar as possible.rVr~rYrZrUrTrXrWZstatuszx-statusr`rXNz%a %b %d %H:%M:%S %Yr*r[r\r+r,z$Cannot convert to specified type: %s)r[r rrLrrPrrrrcalendarZtimegmrZstrptimerO OverflowErrorrrNrr rr r^rrprq)rr!rMZ maybe_daterrrrDs^                          z_mboxMMDFMessage._explain_to)N)N) rwrxryrzrFrrrrLrNrPrQrDrrrrr`s  rc@seZdZdZdS)r z&Message with mbox-specific properties.N)rwrxryrzrrrrr sc@sHeZdZdZdgZdddZddZdd Zd d Zd d Z ddZ dS)r z$Message with MH-specific properties. _sequencesNcCsg|_tj||dS)z!Initialize an MHMessage instance.N)rfrr)rr!rrrrszMHMessage.__init__cCs|jddS)z4Return a list of sequences that include the message.N)rf)rrrrr szMHMessage.get_sequencescCst||_dS)z3Set the list of sequences that include the message.N)r7rf)rr"rrrr$szMHMessage.set_sequencescCs6t|tr"||jkr2|jj|ntdt|dS)z8Add sequence to list of sequences including the message.zsequence type must be str: %sN)r[rgrfrrprq)rr)rrrrs  zMHMessage.add_sequencec Cs*y|jj|Wntk r$YnXdS)zARemove sequence from the list of sequences including the message.N)rfr$rO)rr)rrrremove_sequenceszMHMessage.remove_sequencecCsJt|trdt|j}d|kr*|jdn|jd|jdd|krP|jdd|krb|jdnt|trt|j}d|kr|jdn |jd d|kr|jdd|kr|jd nt|trx||jD]}|j|qWn`t|t r(t|j}d|kr|j dd|krF|j d nt|t r6nt d t |d S)z6Copy MH-specific state to message insofar as possible.r*r~rTr\rYr[rUROrVrZr,z$Cannot convert to specified type: %sN)r[r rr rrPrr rr r^rrprq)rr!r"r)rrrrDs@                     zMHMessage._explain_to)N) rwrxryrzrFrr r$rrgrDrrrrr s c@sbeZdZdZddgZdddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ dS)r z'Message with Babyl-specific properties.r/_visibleNcCsg|_t|_tj||dS)z#Initialize a BabylMessage instance.N)r/rrir)rr!rrrr"szBabylMessage.__init__cCs|jddS)z'Return a list of labels on the message.N)r/)rrrrr0(szBabylMessage.get_labelscCst||_dS)z&Set the list of labels on the message.N)r7r/)rr7rrrr2,szBabylMessage.set_labelscCs6t|tr"||jkr2|jj|ntdt|dS)z+Add label to list of labels on the message.zlabel must be a string: %sN)r[rgr/rrprq)rr9rrrr^0s  zBabylMessage.add_labelc Cs*y|jj|Wntk r$YnXdS)z4Remove label from the list of labels on the message.N)r/r$rO)rr9rrr remove_label8szBabylMessage.remove_labelcCs t|jS)z3Return a Message representation of visible headers.)rri)rrrrr??szBabylMessage.get_visiblecCst||_dS)z2Set the Message representation of visible headers.N)rri)rZvisiblerrrr1CszBabylMessage.set_visiblecCsjx6|jjD](}||kr,|jj|||q |j|=q Wx,dD]$}||kr>||jkr>|||j|<q>WdS) z9Update and/or sensibly generate a set of visible headers.DateFromReply-ToToCCSubjectN)rkrlrmrnrorp)rir8rb)rheaderrrrupdate_visibleGs  zBabylMessage.update_visiblecCsvt|tr~t|j}d|kr*|jdn|jd|jdd|ksNd|krX|jdd|krj|jdd |kr||jd nt|trt|j}d|kr|jd n |jd d |kr|jd d|kr|jdnt|trt|j}d|kr|jdd|krr|jdnXt|t rT|j |j x<|jD]}|j |q>Wnt|t rbntdt|dS)z9Copy Babyl-specific state to message insofar as possible.r*r~rTr-r.r]r,rUr+rWrhrVrXrZr[z$Cannot convert to specified type: %sN)r[r rr0rrPrr rr r1r?r^rrprq)rr!r7r9rrrrDRsF                      zBabylMessage._explain_to)N)rwrxryrzrFrr0r2r^rjr?r1rrrDrrrrr s  c@seZdZdZdS)r z&Message with MMDF-specific properties.N)rwrxryrzrrrrr |sc@seZdZdZd&ddZd'ddZd(ddZd)d d Zd*d d Zd dZ ddZ d+ddZ ddZ ddZ ddZddZddZddZd d!Zd"d#Zed$d%ZdS),rzA read-only wrapper of a file.NcCs$||_|dkr|j|_n||_dS)zInitialize a _ProxyFile.N)rr_pos)rrposrrrrs z_ProxyFile.__init__cCs|j||jjS)z Read bytes.)_readrrS)rsizerrrrSsz_ProxyFile.readcCs|j||jjS)z Read bytes.)rurread1)rrvrrrrwsz_ProxyFile.read1cCs|j||jjS)z Read a line.)rurrn)rrvrrrrnsz_ProxyFile.readlinecCs>g}x4|D],}|j||dk r |t|8}|dkr Pq W|S)zRead multiple lines.Nr)rr)rsizehintrBrurrr readliness   z_ProxyFile.readlinesccs x|j}|sdS|VqWdS)zIterate over lines.N)rn)rrurrrr;s z_ProxyFile.__iter__cCs|jS)zReturn the position.)rs)rrrrrsz_ProxyFile.tellrcCs4|dkr|jj|j|jj|||jj|_dS)zChange position.rWN)rrarsr)roffsetwhencerrrrasz_ProxyFile.seekc Cs0t|dr,zt|jdr"|jjWd|`XdS)zClose the file.rrLN)rErrL)rrrrrLs   z_ProxyFile.closecCs2|dkr d}|jj|j||}|jj|_|S)z"Read size bytes using read_method.NrWrZ)rrarsr)rrv read_methodrBrrrrus  z_ProxyFile._readcCs|S)z$Context management protocol support.r)rrrr __enter__sz_ProxyFile.__enter__cGs |jdS)N)rL)rexcrrr__exit__sz_ProxyFile.__exit__cCs |jjS)N)rreadable)rrrrrsz_ProxyFile.readablecCs |jjS)N)rwritable)rrrrrsz_ProxyFile.writablecCs |jjS)N)rseekable)rrrrrsz_ProxyFile.seekablecCs |jjS)N)rrI)rrrrrIsz_ProxyFile.flushcCs&t|dsdSt|jdsdS|jjS)NrTr F)rErr )rrrrr s   z_ProxyFile.closed)N)N)N)N)N)r)rwrxryrzrrSrwrnryr;rrarLrur}rrrrrIpropertyr rrrrrs$        rc@s<eZdZdZdddZddZddd Zd d Zd d ZdS)rz&A read-only wrapper of part of a file.NcCstj|||||_||_dS)zInitialize a _PartialFile.N)rr_start_stop)rrrrrrrrsz_PartialFile.__init__cCstj||jS)z*Return the position with respect to start.)rrr)rrrrrsz_PartialFile.tellrcCs<|dkr|j|_d}n|dkr*|j|_d}tj|||dS)z8Change position, possibly with respect to start or stop.rrWrUN)rrsrrra)rrzr{rrrrasz_PartialFile.seekcCsB|j|j}|dkrdS|dks0|dks0||kr4|}tj|||S)z;Read size bytes using read_method, honoring start and stop.rrN)rrsrru)rrvr|Z remainingrrrrus  z_PartialFile._readcCst|dr|`dS)Nr)rEr)rrrrrLs z_PartialFile.close)NN)r) rwrxryrzrrrarurLrrrrrs    rTc4Csd}yZtrpytj|tjtjBWnJtk rn}z.|jtjtjtjfkr\t d|j nWYdd}~XnX|r^yt |j d}|j Wn:tk r}z|jtjtjfkrdSWYdd}~XnXy`yt j|j |j dd}Wn2ttfk rt j|j |j dd}YnXt j|j Wn0tk r\t j|j t d|j YnXWn8tr|tj|tj|rt j|j dYnXdS)z(Lock file f using lockf and dot locking.Fzlockf: lock unavailable: %sNz.lockTzdot lock unavailable: %s)fcntllockfZLOCK_EXZLOCK_NBrrZEAGAINrrrrrrLrrrrrr%rr$LOCK_UN)rrZ dotlock_donerZpre_lockrrrr sH    rcCs8trtj|tjtjj|jdr4tj|jddS)z*Unlock file f using lockf and dot locking.z.lockN)rrrrrrrr$)rrrrr4src Cs8tj|tjtjBtjBd}z t|dStj|XdS)zCCreate a file if it doesn't exist and open for reading and writing.izrb+N)rrrrO_RDWRrL)rfdrrrr;s rcCs$td|ttjtjtjfS)zBCreate a temp file based on path and open for reading and writing.z %s.%s.%s.%s)rrrrrrr)rrrrrCsrcCs$|jttdr tj|jdS)z0Ensure changes to file f are physically on disk.fsyncN)rIrErrfileno)rrrrrIs rcCst||jdS)z:Close file f, ensuring all changes are physically on disk.N)rrL)rrrrrOsrc@seZdZdZdS)rz"Raised for module-specific errors.N)rwrxryrzrrrrrUsc@seZdZdZdS)rz:The specified mailbox does not exist and won't be created.N)rwrxryrzrrrrrXsc@seZdZdZdS)rz>The specified mailbox is not empty and deletion was requested.N)rwrxryrzrrrrr[sc@seZdZdZdS)rz)Another process caused an action to fail.N)rwrxryrzrrrrr^sc@seZdZdZdS)rz)A file appears to have an invalid format.N)rwrxryrzrrrrras)T)0rzrrrdrrrBrjr1Z email.messageZemail.generatorr\r-r ImportError__all__rcrMrrrrrrrrr!rr rr r r r rrrrrrrr Exceptionrrrrrrrrrsp     h8DB4-3z%mqH_c' *