
"wTc           @   s  d  Z  d g Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z d d l
 Z d d l m Z y d d l m Z WnM e k
 r Z y d d l m Z Wq e k
 r Z d d l m Z q Xn Xd d l Td d l m Z m Z m Z d d l Z e j d k  r@e j d	  n  d Z e j d e j  Z d d d  Z  d e! f d     YZ" d S(   s   The getmail Message class.

t   MessageiN(   t	   Generator(   t   Header(   t   *(   t   mbox_from_escapet   format_headert   address_no_bracketsi  i  t   sendert   received_byt   received_fromt   received_witht	   recipients
   ^(>*From )c         C   sG  t  j j   } | j d  | | k o3 d  k n rG t d   n  t j d  } d | d <d | d <t j j	 d t
  | d	 <d
 d d d |  d d d d g } | r | j g  | D] } | j   ^ q  n5 | r	| j g  | j   D] } | j   ^ q  n  | j t j j |   x t D] } t | | d  q)W| S(   NsG   failed to parse retrieved message; constructing container for contents
s-   corrupt_message() called with wrong argumentst    s   "unknown sender" <>t   Froms   Corrupt message receivedt   Subjectt	   localtimet   Dates=   A badly-corrupt message was retrieved and could not be parseds   for the following reason:s       %ss:   Below the following line is the original message contents.s>   --------------------------------------------------------------(   t   getmailcoret   loggingt   Loggert   errort   Nonet
   SystemExitt   emailt   message_from_stringt   Utilst
   formatdatet   Truet   extendt   rstript
   splitlinest   set_payloadt   ost   linesept   joint   message_attributest   setattr(   t   whyt	   fromlinest
   fromstringt   logt   msgt   bodyt   linet   attr(    (    s7   /usr/lib/python2.7/dist-packages/getmailcore/message.pyt   corrupt_message5   s0    

	)/c           B   sn   e  Z d  Z d Z d d d d  Z d	   Z d
   Z e e d  Z	 d   Z
 d   Z d   Z d d  Z RS(   s   Message class for getmail.  Does sanity-checking on attribute accesses
    and provides some convenient interfaces to an underlying email.Message()
    object.
    t   __msgt   __rawR   R   R	   R
   R   c         C   s  d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ t j j   } | r y" | j t	 j
 j |   |  _ Wn. t j j k
 r } t | d | |  _ n Xt	 j
 j |  |  _ n | ry | j |  |  _ Wn. t j j k
 r } t | d | |  _ n X| |  _ nb | r\y | j |  |  _ Wqht j j k
 rX} t | d | j   |  _ qhXn t d   t |  j d p{d  |  _ d  S(   NR&   R'   s%   Message() called with wrong argumentss   return-patht   unknown(   R   R   R   R	   R
   t   _Message__rawR   t   Parsert   parsestrR    R!   R"   t   _Message__msgt   Errorst   MessageErrorR-   t   parset   readR   R   R   (   t   selfR&   R'   t   fromfilet   parsert   o(    (    s7   /usr/lib/python2.7/dist-packages/getmailcore/message.pyt   __init__b   s4    					""c         C   s   |  j  S(   N(   R4   (   R9   (    (    s7   /usr/lib/python2.7/dist-packages/getmailcore/message.pyt   content   s    c         C   s.   x' t  D] } t |  | t | |   q Wd  S(   N(   R#   R$   t   getattr(   R9   t   othermsgR,   (    (    s7   /usr/lib/python2.7/dist-packages/getmailcore/message.pyt	   copyattrs   s    c         C   s  | r/ d t  |  j  t j   f t j } n d } t d d |  j  } |  j d =| rv t d |  j pm d  } n d } | r d |  j	 |  j
 |  j f } |  j d k	 r | d |  j 7} n  | d	 t j d
 t j    7} t d |  }	 n d }	 y t j   }
 t |
 t d  } | j |  j t  |
 j   } | rTt j d |  } n  | | | |	 t j j | j   d g  SWn` t k
 r} |  j d k rt d |   n  t | d |  j |  _ |  j | | | |  SXd S(   s  Return a string with native EOL convention.

        The email module apparently doesn't always use native EOL, so we force
        it by writing out what we need, letting the generator write out the
        message, splitting it into lines, and joining them with the platform
        EOL.
        
        Note on mangle_from: the Python email.Generator class apparently only
        quotes "From ", not ">From " (i.e. it uses mboxo format instead of
        mboxrd).  So we don't use its mangling, and do it by hand instead.
        s
   From %s %sR   s   Return-Paths   <%s>s   Delivered-ToR0   s   from %s by %s with %ss	    for <%s>s   ; s   %d %b %Y %H:%M:%S -0000t   Receivedi    s   >\1s<   failed to parse retrieved message and could not recover (%s)R'   N(   R   R   t   timet   asctimeR    R!   R   R4   R   R	   R   R
   R   t   strftimet   gmtimet	   cStringIOt   StringIOR   t   Falset   flattent   getvaluet   RE_FROMLINEt   subR"   R   t	   TypeErrorR1   t   getmailDeliveryErrorR-   (   R9   t   delivered_tot   receivedt   mangle_fromt   include_fromt   fromlinet   rplinet   dtlineR>   t   receivedlinet   tmpft   gent   strmsgR<   (    (    s7   /usr/lib/python2.7/dist-packages/getmailcore/message.pyRJ      sB    
1c         C   s    t  | j   d  |  j | <d  S(   Ns   utf-8(   R   R   R4   (   R9   t   nameR>   (    (    s7   /usr/lib/python2.7/dist-packages/getmailcore/message.pyt
   add_header   s    c         C   s   |  j  | =d  S(   N(   R4   (   R9   R[   (    (    s7   /usr/lib/python2.7/dist-packages/getmailcore/message.pyt   remove_header   s    c         C   s
   |  j  j S(   N(   R4   t   _headers(   R9   (    (    s7   /usr/lib/python2.7/dist-packages/getmailcore/message.pyt   headers   s    c         C   s   |  j  j | |  S(   N(   R4   t   get_all(   R9   R[   t   failobj(    (    s7   /usr/lib/python2.7/dist-packages/getmailcore/message.pyR`      s    (   s   __msgs   __raws   senders   received_bys   received_froms   received_withs	   recipientN(   t   __name__t
   __module__t   __doc__t	   __slots__R   R=   R>   RA   RI   RJ   R\   R]   R_   R`   (    (    (    s7   /usr/lib/python2.7/dist-packages/getmailcore/message.pyR    S   s"         (		<			(   s   senders   received_bys   received_froms   received_withs	   recipient(#   Rd   t   __all__t   sysR    RC   RG   t   reR   t   email.Errorst   email.Utilst   email.Parsert   email.GeneratorR   t   email.headerR   t   ImportErrorR<   t   email.Headert   getmailcore.exceptionst   getmailcore.utilitiesR   R   R   t   getmailcore.loggingR   t
   hexversiont   setrecursionlimitR#   t   compilet	   MULTILINERL   R   R-   t   objectR    (    (    (    s7   /usr/lib/python2.7/dist-packages/getmailcore/message.pyt   <module>   s>   	
    