mblaze/mcomp
2016-07-28 13:23:36 +02:00

123 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
# mcomp [TO] - compose mail
commajoin() {
sed ':a;N;s/\n/, /;$!b a'
}
reffmt() {
sed 's/^[^<]*//g;s/[^>]*$//g;s/>[^<]*</>\n</g' | uniq | sed 's/^/ /'
}
i=0
while [ -f "snd.$i" ]; do
i=$((i+1))
done
draft="snd.$i"
{
case "$0" in
*mcomp*)
echo -n "To: $1"
[ "$#" -ge 1 ] && shift
for rcpt; do
echo -n ", $rcpt"
done
echo
echo "Cc: "
echo "Bcc: "
echo "Subject: "
from=$(mhdr -h local-mailbox ~/.santoku/profile)
[ "$from" ] && echo "From: $from"
cat ~/.santoku/headers 2>/dev/null
echo
echo
;;
*mrepl*)
[ "$#" -eq 0 ] && set -- .
to=$(mhdr -h reply-to "$1")
[ -z "$to" ] && to=$(mhdr -h from "$1")
echo "To: $to"
echo "Cc: $(mhdr -d -A -h to:cc: "$1" | commajoin)"
echo "Bcc: "
s=$(mhdr -d -h subject "$1")
os=
while [ "$os" != "$s" ]; do
os=$s
s=${s# }
s=${s#[Rr][Ee]:}
s=${s#[Aa][Ww]:}
s=${s#[Ff][Ww][Dd]:}
done
echo "Subject: Re: $s"
cat ~/.santoku/headers 2>/dev/null
echo -n "References:"
{
mhdr -h references "$1"
mhdr -h message-id "$1"
} | reffmt
echo "In-Reply-To: $(mhdr -h message-id "$1")"
echo
echo "$(mhdr -d -h from "$1") wrote:"
mshow -R "$1" |
sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' |
sed 's/^/> /'
echo
esac
if [ -f ~/.signature ]; then
printf '%s\n' '-- '
cat ~/.signature
fi
} >$draft
c=e
while :; do
case "$c" in
s|send)
if [ -e $draft.mime ]; then
if [ $draft -ot $draft.mime ]; then
if sendmail -t <$draft.mime; then
rm $draft $draft.mime
exit 0
else
echo "mcomp: sendmail failed, kept draft $draft"
exit 2
fi
else
echo "mcomp: re-run mmime first."
c=
fi
else
if sendmail -t <$draft; then
rm $draft
exit 0
else
echo "mcomp: sendmail failed, kept draft $draft"
exit 2
fi
fi
;;
c|cancel)
echo "mcomp: cancelled draft $draft"
exit 1
;;
m|mime)
mmime <$draft >$draft.mime
mshow -t ./$draft.mime
c=
;;
e|edit)
c=
if ! ${EDITOR:-vi} $draft; then
c=c
fi
;;
*)
echo -n "What now? ([s]end, [c]ancel, [e]dit, [m]ime) "
read -r c
;;
esac
done