mcom: revamp argument parsing, allow setting arbitrary headers from command line

This commit is contained in:
Leah Neukirchen 2018-02-27 15:14:29 +01:00
parent 4b9e7f5952
commit c49c0b4a56
2 changed files with 157 additions and 24 deletions

View File

@ -9,15 +9,19 @@
.Nd compose, reply, forward, bounce, send messages .Nd compose, reply, forward, bounce, send messages
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm mcom .Nm mcom
.Op Fl Ar header Ar values\ ...
.Op Ar recipients\ ... .Op Ar recipients\ ...
.Nm mcom .Nm mcom
.Fl r Op draft .Fl r Op draft
.Nm mrep .Nm mrep
.Op Fl Ar header Ar values\ ... Fl -
.Ar msg .Ar msg
.Nm mfwd .Nm mfwd
.Op Fl r .Op Fl r
.Op Fl Ar header Ar values\ ... Fl -
.Op Ar msgs\ ... .Op Ar msgs\ ...
.Nm mbnc .Nm mbnc
.Op Fl Ar header Ar values\ ... Fl -
.Ar msg .Ar msg
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm mcom .Nm mcom
@ -56,6 +60,30 @@ See
.Xr mmsg 7 .Xr mmsg 7
for the message argument syntax. for the message argument syntax.
.Pc .Pc
.Pp
All commands can take optional
.Ar header
flags
.Pq which consist of two or more characters
to prefill header fields,
e.g. you can use
.Sq Nm mcom Fl to No merrilyn Fl cc No elea becci Fl subject No 'Party invite'
to create a draft with the To, Cc and Subject fields already set.
Note that these flags apply to
.Em all
arguments after them
.Po e.g.
.Sq mcom Fl attach No *.c
works
.Pc ,
so you
need to use
.Sq Fl -
when you want to use this feature together with
.Nm mrep ,
.Nm mfwd ,
or
.Nm mbnc .
.Sh MENU COMMANDS .Sh MENU COMMANDS
.Bl -tag -width 2n .Bl -tag -width 2n
.It Ic s .It Ic s

147
mcom
View File

