Skip to content

Instantly share code, notes, and snippets.

@magcks
Last active February 16, 2017 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magcks/19f7ffbc41eb0a88be29048dd850ac7f to your computer and use it in GitHub Desktop.
Save magcks/19f7ffbc41eb0a88be29048dd850ac7f to your computer and use it in GitHub Desktop.
#!/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