diff options
-rwxr-xr-x | sendmailadvanced | 110 |
1 files changed, 96 insertions, 14 deletions
diff --git a/sendmailadvanced b/sendmailadvanced index 7b5e7fe..d4c4be2 100755 --- a/sendmailadvanced +++ b/sendmailadvanced @@ -1,7 +1,69 @@ #!/bin/bash -MAILER="cantfind" +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) + 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 @@ -10,29 +72,35 @@ do done done -if [ "${MAILER}" == "cantfind" ] +if [ -z "${datei}" ] && [ "${MAILER}" == "cantfind" ] then >&2 echo "ERROR: Can't find suitable mailer." exit 1 fi ( - . /etc/sendmailadvanced.conf - - tos="" - while read s + [ -z "${datei}" ] && cat || cat "${datei}" +) | \ +( + IFS='' + while read -r s do echo "${s}" if [ "${s:0:3}" == "To:" ] || [ "${s:0:3}" == "Cc:" ] then - tos="${tos} ${s:4}" - fi - if [ "${s:0:8}" == "Subject:" ] + s="${s:4}" + if [[ "${s}" == *"<"*">"* ]] + then + s="${s#*<}" + s="${s%>*}" + fi + tos[${#tos[@]}]="${s}" + elif [ "${s:0:8}" == "Subject:" ] then break fi done - hashcash -b ${hashcash_bits} -Xm ${tos} + ${stamp} && hashcash -b ${hashcash_bits} -Xm "${tos[@]}" while read s do echo "${s}" @@ -42,8 +110,22 @@ fi fi done ( - body_header_hook $@ + ${applyHooks} && body_header_hook "${hooks[@]}" cat - body_footer_hook $@ - ) | gpg -e -a -s -r ${gpg_recipient} -) | ${MAILER} -t + ${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 +) |