@ -68,11 +68,21 @@ MBLAZE=${MBLAZE:-$HOME/.mblaze}
sendmail=$(mhdr -h sendmail "$MBLAZE/profile") sendmail=$(mhdr -h sendmail "$MBLAZE/profile")
sendmail_args=$(mhdr -h sendmail-args "$MBLAZE/profile") sendmail_args=$(mhdr -h sendmail-args "$MBLAZE/profile")
sendmail="${sendmail:-sendmail} ${sendmail_args:--t}" sendmail="${sendmail:-sendmail} ${sendmail_args:--t}"
default_from=$(mhdr -h local-mailbox "$MBLAZE/profile")
hdrs=
resume= resume=
case "$0" in case "$0" in
*mcom*) *mcom*)
if [ "$1" = -r ]; then hdr=to
while [ $# -gt 0 ]; do
case "$1" in
--)
# -- is like -to, really
shift
hdr=to
;;
-r)
shift shift
resume=1 resume=1
if [ "$#" -gt 0 ]; then if [ "$#" -gt 0 ]; then
@ -80,10 +90,90 @@ case "$0" in
draft="$1" draft="$1"
shift shift
fi fi
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;;
[!-]*)
[ -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;;
[!-]*)
[ -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;;
[!-]*)
[ -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 -- .
;; ;;
esac esac
hdrs="$(printf '%s\n' "${hdrs#$NL}" | mhdr /dev/stdin)"
outbox=$(mhdr -h outbox "$MBLAZE/profile") outbox=$(mhdr -h outbox "$MBLAZE/profile")
if [ -z "$outbox" ]; then if [ -z "$outbox" ]; then
if [ -z "$resume" ]; then if [ -z "$resume" ]; then
@ -113,27 +203,36 @@ fi
{ {
case "$0" in case "$0" in
*mcom*) *mcom*)
printf 'To: ' {
printf '%s\n' "$@" | commajoin printf '%s' "$hdrs" | mhdr -M -h to /dev/stdin |
printf '%s: \n' Cc Bcc Subject commajoin | sed 's/^/To: /'
from=$(mhdr -h local-mailbox "$MBLAZE/profile") printf '%s' "$hdrs" | mhdr -M -h cc /dev/stdin |
[ "$from" ] && printf 'From: %s\n' "$from" commajoin | sed 's/^/Cc: /'
cat "$MBLAZE/headers" 2>/dev/null printf '%s' "$hdrs" | mhdr -M -h bcc /dev/stdin |
commajoin | sed 's/^/Bcc: /'
printf '%s\n' "$hdrs" | awk '{ print }' |
msed "/to/d; /cc/d; /bcc/d" /dev/stdin
} | msed "/cc/a//; /bcc/a//; /subject/a//; /from/a/$default_from/" /dev/stdin | sed '/^$/d'
msgid msgid
museragent museragent
cat "$MBLAZE/headers" 2>/dev/null
printf '\n\n' printf '\n\n'
;; ;;
*mfwd*) *mfwd*)
raw= {
[ "$1" = -r ] && raw=1 && shift printf '%s' "$hdrs" | mhdr -M -h to /dev/stdin |
[ "$#" -eq 0 ] && set -- . commajoin | sed 's/^/To: /'
printf '%s: \n' To Cc Bcc printf '%s' "$hdrs" | mhdr -M -h cc /dev/stdin |
COLUMNS=10000 mscan -f 'Subject: [%f] %s' "$@" | sed 1q commajoin | sed 's/^/Cc: /'
from=$(mhdr -h local-mailbox "$MBLAZE/profile") printf '%s' "$hdrs" | mhdr -M -h bcc /dev/stdin |
[ "$from" ] && printf 'From: %s\n' "$from" commajoin | sed 's/^/Bcc: /'
cat "$MBLAZE/headers" 2>/dev/null 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 msgid
museragent museragent
cat "$MBLAZE/headers" 2>/dev/null
printf '\n\n' printf '\n\n'
if [ -z "$raw" ]; then if [ -z "$raw" ]; then
mseq -r "$@" | sed 's:^:#message/rfc822#inline :; s:$:>:' mseq -r "$@" | sed 's:^:#message/rfc822#inline :; s:$:>:'
@ -151,9 +250,15 @@ fi
) fi ) fi
;; ;;
*mbnc*) *mbnc*)
printf '%s: \n' Resent-To {
from=$(mhdr -h local-mailbox "$MBLAZE/profile") printf '%s' "$hdrs" | mhdr -M -h resent-to /dev/stdin |
[ "$from" ] && printf 'Resent-From: %s\n' "$from" 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'
msgid | sed 's/^/Resent-/' msgid | sed 's/^/Resent-/'
printf 'Resent-Date: %s\n' "$(mdate)" printf 'Resent-Date: %s\n' "$(mdate)"
( (
@ -162,7 +267,6 @@ fi
) )
;; ;;
*mrep*) *mrep*)
[ "$#" -eq 0 ] && set -- .
ng=$(mhdr -h newsgroups "$1") ng=$(mhdr -h newsgroups "$1")
if [ "$ng" ]; then if [ "$ng" ]; then
printf 'Newsgroups: %s\n' "$ng" printf 'Newsgroups: %s\n' "$ng"
@ -173,11 +277,11 @@ fi
printf 'Cc: %s\n' \ printf 'Cc: %s\n' \
"$(mhdr -d -A -h to:cc: "$1" |notmine |commajoin)" "$(mhdr -d -A -h to:cc: "$1" |notmine |commajoin)"
printf 'Bcc: \n' printf 'Bcc: \n'
printf '%s\n' "$hdrs" | awk '{ print }'
fi fi
printf 'Subject: Re: %s\n' "$(COLUMNS=10000 mscan -f '%S' "$1")" printf 'Subject: Re: %s\n' "$(COLUMNS=10000 mscan -f '%S' "$1")"
from=$(mhdr -h local-mailbox "$MBLAZE/profile") from=$(mhdr -h local-mailbox "$MBLAZE/profile")
[ "$from" ] && printf 'From: %s\n' "$from" [ "$from" ] && printf 'From: %s\n' "$from"
cat "$MBLAZE/headers" 2>/dev/null
mid=$(mhdr -h message-id "$1") mid=$(mhdr -h message-id "$1")
if [ "$mid" ]; then if [ "$mid" ]; then
printf 'References:' printf 'References:'
@ -189,6 +293,7 @@ fi
fi fi
msgid msgid
museragent museragent
cat "$MBLAZE/headers" 2>/dev/null
printf '\n' printf '\n'
mquote "$1" mquote "$1"