From 1771a8d23bed76c9c636d07142af87de844d1d8b Mon Sep 17 00:00:00 2001 From: Alexander Arkhipov Date: Tue, 29 Mar 2022 13:18:48 +0300 Subject: [PATCH] contrib: add mco/mfw/mbn, a more interactive wrapper for mcom/mfwd/mbnc --- contrib/mco | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100755 contrib/mco diff --git a/contrib/mco b/contrib/mco new file mode 100755 index 0000000..23e0763 --- /dev/null +++ b/contrib/mco @@ -0,0 +1,66 @@ +#!/bin/sh + +# mco, mfw and mbn are slightly more interactive variants of mcom(1), mfwd(1) +# and mbnc(1). There is no need for an equivalent of mrep(1) because it already +# inserts all the necessary headers. These scripts read aliases from +# ${MBLAZE:-$HOME/.mblaze}/aliases, which is similar in structure to profile. +# That is on the left side of the colon is an alias, and on the right its actual +# value. Note that *all* values passed to the "To" prompt are comma-separated +# with optional whitespace after the comma. That includes the optional "-cc" and +# "-bcc". "-resent-cc" and "-resent-bcc" are not recognised, instead if the +# script is called as mbn, -cc and -bcc are automatically converted to their +# -resent-* equivalents. Each time there may be only a single "-cc" and a single +# "-bcc" further ones are rejected. Should I state that the values the "To" +# prompt accepts prior to "-*cc" go to To, while the values after the respective +# options to Cc and Bcc? +# +# To use these scripts place mco somewhere in the $PATH and then symlink mbn and +# mfw to it. + +ALIASES="${MBLAZE:-$HOME/.mlbaze}/aliases" +SELF="${0##*/}" + +IFS=' +' +printf %s 'From: '; read -r FROM +printf %s 'To (comma-separated + one or less of each: -cc, -bcc): '; read -r TO +[ "$SELF" = mco ] && { printf %s 'Subject: '; read -r SUBJECT; } + +set -- +[ "$SELF" != "mbn" ] && set -- "$@" "-from" || set -- "$@" "-resent-from" +temp="$(mhdr -h "$(printf "$FROM")" "$ALIASES")" +[ -n "$temp" ] && set -- "$@" "$temp" || set -- "$@" "$FROM" +[ "$SELF" = "mco" ] && set -- "$@" "-subject" && set -- "$@" "$SUBJECT" + +IFS=, +CC= +BCC= +[ "$SELF" != "mbn" ] && set -- "$@" "-to" || set -- "$@" "-resent-to" +for i in $TO; do + # Forgive whitespace after comma. + i="$(printf %s "$i" | sed 's/^[[:blank:]]*//')" + if [ "$i" = "-cc" ]; then + if [ -z "$CC" ]; then + CC=1 + [ "$SELF" != "mbn" ] && set -- "$@" "-cc" || + set -- "$@" "-resent-cc" + fi + elif [ "$i" = "-bcc" ]; then + if [ -z "$BCC" ]; then + BCC=1 + [ "$SELF" != "mbn" ] && set -- "$@" "-bcc" || + set -- "$@" "-resent-bcc" + fi + else + temp="$(mhdr -h "$(printf "$i")" "$ALIASES")" + [ -n "$temp" ] && set -- "$@" "$temp" || set "$@" "$i" + fi +done + +case "$SELF" in +mco) CMD=mcom ;; +mbn) CMD=mbnc ;; +mfw) CMD=mfwd ;; +*) CMD=mcom ;; +esac +"$CMD" "$@"