diff options
author | Mike Pirnat <mpirnat@gmail.com> | 2012-08-08 23:03:36 -0400 |
---|---|---|
committer | Mike Pirnat <mpirnat@gmail.com> | 2012-08-08 23:03:36 -0400 |
commit | 775be2a87ad8a744548d1a1d0ae87a7e665ca10e (patch) | |
tree | eac19f33118764af09f201073f1050537106d479 | |
parent | a530575b4995a28e91f28e08ea211e172beb2607 (diff) | |
download | tiny-webapps-775be2a87ad8a744548d1a1d0ae87a7e665ca10e.tar.bz2 tiny-webapps-775be2a87ad8a744548d1a1d0ae87a7e665ca10e.tar.xz tiny-webapps-775be2a87ad8a744548d1a1d0ae87a7e665ca10e.zip |
Adds rudimentary spam check
-rwxr-xr-x | email_gateway.py | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/email_gateway.py b/email_gateway.py index e73e3e2..fe58fad 100755 --- a/email_gateway.py +++ b/email_gateway.py | |||
@@ -6,7 +6,9 @@ import re | |||
6 | import urlparse | 6 | import urlparse |
7 | from cStringIO import StringIO | 7 | from cStringIO import StringIO |
8 | from email.mime.text import MIMEText | 8 | from email.mime.text import MIMEText |
9 | from ConfigParser import SafeConfigParser as ConfigParser, NoSectionError | 9 | from ConfigParser import SafeConfigParser as ConfigParser, \ |
10 | NoSectionError, NoOptionError | ||
11 | from spambayes.storage import PickledClassifier | ||
10 | 12 | ||
11 | 13 | ||
12 | config = ConfigParser() | 14 | config = ConfigParser() |
@@ -26,6 +28,18 @@ def send_message(text, subject, to, from_email): | |||
26 | p.close() | 28 | p.close() |
27 | 29 | ||
28 | 30 | ||
31 | def looks_like_spam(message, config, section): | ||
32 | pickle_filename = config.get(section, 'spam.pickle_file') | ||
33 | min_spam_prob = config.getfloat(section, 'spam.min_spam_prob') | ||
34 | |||
35 | bayes = PickledClassifier(pickle_filename) | ||
36 | |||
37 | if bayes.chi2_spamprob(message) >= min_spam_prob: | ||
38 | return True | ||
39 | |||
40 | return False | ||
41 | |||
42 | |||
29 | def email_app(environ, start_response): | 43 | def email_app(environ, start_response): |
30 | ignored_fields = [] | 44 | ignored_fields = [] |
31 | useful_fields = [] | 45 | useful_fields = [] |
@@ -60,6 +74,14 @@ def email_app(environ, start_response): | |||
60 | start_response('403 Forbidden', [('Content-Type', 'text/plain')]) | 74 | start_response('403 Forbidden', [('Content-Type', 'text/plain')]) |
61 | return "Invalid send!" | 75 | return "Invalid send!" |
62 | 76 | ||
77 | try: | ||
78 | if config.getboolean(form_key, 'spam.check') \ | ||
79 | and looks_like_spam(context["message"], config, form_key): | ||
80 | start_response('403 Forbidden', [('Content-Type', 'text/plain')]) | ||
81 | return "I don't like SPAM!" | ||
82 | except NoOptionError: | ||
83 | pass | ||
84 | |||
63 | useful_fields = ["{0}: {1}".format(*f) | 85 | useful_fields = ["{0}: {1}".format(*f) |
64 | for f in useful_fields | 86 | for f in useful_fields |
65 | if f[0] not in ignored_fields] | 87 | if f[0] not in ignored_fields] |