-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan_reported_mails.sh
executable file
·96 lines (73 loc) · 3.06 KB
/
scan_reported_mails.sh
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
#!/bin/bash
# Modified by Stuart Espey, for mailcow usage
# Original Author: Zhang Huangbin <[email protected]> - https://docs.iredmail.org/dovecot.imapsieve.html
# Purpose: Copy spam/ham to another directory and call sa-learn to learn.
# ensure script only runs exclusively, additional invocations return immediately
[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" || :
OWNER="vmail"
GROUP="vmail"
# Logging to syslog with 'logger' command.
LOG='logger -p mail.info -t dovecot scan_reported_mails: '
# tools that we pipe spam/ham thru
LEARN_SPAM_TOOL="/usr/lib/dovecot/sieve/rspamd-pipe-spam"
LEARN_HAM_TOOL="/usr/lib/dovecot/sieve/rspamd-pipe-ham"
# Spool directory.
# Must be owned by vmail:vmail.
SPOOL_DIR='/var/vmail/imapsieve_copy'
# Directories which store spam and ham emails.
# These 2 should be created while setup Dovecot antispam plugin.
SPOOL_SPAM_DIR="${SPOOL_DIR}/spam"
SPOOL_HAM_DIR="${SPOOL_DIR}/ham"
# Directory used to store emails we're going to process.
# We will copy new spam/ham messages to these directories, scan them, then
# remove them.
SPOOL_LEARN_DIR="${SPOOL_DIR}/processing"
SPOOL_LEARN_SPAM_DIR="${SPOOL_LEARN_DIR}/spam"
SPOOL_LEARN_HAM_DIR="${SPOOL_LEARN_DIR}/ham"
HAM_COUNT=0
SPAM_COUNT=0
# create learn dirs if they don't exist
for dir in "${SPOOL_DIR}" "${SPOOL_LEARN_DIR}" "${SPOOL_LEARN_SPAM_DIR}" "${SPOOL_LEARN_HAM_DIR}"; do
if [[ ! -d ${dir} ]]; then
mkdir -p ${dir}
fi
chown ${OWNER}:${GROUP} ${dir}
chmod 0700 ${dir}
done
# move any fresh spam/ham to correct learn dirs
[[ -d ${SPOOL_SPAM_DIR} ]] && find ${SPOOL_SPAM_DIR} -name '*.eml' -exec mv -t ${SPOOL_LEARN_SPAM_DIR}/ {} +
[[ -d ${SPOOL_HAM_DIR} ]] && find ${SPOOL_HAM_DIR} -name '*.eml' -exec mv -t ${SPOOL_LEARN_HAM_DIR}/ {} +
# Try to delete empty directory, if failed, that means we have some messages to scan.
rmdir ${SPOOL_LEARN_SPAM_DIR} &>/dev/null
if [[ X"$?" != X'0' ]]; then
# for every file in the learn spam dir, pipe thru the learn spam tool, and log output
for i in $(find ${SPOOL_LEARN_SPAM_DIR} -name '*.eml'); do
((++SPAM_COUNT))
EML_NAME=$(basename ${i})
echo "spam: ${EML_NAME}"
output="$(cat ${i} | ${LEARN_SPAM_TOOL})"
${LOG} '[SPAM]' ${EML_NAME} ${output}
done
rm -rf ${SPOOL_LEARN_SPAM_DIR} &>/dev/null
fi
rmdir ${SPOOL_LEARN_HAM_DIR} &>/dev/null
if [[ X"$?" != X'0' ]]; then
# for every file in the learn ham dir, pipe thru the learn ham tool, and log output
for i in $(find ${SPOOL_LEARN_HAM_DIR} -name '*.eml'); do
((++HAM_COUNT))
EML_NAME=$(basename ${i})
echo "ham: ${EML_NAME}"
output="$(cat ${i} | ${LEARN_HAM_TOOL})"
${LOG} '[HAM]' ${EML_NAME} ${output}
done
rm -rf ${SPOOL_LEARN_HAM_DIR} &>/dev/null
fi
# cleanup processing dir, if empty
rmdir ${SPOOL_LEARN_DIR} &>/dev/null
TOTAL_COUNT=$(($SPAM_COUNT + $HAM_COUNT))
# log to dovecot log and jobrunner log
if [[ $TOTAL_COUNT -gt 0 ]]; then
OUTPUT="learned ${TOTAL_COUNT} mails: ${SPAM_COUNT} spam, ${HAM_COUNT} ham"
echo ${OUTPUT}
${LOG} ${OUTPUT}
fi