2016-07-20 14:25:18 +00:00
|
|
|
#!/bin/sh
|
2016-08-08 14:01:06 +00:00
|
|
|
# mcom [TO] - compose mail
|
2016-07-20 14:25:18 +00:00
|
|
|
|
2016-07-28 11:23:36 +00:00
|
|
|
commajoin() {
|
2017-05-26 13:42:39 +00:00
|
|
|
awk 'NR==1 {l=$0; next}
|
|
|
|
{l=l", "$0}
|
|
|
|
END {print l}'
|
2016-07-28 11:23:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 20:49:17 +00:00
|
|
|
notmine() {
|
|
|
|
mine="$(maddr -a -h local-mailbox:alternate-mailboxes: $MBLAZE/profile)"
|
|
|
|
grep -Fv -e "$mine"
|
|
|
|
}
|
|
|
|
|
2016-07-28 11:23:36 +00:00
|
|
|
reffmt() {
|
|
|
|
sed 's/^[^<]*//g;s/[^>]*$//g;s/>[^<]*</>\n</g' | uniq | sed 's/^/ /'
|
|
|
|
}
|
|
|
|
|
2016-08-01 15:23:18 +00:00
|
|
|
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() {
|
2017-03-26 18:43:15 +00:00
|
|
|
printf 'Date: '
|
2016-08-08 12:01:56 +00:00
|
|
|
mdate
|
2016-08-01 15:23:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-16 17:38:51 +00:00
|
|
|
needs_multipart() {
|
|
|
|
mhdr -h attach "$1" >/dev/null ||
|
|
|
|
grep -q '^#[^ ]*/[^ ]* ' "$1"
|
|
|
|
}
|
|
|
|
|
2016-09-01 23:50:20 +00:00
|
|
|
MBLAZE=${MBLAZE:-$HOME/.mblaze}
|
2016-09-03 22:56:12 +00:00
|
|
|
|
|
|
|
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")
|
2016-08-01 15:23:18 +00:00
|
|
|
if [ -z "$outbox" ]; then
|
|
|
|
i=0
|
|
|
|
while [ -f "snd.$i" ]; do
|
|
|
|
i=$((i+1))
|
|
|
|
done
|
2017-02-17 03:31:05 +00:00
|
|
|
draft="./snd.$i"
|
2016-08-01 15:45:59 +00:00
|
|
|
draftmime="./snd.$i.mime"
|
2016-08-01 15:23:18 +00:00
|
|
|
else
|
2016-09-03 22:48:29 +00:00
|
|
|
draft="$(true | mdeliver -v -c -XD "$outbox")"
|
2016-08-01 15:23:18 +00:00
|
|
|
if [ -z "$draft" ]; then
|
2017-03-26 18:43:15 +00:00
|
|
|
printf '%s\n' "$0: failed to create draft in outbox $outbox." 1>&2
|
2016-08-01 15:23:18 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2017-03-26 18:43:15 +00:00
|
|
|
draftmime="$(printf '%s\n' "$draft" | sed 's,\(.*\)/cur/,\1/tmp/mime-,')"
|
2016-08-01 15:23:18 +00:00
|
|
|
fi
|
2016-07-20 14:25:18 +00:00
|
|
|
|
|
|
|
{
|
2016-07-20 14:58:37 +00:00
|
|
|
case "$0" in
|
2016-08-08 14:01:06 +00:00
|
|
|
*mcom*)
|
2017-03-26 18:43:15 +00:00
|
|
|
printf 'To: '
|
2016-08-08 14:11:17 +00:00
|
|
|
printf '%s\n' "$@" | commajoin
|
2017-03-26 18:43:15 +00:00
|
|
|
printf '%s: \n' Cc Bcc Subject
|
2016-09-02 21:48:56 +00:00
|
|
|
from=$(mhdr -h local-mailbox "$MBLAZE/profile")
|
2017-03-26 18:43:15 +00:00
|
|
|
[ "$from" ] && printf 'From: %s\n' "$from"
|
2016-09-02 21:48:56 +00:00
|
|
|
cat "$MBLAZE/headers" 2>/dev/null
|
2016-08-01 15:23:18 +00:00
|
|
|
msgid
|
2017-06-01 12:57:27 +00:00
|
|
|
museragent
|
2016-08-08 12:01:56 +00:00
|
|
|
msgdate
|
2017-03-26 18:43:15 +00:00
|
|
|
printf '\n\n'
|
2016-07-20 14:58:37 +00:00
|
|
|
;;
|
2016-08-08 14:01:06 +00:00
|
|
|
*mrep*)
|
2016-07-22 16:35:29 +00:00
|
|
|
[ "$#" -eq 0 ] && set -- .
|
2016-09-25 19:30:20 +00:00
|
|
|
ng=$(mhdr -h newsgroups "$1")
|
|
|
|
if [ "$ng" ]; then
|
2017-03-26 18:43:15 +00:00
|
|
|
printf 'Newsgroups: %s\n' "$ng"
|
2016-09-25 19:30:20 +00:00
|
|
|
else
|
|
|
|
to=$(mhdr -h reply-to "$1")
|
|
|
|
[ -z "$to" ] && to=$(mhdr -h from "$1")
|
2017-03-26 18:43:15 +00:00
|
|
|
printf 'To: %s\n' "$to"
|
2017-04-09 20:49:17 +00:00
|
|
|
printf 'Cc: %s\n' \
|
|
|
|
"$(mhdr -d -A -h to:cc: "$1" |notmine |commajoin)"
|
2017-03-26 18:43:15 +00:00
|
|
|
printf 'Bcc: \n'
|
2016-09-25 19:30:20 +00:00
|
|
|
fi
|
2017-03-26 18:43:15 +00:00
|
|
|
printf 'Subject: Re: %s\n' "$(mscan -f '%S' "$1")"
|
2016-09-25 19:30:20 +00:00
|
|
|
from=$(mhdr -h local-mailbox "$MBLAZE/profile")
|
2017-03-26 18:43:15 +00:00
|
|
|
[ "$from" ] && printf 'From: %s\n' "$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
|
2017-03-26 18:43:15 +00:00
|
|
|
printf 'References: '
|
2016-08-08 12:01:33 +00:00
|
|
|
{
|
|
|
|
mhdr -h references "$1"
|
2017-03-26 18:43:15 +00:00
|
|
|
printf '%s\n' "$mid"
|
2016-08-08 12:01:33 +00:00
|
|
|
} | reffmt
|
2017-03-26 18:43:15 +00:00
|
|
|
printf 'In-Reply-To: %s\n' "$mid"
|
2016-08-08 12:01:33 +00:00
|
|
|
fi
|
2016-08-01 15:23:18 +00:00
|
|
|
msgid
|
2017-06-01 12:57:27 +00:00
|
|
|
museragent
|
2016-08-08 12:01:56 +00:00
|
|
|
msgdate
|
2017-03-26 18:43:15 +00:00
|
|
|
printf '\n'
|
2016-07-20 14:58:37 +00:00
|
|
|
|
2016-08-01 16:00:52 +00:00
|
|
|
mquote "$1"
|
2017-03-26 18:43:15 +00:00
|
|
|
printf '\n'
|
2016-07-20 14:58:37 +00:00
|
|
|
esac
|
|
|
|
|
2016-09-02 21:48:56 +00:00
|
|
|
if [ -f "$MBLAZE/signature" ]; then
|
|
|
|
SIGNATURE="$MBLAZE/signature"
|
2016-09-01 23:50:20 +00:00
|
|
|
elif [ -f ~/.signature ]; then
|
2016-09-02 21:48:56 +00:00
|
|
|
SIGNATURE="$HOME/.signature"
|
2016-09-01 23:50:20 +00:00
|
|
|
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)
|
2016-09-25 19:30:20 +00:00
|
|
|
case "$(mhdr -h newsgroups "$draft")" in
|
|
|
|
*gmane.*) sendmail="mblow -s news.gmane.org";;
|
|
|
|
*.*) sendmail="mblow";;
|
|
|
|
esac
|
|
|
|
|
2016-08-01 15:23:18 +00:00
|
|
|
if [ -e $draftmime ]; then
|
|
|
|
if [ $draft -ot $draftmime ]; then
|
2016-09-03 22:56:12 +00:00
|
|
|
if $sendmail <$draftmime; then
|
2016-08-01 15:23:18 +00:00
|
|
|
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
|
2017-03-26 18:43:15 +00:00
|
|
|
printf '%s\n' "mcom: $sendmail failed, kept draft $draft"
|
2016-07-20 21:17:38 +00:00
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
else
|
2017-03-26 18:43:15 +00:00
|
|
|
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
|
2016-09-03 22:56:12 +00:00
|
|
|
if $sendmail <$draft; then
|
2016-08-02 13:32:29 +00:00
|
|
|
if [ "$outbox" ]; then
|
|
|
|
mflag -d $draft
|
|
|
|
else
|
|
|
|
rm $draft
|
|
|
|
fi
|
|
|
|
exit 0
|
2016-08-01 15:23:18 +00:00
|
|
|
else
|
2017-03-26 18:43:15 +00:00
|
|
|
printf '%s\n' "mcom: $sendmail failed, kept draft $draft"
|
2016-08-02 13:32:29 +00:00
|
|
|
exit 2
|
2016-08-01 15:23:18 +00:00
|
|
|
fi
|
2016-07-20 21:17:38 +00:00
|
|
|
else
|
2017-03-26 18:43:15 +00:00
|
|
|
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
|
2016-07-20 14:25:18 +00:00
|
|
|
;;
|
|
|
|
c|cancel)
|
2017-03-26 18:43:15 +00:00
|
|
|
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)
|
2017-04-16 17:38:51 +00:00
|
|
|
if needs_multipart "$draft"; then
|
|
|
|
(
|
|
|
|
IFS='
|
2016-08-01 15:45:59 +00:00
|
|
|
'
|
2017-04-16 17:38:51 +00:00
|
|
|
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
|
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
|
|
|
|
;;
|
2016-08-08 14:06:52 +00:00
|
|
|
d|delete)
|
|
|
|
rm -i $draft
|
|
|
|
if ! [ -f $draft ]; then
|
|
|
|
rm -f $draftmime
|
2017-03-26 18:43:15 +00:00
|
|
|
printf '%s\n' "mcom: deleted draft $draft"
|
2016-08-08 14:06:52 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
c=
|
|
|
|
;;
|
2016-07-20 14:25:18 +00:00
|
|
|
*)
|
2017-03-26 18:43:15 +00:00
|
|
|
printf '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
|