UserPreferences

PycMail


1. spambayes with pycmail

Pycmail is a mail filtering program for Unix systems written in Python. It can be downloaded from http://melkor.dnp.fmph.uniba.sk/~garabik/pycmail/ and is also available as a Debian package.

As it is configured by a python script ~/.pycmailrc, spambayes can be seamlessly integrated and uses the already running python process.

An example .pycmailrc would look like:
  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 
# pycmailrc with spambayes integration
import os, sys, textwrap
from spambayes import hammie, hammiebulk, storage


md = USERHOME+"/Mail/"

# --- Sorting and whitelist ---
#
# (this rules serve also as whitelist, put other sorting below the spam-filter)

if InHeader("List-Post", "docutils"):
    Set(MailDir(md+"docutils-in"))
    Stop()

# --- Blacklist ---

if InHeader("X-TUD-Spam-Level", "****") \
or InHeader("From",  "virusalert@tu-dresden.de"):
    Set(DevNull())
#    Set(MailDir(md+"spam"))
    Stop()

# interface to spambayes

# # lock to have only one invocation per time (with dotlock from pycmail)
# bayeslockfile = ".spambayes"
# 
# if not(dotlocked(bayeslockfile)):
#     dotlock(bayeslockfile, sleeptime=8, retries=5, locktimeout=60, suspend=0)

db_name, db_type = storage.database_type([])
Debug("Database: %s, Type %s"%(db_name, db_type), 5)

h = hammie.open(db_name, db_type) # database

# Test the message (in string variable msg)
spam_score, clues = h.score(msg, True)

del(h)

# remove the lock
# dotunlock(bayeslockfile)

Debug("spam: %f, ham: %f"%(hammiebulk.SPAM_THRESHOLD, hammiebulk.HAM_THRESHOLD), 4)
Debug(textwrap.fill("'%s' from '%s' got score %f"\
                        %(SUBJECT, FROM, spam_score), subsequent_indent=" "*7), 2)

if (spam_score >= hammiebulk.SPAM_THRESHOLD):
    Debug("spambayes: Spam", 3)
    Set(MailDir(md+"spam"))     # sort to spam folder
    Stop()
elif (spam_score <= hammiebulk.HAM_THRESHOLD):
    Debug("spambayes: Ham", 3)
    Stop()                      # leave in default folder
else:
    Debug("spambayes: Unsure", 3)
    Set(MailDir(md+"inbox"))     # sort to incoming (unsure) folder
    Stop()