³ò
®µGc           @   s  d  Z  d d d d d d d d d	 d
 d d d d d d d g Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k	 Z	 d d k
 Z
 d d k Z d d k Td Z h  e d <e d <e d <e d <e d <e d <e d <e d <Z d „  Z d „  Z d  d! „ Z d e f d" „  ƒ  YZ d e f d# „  ƒ  YZ d4 d5 d% „ Z d& „  Z d' „  Z d d  d( „ Z d) „  Z d* „  Z d+ „  Z d, „  Z d- „  Z  d d d d. „ Z! d d d d/ „ Z" d0 „  Z# d1 „  Z$ d2 „  Z% d3 „  Z& d S(6   s+   Utility classes and functions for getmail.
t   address_no_bracketst   change_usergroupt   change_uidgidt   check_ssl_key_and_certt   deliver_maildirt	   eval_boolt   expand_user_varst
   is_maildirt   localhostnamet	   lock_filet   logfilet   mbox_from_escapet	   safe_opent   unlock_filet
   gid_of_uidt   uid_of_usert
   updatefileiÿÿÿÿN(   t   *s   %Y-%m-%d %H:%M:%St   truet   yest   ont   1t   falset   not   offt   0c         C   sg   | d j p t  d | ‚ | d j o t i |  t i ƒ n% | d j o t i |  t i ƒ n d S(   s   Do file locking.t   lockft   flocks   unknown lock type %sN(   s   lockfs   flock(   t   AssertionErrort   fcntlR   t   LOCK_EXR   (   t   filet   locktype(    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR	   4   s
    c         C   sg   | d j p t  d | ‚ | d j o t i |  t i ƒ n% | d j o t i |  t i ƒ n d S(   s   Do file unlocking.R   R   s   unknown lock type %sN(   s   lockfs   flock(   R   R   R   t   LOCK_UNR   (   R   R    (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR   =   s
    i€  c         C   s   t  i d j o t |  | ƒ Sn y< t  i |  t  i t  i Bt  i B| ƒ } t  i | | ƒ } Wn+ t j
 o } t d |  | f ƒ ‚ n X| S(   s   Open a file path safely.
    t   posixs   failure opening %s (%s)(	   t   ost   namet   opent   O_RDWRt   O_CREATt   O_EXCLt   fdopent   OSErrort   getmailDeliveryError(   t   patht   modet   permissionst   fdR   t   o(    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR   F   s    &c           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s7  A class for atomically updating files.

    A new, temporary file is created when this class is instantiated. When the
    object's close() method is called, the file is synced to disk and atomically
    renamed to replace the original file.  close() is automatically called when
    the object is deleted.
    c         C   sà   t  |  _ | |  _ | d t i ƒ  |  _ xB t i i | ƒ o. t i i t i i	 | ƒ t i
 | ƒ ƒ } q, Wy t |  i d ƒ } Wn4 t j
 o( \ } } t d | |  i f ƒ ‚ n X| |  _ | i |  _ | i |  _ d  S(   Ns   .tmp.%dt   wbs   %s, opening output file "%s"(   t   Falset   closedt   filenameR#   t   getpidt   tmpnameR,   t   islinkt   joint   dirnamet   readlinkR   t   IOErrorR   t   writet   flush(   t   selfR4   t   ft   codet   msg(    (    s,   /usr/share/getmail4/getmailcore/utilities.pyt   __init__[   s    		 	c         C   s   |  i  ƒ  d  S(   N(   t   close(   R>   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyt   __del__n   s    c         C   sH   y% t  |  d ƒ o |  i i ƒ  n Wn t j
 o n Xt |  _ d  S(   NR   (   t   hasattrR   RC   R;   t   TrueR3   (   R>   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyt   abortq   s    c         C   s`   |  i  p t |  d ƒ o d  Sn |  i i ƒ  |  i i ƒ  t i |  i |  i ƒ t	 |  _  d  S(   NR   (
   R3   RE   R   R=   RC   R#   t   renameR6   R4   RF   (   R>   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyRC   y   s    (   t   __name__t
   __module__t   __doc__RB   RD   RG   RC   (    (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR   S   s
   			c           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sL   A class for locking and appending timestamped data lines to a log file.
    c         C   sl   t  |  _ | |  _ y t t |  i ƒ d ƒ |  _ Wn4 t j
 o( \ } } t d | |  i f ƒ ‚ n Xd  S(   Nt   abs   %s, opening file "%s"(   R2   R3   R4   R%   R   R   R;   (   R>   R4   R@   RA   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyRB   …   s    		c         C   s   |  i  ƒ  d  S(   N(   RC   (   R>   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyRD      s    c         C   s   d |  i  S(   Ns   logfile(filename="%s")(   R4   (   R>   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyt   __str__   s    c         C   s9   |  i  o d  Sn |  i i ƒ  |  i i ƒ  t |  _  d  S(   N(   R3   R   R=   RC   RF   (   R>   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyRC   “   s
    
c         C   sƒ   zk t  |  i d ƒ |  i i d d ƒ |  i i t i t t i ƒ  ƒ d | i ƒ  t	 i
 ƒ |  i i ƒ  Wd  t |  i d ƒ Xd  S(   NR   i    i   t    (   R	   R   t   seekR<   t   timet   strftimet   logtimeformatt	   localtimet   rstripR#   t   linesepR=   R   (   R>   t   s(    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR<   š   s    	.(   RI   RJ   RK   RB   RD   RM   RC   R<   (    (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR
   ‚   s   				t   passwordc         C   s‘   d } |  i  ƒ  } | i ƒ  xn | D]f } | | j o q# n | o | d 7} n | | j o | d | 7} q# | d | |  | f 7} q# W| S(   sA   Take a dictionary of parameters and return a string summary.
    t    t   ,s   %s=*s   %s="%s"(   t   keyst   sort(   t   dt	   maskitemst	   skipitemsRV   RZ   t   key(    (    s,   /usr/share/getmail4/getmailcore/utilities.pyt   format_params¦   s    
 c          G   s   t  d ƒ ‚ d S(   sG   Handle an alarm during maildir delivery.

    Should never happen.
    s   Delivery timeoutN(   R+   (   t   unused(    (    s,   /usr/share/getmail4/getmailcore/utilities.pyt   alarm_handler¸   s    c         C   s  t  i i |  i d ƒ o |  d  p |  ƒ } t  i | t  i ƒ p t d |  ƒ ‚ n t  i i |  ƒ p t Sn t  i |  t  i ƒ p t d |  ƒ ‚ n xh d	 D]` } t  i i	 |  | ƒ } t  i i | ƒ p t Sn t  i | t  i
 ƒ p t d |  ƒ ‚ q£ q£ Wt S(
   s    Verify a path is a maildir.
    t   /iÿÿÿÿsP   cannot read contents of parent directory of %s - check permissions and ownershipsF   cannot read contents of directory %s - check permissions and ownershipt   tmpt   curt   news<   cannot write to maildir %s - check permissions and ownership(   Rd   Re   s   new(   R#   R,   R9   t   endswitht   accesst   X_OKt   getmailConfigurationErrort   isdirR2   R8   t   W_OKRF   (   R\   t
   dir_parentt   subt   subdir(    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR   À   s*    - c         C   s¹  t  |  ƒ p t d |  ƒ ‚ n t i t i t ƒ t i d& ƒ h  | d <| i d ƒ d i d d ƒ i d	 d
 ƒ d <t i	 ƒ  d <} t i
 i |  d ƒ } t i
 i |  d ƒ } x®t d ƒ D]‡} t i ƒ  }	 t |	 ƒ | d <t |	 t |	 ƒ d ƒ | d <d | | d <| d d' j	 o | d c d | 7<n yX | d c d d i g  }
 t d d ƒ i d ƒ D] } |
 d t | ƒ qn~
 ƒ 7<Wn t j
 o n Xd | } t i
 i | | ƒ } t i
 i | | ƒ } t i
 i | ƒ o t i d ƒ qÄ n t i
 i |  d | ƒ d } t i | ƒ } | o t d  | ƒ ‚ n PqÄ Wt i d ƒ t d! ƒ ‚ t i |  ƒ } yJ t | d" | ƒ } | i | ƒ | i ƒ  t i | i ƒ  ƒ | i ƒ  Wn8 t j
 o, } t i d ƒ t d# | | f ƒ ‚ n Xy! t i | | ƒ t i  | ƒ Wnr t! j
 of t i d ƒ y t i  | ƒ Wn( t" j
 o
 ‚  n t j
 o n Xt d$ | | f ƒ ‚ n Xt i d ƒ t i t i t i# ƒ | S((   s0  Reliably deliver a mail message into a Maildir.  Uses Dan Bernstein's
    documented rules for maildir delivery, and the updated naming convention
    for new files (modern delivery identifiers).  See
    http://cr.yp.to/proto/maildir.html and
    http://qmail.org/man/man5/maildir.html for details.
    s   not a Maildir (%s)i   i<   t   deliverycountt   .i    Rc   s   \057t   :s   \072t   hostnamet   pidRd   Rf   i   t   secsi@B t   usecss   M%(usecs)dP%(pid)st   uniques   Q%(deliverycount)ss   R%sRX   s   /dev/urandomt   rbi   s   %02xs    %(secs)s.%(unique)s.%(hostname)si   Re   s   :*s   collision with %ss"   failed to allocate file in maildirR1   s   failure writing file %s (%s)s   failure renaming "%s" to "%s"i   i€Q N($   R   R+   t   signalt   SIGALRMRb   t   alarmt   splitt   replaceR#   R5   R,   R8   t   rangeRP   t   intt   NoneR%   t   readt   ordt   StandardErrort   existst   sleept   globt   statR   R<   R=   t   fsynct   filenoRC   R;   t   linkt   unlinkR*   t   KeyboardInterruptt   SIG_DFL(   t   maildirpatht   dataRs   t   dcountt   filemodet   infot   dir_tmpt   dir_newRa   t   tt   _[1]t   charR4   t	   fname_tmpt	   fname_newt   curpatt	   collisiont	   s_maildirR?   R0   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR   Ü   s€    	 >

c         C   sC   d i  g  } |  D]! } | | d j o d p | q ~ ƒ p d S(   sA   Escape spaces, tabs, and newlines in the envelope sender address.RX   RN   s   	s   
t   -s   <>(   RN   s   	s   
(   R8   (   RV   R–   t   c(    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR   C  s    c         C   s7   |  i  d ƒ o |  i d ƒ o |  d d !Sn |  Sd S(   s5   Strip surrounding <> on an email address, if present.t   <t   >i   iÿÿÿÿN(   t
   startswithRg   (   t   addr(    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR    H  s     c         C   sB   y t  t |  ƒ i ƒ  SWn# t j
 o t d |  ƒ ‚ n Xd S(   s)   Handle boolean values intelligently.
    sE   boolean parameter requires value to be one of true or false, not "%s"N(   t   _bool_valuest   strt   lowert   KeyErrorRj   (   RV   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR   P  s
    c         C   s@   y t  i |  ƒ i SWn% t j
 o } t d | ƒ ‚ n Xd  S(   Ns   no such specified uid (%s)(   t   pwdt   getpwuidt   pw_gidR¦   Rj   (   t   uidR0   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR   Z  s    c         C   s@   y t  i |  ƒ i SWn% t j
 o } t d | ƒ ‚ n Xd  S(   Ns   no such specified user (%s)(   R§   t   getpwnamt   pw_uidR¦   Rj   (   t   userR0   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR   a  s    c         C   s¸   d } d } | o^ |  o |  i d | ƒ n y t i | ƒ i } Wqq t j
 o } t d | ƒ ‚ qq Xn | o, |  o |  i d | ƒ n t | ƒ } n t |  | | ƒ d S(   s]   
    Change the current effective GID and UID to those specified by user and
    _group.
    s#   Getting GID for specified group %s
s   no such specified group (%s)s"   Getting UID for specified user %s
N(	   R€   t   debugt   grpt   getgrnamt   gr_gidR¦   Rj   R   R   (   t   loggerR­   t   _groupRª   t   gidR0   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR   h  s    c         C   sÕ   y  | oG t  i ƒ  | j o0 |  o |  i d | ƒ n t  i | | ƒ qQ n | oG t  i ƒ  | j o0 |  o |  i d | ƒ n t  i | | ƒ qŸ n Wn. t j
 o" } t d | | | f ƒ ‚ n Xd S(   sY   
    Change the current effective GID and UID to those specified by uid
    and gid.
    s   Setting egid to %d
s   Setting euid to %d
s#   change UID/GID to %s/%s failed (%s)N(   R#   t   getegidR®   t   setregidt   geteuidt   setreuidR*   R+   (   R²   Rª   R´   R0   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR   ~  s    c         C   s/  d } |  i  ƒ  d d i g  } | i ƒ  D] } | | i  ƒ  q* ~ ƒ } x  | o˜ t | ƒ d j o… | i d d d ƒ } | d j o% | i d ƒ } | d j o Pq® n | o | t i d 7} n | | |  7} | | i ƒ  } qM W| o | t i d 7} n | o | | i  ƒ  t i 7} n | S(   s?   Take a long line and return rfc822-style multiline header.
    RX   s   : RN   iN   i    iÿÿÿÿs     (   t   stripR8   t
   splitlinest   lent   rfindR#   RU   t   lstrip(   R$   t   linet   headerR–   t   partt   i(    (    s,   /usr/share/getmail4/getmailcore/utilities.pyt   format_header“  s$    D 	c         C   s   t  i i t  i i |  ƒ ƒ S(   s‰   Return a string expanded for both leading "~/" or "~username/" and
    environment variables in the form "$varname" or "${varname}".
    (   R#   R,   t
   expandusert
   expandvars(   RV   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR   ®  s    c          C   s+   t  i ƒ  }  d |  j o |  Sn t  i ƒ  S(   sI   Return a name for localhost which is (hopefully) the "correct" FQDN.
    Rq   (   t   sockett   gethostnamet   getfqdn(   t   n(    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR   µ  s    c         C   sÕ   |  d } | d  j	 o t | ƒ } n |  d } | d  j	 o t | ƒ } n | o$ t i i | ƒ o t d ƒ ‚ n | o$ t i i | ƒ o t d ƒ ‚ n | d  j | d  j Ao t d ƒ ‚ n | | f S(   Nt   keyfilet   certfiles-   optional keyfile must be path to a valid files.   optional certfile must be path to a valid files7   optional certfile and keyfile must be supplied together(   R€   R   R#   R,   t   isfileRj   (   t   confRÉ   RÊ   (    (    s,   /usr/share/getmail4/getmailcore/utilities.pyR   ¾  s    

(   RW   (    ('   RK   t   __all__R#   t   os.pathRÅ   Ry   R‡   RP   R†   R   R§   R¯   t   getmailcore.exceptionsRR   RF   R2   R£   R	   R   R   t   objectR   R
   R`   Rb   R   R€   R   R   R    R   R   R   R   R   RÂ   R   R   R   (    (    (    s,   /usr/share/getmail4/getmailcore/utilities.pys   <module>   st   	
											/$		g			
						