diff options
author | Mike Crute <mcrute@gmail.com> | 2012-08-09 23:19:13 -0400 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2012-08-09 23:19:13 -0400 |
commit | fed60d47bb4048fbacd24e7707bfce005c63c2da (patch) | |
tree | f2eb5e1f6deb626f0d621f0db0bb8f74302961ee | |
parent | 645f95c6549bdf4fcc0e465b489b014945377291 (diff) | |
download | tiny-webapps-master.tar.bz2 tiny-webapps-master.tar.xz tiny-webapps-master.zip |
-rwxr-xr-x | email_gateway.py | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/email_gateway.py b/email_gateway.py index fe58fad..edb503a 100755 --- a/email_gateway.py +++ b/email_gateway.py | |||
@@ -1,5 +1,6 @@ | |||
1 | #!/usr/bin/env python2.6 | 1 | #!/usr/bin/env python2.6 |
2 | 2 | ||
3 | import logging | ||
3 | import os | 4 | import os |
4 | import smtplib | 5 | import smtplib |
5 | import re | 6 | import re |
@@ -10,6 +11,11 @@ from ConfigParser import SafeConfigParser as ConfigParser, \ | |||
10 | NoSectionError, NoOptionError | 11 | NoSectionError, NoOptionError |
11 | from spambayes.storage import PickledClassifier | 12 | from spambayes.storage import PickledClassifier |
12 | 13 | ||
14 | log = logging.getLogger("email_gateway") | ||
15 | handler = logging.FileHandler("/var/log/webapps/email_gateway.log") | ||
16 | log.addHandler(handler) | ||
17 | log.setLevel(logging.DEBUG) | ||
18 | |||
13 | 19 | ||
14 | config = ConfigParser() | 20 | config = ConfigParser() |
15 | with open("/etc/email_gateway.cfg") as fp: | 21 | with open("/etc/email_gateway.cfg") as fp: |
@@ -29,14 +35,21 @@ def send_message(text, subject, to, from_email): | |||
29 | 35 | ||
30 | 36 | ||
31 | def looks_like_spam(message, config, section): | 37 | def looks_like_spam(message, config, section): |
38 | log.info("Checking message for spam...") | ||
39 | log.debug(message) | ||
32 | pickle_filename = config.get(section, 'spam.pickle_file') | 40 | pickle_filename = config.get(section, 'spam.pickle_file') |
33 | min_spam_prob = config.getfloat(section, 'spam.min_spam_prob') | 41 | min_spam_prob = config.getfloat(section, 'spam.min_spam_prob') or 0.90 |
34 | 42 | ||
43 | log.debug("Loading pickle from %s", pickle_filename) | ||
35 | bayes = PickledClassifier(pickle_filename) | 44 | bayes = PickledClassifier(pickle_filename) |
36 | 45 | ||
37 | if bayes.chi2_spamprob(message) >= min_spam_prob: | 46 | spamprob = bayes.chi2_spamprob(message) |
47 | |||
48 | if spamprob >= min_spam_prob: | ||
49 | log.debug("spamprob %s >= %s, probably spam", spamprob, min_spam_prob) | ||
38 | return True | 50 | return True |
39 | 51 | ||
52 | log.debug("spamprob %s <= %s, probably not spam", spamprob, min_spam_prob) | ||
40 | return False | 53 | return False |
41 | 54 | ||
42 | 55 | ||
@@ -47,6 +60,7 @@ def email_app(environ, start_response): | |||
47 | message_buffer = StringIO() | 60 | message_buffer = StringIO() |
48 | 61 | ||
49 | context = {} | 62 | context = {} |
63 | to_check = [] | ||
50 | 64 | ||
51 | fields = urlparse.parse_qsl(environ["wsgi.input"].read()) | 65 | fields = urlparse.parse_qsl(environ["wsgi.input"].read()) |
52 | for key, value in fields: | 66 | for key, value in fields: |
@@ -61,6 +75,7 @@ def email_app(environ, start_response): | |||
61 | elif key == "mailer.fields.ignore": | 75 | elif key == "mailer.fields.ignore": |
62 | ignored_fields = value.split(",") | 76 | ignored_fields = value.split(",") |
63 | else: | 77 | else: |
78 | to_check.append(value) | ||
64 | useful_fields.append((key, value)) | 79 | useful_fields.append((key, value)) |
65 | 80 | ||
66 | try: | 81 | try: |
@@ -76,7 +91,7 @@ def email_app(environ, start_response): | |||
76 | 91 | ||
77 | try: | 92 | try: |
78 | if config.getboolean(form_key, 'spam.check') \ | 93 | if config.getboolean(form_key, 'spam.check') \ |
79 | and looks_like_spam(context["message"], config, form_key): | 94 | and looks_like_spam(" ".join(to_check), config, form_key): |
80 | start_response('403 Forbidden', [('Content-Type', 'text/plain')]) | 95 | start_response('403 Forbidden', [('Content-Type', 'text/plain')]) |
81 | return "I don't like SPAM!" | 96 | return "I don't like SPAM!" |
82 | except NoOptionError: | 97 | except NoOptionError: |