ó
Iü½cc           @   s4  d  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 Z d d l 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 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 m Z d d l m  Z  d d l! m" Z" e j# d  Z$ e j# d  Z% e j# d  Z& d Z' d Z( y e) e* f Wn e+ k
 rŖd Z) d Z* n Xy d d l, m- Z- Wn) e. k
 rźd d l, Z, e) d  Z- n Xd   Z/ d   Z0 d   Z1 d   Z2 d d  Z4 d    Z5 e) d!  Z6 d S("   s    Cleanse a message for archiving.i’’’’(   t   nested_scopesN(   t   StringIO(   t   IntTypet
   StringType(   t	   parsedate(   t   HeaderParser(   t	   Generator(   t   Charset(   t   mm_cfg(   t   Utils(   t   LockFile(   t   Message(   t   DiscardMessage(   t   _(   t   syslog(   t   sha_news   [/\\:]s   [^-\w.]s   ^\.*s   <br>
t    i   i    (   t   guess_all_extensionsc            sO   g       f d   } | t  j  | rK t t  d  rK | t  j  n    S(   Nc            s=   x6 |  j    D]( \ } } |  k r   j |  q q Wd  S(   N(   t   itemst   append(   t   mapt   et   t(   t   allt   ctype(    s8   /export/web/mailman/mailman/Mailman/Handlers/Scrubber.pyt   checkF   s    t   common_types(   t	   mimetypest	   types_mapt   hasattrR   (   R   t   strictR   (    (   R   R   s8   /export/web/mailman/mailman/Mailman/Handlers/Scrubber.pyR   C   s    c         C   sV   t  |  d t } | | k r" | S|  j d k r5 d S|  j d k rH d S| oU | d S(   NR   s   application/octet-streams   .bins
   text/plains   .txti    (   R   t   Falset   lower(   R   t   extR   (    (    s8   /export/web/mailman/mailman/Mailman/Handlers/Scrubber.pyt   guess_extensionR   s    c         C   s6   y t  j |  |  SWn t t t f k
 r1 d  SXd  S(   N(   t   timet   strftimet	   TypeErrort
   ValueErrort   OverflowErrort   None(   t   fmtR   (    (    s8   /export/web/mailman/mailman/Mailman/Handlers/Scrubber.pyt   safe_strftimec   s    c         C   sŽ  d } | j  d  } | r* t |  } n! t j | j  d t j     } t | |  } | s | j  d  } | r t | |  } q n  | sn| j   j   } y i d d 6d d 6d	 d
 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6j  | d	 d  } t | d  }	 t | d  }
 Wn% t t	 f k
 rWd } }	 }
 n Xd |
 | |	 f } n  | szt
  | d } | d  k rŖt j |   } | d  <n  t |  j   } t j j d! | | d  | d"  S(#   Ns   %Y%m%dt   Datet   received_times   X-List-Received-Datei   t   Jani   t   Febi   t   Mari   t   Apri   t   Mayi   t   Juni   t   Juli   t   Augi	   t   Sepi
   t   Octi   t   Novi   t   Deci    s   %04d%02d%02ds
   message-ids
   Message-IDt   attachmentsiü’’’(   t   getR   R#   t   gmtimeR*   t   get_unixfromt   splitt   intt
   IndexErrorR&   t   AssertionErrorR(   R	   t   unique_message_idR   t	   hexdigestt   ost   patht   join(   t   mlistt   msgt   msgdataR)   t   datestrt   nowt   datedirt   partst   montht   dayt   yeart   msgidt   digest(    (    s8   /export/web/mailman/mailman/Mailman/Handlers/Scrubber.pyt   calculate_attachments_dirj   s6    !--
c         C   sC   |  d =|  d =t  | t  r/ | j d  } n  |  j | |  d  S(   Ns   content-types   content-transfer-encodings   us-ascii(   t
   isinstancet   unicodet   encodet   set_payload(   RG   t   textt   charset(    (    s8   /export/web/mailman/mailman/Mailman/Handlers/Scrubber.pyt   replace_payload_by_text   s
    c      	   C   s®  t  j } t } | d  k r$ i  } n  | r: |  j s: d  Sn  t |  | |  } d  } t j |  j  } t	 |  j
 pv | } d  }	 }
 xh| j   D]Z} | j   } | d k r| d  k rź | j |  } | j d  }	 | j d  }
 n  | j d d  j   d k rä| j   rät j d  } z t |  | |  } Wd  t j |  X| j t d   } t j | |  } t | t d	  |  qänV| d
 k rt | t  r| d k rŻ| rÄt  n  t | t d  |  qä| d k rģqä| d k rMt j d  } z t |  | | d t } Wd  t j |  Xt | t d  |  qät j | j d t   } d   } g  | j d  D] } | |  ^ q} d t j  |  d } | j! |  | d =t j d  } z t |  | | d t } Wd  t j |  Xt | t d  |  nĖ| d k rļ| j d  } t j d  } z t |  | |  } Wd  t j |  X| j d t d   } t j | |  } | j d t d   } | j d t d   } t" t# |   } t | t d  |  nõ | j   rä| j$   rä| j d t  } | j   } | d  k r8q n  t" |  } t j d  } z t |  | |  } Wd  t j |  X| j d t d   } t j | |  } | j t d   } t j | |  } t | t d  |  n  t } q W| j$   rŖ| s| d  k r| } n t	 |  j
 p(| } g  } xŌ| j   D]Ę} | j   s>| j$   rcq>n  | j   } | d k r¬| d
 k s| d k r¬| j% t d!   q>n  y | j d t  pÄd } Wn, t& j' t( f k
 rö| j   pšd } n X| j)   } | rt# |  } n | j   } | rÉ| | k rÉy t* | | d"  } Wn/ t+ t, t- t. f k
 r}t* | d# d"  } n Xy | j/ | d"  } WqÉt+ t, t- t. f k
 rÅ| j/ | d"  } qÉXn  t | t0  r>| j1 d  sō| d 7} n  | j% |  q>q>Wt d$  } y( t* | | d"  } | j/ | d"  } Wn t+ t, t- t. f k
 r[n Xt | | j  |  |  |	 r| j2 d% |	  n  |
 rŖ| j2 d& |
  qŖn  | S('   Ns
   text/plaint   formatt   delsps   content-dispositiont    t
   attachmenti   s   not availablesY   An embedded and charset-unspecified text was scrubbed...
Name: %(filename)s
URL: %(url)s
s	   text/htmli    s$   HTML attachment scrubbed and removedi   t   filter_htmls0   An HTML attachment was scrubbed...
URL: %(url)s
t   decodec         S   s   |  j  d  j d d  S(   Ni   R   s   &nbsp;(   t
   expandtabst   replace(   t   s(    (    s8   /export/web/mailman/mailman/Mailman/Handlers/Scrubber.pyt	   doreplaceü   s    s   
s   <tt>
s   
</tt>
s   content-transfer-encodings   message/rfc822t   subjects
   no subjectt   dates   no datet   froms   unknown sendersr   An embedded message was scrubbed...
From: %(who)s
Subject: %(subject)s
Date: %(date)s
Size: %(size)s
URL: %(url)s
s   content-descriptionsz   A non-text attachment was scrubbed...
Name: %(filename)s
Type: %(ctype)s
Size: %(size)d bytes
Desc: %(desc)s
URL: %(url)s
s   us-asciis&   Skipped content of type %(partctype)s
Ra   t   asciis(   -------------- next part --------------
t   Formatt   DelSp(3   R   t   ARCHIVE_HTML_SANITIZERt   TrueR(   t   scrub_nondigestRR   R	   t
   GetCharSett   preferred_languageR   t   output_charsett   walkt   get_content_typet   get_content_charsett	   get_paramR:   R    RC   t   umaskt   save_attachmentt   get_filenameR   t   onelineRY   RS   R   R   R   t   websafet   get_payloadR=   t   BRRE   RV   t   lent   strt   is_multipartR   t   binasciit   ErrorR%   t   get_charsetRT   t   UnicodeErrort   LookupErrorR&   R@   RU   R   t   endswitht	   set_param(   RF   RG   RH   t   sanitizet   outert   dirRX   t   lcsett	   lcset_outRZ   R[   t   partR   t   omaskt   urlt   filenamet   payloadRc   Rb   t   linest   submsgRd   Re   t   whot   sizet   descRW   t	   partctypeR   t   partcharsett   sep(    (    s8   /export/web/mailman/mailman/Mailman/Handlers/Scrubber.pyt   process¢   s   			
			
		(			
	


c         C   se   y3 t  j |  d  d   } t  j j |  | d   Wn+ t k
 r` } | j t j k ra   qa n Xd  S(   Niż  c         S   s   t  j | d  d  S(   Niż  (   RC   t   chmod(   t   argt   dirnamet   names(    (    s8   /export/web/mailman/mailman/Mailman/Handlers/Scrubber.pyt   twiddle¢  s    (   RC   t   makedirsRD   Rp   R(   t   OSErrort   errnot   EEXIST(   R   R   R   (    (    s8   /export/web/mailman/mailman/Mailman/Handlers/Scrubber.pyR     s    	 c         C   sr  t  j j |  j   |  } t |  | j d t  } | j   } t j	 |  j
  } t j | j d  |  } t  j j |  \ } }	 t j r© |	 p£ t | |	  }
 n t | |	  }
 |
 sÜ | d k rÓ d }
 qÜ d }
 n  t j d |
  }
 d  } t  j j | d  } t j |  } | j   zÄ | s5t j r>d } nG t j |  } | d } t j d |  } t j d |  } | d	  } d
 } d } xQ t rät  j j | | | |
  } t  j j |  rą| d 7} d | } qPqWWd  | j   X| rŃ| d k rŃt  j j |  \ } }
 | d |
 } t | d  } zr | j |  | j   t j i | d 6} t  j  | d  } | j!   } | j   } | r¬t" d d |  n  Wd  t  j# |  Xd }
 | d } n0 | d k r| j   } t j$ t% |   } n  t | d  } | j |  | j   |  j&   } | d d k rP| d 7} n  d | d | | | |
 f } | S(   NR_   R\   s   message/rfc822s   .txts   .bins   attachments.lockR]   i’’’’iš   i    i   s   -%04ds	   text/htmls   -tmpt   wR   t   rt   errors.   HTML sanitizer exited with non-zero status: %st   /t   <s
   %s/%s%s%s>('   RC   RD   RE   t   archive_dirR   Ry   Rk   Rq   R	   Rm   Rn   Rw   Rv   t   splitextR   t*   SCRUBBER_USE_ATTACHMENT_FILENAME_EXTENSIONR"   t   sret   subR(   R
   t   lockt%   SCRUBBER_DONT_USE_ATTACHMENT_FILENAMEt   preR=   t   dret   existst   unlockt   opent   writet   closeRj   t   popent   readR   t   unlinkRx   R|   t   GetBaseArchiveURL(   RF   RG   R   R^   t   fsdirt   decodedpayloadR   R   R   t   fnextR!   RD   t   lockfileR«   t   filebaseRL   t   countert   extrat   baset   tmppatht   fpt   cmdt   progfpt   statusR   t   baseurlR   (    (    s8   /export/web/mailman/mailman/Mailman/Handlers/Scrubber.pyRu   Ŗ  s    
			
	

	
	

(7   t   __doc__t
   __future__R    RC   t   reR#   R   R~   t   tempfilet	   cStringIOR   t   typesR   R   t   email.UtilsR   t   email.ParserR   t   email.GeneratorR   t   email.CharsetR   t   MailmanR   R	   R
   R   t   Mailman.ErrorsR   t   Mailman.i18nR   t   Mailman.Logging.SyslogR   t   Mailman.UtilsR   t   compileR­   R©   R®   Rz   t   SPACERk   R   t	   NameErrorR   R   t   ImportErrorR"   R*   RR   RY   R(   R   R   Ru   (    (    (    s8   /export/web/mailman/mailman/Mailman/Handlers/Scrubber.pyt   <module>   sV   
			,	ś	