summaryrefslogtreecommitdiff
path: root/mutt_check_attachment
blob: 2365194d998b3881d0f758cc868572bde344af6c (plain) (blame)
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
#!/bin/bash

## Original script adapted from source: http://wiki.mutt.org/?ConfigTricks/CheckAttach
##
## Adapted by Ismaël Bouya (http://www.normalesup.org/~bouya/) to make it so
## that retrying to send the email shortly after will work
##
## Edit muttrc to have this line:
## set sendmail = "/usr/local/bin/mutt_check_attachment_before_send.sh /usr/lib/sendmail -oem -oi"
##


# Feel like tipping/donating? https://www.immae.eu/licenses_and_tipping


## Attachment keywords that the message body will be searched for:
KEYWORDS='attach|joint|voici|voil'

## Check that sendmail or other program is supplied as first argument.
if [ ! -x "$1" ]; then
    echo "Usage: $0 </path/to/mailprog> <args> ..."
    echo "e.g.: $0 /usr/sbin/sendmail -oem -oi"
    exit 2
fi

## Save msg in file to re-use it for multiple tests.
TMPFILE=`mktemp -t mutt_checkattach.XXXXXX` || exit 2
cat > "$TMPFILE"

## Define test for multipart message.
function multipart {
#    grep -q '^Content-Type: multipart' "$TMPFILE"
    grep -q '^Content-Disposition: attachment' "$TMPFILE"
}

## Define test for keyword search.
function word-attach {
    grep -v '^>' "$TMPFILE" | grep -E -i -q "$KEYWORDS"
}

## Header override.
function header-override {
    grep -i -E "^X-attached: *none *$" "$TMPFILE"
}

function ask {
	terminal=`tty`
	dialog --yesno "Envoyer malgré la pièce jointe manquante ?" 5 30 < $terminal > $terminal
	}

#Verifie qu'on a essayé de l'envoyer y'a moins d'une minute
function file_last {
    if [ ! -e $HOME/.mutt_attach ]; then
	return 0
	fi
    valeur=`echo \`date +%s\`-\`stat -c %Y $HOME/.mutt_attach\`'<60' | bc`
    return $valeur
}
## FINAL DECISION:
if multipart || ! word-attach || header-override || ! file_last ; then
    "$@" < "$TMPFILE"
    EXIT_STATUS=$?
    if [ ! -s $HOME/.mutt_attach ]; then
	rm -f $HOME/.mutt_attach
	fi
else
    echo "No file was attached but a search of the message text suggests there should be one.  Add a header \"X-attached: none\" to override this check if no attachment is intended."
    echo "You can also send the email again in the next minute."
    EXIT_STATUS=1
    touch $HOME/.mutt_attach
fi

## Delete the temporary file.
rm -f "$TMPFILE"

## That's all folks.
exit $EXIT_STATUS