2016-07-20 14:25:18 +00:00
|
|
|
#!/bin/sh
|
|
|
|
# mcomp [TO] - compose mail
|
|
|
|
|
2016-07-28 11:23:36 +00:00
|
|
|
commajoin() {
|
|
|
|
sed ':a;N;s/\n/, /;$!b a'
|
|
|
|
}
|
|
|
|
|
|
|
|
reffmt() {
|
|
|
|
sed 's/^[^<]*//g;s/[^>]*$//g;s/>[^<]*</>\n</g' | uniq | sed 's/^/ /'
|
|
|
|
}
|
|
|
|
|
2016-08-01 15:23:18 +00:00
|
|
|
msgid() {
|
|
|
|
mgenmid 2>/dev/null | sed 's/.*/Message-Id: &/'
|
|
|
|
}
|
|
|
|
|
2016-08-01 17:36:23 +00:00
|
|
|
outbox=$(mhdr -h outbox ~/.mblaze/profile)
|
2016-08-01 15:23:18 +00:00
|
|
|
if [ -z "$outbox" ]; then
|
|
|
|
i=0
|
|
|
|
while [ -f "snd.$i" ]; do
|
|
|
|
i=$((i+1))
|
|
|
|
done
|
|
|
|
draft="snd.$i"
|
2016-08-01 15:45:59 +00:00
|
|
|
draftmime="./snd.$i.mime"
|
2016-08-01 15:23:18 +00:00
|
|
|
else
|
|
|
|
draft="$(true | mdeliver -v -XD "$outbox")"
|
|
|
|
if [ -z "$draft" ]; then
|
|
|
|
echo "$0: failed to create draft in outbox $outbox." 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
draftmime="$(echo $draft | sed 's,\(.*\)/cur/,\1/tmp/mime-,')"
|
|
|
|
fi
|
2016-07-20 14:25:18 +00:00
|
|
|
|
|
|
|
{
|
2016-07-20 14:58:37 +00:00
|
|
|
case "$0" in
|
|
|
|
*mcomp*)
|
|
|
|
echo -n "To: $1"
|
2016-07-20 20:11:25 +00:00
|
|
|
[ "$#" -ge 1 ] && shift
|
2016-07-20 14:58:37 +00:00
|
|
|
for rcpt; do
|
|
|
|
echo -n ", $rcpt"
|
|
|
|
done
|
|
|
|
echo
|
|
|
|
echo "Cc: "
|
|
|
|
echo "Bcc: "
|
|
|
|
echo "Subject: "
|
2016-08-01 17:36:23 +00:00
|
|
|
from=$(mhdr -h local-mailbox ~/.mblaze/profile)
|
2016-07-25 19:04:04 +00:00
|
|
|
[ "$from" ] && echo "From: $from"
|
2016-08-01 17:36:23 +00:00
|
|
|
cat ~/.mblaze/headers 2>/dev/null
|
2016-08-01 15:23:18 +00:00
|
|
|
msgid
|
2016-07-20 14:58:37 +00:00
|
|
|
echo
|
|
|
|
echo
|
|
|
|
;;
|
|
|
|
*mrepl*)
|
2016-07-22 16:35:29 +00:00
|
|
|
[ "$#" -eq 0 ] && set -- .
|
2016-07-28 11:23:36 +00:00
|
|
|
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)"
|
2016-07-20 14:58:37 +00:00
|
|
|
echo "Bcc: "
|
2016-07-28 11:08:04 +00:00
|
|
|
s=$(mhdr -d -h subject "$1")
|
2016-07-20 14:58:37 +00:00
|
|
|
os=
|
|
|
|
while [ "$os" != "$s" ]; do
|
|
|
|
os=$s
|
2016-07-28 11:08:04 +00:00
|
|
|
s=${s# }
|
|
|
|
s=${s#[Rr][Ee]:}
|
|
|
|
s=${s#[Aa][Ww]:}
|
|
|
|
s=${s#[Ff][Ww][Dd]:}
|
2016-07-20 14:58:37 +00:00
|
|
|
done
|
|
|
|
echo "Subject: Re: $s"
|
2016-08-01 17:36:23 +00:00
|
|
|
cat ~/.mblaze/headers 2>/dev/null
|
2016-07-20 14:58:37 +00:00
|
|
|
echo -n "References:"
|
|
|
|
{
|
2016-07-28 11:08:04 +00:00
|
|
|
mhdr -h references "$1"
|
|
|
|
mhdr -h message-id "$1"
|
2016-07-28 11:23:36 +00:00
|
|
|
} | reffmt
|
2016-07-28 11:08:04 +00:00
|
|
|
echo "In-Reply-To: $(mhdr -h message-id "$1")"
|
2016-08-01 15:23:18 +00:00
|
|
|
msgid
|
2016-07-20 14:58:37 +00:00
|
|
|
echo
|
|
|
|
|
2016-08-01 16:00:52 +00:00
|
|
|
mquote "$1"
|
2016-07-20 14:58:37 +00:00
|
|
|
echo
|
|
|
|
esac
|
|
|
|
|
2016-07-20 14:25:18 +00:00
|
|
|
if [ -f ~/.signature ]; then
|
|
|
|
printf '%s\n' '-- '
|
|
|
|
cat ~/.signature
|
|
|
|
fi
|
2016-08-01 15:25:40 +00:00
|
|
|
} | msed '/date/a' /dev/stdin >$draft
|
2016-07-20 14:25:18 +00:00
|
|
|
|
|
|
|
c=e
|
|
|
|
while :; do
|
|
|
|
case "$c" in
|
|
|
|
s|send)
|
2016-08-01 15:23:18 +00:00
|
|
|
if [ -e $draftmime ]; then
|
|
|
|
if [ $draft -ot $draftmime ]; then
|
|
|
|
if sendmail -t <$draftmime; then
|
|
|
|
if [ "$outbox" ]; then
|
|
|
|
mv $draftmime $draft
|
|
|
|
mflag -d $draft
|
|
|
|
else
|
|
|
|
rm $draft $draftmime
|
|
|
|
fi
|
2016-07-20 21:17:38 +00:00
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
echo "mcomp: sendmail failed, kept draft $draft"
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "mcomp: re-run mmime first."
|
|
|
|
c=
|
|
|
|
fi
|
|
|
|
else
|
2016-08-02 13:32:29 +00:00
|
|
|
if mmime -c <$draft; then
|
|
|
|
if sendmail -t <$draft; then
|
|
|
|
if [ "$outbox" ]; then
|
|
|
|
mflag -d $draft
|
|
|
|
else
|
|
|
|
rm $draft
|
|
|
|
fi
|
|
|
|
exit 0
|
2016-08-01 15:23:18 +00:00
|
|
|
else
|
2016-08-02 13:32:29 +00:00
|
|
|
echo "mcomp: sendmail failed, kept draft $draft"
|
|
|
|
exit 2
|
2016-08-01 15:23:18 +00:00
|
|
|
fi
|
2016-07-20 21:17:38 +00:00
|
|
|
else
|
2016-08-02 13:32:29 +00:00
|
|
|
echo "mcomp: message needs to be MIME-encoded first."
|
|
|
|
c=
|
2016-07-20 21:17:38 +00:00
|
|
|
fi
|
|
|
|
fi
|
2016-07-20 14:25:18 +00:00
|
|
|
;;
|
|
|
|
c|cancel)
|
|
|
|
echo "mcomp: cancelled draft $draft"
|
|
|
|
exit 1
|
|
|
|
;;
|
2016-07-20 21:17:38 +00:00
|
|
|
m|mime)
|
2016-08-01 15:45:59 +00:00
|
|
|
(
|
|
|
|
IFS='
|
|
|
|
'
|
|
|
|
msed '/attach/d' $draft
|
|
|
|
for f in $(mhdr -M -h attach $draft); do
|
|
|
|
echo "#$(file -Lbi $f | sed 's/ //g') $f"
|
|
|
|
done
|
|
|
|
) | tee /tmp/v | mmime >$draftmime
|
|
|
|
mshow -t $draftmime
|
2016-07-20 21:17:38 +00:00
|
|
|
c=
|
|
|
|
;;
|
2016-07-20 14:25:18 +00:00
|
|
|
e|edit)
|
|
|
|
c=
|
|
|
|
if ! ${EDITOR:-vi} $draft; then
|
2016-07-20 14:58:37 +00:00
|
|
|
c=c
|
2016-07-20 14:25:18 +00:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
2016-07-20 21:17:38 +00:00
|
|
|
echo -n "What now? ([s]end, [c]ancel, [e]dit, [m]ime) "
|
2016-07-20 14:25:18 +00:00
|
|
|
read -r c
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|