From 3969d987bd7daf12306e8e6cae372d4d307298a7 Mon Sep 17 00:00:00 2001 From: Daniel Demus Date: Sun, 19 Jan 2020 19:38:16 +0100 Subject: [PATCH] First implementation --- README.md | 34 ++++++++++- sa-bayes-learn.sh | 10 ---- sa-train-group.conf | 2 + sa-train-user.sh | 51 +++++++++-------- sa-train.conf | 12 ++++ sa-train.ipr | 134 ++++++++++++++++++++++++++++++++++++++++++++ sa-train.iws | 92 ++++++++++++++++++++++++++++++ sa-train.service | 2 +- sa-train.sh | 26 +++++++++ sa-train.spec | 78 ++++++++++++++++++++++++++ 10 files changed, 406 insertions(+), 35 deletions(-) delete mode 100755 sa-bayes-learn.sh create mode 100644 sa-train-group.conf create mode 100644 sa-train.conf create mode 100644 sa-train.ipr create mode 100644 sa-train.iws create mode 100755 sa-train.sh create mode 100644 sa-train.spec diff --git a/README.md b/README.md index 416a791..aa873ba 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,34 @@ # sa-train -A bash script and systemd setup that run sa-learn on mailbox folders +A bash script and systemd setup that run sa-learn on all mailbox folders for users that +are members of the satrain group. + +It is assumed that read mails in a Spam folder in the root of a user's maildir has spam, +while everthing else is ham. A directory called SpamSuspect is assumed to contain mail +that needs to be manually categorised by being moved into Spam or another folder. + +All the specific names can be configured in either */etc/sa-train.conf* or +*~/.sa-train/config* where the user configuration overrides the system configuration. + +**SETTINGS:** + +Possible configuration values are (with the default value if not otherwise specified +in config): + +**spam_folder .Spam** + The subfolder in the user's mailbox folder that contains mail to train as spam. + Only read mails are used, so if the MTA delivers mail marked as spam by spamassassin + to this folder as unread, the user can verify them by marking them as read, after + which sa-train will add them to the bayesian filter. + +**max_age 90** + The maximum age in days of mails to train spamassassin with. This is probably only + relevant the first time training is performed. Normally sa-train only examines mails + that have a modified date more recent than the newest mail in the last training + session. To re-train spamassassin remove the ~/.sa-train/last* files, delete the + bayesian database and run sa-train.sh as root. + +**spammed_group sa-train** + The name of the unix group, the members of which will have their mailboxes used to + train the spamassassin bayesian filter. Each users filter is trained using their + specific mailboxes, so the filtering conform to their patterns of spam. + *This setting is only relevant in the global config file and is ignored in the user configs.* diff --git a/sa-bayes-learn.sh b/sa-bayes-learn.sh deleted file mode 100755 index 880f33d..0000000 --- a/sa-bayes-learn.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash -# Learn spam from Spam Maildir folder, ham from INBOX -/usr/bin/sa-learn --spam --no-sync ~/Maildir/.Spam/cur -/usr/bin/sa-learn --ham --no-sync ~/Maildir/cur -/usr/bin/sa-learn --sync - -# Removes all files from ~/Maildir/.Spam/cur that are older than -# 31 days ago - -find ~/Maildir/.Spam/cur -mtime +30 -exec rm -f {} \; diff --git a/sa-train-group.conf b/sa-train-group.conf new file mode 100644 index 0000000..cfef0b8 --- /dev/null +++ b/sa-train-group.conf @@ -0,0 +1,2 @@ +# The default group of spamassassin trainers +g sa-train - - diff --git a/sa-train-user.sh b/sa-train-user.sh index 3584816..3c4d7c0 100644 --- a/sa-train-user.sh +++ b/sa-train-user.sh @@ -1,53 +1,58 @@ #!/bin/bash shopt -s extglob -while getopts u:d:p:f: option -do -case "${option}" -in -u) USER=${OPTARG};; -d) DATE=${OPTARG};; -p) PRODUCT=${OPTARG};; -f) FORMAT=${OPTARG};; -esac -done +if [ -r /etc/sa-train.conf ]; then + while read -r name value; do + if [[ -n "$name" && ! "$name" =~ "$\w*#" ]]; then + typeset "$name=$value" + fi + done < /etc/sa-train.conf +fi + +if [ -r ~/.sa-train/config ]; then + while read -r name value; do + if [[ -n "$name" && ! "$name" =~ "$\w*#" ]]; then + typeset "$name=$value" + fi + done < ~/.sa-train/config +fi + +local SPAMFOLDER=${spam_folder:-.Spam} +typeset -i "MAX_AGE=${max_age:-90}" function examine { local subdir=$1 - if [[ -n "$subdir" ]]; then + if [[ -n "$subdir" -a "$subdir" == "${subdir%/}" ]]; then subdir="$subdir/" fi - if [[ ! -d ./${subdir}cur ]]; then + if [[ ! -d ./${subdir}cur ]]; then return fi - local action=ham - if [[ ! -z "$2" ]]; then - action=$2 - fi + local action=${2:-ham} echo Learning from $subdir as $action pushd ${subdir}cur - if [[ -f ~/.sa-learn/last$1 ]]; then - find -H . -type f -regex ".*,[^,]*S[^,]*$" -newer ~/.sa-learn/last$1 -exec sa-learn --$action --no-sync {} \+; + if [[ -f ~/.sa-train/last$1 ]]; then + find -H . -type f -regex ".*,[^,]*S[^,]*$" -newer ~/.sa-train/last$1 -exec sa-learn --$action --no-sync {} \+; else - find . -type f -regex ".,[^,]*S[^,]*$" -mtime -30 -exec sa-learn --$action --no-sync {} \+; + find . -type f -regex ".,[^,]*S[^,]*$" -mtime -$MAX_AGE -exec sa-learn --$action --no-sync {} \+; fi - rm -f ~/.sa-learn/last$1 + rm -f ~/.sa-train/last$1 if [[ $( ls *,*([!,])S*([!,]) 2>/dev/null ) ]]; then - ln -s $( realpath $( ls -1t *,*([!,])S*([!,]) | head -1) ) ~/.sa-learn/last$1; + ln -s $( realpath $( ls -1t *,*([!,])S*([!,]) | head -1) ) ~/.sa-train/last$1; fi popd } if [ -d ~/Maildir ]; then - mkdir -p ~/.sa-learn + mkdir -p ~/.sa-train pushd ~/Maildir - examine .Spam spam + examine .$SPAMFOLDER spam examine examine .Root for inboxdir in ./.INBOX*; do diff --git a/sa-train.conf b/sa-train.conf new file mode 100644 index 0000000..7c00927 --- /dev/null +++ b/sa-train.conf @@ -0,0 +1,12 @@ +# An example config file for sa-train. Copy it to +# ~/.sa-learn/config and uncomment the relevant lines +# to create user specific configuration + +# Unix group that will be trained +# spammed_group sa-train + +# The user mailbox spam subfolder +# spam_folder .Spam + +# The maximum age in days of mails to train spamassassin with +# max_age 90 diff --git a/sa-train.ipr b/sa-train.ipr new file mode 100644 index 0000000..0d09a77 --- /dev/null +++ b/sa-train.ipr @@ -0,0 +1,134 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CodeNarc - Ruleset rulesets/imports.xml + + + CodeNarc - Ruleset rulesets/unused.xml + + + Data flowGroovy + + + Declaration redundancyGroovy + + + Error handlingGroovy + + + GSPGrailsGroovy + + + GrailsGroovy + + + Groovy + + + + + YAML + + + + + + + + + + + + \ No newline at end of file diff --git a/sa-train.iws b/sa-train.iws new file mode 100644 index 0000000..85fa715 --- /dev/null +++ b/sa-train.iws @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +