Edgewall Software

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

Last change on this file was 14824, checked in by Ryan J Ollos, 7 years ago

1.0.9dev: Conform to PEP8

File size: 4.1 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright (C) 2005-2011 Edgewall Software
4# Copyright (C) 2005 Matthew Good <trac@matt-good.net>
5# Copyright (C) 2006 Christopher Lenz <cmlenz@gmx.de>
6# Copyright (C) 2011 Dirk Stöcker <trac@dstoecker.de>
7# All rights reserved.
8#
9# This software is licensed as described in the file COPYING, which
10# you should have received as part of this distribution. The terms
11# are also available at http://trac.edgewall.com/license.html.
12#
13# This software consists of voluntary contributions made by many
14# individuals. For the exact contribution history, see the revision
15# history and logs, available at http://projects.edgewall.com/trac/.
16#
17# Author: Dirk Stöcker <trac@dstoecker.de>,
18# Matthew Good <trac@matt-good.net>
19
20import re
21
22from trac.config import BoolOption, IntOption, Option
23from trac.core import Component, implements
24from trac.wiki.api import IWikiChangeListener
25from trac.wiki.model import WikiPage
26
27from tracspamfilter.api import IFilterStrategy, N_
28
29
30class IPRegexFilterStrategy(Component):
31 """Spam filter for submitter's IP based on regular expressions
32 defined in BadIP page.
33 """
34 implements(IFilterStrategy, IWikiChangeListener)
35
36 karma_points = IntOption('spam-filter', 'ipregex_karma', '20',
37 """By how many points a match with a pattern on the BadIP page
38 impacts the overall karma of a submission.""",
39 doc_domain='tracspamfilter')
40
41 badcontent_file = Option('spam-filter', 'ipbadcontent_file', '',
42 """Local file to be loaded to get BadIP. Can be used in
43 addition to BadIP wiki page.""", doc_domain='tracspamfilter')
44
45 show_blacklisted = BoolOption('spam-filter', 'show_blacklisted_ip',
46 'true',
47 "Show the matched bad IP patterns in rejection message.",
48 doc_domain='tracspamfilter')
49
50 def __init__(self):
51 self.patterns = []
52 page = WikiPage(self.env, 'BadIP')
53 if page.exists:
54 self._load_patterns(page)
55 if self.badcontent_file != '':
56 file = open(self.badcontent_file, 'r')
57 if file is None:
58 self.log.warning("BadIP file cannot be opened")
59 else:
60 lines = file.read().splitlines()
61 pat = [re.compile(p.strip()) for p in lines if p.strip()]
62 self.log.debug("Loaded %s patterns from BadIP file", len(pat))
63 self.patterns += pat
64
65 # IFilterStrategy implementation
66
67 def is_external(self):
68 return False
69
70 def test(self, req, author, content, ip):
71 gotcha = []
72 points = 0
73 for pattern in self.patterns:
74 match = pattern.search(ip)
75 if match:
76 gotcha.append("'%s'" % pattern.pattern)
77 self.log.debug("Pattern %s found in submission",
78 pattern.pattern)
79 points -= abs(self.karma_points)
80 if points != 0:
81 if self.show_blacklisted:
82 matches = ", ".join(gotcha)
83 return points, N_("IP catched by these blacklisted "
84 "patterns: %s"), matches
85 else:
86 return (points, N_("IP catched by %s blacklisted patterns"),
87 str(len(gotcha)))
88
89 def train(self, req, author, content, ip, spam=True):
90 return 0
91
92 # IWikiChangeListener implementation
93
94 def wiki_page_changed(self, page, *args):
95 if page.name == 'BadIP':
96 self._load_patterns(page)
97 wiki_page_added = wiki_page_changed
98 wiki_page_version_deleted = wiki_page_changed
99
100 def wiki_page_deleted(self, page):
101 if page.name == 'BadIP':
102 self.patterns = []
103
104 # Internal methods
105
106 def _load_patterns(self, page):
107 if '{{{' in page.text and '}}}' in page.text:
108 lines = page.text.split('{{{', 1)[1].split('}}}', 1)[0].splitlines()
109 self.patterns = [re.compile(p.strip()) for p in lines if p.strip()]
110 self.log.debug("Loaded %s patterns from BadIP",
111 len(self.patterns))
112 else:
113 self.log.warning("BadIP page does not contain any patterns")
114 self.patterns = []
Note: See TracBrowser for help on using the repository browser.