Edgewall Software

source: plugins/1.0/spam-filter/tracspamfilter/filters/trapfield.py

Last change on this file was 16632, checked in by Ryan J Ollos, 5 years ago

1.0.12dev: Make compatible with Trac < 1.0.2

File size: 3.9 KB
Line 
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
14from genshi.filters.transform import Transformer
15from trac.config import IntOption, Option
16from trac.core import Component, implements
17from trac.util.html import html as tag
18from trac.util.text import javascript_quote, shorten_line
19from trac.web.api import ITemplateStreamFilter
20
21from tracspamfilter.api import IFilterStrategy, N_
22
23
24class TrapFieldFilterStrategy(Component):
25 """Spam filter using a hidden trap field.
26 """
27 implements(IFilterStrategy, ITemplateStreamFilter)
28
29 karma_points = IntOption('spam-filter', 'trap_karma', '10',
30 """By how many points a trap reject impacts the overall karma of
31 a submission.""", doc_domain='tracspamfilter')
32
33 name = Option('spam-filter', 'trap_name', 'sfp_email',
34 """Name of the invisible trap field, should contain some reference
35 to e-mail for better results.""", doc_domain='tracspamfilter')
36
37 name_hidden = Option('spam-filter', 'trap_name_hidden', 'sfph_mail',
38 """Name of the hidden trap field, should contain some reference
39 to e-mail for better results.""", doc_domain='tracspamfilter')
40
41 name_register = Option('spam-filter', 'trap_name_register', 'spf_homepage',
42 """Name of the register trap field, should contain some reference
43 to web/homepage for better results.""", doc_domain='tracspamfilter')
44
45 # IFilterStrategy implementation
46
47 def is_external(self):
48 return False
49
50 def get_trap(self, req):
51 i = req.args.getfirst(self.name)
52 h = req.args.getfirst(self.name_hidden)
53 if i and h and i != h:
54 return i + "\n" + h
55 elif h:
56 return h
57 return i
58
59 def test(self, req, author, content, ip):
60 i = req.args.getfirst(self.name)
61 h = req.args.getfirst(self.name_hidden)
62 r = self.getlink(req)
63
64 if i and h:
65 i = shorten_line(javascript_quote(i), 50)
66 h = shorten_line(javascript_quote(h), 50)
67 return -abs(self.karma_points), \
68 N_("Both trap fields says this is spam (%s, %s)"), i, h
69 elif i:
70 i = shorten_line(javascript_quote(i), 50)
71 return -abs(self.karma_points), \
72 N_("Invisible trap field says this is spam (%s)"), i
73 elif h:
74 h = shorten_line(javascript_quote(h), 50)
75 return -abs(self.karma_points), \
76 N_("Hidden trap field says this is spam (%s)"), h
77 elif r:
78 r = shorten_line(javascript_quote(r), 50)
79 return -abs(self.karma_points), \
80 N_("Register trap field starts with HTTP URL (%s)"), r
81
82 def getlink(self, req):
83 r = req.args.getfirst(self.name_register)
84 if not (r and (r.startswith('http://') or r.startswith('https://'))):
85 return None
86 return r
87
88 def train(self, req, author, content, ip, spam=True):
89 return 0
90
91 # ITemplateStreamFilter interface
92 def filter_stream(self, req, method, filename, stream, data):
93 if self.karma_points > 0:
94 # Insert the hidden field right before the submit buttons
95 trap = tag.div(style='display:none;')(
96 tag.input(type='text', name=self.name, value=''),
97 tag.input(type='hidden', name=self.name_hidden, value=''))
98 stream = stream | \
99 Transformer('//div[@class="buttons"]').before(trap)
100 return stream
Note: See TracBrowser for help on using the repository browser.