File: session.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 (49 lines) | stat: -rw-r--r-- 1,867 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
# -*- 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 email.Utils import parseaddr

from trac.config import IntOption
from trac.core import *
from tracspamfilter.api import IFilterStrategy


class SessionFilterStrategy(Component):
    """This strategy grants positive karma points to users with an existing
    session, and extra points if they've set up their user name and password."""

    implements(IFilterStrategy)

    karma_points = IntOption('spam-filter', 'session_karma', '9',
        """By how many points an existing and configured session improves the
        overall karma of the submission. A third of the points is granted for
        having an existing session at all, the other two thirds are granted
        when the user has his name and/or email address set in the session,
        respectively.""")

    # IFilterStrategy implementation

    def test(self, req, author, content):
        points = 0
        if req.session.last_visit:
            points += abs(self.karma_points) / 3
            if req.session.get('name'):
                points += abs(self.karma_points) / 3
            if req.session.get('email'):
                email = parseaddr(req.session.get('email'))[1]
                if email and '@' in email:
                    points += abs(self.karma_points) / 3
            return points, 'Existing session found'

    def train(self, req, author, content, spam=True):
        pass