-
Notifications
You must be signed in to change notification settings - Fork 4
/
pre-sendmail
executable file
·62 lines (48 loc) · 1.25 KB
/
pre-sendmail
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
#!/bin/bash
# Pre sendmail hook, just do attachment check now
# Set MTA
SENDMAIL=/usr/bin/msmtp
# Save msg in file for multiple tests
TMPFILE=`mktemp -t pre-sendmail.XXXXXXXX` || exit 2
# Define test for multipart message
multipart() {
grep -q '^Content-Type: multipart' "$TMPFILE"
}
# Header to override this check
override() {
grep -i -q '^X-attached: none$' "$TMPFILE"
}
# Define test for keyword search
attach() {
grep -a -v '^>' "$TMPFILE" | grep -E -i -q 'attach|patch|附件'
}
# Query function to pass message
ask() {
# try to get stdin/stdout/stderr from parent process
pid=$$
tty=$(ps -o tty= -p $pid)
while ! [[ $tty = *t* ]]; do # at least contain a *t* for pts/tty/etc
pid=$(ps -o ppid= -p $pid)
tty=$(ps -o tty= -p $pid)
done
TTY=/dev/$tty
# ps -p $pid |grep nonono
dialog --defaultno --title "Attachment Missing" \
--yesno "Do you still want to send it anyway?" 0 0 < $TTY > $TTY
RETURN_VALUE=$?
clear < $TTY > $TTY
return $RETURN_VALUE
}
cat > "$TMPFILE"
if override || multipart || ! attach || ask
then
$SENDMAIL "$@" < "$TMPFILE"
EXIT_STATUS=$?
else
echo "Attachment Missing"
echo
echo "Add your attachment, or add a header \"X-attached: none\" to override this check."
EXIT_STATUS=1
fi
rm -f "$TMPFILE"
exit $EXIT_STATUS