# 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, -header-field, and -attach options.
# * 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:
-# * add support for undocumented -resent compiled mhmail switch
-# * add -attach file ... switch
usage='Usage: mhmail [addrs ... [switches]]
switches are:
+ -at(tach) file [-at(tach) file] ...
-b(ody) text
-c(c) addrs ...
-f(rom) addr
- -s(ubject) text
+ -hea(der-field) name:field [-hea(der-field) name:field] ...
+ -su(bject) text
+ -r(esent)
-pr(ofile)
+ -se(nd)
+ -nose(nd)
-v(ersion)
- -h(elp)
- -d(ebug)
+ -hel(p)
and all post(8)/send(1) switches'
bindir=`dirname $0`
else
#### Go through all the switches so we can build the draft.
tolist=
+ attacharg=0
+ attach_send_switch_added=0
body=
bodyarg=0
cclist=
ccarg=0
from=
fromarg=0
+ headerfieldlist=
+ headerfieldarg=0
subject=
subjectarg=0
+ resent=0
postsendargs=
switcharg=0
use_send=0
- debug=
+ sendsw=1
for arg in "$@"; do
case "${arg}" in
- -*) switcharg=0
+ -*) switcharg=0; headerfieldarg=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 or -c. 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, -c, or -r. 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)
+ 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 Nmh-Attachment"
+ attach_send_switch_added=1
+ fi
+ ;;
-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 ;;
+ -hea|-head|-heade|-header|-header-|-header-f|-header-fi|-header-fie|-header-fiel|-header-field) headerfieldarg=1 ;;
+ -hel|-help) printf "%s\n" "${usage}"; exit ;;
+ -nose|-nosen|-nosend) sendsw=0 ;;
-p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
- -s|-su|-sub|-subj|-subje|-subjec|-subject) subjectarg=1 ;;
+ -resend) printf "mhmail: did you mean -resent instead of -resend?\n" 1>&2
+ exit 1 ;;
+ -r|-re|-res|-rese|-resen|-resent) resent=1 ;;
+ -se|-sen|-send) sendsw=1 ;;
+ -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
+"
+ 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
+ from="${arg}"
+ fromarg=0
elif [ ${subjectarg} -eq 1 ]; then
- subject="${arg}"; subjectarg=0
+ subject="${arg}"
+ subjectarg=0
+ elif [ ${attacharg} -eq 1 ]; then
+ headerfieldlist="${headerfieldlist:+${headerfieldlist}}\
+Nmh-Attachment: ${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 "${arg}" | sed -e 's/:/: /' -e 's/: /: /'`
+ headerfieldlist="${headerfieldlist:+${headerfieldlist}}${add}
+"
+ headerfieldarg=0
elif [ ${switcharg} -eq 1 ]; then
postsendargs="${postsendargs:+${postsendargs} }${arg}"
elif [ ${ccarg} -eq 1 ]; then
else
#### An address.
tolist="${tolist:+${tolist}, }${arg}"
- fi
+ fi ;;
esac
done
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
#### Build header.
- header="To: ${tolist}
+ [ ${resent} -eq 0 ] && prefix= || prefix='Resent-'
+ header="${prefix}To: ${tolist}
"
- [ "${cclist}"x = x ] || header="${header}Cc: ${cclist}
+ [ "${cclist}"x = x ] || header="${header}${prefix}Cc: ${cclist}
"
- [ "${subject}"x = x ] || header="${header}Subject: ${subject}
+ [ "${subject}"x = x ] || header="${header}${prefix}Subject: ${subject}
";
- [ "${from}"x = x ] || header="${header}From: ${from}
+ [ "${from}"x = x ] || header="${header}${prefix}From: ${from}
";
- header="${header}
-"
+
+ if [ "${headerfieldlist}" ]; then
+ header="${header}${headerfieldlist}";
+ fi
#### 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
+ 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
+ #### 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. cat >> handles blank
- #### lines better than body=`cat`.
- printf "%s" "${header}" > "${tmpfil}" || exit 1
+ #### First put message header in the file.
+ printf "%s" "${header}" >"${tmpfile}" || exit 2
- tmpfile_size_before=`wc -c "${tmpfil}"`
- #### Now grab the body from stdin.
- cat >>"${tmpfil}"
- tmpfile_size_after=`wc -c "${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.
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 "${body}" | tail -n 1 | wc -l` -ne 1 ] && body="${body}
"
- else
- #### Put message header and body in the file.
- printf "%s" "${header}${body}" > "${tmpfil}" || exit 1
- fi
- if [ "${debug}" ]; then
- printf "%s:\n" `ls -1 "${tmpfil}"`
- cat "${tmpfil}"
+ 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 "${body}" >"${tmpfileresent}" || exit 2
+ else
+ #### Put message header and body in the file.
+ printf "%s" "${header}${body}" >"${tmpfile}" || exit 2
+ fi
fi
- if [ "$use_send" -eq 0 ]; then
- post_or_send=`${nmhbindir}/mhparam postproc`
+ if [ ${sendsw} -eq 0 ]; then
+ cat "${tmpfile}"
else
- post_or_send="${nmhbindir}/send"
- fi
+ 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
- :
- else
- printf "Letter saved in dead.letter\n"
- #### exec skips the trap set above.
- exec mv "${tmpfil}" dead.letter
+ if "${post_or_send}" "${tmpfile}" ${postsendargs}; then
+ exit
+ else
+ printf "Letter saved in dead.letter\n"
+ #### exec skips the trap set above.
+ [ "${tmpfileresent}" ] && rm -f "${tmpfileresent}"
+ exec mv "${tmpfile}" dead.letter
+ fi
fi
fi