-
Notifications
You must be signed in to change notification settings - Fork 0
/
emlv-report
executable file
·140 lines (110 loc) · 2.43 KB
/
emlv-report
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
133
134
135
136
137
138
139
140
#!/bin/bash
set -u
. /usr/lib/tool/bash-utils || exit -1
spam_dir_basename=Spam
EMLV_REPORT_TYPE=$1
shift
case "$EMLV_REPORT_TYPE" in
(spam) true;;
(ham) true;;
(*) errx -1 "unknown report type: $EMLV_REPORT_TYPE";;
esac
files_to_report=("$@")
commands=()
args=()
for EMLV_EMAIL_FILE in "${files_to_report[@]}"
do
case "$EMLV_REPORT_TYPE" in
(spam)
commands+=("razor-report -d -f")
args+=("$EMLV_EMAIL_FILE")
commands+=("pyzor-files -d report --")
args+=("$EMLV_EMAIL_FILE")
commands+=("bogofilter -s -v -B")
args+=("$EMLV_EMAIL_FILE")
;;
(ham)
commands+=("razor-revoke -d -f")
args+=("$EMLV_EMAIL_FILE")
commands+=("pyzor-files -d whitelist --")
args+=("$EMLV_EMAIL_FILE")
commands+=("bogofilter -n -v -B")
args+=("$EMLV_EMAIL_FILE")
;;
esac
done
# start commands in the background
pids=()
for idx in "${!commands[@]}"
do
cmd=${commands[$idx]}
arg=${args[$idx]}
command $cmd "$arg" &
pids+=($!)
done
# wait for all background process to finish
# and gather exit codes
exitcodes=()
for idx in "${!commands[@]}"
do
pid=${pids[$idx]}
wait $pid
exitcode=$?
exitcodes[$idx]=$exitcode
done
# report if all command ran successfully
# or which ones failed.
all_ok=1
errorlevel=0
unprocessed_files=()
for idx in "${!commands[@]}"
do
exitcode=${exitcodes[$idx]}
if [ $exitcode = 0 ]
then
true
else
all_ok=0
if [ $errorlevel -lt $exitcode ]; then errorlevel=$exitcode; fi
printf "error %3d: %s %s\n" $exitcode "${commands[$idx]}" "${args[$idx]}"
unprocessed_files+=("${args[$idx]}")
fi
done
if [ $all_ok = 1 ]
then
warnx "All command ran successfully."
fi
# move files reported as spam to the Spam folder (create one in the directory where the file was),
# and move ham files out of the Spam folder if they accidentaly happened to be there.
for path in "${files_to_report[@]}"
do
if in_list "$path" "${unprocessed_files[@]}"
then
# don't move the file if any of the commands it was passed to as an argument failed.
continue
fi
if [ ! -e "$path" ]
then
# it must have been moved already.
continue
fi
realpath=`readlink -f "$path"`
dirpath=`dirname "$realpath"`
dirname=`basename "$dirpath"`
case "$EMLV_REPORT_TYPE" in
(spam)
if [ "$dirname" != "$spam_dir_basename" ]
then
spamdir=$dirpath/$spam_dir_basename
mkdir -p "$spamdir"
mv -n "$path" "$spamdir/"
fi
;;
(ham)
if [ "$dirname" = "$spam_dir_basename" ]
then
mv -n "$path" "$dirpath/../"
fi
;;
esac
done