Last active
February 16, 2017 17:54
-
-
Save magcks/19f7ffbc41eb0a88be29048dd850ac7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# visudo: | |
# vmail ALL= (root) NOPASSWD: /usr/bin/sa-learn | |
time=5 | |
spoolsalearn="/var/spool/salearn" | |
lvl="info" | |
type="spam" | |
address="unknown@example.invalid" | |
for i in "$@"; do | |
case $i in | |
--spam) | |
type="spam" | |
shift | |
;; | |
--ham) | |
type="ham" | |
shift | |
;; | |
--username=*) | |
address="${i#*=}" | |
shift | |
;; | |
*) | |
# unknown | |
;; | |
esac | |
done | |
domain="${address#*@}" | |
user="${address%@*}" | |
sub="$domain/$user/$type" | |
pid=$$ | |
subdir="$spoolsalearn/$sub" | |
newdir="$subdir/new" | |
# write mail | |
mkdir -p $newdir | |
chmod 750 "$spoolsalearn/$domain" | |
chmod 750 "$spoolsalearn/$domain/$user" | |
chmod 750 "$subdir" | |
chmod 750 "$newdir" | |
mail="$newdir/$pid.msg" | |
touch $mail.pending | |
chmod 660 $mail.pending | |
cat > $mail.pending | |
mv $mail.pending $mail | |
logger -p mail.$lvl -i -t sa-learn-pipe "saved mail to $mail" | |
touch $subdir/newmail | |
( | |
if ! lockfile-create --use-pid -r 0 $subdir/learner; then | |
exit 0 | |
fi 2> /dev/null | |
# sleep until there are no changes in a 5 second frame | |
while [ -f $subdir/newmail ]; do | |
rm $subdir/newmail | |
sleep $time | |
done | |
mails=$subdir/cur-$pid | |
mkdir $mails | |
chmod 750 $mails | |
mv $newdir/*.msg $mails # move all completely written mails | |
lockfile-remove $subdir/learner | |
logger -p mail.$lvl -i -t sa-learn-pipe "executing sa-learn for user $address with type $type on $mails/*" | |
# echo "learning mails as $sub:" | |
# ls $mails | |
res=$(sudo -Hu root sa-learn --dbpath=/var/lib/amavis/.spamassassin --$type --username=$address $mails/* 2>&1) | |
logger -p mail.$lvl -i -t sa-learn-pipe "executing sa-learn for user $address with type $type result: $res" | |
# rm -fr $mails | |
) & | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment