summaryrefslogtreecommitdiff
path: root/sendmailadvanced
blob: 12a327ca0be2e172a53735c00ccc39965c64bca0 (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
#!/bin/bash

verwendung() {
  >&2 echo 'sendmailadvanced [-h|--hook param-for-hooks] [-i|--inline file] [-e|--encrypt] [-s|--stamp] [-t] [--no-hooks]'
  exit 1
}

datei=""
encrypt=false
stamp=false
applyHooks=true

if [ -x /etc/sendmailadvanced.conf ]
then
  . /etc/sendmailadvanced.conf
else
  for konfig in $(readlink -f "$0").conf $(find . -name sendmailadvanced.conf 2> /dev/null)
  do
    [ -x "${konfig}" ] || continue
    . "${konfig}"
    break
  done
fi


eval set -- "$(getopt -o eh:i:st --long encrypt,hook:,inline:,no-hooks,stamp -n "$(basename "$0")" -- "$@")"

while true
do
  case "$1" in
    -h|--hook)
      shift
      hooks[${#hooks[@]}]="$1"
    ;;
    -i|--inline)
      echo "inline erkannt"
      shift
      datei="$1"
    ;;
    -e|--encrypt)
      encrypt=true
    ;;
    -s|--stamp)
      stamp=true
    ;;
    --no-hooks)
      applyHooks=false
    ;;
    -t)
      encrypt=true
      stamp=true
    ;;
    --)
      shift
      break
    ;;
    *)
      >&2 echo "FEHLER: Verstehe Option \"$1\" doch nicht! Ich beende."
      verwendung
    ;;
  esac
  shift
done

[ $# -ne 0 ] && verwendung

MAILER="cantfind"
for executable in sendmail msmtp
do
  for prefix in /usr/bin /usr/sbin /bin /sbin
  do
    [ "${MAILER}" == "cantfind" ] && [ -x ${prefix}/${executable} ] && MAILER="${prefix}/${executable}"
  done
done

if [ -z "${datei}" ] && [ "${MAILER}" == "cantfind" ]
then
  >&2 echo "ERROR: Can't find suitable mailer."
  exit 1
fi

(
  [ -z "${datei}" ] && cat || cat "${datei}"
) | \
(
  IFS=''
  adressatenString=""
  adressatenSammeln=false
  while read -r zeile
  do
    echo "${zeile}" | grep -q "\S" || break
    s="${zeile}"
    if [ "${s:0:3}" == "To:" ] || [ "${s:0:3}" == "Cc:" ]
    then
      adressatenSammeln=true
      s=" ,${s:4}"
    fi
    [ "${s:0:1}" == " " ] || adressatenSammeln=false
    ${adressatenSammeln} && adressatenString="${adressatenString}${s}"
    echo "${zeile}"
  done

  if ${stamp}
  then
    IFS=','
    for adressat in ${adressatenString}
    do
      if [[ "${adressat}" == *"<"*">"* ]]
      then
        adressat="${adressat#*<}"
        adressat="${adressat%>*}"
      fi
      adressat="$(
        echo "${adressat}" | \
          tr -d "[:space:]"
      )"
      echo "${adressat}" | grep -q "\S" || continue
      hashcash -b ${hashcash_bits} -Xm "${adressat}"
    done
  fi
  echo "${zeile}"

  (
    ${applyHooks} && body_header_hook "${hooks[@]}"
    cat
    ${applyHooks} && body_footer_hook "${hooks[@]}"
  ) | \
  (
    ${encrypt} && gpg -e -a -s -r ${gpg_recipient} || cat
  )
) | \
(
  if [ -z "${datei}" ]
  then
    ${MAILER} -t
  else
    tmpFile="$(mktemp)"
    cat > "${tmpFile}"
    cat "${tmpFile}" > "${datei}"
    rm -f "${tmpFile}"
  fi
)