Edgewall Software

Ticket #8406: Extlinks-with-whitelist.diff

File Extlinks-with-whitelist.diff, 1.5 KB (added by Sven, 3 years ago)

Patch to extend the extlinks filter with a domain whitelist

  • tracspamfilter/filters/extlinks.py

     
    1313 
    1414import re 
    1515 
    16 from trac.config import IntOption 
     16from trac.config import ListOption, IntOption 
    1717from trac.core import * 
    1818from tracspamfilter.api import IFilterStrategy 
    1919from tracspamfilter.model import LogEntry 
     
    3333        """The maximum number of external links allowed in a submission until 
    3434        that submission gets negative karma.""") 
    3535 
     36    allowed_domains = ListOption('spam-filter', 'extlinks_allowed_domains', 
     37                                 'example.com, example.org', doc= 
     38        """List of domains that should be allowed in external links""") 
     39     
    3640    _URL_RE = re.compile('https?://([^/]+)/?', re.IGNORECASE) 
    3741 
    3842    # IFilterStrategy methods 
    3943 
    4044    def test(self, req, author, content): 
    4145        num_ext = 0 
     46        allowed = self.allowed_domains 
     47        allowed.append(req.get_header('Host')) 
     48         
    4249        for host in self._URL_RE.findall(content): 
    43             if host != req.get_header('Host'): 
     50            if host not in allowed: 
     51                self.env.log.debug('"%s" is not in extlink_allowed_domains' % host) 
    4452                num_ext += 1 
     53            else: 
     54                self.env.log.debug('"%s" is whitelisted.' % host) 
    4555 
    4656        if num_ext > self.max_links: 
    4757            return -abs(self.karma_points) * num_ext / self.max_links, \