X-Git-Url: http://git.marmaro.de/?a=blobdiff_plain;f=uip%2Fmhmail;h=afe633f842c9edaf92d69ce480ca5eac8a4a3905;hb=0d33f1e27c4edb7290ef51cd03d50c0dbc9059ef;hp=30fbbb7a3dcd969ff8ef1b5066ab45653350aef7;hpb=8ba186801a31551de4f12ffd26b617a02b289d32;p=mmh diff --git a/uip/mhmail b/uip/mhmail index 30fbbb7..afe633f 100755 --- a/uip/mhmail +++ b/uip/mhmail @@ -7,194 +7,288 @@ # complete copyright information. # # Emulation of compiled mhmail(1), with these differences: -# * Instead of silently not sending an empty message, notifies user -# "mhmail: empty message not sent, use -body '' to force." -# * The compiled mhmail dropped a trailing newline from the -body argument. +# * Adds -send/-nosend, -headerfield, and -attach options. +# * Adds optional -to switch for recipient addresses. # * Supports all post(8) (by default, without -profile) or send(1) # (with -profile) options. # * Optionally (with -profile) obeys the users profile, including # AliasFile and send entries. -# * Adds -debug option for debugging (sending, not incorporating new mail). +# * Instead of silently not sending an empty message, notifies user +# "mhmail: empty message not sent, use -body '' to force." +# * The compiled mhmail dropped a trailing newline from the -body argument. # * Drops support for undocumented -queue option. -# -# To do: -# * fix -resent with -profile -# * rename -debug to -nosend and add -send (and note in doc that last one takes) -# * add -attach file ... switch -usage='Usage: mhmail [addrs ... [switches]] +usage='Usage: mhmail [-t(o)] addrs ... [switches] switches are: + -at(tach) file [-at(tach) file] ... -b(ody) text -c(c) addrs ... -f(rom) addr - -s(ubject) text + -hea(derfield) name:value [-hea(derfield) name:value] ... + -su(bject) text -r(esent) -pr(ofile) + -se(nd) + -nose(nd) -v(ersion) - -h(elp) - -d(ebug) - and all post(8)/send(1) switches' + -hel(p) + and all post(8)/send(1) switches + mhmail with no arguments is equivalent to inc' + + +#### Find location of a program. Bourne shell just puts the name in +#### $0 if it's found from the PATH, so search that if necessary. +finddir() { + case $1 in + */*) dirname "$1" ;; + * ) IFS=: + for d in $PATH; do + [ -f "${d:=.}/$1" -a -x "$d/$1" ] && printf %s "$d" && break + done ;; + esac +} -bindir=`dirname $0` -nmhbindir=`cd "${bindir}" && pwd` +bindir=`finddir $0` +nmhbindir=`cd "$bindir" && pwd` + + +#### Checks for missing mandatory arguments. +checkforargs() { + if [ $attacharg -eq 1 ]; then + printf 'mhmail: missing argument to -attach\n' >&2; exit 1 + elif [ $bodyarg -eq 1 ]; then + printf 'mhmail: missing argument to -body\n' >&2; exit 1 + elif [ $ccarg -eq 1 -a "$cclist"x = x ]; then + printf 'mhmail: missing argument to -cc\n' >&2; exit 1 + elif [ $fromarg -eq 1 ]; then + printf 'mhmail: missing argument to -from\n' >&2; exit 1 + elif [ $headerfieldarg -eq 1 ]; then + printf 'mhmail: missing argument to -headerfield\n' >&2; exit 1 + elif [ $subjectarg -eq 1 ]; then + printf 'mhmail: missing argument to -subject\n' >&2; exit 1 + elif [ $toarg -eq 1 ]; then + printf 'mhmail: missing argument to -to\n' >&2; exit 1 + fi +} if [ $# -eq 0 ]; then #### Emulate mhmail for reading mail. - exec "${nmhbindir}"/inc -else - #### Go through all the switches so we can build the draft. - tolist= - body= - bodyarg=0 - cclist= - ccarg=0 - from= - fromarg=0 - subject= - subjectarg=0 - resent=0 - postsendargs= - switcharg=0 - use_send=0 - debug= - for arg in "$@"; do - case "${arg}" in - -*) switcharg=0 - esac - - case "${arg}" in - #### Post and send won't accept -f -or -s because they'd be - #### ambiguous, so no conflicts with them. And they don't have - #### -b, -c, or -r. For the new switches that compiled mhmail - #### didn't have: let -p indicate mhmail -profile, not send - #### -port, and let -d indicate mhmail -debug, not send -draft. - -b|-bo|-bod|-body) bodyarg=1 ;; - -c|-cc) ccarg=1 ;; - -d|-de|-deb|-debu|-debug) debug=echo ;; - -f|-fr|-fro|-from) fromarg=1 ;; - -h|-he|-hel|-help) printf "%s\n" "${usage}"; exit ;; - -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;; - -resend) printf "mhmail: did you mean -resent instead of -resend?\n" 1>&2 - exit 1 ;; - -r|-re|-res|-rese|-resen|-resent) resent=1 ;; - -s|-su|-sub|-subj|-subje|-subjec|-subject) subjectarg=1 ;; - -v|-ve|-ver|-vers|-versi|-versio|-version) - #### Cheat instead of using autoconf and make to fill in the version. - "${nmhbindir}"/mhpath -v | sed 's/mhpath/mhmail/'; exit ;; - -*) postsendargs="${postsendargs:+${postsendargs} }${arg}"; switcharg=1 ;; - *) if [ ${bodyarg} -eq 1 ]; then - body="${arg} -"; bodyarg=0 - #### Allow -body "" by using just a newline for the body. - [ "${body}"x = x ] && body=' + exec "$nmhbindir"/inc +fi + +#### Go through all the switches so we can build the draft. +tolist= ## To: addresses +toarg=0 ## whether currently handling -to +attacharg=0 ## whether currently handling -attach +attach_send_switch_added=0 ## whether added "-attach Nmh-Attachment" switch +attachind=Nmh-Attachment ## attachment indicator +body= ## contents of the message body +bodyarg=0 ## whether currently handling -body +cclist= ## Cc: addresses +ccarg=0 ## whether currently handling -cc +from= ## From: contents +fromarg=0 ## whether currently handling -from +headerfieldlist= ## header fields to be added to draft +headerfieldarg=0 ## whether currently handling -headerfield +mhmailswitch=0 ## whether currently handling any mhmail switch +subject= ## Subject: contents +subjectarg=0 ## whether currently handling -subject +resent=0 ## whether resending +postsendargs= ## switches to pass on to post or send +post_send_switch_arg=0 ## whether currently handling a post/send switch +use_send=0 ## use post (default) or send (-profile) +sendsw=1 ## to send (default) or not to send +for arg in "$@"; do + case $arg in + #### Post and send won't accept -f -or -s because they'd be + #### ambiguous, so no conflicts with them. And they don't have + #### -b, -c, -r, -t. For the new switches that compiled mhmail + #### didn't have: let -p indicate mhmail -profile, not send + #### -port. -send masks the send(1) -send switch. -attach + #### masks the send(1) -attach switch. + -at|-att|-atta|-attac|-attach) + mhmailswitch=1 + attacharg=1 + use_send=1 + if [ $attach_send_switch_added -eq 0 ]; then + #### Override any send -attach switch in user's profile. + postsendargs="${postsendargs:+$postsendargs }-attach $attachind" + attach_send_switch_added=1 + fi ;; + -b|-bo|-bod|-body) mhmailswitch=1; bodyarg=1 ;; + -c|-cc) mhmailswitch=1; ccarg=1 ;; + -f|-fr|-fro|-from) mhmailswitch=1; fromarg=1 ;; + -hea|-head|-heade|-header|-headerf|-headerfi|-headerfie|-headerfiel|\ +-headerfield) mhmailswitch=1; headerfieldarg=1 ;; + -hel|-help) printf '%s\n' "$usage"; exit ;; + -nose|-nosen|-nosend) mhmailswitch=1; sendsw=0 ;; + -p|-pr|-pro|-prof|-profi|-profil|-profile) mhmailswitch=1; use_send=1 ;; + -resend) printf 'mhmail: did you mean -resent instead of -resend?\n' >&2 + exit 1 ;; + -r|-re|-res|-rese|-resen|-resent) mhmailswitch=1; resent=1 ;; + -se|-sen|-send) mhmailswitch=1; sendsw=1 ;; + -su|-sub|-subj|-subje|-subjec|-subject) mhmailswitch=1; subjectarg=1 ;; + -t|-to) toarg=1; ccarg=0 ;; + -v|-ve|-ver|-vers|-versi|-versio|-version) + #### Cheat instead of using autoconf and make to fill in the version. + "$nmhbindir"/mhpath -v | sed 's/mhpath/mhmail/'; exit ;; + -*) if [ $mhmailswitch -eq 1 ]; then + checkforargs + mhmailswitch=0 + fi + post_send_switch_arg=1 + postsendargs="${postsendargs:+$postsendargs }$arg" ;; + *) mhmailswitch=0 + if [ $bodyarg -eq 1 ]; then + body="$arg +" + bodyarg=0 + #### Allow -body "" by using just a newline for the body. + [ "$body"x = x ] && body=' ' - elif [ ${fromarg} -eq 1 ]; then - from="${arg}"; fromarg=0 - elif [ ${subjectarg} -eq 1 ]; then - subject="${arg}"; subjectarg=0 - elif [ ${switcharg} -eq 1 ]; then - postsendargs="${postsendargs:+${postsendargs} }${arg}" - elif [ ${ccarg} -eq 1 ]; then - #### Never reset ccarg to 0, for compatibilty with compiled mhmail. - cclist="${cclist:+${cclist}, }${arg}" - else - #### An address. - tolist="${tolist:+${tolist}, }${arg}" - fi - esac - done - - #### Check for at least one address and -from. - if [ "${tolist}"x = x ]; then - printf "mhmail: usage: mhmail addrs ... [switches]\n" - exit 1 - fi - if [ "${from}"x = x ]; then - nmhlibdir=`${nmhbindir}/mhparam libdir`/ - #### If nmhlibdir isn't right, assume that the nmh lib dir is on the PATH. - [ -x "${nmhlibdir}ap" ] || nmhlibdir= - from=`${nmhlibdir}ap -format '%(localmbox)' 0` - fi + elif [ $fromarg -eq 1 ]; then + from="$arg" + fromarg=0 + elif [ $subjectarg -eq 1 ]; then + subject="$arg" + subjectarg=0 + elif [ $attacharg -eq 1 ]; then + headerfieldlist="${headerfieldlist:+$headerfieldlist}$attachind: $arg +" + attacharg=0 + elif [ $headerfieldarg -eq 1 ]; then + #### It's not strictly necessary to have one space after + #### the : that separates the header field name from the + #### body, but do it to avoid surprising someone. + add=`printf %s "$arg" | sed -e 's/:/: /' -e 's/: /: /'` + headerfieldlist="${headerfieldlist:+$headerfieldlist}$add +" + headerfieldarg=0 + elif [ $post_send_switch_arg -eq 1 ]; then + postsendargs="${postsendargs:+$postsendargs }$arg" + elif [ $ccarg -eq 1 ]; then + #### ccarg can only be reset to 0 by -to. + cclist="${cclist:+$cclist, }$arg" + else + #### An address. + tolist="${tolist:+$tolist, }$arg" + toarg=0 + fi ;; + esac +done + +#### Check for at least one address and -from. +if [ "$tolist"x = x ]; then + printf 'Usage: mhmail [-t(o)] addrs ... [switches]\n' >&2; exit 1 +fi +if [ "$from"x = x ]; then + nmhlibdir=`$nmhbindir/mhparam libdir`/ + from=`${nmhlibdir}ap -format '%(localmbox)' 0` +fi - #### Build header. - [ ${resent} -eq 0 ] && prefix= || prefix='Resent-' - header="${prefix}To: ${tolist} +#### Check for missing mandatory arguments. +checkforargs + +#### Build header. +[ $resent -eq 0 ] && prefix= || prefix='Resent-' +header="${prefix}To: $tolist " - [ "${cclist}"x = x ] || header="${header}${prefix}Cc: ${cclist} +[ "$cclist"x = x ] || header="$header${prefix}Cc: $cclist " - [ "${subject}"x = x ] || header="${header}${prefix}Subject: ${subject} -"; - [ "${from}"x = x ] || header="${header}${prefix}From: ${from} -"; - - #### Set up a file to supply as a draft to send/post. And set a - #### trap to remove it. send moves the file to a backup, so it will - #### remove that, too. - umask 077 - tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}" - tmpfil="${tmpdir}/mhmail$$" - tmpfilbackup="${tmpdir}/[,#]mhmail$$" - trap 'rm -f '"${tmpfil}"' '"${tmpfilbackup}" EXIT - - if [ ${resent} -eq 0 ]; then - #### Add blank line after header if not resending. - header="${header} +[ "$subject"x = x ] || header="$header${prefix}Subject: $subject " - else - if [ "$use_send" -eq 0 ]; then - postsendargs="${postsendargs:+${postsendargs} }-dist" - else - mhdist=1; export mhdist - mhaltmsg=${tmpfil}; export mhaltmsg - #### This doesn't work, I'm not sure about mhaltmsg. - printf "mhmail: -resent not currently supported with -profile\n" - exit 1 - fi - fi +[ "$from"x = x ] || header="$header${prefix}From: $from +" + +if [ "$headerfieldlist" ]; then + header="$header$headerfieldlist" +fi - if [ "${body}"x = x ]; then - #### First put message header in the file. cat >> handles blank - #### lines better than body=`cat`. - printf "%s" "${header}" > "${tmpfil}" || exit 1 - - tmpfile_size_before=`wc -c "${tmpfil}"` - #### Now grab the body from stdin. - cat >>"${tmpfil}" - tmpfile_size_after=`wc -c "${tmpfil}"` - - #### Don't allow an empty body (from stdin). Use string - #### comparison so we don't have to strip the filename, etc. - if [ "${tmpfile_size_before}" = "${tmpfile_size_after}" ]; then - printf "mhmail: empty message not sent, use -body '' to force.\n" 1>&2 - exit 1 - fi - - #### Add trailing newline to body if it doesn't have one. - [ `printf "${body}" | tail -n 1 | wc -l` -ne 1 ] && body="${body} +#### Set up a file to supply as a draft to post/send. And set a +#### trap to remove it. send moves the file to a backup, so it will +#### remove that, too. +umask 077 +tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`$nmhbindir/mhpath +`}}}" +tmpfile="$tmpdir/mhmail$$" +tmpfilebackup="$tmpdir/[,#]mhmail$$" +tmpfileresent= + +message_file= +if [ $resent -eq 0 ]; then + #### Add blank line after header if not resending. + header="$header " + message_file="$tmpfile" +else + if [ $use_send -eq 0 ]; then + postsendargs="${postsendargs:+$postsendargs }-dist" + message_file="$tmpfile" else - #### Put message header and body in the file. - printf "%s" "${header}${body}" > "${tmpfil}" || exit 1 + #### When resending with send, tmpfile will just contain the + #### Resent- header fields. "$tmpfileresent" will contain + #### the message that is being resent. + tmpfileresent="$tmpdir/mhmail-resent$$" + mhdist=1; export mhdist + mhaltmsg=$tmpfileresent; export mhaltmsg + message_file="$tmpfileresent" + printf '' >"$message_file" || exit 2 fi +fi + +trap 'rm -f '"$tmpfile"' '"$tmpfilebackup"' '"$tmpfileresent" EXIT + +if [ "$body"x = x ]; then + #### First put message header in the file. + printf %s "$header" >"$tmpfile" || exit 2 - if [ "${debug}" ]; then - printf "%s:\n" `ls -1 "${tmpfil}"` - cat "${tmpfil}" + tmpfile_size_before=`wc -c "$message_file"` + #### Now grab the body from stdin. cat >> handles blank lines + #### better than body=`cat`. + cat >>"$message_file" || exit 2 + tmpfile_size_after=`wc -c "$message_file"` + + #### Don't allow an empty body (from stdin). Use string + #### comparison so we don't have to strip the filename, etc. + if [ "$tmpfile_size_before" = "$tmpfile_size_after" ]; then + printf 'mhmail: empty message not sent, use -body '"''"' to force.\n' >&2 + exit 1 + fi + + #### Add trailing newline to body if it doesn't have one. + if [ `tail -n 1 "$message_file" | wc -l` -ne 1 ]; then + printf '\n' >>"$message_file" || exit 2 fi +else + #### Add trailing newline to body if it doesn't have one. + [ `printf %s "$body" | tail -n 1 | wc -l` -ne 1 ] && body="$body +" - if [ "$use_send" -eq 0 ]; then - post_or_send=`${nmhbindir}/mhparam postproc` + if [ "$tmpfileresent" ]; then + #### Put just the new message header in the file. + printf %s "$header" >"$tmpfile" || exit 2 + #### and the body in the file to resend. + printf %s "$body" >"$tmpfileresent" || exit 2 else - post_or_send="${nmhbindir}/send" + #### Put message header and body in the file. + printf %s "$header$body" >"$tmpfile" || exit 2 fi +fi - status=$? +if [ $sendsw -eq 0 ]; then + cat "$tmpfile" +else + if [ $use_send -eq 0 ]; then + post_or_send=`$nmhbindir/mhparam postproc` + else + post_or_send="$nmhbindir/send" + fi - if $debug "${post_or_send}" "${tmpfil}" ${postsendargs}; then - exit ${status} + if "$post_or_send" "$tmpfile" $postsendargs; then + exit else - printf "Letter saved in dead.letter\n" - #### exec skips the trap set above. - exec mv "${tmpfil}" dead.letter + status=$? + mv -f "$tmpfile" dead.letter + printf 'Letter saved in dead.letter\n' >&2 + exit $status fi fi