blob: 764f5eb76dc545536bad633406c2218cc7ba8908 (
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
#!/bin/bash
set +e
verwendung() {
>&2 echo 'digest-mailer collects emails into digests before sending them.'
>&2 echo ''
>&2 echo 'Usage: digest-mailer [OPTIONS]'
>&2 echo ' -f:'
>&2 echo ' flush all pending emails'
>&2 echo ' -f $receiver:'
>&2 echo ' flush pending emails to $receiver (hash)'
>&2 echo \
'#HELPTEXT# #'
exit 1
}
extract_receiver_hash() {
sed -n '
/^$/q
s/^To:\s*//
T
p
' | \
sha224sum | \
awk '{print $1}'
}
if [ "x$1" = 'x--help' ]; then
verwendung
fi
if [ "x$1" = 'x--version' ]; then
echo '#VERSION#'
exit
fi
if [ "x$1" = 'x-f' ]; then
flush=true
shift
if [ $# -eq 0 ]; then
receiver=''
else
receiver="$1"
shift
fi
else
flush=false
fi
if [ $# -ne 0 ]; then
verwendung
fi
min_delay=3600
max_delay=3600
agressivity=0
mail_cmd='sendmail -t'
if [ -r '#ETCDIR#/digest-mailer.conf' ]
then
. '#ETCDIR#/digest-mailer.conf'
fi
sender=$(
whoami | \
sha224sum | \
awk '{print $1}'
)
if ${flush} && \
[ -z "${receiver}" ]; then
exec 9> '#LIBDIR#/digest-mailer/digest-mailer.flush.from.'"${sender}"'.lock'
if ! flock -n 9; then
# silently fail if lock is not available ("flush" is not _that_ important)
exit
fi
find '#LIBDIR#/digest-mailer' -regextype grep -maxdepth 1 -mindepth 1 \
-regex ".*/${sender}\.[0-9a-f]\+\.[0-9]\+\.[0-9a-f]\+\$" | \
while read -r file; do
extract_receiver_hash < "${file}"
done | \
sort -u | \
xargs -rn1 "$0" -f
exit
fi
time=$(
date +%s
)
if ! ${flush}; then
message=$(
cat
)
receiver=$(
printf '%s\n' "${message}" | \
extract_receiver_hash
)
hash=$(
printf '%s' "${message}" | \
sha224sum | \
awk '{print $1}'
)
exec 9> '#LIBDIR#/digest-mailer/digest-mailer.from.'"${sender}"'.to.'"${receiver}"'.lock'
flock 9
# we block if that lock is not available - the sender expects his mails delivered!
printf '%s' "${message}" > "#LIBDIR#/digest-mailer/${sender}.${receiver}.${time}.${hash}"
else
exec 9> '#LIBDIR#/digest-mailer/digest-mailer.from.'"${sender}"'.to.'"${receiver}"'.lock'
if ! flock -n 9; then
# silently fail if lock is not available ("flush" is not _that_ important)
exit
fi
fi
if [ -s "#LIBDIR#/digest-mailer/${sender}.${receiver}.last-sent" ]; then
last_sent=$(
cat "#LIBDIR#/digest-mailer/${sender}.${receiver}.last-sent"
)
else
last_sent='0'
fi
first_unsent=$(
find '#LIBDIR#/digest-mailer' -regextype grep -maxdepth 1 -mindepth 1 \
-regex ".*/${sender}\.${receiver}\.[0-9]\+\.[0-9a-f]\+\$" \
-printf '%f\n' | \
cut -d. -f3 | \
sort -nr | \
tail -n1
)
if ${flush} && \
[ -z "${first_unsent}" ]; then
# no pending emails at all
exit
fi
to_send=$(
find '#LIBDIR#/digest-mailer' -regextype grep -maxdepth 1 -mindepth 1 \
-regex ".*/${sender}\.${receiver}\.[0-9]\+\.[0-9a-f]\+\$" | \
sort
)
to_send_count=$(
printf '%s\n' "${to_send}" | \
wc -l
)
if [ $((last_sent + min_delay)) -gt ${time} ]; then
# last email was sent too close
exit
fi
if [ ${to_send_count} -gt 1 ] && \
printf '%s - (%s+%s)*%s - (%s+%s)*(1-%s)\n' \
"${time}" \
"${min_delay}" "${last_sent}" "${agressivity}" \
"${max_delay}" "${first_unsent}" "${agressivity}" | \
bc | \
grep -q '^-' && \
[ $((first_unsent + max_delay)) -ge ${time} ]; then
exit
fi
printf '%s' "${time}" > "#LIBDIR#/digest-mailer/${sender}.${receiver}.last-sent"
if [ ${to_send_count} -eq 1 ]; then
${mail_cmd} < "${to_send}"
rm "${to_send}"
exit
fi
{
sed -n '
/^$/q
s/^Subject:.*$/Subject: mail-digest/
p
' "$(
printf '%s\n' "${to_send}" | \
head -n1
)"
printf '\n'
counter=1
printf '%s\n' "${to_send}" | \
while read -r file; do
printf 'vvvvv %s. vvvvv\n' "${counter}"
cat "${file}"
printf '\n^^^^^ %s. ^^^^^\n' "${counter}"
counter=$((counter+1))
rm "${file}"
done
} | ${mail_cmd}
|