ó
Iü½cc           @   s=  d  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 y e e f Wn e k
 rß d Z d Z n Xd „  Z d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ e j Z d d „ Z d „  Z d S(   s9  Do more detailed spam detection.

This module hard codes site wide spam detection.  By hacking the
KNOWN_SPAMMERS variable, you can set up more regular expression matches
against message headers.  If spam is detected the message is discarded
immediately.

TBD: This needs to be made more configurable and robust.
iÿÿÿÿN(   t	   normalize(   t   HeaderParseError(   t   decode_header(   t	   parseaddr(   t   mm_cfg(   t   Errors(   t   i18n(   t   Utils(   t   hold_for_approval(   t   syslogi   i    c         C   s   |  S(   N(    (   t   s(    (    s:   /export/web/mailman/mailman/Mailman/Handlers/SpamDetect.pyt   _2   s    t   SpamDetectedc           B   s   e  Z d  Z RS(   s   The message contains known spam(   t   __name__t
   __module__t   __doc__(    (    (    s:   /export/web/mailman/mailman/Mailman/Handlers/SpamDetect.pyR   7   s   t   HeaderMatchHoldc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   _HeaderMatchHold__pattern(   t   selft   pattern(    (    s:   /export/web/mailman/mailman/Mailman/Handlers/SpamDetect.pyt   __init__;   s    c         C   s   |  j  } t d ƒ S(   Ns"   Header matched regexp: %(pattern)s(   R   R   (   R   R   (    (    s:   /export/web/mailman/mailman/Mailman/Handlers/SpamDetect.pyt   reason_notice>   s    	(   R   R   R   R   (    (    (    s:   /export/web/mailman/mailman/Mailman/Handlers/SpamDetect.pyR   :   s   	s   utf-8c   	      C   s  d } xþ |  j  ƒ  D]ð \ } } d } y t t j d d | ƒ ƒ } Wn  t k
 rf | d f g } n Xxg | D]_ \ } } | s‰ d } n  y | t | | d ƒ 7} Wqn t k
 rÌ | t | d d ƒ 7} qn Xqn W| j d d ƒ } | d | t t	 j
 | ƒ f 7} q W| S(   sƒ   Returns a unicode containing all the headers of msg, unfolded and
    RFC 2047 decoded, normalized and separated by new lines.
    u    s   
\st    s   us-asciit   replaces
   iso-8859-1u   %s: %s
(   t   itemsR   t   ret   subR   t   unicodet   LookupErrort   decodeR    R   t   NORMALIZE_FORM(	   t   msgt   csett   headerst   ht   vt   uvaluet   fragt   cst   uhdr(    (    s:   /export/web/mailman/mailman/Mailman/Handlers/SpamDetect.pyt   getDecodedHeadersH   s"    	$c         C   s  | j  d ƒ s²d | d <t | j  d ƒ ƒ \ } } | r)|  j d k r)|  j | |  j ƒ sp t j |  | ƒ r)|  j d k rŒ d | d <q&|  j d k r¨ d | d <q&|  j d k r|  j } | rØ t j | ƒ } n! |  j	 ƒ  } t j t
 d ƒ ƒ } t j | ‚ q&|  j d	 k r&t j ‚ q&q)n  x3 | j ƒ  D] } |  j | ƒ r6Pq6q6W| j ƒ  } |  j d k r²t j |  | ƒ r²|  j | t j d ƒ t d
 d |  j | ƒ q²n  | j  d ƒ rÅd  Sxg t j D]\ \ } }	 t j |	 t j ƒ }
 x8 | j | g  ƒ D]$ } |
 j | ƒ } | rt ‚ qqWqÏWd } t j |  j  ƒ } x' | j! ƒ  D] } | t" | | ƒ 7} qTWxŸ|  j# D]”\ } } } | t j$ k rŸq{n  xm| j% ƒ  D]_} | j& d ƒ rÇq¬n  | j' ƒ  sÙq¬n  t j( | | ƒ } t) t j* | ƒ } y* t j | | t j t j+ Bt j, Bƒ } Wn* t j- t. f k
 rSt d d | ƒ n X| r¬| t j/ k rut j ‚ n  | t j0 k r·| j  d ƒ rŸt j ‚ n  t j t
 d ƒ ƒ ‚ n  | t j1 k rõ| j  d ƒ rÙd  St2 |  | | t3 | ƒ ƒ n  | t j4 k rd  Sq¬q¬Wq{Wd  S(   Nt   toowneri    t   from_is_listt   fromi   i   i   s  You are not allowed to post to this mailing list From: a domain which
publishes a DMARC policy of reject or quarantine, and your message has been
automatically rejected.  If you think that your messages are being rejected in
error, contact the mailing list owner at %(listowner)s.i   t   vettes4   %s: Automatically Moderated %s for verbose postings.t   approvedu    t   #t   errors0   ignoring header_filter_rules invalid pattern: %ss%   Message rejected by filter rule match(5   t   getR   t   dmarc_moderation_actiont
   GetPatternt   dmarc_moderation_addressesR   t   IsDMARCProhibitedt   dmarc_moderation_noticet   wrapt   GetOwnerEmailR   R   t   RejectMessaget   DiscardMessaget   get_senderst   isMembert
   get_sendert   member_verbosity_thresholdt   IsVerboseMembert   setMemberOptionR   t   ModerateR	   t	   real_namet   KNOWN_SPAMMERSR   t   compilet
   IGNORECASEt   get_allt   searchR   t
   GetCharSett   preferred_languaget   walkR(   t   header_filter_rulest   DEFERt
   splitlinest
   startswitht   stript   xml_to_unicodeR    R   t	   MULTILINEt   UNICODER/   t	   TypeErrort   DISCARDt   REJECTt   HOLDR   R   t   ACCEPT(   t   mlistR   t   msgdatat   dnt   addrt   textt	   listownert   sendert   headert   regext   cret   valuet   moR!   t   lcsett   pt   patternst   actiont   emptyR   (    (    s:   /export/web/mailman/mailman/Mailman/Handlers/SpamDetect.pyt   processf   s–    
			(   R   R   t   unicodedataR    t   email.ErrorsR   t   email.HeaderR   t   email.UtilsR   t   MailmanR   R   R   R   t   Mailman.Handlers.HoldR   t   Mailman.Logging.SyslogR	   t   Truet   Falset	   NameErrorR   R9   R   t   HoldMessageR   R(   Rh   (    (    (    s:   /export/web/mailman/mailman/Mailman/Handlers/SpamDetect.pyt   <module>   s,   
	
	