| 1 | # -*- coding: utf-8 -*-
|
|---|
| 2 | #
|
|---|
| 3 | # Copyright (C) 2012 Dirk Stöcker <trac@dstoecker.de>
|
|---|
| 4 | # All rights reserved.
|
|---|
| 5 | #
|
|---|
| 6 | # This software is licensed as described in the file COPYING, which
|
|---|
| 7 | # you should have received as part of this distribution. The terms
|
|---|
| 8 | # are also available at http://trac.edgewall.com/license.html.
|
|---|
| 9 | #
|
|---|
| 10 | # This software consists of voluntary contributions made by many
|
|---|
| 11 | # individuals. For the exact contribution history, see the revision
|
|---|
| 12 | # history and logs, available at http://projects.edgewall.com/trac/.
|
|---|
| 13 |
|
|---|
| 14 | import string
|
|---|
| 15 | import urllib2
|
|---|
| 16 | from email.Utils import parseaddr
|
|---|
| 17 | from pkg_resources import get_distribution
|
|---|
| 18 | from urllib import urlencode
|
|---|
| 19 |
|
|---|
| 20 | from trac import __version__ as TRAC_VERSION
|
|---|
| 21 | from trac.config import IntOption, Option
|
|---|
| 22 | from trac.core import Component, implements
|
|---|
| 23 |
|
|---|
| 24 | from tracspamfilter.api import IFilterStrategy, N_
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 | class BotScoutFilterStrategy(Component):
|
|---|
| 28 | """Spam filter using the BotScout (http://botscout.com/).
|
|---|
| 29 | """
|
|---|
| 30 | implements(IFilterStrategy)
|
|---|
| 31 |
|
|---|
| 32 | karma_points = IntOption('spam-filter', 'botscout_karma', '3',
|
|---|
| 33 | """By how many points a BotScout reject impacts the overall karma of
|
|---|
| 34 | a submission.""", doc_domain='tracspamfilter')
|
|---|
| 35 |
|
|---|
| 36 | api_key = Option('spam-filter', 'botscout_api_key', '',
|
|---|
| 37 | "API key required to use BotScout.", doc_domain='tracspamfilter')
|
|---|
| 38 |
|
|---|
| 39 | user_agent = 'Trac/%s | SpamFilter/%s' % (
|
|---|
| 40 | TRAC_VERSION, get_distribution('TracSpamFilter').version
|
|---|
| 41 | )
|
|---|
| 42 |
|
|---|
| 43 | # IFilterStrategy implementation
|
|---|
| 44 |
|
|---|
| 45 | def is_external(self):
|
|---|
| 46 | return True
|
|---|
| 47 |
|
|---|
| 48 | def test(self, req, author, content, ip):
|
|---|
| 49 | if not self._check_preconditions(False):
|
|---|
| 50 | return
|
|---|
| 51 | try:
|
|---|
| 52 | resp = self._send(req, author, ip)
|
|---|
| 53 | if resp.startswith('Y'):
|
|---|
| 54 | count = 0
|
|---|
| 55 | res = string.split(resp, '|')
|
|---|
| 56 | if res[3] != '0':
|
|---|
| 57 | count += 1
|
|---|
| 58 | if res[5] != '0':
|
|---|
| 59 | count += 1
|
|---|
| 60 | if res[7] != '0':
|
|---|
| 61 | count += 1
|
|---|
| 62 | return (-abs(self.karma_points) * count,
|
|---|
| 63 | N_("BotScout says this is spam (%s)"), resp)
|
|---|
| 64 | except urllib2.URLError, e:
|
|---|
| 65 | self.log.warn("BotScout request failed (%s)", e)
|
|---|
| 66 |
|
|---|
| 67 | def train(self, req, author, content, ip, spam=True):
|
|---|
| 68 | return 0
|
|---|
| 69 |
|
|---|
| 70 | # Internal methods
|
|---|
| 71 |
|
|---|
| 72 | def _check_preconditions(self, train):
|
|---|
| 73 | if self.karma_points == 0:
|
|---|
| 74 | return False
|
|---|
| 75 |
|
|---|
| 76 | if not self.api_key:
|
|---|
| 77 | return False
|
|---|
| 78 |
|
|---|
| 79 | return True
|
|---|
| 80 |
|
|---|
| 81 | def _send(self, req, author, ip):
|
|---|
| 82 | # Split up author into name and email, if possible
|
|---|
| 83 | author = author.encode('utf-8')
|
|---|
| 84 | author_name, author_email = parseaddr(author)
|
|---|
| 85 | if not author_name and not author_email:
|
|---|
| 86 | author_name = author
|
|---|
| 87 | elif not author_name and author_email.find('@') < 1:
|
|---|
| 88 | author_name = author
|
|---|
| 89 | author_email = None
|
|---|
| 90 | if author_name == 'anonymous':
|
|---|
| 91 | author_name = None
|
|---|
| 92 |
|
|---|
| 93 | params = {'ip': ip, 'key': self.api_key}
|
|---|
| 94 | if author_name:
|
|---|
| 95 | params['name'] = author_name
|
|---|
| 96 | if author_email:
|
|---|
| 97 | params['mail'] = author_email
|
|---|
| 98 |
|
|---|
| 99 | url = 'http://botscout.com/test/?multi&' + urlencode(params)
|
|---|
| 100 | url_req = urllib2.Request(url, None, {'User-Agent': self.user_agent})
|
|---|
| 101 |
|
|---|
| 102 | resp = urllib2.urlopen(url_req)
|
|---|
| 103 | return resp.read()
|
|---|