Removed possible tmp file leak.
[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 # * 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.
19 #
20 # To do:
21 # * add -header-field name:body switch
22 # * add -attach file ... switch
23
24 usage='Usage: mhmail [addrs ... [switches]]
25   switches are:
26   -b(ody) text
27   -c(c) addrs ...
28   -f(rom) addr
29   -su(bject) text
30   -r(esent)
31   -pr(ofile)
32   -se(nd)
33   -nose(nd)
34   -v(ersion)
35   -h(elp)
36   and all post(8)/send(1) switches'
37
38 bindir=`dirname $0`
39 nmhbindir=`cd "${bindir}" && pwd`
40
41 if [ $# -eq 0 ]; then
42   #### Emulate mhmail for reading mail.
43   exec "${nmhbindir}"/inc
44 else
45   #### Go through all the switches so we can build the draft.
46   tolist=
47   body=
48   bodyarg=0
49   cclist=
50   ccarg=0
51   from=
52   fromarg=0
53   subject=
54   subjectarg=0
55   resent=0
56   postsendargs=
57   switcharg=0
58   use_send=0
59   sendsw=1
60   for arg in "$@"; do
61     case "${arg}" in
62       -*) switcharg=0
63     esac
64
65     case "${arg}" in
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 ;;
72       -c|-cc) ccarg=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
78          exit 1 ;;
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
87            body="${arg}
88 "; bodyarg=0
89            #### Allow -body "" by using just a newline for the body.
90            [ "${body}"x = x ]  &&  body='
91 '
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}"
101          else
102            #### An address.
103            tolist="${tolist:+${tolist}, }${arg}"
104          fi
105     esac
106   done
107
108   #### Check for at least one address and -from.
109   if [ "${tolist}"x = x ]; then
110     printf "mhmail: usage: mhmail addrs ... [switches]\n"
111     exit 1
112   fi
113   if [ "${from}"x = x ]; then
114     nmhlibdir=`${nmhbindir}/mhparam libdir`/
115     from=`${nmhlibdir}ap -format '%(localmbox)' 0`
116   fi
117
118   #### Build header.
119   [ ${resent} -eq 0 ]  &&  prefix=  ||  prefix='Resent-'
120   header="${prefix}To: ${tolist}
121 "
122   [ "${cclist}"x = x ]  ||  header="${header}${prefix}Cc: ${cclist}
123 "
124   [ "${subject}"x = x ]  ||  header="${header}${prefix}Subject: ${subject}
125 ";
126   [ "${from}"x = x ]  ||  header="${header}${prefix}From: ${from}
127 ";
128
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.
132   umask 077
133   tmpdir="${MHTMPDIR:-${TMPDIR:-${TMP:-`${nmhbindir}/mhpath +`}}}"
134   tmpfile="${tmpdir}/mhmail$$"
135   tmpfilebackup="${tmpdir}/[,#]mhmail$$"
136   tmpfileresent=
137
138   message_file=
139   if [ ${resent} -eq 0 ]; then
140     #### Add blank line after header if not resending.
141     header="${header}
142 "
143     message_file="${tmpfile}"
144   else
145     if [ ${use_send} -eq 0 ]; then
146       postsendargs="${postsendargs:+${postsendargs} }-dist"
147       message_file="${tmpfile}"
148     else
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
157     fi
158   fi
159
160   trap 'rm -f '"${tmpfile}"' '"${tmpfilebackup}"' '"${tmpfileresent}" EXIT
161
162   if [ "${body}"x = x ]; then
163     #### First put message header in the file.
164     printf "%s" "${header}" >"${tmpfile}" || exit 2
165
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}"`
171
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
176       exit 1
177     fi
178
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
182     fi
183   else
184     #### Add trailing newline to body if it doesn't have one.
185     [ `printf "${body}" | tail -n 1 | wc -l` -ne 1 ]  &&  body="${body}
186 "
187
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
193     else
194       #### Put message header and body in the file.
195       printf "%s" "${header}${body}" >"${tmpfile}" || exit 2
196     fi
197   fi
198
199   if [ ${sendsw} -eq 0 ]; then
200     cat "${tmpfile}"
201   else
202     if [ ${use_send} -eq 0 ]; then
203       post_or_send=`${nmhbindir}/mhparam postproc`
204     else
205       post_or_send="${nmhbindir}/send"
206     fi
207
208     if "${post_or_send}" "${tmpfile}" ${postsendargs}; then
209       exit
210     else
211       printf "Letter saved in dead.letter\n"
212       #### exec skips the trap set above.
213       [ "${tmpfileresent}" ]  &&  rm -f "${tmpfileresent}"
214       exec mv "${tmpfile}" dead.letter
215     fi
216   fi
217 fi