From c49c0b4a56f110c4f0782cb4c541be589e1c45a1 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Tue, 27 Feb 2018 15:14:29 +0100 Subject: [PATCH] mcom: revamp argument parsing, allow setting arbitrary headers from command line --- man/mcom.1 | 28 ++++++++++ mcom | 153 ++++++++++++++++++++++++++++++++++++++++++++--------- 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 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"