ó
:ü½cc           @   s_  d  Z  d d l Z d d l m 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 m Z d d l m Z d d l m Z d	 Z e j d
 e j ƒ Z e g  e j ƒ  j d ƒ D] Z e e ƒ ^ qÜ ƒ Z d e j j f d „  ƒ  YZ d e j j f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s¢   Standard Mailman message object.

This is a subclass of email.Message but provides a slightly extended interface
which is more convenient for use inside Mailman.
iÿÿÿÿN(   t   StringIO(   t   Charset(   t   Header(   t   ListTypet
   StringType(   t   mm_cfg(   t   Utilss   , s   ([\d.]+)t   .t	   Generatorc           B   s)   e  Z d  Z e d d d „ Z d „  Z RS(   s…   Generates output from a Message object tree, keeping signatures.

       Headers will by default _not_ be folded in attachments.
    iN   i    c         C   s/   t  j j j |  | d | d | ƒ| |  _ d  S(   Nt   mangle_from_t   maxheaderlen(   t   emailR   t   __init__t!   _Generator__children_maxheaderlen(   t   selft   outfpR	   R
   t   children_maxheaderlen(    (    s.   /export/web/mailman/mailman/Mailman/Message.pyR   3   s    c         C   s   |  j  | |  j |  j |  j ƒ S(   s7   Clone this generator with maxheaderlen set for children(   t	   __class__t   _mangle_from_R   (   R   t   fp(    (    s.   /export/web/mailman/mailman/Mailman/Message.pyt   clone9   s    (   t   __name__t
   __module__t   __doc__t   TrueR   R   (    (    (    s.   /export/web/mailman/mailman/Mailman/Message.pyR   .   s   t   Messagec           B   s\   e  Z d  „  Z d „  Z d „  Z d d d „ Z d d d „ Z d d „ Z e	 e
 d „ Z RS(	   c         C   s    t  |  _ t j j j |  ƒ d  S(   N(   t   VERSIONt   __version__R   R   R   (   R   (    (    s.   /export/web/mailman/mailman/Mailman/Message.pyR   A   s    	c         C   s
   |  j  ƒ  S(   N(   t   __str__(   R   (    (    s.   /export/web/mailman/mailman/Mailman/Message.pyt   __repr__G   s    c         C   sA  | |  _  | j d d ƒ } t | d <| t k r5 d  S| j d ƒ sP d  |  _ n  | j d ƒ sk d |  _ n  g  } d } x± |  j D]¦ \ } } t | t	 ƒ rg  } d } xN | j
 D]C \ }	 }
 t |
 t ƒ râ t |
 ƒ }
 d } n  | j |	 |
 f ƒ q² W| r| | _
 d } qn  | j | | f ƒ q W| r=| |  _ n  d  S(   NR   i    t   _charsett   _default_types
   text/plaini   (   i    i    i    (   t   __dict__t   getR   t   has_keyt   NoneR   R   t   _headerst
   isinstanceR   t   _chunksR   R   t   append(   R   t   dt   versiont   headerst   hchangedt   kt   vt   chunkst   cchangedt   st   charset(    (    s.   /export/web/mailman/mailman/Mailman/Message.pyt   __setstate__J   s4    	
		i    c         C   sø   t  j } | d k	 r | } n  | r- d } n d } x® | D]{ } |  | } | sV q: n  d j | j ƒ  ƒ } t j j | g ƒ } y | d \ } }	 Wn t k
 rª q: n X|	 r: Pq: q: W|  j	 ƒ  }
 |
 rÞ |
 j
 ƒ  d }	 n d }	 | sô |	 j ƒ  S|	 S(	   sÆ  Return the address considered to be the author of the email.

        This can return either the From: header, the Sender: header or the
        envelope header (a.k.a. the unixfrom header).  The first non-empty
        header value found is returned.  However the search order is
        determined by the following:

        - If mm_cfg.USE_ENVELOPE_SENDER is true, then the search order is
          Sender:, From:, unixfrom

        - Otherwise, the search order is From:, Sender:, unixfrom

        The optional argument use_envelope, if given overrides the
        mm_cfg.USE_ENVELOPE_SENDER setting.  It should be set to either 0 or 1
        (don't use None since that indicates no-override).

        unixfrom should never be empty.  The return address is always
        lowercased, unless preserve_case is true.

        This method differs from get_senders() in that it returns one and only
        one address, and uses a different search order.
        t   sendert   fromt    i    i   N(   R3   R4   (   R4   R3   (   R   t   USE_ENVELOPE_SENDERR#   t   joint
   splitlinesR   R   t   getaddressest
   IndexErrort   get_unixfromt   splitt   lower(   R   t   use_envelopet   preserve_caset   senderfirstR*   t   ht   fieldvalt   addrst   realnamet   addresst   unixfrom(    (    s.   /export/web/mailman/mailman/Mailman/Message.pyt
   get_senders   s2    			

c         C   s2  | d k r t j } n  g  } x½ | D]µ } | d k r |  j ƒ  pF d } y! | j d | j ƒ  d f ƒ WqÚ t k
 r} qÚ Xq% |  j | ƒ } | r% g  | D] } d j | j	 ƒ  ƒ ^ q } | j
 t j j | ƒ ƒ q% q% Wg  } xG | D]? }	 |	 d }
 |
 d k	 r| r|
 j ƒ  }
 n  | j |
 ƒ që W| S(   s  Return a list of addresses representing the author of the email.

        The list will contain the following addresses (in order)
        depending on availability:

        1. From:
        2. unixfrom
        3. Reply-To:
        4. Sender:

        The return addresses are always lower cased, unless `preserve_case' is
        true.  Optional `headers' gives an alternative search order, with None
        meaning, search the unixfrom header.  Items in `headers' are field
        names without the trailing colon.
        R5   i   N(   R#   R   t   SENDER_HEADERSR;   R'   R<   R:   t   get_allR7   R8   t   extendR   R   R9   R=   (   R   R?   R*   t   pairsRA   RB   t	   fieldvalst   fvt   authorst   pairRE   (    (    s.   /export/web/mailman/mailman/Mailman/Message.pyt   get_senders³   s,    !% 
c         C   sB   y  t  j j j |  | ƒ } | SWn t t t f k
 r= | SXd S(   s   Some MUA have bugs in RFC2231 filename encoding and cause
        Mailman to stop delivery in Scrubber.py (called from ToDigest.py).
        N(   R   R   t   get_filenamet   UnicodeErrort   LookupErrort
   ValueError(   R   t   failobjt   filename(    (    s.   /export/web/mailman/mailman/Mailman/Message.pyRQ   ß   s
    c         C   s8   t  ƒ  } t | d | ƒ} | j |  d | ƒ| j ƒ  S(   sù   Return entire formatted message as a string using
        Mailman.Message.Generator.

        Operates like email.Message.Message.as_string, only
        using Mailman's Message.Generator class. Only the top headers will
        get folded.
        R	   RF   (   R    R   t   flattent   getvalue(   R   RF   R	   R   t   g(    (    s.   /export/web/mailman/mailman/Mailman/Message.pyt	   as_stringê   s    	N(   R   R   R   R   R2   R#   RG   RP   RQ   t   FalseR   RZ   (    (    (    s.   /export/web/mailman/mailman/Mailman/Message.pyR   @   s   			)@,t   UserNotificationc           B   s5   e  Z d  Z d d d d „ Z e d „ Z d „  Z RS(   s&   Class for internally crafted messages.c         C   sÜ   t  j |  ƒ d  } | d  k	 r7 t t j | ƒ ƒ } n  | d  k	 rV |  j | | ƒ n  | d  k rk d } n  t | | d d d d ƒ|  d <| |  d <t | t	 ƒ rÂ t
 j | ƒ |  d <| |  _ n | |  d <| g |  _ d  S(   Ns   (no subject)t   header_namet   Subjectt   errorst   replacet   Fromt   To(   R   R   R#   R   R   t
   GetCharSett   set_payloadR   R%   R   t
   COMMASPACER7   t   recips(   R   t   recipR3   t   subjectt   textt   langR1   (    (    s.   /export/web/mailman/mailman/Mailman/Message.pyR   ü   s     	

c         K   s®   |  j  d ƒ s% t j | ƒ |  d <n  |  j  d ƒ sP t j j d d ƒ |  d <n  |  j  d ƒ pb | sš |  j d ƒ | j ƒ  k r d	 |  d
 <qš d |  d
 <n  |  j | |  d S(   s|   Sends the message by enqueuing it to the `virgin' queue.

        This is used for all internally crafted messages.
        s
   message-ids
   Message-IDt   datet	   localtimei   t   Datet
   precedencet   tot   listt
   Precedencet   bulkN(   R"   R   t   unique_message_idR   t
   formatdateR!   t   GetOwnerEmailt   _enqueue(   R   t   mlistt   noprecedencet   _kws(    (    s.   /export/web/mailman/mailman/Mailman/Message.pyt   send  s    c         K   sT   d d l  m } | t j ƒ } | j |  d | j ƒ  d |  j d d d d | d  S(   Niÿÿÿÿ(   t   get_switchboardt   listnameRf   t
   nodecoratei   t   reduced_list_headers(   t   Mailman.Queue.sbcacheR{   R   t   VIRGINQUEUE_DIRt   enqueuet   internal_nameRf   (   R   Rw   Ry   R{   t   virginq(    (    s.   /export/web/mailman/mailman/Mailman/Message.pyRv   (  s    	N(   R   R   R   R#   R   R[   Rz   Rv   (    (    (    s.   /export/web/mailman/mailman/Mailman/Message.pyR\   ù   s   t   OwnerNotificationc           B   s)   e  Z d  Z d d d d „ Z d „  Z RS(   sB   Like user notifications, but this message goes to the list owners.i   c         C   s‹   | j  } | r# | j | j ƒ n  t j | j d ƒ } | j } t j |  | | | | | ƒ |  d =| j	 ƒ  |  d <| |  _
 d |  d <d  S(   Nt   bouncesRo   Rb   Rp   Rq   (   t   ownerRJ   t	   moderatorR   t   get_site_emailt	   host_namet   preferred_languageR\   R   Ru   t   _sender(   R   Rw   Rh   Ri   t   tomoderatorsRf   R3   Rj   (    (    s.   /export/web/mailman/mailman/Mailman/Message.pyR   9  s    
		c         K   s]   d d l  m } | t j ƒ } | j |  d | j ƒ  d |  j d d d d d |  j | d  S(	   Niÿÿÿÿ(   R{   R|   Rf   R}   i   R~   t	   envsender(   R   R{   R   R€   R   R‚   Rf   R‹   (   R   Rw   Ry   R{   Rƒ   (    (    s.   /export/web/mailman/mailman/Mailman/Message.pyRv   M  s    	N(   R   R   R   R#   R   Rv   (    (    (    s.   /export/web/mailman/mailman/Mailman/Message.pyR„   6  s   (    R   t   ret	   cStringIOR    R   t   email.Generatort   email.Messaget   email.Utilst   email.CharsetR   t   email.HeaderR   t   typesR   R   t   MailmanR   R   Re   t   matchR   t   mot   tuplet   groupR<   R0   t   intR   R   R   R\   R„   (    (    (    s.   /export/web/mailman/mailman/Mailman/Message.pyt   <module>   s$   4¹=