ó
Hü½cc           @   s  d  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	 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 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   s\  MIME-stripping filter for Mailman.

This module scans a message for MIME content, removing those sections whose
MIME types match one of a list of matches.  multipart/alternative sections are
replaced by the first non-empty component, and multipart/mixed sections
wrapping only single sections after other processing are replaced by their
contents.
iÿÿÿÿN(   t   splitext(   t   typed_subpart_iterator(   t   mm_cfg(   t   Errors(   t   UserNotification(   t   get_switchboard(   t   syslog(   t   VERSION(   t   _(   t   onelinec         C   s  |  j  s d  S| j d ƒ r  d  S| j ƒ  } | j ƒ  } |  j } |  j } | | k sb | | k r~ t |  | | t d ƒ ƒ n  | r¹ | | k p™ | | k r¹ t |  | | t d ƒ ƒ n  |  j } |  j	 } t
 | ƒ }	 |	 r6|	 | k rt |  | | t d ƒ ƒ n  | r6|	 | k r6t |  | | t d ƒ ƒ q6n  t g  | j ƒ  D] }
 |
 ^ qFƒ } | j ƒ  rØt | j ƒ  ƒ } t | | | | | ƒ t | j ƒ  ƒ } | d k rØ| d k rØt |  | | t d ƒ ƒ qØn  |  j rt | ƒ | d k r| j d ƒ } t | | ƒ qn  | j ƒ  r2t | ƒ n  d } | t g  | j ƒ  D] }
 |
 ^ qKƒ k rld	 } n  |  j r‘t j r‘| t | ƒ 7} n  | j ƒ  røt | j ƒ  ƒ d
 k rø| j d ƒ j ƒ  d k rø| j d	 ƒ } t | | ƒ d	 } qøn  | rd t | d <n  d  S(   Nt   isdigests4   The message's content type was explicitly disalloweds5   The message's content type was not explicitly alloweds6   The message's file extension was explicitly disalloweds7   The message's file extension was not explicitly allowedi    s.   After content filtering, the message was emptys   multipart/alternativei   i   t    s   Mailman/MimeDel %ss   X-Content-Filtered-By(   t   filter_contentt   gett   get_content_typet   get_content_maintypet   filter_mime_typest   pass_mime_typest   disposeR   t   filter_filename_extensionst   pass_filename_extensionst   get_file_extt   lent   walkt   is_multipartt   get_payloadt   filter_partst   collapse_alternativest   collapse_multipart_alternativest   reset_payloadt   recast_multipartt   convert_html_to_plaintextR   t   HTML_TO_PLAIN_TEXT_COMMANDt   to_plaintextR   (   t   mlistt   msgt   msgdatat   ctypet   mtypet   filtertypest	   passtypest
   filterextst   passextst   fextt   subpartt   numpartst   prelent   postlent   firstaltt   changedpt   useful(    (    s7   /export/web/mailman/mailman/Mailman/Handlers/MimeDel.pyt   process-   sf    					%	
+	$c         C   sµ   | j  ƒ  } |  j | ƒ |  d =|  d =|  d =|  d =| j d d ƒ |  d <| j d ƒ } | rm | |  d <n  | j d ƒ } | r | |  d <n  | j d ƒ } | r± | |  d	 <n  d  S(
   Ns   content-types   content-transfer-encodings   content-dispositions   content-descriptions
   text/plains   Content-Types   Content-Transfer-Encodings   Content-Dispositions   Content-Description(   R   t   set_payloadR   (   R#   R,   t   payloadt   ctet   cdispt   cdesc(    (    s7   /export/web/mailman/mailman/Mailman/Handlers/MimeDel.pyR   {   s     c         C   sC  |  j  ƒ  s d S|  j ƒ  } t | ƒ } g  } xÙ | D]Ñ } t | | | | | ƒ }	 |	 s_ q5 n  | j ƒ  }
 | j ƒ  } |
 | k s5 | | k r• q5 n  | rº |
 | k p° | | k rº q5 n  t | ƒ } | rù | | k rÞ q5 n  | rù | | k rù q5 qù n  | j | ƒ q5 Wt | ƒ } |  j | ƒ | d k r?| d k r?d Sd S(   Ni   i    (	   R   R   R   R   R   R   R   t   appendR4   (   R#   R'   R(   R)   R*   R5   R.   t
   newpayloadR,   t   keepR%   R&   R+   R/   (    (    s7   /export/web/mailman/mailman/Mailman/Handlers/MimeDel.pyR      s8    	c         C   sï   |  j  ƒ  s d  Sg  } xÅ |  j ƒ  D]· } | j ƒ  d k r§ yO | j d ƒ } |  j ƒ  d k r| t | | ƒ | j | ƒ n | j | ƒ WqÚ t t f k
 r£ qÚ Xq# | j  ƒ  rÍ t | ƒ | j | ƒ q# | j | ƒ q# W|  j | ƒ d  S(   Ns   multipart/alternativei    s   message/rfc822(	   R   R   R   R   R9   t
   IndexErrort	   TypeErrorR   R4   (   R#   R:   R,   R0   (    (    s7   /export/web/mailman/mailman/Mailman/Handlers/MimeDel.pyR   ¶   s$    
c         C   s—   |  j  ƒ  d k r d  S|  j ƒ  r“ t |  j ƒ  ƒ d k ro |  j  ƒ  d k ro t |  |  j d ƒ ƒ t |  ƒ q“ x! |  j ƒ  D] } t | ƒ q| Wn  d  S(   Ns   multipart/signedi   s   message/rfc822i    (   R   R   R   R   R   R   (   R#   t   part(    (    s7   /export/web/mailman/mailman/Mailman/Handlers/MimeDel.pyR   Ñ   s    c   	   	   C   s$  d } xt  |  d d ƒ D]} t j d ƒ } t | d ƒ } zu | j | j d d ƒ ƒ | j ƒ  t j t	 j
 i | d 6ƒ } | j ƒ  } | j ƒ  } | r± t d	 d
 | ƒ n  Wd  y t j | ƒ Wn+ t k
 ró } | j t j k rô ‚  qô n XX| d =| j | ƒ | j d ƒ d } q W| S(   Ni    t   textt   htmls   .htmlt   wt   decodei   t   filenamet   errors   HTML->text/plain error: %ss   content-transfer-encodings
   text/plain(   R   t   tempfilet   mktempt   opent   writeR   t   closet   ost   popenR   R    t   readR   t   unlinkt   OSErrort   errnot   ENOENTR4   t   set_type(	   R#   R1   R,   RC   t   fpt   cmdt	   plaintextt   rtnt   e(    (    s7   /export/web/mailman/mailman/Mailman/Handlers/MimeDel.pyR!   ç   s.    
 
c         C   s¨   |  j  d k r t j | ‚ n  |  j  d k ra |  j ƒ  } |  j | d t d ƒ d t d ƒ ƒn  |  j  d k r› t j r› t t j	 ƒ } | j
 | | ƒ n  t j ‚ d  S(   Ni   i   R?   sÝ   The attached message matched the %(listname)s mailing list's content filtering
rules and was prevented from being forwarded on to the list membership.  You
are receiving the only remaining copy of the discarded message.

t   subjects%   Content filtered message notificationi   (   t   filter_actionR   t   RejectMessaget   internal_namet   ForwardMessageR   R   t%   OWNERS_CAN_PRESERVE_FILTERED_MESSAGESR   t   BADQUEUE_DIRt   enqueuet   DiscardMessage(   R"   R#   R$   t   whyt   listnamet   badq(    (    s7   /export/web/mailman/mailman/Mailman/Handlers/MimeDel.pyR     s    		c         C   sx   d } |  j  d ƒ p$ |  j d d ƒ } | rn t t | d ƒ ƒ d } t | ƒ d k re | d } qn d } n  | j ƒ  S(   si   
    Get filename extension. Caution: some virus don't put filename
    in 'Content-Disposition' header.
R   t   names   utf-8i   (   t   get_filenamet	   get_paramR    R	   R   t   lower(   t   mR+   RC   (    (    s7   /export/web/mailman/mailman/Mailman/Handlers/MimeDel.pyR     s    !	(   t   __doc__RJ   RO   RE   t   os.pathR    t   email.IteratorsR   t   MailmanR   R   t   Mailman.MessageR   t   Mailman.Queue.sbcacheR   t   Mailman.Logging.SyslogR   t   Mailman.VersionR   t   Mailman.i18nR   t   Mailman.UtilsR	   R3   R   R   R   R   R!   R   R   (    (    (    s7   /export/web/mailman/mailman/Mailman/Handlers/MimeDel.pyt   <module>   s*   	N		&				