#!/bin/bash
HAMDB=/home/vpopmail/domains/learn-no-spam.db
SPAMDB=/home/vpopmail/domains/learn-spam.db
SHPAMDB=/home/vpopmail/domains/learn-combined.db
DATE=`date`
FILE_LASTSCAN="lastscan:2,S"
FILE_ATTENTION="attention:2,S"

FROM="postmaster"
TO="reporter"

function create_notices() {
    cat > "$i/$FILE_LASTSCAN" << EOF
Return-Path: <$FROM>
Received: (qmail 4181 invoked by uid 89); 9 Apr 2004 13:26:51 -0000
Message-ID: <20040409132651.4180.qmail@customerinterface.de>
From: "Postmaster" <$FROM>
To: "Reporter" <$TO>
Subject: Last scanned at $DATE
Date: Fri, 09 Apr 2004 15:26:51 +0200
Mime-Version: 1.0
Content-Type: text/plain; format=flowed; charset="utf-8"
Content-Transfer-Encoding: 7bit


Dear user,

thanks for reporting mistakenly classified (no-)spam-mail.
We've scanned your reported mails and added their patterns to our database,
so hopefully they will be detected and correctly identified in the future.

Kind regards,
Your Postmaster
EOF

    cat > "$i/$FILE_ATTENTION" << EOF
Return-Path: <$FROM>
Received: (qmail 4181 invoked by uid 89); 9 Apr 2004 13:26:51 -0000
Message-ID: <20040409132651.4180.qmail@customerinterface.de>
From: "Postmaster" <$FROM>
To: "Reporter" <$TO>
Subject: IMPORTANT NOTICE ABOUT THIS FOLDER
Date: Fri, 09 Apr 2004 15:26:51 +0200
Mime-Version: 1.0
Content-Type: text/plain; format=flowed; charset="utf-8"
Content-Transfer-Encoding: 7bit


Dear user,

eMails in this folder are reported to our Spam-database on a
regular basis.
Please be aware that all emails in this folder are deleted after
they have been successfully scanned.
So in your own interest:

 Please DON'T MOVE mistakenly classified emails to this folder, 
 BUT COPY them here.

Kind regards,
Your Postmaster
EOF
}

function remove_notices() {
    rm "$i/$FILE_LASTSCAN" &> /dev/null
    rm "$i/$FILE_ATTENTION" &> /dev/null
}

function scan_folder() {
     while read i
     do
        remove_notices
     done < $2
    
     nice -n19 sa-learn --no-sync --$1 --folders="$2"
    
     while read i
     do
        cd "$i"
        rm -f ./*
        create_notices
     done < $2
}
                    						
                    						    
find /home/vpopmail/domains/ -path "*learn no-spam" -type d -prune -printf "%h/%f/cur\n" > $HAMDB
find /home/vpopmail/domains/ -path "*learn spam" -type d -prune -printf "%h/%f/cur\n" > $SPAMDB

scan_folder ham $HAMDB
scan_folder spam $SPAMDB
nice -n19 sa-learn --sync

if [ "`id -u`" -eq 0 ]; then
    chown vpopmail.vchkpw /home/vpopmail/.spamassassin/*
fi

rm $HAMDB
rm $SPAMDB
