If post or send fail, exit with their exit status.
[mmh] / uip / mhmail
1 #! /bin/sh
2 #
3 # mhmail -- simple mail program
4 #
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.
8 #
9 # Emulation of compiled mhmail(1), with these differences:
10 # * Adds -send/-nosend, -headerfield, and -attach options.
11 # * Adds optional -to switch for recipient addresses.
12 # * Supports all post(8) (by default, without -profile) or send(1)
13 #   (with -profile) options.
14 # * Optionally (with -profile) obeys the users profile, including
15 #   AliasFile and send entries.
16 # * Instead of silently not sending an empty message, notifies user
17 #   "mhmail: empty message not sent, use -body '' to force."
18 # * The compiled mhmail dropped a trailing newline from the -body argument.
19 # * Drops support for undocumented -queue option.
20
21 usage='Usage: mhmail [-t(o)] addrs ... [switches]
22   switches are:
23   -at(tach) file [-at(tach) file] ...
24   -b(ody) text
25   -c(c) addrs ...
26   -f(rom) addr
27   -hea(derfield) name:value [-hea(derfield) name:value] ...
28   -su(bject) text
29   -r(esent)
30   -pr(ofile)
31   -se(nd)
32   -nose(nd)
33   -v(ersion)
34   -hel(p)
35   and all post(8)/send(1) switches
36   mhmail with no arguments is equivalent to inc'
37
38 bindir=`dirname $0`
39 nmhbindir=`cd "${bindir}" && pwd`
40
41 #### Checks for missing mandatory arguments.
42 checkforargs() {
43   if [ ${attacharg} -eq 1 ]; then
44     printf "mhmail: missing argument to -attach\n"; exit 1
45   elif [ ${bodyarg} -eq 1 ]; then
46     printf "mhmail: missing argument to -body\n"; exit 1
47   elif [ ${ccarg} -eq 1  -a  "${cclist}"x = x ]; then
48     printf "mhmail: missing argument to -cc\n"; exit 1
49   elif [ ${fromarg} -eq 1 ]; then
50     printf "mhmail: missing argument to -from\n"; exit 1
51   elif [ ${headerfieldarg} -eq 1 ]; then
52     printf "mhmail: missing argument to -headerfield\n"; exit 1
53   elif [ ${subjectarg} -eq 1 ]; then
54     printf "mhmail: missing argument to -subject\n"; exit 1
55   elif [ ${toarg} -eq 1 ]; then
56     printf "mhmail: missing argument to -to\n"; exit 1
57   fi
58 }
59
60 if [ $# -eq 0 ]; then
61   #### Emulate mhmail for reading mail.
62   exec "${nmhbindir}"/inc
63 else
64   #### Go through all the switches so we can build the draft.
65   tolist=                     ## To: addresses
66   toarg=0                     ## whether currently handling -to
67   attacharg=0                 ## whether currently handling -attach
68   attach_send_switch_added=0  ## whether added "-attach Nmh-Attachment" switch
69   body=                       ## contents of the message body
70   bodyarg=0                   ## whether currently handling -body
71   cclist=                     ## Cc: addresses
72   ccarg=0                     ## whether currently handling -cc
73   from=                       ## From: contents
74   fromarg=0                   ## whether currently handling -from
75   headerfieldlist=            ## header fields to be added to draft
76   headerfieldarg=0            ## whether currently handling -headerfield
77   mhmailswitch=0              ## whether currently handling any mhmail switch
78   subject=                    ## Subject: contents
79   subjectarg=0                ## whether currently handling -subject
80   resent=0                    ## whether resending
81   postsendargs=               ## switches to pass on to post or send
82   post_send_switch_arg=0      ## whether currently handling a post/send switch
83   use_send=0                  ## use post (default) or send (-profile)
84   sendsw=1                    ## to send (default) or not to send
85   for arg in "$@"; do
86     case "${arg}" in
87       #### Post and send won't accept -f -or -s because they'd be
88       #### ambiguous, so no conflicts with them.  And they don't have
89       #### -b, -c, -r, -t.  For the new switches that compiled mhmail
90       #### didn't have:  let -p indicate mhmail -profile, not send
91       #### -port.  -send masks the send(1) -send switch.  -attach
92       #### masks the send(1) -attach switch.
93       -at|-att|-atta|-attac|-attach)
94          mhmailswitch=1
95          attacharg=1
96          use_send=1
97          if [ ${attach_send_switch_added} -eq 0 ]; then
98            #### Override any send -attach switch in user's profile.
99            postsendargs=\
100 "${postsendargs:+${postsendargs} }-attach Nmh-Attachment"
101            attach_send_switch_added=1
102          fi ;;
103       -b|-bo|-bod|-body) mhmailswitch=1; bodyarg=1 ;;
104       -c|-cc) mhmailswitch=1; ccarg=1 ;;
105       -f|-fr|-fro|-from) mhmailswitch=1; fromarg=1 ;;
106       -hea|-head|-heade|-header|-headerf|-headerfi|-headerfie|-headerfiel|\
107 -headerfield) mhmailswitch=1; headerfieldarg=1 ;;
108       -hel|-help) printf "%s\n" "${usage}"; exit ;;
109       -nose|-nosen|-nosend) mhmailswitch=1; sendsw=0 ;;
110       -p|-pr|-pro|-prof|-profi|-profil|-profile) mhmailswitch=1; use_send=1 ;;
111       -resend) printf "mhmail: did you mean -resent instead of -resend?\n" 1>&2
112          exit 1 ;;
113       -r|-re|-res|-rese|-resen|-resent) mhmailswitch=1; resent=1 ;;
114       -se|-sen|-send) mhmailswitch=1; sendsw=1 ;;
115       -su|-sub|-subj|-subje|-subjec|-subject) mhmailswitch=1; subjectarg=1 ;;
116       -t|-to) toarg=1; ccarg=0 ;;
117       -v|-ve|-ver|-vers|-versi|-versio|-version)
118          #### Cheat instead of using autoconf and make to fill in the version.
119          "${nmhbindir}"/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
120       -*) if [ ${mhmailswitch} -eq 1 ]; then
121             checkforargs
122             mhmailswitch=0
123           fi
124           post_send_switch_arg=1
125           postsendargs="${postsendargs:+${postsendargs} }${arg}" ;;
126       *) mhmailswitch=0
127          if [ ${bodyarg} -eq 1 ]; then
128            body="${arg}
129 "
130            bodyarg=0
131            #### Allow -body "" by using just a newline for the body.
132            [ "${body}"x = x ]  &&  body='
133 '
134          elif [ ${fromarg} -eq 1 ]; then
135            from="${arg}"
136            fromarg=0
137          elif [ ${subjectarg} -eq 1 ]; then
138            subject="${arg}"
139            subjectarg=0
140          elif [ ${attacharg} -eq 1 ]; then
141            headerfieldlist="${headerfieldlist:+${headerfieldlist}}\
142 Nmh-Attachment: ${arg}
143 "
144            attacharg=0
145          elif [ ${headerfieldarg} -eq 1 ]; then
146            #### It's not strictly necessary to have one space after
147            #### the : that separates the header field name from the
148            #### body, but do it to avoid surprising someone.
149            add=`printf "${arg}" | sed -e 's/:/: /' -e 's/:  /: /'`
150            headerfieldlist="${headerfieldlist:+${headerfieldlist}}${add}
151 "
152            headerfieldarg=0
153          elif [ ${post_send_switch_arg} -eq 1 ]; then
154            postsendargs="${postsendargs:+${postsendargs} }${arg}"
155          elif [ ${ccarg} -eq 1 ]; then
156            #### ccarg can only be reset to 0 by -to.
157            cclist="${cclist:+${cclist}, }${arg}"
158          else
159            #### An address.
160            tolist="${tolist:+${tolist}, }${arg}"
161            toarg=0
162          fi ;;
163     esac
164   done
165
166   #### Check for at least one address and -from.
167   if [ "${tolist}"x = x ]; then
168     printf "mhmail: usage: mhmail addrs ... [switches]\n"; exit 1
169   fi
170   if [ "${from}"x = x ]; then
171     nmhlibdir=`${nmhbindir}/mhparam libdir`/
172     from=`${nmhlibdir}ap -format '%(localmbox)' 0`
173   fi
174
175   #### Check for missing mandatory arguments.
176   checkforargs
177
178   #### Build header.
179   [ ${resent} -eq 0 ]  &&  prefix=  ||  prefix='Resent-'
180   header="${prefix}To: ${tolist}
181 "
182   [ "${cclist}"x = x ]  ||  header="${header}${prefix}Cc: ${cclist}
183 "
184   [ "${subject}"x = x ]  ||  header="${header}${prefix}Subject: ${subject}
185 "
186   [ "${from}"x = x ]  ||  header="${header}${prefix}From: ${from}
187 "
188
189   if [ "${headerfieldlist}" ]; then
190     header="${header}${headerfieldlist}"
191   fi
192
193   #### Set up a file to supply as a draft to post/send.  And set a
194   #### trap to remove it.  send moves the file to a backup, so it will
195   #### remove that, too.
196   umask 077
197   tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}"
198   tmpfile="${tmpdir}/mhmail$$"
199   tmpfilebackup="${tmpdir}/[,#]mhmail$$"
200   tmpfileresent=
201
202   message_file=
203   if [ ${resent} -eq 0 ]; then
204     #### Add blank line after header if not resending.
205     header="${header}
206 "
207     message_file="${tmpfile}"
208   else
209     if [ ${use_send} -eq 0 ]; then
210       postsendargs="${postsendargs:+${postsendargs} }-dist"
211       message_file="${tmpfile}"
212     else
213       #### When resending with send, tmpfile will just contain the
214       #### Resent- header fields.  "${tmpfileresent}" will contain
215       #### the message that is being resent.
216       tmpfileresent="${tmpdir}/mhmail-resent$$"
217       mhdist=1; export mhdist
218       mhaltmsg=${tmpfileresent}; export mhaltmsg
219       message_file="${tmpfileresent}"
220       printf "" >"${message_file}"  || exit 2
221     fi
222   fi
223
224   trap 'rm -f '"${tmpfile}"' '"${tmpfilebackup}"' '"${tmpfileresent}" EXIT
225
226   if [ "${body}"x = x ]; then
227     #### First put message header in the file.
228     printf "%s" "${header}" >"${tmpfile}" || exit 2
229
230     tmpfile_size_before=`wc -c "${message_file}"`
231     #### Now grab the body from stdin.  cat >> handles blank lines
232     #### better than body=`cat`.
233     cat >>"${message_file}" || exit 2
234     tmpfile_size_after=`wc -c "${message_file}"`
235
236     #### Don't allow an empty body (from stdin).  Use string
237     #### comparison so we don't have to strip the filename, etc.
238     if [ "${tmpfile_size_before}" = "${tmpfile_size_after}" ]; then
239       printf "mhmail: empty message not sent, use -body '' to force.\n" 1>&2
240       exit 1
241     fi
242
243     #### Add trailing newline to body if it doesn't have one.
244     if [ `tail -n 1 "${message_file}" | wc -l` -ne 1 ]; then
245       printf "\n" >>"${message_file}" || exit 2
246     fi
247   else
248     #### Add trailing newline to body if it doesn't have one.
249     [ `printf "${body}" | tail -n 1 | wc -l` -ne 1 ]  &&  body="${body}
250 "
251
252     if [ "${tmpfileresent}" ]; then
253       #### Put just the new message header in the file.
254       printf "%s" "${header}" >"${tmpfile}" || exit 2
255       #### and the body in the file to resend.
256       printf "${body}" >"${tmpfileresent}" || exit 2
257     else
258       #### Put message header and body in the file.
259       printf "%s" "${header}${body}" >"${tmpfile}" || exit 2
260     fi
261   fi
262
263   if [ ${sendsw} -eq 0 ]; then
264     cat "${tmpfile}"
265   else
266     if [ ${use_send} -eq 0 ]; then
267       post_or_send=`${nmhbindir}/mhparam postproc`
268     else
269       post_or_send="${nmhbindir}/send"
270     fi
271
272     if "${post_or_send}" "${tmpfile}" ${postsendargs}; then
273       exit
274     else
275       status=$?
276       mv -f "${tmpfile}" dead.letter
277       printf "Letter saved in dead.letter\n"
278       exit $status
279     fi
280   fi
281 fi