howtos:dspam_ham-spam_learning_script
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
Previous revision | |||
— | howtos:dspam_ham-spam_learning_script [15/02/2023 17:38] (current) – [train-spam.sh] domingo | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Training script for Dspam ====== | ||
+ | This script will go through all mail users in a predefined maildir directory and look for two imap folders: Spam and NotSpam. | ||
+ | Mail in the Spam folder will be processed as missed spam and dealt with accordingly by Dspam. The opposite is the case for mail put into NotSpam. | ||
+ | |||
+ | ===== Usage ===== | ||
+ | Create the appropriate folders in the users maildir (or tell them to do so if they want to play along) and when ever a mail is missed or miss interpret move/copy it to the Spam or NotSpam folder. | ||
+ | |||
+ | This is the beauty of the script, if you don't make the folders you opt-out and if you only make one of the folders you only choose to that kind of training. | ||
+ | |||
+ | You then setup a cron job running once a day (or more frequent if you like), which will run through the folders, train and clean them up. | ||
+ | |||
+ | Remember to copy HAM to the NotSpam folder as you otherwise will loose the mail. | ||
+ | |||
+ | ===== train-spam.sh ===== | ||
+ | |||
+ | |||
+ | < | ||
+ | #!/bin/bash | ||
+ | # | ||
+ | # train-spam.sh | ||
+ | # | ||
+ | # Description: | ||
+ | # the folder " | ||
+ | # the user placed any " | ||
+ | # through Dspam to their INBOX. | ||
+ | # If there are messages in this directory, then the script | ||
+ | # invokes dspam to update the site-wide tokens to try | ||
+ | # and improve the defenses for next time... | ||
+ | # | ||
+ | |||
+ | # learn_spam - Function which takes a directory and a user as | ||
+ | # arguments, and then feeds that directory to our anti-spam | ||
+ | # applications for further SPAM training. | ||
+ | # | ||
+ | # Arguments: | ||
+ | # $1 - Directory name containing SPAM emails. Required | ||
+ | # $2 - Dspam user name. Required | ||
+ | # | ||
+ | # Example: | ||
+ | # learn_spam / | ||
+ | # | ||
+ | function learn_spam { | ||
+ | |||
+ | # set file location for dspam binary | ||
+ | DSPAM=/ | ||
+ | |||
+ | # loop through all emails in given directory | ||
+ | for email in $(ls $1); do | ||
+ | |||
+ | # process SPAM email using DSPAM | ||
+ | $DSPAM --source=error --class=spam --user $2 < $1/$email | ||
+ | echo -n " | ||
+ | |||
+ | # delete SPAM email | ||
+ | rm $1/$email | ||
+ | |||
+ | done # end of email loop | ||
+ | |||
+ | } # end function learn_spam | ||
+ | |||
+ | # learn_ham - Function which takes a directory and a user as | ||
+ | # arguments, and then feeds that directory to our anti-spam | ||
+ | # applications for further HAM training. | ||
+ | # | ||
+ | # Arguments: | ||
+ | # $1 - Directory name containing HAM emails. Required | ||
+ | # $2 - Dspam user name. Required | ||
+ | # | ||
+ | # Example: | ||
+ | # learn_spam / | ||
+ | # | ||
+ | function learn_ham { | ||
+ | |||
+ | # loop through all emails in given directory | ||
+ | for email in $(ls $1); do | ||
+ | |||
+ | # process HAM email using DSPAM | ||
+ | / | ||
+ | echo -n " | ||
+ | |||
+ | # delete HAM | ||
+ | rm $1/$email | ||
+ | |||
+ | done # end of email loop | ||
+ | |||
+ | } # end function learn_ham | ||
+ | |||
+ | # | ||
+ | # Script starts here! | ||
+ | # | ||
+ | # Our maildir location | ||
+ | maildir=/ | ||
+ | # Static Dspam user | ||
+ | user=" | ||
+ | |||
+ | # loop through all users maildir | ||
+ | for domain in $(ls $maildir); do | ||
+ | |||
+ | for userdir in $(ls $maildir/ | ||
+ | # if there is a Spam/Missed maildir | ||
+ | if [ -d $maildir/ | ||
+ | # then process any missed SPAM | ||
+ | echo -n " | ||
+ | #If using individual spam/ham settings | ||
+ | # | ||
+ | #learn_spam $maildir/ | ||
+ | #I'm only using one user for spam learning | ||
+ | learn_spam $maildir/ | ||
+ | learn_spam $maildir/ | ||
+ | | ||
+ | echo "" | ||
+ | fi | ||
+ | # if there is a Spam/ | ||
+ | if [ -d $maildir/ | ||
+ | |||
+ | # then process any falsely identified spam, i.e. HAM | ||
+ | echo -n "false positives for $maildir/ | ||
+ | #If using individual spam/ham settings | ||
+ | # | ||
+ | #learn_ham $maildir/ | ||
+ | #I'm only using one user for spam learning | ||
+ | learn_ham $maildir/ | ||
+ | learn_ham $maildir/ | ||
+ | echo "" | ||
+ | |||
+ | fi # end if | ||
+ | done | ||
+ | |||
+ | |||
+ | |||
+ | done # end for loop | ||
+ | |||
+ | echo " | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Original script by Alan ===== | ||
+ | |||
+ | |||
+ | < | ||
+ | |||
+ | #!/bin/sh | ||
+ | |||
+ | # train-spam.sh | ||
+ | # | ||
+ | # Description: | ||
+ | # directories to see if the user placed any " | ||
+ | # messages which got through SpamAssassin to their INBOX. | ||
+ | # If there are messages in this directory, then the script | ||
+ | # invokes sa-learn to update the site-wide tokens to try | ||
+ | # and improve the defenses for next time... | ||
+ | # | ||
+ | |||
+ | # learn_spam - Function which takes a directory and a user as | ||
+ | # arguments, and then feeds that directory to our anti-spam | ||
+ | # applications for further SPAM training. | ||
+ | # | ||
+ | # Arguments: | ||
+ | # $1 - Directory name containing SPAM emails. Required | ||
+ | # $2 - User name. If it is not provided, $USER will be used. | ||
+ | # | ||
+ | # Example: | ||
+ | # learn_spam / | ||
+ | # | ||
+ | function learn_spam { | ||
+ | |||
+ | # loop through all emails in given directory | ||
+ | for email in $(ls $1); do | ||
+ | |||
+ | # process SPAM email using DSPAM | ||
+ | / | ||
+ | echo -n " | ||
+ | |||
+ | # delete SPAM email | ||
+ | rm $1/$email | ||
+ | |||
+ | done # end of email loop | ||
+ | |||
+ | } # end function learn_spam | ||
+ | |||
+ | # learn_ham - Function which takes a directory and a user as | ||
+ | # arguments, and then feeds that directory to our anti-spam | ||
+ | # applications for further HAM training. | ||
+ | # | ||
+ | # Arguments: | ||
+ | # $1 - Directory name containing HAM emails. Required | ||
+ | # $2 - User name. If it is not provided, $USER will be used. | ||
+ | # | ||
+ | # Example: | ||
+ | # learn_ham / | ||
+ | # | ||
+ | function learn_ham { | ||
+ | |||
+ | # loop through all emails in given directory | ||
+ | for email in $(ls $1); do | ||
+ | |||
+ | # process HAM email using DSPAM | ||
+ | / | ||
+ | echo -n " | ||
+ | |||
+ | # delete HAM | ||
+ | rm $1/$email | ||
+ | |||
+ | done # end of email loop | ||
+ | |||
+ | } # end function learn_ham | ||
+ | |||
+ | # | ||
+ | # Script starts here! | ||
+ | # | ||
+ | |||
+ | # loop through all user home directories | ||
+ | for file in $(ls /home); do | ||
+ | |||
+ | # if there is a Spam/Missed maildir | ||
+ | if [ -d / | ||
+ | |||
+ | # then process any missed SPAM | ||
+ | echo -n " | ||
+ | learn_spam / | ||
+ | learn_spam / | ||
+ | echo "" | ||
+ | |||
+ | fi # end if | ||
+ | |||
+ | # if there is a Spam/ | ||
+ | if [ -d / | ||
+ | |||
+ | # then process any falsely identified spam, i.e. HAM | ||
+ | echo -n "false positives for $file: " | ||
+ | learn_ham / | ||
+ | learn_ham / | ||
+ | echo "" | ||
+ | |||
+ | fi # end if | ||
+ | |||
+ | done # end for loop | ||
+ | |||
+ | echo " | ||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | Source: http:// |
howtos/dspam_ham-spam_learning_script.txt · Last modified: 15/02/2023 17:38 by domingo