summaryrefslogtreecommitdiff
path: root/junker.sh
blob: 964f406c8fa0bfa00e9f75331f46991e56a02e28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash

[ ! -z "${DEBUG}" ] && set -x

USER=${1:-$( id -un )}
HOME="$( getent passwd $USER | cut -d ':' -f 6 )"
BASE="${HOME}"/Maildir
INOTIFY='/usr/bin/inotifywait'
INOTIFY_EVENTS='moved_to,moved_from'
INOTIFY_OPTIONS='--monitor --recursive --event'" ${INOTIFY_EVENTS}"
LOG=/var/log/junker/${USER}.log
SPAMC='/usr/bin/spamc'

JUNK="${BASE}/Junk"
NONJUNK="${BASE}/Junk"
TRASH="${BASE}/Trash"
SENT="${BASE}/Sent"
DRAFTS="${BASE}/Drafts"

if [[ -f "${HOME}/.junkerrc" && -r "${HOME}/.junkerrc" ]]
then
    . "${HOME}/.junkerrc"
fi

function OpenLog {
    exec 3>>"${LOG}"
    exec 1>&3
    exec 2>&3
    Log "Log opened"
}
function CloseLog {
    Log "Log closed"
    exec 3>&-
}
function Log {
    echo -e "$( date +'%Y%m%d %H:%M:%S' )\t$@"
}

function Cleanup {
    CloseLog
    exit 0
}

function Learn {
    [[ "$1" != "ham" && "$1" != "spam" ]] && return 3
    [[ ! -f "$2" || ! -r "$2" ]] && return 4
    Log "Learning $2 as $1"
    exec 9<"$2"
    ${SPAMC} --learntype $1 <&9
    ret=$?
    case $ret in
        0) Log "Done"
            ;;
        5) Log "Learned"
            ;;
        6) Log "Already known"
            ;;
        *) Log "Error occured, code $ret"
            ;;
    esac
    exec 9<&-
}

function Reload {
    CloseLog
    OpenLog
}

if [ ! -d "${BASE}" ]
then
    echo "Error: Mail directory not found at ${BASE}, exiting" >&2
    exit 1
fi
if [ ! -d "$( dirname ${LOG} )" ]
then
    echo "Error: Log directory not found at ${LOG}, exiting" >&2
    exit 1
fi
if [ ! -x ${INOTIFY} ]
then
    echo "Error: inotifywait not found, check path" >&2
    exit 1
fi
if [ ! -x ${SPAMC} ]
then
    echo "Error: spamc not found, check path" >&2
    exit 1
fi
checkfile=$( mktemp )
${SPAMC} --learntype forget <$checkfile
if [ $? -eq 74 ]
then
    echo "Error: spamd doesn't allow connections, needs --allow-tell" >&2
    exit 1
fi

OpenLog

trap "Cleanup" EXIT SIGINT SIGTERM
trap "Reload" SIGHUP

while IFS='	' read -r dir event file
do
    if [[ "${dir}" != */cur/ && "${dir}" != */new/ ]]
    then
        continue
    fi
    case "${dir}" in
        ${TRASH}/*)
            ;&
        ${SENT}/*)
            ;&
        ${DRAFTS}/*)
            continue
    esac
    if [[ "${event}" == *MOVED_TO* && "${dir}" == "${JUNK}"/cur/ ]]
    then
        Learn "spam" "${dir}/${file}"
    elif [[ "${event}" == *MOVED_TO* && "${dir}" == "${JUNK}"/new/ ]]
    then
        Learn "spam" "${dir}/${file}"
    elif [[ "${event}" == *MOVED_TO* && "${dir}" == "${NONJUNK}"/cur/ ]]
    then
        Learn "ham" "${dir}/${file}"
    elif [[ "${event}" == *MOVED_FROM* && "${dir}" == "${JUNK}"/cur/ ]]
    then
        Learn "ham" "${dir}/${file}"
    elif [[ "${event}" == *MOVED_TO* && "${dir}" == ${BASE}/*/new/ ]]
    then
        Learn "ham" "${dir}/${file}"
    fi
done < <( ${INOTIFY} ${INOTIFY_OPTIONS} --format '%w	%e	%f' ${BASE} )