]>
diplodocus.org Git - nmh/blob - uip/mhmail
3 # mhmail -- simple mail program
5 # This code is Copyright (c) 2012, by the authors of nmh. See the
6 # COPYRIGHT file in the root directory of the nmh distribution for
7 # complete copyright information.
9 # Emulation of compiled mhmail(1), with these differences:
10 # * Instead of silently not sending an empty message, notifies user
11 # "mhmail: empty message not sent, use -body '' to force."
12 # * The compiled mhmail dropped a trailing newline from the -body argument.
13 # * Supports all post(8) (by default, without -profile) or send(1)
14 # (with -profile) options.
15 # * Optionally (with -profile) obeys the users profile, including
16 # AliasFile and send entries.
17 # * Adds -nosend option, which displays the draft but does not send it.
18 # * Drops support for undocumented -queue option.
21 # * add -header-field name:body switch
22 # * add -attach file ... switch
24 usage
='Usage: mhmail [addrs ... [switches]]
36 and all post(8)/send(1) switches'
39 nmhbindir
=`cd "${bindir}" && pwd`
42 #### Emulate mhmail for reading mail.
43 exec "${nmhbindir}"/inc
45 #### Go through all the switches so we can build the draft.
66 #### Post and send won't accept -f -or -s because they'd be
67 #### ambiguous, so no conflicts with them. And they don't have
68 #### -b, -c, or -r. For the new switches that compiled mhmail
69 #### didn't have: let -p indicate mhmail -profile, not send
70 #### -port. -send masks the send(1) -send switch.
71 -b|-bo
|-bod
|-body
) bodyarg
=1 ;;
73 -f|-fr
|-fro
|-from
) fromarg
=1 ;;
74 -h|-he
|-hel
|-help) printf "%s\n" "${usage}"; exit ;;
75 -nose|-nosen
|-nosend
) sendsw
=0 ;;
76 -p|-pr|-pro
|-prof
|-profi
|-profil
|-profile
) use_send
=1 ;;
77 -resend) printf "mhmail: did you mean -resent instead of -resend?\n" 1>&2
79 -r|-re
|-res
|-rese
|-resen
|-resent
) resent
=1 ;;
80 -se|-sen
|-send
) sendsw
=1 ;;
81 -su|-sub
|-subj
|-subje
|-subjec
|-subject
) subjectarg
=1 ;;
82 -v|-ve
|-ver
|-vers
|-versi
|-versio
|-version
)
83 #### Cheat instead of using autoconf and make to fill in the version.
84 "${nmhbindir}"/mhpath
-v | sed 's/mhpath/mhmail/'; exit ;;
85 -*) postsendargs
="${postsendargs:+${postsendargs} }${arg}"; switcharg
=1 ;;
86 *) if [ ${bodyarg} -eq 1 ]; then
89 #### Allow -body "" by using just a newline for the body.
90 [ "${body}"x
= x
] && body
='
92 elif [ ${fromarg} -eq 1 ]; then
93 from
="${arg}"; fromarg
=0
94 elif [ ${subjectarg} -eq 1 ]; then
95 subject
="${arg}"; subjectarg
=0
96 elif [ ${switcharg} -eq 1 ]; then
97 postsendargs
="${postsendargs:+${postsendargs} }${arg}"
98 elif [ ${ccarg} -eq 1 ]; then
99 #### Never reset ccarg to 0, for compatibilty with compiled mhmail.
100 cclist
="${cclist:+${cclist}, }${arg}"
103 tolist
="${tolist:+${tolist}, }${arg}"
108 #### Check for at least one address and -from.
109 if [ "${tolist}"x
= x
]; then
110 printf "mhmail: usage: mhmail addrs ... [switches]\n"
113 if [ "${from}"x
= x
]; then
114 nmhlibdir
=`${nmhbindir}/mhparam libdir`/
115 from
=`${nmhlibdir}ap -format '%(localmbox)' 0`
119 [ ${resent} -eq 0 ] && prefix
= || prefix
='Resent-'
120 header
="${prefix}To: ${tolist}
122 [ "${cclist}"x
= x
] || header
="${header}${prefix}Cc: ${cclist}
124 [ "${subject}"x
= x
] || header
="${header}${prefix}Subject: ${subject}
126 [ "${from}"x
= x
] || header
="${header}${prefix}From: ${from}
129 #### Set up a file to supply as a draft to send/post. And set a
130 #### trap to remove it. send moves the file to a backup, so it will
131 #### remove that, too.
133 tmpdir
="${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}"
134 tmpfile
="${tmpdir}/mhmail$$"
135 tmpfilebackup
="${tmpdir}/[,#]mhmail$$"
139 if [ ${resent} -eq 0 ]; then
140 #### Add blank line after header if not resending.
143 message_file
="${tmpfile}"
145 if [ ${use_send} -eq 0 ]; then
146 postsendargs
="${postsendargs:+${postsendargs} }-dist"
147 message_file
="${tmpfile}"
149 #### When resending with send, tmpfile will just contain the
150 #### Resent- header fields. "${tmpfileresent}" will contain
151 #### the message that is being resent.
152 tmpfileresent
="${tmpdir}/mhmail-resent$$"
153 mhdist
=1; export mhdist
154 mhaltmsg
=${tmpfileresent}; export mhaltmsg
155 message_file
="${tmpfileresent}"
156 printf "" >"${message_file}" || exit 2
160 trap 'rm -f '"${tmpfile}"' '"${tmpfilebackup}"' '"${tmpfileresent}" EXIT
162 if [ "${body}"x
= x
]; then
163 #### First put message header in the file.
164 printf "%s" "${header}" >"${tmpfile}" || exit 2
166 tmpfile_size_before
=`wc -c "${message_file}"`
167 #### Now grab the body from stdin. cat >> handles blank lines
168 #### better than body=`cat`.
169 cat >>"${message_file}" || exit 2
170 tmpfile_size_after
=`wc -c "${message_file}"`
172 #### Don't allow an empty body (from stdin). Use string
173 #### comparison so we don't have to strip the filename, etc.
174 if [ "${tmpfile_size_before}" = "${tmpfile_size_after}" ]; then
175 printf "mhmail: empty message not sent, use -body '' to force.\n" 1>&2
179 #### Add trailing newline to body if it doesn't have one.
180 if [ `tail -n 1 "${message_file}" | wc -l` -ne 1 ]; then
181 printf "\n" >>"${message_file}" || exit 2
184 #### Add trailing newline to body if it doesn't have one.
185 [ `printf "${body}" | tail -n 1 | wc -l` -ne 1 ] && body
="${body}
188 if [ "${tmpfileresent}" ]; then
189 #### Put just the new message header in the file.
190 printf "%s" "${header}" >"${tmpfile}" || exit 2
191 #### and the body in the file to resend.
192 printf "${body}" >"${tmpfileresent}" || exit 2
194 #### Put message header and body in the file.
195 printf "%s" "${header}${body}" >"${tmpfile}" || exit 2
199 if [ ${sendsw} -eq 0 ]; then
202 if [ ${use_send} -eq 0 ]; then
203 post_or_send
=`${nmhbindir}/mhparam postproc`
205 post_or_send
="${nmhbindir}/send"
208 if "${post_or_send}" "${tmpfile}" ${postsendargs}; then
211 printf "Letter saved in dead.letter\n"
212 #### exec skips the trap set above.
213 exec mv "${tmpfile}" dead.letter