mblaze/mcom

188 lines
3.4 KiB
Plaintext
Raw Normal View History

2016-07-20 14:25:18 +00:00
#!/bin/sh
# mcom [TO] - compose mail
2016-07-20 14:25:18 +00:00
commajoin() {
sed ':a;N;s/\n/, /;$!b a'
}
reffmt() {
sed 's/^[^<]*//g;s/[^>]*$//g;s/>[^<]*</>\n</g' | uniq | sed 's/^/ /'
}
msgid() {
2016-08-08 12:01:48 +00:00
mgenmid 2>/dev/null | sed 's/^/Message-Id: /'
}
2016-08-08 12:01:56 +00:00
msgdate() {
echo -n "Date: "
mdate
}
MBLAZE=${MBLAZE:-$HOME/.mblaze}
sendmail=$(mhdr -h sendmail "$MBLAZE/profile")
sendmail_args=$(mhdr -h sendmail-args "$MBLAZE/profile")
sendmail="${sendmail:-sendmail} ${sendmail_args:--t}"
2016-09-02 21:48:56 +00:00
outbox=$(mhdr -h outbox "$MBLAZE/profile")
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"
else
draft="$(true | mdeliver -v -c -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
*mcom*)
2016-08-08 14:11:17 +00:00
echo -n "To: "
printf '%s\n' "$@" | commajoin
2016-07-20 14:58:37 +00:00
echo "Cc: "
echo "Bcc: "
echo "Subject: "
2016-09-02 21:48:56 +00:00
from=$(mhdr -h local-mailbox "$MBLAZE/profile")
[ "$from" ] && echo "From: $from"
2016-09-02 21:48:56 +00:00
cat "$MBLAZE/headers" 2>/dev/null
msgid
2016-08-08 12:01:56 +00:00
msgdate
2016-07-20 14:58:37 +00:00
echo
echo
;;
*mrep*)
2016-07-22 16:35:29 +00:00
[ "$#" -eq 0 ] && set -- .
ng=$(mhdr -h newsgroups "$1")
if [ "$ng" ]; then
echo "Newsgroups: $ng"
else
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: "
fi
2016-08-10 15:50:04 +00:00
echo "Subject: Re: $(mscan -f '%S' "$1")"
from=$(mhdr -h local-mailbox "$MBLAZE/profile")
[ "$from" ] && echo "From: $from"
2016-09-02 21:48:56 +00:00
cat "$MBLAZE/headers" 2>/dev/null
2016-08-08 12:01:33 +00:00
mid=$(mhdr -h message-id "$1")
if [ "$mid" ]; then
echo -n "References:"
{
mhdr -h references "$1"
printf "%s\n" "$mid"
} | reffmt
echo "In-Reply-To: $mid"
fi
msgid
2016-08-08 12:01:56 +00:00
msgdate
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-09-02 21:48:56 +00:00
if [ -f "$MBLAZE/signature" ]; then
SIGNATURE="$MBLAZE/signature"
elif [ -f ~/.signature ]; then
2016-09-02 21:48:56 +00:00
SIGNATURE="$HOME/.signature"
fi
2016-09-02 21:48:56 +00:00
if [ -n "$SIGNATURE" ]; then
2016-07-20 14:25:18 +00:00
printf '%s\n' '-- '
2016-09-02 21:48:56 +00:00
cat "$SIGNATURE"
2016-07-20 14:25:18 +00:00
fi
2016-08-08 12:01:56 +00:00
} >$draft
2016-07-20 14:25:18 +00:00
c=e
while :; do
case "$c" in
s|send)
case "$(mhdr -h newsgroups "$draft")" in
*gmane.*) sendmail="mblow -s news.gmane.org";;
*.*) sendmail="mblow";;
esac
if [ -e $draftmime ]; then
if [ $draft -ot $draftmime ]; then
if $sendmail <$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 "mcom: $sendmail failed, kept draft $draft"
2016-07-20 21:17:38 +00:00
exit 2
fi
else
echo "mcom: re-run mmime first."
2016-07-20 21:17:38 +00:00
c=
fi
else
2016-08-02 13:32:29 +00:00
if mmime -c <$draft; then
if $sendmail <$draft; then
2016-08-02 13:32:29 +00:00
if [ "$outbox" ]; then
mflag -d $draft
else
rm $draft
fi
exit 0
else
echo "mcom: $sendmail failed, kept draft $draft"
2016-08-02 13:32:29 +00:00
exit 2
fi
2016-07-20 21:17:38 +00:00
else
echo "mcom: message needs to be MIME-encoded first."
2016-08-02 13:32:29 +00:00
c=
2016-07-20 21:17:38 +00:00
fi
fi
2016-07-20 14:25:18 +00:00
;;
c|cancel)
echo "mcom: cancelled draft $draft"
2016-07-20 14:25:18 +00:00
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-08-08 14:06:52 +00:00
d|delete)
rm -i $draft
if ! [ -f $draft ]; then
rm -f $draftmime
echo "mcom: deleted draft $draft"
exit 0
fi
c=
;;
2016-07-20 14:25:18 +00:00
*)
2016-08-08 14:06:52 +00:00
echo -n "What now? ([s]end, [c]ancel, [d]elete, [e]dit, [m]ime) "
2016-07-20 14:25:18 +00:00
read -r c
;;
esac
done