mblaze/mcom

450 lines
8.7 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
2018-02-26 15:57:23 +00:00
NL='
'
commajoin() {
awk 'NR==1 {l=$0; next}
{l=l", "$0}
END {print l}'
}
notmine() {
mine="$(maddr -a -h local-mailbox:alternate-mailboxes: $MBLAZE/profile)"
grep -Fv -e "$mine"
}
reffmt() {
2017-07-06 16:41:14 +00:00
sed 's/^[^<]*//g;s/[^>]*$//g;s/>[^<]*</>\
</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
stampdate() {
if ! mhdr -h date "$1" >/dev/null; then
tmp=$(mktemp -t mcom.XXXXXX)
{
printf 'Date: '
mdate
cat "$1"
} >"$tmp" &&
mv "$tmp" "$1"
fi
}
stripempty() {
tmp=$(mktemp -t mcom.XXXXXX)
msed 's/^[ \t]*$//d' "$1" >"$tmp"
mv "$tmp" "$1"
}
needs_multipart() {
mhdr -h attach "$1" >/dev/null ||
grep -q '^#[^ ]*/[^ ]* ' "$1"
}
do_mime() {
if needs_multipart "$draft"; then
(
2018-02-26 15:57:23 +00:00
IFS=$NL
msed '/attach/d' $draft
for f in $(mhdr -M -h attach $draft); do
printf '#%s %s\n' \
"$(file -Lbi $f | sed 's/ //g')" \
"$f"
done
) | mmime >$draftmime
else
mmime -r <"$draft" >"$draftmime"
fi
}
MBLAZE=${MBLAZE:-$HOME/.mblaze}
sendmail=$(mhdr -h sendmail "$MBLAZE/profile")
sendmail_args=$(mhdr -h sendmail-args "$MBLAZE/profile")
sendmail="${sendmail:-sendmail} ${sendmail_args:--t}"
default_from=$(mhdr -h local-mailbox "$MBLAZE/profile")
hdrs=
2017-06-19 12:00:29 +00:00
resume=
case "$0" in
*mcom*)
hdr=to
while [ $# -gt 0 ]; do
case "$1" in
--)
# -- is like -to, really
shift
hdr=to
;;
-r)
2017-06-19 12:00:29 +00:00
shift
resume=1
if [ "$#" -gt 0 ]; then
echo "used draft $1"
2017-06-19 12:00:29 +00:00
draft="$1"
shift
fi
;;
-??*)
hdr=${1#-}
shift;;
[!-]*)
hdrs="$hdrs$NL$(printf '%s: %s\n' "${hdr}" "$1")"
shift;;
*)
printf 'mcom: invalid argument %s\n' "$1" 1>&2
exit 1;;
esac
done
;;
*mfwd*)
hdr=
raw=
while [ $# -gt 0 ]; do
case "$1" in
--)
shift
break;;
-r)
shift
raw=1;;
-??*)
hdr=${1#-}
shift;;
[!-]*)
2018-03-02 19:42:27 +00:00
[ -z "$hdr" ] && break
hdrs="$hdrs$NL$(printf '%s: %s\n' "${hdr}" "$1")"
shift;;
*)
printf 'mfwd: invalid argument %s\n' "$1" 1>&2
exit 1;;
esac
done
[ "$#" -eq 0 ] && set -- .
;;
*mbnc*)
hdr=
while [ $# -gt 0 ]; do
case "$1" in
--)
shift
break;;
-??*)
hdr=${1#-}
shift;;
[!-]*)
2018-03-02 19:42:27 +00:00
[ -z "$hdr" ] && break
hdrs="$hdrs$NL$(printf '%s: %s\n' "${hdr}" "$1")"
shift;;
*)
printf 'mbnc: invalid argument %s\n' "$1" 1>&2
exit 1;;
esac
done
[ "$#" -eq 0 ] && set -- .
;;
*mrep*)
hdr=
while [ $# -gt 0 ]; do
case "$1" in
--)
shift
break;;
-??*)
hdr=${1#-}
shift;;
[!-]*)
2018-03-02 19:42:27 +00:00
[ -z "$hdr" ] && break
hdrs="$hdrs$NL$(printf '%s: %s\n' "${hdr}" "$1")"
shift;;
*)
printf 'mrep: invalid argument %s\n' "$1" 1>&2
exit 1;;
esac
done
[ "$#" -eq 0 ] && set -- .
;;
2017-06-19 12:00:29 +00:00
esac
hdrs="$(printf '%s\n' "${hdrs#$NL}" | mhdr /dev/stdin)"
2016-09-02 21:48:56 +00:00
outbox=$(mhdr -h outbox "$MBLAZE/profile")
if [ -z "$outbox" ]; then
2017-06-19 12:00:29 +00:00
if [ -z "$resume" ]; then
i=0
while [ -f "snd.$i" ]; do
i=$((i+1))
done
draft="./snd.$i"
elif [ -z "$draft" ]; then
draft=$(ls -1t ./snd.*[0-9] | sed 1q)
fi
draftmime="$draft.mime"
else
2017-06-19 12:00:29 +00:00
if [ -z "$resume" ]; then
draft="$(true | mdeliver -v -c -XD "$outbox")"
if [ -z "$draft" ]; then
printf '%s\n' "$0: failed to create draft in outbox $outbox." 1>&2
exit 1
fi
elif [ -z "$draft" ]; then
draft=$(mlist -D "$outbox" | msort -r -M | sed 1q)
fi
draftmime="$(printf '%s\n' "$draft" | sed 's,\(.*\)/cur/,\1/tmp/mime-,')"
fi
2016-07-20 14:25:18 +00:00
2017-06-19 12:00:29 +00:00
[ -z "$resume" ] &&
2016-07-20 14:25:18 +00:00
{
2016-07-20 14:58:37 +00:00
case "$0" in
*mcom*)
{
printf '%s' "$hdrs" | mhdr -M -h to /dev/stdin |
commajoin | sed 's/^/To: /'
printf '%s' "$hdrs" | mhdr -M -h cc /dev/stdin |
commajoin | sed 's/^/Cc: /'
printf '%s' "$hdrs" | mhdr -M -h bcc /dev/stdin |
commajoin | sed 's/^/Bcc: /'
printf '%s\n' "$hdrs" | awk '{ print }' |
2018-03-10 21:28:41 +00:00
msed "/to/d; /cc/d; /bcc/d; /body/d" /dev/stdin
} | msed "/cc/a//; /bcc/a//; /subject/a//; /from/a/$default_from/" /dev/stdin | sed '/^$/d'
msgid
2017-06-01 12:57:27 +00:00
museragent
cat "$MBLAZE/headers" 2>/dev/null
2018-03-10 21:28:41 +00:00
printf '\n'
(
IFS=$NL
cat -- /dev/null $(printf '%s' "$hdrs" | mhdr -M -h body /dev/stdin)
)
printf '\n'
2016-07-20 14:58:37 +00:00
;;
*mfwd*)
{
printf '%s' "$hdrs" | mhdr -M -h to /dev/stdin |
commajoin | sed 's/^/To: /'
printf '%s' "$hdrs" | mhdr -M -h cc /dev/stdin |
commajoin | sed 's/^/Cc: /'
printf '%s' "$hdrs" | mhdr -M -h bcc /dev/stdin |
commajoin | sed 's/^/Bcc: /'
COLUMNS=10000 mscan -f 'Subject: [%f] %s' "$@" 2>/dev/null | sed 1q
printf '%s\n' "$hdrs" | awk '{ print }' |
msed "/to/d; /cc/d; /bcc/d" /dev/stdin
} | msed "/cc/a//; /bcc/a//; /from/a/$default_from/" /dev/stdin | sed '/^$/d'
msgid
museragent
cat "$MBLAZE/headers" 2>/dev/null
printf '\n\n'
if [ -z "$raw" ]; then
mseq -r "$@" | sed 's:^:#message/rfc822#inline :; s:$:>:'
else (
SEP=-----
2018-02-26 15:57:23 +00:00
IFS=$NL
for f in $(mseq -r "$@"); do
printf '%s Forwarded message from %s %s\n\n' \
$SEP "$(mhdr -d -h from "$f")" $SEP
DISPLAY= mshow -n -N "$f" </dev/null |
sed 's/^-/- &/' # RFC 934
printf '\n%s %s %s\n\n' \
$SEP 'End forwarded message' $SEP
done
) fi
;;
2018-01-06 18:17:49 +00:00
*mbnc*)
{
printf '%s' "$hdrs" | mhdr -M -h resent-to /dev/stdin |
commajoin | sed 's/^/Resent-To: /'
printf '%s' "$hdrs" | mhdr -M -h resent-cc /dev/stdin |
commajoin | sed 's/^/Resent-Cc: /'
printf '%s\n' "$hdrs" | awk '{ print }' |
msed "/resent-to/d; /resent-cc/d" /dev/stdin
} |
msed "/resent-to/a//; /resent-from/a/$default_from/" /dev/stdin | sed '/^$/d'
2018-01-06 18:17:49 +00:00
msgid | sed 's/^/Resent-/'
printf 'Resent-Date: %s\n' "$(mdate)"
2018-01-06 21:53:23 +00:00
(
2018-02-26 15:57:23 +00:00
IFS=$NL
2018-01-06 21:53:23 +00:00
cat $(mseq "${@:-.}")
)
2018-01-06 18:17:49 +00:00
;;
*mrep*)
ng=$(mhdr -h newsgroups "$1")
if [ "$ng" ]; then
printf 'Newsgroups: %s\n' "$ng"
else
to=$(mhdr -h reply-to "$1")
[ -z "$to" ] && to=$(mhdr -h from "$1")
printf 'To: %s\n' "$to"
printf 'Cc: %s\n' \
"$(mhdr -d -A -h to:cc: "$1" |notmine |commajoin)"
printf 'Bcc: \n'
2018-03-10 21:28:41 +00:00
printf '%s\n' "$hdrs" | awk '{ print }' |
msed "/body/d" /dev/stdin
fi | sed '/^$/d'
printf 'Subject: Re: %s\n' "$(COLUMNS=10000 mscan -f '%S' "$1")"
from=$(mhdr -h local-mailbox "$MBLAZE/profile")
[ "$from" ] && printf 'From: %s\n' "$from"
2016-08-08 12:01:33 +00:00
mid=$(mhdr -h message-id "$1")
if [ "$mid" ]; then
printf 'References:'
2016-08-08 12:01:33 +00:00
{
mhdr -h references "$1"
printf '%s\n' "$mid"
2016-08-08 12:01:33 +00:00
} | reffmt
printf 'In-Reply-To: %s\n' "$mid"
2016-08-08 12:01:33 +00:00
fi
msgid
2017-06-01 12:57:27 +00:00
museragent
cat "$MBLAZE/headers" 2>/dev/null
printf '\n'
2016-07-20 14:58:37 +00:00
2016-08-01 16:00:52 +00:00
mquote "$1"
printf '\n'
2018-03-10 21:28:41 +00:00
(
IFS=$NL
cat -- /dev/null $(printf '%s' "$hdrs" | mhdr -M -h body /dev/stdin)
)
printf '\n'
;;
2016-07-20 14:58:37 +00:00
esac
2018-01-06 18:17:49 +00:00
case "$0" in
*mbnc*) ;;
*)
if [ -f "$MBLAZE/signature" ]; then
SIGNATURE="$MBLAZE/signature"
elif [ -f ~/.signature ]; then
SIGNATURE="$HOME/.signature"
fi
2018-01-06 18:17:49 +00:00
if [ -n "$SIGNATURE" ]; then
printf '%s\n' '-- '
cat "$SIGNATURE"
fi
esac
2016-08-08 12:01:56 +00:00
} >$draft
2016-07-20 14:25:18 +00:00
automime=
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
2018-01-06 18:17:49 +00:00
resent="$(maddr -h resent-to "$draft")"
case "$resent" in
?*)
sendmail=$(mhdr -h sendmail "$MBLAZE/profile")
sendmail="${sendmail:-sendmail} -- $resent"
;;
esac
if [ -e $draftmime ]; then
if [ $draft -ot $draftmime ] || [ "$automime" -eq 1 ]; then
stampdate $draftmime
if $sendmail <$draftmime; then
if [ "$outbox" ]; then
mv $draftmime $draft
mflag -d -S $draft
else
rm $draft $draftmime
fi
2016-07-20 21:17:38 +00:00
else
printf '%s\n' "mcom: $sendmail failed, kept draft $draft"
2016-07-20 21:17:38 +00:00
exit 2
fi
else
printf 'mcom: re-run mmime first.\n'
2016-07-20 21:17:38 +00:00
c=
fi
else
2016-08-02 13:32:29 +00:00
if mmime -c <$draft; then
stampdate $draft
if $sendmail <$draft; then
2016-08-02 13:32:29 +00:00
if [ "$outbox" ]; then
mflag -d -S $draft
2016-08-02 13:32:29 +00:00
else
rm $draft
fi
else
printf '%s\n' "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
printf '%s\n' "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
case "$0" in
*mrep*) mflag -R -- "$1" ;;
*mbnc*) mflag -P -- "$1" ;;
*mfwd*) mflag -P -- "$@" ;;
esac
exit 0
2016-07-20 14:25:18 +00:00
;;
c|cancel)
stampdate $draft
printf '%s\n' "mcom: cancelled draft $draft"
2016-07-20 14:25:18 +00:00
exit 1
;;
2016-07-20 21:17:38 +00:00
m|mime)
do_mime
2016-08-01 15:45:59 +00:00
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
stripempty $draft
if mmime -c <$draft; then
automime=
else
automime=1
do_mime
fi
2016-07-20 14:25:18 +00:00
;;
2016-08-08 14:06:52 +00:00
d|delete)
rm -i $draft
if ! [ -f $draft ]; then
rm -f $draftmime
printf '%s\n' "mcom: deleted draft $draft"
2016-08-08 14:06:52 +00:00
exit 0
fi
c=
;;
2017-06-19 15:17:20 +00:00
sign)
msign $draft >$draftmime
mshow -t $draftmime
c=
;;
encrypt)
mencrypt $draft >$draftmime
mshow -t $draftmime
c=
;;
show)
if [ -e $draftmime ]; then
mshow "$draftmime"
else
mshow "$draft"
fi
c=
;;
2016-07-20 14:25:18 +00:00
*)
printf 'What now? (%s[s]end, [c]ancel, [d]elete, [e]dit, [m]ime, sign, encrypt) ' "${automime:+mime and }"
2016-07-20 14:25:18 +00:00
read -r c
;;
esac
done