bgneal@214
|
1 """Antispam utility functions other apps can use."""
|
bgneal@215
|
2 import datetime
|
bgneal@215
|
3 import logging
|
bgneal@215
|
4 import textwrap
|
bgneal@215
|
5
|
bgneal@214
|
6 from django.core.cache import cache
|
bgneal@214
|
7
|
bgneal@214
|
8 from antispam import SPAM_PHRASE_KEY
|
bgneal@214
|
9 from antispam.models import SpamPhrase
|
bgneal@215
|
10 from core.functions import email_admins
|
bgneal@215
|
11 from bio.models import STA_SUSPENDED
|
bgneal@214
|
12
|
bgneal@214
|
13
|
bgneal@214
|
14 def contains_spam(s):
|
bgneal@214
|
15 """This function returns True if the supplied string s contains any spam
|
bgneal@214
|
16 phrases and False otherwise.
|
bgneal@214
|
17 """
|
bgneal@214
|
18 phrases = _get_spam_phrases()
|
bgneal@215
|
19 s = s.lower()
|
bgneal@214
|
20 for spam in phrases:
|
bgneal@214
|
21 if spam in s:
|
bgneal@214
|
22 return True
|
bgneal@214
|
23
|
bgneal@214
|
24 return False
|
bgneal@214
|
25
|
bgneal@214
|
26
|
bgneal@215
|
27 def spam_check(request, content):
|
bgneal@215
|
28 """This function checks the supplied content for spam if the user from the
|
bgneal@215
|
29 supplied request is a stranger (new to the site). If spam is found, the
|
bgneal@215
|
30 function makes a log entry, emails the admins, suspends the user's account
|
bgneal@215
|
31 and returns True. If spam is not found, False is returned.
|
bgneal@215
|
32 It is assumed that request.user is an authenticated user and thus has a
|
bgneal@215
|
33 user profile.
|
bgneal@215
|
34 """
|
bgneal@215
|
35 user = request.user
|
bgneal@215
|
36 if user.get_profile().is_stranger() and contains_spam(content):
|
bgneal@215
|
37
|
bgneal@215
|
38 ip = request.META.get('REMOTE_ADDR', "unknown")
|
bgneal@215
|
39
|
bgneal@215
|
40 msg = textwrap.dedent("""\
|
bgneal@215
|
41 SPAM FILTER TRIPPED by %s
|
bgneal@215
|
42 PATH: %s
|
bgneal@215
|
43 IP: %s
|
bgneal@215
|
44 Message:
|
bgneal@215
|
45 %s
|
bgneal@215
|
46 """ % (user.username, request.path, ip, content))
|
bgneal@215
|
47
|
bgneal@215
|
48 logging.info(msg)
|
bgneal@215
|
49 email_admins("SPAM FILTER TRIPPED BY %s" % user.username, msg)
|
bgneal@215
|
50 suspend_user(user)
|
bgneal@215
|
51 return True
|
bgneal@215
|
52
|
bgneal@215
|
53 return False
|
bgneal@215
|
54
|
bgneal@215
|
55
|
bgneal@215
|
56 def suspend_user(user):
|
bgneal@215
|
57 """This function marks the user as suspended."""
|
bgneal@215
|
58 user.is_active = False
|
bgneal@215
|
59 user.save()
|
bgneal@215
|
60 profile = user.get_profile()
|
bgneal@215
|
61 profile.status = STA_SUSPENDED
|
bgneal@215
|
62 profile.status_date = datetime.datetime.now()
|
bgneal@215
|
63 profile.save()
|
bgneal@215
|
64
|
bgneal@215
|
65
|
bgneal@214
|
66 def _get_spam_phrases():
|
bgneal@214
|
67 """This function returns the current list of spam phrase strings.
|
bgneal@214
|
68 The strings are cached to avoid hitting the database.
|
bgneal@214
|
69 """
|
bgneal@214
|
70 phrases = cache.get(SPAM_PHRASE_KEY)
|
bgneal@214
|
71 if phrases:
|
bgneal@214
|
72 return phrases
|
bgneal@214
|
73
|
bgneal@214
|
74 phrases = SpamPhrase.objects.values_list('phrase', flat=True)
|
bgneal@214
|
75 cache.set(SPAM_PHRASE_KEY, phrases)
|
bgneal@214
|
76 return phrases
|