edeca.net logo
  • rss
  • Home
  • Graphics
  • Electronics
  • Computing
    • ROT Util
    • MOTD maker
    • GNU screen config
    • VB6 reverse DNS
    • VB6 RichTextBox control
  • About

Training dspam from Thunderbird junk messages

David | February 21, 2010

Recently I have installed and configured dspam on my mailserver. It seems to work nicely but needs occasional training. I wanted to integrate this with Thunderbird so that users could automatically train dspam from their mail client.

Based on this code I knocked together a few lines of bash script which will scan junk mail directories on the server and automatically train dspam. This means that an end-user can click the “Junk” button in Thunderbird (or Mail.app, etc) and dspam will be trained for them automagically. The user could even just move the messages there manually, or use some sort of filtering or an extension.

The best bit is that it is completely transparent to the end user and doesn’t require them to forward messages with headers intact to a weird user-spam@example.net address in order to conduct training.

If you find it useful or make changes, please let me know in the comments below.

The code

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
#!/bin/bash

########
# TrainDspam.sh
#
#   Author: David Cannings <david @edeca.net>
#     Date: 21/02/2010
# Based on: http://tinyurl.com/yhky5w9
#
# This script scans mail directories for the Thunderbird "Junk" folder
# (or any other folder with the same name) and trains dspam with the
# messages contained within it.
#
# It can be used for periodic (e.g. daily) training of spam messages which
# a user has flagged as junk in their mail client.
#
# It will only train for accounts which appear to be using dspam.
########

########
# Configuration

# Path to mail directory, which should contain folders per domain and
# user e.g. /home/mail/<domain>/<user1>/
MAIL_PATH="/home/mail"

# Path to the directory containing 'dspam'
DSPAM_BIN_DIR="/usr/bin"

# Path to the directory containing the dspam user data files
DSPAM_DATA_DIR="/var/spool/dspam"

# If you want this script to delete messages from the Junk folder
# after training, set this to 1.
DELETE_MAIL=0

# DON'T EDIT BELOW THIS LINE
########

for FOLDER in `find $MAIL_PATH -name '.Junk' -type d -print`; do
 DOMAIN=`echo $FOLDER | awk -F/ '{print $(NF-3)}'`
 USER=`echo $FOLDER | awk -F/ '{print $(NF-2)}'`

 # We only want to train for accounts that are dspam users,
 # so check the data directory
 if [ -d "${DSPAM_DATA_DIR}/data/${DOMAIN}/${USER}" ]; then
 TRAINED_MESSAGES=0
 cd $FOLDER/cur/
 for MESSAGE in `ls -1`; do
   TRAINED_MESSAGES=`expr $TRAINED_MESSAGES + 1`
   cat $MESSAGE | $DSPAM_BIN_DIR/dspam --user ${USER}@${DOMAIN} --class=spam --source=error
   [ $DELETE_MAIL -gt 0 ] && rm -f $NAME
 done
 echo "- Trained $TRAINED_MESSAGES messages for ${USER}@${DOMAIN}"
 fi
done

Categories
Computing
Tags
dspam, linux
Comments rss
Comments rss
Trackback
Trackback

« Updates to libnids dspam integration with dovecot »

One Response to “Training dspam from Thunderbird junk messages”

  1. edeca.net » dspam integration with dovecot says:
    March 19, 2010 at 7:23 pm

    [...] a previous message I spoke about training dspam automatically. The script I posted there works, but it’s not [...]

Leave a Reply

Click here to cancel reply.

Categories

  • Computing
  • Electronics
  • General
  • Perl
  • Photography

Archives

  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • November 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009

Links

  • My photo gallery
  • Pookey's site

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox