File: adapters.py

package info (click to toggle)
trac-spamfilter 0.2.1%2Bsvn6871-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 256 kB
  • ctags: 259
  • sloc: python: 1,683; cs: 341; makefile: 40
file content (136 lines) | stat: -rw-r--r-- 4,282 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
#
# Copyright (C) 2006 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.com/license.html.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://projects.edgewall.com/trac/.

from StringIO import StringIO

from trac.attachment import IAttachmentManipulator
from trac.core import *
from trac.config import IntOption
from trac.mimeview import is_binary
from trac.ticket import ITicketManipulator, TicketSystem
from trac.util.text import to_unicode
from trac.wiki import WikiPage, IWikiPageManipulator
from tracspamfilter.api import FilterSystem


class TicketFilterAdapter(Component):
    implements(ITicketManipulator)

    # ITicketManipulator methods

    def prepare_ticket(self, req, ticket, fields, actions):
        pass

    def validate_ticket(self, req, ticket):
        if req.perm.has_permission('TICKET_ADMIN'):
            # An administrator is allowed to spam
            return []

        if 'preview' in req.args:
            # Only a preview, no need to filter the submission yet
            return []

        changes = []

        # Add the author/reporter name
        if not ticket.exists:
            author = ticket['reporter']
        else:
            author = req.args.get('author')
        if author:
            changes += [(None, author)]

        # Add any modified text fields of the ticket (except for the CC field)
        fields = [f['name'] for f in
                  TicketSystem(self.env).get_ticket_fields()
                  if f['type'] in ('textarea', 'text')]
        fields.remove('cc')
        for field in fields:
            if ticket.exists and field == 'description':
                continue
            if field in ticket._old:
                changes.append((ticket._old[field], ticket[field]))

        if 'comment' in req.args:
            changes.append((None, req.args.get('comment')))

        FilterSystem(self.env).test(req, author, changes)
        return []


class WikiFilterAdapter(Component):
    implements(IWikiPageManipulator)

    # ITicketManipulator methods

    def prepare_wiki_page(self, req, page, fields):
        pass

    def validate_wiki_page(self, req, page):
        if req.perm.has_permission('WIKI_ADMIN'):
            # An administrator is allowed to spam
            return []

        if 'preview' in req.args:
            # Only a preview, no need to filter the submission yet
            return []

        cur_page = WikiPage(self.env, name=page.name, version=page.version)
        author = req.args.get('author', req.authname)
        comment = req.args['comment']

        # Test the actual page changes as well as the comment
        changes = [(cur_page.text, page.text), (None, author)]
        if comment:
            changes += [(None, comment)]

        FilterSystem(self.env).test(req, author, changes)
        return []


class AttachmentFilterAdapter(Component):
    implements(IAttachmentManipulator)

    sample_size = IntOption('spam-filter', 'attachment_sample_size', 16384,
        """The number of bytes from an attachment to pass through the spam
        filters.""")

    # ITicketManipulator methods

    def prepare_attachment(self, req, attachment, fields):
        pass

    def validate_attachment(self, req, attachment):
        if req.perm.has_permission('WIKI_ADMIN'):
            # An administrator is allowed to spam
            return []

        author = req.args.get('author', req.authname)
        description = req.args['description']

        upload = req.args['attachment']
        content = ''
        try:
            data = upload.file.read(self.sample_size)
            if not is_binary(data):
                content = to_unicode(data)
        finally:
            upload.file.seek(0)
        filename = upload.filename

        changes = []
        for field in filter(None, [author, description, filename, content]):
            changes += [(None, field)]

        FilterSystem(self.env).test(req, author, changes)
        return []