From 73e35d529ed91ff538a32e5317197a407392404e Mon Sep 17 00:00:00 2001 From: Peter Ludikovsky Date: Thu, 28 Jul 2016 14:54:16 +0200 Subject: Initial commit --- junker.sh | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 junker.sh diff --git a/junker.sh b/junker.sh new file mode 100755 index 0000000..28fdf1a --- /dev/null +++ b/junker.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +[ ! -z "${DEBUG}" ] && set -x + +BASE=$1 +INOTIFY='/usr/bin/inotifywait' +INOTIFY_EVENTS='close_write,moved_to' +INOTIFY_OPTIONS='--monitor --recursive --event'" ${INOTIFY_EVENTS}" +SALEARN=/usr/bin/sa-learn +SAOPTS=--no-sync +LOGS=$2 + +function OpenLog { + exec 3>>"${LOGS}/junker.log" + exec 1>&3 + exec 2>&3 + echo -e "$( date +'%Y%m%d %H:%M:%S' )\tLog opened" +} +function CloseLog { + echo -e "$( date +'%Y%m%d %H:%M:%S' )\tLog closed" + exec 3>&- +} + +function Cleanup { + RunAndLog echo ${SALEARN} --sync + CloseLog + exit 0 +} + +function RunAndLog { + echo -e "$( date +'%Y%m%d %H:%M:%S' )\t$@" + $@ +} + +function Reload { + CloseLog + OpenLog +} + +OpenLog + +trap "Cleanup" EXIT SIGINT SIGTERM +trap "Reload" SIGHUP + +while IFS=' ' read -r dir event file +do + if [[ "${event}" == *MOVED_TO* && "${dir}" == */Junk/ ]] + then + RunAndLog echo ${SALEARN} ${SAOPTS} --forget "${dir}/${file}" + RunAndLog echo ${SALEARN} ${SAOPTS} --spam "${dir}/${file}" + elif [[ "${event}" == *MOVED_TO* && "${dir}" == */!Junk/ ]] + then + RunAndLog echo ${SALEARN} ${SAOPTS} --forget "${dir}/${file}" + RunAndLog echo ${SALEARN} ${SAOPTS} --ham "${dir}/${file}" + elif [[ "${event}" == *CLOSE_WRITE* && "${dir}" == */Junk/ ]] + then + RunAndLog echo ${SALEARN} ${SAOPTS} --spam "${dir}/${file}" + else + RunAndLog echo ${SALEARN} ${SAOPTS} --ham "${dir}/${file}" + fi +done < <( ${INOTIFY} ${INOTIFY_OPTIONS} --format '%w %e %f' ${BASE} ) -- cgit v1.2.3