about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2018-02-27 15:14:29 +0100
committerLeah Neukirchen <leah@vuxu.org>2018-02-27 15:14:29 +0100
commitc49c0b4a56f110c4f0782cb4c541be589e1c45a1 (patch)
tree099bbae3cb74abd2a82c4d08fc8612cfa2ad4199
parent4b9e7f5952c8ecd9e1c10eb8559f69dafb7ad86a (diff)
downloadmblaze-c49c0b4a56f110c4f0782cb4c541be589e1c45a1.tar.gz
mblaze-c49c0b4a56f110c4f0782cb4c541be589e1c45a1.tar.xz
mblaze-c49c0b4a56f110c4f0782cb4c541be589e1c45a1.zip
mcom: revamp argument parsing, allow setting arbitrary headers from command line
-rw-r--r--man/mcom.128
-rwxr-xr-xmcom153
2 files changed, 157 insertions, 24 deletions
diff --git a/man/mcom.1 b/man/mcom.1
index d4b9267..17fd814 100644
--- a/man/mcom.1
+++ b/man/mcom.1
@@ -9,15 +9,19 @@
 .Nd compose, reply, forward, bounce, send messages
 .Sh SYNOPSIS
 .Nm mcom
+.Op Fl Ar header Ar values\ ...
 .Op Ar recipients\ ...
 .Nm mcom
 .Fl r Op draft
 .Nm mrep
+.Op Fl Ar header Ar values\ ... Fl -
 .Ar msg
 .Nm mfwd
 .Op Fl r
+.Op Fl Ar header Ar values\ ... Fl -
 .Op Ar msgs\ ...
 .Nm mbnc
+.Op Fl Ar header Ar values\ ... Fl -
 .Ar msg
 .Sh DESCRIPTION
 .Nm mcom
@@ -56,6 +60,30 @@ See
 .Xr mmsg 7
 for the message argument syntax.
 .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
 .Bl -tag -width 2n
 .It Ic s
diff --git a/mcom b/mcom
index abd0a76..b7f9d34 100755
--- a/mcom
+++ b/mcom
@@ -68,11 +68,21 @@ 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=
 resume=
 case "$0" in
-	*mcom*)
-		if [ "$1" = -r ]; then
+*mcom*)
+	hdr=to
+	while [ $# -gt 0 ]; do
+		case "$1" in
+		--)
+			# -- is like -to, really
+			shift
+			hdr=to
+			;;
+		-r)
 			shift
 			resume=1
 			if [ "$#" -gt 0 ]; then
@@ -80,10 +90,90 @@ case "$0" in
 				draft="$1"
 				shift
 			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
 
+hdrs="$(printf '%s\n' "${hdrs#$NL}" | mhdr /dev/stdin)"
+
 outbox=$(mhdr -h outbox "$MBLAZE/profile")
 if [ -z "$outbox" ]; then
 	if [ -z "$resume" ]; then
@@ -113,27 +203,36 @@ fi
 {
 	case "$0" in
 	*mcom*)
-		printf 'To: '
-		printf '%s\n' "$@" | commajoin
-		printf '%s: \n' Cc Bcc Subject
-		from=$(mhdr -h local-mailbox "$MBLAZE/profile")
-		[ "$from" ] && printf 'From: %s\n' "$from"
-		cat "$MBLAZE/headers" 2>/dev/null
+		{
+			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 }' |
+				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
 		museragent
+		cat "$MBLAZE/headers" 2>/dev/null
 		printf '\n\n'
 		;;
 	*mfwd*)
-		raw=
-		[ "$1" = -r ] && raw=1 && shift
-		[ "$#" -eq 0 ] && set -- .
-		printf '%s: \n' To Cc Bcc
-		COLUMNS=10000 mscan -f 'Subject: [%f] %s' "$@" | sed 1q
-		from=$(mhdr -h local-mailbox "$MBLAZE/profile")
-		[ "$from" ] && printf 'From: %s\n' "$from"
-		cat "$MBLAZE/headers" 2>/dev/null
+		{
+			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:$:>:'
@@ -144,16 +243,22 @@ fi
 				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/^-/- &/'    # RFC934
+					sed 's/^-/- &/'    # RFC 934
 				printf '\n%s %s %s\n\n' \
 				       $SEP 'End forwarded message' $SEP
 			done
 		) fi
 		;;
 	*mbnc*)
-		printf '%s: \n' Resent-To
-		from=$(mhdr -h local-mailbox "$MBLAZE/profile")
-		[ "$from" ] && printf 'Resent-From: %s\n' "$from"
+		{
+			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'
 		msgid | sed 's/^/Resent-/'
 		printf 'Resent-Date: %s\n' "$(mdate)"
 		(
@@ -162,7 +267,6 @@ fi
 		)
 		;;
 	*mrep*)
-		[ "$#" -eq 0 ] && set -- .
 		ng=$(mhdr -h newsgroups "$1")
 		if [ "$ng" ]; then
 			printf 'Newsgroups: %s\n' "$ng"
@@ -173,11 +277,11 @@ fi
 			printf 'Cc: %s\n' \
 			       "$(mhdr -d -A -h to:cc: "$1" |notmine |commajoin)"
 			printf 'Bcc: \n'
+			printf '%s\n' "$hdrs" | awk '{ print }'
 		fi
 		printf 'Subject: Re: %s\n' "$(COLUMNS=10000 mscan -f '%S' "$1")"
 		from=$(mhdr -h local-mailbox "$MBLAZE/profile")
 		[ "$from" ] && printf 'From: %s\n' "$from"
-		cat "$MBLAZE/headers" 2>/dev/null
 		mid=$(mhdr -h message-id "$1")
 		if [ "$mid" ]; then
 			printf 'References:'
@@ -189,6 +293,7 @@ fi
 		fi
 		msgid
 		museragent
+		cat "$MBLAZE/headers" 2>/dev/null
 		printf '\n'
 
 		mquote "$1"